all repos — mgba @ 5b50c43857a5aa5bcefc7b339648bfc73d74fc2f

mGBA Game Boy Advance Emulator

src/gba/audio.c (view raw)

  1/* Copyright (c) 2013-2016 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#include "audio.h"
  7
  8#include "core/sync.h"
  9#include "gba/gba.h"
 10#include "gba/io.h"
 11#include "gba/serialize.h"
 12#include "gba/video.h"
 13
 14#ifdef _3DS
 15#define blip_add_delta blip_add_delta_fast
 16#endif
 17
 18mLOG_DEFINE_CATEGORY(GBA_AUDIO, "GBA Audio");
 19
 20const unsigned GBA_AUDIO_SAMPLES = 2048;
 21const unsigned GBA_AUDIO_FIFO_SIZE = 8 * sizeof(int32_t);
 22const int GBA_AUDIO_VOLUME_MAX = 0x100;
 23
 24static const int CLOCKS_PER_FRAME = 0x400;
 25
 26static int _applyBias(struct GBAAudio* audio, int sample);
 27static void _sample(struct GBAAudio* audio);
 28
 29void GBAAudioInit(struct GBAAudio* audio, size_t samples) {
 30	audio->psg.p = NULL;
 31	uint8_t* nr52 = (uint8_t*) &audio->p->memory.io[REG_SOUNDCNT_X >> 1];
 32#ifdef __BIG_ENDIAN__
 33	++nr52;
 34#endif
 35	GBAudioInit(&audio->psg, 0, nr52, GB_AUDIO_GBA);
 36	audio->samples = samples;
 37	audio->psg.clockRate = GBA_ARM7TDMI_FREQUENCY;
 38	// Guess too large; we hang producing extra samples if we guess too low
 39	blip_set_rates(audio->psg.left, GBA_ARM7TDMI_FREQUENCY, 96000);
 40	blip_set_rates(audio->psg.right, GBA_ARM7TDMI_FREQUENCY, 96000);
 41	CircleBufferInit(&audio->chA.fifo, GBA_AUDIO_FIFO_SIZE);
 42	CircleBufferInit(&audio->chB.fifo, GBA_AUDIO_FIFO_SIZE);
 43
 44	audio->forceDisableChA = false;
 45	audio->forceDisableChB = false;
 46	audio->masterVolume = GBA_AUDIO_VOLUME_MAX;
 47}
 48
 49void GBAAudioReset(struct GBAAudio* audio) {
 50	GBAudioReset(&audio->psg);
 51	audio->nextEvent = 0;
 52	audio->chA.dmaSource = 1;
 53	audio->chB.dmaSource = 2;
 54	audio->chA.sample = 0;
 55	audio->chB.sample = 0;
 56	audio->eventDiff = 0;
 57	audio->nextSample = 0;
 58	audio->sampleRate = 0x8000;
 59	audio->soundbias = 0x200;
 60	audio->volume = 0;
 61	audio->volumeChA = false;
 62	audio->volumeChB = false;
 63	audio->chARight = false;
 64	audio->chALeft = false;
 65	audio->chATimer = false;
 66	audio->chBRight = false;
 67	audio->chBLeft = false;
 68	audio->chBTimer = false;
 69	audio->enable = false;
 70	audio->sampleInterval = GBA_ARM7TDMI_FREQUENCY / audio->sampleRate;
 71
 72	blip_clear(audio->psg.left);
 73	blip_clear(audio->psg.right);
 74	audio->clock = 0;
 75	CircleBufferClear(&audio->chA.fifo);
 76	CircleBufferClear(&audio->chB.fifo);
 77}
 78
 79void GBAAudioDeinit(struct GBAAudio* audio) {
 80	GBAudioDeinit(&audio->psg);
 81	CircleBufferDeinit(&audio->chA.fifo);
 82	CircleBufferDeinit(&audio->chB.fifo);
 83}
 84
 85void GBAAudioResizeBuffer(struct GBAAudio* audio, size_t samples) {
 86	mCoreSyncLockAudio(audio->p->sync);
 87	audio->samples = samples;
 88	blip_clear(audio->psg.left);
 89	blip_clear(audio->psg.right);
 90	audio->clock = 0;
 91	mCoreSyncConsumeAudio(audio->p->sync);
 92}
 93
 94int32_t GBAAudioProcessEvents(struct GBAAudio* audio, int32_t cycles) {
 95	audio->nextEvent -= cycles;
 96	audio->eventDiff += cycles;
 97	while (audio->nextEvent <= 0) {
 98		audio->nextEvent = INT_MAX;
 99		if (audio->enable) {
100			audio->nextEvent = GBAudioProcessEvents(&audio->psg, audio->eventDiff / 4);
101			if (audio->nextEvent != INT_MAX) {
102				audio->nextEvent *= 4;
103			}
104		}
105
106		audio->nextSample -= audio->eventDiff;
107		if (audio->nextSample <= 0) {
108			_sample(audio);
109			audio->nextSample += audio->sampleInterval;
110		}
111
112		if (audio->nextSample < audio->nextEvent) {
113			audio->nextEvent = audio->nextSample;
114		}
115		audio->eventDiff = 0;
116	}
117	return audio->nextEvent;
118}
119
120void GBAAudioScheduleFifoDma(struct GBAAudio* audio, int number, struct GBADMA* info) {
121	switch (info->dest) {
122	case BASE_IO | REG_FIFO_A_LO:
123		audio->chA.dmaSource = number;
124		break;
125	case BASE_IO | REG_FIFO_B_LO:
126		audio->chB.dmaSource = number;
127		break;
128	default:
129		mLOG(GBA_AUDIO, GAME_ERROR, "Invalid FIFO destination: 0x%08X", info->dest);
130		return;
131	}
132	info->reg = GBADMARegisterSetDestControl(info->reg, DMA_FIXED);
133}
134
135void GBAAudioWriteSOUND1CNT_LO(struct GBAAudio* audio, uint16_t value) {
136	GBAudioWriteNR10(&audio->psg, value);
137}
138
139void GBAAudioWriteSOUND1CNT_HI(struct GBAAudio* audio, uint16_t value) {
140	GBAudioWriteNR11(&audio->psg, value);
141	GBAudioWriteNR12(&audio->psg, value >> 8);
142}
143
144void GBAAudioWriteSOUND1CNT_X(struct GBAAudio* audio, uint16_t value) {
145	GBAudioWriteNR13(&audio->psg, value);
146	GBAudioWriteNR14(&audio->psg, value >> 8);
147}
148
149void GBAAudioWriteSOUND2CNT_LO(struct GBAAudio* audio, uint16_t value) {
150	GBAudioWriteNR21(&audio->psg, value);
151	GBAudioWriteNR22(&audio->psg, value >> 8);
152}
153
154void GBAAudioWriteSOUND2CNT_HI(struct GBAAudio* audio, uint16_t value) {
155	GBAudioWriteNR23(&audio->psg, value);
156	GBAudioWriteNR24(&audio->psg, value >> 8);
157}
158
159void GBAAudioWriteSOUND3CNT_LO(struct GBAAudio* audio, uint16_t value) {
160	audio->psg.ch3.size = GBAudioRegisterBankGetSize(value);
161	audio->psg.ch3.bank = GBAudioRegisterBankGetBank(value);
162	GBAudioWriteNR30(&audio->psg, value);
163}
164
165void GBAAudioWriteSOUND3CNT_HI(struct GBAAudio* audio, uint16_t value) {
166	GBAudioWriteNR31(&audio->psg, value);
167	audio->psg.ch3.volume = GBAudioRegisterBankVolumeGetVolumeGBA(value >> 8);
168}
169
170void GBAAudioWriteSOUND3CNT_X(struct GBAAudio* audio, uint16_t value) {
171	GBAudioWriteNR33(&audio->psg, value);
172	GBAudioWriteNR34(&audio->psg, value >> 8);
173}
174
175void GBAAudioWriteSOUND4CNT_LO(struct GBAAudio* audio, uint16_t value) {
176	GBAudioWriteNR41(&audio->psg, value);
177	GBAudioWriteNR42(&audio->psg, value >> 8);
178}
179
180void GBAAudioWriteSOUND4CNT_HI(struct GBAAudio* audio, uint16_t value) {
181	GBAudioWriteNR43(&audio->psg, value);
182	GBAudioWriteNR44(&audio->psg, value >> 8);
183}
184
185void GBAAudioWriteSOUNDCNT_LO(struct GBAAudio* audio, uint16_t value) {
186	GBAudioWriteNR50(&audio->psg, value);
187	GBAudioWriteNR51(&audio->psg, value >> 8);
188}
189
190void GBAAudioWriteSOUNDCNT_HI(struct GBAAudio* audio, uint16_t value) {
191	audio->volume = GBARegisterSOUNDCNT_HIGetVolume(value);
192	audio->volumeChA = GBARegisterSOUNDCNT_HIGetVolumeChA(value);
193	audio->volumeChB = GBARegisterSOUNDCNT_HIGetVolumeChB(value);
194	audio->chARight = GBARegisterSOUNDCNT_HIGetChARight(value);
195	audio->chALeft = GBARegisterSOUNDCNT_HIGetChALeft(value);
196	audio->chATimer = GBARegisterSOUNDCNT_HIGetChATimer(value);
197	audio->chBRight = GBARegisterSOUNDCNT_HIGetChBRight(value);
198	audio->chBLeft = GBARegisterSOUNDCNT_HIGetChBLeft(value);
199	audio->chBTimer = GBARegisterSOUNDCNT_HIGetChBTimer(value);
200	if (GBARegisterSOUNDCNT_HIIsChAReset(value)) {
201		CircleBufferClear(&audio->chA.fifo);
202	}
203	if (GBARegisterSOUNDCNT_HIIsChBReset(value)) {
204		CircleBufferClear(&audio->chB.fifo);
205	}
206}
207
208void GBAAudioWriteSOUNDCNT_X(struct GBAAudio* audio, uint16_t value) {
209	audio->enable = GBAudioEnableGetEnable(value);
210	GBAudioWriteNR52(&audio->psg, value);
211}
212
213void GBAAudioWriteSOUNDBIAS(struct GBAAudio* audio, uint16_t value) {
214	audio->soundbias = value;
215}
216
217void GBAAudioWriteWaveRAM(struct GBAAudio* audio, int address, uint32_t value) {
218	audio->psg.ch3.wavedata32[address | (!audio->psg.ch3.bank * 4)] = value;
219}
220
221void GBAAudioWriteFIFO(struct GBAAudio* audio, int address, uint32_t value) {
222	struct CircleBuffer* fifo;
223	switch (address) {
224	case REG_FIFO_A_LO:
225		fifo = &audio->chA.fifo;
226		break;
227	case REG_FIFO_B_LO:
228		fifo = &audio->chB.fifo;
229		break;
230	default:
231		mLOG(GBA_AUDIO, ERROR, "Bad FIFO write to address 0x%03x", address);
232		return;
233	}
234	int i;
235	for (i = 0; i < 4; ++i) {
236		while (!CircleBufferWrite8(fifo, value >> (8 * i))) {
237			int8_t dummy;
238			CircleBufferRead8(fifo, &dummy);
239		}
240	}
241}
242
243void GBAAudioSampleFIFO(struct GBAAudio* audio, int fifoId, int32_t cycles) {
244	struct GBAAudioFIFO* channel;
245	if (fifoId == 0) {
246		channel = &audio->chA;
247	} else if (fifoId == 1) {
248		channel = &audio->chB;
249	} else {
250		mLOG(GBA_AUDIO, ERROR, "Bad FIFO write to address 0x%03x", fifoId);
251		return;
252	}
253	if (CircleBufferSize(&channel->fifo) <= 4 * sizeof(int32_t) && channel->dmaSource > 0) {
254		struct GBADMA* dma = &audio->p->memory.dma[channel->dmaSource];
255		if (GBADMARegisterGetTiming(dma->reg) == DMA_TIMING_CUSTOM) {
256			dma->nextCount = 4;
257			dma->nextEvent = 0;
258			dma->reg = GBADMARegisterSetWidth(dma->reg, 1);
259			dma->reg = GBADMARegisterSetDestControl(dma->reg, 2);
260			GBAMemoryUpdateDMAs(audio->p, -cycles);
261		} else {
262			channel->dmaSource = 0;
263		}
264	}
265	CircleBufferRead8(&channel->fifo, (int8_t*) &channel->sample);
266}
267
268static int _applyBias(struct GBAAudio* audio, int sample) {
269	sample += GBARegisterSOUNDBIASGetBias(audio->soundbias);
270	if (sample >= 0x400) {
271		sample = 0x3FF;
272	} else if (sample < 0) {
273		sample = 0;
274	}
275	return ((sample - GBARegisterSOUNDBIASGetBias(audio->soundbias)) * audio->masterVolume) >> 3;
276}
277
278static void _sample(struct GBAAudio* audio) {
279	int16_t sampleLeft = 0;
280	int16_t sampleRight = 0;
281	int psgShift = 5 - audio->volume;
282	GBAudioSamplePSG(&audio->psg, &sampleLeft, &sampleRight);
283	sampleLeft >>= psgShift;
284	sampleRight >>= psgShift;
285
286	if (!audio->forceDisableChA) {
287		if (audio->chALeft) {
288			sampleLeft += (audio->chA.sample << 2) >> !audio->volumeChA;
289		}
290
291		if (audio->chARight) {
292			sampleRight += (audio->chA.sample << 2) >> !audio->volumeChA;
293		}
294	}
295
296	if (!audio->forceDisableChB) {
297		if (audio->chBLeft) {
298			sampleLeft += (audio->chB.sample << 2) >> !audio->volumeChB;
299		}
300
301		if (audio->chBRight) {
302			sampleRight += (audio->chB.sample << 2) >> !audio->volumeChB;
303		}
304	}
305
306	sampleLeft = _applyBias(audio, sampleLeft);
307	sampleRight = _applyBias(audio, sampleRight);
308
309	mCoreSyncLockAudio(audio->p->sync);
310	unsigned produced;
311	if ((size_t) blip_samples_avail(audio->psg.left) < audio->samples) {
312		blip_add_delta(audio->psg.left, audio->clock, sampleLeft - audio->lastLeft);
313		blip_add_delta(audio->psg.right, audio->clock, sampleRight - audio->lastRight);
314		audio->lastLeft = sampleLeft;
315		audio->lastRight = sampleRight;
316		audio->clock += audio->sampleInterval;
317		if (audio->clock >= CLOCKS_PER_FRAME) {
318			blip_end_frame(audio->psg.left, audio->clock);
319			blip_end_frame(audio->psg.right, audio->clock);
320			audio->clock -= CLOCKS_PER_FRAME;
321		}
322	}
323	produced = blip_samples_avail(audio->psg.left);
324	if (audio->p->stream && audio->p->stream->postAudioFrame) {
325		audio->p->stream->postAudioFrame(audio->p->stream, sampleLeft, sampleRight);
326	}
327	bool wait = produced >= audio->samples;
328	mCoreSyncProduceAudio(audio->p->sync, wait);
329
330	if (wait && audio->p->stream && audio->p->stream->postAudioBuffer) {
331		audio->p->stream->postAudioBuffer(audio->p->stream, audio->psg.left, audio->psg.right);
332	}
333}
334
335void GBAAudioSerialize(const struct GBAAudio* audio, struct GBASerializedState* state) {
336	GBAudioPSGSerialize(&audio->psg, &state->audio.psg, &state->audio.flags);
337
338	CircleBufferDump(&audio->chA.fifo, state->audio.fifoA, sizeof(state->audio.fifoA));
339	CircleBufferDump(&audio->chB.fifo, state->audio.fifoB, sizeof(state->audio.fifoB));
340	uint32_t fifoSize = CircleBufferSize(&audio->chA.fifo);
341	STORE_32(fifoSize, 0, &state->audio.fifoSize);
342
343	STORE_32(audio->nextEvent, 0, &state->audio.nextEvent);
344	STORE_32(audio->eventDiff, 0, &state->audio.eventDiff);
345	STORE_32(audio->nextSample, 0, &state->audio.nextSample);
346}
347
348void GBAAudioDeserialize(struct GBAAudio* audio, const struct GBASerializedState* state) {
349	GBAudioPSGDeserialize(&audio->psg, &state->audio.psg, &state->audio.flags);
350
351	CircleBufferClear(&audio->chA.fifo);
352	CircleBufferClear(&audio->chB.fifo);
353	uint32_t fifoSize;
354	LOAD_32(fifoSize, 0, &state->audio.fifoSize);
355	if (state->audio.fifoSize > CircleBufferCapacity(&audio->chA.fifo)) {
356		fifoSize = CircleBufferCapacity(&audio->chA.fifo);
357	}
358	size_t i;
359	for (i = 0; i < fifoSize; ++i) {
360		CircleBufferWrite8(&audio->chA.fifo, state->audio.fifoA[i]);
361		CircleBufferWrite8(&audio->chB.fifo, state->audio.fifoB[i]);
362	}
363
364	LOAD_32(audio->nextEvent, 0, &state->audio.nextEvent);
365	LOAD_32(audio->eventDiff, 0, &state->audio.eventDiff);
366	LOAD_32(audio->nextSample, 0, &state->audio.nextSample);
367}
368
369float GBAAudioCalculateRatio(float inputSampleRate, float desiredFPS, float desiredSampleRate) {
370	return desiredSampleRate * GBA_ARM7TDMI_FREQUENCY / (VIDEO_TOTAL_LENGTH * desiredFPS * inputSampleRate);
371}