all repos — mgba @ c12b8e6e6db57c76a4e3622e10d25e93e43c9abc

mGBA Game Boy Advance Emulator

src/gba/gba-audio.h (view raw)

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