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