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 <mgba/internal/gba/audio.h>
7
8#include <mgba/internal/arm/macros.h>
9#include <mgba/core/blip_buf.h>
10#include <mgba/core/sync.h>
11#include <mgba/internal/gba/dma.h>
12#include <mgba/internal/gba/gba.h>
13#include <mgba/internal/gba/io.h>
14#include <mgba/internal/gba/serialize.h>
15#include <mgba/internal/gba/video.h>
16
17#define MP2K_LOCK_MAX 8
18
19#ifdef _3DS
20#define blip_add_delta blip_add_delta_fast
21#endif
22
23mLOG_DEFINE_CATEGORY(GBA_AUDIO, "GBA Audio", "gba.audio");
24
25const unsigned GBA_AUDIO_SAMPLES = 2048;
26const int GBA_AUDIO_VOLUME_MAX = 0x100;
27
28static const int CLOCKS_PER_FRAME = 0x800;
29
30static int _applyBias(struct GBAAudio* audio, int sample);
31static void _sample(struct mTiming* timing, void* user, uint32_t cyclesLate);
32
33void GBAAudioInit(struct GBAAudio* audio, size_t samples) {
34 audio->sampleEvent.context = audio;
35 audio->sampleEvent.name = "GBA Audio Sample";
36 audio->sampleEvent.callback = _sample;
37 audio->sampleEvent.priority = 0x18;
38 audio->psg.p = NULL;
39 uint8_t* nr52 = (uint8_t*) &audio->p->memory.io[REG_SOUNDCNT_X >> 1];
40#ifdef __BIG_ENDIAN__
41 ++nr52;
42#endif
43 GBAudioInit(&audio->psg, 0, nr52, GB_AUDIO_GBA);
44 audio->psg.timing = &audio->p->timing;
45 audio->psg.clockRate = GBA_ARM7TDMI_FREQUENCY;
46 audio->samples = samples;
47 // Guess too large; we hang producing extra samples if we guess too low
48 blip_set_rates(audio->psg.left, GBA_ARM7TDMI_FREQUENCY, 96000);
49 blip_set_rates(audio->psg.right, GBA_ARM7TDMI_FREQUENCY, 96000);
50
51 audio->externalMixing = false;
52 audio->forceDisableChA = false;
53 audio->forceDisableChB = false;
54 audio->masterVolume = GBA_AUDIO_VOLUME_MAX;
55 audio->mixer = NULL;
56}
57
58void GBAAudioReset(struct GBAAudio* audio) {
59 GBAudioReset(&audio->psg);
60 mTimingDeschedule(&audio->p->timing, &audio->sampleEvent);
61 mTimingSchedule(&audio->p->timing, &audio->sampleEvent, 0);
62 audio->chA.dmaSource = 1;
63 audio->chB.dmaSource = 2;
64 audio->chA.fifoWrite = 0;
65 audio->chA.fifoRead = 0;
66 audio->chA.internalSample = 0;
67 audio->chA.internalRemaining = 0;
68 memset(audio->chA.fifo, 0, sizeof(audio->chA.fifo));
69 audio->chA.sample = 0;
70 audio->chB.fifoWrite = 0;
71 audio->chB.fifoRead = 0;
72 audio->chB.internalSample = 0;
73 audio->chB.internalRemaining = 0;
74 memset(audio->chB.fifo, 0, sizeof(audio->chB.fifo));
75 audio->chB.sample = 0;
76 audio->sampleRate = 0x8000;
77 audio->soundbias = 0x200;
78 audio->volume = 0;
79 audio->volumeChA = false;
80 audio->volumeChB = false;
81 audio->chARight = false;
82 audio->chALeft = false;
83 audio->chATimer = false;
84 audio->chBRight = false;
85 audio->chBLeft = false;
86 audio->chBTimer = false;
87 audio->enable = false;
88 audio->sampleInterval = GBA_ARM7TDMI_FREQUENCY / audio->sampleRate;
89 audio->psg.sampleInterval = audio->sampleInterval;
90
91 blip_clear(audio->psg.left);
92 blip_clear(audio->psg.right);
93 audio->clock = 0;
94}
95
96void GBAAudioDeinit(struct GBAAudio* audio) {
97 GBAudioDeinit(&audio->psg);
98}
99
100void GBAAudioResizeBuffer(struct GBAAudio* audio, size_t samples) {
101 mCoreSyncLockAudio(audio->p->sync);
102 audio->samples = samples;
103 blip_clear(audio->psg.left);
104 blip_clear(audio->psg.right);
105 audio->clock = 0;
106 mCoreSyncConsumeAudio(audio->p->sync);
107}
108
109void GBAAudioScheduleFifoDma(struct GBAAudio* audio, int number, struct GBADMA* info) {
110 info->reg = GBADMARegisterSetDestControl(info->reg, GBA_DMA_FIXED);
111 info->reg = GBADMARegisterSetWidth(info->reg, 1);
112 switch (info->dest) {
113 case BASE_IO | REG_FIFO_A_LO:
114 audio->chA.dmaSource = number;
115 break;
116 case BASE_IO | REG_FIFO_B_LO:
117 audio->chB.dmaSource = number;
118 break;
119 default:
120 mLOG(GBA_AUDIO, GAME_ERROR, "Invalid FIFO destination: 0x%08X", info->dest);
121 return;
122 }
123 uint32_t source = info->source;
124 uint32_t magic[2] = {
125 audio->p->cpu->memory.load32(audio->p->cpu, source - 0x350, NULL),
126 audio->p->cpu->memory.load32(audio->p->cpu, source - 0x980, NULL)
127 };
128 if (audio->mixer) {
129 if (magic[0] - MP2K_MAGIC <= MP2K_LOCK_MAX) {
130 audio->mixer->engage(audio->mixer, source - 0x350);
131 } else if (magic[1] - MP2K_MAGIC <= MP2K_LOCK_MAX) {
132 audio->mixer->engage(audio->mixer, source - 0x980);
133 } else {
134 audio->externalMixing = false;
135 }
136 }
137}
138
139void GBAAudioWriteSOUND1CNT_LO(struct GBAAudio* audio, uint16_t value) {
140 GBAudioWriteNR10(&audio->psg, value);
141}
142
143void GBAAudioWriteSOUND1CNT_HI(struct GBAAudio* audio, uint16_t value) {
144 GBAudioWriteNR11(&audio->psg, value);
145 GBAudioWriteNR12(&audio->psg, value >> 8);
146}
147
148void GBAAudioWriteSOUND1CNT_X(struct GBAAudio* audio, uint16_t value) {
149 GBAudioWriteNR13(&audio->psg, value);
150 GBAudioWriteNR14(&audio->psg, value >> 8);
151}
152
153void GBAAudioWriteSOUND2CNT_LO(struct GBAAudio* audio, uint16_t value) {
154 GBAudioWriteNR21(&audio->psg, value);
155 GBAudioWriteNR22(&audio->psg, value >> 8);
156}
157
158void GBAAudioWriteSOUND2CNT_HI(struct GBAAudio* audio, uint16_t value) {
159 GBAudioWriteNR23(&audio->psg, value);
160 GBAudioWriteNR24(&audio->psg, value >> 8);
161}
162
163void GBAAudioWriteSOUND3CNT_LO(struct GBAAudio* audio, uint16_t value) {
164 audio->psg.ch3.size = GBAudioRegisterBankGetSize(value);
165 audio->psg.ch3.bank = GBAudioRegisterBankGetBank(value);
166 GBAudioWriteNR30(&audio->psg, value);
167}
168
169void GBAAudioWriteSOUND3CNT_HI(struct GBAAudio* audio, uint16_t value) {
170 GBAudioWriteNR31(&audio->psg, value);
171 audio->psg.ch3.volume = GBAudioRegisterBankVolumeGetVolumeGBA(value >> 8);
172}
173
174void GBAAudioWriteSOUND3CNT_X(struct GBAAudio* audio, uint16_t value) {
175 GBAudioWriteNR33(&audio->psg, value);
176 GBAudioWriteNR34(&audio->psg, value >> 8);
177}
178
179void GBAAudioWriteSOUND4CNT_LO(struct GBAAudio* audio, uint16_t value) {
180 GBAudioWriteNR41(&audio->psg, value);
181 GBAudioWriteNR42(&audio->psg, value >> 8);
182}
183
184void GBAAudioWriteSOUND4CNT_HI(struct GBAAudio* audio, uint16_t value) {
185 GBAudioWriteNR43(&audio->psg, value);
186 GBAudioWriteNR44(&audio->psg, value >> 8);
187}
188
189void GBAAudioWriteSOUNDCNT_LO(struct GBAAudio* audio, uint16_t value) {
190 GBAudioWriteNR50(&audio->psg, value);
191 GBAudioWriteNR51(&audio->psg, value >> 8);
192}
193
194void GBAAudioWriteSOUNDCNT_HI(struct GBAAudio* audio, uint16_t value) {
195 audio->volume = GBARegisterSOUNDCNT_HIGetVolume(value);
196 audio->volumeChA = GBARegisterSOUNDCNT_HIGetVolumeChA(value);
197 audio->volumeChB = GBARegisterSOUNDCNT_HIGetVolumeChB(value);
198 audio->chARight = GBARegisterSOUNDCNT_HIGetChARight(value);
199 audio->chALeft = GBARegisterSOUNDCNT_HIGetChALeft(value);
200 audio->chATimer = GBARegisterSOUNDCNT_HIGetChATimer(value);
201 audio->chBRight = GBARegisterSOUNDCNT_HIGetChBRight(value);
202 audio->chBLeft = GBARegisterSOUNDCNT_HIGetChBLeft(value);
203 audio->chBTimer = GBARegisterSOUNDCNT_HIGetChBTimer(value);
204 if (GBARegisterSOUNDCNT_HIIsChAReset(value)) {
205 audio->chA.fifoWrite = 0;
206 audio->chA.fifoRead = 0;
207 }
208 if (GBARegisterSOUNDCNT_HIIsChBReset(value)) {
209 audio->chB.fifoWrite = 0;
210 audio->chB.fifoRead = 0;
211 }
212}
213
214void GBAAudioWriteSOUNDCNT_X(struct GBAAudio* audio, uint16_t value) {
215 audio->enable = GBAudioEnableGetEnable(value);
216 GBAudioWriteNR52(&audio->psg, value);
217}
218
219void GBAAudioWriteSOUNDBIAS(struct GBAAudio* audio, uint16_t value) {
220 audio->soundbias = value;
221}
222
223void GBAAudioWriteWaveRAM(struct GBAAudio* audio, int address, uint32_t value) {
224 audio->psg.ch3.wavedata32[address | (!audio->psg.ch3.bank * 4)] = value;
225}
226
227uint32_t GBAAudioWriteFIFO(struct GBAAudio* audio, int address, uint32_t value) {
228 struct GBAAudioFIFO* channel;
229 switch (address) {
230 case REG_FIFO_A_LO:
231 channel = &audio->chA;
232 break;
233 case REG_FIFO_B_LO:
234 channel = &audio->chB;
235 break;
236 default:
237 mLOG(GBA_AUDIO, ERROR, "Bad FIFO write to address 0x%03x", address);
238 return value;
239 }
240 channel->fifo[channel->fifoWrite] = value;
241 ++channel->fifoWrite;
242 if (channel->fifoWrite == GBA_AUDIO_FIFO_SIZE) {
243 channel->fifoWrite = 0;
244 }
245 return channel->fifo[channel->fifoWrite];
246}
247
248void GBAAudioSampleFIFO(struct GBAAudio* audio, int fifoId, int32_t cycles) {
249 struct GBAAudioFIFO* channel;
250 if (fifoId == 0) {
251 channel = &audio->chA;
252 } else if (fifoId == 1) {
253 channel = &audio->chB;
254 } else {
255 mLOG(GBA_AUDIO, ERROR, "Bad FIFO write to address 0x%03x", fifoId);
256 return;
257 }
258 int fifoSize;
259 if (channel->fifoWrite >= channel->fifoRead) {
260 fifoSize = channel->fifoWrite - channel->fifoRead;
261 } else {
262 fifoSize = GBA_AUDIO_FIFO_SIZE - channel->fifoRead + channel->fifoWrite;
263 }
264 if (GBA_AUDIO_FIFO_SIZE - fifoSize > 4 && channel->dmaSource > 0) {
265 struct GBADMA* dma = &audio->p->memory.dma[channel->dmaSource];
266 if (GBADMARegisterGetTiming(dma->reg) == GBA_DMA_TIMING_CUSTOM) {
267 dma->when = mTimingCurrentTime(&audio->p->timing) - cycles;
268 dma->nextCount = 4;
269 GBADMASchedule(audio->p, channel->dmaSource, dma);
270 }
271 }
272 if (!channel->internalRemaining && fifoSize) {
273 channel->internalSample = channel->fifo[channel->fifoRead];
274 channel->internalRemaining = 4;
275 ++channel->fifoRead;
276 if (channel->fifoRead == GBA_AUDIO_FIFO_SIZE) {
277 channel->fifoRead = 0;
278 }
279 }
280 channel->sample = channel->internalSample;
281 if (channel->internalRemaining) {
282 channel->internalSample >>= 8;
283 --channel->internalRemaining;
284 }
285}
286
287static int _applyBias(struct GBAAudio* audio, int sample) {
288 sample += GBARegisterSOUNDBIASGetBias(audio->soundbias);
289 if (sample >= 0x400) {
290 sample = 0x3FF;
291 } else if (sample < 0) {
292 sample = 0;
293 }
294 return ((sample - GBARegisterSOUNDBIASGetBias(audio->soundbias)) * audio->masterVolume * 3) >> 4;
295}
296
297static void _sample(struct mTiming* timing, void* user, uint32_t cyclesLate) {
298 struct GBAAudio* audio = user;
299 int16_t sampleLeft = 0;
300 int16_t sampleRight = 0;
301 int psgShift = 4 - audio->volume;
302 GBAudioSamplePSG(&audio->psg, &sampleLeft, &sampleRight);
303 sampleLeft >>= psgShift;
304 sampleRight >>= psgShift;
305
306 if (audio->mixer) {
307 audio->mixer->step(audio->mixer);
308 }
309 if (!audio->externalMixing) {
310 if (!audio->forceDisableChA) {
311 if (audio->chALeft) {
312 sampleLeft += (audio->chA.sample << 2) >> !audio->volumeChA;
313 }
314
315 if (audio->chARight) {
316 sampleRight += (audio->chA.sample << 2) >> !audio->volumeChA;
317 }
318 }
319
320 if (!audio->forceDisableChB) {
321 if (audio->chBLeft) {
322 sampleLeft += (audio->chB.sample << 2) >> !audio->volumeChB;
323 }
324
325 if (audio->chBRight) {
326 sampleRight += (audio->chB.sample << 2) >> !audio->volumeChB;
327 }
328 }
329 }
330
331 sampleLeft = _applyBias(audio, sampleLeft);
332 sampleRight = _applyBias(audio, sampleRight);
333
334 mCoreSyncLockAudio(audio->p->sync);
335 unsigned produced;
336 if ((size_t) blip_samples_avail(audio->psg.left) < audio->samples) {
337 blip_add_delta(audio->psg.left, audio->clock, sampleLeft - audio->lastLeft);
338 blip_add_delta(audio->psg.right, audio->clock, sampleRight - audio->lastRight);
339 audio->lastLeft = sampleLeft;
340 audio->lastRight = sampleRight;
341 audio->clock += audio->sampleInterval;
342 if (audio->clock >= CLOCKS_PER_FRAME) {
343 blip_end_frame(audio->psg.left, CLOCKS_PER_FRAME);
344 blip_end_frame(audio->psg.right, CLOCKS_PER_FRAME);
345 audio->clock -= CLOCKS_PER_FRAME;
346 }
347 }
348 produced = blip_samples_avail(audio->psg.left);
349 if (audio->p->stream && audio->p->stream->postAudioFrame) {
350 audio->p->stream->postAudioFrame(audio->p->stream, sampleLeft, sampleRight);
351 }
352 bool wait = produced >= audio->samples;
353 if (!mCoreSyncProduceAudio(audio->p->sync, audio->psg.left, audio->samples)) {
354 // Interrupted
355 audio->p->earlyExit = true;
356 }
357
358 if (wait && audio->p->stream && audio->p->stream->postAudioBuffer) {
359 audio->p->stream->postAudioBuffer(audio->p->stream, audio->psg.left, audio->psg.right);
360 }
361
362 mTimingSchedule(timing, &audio->sampleEvent, audio->sampleInterval - cyclesLate);
363}
364
365void GBAAudioSerialize(const struct GBAAudio* audio, struct GBASerializedState* state) {
366 GBAudioPSGSerialize(&audio->psg, &state->audio.psg, &state->audio.flags);
367
368 STORE_32(audio->chA.internalSample, 0, &state->audio.internalA);
369 STORE_32(audio->chB.internalSample, 0, &state->audio.internalB);
370 state->audio.sampleA = audio->chA.sample;
371 state->audio.sampleB = audio->chB.sample;
372
373 int readA = audio->chA.fifoRead;
374 int readB = audio->chB.fifoRead;
375 size_t i;
376 for (i = 0; i < GBA_AUDIO_FIFO_SIZE; ++i) {
377 STORE_32(audio->chA.fifo[readA], i << 2, state->audio.fifoA);
378 STORE_32(audio->chB.fifo[readB], i << 2, state->audio.fifoB);
379 ++readA;
380 if (readA == GBA_AUDIO_FIFO_SIZE) {
381 readA = 0;
382 }
383 ++readB;
384 if (readB == GBA_AUDIO_FIFO_SIZE) {
385 readB = 0;
386 }
387 }
388
389 int fifoSizeA;
390 if (audio->chA.fifoWrite >= audio->chA.fifoRead) {
391 fifoSizeA = audio->chA.fifoWrite - audio->chA.fifoRead;
392 } else {
393 fifoSizeA = GBA_AUDIO_FIFO_SIZE - audio->chA.fifoRead + audio->chA.fifoWrite;
394 }
395
396 int fifoSizeB;
397 if (audio->chB.fifoWrite >= audio->chB.fifoRead) {
398 fifoSizeB = audio->chB.fifoWrite - audio->chB.fifoRead;
399 } else {
400 fifoSizeB = GBA_AUDIO_FIFO_SIZE - audio->chB.fifoRead + audio->chB.fifoWrite;
401 }
402
403 GBASerializedAudioFlags flags = 0;
404 flags = GBASerializedAudioFlagsSetFIFOSamplesA(flags, fifoSizeA);
405 flags = GBASerializedAudioFlagsSetFIFOSamplesB(flags, fifoSizeB);
406 flags = GBASerializedAudioFlagsSetFIFOInternalSamplesA(flags, audio->chA.internalRemaining);
407 flags = GBASerializedAudioFlagsSetFIFOInternalSamplesB(flags, audio->chB.internalRemaining);
408 STORE_16(flags, 0, &state->audio.gbaFlags);
409 STORE_32(audio->sampleEvent.when - mTimingCurrentTime(&audio->p->timing), 0, &state->audio.nextSample);
410}
411
412void GBAAudioDeserialize(struct GBAAudio* audio, const struct GBASerializedState* state) {
413 GBAudioPSGDeserialize(&audio->psg, &state->audio.psg, &state->audio.flags);
414
415 LOAD_32(audio->chA.internalSample, 0, &state->audio.internalA);
416 LOAD_32(audio->chB.internalSample, 0, &state->audio.internalB);
417 audio->chA.sample = state->audio.sampleA;
418 audio->chB.sample = state->audio.sampleB;
419
420 int readA = 0;
421 int readB = 0;
422 size_t i;
423 for (i = 0; i < GBA_AUDIO_FIFO_SIZE; ++i) {
424 LOAD_32(audio->chA.fifo[readA], i << 2, state->audio.fifoA);
425 LOAD_32(audio->chB.fifo[readB], i << 2, state->audio.fifoB);
426 ++readA;
427 ++readB;
428 }
429 audio->chA.fifoRead = 0;
430 audio->chB.fifoRead = 0;
431
432 GBASerializedAudioFlags flags;
433 LOAD_16(flags, 0, &state->audio.gbaFlags);
434 audio->chA.fifoWrite = GBASerializedAudioFlagsGetFIFOSamplesA(flags);
435 audio->chB.fifoWrite = GBASerializedAudioFlagsGetFIFOSamplesB(flags);
436 audio->chA.internalRemaining = GBASerializedAudioFlagsGetFIFOInternalSamplesA(flags);
437 audio->chB.internalRemaining = GBASerializedAudioFlagsGetFIFOInternalSamplesB(flags);
438
439 uint32_t when;
440 LOAD_32(when, 0, &state->audio.nextSample);
441 mTimingSchedule(&audio->p->timing, &audio->sampleEvent, when);
442}
443
444float GBAAudioCalculateRatio(float inputSampleRate, float desiredFPS, float desiredSampleRate) {
445 return desiredSampleRate * GBA_ARM7TDMI_FREQUENCY / (VIDEO_TOTAL_LENGTH * desiredFPS * inputSampleRate);
446}