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