src/gba/audio.h (view raw)
1/* Copyright (c) 2013-2015 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#ifndef GBA_AUDIO_H
7#define GBA_AUDIO_H
8
9#include "util/common.h"
10#include "macros.h"
11
12#include "util/circle-buffer.h"
13
14#define RESAMPLE_NN 0
15#define RESAMPLE_BLIP_BUF 2
16
17#if RESAMPLE_LIBRARY == RESAMPLE_BLIP_BUF
18#include "third-party/blip_buf/blip_buf.h"
19#endif
20
21struct GBADMA;
22
23extern const unsigned GBA_AUDIO_SAMPLES;
24extern const int GBA_AUDIO_VOLUME_MAX;
25
26DECL_BITFIELD(GBAAudioRegisterEnvelope, uint16_t);
27DECL_BITS(GBAAudioRegisterEnvelope, Length, 0, 6);
28DECL_BITS(GBAAudioRegisterEnvelope, Duty, 6, 2);
29DECL_BITS(GBAAudioRegisterEnvelope, StepTime, 8, 3);
30DECL_BIT(GBAAudioRegisterEnvelope, Direction, 11);
31DECL_BITS(GBAAudioRegisterEnvelope, InitialVolume, 12, 4);
32
33DECL_BITFIELD(GBAAudioRegisterControl, uint16_t);
34DECL_BITS(GBAAudioRegisterControl, Rate, 0, 11);
35DECL_BITS(GBAAudioRegisterControl, Frequency, 0, 11);
36DECL_BIT(GBAAudioRegisterControl, Stop, 14);
37DECL_BIT(GBAAudioRegisterControl, Restart, 15);
38
39DECL_BITFIELD(GBAAudioRegisterSquareSweep, uint16_t);
40DECL_BITS(GBAAudioRegisterSquareSweep, Shift, 0, 3);
41DECL_BIT(GBAAudioRegisterSquareSweep, Direction, 3);
42DECL_BITS(GBAAudioRegisterSquareSweep, Time, 4, 3);
43
44DECL_BITFIELD(GBAAudioRegisterBank, uint16_t);
45DECL_BIT(GBAAudioRegisterBank, Size, 5);
46DECL_BIT(GBAAudioRegisterBank, Bank, 6);
47DECL_BIT(GBAAudioRegisterBank, Enable, 7);
48
49DECL_BITFIELD(GBAAudioRegisterBankWave, uint16_t);
50DECL_BITS(GBAAudioRegisterBankWave, Length, 0, 8);
51DECL_BITS(GBAAudioRegisterBankWave, Volume, 13, 3);
52
53DECL_BITFIELD(GBAAudioRegisterCh4Control, uint16_t);
54DECL_BITS(GBAAudioRegisterCh4Control, Ratio, 0, 3);
55DECL_BIT(GBAAudioRegisterCh4Control, Power, 3);
56DECL_BITS(GBAAudioRegisterCh4Control, Frequency, 4, 4);
57DECL_BIT(GBAAudioRegisterCh4Control, Stop, 14);
58DECL_BIT(GBAAudioRegisterCh4Control, Restart, 15);
59
60struct GBAAudioEnvelope {
61 uint8_t length;
62 uint8_t duty;
63 uint8_t stepTime;
64 uint8_t initialVolume;
65 bool direction;
66 int currentVolume;
67 int dead;
68 int32_t nextStep;
69};
70
71struct GBAAudioSquareControl {
72 uint16_t frequency;
73 bool stop;
74 int hi;
75 int32_t nextStep;
76 int32_t endTime;
77};
78
79struct GBAAudioChannel1 {
80 struct GBAAudioSquareSweep {
81 uint8_t shift;
82 uint8_t time;
83 bool direction;
84 } sweep;
85 int32_t nextSweep;
86
87 struct GBAAudioEnvelope envelope;
88 struct GBAAudioSquareControl control;
89 int8_t sample;
90};
91
92struct GBAAudioChannel2 {
93 struct GBAAudioEnvelope envelope;
94 struct GBAAudioSquareControl control;
95 int8_t sample;
96};
97
98struct GBAAudioChannel3 {
99 struct {
100 bool size;
101 bool bank;
102 bool enable;
103 } bank;
104
105 struct {
106 uint8_t length;
107 uint8_t volume;
108 } wave;
109
110 struct {
111 uint16_t rate;
112 bool stop;
113 int32_t endTime;
114 } control;
115
116 uint32_t wavedata[8];
117 int8_t sample;
118};
119
120struct GBAAudioChannel4 {
121 struct GBAAudioEnvelope envelope;
122
123 struct {
124 uint8_t ratio;
125 uint8_t frequency;
126 bool power;
127 bool stop;
128 int32_t endTime;
129 } control;
130
131 uint32_t lfsr;
132 int8_t sample;
133};
134
135struct GBAAudioFIFO {
136 struct CircleBuffer fifo;
137 int dmaSource;
138 int8_t sample;
139};
140
141DECL_BITFIELD(GBARegisterSOUNDCNT_LO, uint16_t);
142DECL_BITS(GBARegisterSOUNDCNT_LO, VolumeRight, 0, 3);
143DECL_BITS(GBARegisterSOUNDCNT_LO, VolumeLeft, 4, 3);
144DECL_BIT(GBARegisterSOUNDCNT_LO, Ch1Right, 8);
145DECL_BIT(GBARegisterSOUNDCNT_LO, Ch2Right, 9);
146DECL_BIT(GBARegisterSOUNDCNT_LO, Ch3Right, 10);
147DECL_BIT(GBARegisterSOUNDCNT_LO, Ch4Right, 11);
148DECL_BIT(GBARegisterSOUNDCNT_LO, Ch1Left, 12);
149DECL_BIT(GBARegisterSOUNDCNT_LO, Ch2Left, 13);
150DECL_BIT(GBARegisterSOUNDCNT_LO, Ch3Left, 14);
151DECL_BIT(GBARegisterSOUNDCNT_LO, Ch4Left, 15);
152
153DECL_BITFIELD(GBARegisterSOUNDCNT_HI, uint16_t);
154DECL_BITS(GBARegisterSOUNDCNT_HI, Volume, 0, 2);
155DECL_BIT(GBARegisterSOUNDCNT_HI, VolumeChA, 2);
156DECL_BIT(GBARegisterSOUNDCNT_HI, VolumeChB, 3);
157DECL_BIT(GBARegisterSOUNDCNT_HI, ChARight, 8);
158DECL_BIT(GBARegisterSOUNDCNT_HI, ChALeft, 9);
159DECL_BIT(GBARegisterSOUNDCNT_HI, ChATimer, 10);
160DECL_BIT(GBARegisterSOUNDCNT_HI, ChAReset, 11);
161DECL_BIT(GBARegisterSOUNDCNT_HI, ChBRight, 12);
162DECL_BIT(GBARegisterSOUNDCNT_HI, ChBLeft, 13);
163DECL_BIT(GBARegisterSOUNDCNT_HI, ChBTimer, 14);
164DECL_BIT(GBARegisterSOUNDCNT_HI, ChBReset, 15);
165
166DECL_BITFIELD(GBARegisterSOUNDCNT_X, uint16_t);
167DECL_BIT(GBARegisterSOUNDCNT_X, PlayingCh1, 0);
168DECL_BIT(GBARegisterSOUNDCNT_X, PlayingCh2, 1);
169DECL_BIT(GBARegisterSOUNDCNT_X, PlayingCh3, 2);
170DECL_BIT(GBARegisterSOUNDCNT_X, PlayingCh4, 3);
171DECL_BIT(GBARegisterSOUNDCNT_X, Enable, 7);
172
173DECL_BITFIELD(GBARegisterSOUNDBIAS, uint16_t);
174DECL_BITS(GBARegisterSOUNDBIAS, Bias, 0, 10);
175DECL_BITS(GBARegisterSOUNDBIAS, Resolution, 14, 2);
176
177struct GBAAudio {
178 struct GBA* p;
179
180 struct GBAAudioChannel1 ch1;
181 struct GBAAudioChannel2 ch2;
182 struct GBAAudioChannel3 ch3;
183 struct GBAAudioChannel4 ch4;
184
185 struct GBAAudioFIFO chA;
186 struct GBAAudioFIFO chB;
187
188#if RESAMPLE_LIBRARY != RESAMPLE_BLIP_BUF
189 struct CircleBuffer left;
190 struct CircleBuffer right;
191#else
192 blip_t* left;
193 blip_t* right;
194 int16_t lastLeft;
195 int16_t lastRight;
196 int clock;
197#endif
198
199 uint8_t volumeRight;
200 uint8_t volumeLeft;
201 bool ch1Right;
202 bool ch2Right;
203 bool ch3Right;
204 bool ch4Right;
205 bool ch1Left;
206 bool ch2Left;
207 bool ch3Left;
208 bool ch4Left;
209
210 uint8_t volume;
211 bool volumeChA;
212 bool volumeChB;
213 bool chARight;
214 bool chALeft;
215 bool chATimer;
216 bool chBRight;
217 bool chBLeft;
218 bool chBTimer;
219
220 bool playingCh1;
221 bool playingCh2;
222 bool playingCh3;
223 bool playingCh4;
224 bool enable;
225
226 size_t samples;
227 unsigned sampleRate;
228
229 GBARegisterSOUNDBIAS soundbias;
230
231 int32_t nextEvent;
232 int32_t eventDiff;
233 int32_t nextCh1;
234 int32_t nextCh2;
235 int32_t nextCh3;
236 int32_t nextCh4;
237 int32_t nextSample;
238
239 int32_t sampleInterval;
240
241 bool forceDisableCh[4];
242 bool forceDisableChA;
243 bool forceDisableChB;
244 int masterVolume;
245};
246
247struct GBAStereoSample {
248 int16_t left;
249 int16_t right;
250};
251
252void GBAAudioInit(struct GBAAudio* audio, size_t samples);
253void GBAAudioReset(struct GBAAudio* audio);
254void GBAAudioDeinit(struct GBAAudio* audio);
255
256void GBAAudioResizeBuffer(struct GBAAudio* audio, size_t samples);
257
258int32_t GBAAudioProcessEvents(struct GBAAudio* audio, int32_t cycles);
259void GBAAudioScheduleFifoDma(struct GBAAudio* audio, int number, struct GBADMA* info);
260
261void GBAAudioWriteSOUND1CNT_LO(struct GBAAudio* audio, uint16_t value);
262void GBAAudioWriteSOUND1CNT_HI(struct GBAAudio* audio, uint16_t value);
263void GBAAudioWriteSOUND1CNT_X(struct GBAAudio* audio, uint16_t value);
264void GBAAudioWriteSOUND2CNT_LO(struct GBAAudio* audio, uint16_t value);
265void GBAAudioWriteSOUND2CNT_HI(struct GBAAudio* audio, uint16_t value);
266void GBAAudioWriteSOUND3CNT_LO(struct GBAAudio* audio, uint16_t value);
267void GBAAudioWriteSOUND3CNT_HI(struct GBAAudio* audio, uint16_t value);
268void GBAAudioWriteSOUND3CNT_X(struct GBAAudio* audio, uint16_t value);
269void GBAAudioWriteSOUND4CNT_LO(struct GBAAudio* audio, uint16_t value);
270void GBAAudioWriteSOUND4CNT_HI(struct GBAAudio* audio, uint16_t value);
271void GBAAudioWriteSOUNDCNT_LO(struct GBAAudio* audio, uint16_t value);
272void GBAAudioWriteSOUNDCNT_HI(struct GBAAudio* audio, uint16_t value);
273void GBAAudioWriteSOUNDCNT_X(struct GBAAudio* audio, uint16_t value);
274void GBAAudioWriteSOUNDBIAS(struct GBAAudio* audio, uint16_t value);
275
276void GBAAudioWriteWaveRAM(struct GBAAudio* audio, int address, uint32_t value);
277void GBAAudioWriteFIFO(struct GBAAudio* audio, int address, uint32_t value);
278void GBAAudioSampleFIFO(struct GBAAudio* audio, int fifoId, int32_t cycles);
279
280#if RESAMPLE_LIBRARY != RESAMPLE_BLIP_BUF
281unsigned GBAAudioCopy(struct GBAAudio* audio, void* left, void* right, unsigned nSamples);
282unsigned GBAAudioResampleNN(struct GBAAudio*, float ratio, float* drift, struct GBAStereoSample* output,
283 unsigned nSamples);
284#endif
285
286struct GBASerializedState;
287void GBAAudioSerialize(const struct GBAAudio* audio, struct GBASerializedState* state);
288void GBAAudioDeserialize(struct GBAAudio* audio, const struct GBASerializedState* state);
289
290float GBAAudioCalculateRatio(float inputSampleRate, float desiredFPS, float desiredSampleRatio);
291
292#endif