all repos — mgba @ 3182b5e35d418fe281eca092e8c53263184920c8

mGBA Game Boy Advance Emulator

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