all repos — mgba @ e9c1a53cfbf03252db40b5a19b830f8eb968c684

mGBA Game Boy Advance Emulator

src/ds/audio.c (view raw)

  1/* Copyright (c) 2013-2017 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 <mgba/internal/ds/audio.h>
  7
  8#include <mgba/core/blip_buf.h>
  9#include <mgba/core/sync.h>
 10#include <mgba/internal/ds/ds.h>
 11
 12mLOG_DEFINE_CATEGORY(DS_AUDIO, "DS Audio", "ds.audio");
 13
 14static const unsigned BLIP_BUFFER_SIZE = 0x4000;
 15static const int CLOCKS_PER_FRAME = 0x4000;
 16const int DS_AUDIO_VOLUME_MAX = 0x100;
 17
 18static void _updateChannel(struct mTiming* timing, void* user, uint32_t cyclesLate);
 19static void _sample(struct mTiming* timing, void* user, uint32_t cyclesLate);
 20static void _updateMixer(struct DSAudio*);
 21
 22static const int _adpcmIndexTable[8] = {
 23	-1, -1, -1, -1, 2, 4, 6, 8
 24};
 25
 26static const uint16_t _adpcmTable[89] = {
 27	0x0007, 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x0010, 0x0011, 0x0013, 0x0015,
 28	0x0017, 0x0019, 0x001C, 0x001F, 0x0022, 0x0025, 0x0029, 0x002D, 0x0032, 0x0037, 0x003C, 0x0042,
 29	0x0049, 0x0050, 0x0058, 0x0061, 0x006B, 0x0076, 0x0082, 0x008F, 0x009D, 0x00AD, 0x00BE, 0x00D1,
 30	0x00E6, 0x00FD, 0x0117, 0x0133, 0x0151, 0x0173, 0x0198, 0x01C1, 0x01EE, 0x0220, 0x0256, 0x0292,
 31	0x02D4, 0x031C, 0x036C, 0x03C3, 0x0424, 0x048E, 0x0502, 0x0583, 0x0610, 0x06AB, 0x0756, 0x0812,
 32	0x08E0, 0x09C3, 0x0ABD, 0x0BD0, 0x0CFF, 0x0E4C, 0x0FBA, 0x114C, 0x1307, 0x14EE, 0x1706, 0x1954,
 33	0x1BDC, 0x1EA5, 0x21B6, 0x2515, 0x28CA, 0x2CDF, 0x315B, 0x364B, 0x3BB9, 0x41B2, 0x4844, 0x4F7E,
 34	0x5771, 0x602F, 0x69CE, 0x7462, 0x7FFF
 35};
 36
 37void DSAudioInit(struct DSAudio* audio, size_t samples) {
 38	audio->samples = samples;
 39	audio->left = blip_new(BLIP_BUFFER_SIZE);
 40	audio->right = blip_new(BLIP_BUFFER_SIZE);
 41
 42	audio->ch[0].updateEvent.name = "DS Audio Channel 0";
 43	audio->ch[1].updateEvent.name = "DS Audio Channel 1";
 44	audio->ch[2].updateEvent.name = "DS Audio Channel 2";
 45	audio->ch[3].updateEvent.name = "DS Audio Channel 3";
 46	audio->ch[4].updateEvent.name = "DS Audio Channel 4";
 47	audio->ch[5].updateEvent.name = "DS Audio Channel 5";
 48	audio->ch[6].updateEvent.name = "DS Audio Channel 6";
 49	audio->ch[7].updateEvent.name = "DS Audio Channel 7";
 50	audio->ch[8].updateEvent.name = "DS Audio Channel 8";
 51	audio->ch[9].updateEvent.name = "DS Audio Channel 9";
 52	audio->ch[10].updateEvent.name = "DS Audio Channel 10";
 53	audio->ch[11].updateEvent.name = "DS Audio Channel 11";
 54	audio->ch[12].updateEvent.name = "DS Audio Channel 12";
 55	audio->ch[13].updateEvent.name = "DS Audio Channel 13";
 56	audio->ch[14].updateEvent.name = "DS Audio Channel 14";
 57	audio->ch[15].updateEvent.name = "DS Audio Channel 15";
 58
 59	int ch;
 60	for (ch = 0; ch < 16; ++ch) {
 61		audio->ch[ch].index = ch;
 62		audio->ch[ch].updateEvent.priority = 0x10 | ch;
 63		audio->ch[ch].updateEvent.context = &audio->ch[ch];
 64		audio->ch[ch].updateEvent.callback = _updateChannel;
 65		audio->ch[ch].p = audio;
 66		audio->forceDisableCh[ch] = false;
 67	}
 68	audio->masterVolume = DS_AUDIO_VOLUME_MAX;
 69
 70	audio->sampleEvent.name = "DS Audio Sample";
 71	audio->sampleEvent.context = audio;
 72	audio->sampleEvent.callback = _sample;
 73	audio->sampleEvent.priority = 0x110;
 74
 75	blip_set_rates(audio->left, DS_ARM7TDMI_FREQUENCY, 96000);
 76	blip_set_rates(audio->right, DS_ARM7TDMI_FREQUENCY, 96000);
 77}
 78
 79void DSAudioDeinit(struct DSAudio* audio) {
 80	blip_delete(audio->left);
 81	blip_delete(audio->right);
 82}
 83
 84void DSAudioReset(struct DSAudio* audio) {
 85	mTimingDeschedule(&audio->p->ds7.timing, &audio->sampleEvent);
 86	mTimingSchedule(&audio->p->ds7.timing, &audio->sampleEvent, 0);
 87	audio->sampleRate = 0x8000;
 88	audio->sampleInterval = DS_ARM7TDMI_FREQUENCY / audio->sampleRate;
 89
 90	int ch;
 91	for (ch = 0; ch < 16; ++ch) {
 92		audio->ch[ch].source = 0;
 93		audio->ch[ch].loopPoint = 0;
 94		audio->ch[ch].length = 0;
 95		audio->ch[ch].offset = 0;
 96		audio->ch[ch].sample = 0;
 97		audio->ch[ch].adpcmOffset = 0;
 98		audio->ch[ch].adpcmStartSample = 0;
 99		audio->ch[ch].adpcmStartIndex = 0;
100		audio->ch[ch].adpcmSample = 0;
101		audio->ch[ch].adpcmIndex = 0;
102	}
103
104	blip_clear(audio->left);
105	blip_clear(audio->right);
106	audio->clock = 0;
107	audio->bias = 0x200;
108}
109
110void DSAudioResizeBuffer(struct DSAudio* audio, size_t samples) {
111	// TODO: Share between other cores
112	mCoreSyncLockAudio(audio->p->sync);
113	audio->samples = samples;
114	blip_clear(audio->left);
115	blip_clear(audio->right);
116	audio->clock = 0;
117	mCoreSyncConsumeAudio(audio->p->sync);
118}
119
120void DSAudioWriteSOUNDCNT_LO(struct DSAudio* audio, int chan, uint16_t value) {
121	audio->ch[chan].volume = DSRegisterSOUNDxCNTGetVolumeMul(value);
122	audio->ch[chan].divider = DSRegisterSOUNDxCNTGetVolumeDiv(value);
123	if (audio->ch[chan].divider == 3) {
124		++audio->ch[chan].divider;
125	}
126}
127
128void DSAudioWriteSOUNDCNT_HI(struct DSAudio* audio, int chan, uint16_t value) {
129	DSRegisterSOUNDxCNT reg = value << 16;
130	struct DSAudioChannel* ch = &audio->ch[chan];
131
132	ch->panning = DSRegisterSOUNDxCNTGetPanning(reg);
133	ch->repeat = DSRegisterSOUNDxCNTGetRepeat(reg);
134	ch->format = DSRegisterSOUNDxCNTGetFormat(reg);
135	ch->duty = DSRegisterSOUNDxCNTGetDuty(reg);
136
137	if (ch->format > 2) {
138		mLOG(DS_AUDIO, STUB, "Unimplemented audio format %i", ch->format);
139	}
140
141	if (ch->enable && !DSRegisterSOUNDxCNTIsBusy(reg)) {
142		mTimingDeschedule(&audio->p->ds7.timing, &ch->updateEvent);
143	} else if (!ch->enable && DSRegisterSOUNDxCNTIsBusy(reg)) {
144		ch->offset = 0;
145		ch->lfsr = 0x4000;
146		mTimingDeschedule(&audio->p->ds7.timing, &ch->updateEvent);
147		mTimingSchedule(&audio->p->ds7.timing, &ch->updateEvent, 0);
148		if (ch->format == 2) {
149			uint32_t header = audio->p->ds7.cpu->memory.load32(audio->p->ds7.cpu, ch->source, NULL);
150			ch->offset += 4;
151			ch->adpcmStartSample = header & 0xFFFF;
152			ch->adpcmStartIndex = header >> 16;
153			ch->adpcmSample = ch->adpcmStartSample;
154			ch->adpcmIndex = ch->adpcmStartIndex;
155		}
156	}
157	ch->enable = DSRegisterSOUNDxCNTIsBusy(reg);
158}
159
160void DSAudioWriteSOUNDTMR(struct DSAudio* audio, int chan, uint16_t value) {
161	audio->ch[chan].period = (0x10000 - value) << 1;
162}
163
164void DSAudioWriteSOUNDPNT(struct DSAudio* audio, int chan, uint16_t value) {
165	audio->ch[chan].loopPoint = value << 2;
166}
167
168void DSAudioWriteSOUNDSAD(struct DSAudio* audio, int chan, uint32_t value) {
169	audio->ch[chan].source = value;
170}
171
172void DSAudioWriteSOUNDLEN(struct DSAudio* audio, int chan, uint32_t value) {
173	audio->ch[chan].length = value << 2;
174}
175
176static void _updateMixer(struct DSAudio* audio) {
177	int32_t sampleLeft = 0;
178	int32_t sampleRight = 0;
179	int ch;
180	for (ch = 0; ch < 16; ++ch) {
181		if (!audio->ch[ch].enable) {
182			continue;
183		}
184		int32_t sample = audio->ch[ch].sample << 4;
185		sample >>= audio->ch[ch].divider;
186		sample *= audio->ch[ch].volume;
187		sample >>= 2;
188
189		int32_t left = sample * (0x7F - audio->ch[ch].panning);
190		int32_t right = sample * audio->ch[ch].panning;
191		sampleLeft += left >>= 16;
192		sampleRight += right >>= 16;
193	}
194	audio->sampleLeft = sampleLeft >> 6;
195	audio->sampleRight = sampleRight >> 6;
196}
197
198static void _updateAdpcm(struct DSAudioChannel* ch, int sample) {
199	ch->sample = ch->adpcmSample;
200	if (ch->adpcmIndex < 0) {
201		ch->adpcmIndex = 0;
202	} else if (ch->adpcmIndex > 88) {
203		ch->adpcmIndex = 88;
204	}
205	int16_t diff = _adpcmTable[ch->adpcmIndex] >> 3;
206	if (sample & 1) {
207		diff += _adpcmTable[ch->adpcmIndex] >> 2;
208	}
209	if (sample & 2) {
210		diff += _adpcmTable[ch->adpcmIndex] >> 1;
211	}
212	if (sample & 4) {
213		diff += _adpcmTable[ch->adpcmIndex];
214	}
215	if (sample & 8) {
216		int32_t newSample = ch->adpcmSample - diff;
217		if (newSample < -0x7FFF) {
218			ch->adpcmSample = -0x7FFF;
219		} else {
220			ch->adpcmSample = newSample;
221		}
222	} else {
223		int32_t newSample = ch->adpcmSample + diff;
224		if (newSample > 0x7FFF) {
225			ch->adpcmSample = 0x7FFF;
226		} else {
227			ch->adpcmSample = newSample;
228		}
229	}
230	ch->adpcmIndex += _adpcmIndexTable[sample & 0x7];
231}
232
233static void _updateNoiseChannel(struct DSAudioChannel* ch) {
234	int lsb = ch->lfsr & 1;
235	ch->high = lsb;
236	ch->lfsr >>= 1;
237	ch->lfsr ^= lsb * 0x6000;
238}
239
240static void _updateChannel(struct mTiming* timing, void* user, uint32_t cyclesLate) {
241	struct DSAudioChannel* ch = user;
242	struct ARMCore* cpu = ch->p->p->ds7.cpu;
243	switch (ch->format) {
244	case 0:
245		ch->sample = cpu->memory.load8(cpu, ch->offset + ch->source, NULL) << 8;
246		++ch->offset;
247		break;
248	case 1:
249		ch->sample = cpu->memory.load16(cpu, ch->offset + ch->source, NULL);
250		ch->offset += 2;
251		break;
252	case 2:
253		_updateAdpcm(ch, (cpu->memory.load8(cpu, ch->offset + ch->source, NULL) >> ch->adpcmOffset) & 0xF);
254		ch->offset += ch->adpcmOffset >> 2;
255		ch->adpcmOffset ^= 4;
256		if (ch->offset == ch->loopPoint && !ch->adpcmOffset) {
257			ch->adpcmStartSample = ch->adpcmSample;
258			ch->adpcmStartIndex = ch->adpcmIndex;
259		}
260		break;
261	case 3:
262		switch (ch->index) {
263		case 8:
264		case 9:
265		case 10:
266		case 11:
267		case 12:
268		case 13:
269			ch->high = !ch->high;
270			break;
271		case 14:
272		case 15:
273			_updateNoiseChannel(ch);
274			break;
275		}
276		ch->sample = (0xFFFF * ch->high) - 0x8000;
277	}
278	_updateMixer(ch->p);
279	if (ch->format == 3 && ch->index < 14) {
280		int32_t period = ch->period;
281		if (ch->high) {
282			period *= ch->duty + 1;
283		} else {
284			period *= 7 - ch->duty;
285		}
286		mTimingSchedule(timing, &ch->updateEvent, period - cyclesLate);
287		return;
288	}
289
290	switch (ch->repeat) {
291	case 1:
292		if (ch->offset >= ch->length + ch->loopPoint) {
293			ch->offset = ch->loopPoint;
294			if (ch->format == 2) {
295				ch->adpcmSample = ch->adpcmStartSample;
296				ch->adpcmIndex = ch->adpcmStartIndex;
297			}
298		}
299		break;
300	case 2:
301		if (ch->offset >= ch->length + ch->loopPoint) {
302			ch->enable = false;
303			ch->p->p->memory.io7[(DS7_REG_SOUND0CNT_HI + (ch->index << 4)) >> 1] &= 0x7FFF;
304		}
305		break;
306	}
307	if (ch->enable) {
308		mTimingSchedule(timing, &ch->updateEvent, ch->period - cyclesLate);
309	}
310}
311
312static int _applyBias(struct DSAudio* audio, int sample) {
313	sample += audio->bias;
314	if (sample >= 0x400) {
315		sample = 0x3FF;
316	} else if (sample < 0) {
317		sample = 0;
318	}
319	return ((sample - audio->bias) * audio->masterVolume) >> 3;
320}
321
322static void _sample(struct mTiming* timing, void* user, uint32_t cyclesLate) {
323	struct DSAudio* audio = user;
324
325	int16_t sampleLeft = _applyBias(audio, audio->sampleLeft);
326	int16_t sampleRight = _applyBias(audio, audio->sampleRight);
327
328	mCoreSyncLockAudio(audio->p->sync);
329	unsigned produced;
330	if ((size_t) blip_samples_avail(audio->left) < audio->samples) {
331		blip_add_delta(audio->left, audio->clock, sampleLeft - audio->lastLeft);
332		blip_add_delta(audio->right, audio->clock, sampleRight - audio->lastRight);
333		audio->lastLeft = sampleLeft;
334		audio->lastRight = sampleRight;
335		audio->clock += audio->sampleInterval;
336		if (audio->clock >= CLOCKS_PER_FRAME) {
337			blip_end_frame(audio->left, audio->clock);
338			blip_end_frame(audio->right, audio->clock);
339			audio->clock -= CLOCKS_PER_FRAME;
340		}
341	}
342	produced = blip_samples_avail(audio->left);
343	if (audio->p->stream && audio->p->stream->postAudioFrame) {
344		audio->p->stream->postAudioFrame(audio->p->stream, sampleLeft, sampleRight);
345	}
346	bool wait = produced >= audio->samples;
347	mCoreSyncProduceAudio(audio->p->sync, wait);
348
349	if (wait && audio->p->stream && audio->p->stream->postAudioBuffer) {
350		audio->p->stream->postAudioBuffer(audio->p->stream, audio->left, audio->right);
351	}
352	mTimingSchedule(timing, &audio->sampleEvent, audio->sampleInterval - cyclesLate);
353}