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
136 if (ch->format > 2) {
137 mLOG(DS_AUDIO, STUB, "Unimplemented audio format %i", ch->format);
138 }
139
140 if (ch->enable && !DSRegisterSOUNDxCNTIsBusy(reg)) {
141 mTimingDeschedule(&audio->p->ds7.timing, &ch->updateEvent);
142 } else if (!ch->enable && DSRegisterSOUNDxCNTIsBusy(reg)) {
143 ch->offset = 0;
144 mTimingDeschedule(&audio->p->ds7.timing, &ch->updateEvent);
145 mTimingSchedule(&audio->p->ds7.timing, &ch->updateEvent, 0);
146 if (ch->format == 2) {
147 uint32_t header = audio->p->ds7.cpu->memory.load32(audio->p->ds7.cpu, ch->source, NULL);
148 ch->offset += 4;
149 ch->adpcmStartSample = header &= 0xFFFF;
150 ch->adpcmStartIndex = header >> 16;
151 }
152 }
153 ch->enable = DSRegisterSOUNDxCNTIsBusy(reg);
154}
155
156void DSAudioWriteSOUNDTMR(struct DSAudio* audio, int chan, uint16_t value) {
157 audio->ch[chan].period = (0x10000 - value) << 1;
158}
159
160void DSAudioWriteSOUNDPNT(struct DSAudio* audio, int chan, uint16_t value) {
161 audio->ch[chan].loopPoint = value << 2;
162}
163
164void DSAudioWriteSOUNDSAD(struct DSAudio* audio, int chan, uint32_t value) {
165 audio->ch[chan].source = value;
166}
167
168void DSAudioWriteSOUNDLEN(struct DSAudio* audio, int chan, uint32_t value) {
169 audio->ch[chan].length = value << 2;
170}
171
172static void _updateMixer(struct DSAudio* audio) {
173 int32_t sampleLeft = 0;
174 int32_t sampleRight = 0;
175 int ch;
176 for (ch = 0; ch < 16; ++ch) {
177 if (!audio->ch[ch].enable) {
178 continue;
179 }
180 int32_t sample = audio->ch[ch].sample << 4;
181 sample >>= audio->ch[ch].divider;
182 sample *= audio->ch[ch].volume;
183 sample >>= 2;
184
185 int32_t left = sample * (0x7F - audio->ch[ch].panning);
186 int32_t right = sample * audio->ch[ch].panning;
187 sampleLeft += left >>= 16;
188 sampleRight += right >>= 16;
189 }
190 audio->sampleLeft = sampleLeft >> 6;
191 audio->sampleRight = sampleRight >> 6;
192}
193
194static void _updateAdpcm(struct DSAudioChannel* ch, int sample) {
195 if (ch->adpcmIndex < 0) {
196 ch->adpcmIndex = 0;
197 } else if (ch->adpcmIndex > 88) {
198 ch->adpcmIndex = 88;
199 }
200 int16_t diff = _adpcmTable[ch->adpcmIndex] >> 3;
201 if (sample & 1) {
202 diff += _adpcmTable[ch->adpcmIndex] >> 2;
203 }
204 if (sample & 2) {
205 diff += _adpcmTable[ch->adpcmIndex] >> 1;
206 }
207 if (sample & 4) {
208 diff += _adpcmTable[ch->adpcmIndex];
209 }
210 if (sample & 8) {
211 int32_t newSample = ch->adpcmSample - diff;
212 if (newSample < -0x7FFF) {
213 ch->adpcmSample = -0x7FFF;
214 } else {
215 ch->adpcmSample = newSample;
216 }
217 } else {
218 int32_t newSample = ch->adpcmSample + diff;
219 if (newSample > 0x7FFF) {
220 ch->adpcmSample = 0x7FFF;
221 } else {
222 ch->adpcmSample = newSample;
223 }
224 }
225 ch->sample = ch->adpcmSample;
226 ch->adpcmIndex += _adpcmIndexTable[sample & 0x7];
227}
228
229static void _updateChannel(struct mTiming* timing, void* user, uint32_t cyclesLate) {
230 struct DSAudioChannel* ch = user;
231 struct ARMCore* cpu = ch->p->p->ds7.cpu;
232 switch (ch->format) {
233 case 0:
234 ch->sample = cpu->memory.load8(cpu, ch->offset + ch->source, NULL) << 8;
235 ++ch->offset;
236 break;
237 case 1:
238 ch->sample = cpu->memory.load16(cpu, ch->offset + ch->source, NULL);
239 ch->offset += 2;
240 break;
241 case 2:
242 _updateAdpcm(ch, (cpu->memory.load8(cpu, ch->offset + ch->source, NULL) >> ch->adpcmOffset) & 0xF);
243 ch->offset += ch->adpcmOffset >> 2;
244 ch->adpcmOffset ^= 4;
245 if (ch->offset == ch->loopPoint && !ch->adpcmOffset) {
246 ch->adpcmStartSample = ch->adpcmSample;
247 ch->adpcmStartIndex = ch->adpcmIndex;
248 }
249 break;
250 }
251 _updateMixer(ch->p);
252 switch (ch->repeat) {
253 case 1:
254 if (ch->offset >= ch->length + ch->loopPoint) {
255 ch->offset = ch->loopPoint;
256 if (ch->format == 2) {
257 ch->adpcmSample = ch->adpcmStartSample;
258 ch->adpcmIndex = ch->adpcmStartIndex;
259 }
260 }
261 break;
262 case 2:
263 if (ch->offset >= ch->length + ch->loopPoint) {
264 ch->enable = false;
265 ch->p->p->memory.io7[(DS7_REG_SOUND0CNT_HI + (ch->index << 4)) >> 1] &= 0x7FFF;
266 }
267 break;
268 }
269 if (ch->enable) {
270 mTimingSchedule(timing, &ch->updateEvent, ch->period - cyclesLate);
271 }
272}
273
274static int _applyBias(struct DSAudio* audio, int sample) {
275 sample += audio->bias;
276 if (sample >= 0x400) {
277 sample = 0x3FF;
278 } else if (sample < 0) {
279 sample = 0;
280 }
281 return ((sample - audio->bias) * audio->masterVolume) >> 3;
282}
283
284static void _sample(struct mTiming* timing, void* user, uint32_t cyclesLate) {
285 struct DSAudio* audio = user;
286
287 int16_t sampleLeft = _applyBias(audio, audio->sampleLeft);
288 int16_t sampleRight = _applyBias(audio, audio->sampleRight);
289
290 mCoreSyncLockAudio(audio->p->sync);
291 unsigned produced;
292 if ((size_t) blip_samples_avail(audio->left) < audio->samples) {
293 blip_add_delta(audio->left, audio->clock, sampleLeft - audio->lastLeft);
294 blip_add_delta(audio->right, audio->clock, sampleRight - audio->lastRight);
295 audio->lastLeft = sampleLeft;
296 audio->lastRight = sampleRight;
297 audio->clock += audio->sampleInterval;
298 if (audio->clock >= CLOCKS_PER_FRAME) {
299 blip_end_frame(audio->left, audio->clock);
300 blip_end_frame(audio->right, audio->clock);
301 audio->clock -= CLOCKS_PER_FRAME;
302 }
303 }
304 produced = blip_samples_avail(audio->left);
305 if (audio->p->stream && audio->p->stream->postAudioFrame) {
306 audio->p->stream->postAudioFrame(audio->p->stream, sampleLeft, sampleRight);
307 }
308 bool wait = produced >= audio->samples;
309 mCoreSyncProduceAudio(audio->p->sync, wait);
310
311 if (wait && audio->p->stream && audio->p->stream->postAudioBuffer) {
312 audio->p->stream->postAudioBuffer(audio->p->stream, audio->left, audio->right);
313 }
314 mTimingSchedule(timing, &audio->sampleEvent, audio->sampleInterval - cyclesLate);
315}