src/gba/gba-audio.h (view raw)
1#ifndef GBA_AUDIO_H
2#define GBA_AUDIO_H
3
4#include "circle-buffer.h"
5
6#include <pthread.h>
7#include <stdint.h>
8
9struct GBADMA;
10
11const unsigned GBA_AUDIO_SAMPLES;
12
13struct GBAAudioEnvelope {
14 union {
15 struct {
16 unsigned length : 6;
17 unsigned duty : 2;
18 unsigned stepTime : 3;
19 unsigned direction : 1;
20 unsigned initialVolume : 4;
21 };
22 uint16_t packed;
23 };
24 int currentVolume;
25 int32_t nextStep;
26};
27
28struct GBAAudioSquareControl {
29 union {
30 struct {
31 unsigned frequency : 11;
32 unsigned : 3;
33 unsigned stop : 1;
34 unsigned restart : 1;
35 };
36 uint16_t packed;
37 };
38 int hi;
39 int currentFrequency;
40 int32_t nextStep;
41};
42
43struct GBAAudioChannel1 {
44 union GBAAudioSquareSweep {
45 struct {
46 unsigned shift : 3;
47 unsigned direction : 1;
48 unsigned time : 3;
49 unsigned : 9;
50 };
51 uint16_t packed;
52 } sweep;
53 int32_t nextSweep;
54
55 struct GBAAudioEnvelope envelope;
56 struct GBAAudioSquareControl control;
57 int8_t sample;
58};
59
60struct GBAAudioChannel2 {
61 struct GBAAudioEnvelope envelope;
62 struct GBAAudioSquareControl control;
63 int8_t sample;
64};
65
66struct GBAAudioChannel3 {
67 union {
68 struct {
69 unsigned : 5;
70 unsigned size : 1;
71 unsigned bank : 1;
72 unsigned disable : 1;
73 unsigned : 7;
74 };
75 uint16_t packed;
76 } bank;
77
78 union {
79 struct {
80 unsigned length : 8;
81 unsigned : 5;
82 unsigned volume : 3;
83 unsigned disable : 1;
84 unsigned : 7;
85 };
86 uint16_t packed;
87 } wave;
88
89 union {
90 struct {
91 unsigned rate : 11;
92 unsigned : 3;
93 unsigned stop : 1;
94 unsigned restart : 1;
95 };
96 uint16_t packed;
97 } control;
98};
99
100struct GBAAudioChannel4 {
101 struct GBAAudioEnvelope envelope;
102 union {
103 struct {
104 unsigned ratio : 3;
105 unsigned power : 1;
106 unsigned frequency : 4;
107 unsigned : 6;
108 unsigned stop : 1;
109 unsigned restart : 1;
110 };
111 uint16_t packed;
112 } control;
113
114 unsigned lfsr;
115 int8_t sample;
116};
117
118struct GBAAudioFIFO {
119 struct CircleBuffer fifo;
120 int dmaSource;
121 int8_t sample;
122};
123
124struct GBAAudio {
125 struct GBA* p;
126
127 struct GBAAudioChannel1 ch1;
128 struct GBAAudioChannel2 ch2;
129 struct GBAAudioChannel3 ch3;
130 struct GBAAudioChannel4 ch4;
131
132 struct GBAAudioFIFO chA;
133 struct GBAAudioFIFO chB;
134
135 struct CircleBuffer left;
136 struct CircleBuffer right;
137
138 union {
139 struct {
140 unsigned volumeRight : 3;
141 unsigned : 1;
142 unsigned volumeLeft : 3;
143 unsigned : 1;
144 unsigned ch1Right : 1;
145 unsigned ch2Right : 1;
146 unsigned ch3Right : 1;
147 unsigned ch4Right : 1;
148 unsigned ch1Left : 1;
149 unsigned ch2Left : 1;
150 unsigned ch3Left : 1;
151 unsigned ch4Left : 1;
152 };
153 uint16_t soundcntLo;
154 };
155
156 union {
157 struct {
158 unsigned volume : 2;
159 unsigned volumeChA : 1;
160 unsigned volumeChB : 1;
161 unsigned : 4;
162 unsigned chARight : 1;
163 unsigned chALeft : 1;
164 unsigned chATimer : 1;
165 unsigned chAReset : 1;
166 unsigned chBRight : 1;
167 unsigned chBLeft : 1;
168 unsigned chBTimer : 1;
169 unsigned chBReset : 1;
170 };
171 uint16_t soundcntHi;
172 };
173
174 union {
175 struct {
176 unsigned playingCh1 : 1;
177 unsigned playingCh2 : 1;
178 unsigned playingCh3 : 1;
179 unsigned playingCh4 : 1;
180 unsigned : 3;
181 unsigned enable : 1;
182 unsigned : 8;
183 };
184 uint16_t soundcntX;
185 };
186
187 unsigned sampleRate;
188
189 int32_t nextEvent;
190 int32_t eventDiff;
191 int32_t nextCh1;
192 int32_t nextCh2;
193 int32_t nextCh3;
194 int32_t nextCh4;
195 int32_t nextSample;
196
197 int32_t sampleInterval;
198
199 pthread_mutex_t bufferMutex;
200};
201
202void GBAAudioInit(struct GBAAudio* audio);
203void GBAAudioDeinit(struct GBAAudio* audio);
204
205int32_t GBAAudioProcessEvents(struct GBAAudio* audio, int32_t cycles);
206void GBAAudioScheduleFifoDma(struct GBAAudio* audio, int number, struct GBADMA* info);
207
208void GBAAudioWriteSOUND1CNT_LO(struct GBAAudio* audio, uint16_t value);
209void GBAAudioWriteSOUND1CNT_HI(struct GBAAudio* audio, uint16_t value);
210void GBAAudioWriteSOUND1CNT_X(struct GBAAudio* audio, uint16_t value);
211void GBAAudioWriteSOUND2CNT_LO(struct GBAAudio* audio, uint16_t value);
212void GBAAudioWriteSOUND2CNT_HI(struct GBAAudio* audio, uint16_t value);
213void GBAAudioWriteSOUND3CNT_LO(struct GBAAudio* audio, uint16_t value);
214void GBAAudioWriteSOUND3CNT_HI(struct GBAAudio* audio, uint16_t value);
215void GBAAudioWriteSOUND3CNT_X(struct GBAAudio* audio, uint16_t value);
216void GBAAudioWriteSOUND4CNT_LO(struct GBAAudio* audio, uint16_t value);
217void GBAAudioWriteSOUND4CNT_HI(struct GBAAudio* audio, uint16_t value);
218void GBAAudioWriteSOUNDCNT_LO(struct GBAAudio* audio, uint16_t value);
219void GBAAudioWriteSOUNDCNT_HI(struct GBAAudio* audio, uint16_t value);
220void GBAAudioWriteSOUNDCNT_X(struct GBAAudio* audio, uint16_t value);
221
222void GBAAudioWriteWaveRAM(struct GBAAudio* audio, int address, uint32_t value);
223void GBAAudioWriteFIFO(struct GBAAudio* audio, int address, uint32_t value);
224void GBAAudioSampleFIFO(struct GBAAudio* audio, int fifoId);
225
226#endif