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 int32_t nextStep;
40 int32_t endTime;
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 enable : 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 };
84 uint16_t packed;
85 } wave;
86
87 struct {
88 union {
89 struct {
90 unsigned rate : 11;
91 unsigned : 3;
92 unsigned stop : 1;
93 unsigned restart : 1;
94 };
95 uint16_t packed;
96 };
97 int32_t endTime;
98 } control;
99
100 uint32_t wavedata[8];
101 int8_t sample;
102};
103
104struct GBAAudioChannel4 {
105 struct GBAAudioEnvelope envelope;
106 struct {
107 union {
108 struct {
109 unsigned ratio : 3;
110 unsigned power : 1;
111 unsigned frequency : 4;
112 unsigned : 6;
113 unsigned stop : 1;
114 unsigned restart : 1;
115 };
116 uint16_t packed;
117 };
118 int32_t endTime;
119 } control;
120
121 unsigned lfsr;
122 int8_t sample;
123};
124
125struct GBAAudioFIFO {
126 struct CircleBuffer fifo;
127 int dmaSource;
128 int8_t sample;
129};
130
131struct GBAAudio {
132 struct GBA* p;
133
134 struct GBAAudioChannel1 ch1;
135 struct GBAAudioChannel2 ch2;
136 struct GBAAudioChannel3 ch3;
137 struct GBAAudioChannel4 ch4;
138
139 struct GBAAudioFIFO chA;
140 struct GBAAudioFIFO chB;
141
142 struct CircleBuffer left;
143 struct CircleBuffer right;
144
145 union {
146 struct {
147 unsigned volumeRight : 3;
148 unsigned : 1;
149 unsigned volumeLeft : 3;
150 unsigned : 1;
151 unsigned ch1Right : 1;
152 unsigned ch2Right : 1;
153 unsigned ch3Right : 1;
154 unsigned ch4Right : 1;
155 unsigned ch1Left : 1;
156 unsigned ch2Left : 1;
157 unsigned ch3Left : 1;
158 unsigned ch4Left : 1;
159 };
160 uint16_t soundcntLo;
161 };
162
163 union {
164 struct {
165 unsigned volume : 2;
166 unsigned volumeChA : 1;
167 unsigned volumeChB : 1;
168 unsigned : 4;
169 unsigned chARight : 1;
170 unsigned chALeft : 1;
171 unsigned chATimer : 1;
172 unsigned chAReset : 1;
173 unsigned chBRight : 1;
174 unsigned chBLeft : 1;
175 unsigned chBTimer : 1;
176 unsigned chBReset : 1;
177 };
178 uint16_t soundcntHi;
179 };
180
181 union {
182 struct {
183 unsigned playingCh1 : 1;
184 unsigned playingCh2 : 1;
185 unsigned playingCh3 : 1;
186 unsigned playingCh4 : 1;
187 unsigned : 3;
188 unsigned enable : 1;
189 unsigned : 8;
190 };
191 uint16_t soundcntX;
192 };
193
194 unsigned sampleRate;
195
196 int32_t nextEvent;
197 int32_t eventDiff;
198 int32_t nextCh1;
199 int32_t nextCh2;
200 int32_t nextCh3;
201 int32_t nextCh4;
202 int32_t nextSample;
203
204 int32_t sampleInterval;
205
206 pthread_mutex_t bufferMutex;
207};
208
209void GBAAudioInit(struct GBAAudio* audio);
210void GBAAudioDeinit(struct GBAAudio* audio);
211
212int32_t GBAAudioProcessEvents(struct GBAAudio* audio, int32_t cycles);
213void GBAAudioScheduleFifoDma(struct GBAAudio* audio, int number, struct GBADMA* info);
214
215void GBAAudioWriteSOUND1CNT_LO(struct GBAAudio* audio, uint16_t value);
216void GBAAudioWriteSOUND1CNT_HI(struct GBAAudio* audio, uint16_t value);
217void GBAAudioWriteSOUND1CNT_X(struct GBAAudio* audio, uint16_t value);
218void GBAAudioWriteSOUND2CNT_LO(struct GBAAudio* audio, uint16_t value);
219void GBAAudioWriteSOUND2CNT_HI(struct GBAAudio* audio, uint16_t value);
220void GBAAudioWriteSOUND3CNT_LO(struct GBAAudio* audio, uint16_t value);
221void GBAAudioWriteSOUND3CNT_HI(struct GBAAudio* audio, uint16_t value);
222void GBAAudioWriteSOUND3CNT_X(struct GBAAudio* audio, uint16_t value);
223void GBAAudioWriteSOUND4CNT_LO(struct GBAAudio* audio, uint16_t value);
224void GBAAudioWriteSOUND4CNT_HI(struct GBAAudio* audio, uint16_t value);
225void GBAAudioWriteSOUNDCNT_LO(struct GBAAudio* audio, uint16_t value);
226void GBAAudioWriteSOUNDCNT_HI(struct GBAAudio* audio, uint16_t value);
227void GBAAudioWriteSOUNDCNT_X(struct GBAAudio* audio, uint16_t value);
228
229void GBAAudioWriteWaveRAM(struct GBAAudio* audio, int address, uint32_t value);
230void GBAAudioWriteFIFO(struct GBAAudio* audio, int address, uint32_t value);
231void GBAAudioSampleFIFO(struct GBAAudio* audio, int fifoId);
232
233#endif