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