src/gb/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/gb/audio.h>
7
8#include <mgba/core/blip_buf.h>
9#include <mgba/core/interface.h>
10#include <mgba/core/sync.h>
11#include <mgba/internal/gb/gb.h>
12#include <mgba/internal/gb/serialize.h>
13#include <mgba/internal/gb/io.h>
14
15#ifdef _3DS
16#define blip_add_delta blip_add_delta_fast
17#endif
18
19#define FRAME_CYCLES (DMG_LR35902_FREQUENCY >> 9)
20
21const uint32_t DMG_LR35902_FREQUENCY = 0x400000;
22static const int CLOCKS_PER_BLIP_FRAME = 0x1000;
23static const unsigned BLIP_BUFFER_SIZE = 0x4000;
24const int GB_AUDIO_VOLUME_MAX = 0x100;
25
26static bool _writeSweep(struct GBAudioSweep* sweep, uint8_t value);
27static void _writeDuty(struct GBAudioEnvelope* envelope, uint8_t value);
28static bool _writeEnvelope(struct GBAudioEnvelope* envelope, uint8_t value, enum GBAudioStyle style);
29
30static void _resetSweep(struct GBAudioSweep* sweep);
31static bool _resetEnvelope(struct GBAudioEnvelope* sweep);
32
33static void _updateEnvelope(struct GBAudioEnvelope* envelope);
34static void _updateEnvelopeDead(struct GBAudioEnvelope* envelope);
35static bool _updateSweep(struct GBAudioSquareChannel* sweep, bool initial);
36
37static void _updateSquareSample(struct GBAudioSquareChannel* ch);
38static int32_t _updateSquareChannel(struct GBAudioSquareChannel* ch);
39
40static void _updateFrame(struct mTiming* timing, void* user, uint32_t cyclesLate);
41static void _updateChannel1(struct mTiming* timing, void* user, uint32_t cyclesLate);
42static void _updateChannel2(struct mTiming* timing, void* user, uint32_t cyclesLate);
43static void _updateChannel3(struct mTiming* timing, void* user, uint32_t cyclesLate);
44static void _fadeChannel3(struct mTiming* timing, void* user, uint32_t cyclesLate);
45static void _updateChannel4(struct mTiming* timing, void* user, uint32_t cyclesLate);
46static void _sample(struct mTiming* timing, void* user, uint32_t cyclesLate);
47
48void GBAudioInit(struct GBAudio* audio, size_t samples, uint8_t* nr52, enum GBAudioStyle style) {
49 audio->samples = samples;
50 audio->left = blip_new(BLIP_BUFFER_SIZE);
51 audio->right = blip_new(BLIP_BUFFER_SIZE);
52 audio->clockRate = DMG_LR35902_FREQUENCY;
53 // Guess too large; we hang producing extra samples if we guess too low
54 blip_set_rates(audio->left, DMG_LR35902_FREQUENCY, 96000);
55 blip_set_rates(audio->right, DMG_LR35902_FREQUENCY, 96000);
56 audio->forceDisableCh[0] = false;
57 audio->forceDisableCh[1] = false;
58 audio->forceDisableCh[2] = false;
59 audio->forceDisableCh[3] = false;
60 audio->masterVolume = GB_AUDIO_VOLUME_MAX;
61 audio->nr52 = nr52;
62 audio->style = style;
63 if (style == GB_AUDIO_GBA) {
64 audio->timingFactor = 4;
65 } else {
66 audio->timingFactor = 1;
67 }
68
69 audio->frameEvent.context = audio;
70 audio->frameEvent.name = "GB Audio Frame Sequencer";
71 audio->frameEvent.callback = _updateFrame;
72 audio->frameEvent.priority = 0x10;
73 audio->ch1Event.context = audio;
74 audio->ch1Event.name = "GB Audio Channel 1";
75 audio->ch1Event.callback = _updateChannel1;
76 audio->ch1Event.priority = 0x11;
77 audio->ch2Event.context = audio;
78 audio->ch2Event.name = "GB Audio Channel 2";
79 audio->ch2Event.callback = _updateChannel2;
80 audio->ch2Event.priority = 0x12;
81 audio->ch3Event.context = audio;
82 audio->ch3Event.name = "GB Audio Channel 3";
83 audio->ch3Event.callback = _updateChannel3;
84 audio->ch3Event.priority = 0x13;
85 audio->ch3Fade.context = audio;
86 audio->ch3Fade.name = "GB Audio Channel 3 Memory";
87 audio->ch3Fade.callback = _fadeChannel3;
88 audio->ch3Fade.priority = 0x14;
89 audio->ch4Event.context = audio;
90 audio->ch4Event.name = "GB Audio Channel 4";
91 audio->ch4Event.callback = _updateChannel4;
92 audio->ch4Event.priority = 0x15;
93 audio->sampleEvent.context = audio;
94 audio->sampleEvent.name = "GB Audio Sample";
95 audio->sampleEvent.callback = _sample;
96 audio->ch1Event.priority = 0x18;
97}
98
99void GBAudioDeinit(struct GBAudio* audio) {
100 blip_delete(audio->left);
101 blip_delete(audio->right);
102}
103
104void GBAudioReset(struct GBAudio* audio) {
105 mTimingDeschedule(audio->timing, &audio->frameEvent);
106 mTimingDeschedule(audio->timing, &audio->ch1Event);
107 mTimingDeschedule(audio->timing, &audio->ch2Event);
108 mTimingDeschedule(audio->timing, &audio->ch3Event);
109 mTimingDeschedule(audio->timing, &audio->ch3Fade);
110 mTimingDeschedule(audio->timing, &audio->ch4Event);
111 mTimingDeschedule(audio->timing, &audio->sampleEvent);
112 if (audio->style != GB_AUDIO_GBA) {
113 mTimingSchedule(audio->timing, &audio->sampleEvent, 0);
114 }
115 if (audio->style == GB_AUDIO_GBA) {
116 mTimingSchedule(audio->timing, &audio->frameEvent, 0);
117 }
118 audio->ch1 = (struct GBAudioSquareChannel) { .envelope = { .dead = 2 } };
119 audio->ch2 = (struct GBAudioSquareChannel) { .envelope = { .dead = 2 } };
120 audio->ch3 = (struct GBAudioWaveChannel) { .bank = 0 };
121 // TODO: DMG randomness
122 audio->ch3.wavedata8[0] = 0x00;
123 audio->ch3.wavedata8[1] = 0xFF;
124 audio->ch3.wavedata8[2] = 0x00;
125 audio->ch3.wavedata8[3] = 0xFF;
126 audio->ch3.wavedata8[4] = 0x00;
127 audio->ch3.wavedata8[5] = 0xFF;
128 audio->ch3.wavedata8[6] = 0x00;
129 audio->ch3.wavedata8[7] = 0xFF;
130 audio->ch3.wavedata8[8] = 0x00;
131 audio->ch3.wavedata8[9] = 0xFF;
132 audio->ch3.wavedata8[10] = 0x00;
133 audio->ch3.wavedata8[11] = 0xFF;
134 audio->ch3.wavedata8[12] = 0x00;
135 audio->ch3.wavedata8[13] = 0xFF;
136 audio->ch3.wavedata8[14] = 0x00;
137 audio->ch3.wavedata8[15] = 0xFF;
138 audio->ch4 = (struct GBAudioNoiseChannel) { .envelope = { .dead = 2 } };
139 audio->frame = 0;
140 audio->sampleInterval = 128;
141 audio->lastLeft = 0;
142 audio->lastRight = 0;
143 audio->capLeft = 0;
144 audio->capRight = 0;
145 audio->clock = 0;
146 audio->playingCh1 = false;
147 audio->playingCh2 = false;
148 audio->playingCh3 = false;
149 audio->playingCh4 = false;
150 if (audio->p && audio->p->model != GB_MODEL_SGB) {
151 audio->playingCh1 = true;
152 audio->enable = true;
153 *audio->nr52 |= 0x01;
154 }
155}
156
157void GBAudioResizeBuffer(struct GBAudio* audio, size_t samples) {
158 mCoreSyncLockAudio(audio->p->sync);
159 audio->samples = samples;
160 blip_clear(audio->left);
161 blip_clear(audio->right);
162 audio->clock = 0;
163 mCoreSyncConsumeAudio(audio->p->sync);
164}
165
166void GBAudioWriteNR10(struct GBAudio* audio, uint8_t value) {
167 if (!_writeSweep(&audio->ch1.sweep, value)) {
168 mTimingDeschedule(audio->timing, &audio->ch1Event);
169 audio->playingCh1 = false;
170 *audio->nr52 &= ~0x0001;
171 }
172}
173
174void GBAudioWriteNR11(struct GBAudio* audio, uint8_t value) {
175 _writeDuty(&audio->ch1.envelope, value);
176 audio->ch1.control.length = 64 - audio->ch1.envelope.length;
177}
178
179void GBAudioWriteNR12(struct GBAudio* audio, uint8_t value) {
180 if (!_writeEnvelope(&audio->ch1.envelope, value, audio->style)) {
181 mTimingDeschedule(audio->timing, &audio->ch1Event);
182 audio->playingCh1 = false;
183 *audio->nr52 &= ~0x0001;
184 }
185}
186
187void GBAudioWriteNR13(struct GBAudio* audio, uint8_t value) {
188 audio->ch1.control.frequency &= 0x700;
189 audio->ch1.control.frequency |= GBAudioRegisterControlGetFrequency(value);
190}
191
192void GBAudioWriteNR14(struct GBAudio* audio, uint8_t value) {
193 audio->ch1.control.frequency &= 0xFF;
194 audio->ch1.control.frequency |= GBAudioRegisterControlGetFrequency(value << 8);
195 bool wasStop = audio->ch1.control.stop;
196 audio->ch1.control.stop = GBAudioRegisterControlGetStop(value << 8);
197 if (!wasStop && audio->ch1.control.stop && audio->ch1.control.length && !(audio->frame & 1)) {
198 --audio->ch1.control.length;
199 if (audio->ch1.control.length == 0) {
200 mTimingDeschedule(audio->timing, &audio->ch1Event);
201 audio->playingCh1 = false;
202 }
203 }
204 if (GBAudioRegisterControlIsRestart(value << 8)) {
205 audio->playingCh1 = _resetEnvelope(&audio->ch1.envelope);
206
207 if (audio->playingCh1) {
208 _updateSquareSample(&audio->ch1);
209 }
210
211 audio->ch1.sweep.realFrequency = audio->ch1.control.frequency;
212 _resetSweep(&audio->ch1.sweep);
213 if (audio->playingCh1 && audio->ch1.sweep.shift) {
214 audio->playingCh1 = _updateSweep(&audio->ch1, true);
215 }
216 if (!audio->ch1.control.length) {
217 audio->ch1.control.length = 64;
218 if (audio->ch1.control.stop && !(audio->frame & 1)) {
219 --audio->ch1.control.length;
220 }
221 }
222 if (audio->playingCh1 && audio->ch1.envelope.dead != 2 && !mTimingIsScheduled(audio->timing, &audio->ch1Event)) {
223 mTimingSchedule(audio->timing, &audio->ch1Event, 0);
224 }
225 }
226 *audio->nr52 &= ~0x0001;
227 *audio->nr52 |= audio->playingCh1;
228}
229
230void GBAudioWriteNR21(struct GBAudio* audio, uint8_t value) {
231 _writeDuty(&audio->ch2.envelope, value);
232 audio->ch2.control.length = 64 - audio->ch2.envelope.length;
233}
234
235void GBAudioWriteNR22(struct GBAudio* audio, uint8_t value) {
236 if (!_writeEnvelope(&audio->ch2.envelope, value, audio->style)) {
237 mTimingDeschedule(audio->timing, &audio->ch2Event);
238 audio->playingCh2 = false;
239 *audio->nr52 &= ~0x0002;
240 }
241}
242
243void GBAudioWriteNR23(struct GBAudio* audio, uint8_t value) {
244 audio->ch2.control.frequency &= 0x700;
245 audio->ch2.control.frequency |= GBAudioRegisterControlGetFrequency(value);
246}
247
248void GBAudioWriteNR24(struct GBAudio* audio, uint8_t value) {
249 audio->ch2.control.frequency &= 0xFF;
250 audio->ch2.control.frequency |= GBAudioRegisterControlGetFrequency(value << 8);
251 bool wasStop = audio->ch2.control.stop;
252 audio->ch2.control.stop = GBAudioRegisterControlGetStop(value << 8);
253 if (!wasStop && audio->ch2.control.stop && audio->ch2.control.length && !(audio->frame & 1)) {
254 --audio->ch2.control.length;
255 if (audio->ch2.control.length == 0) {
256 mTimingDeschedule(audio->timing, &audio->ch2Event);
257 audio->playingCh2 = false;
258 }
259 }
260 if (GBAudioRegisterControlIsRestart(value << 8)) {
261 audio->playingCh2 = _resetEnvelope(&audio->ch2.envelope);
262
263 if (audio->playingCh2) {
264 _updateSquareSample(&audio->ch2);
265 }
266
267 if (!audio->ch2.control.length) {
268 audio->ch2.control.length = 64;
269 if (audio->ch2.control.stop && !(audio->frame & 1)) {
270 --audio->ch2.control.length;
271 }
272 }
273 if (audio->playingCh2 && audio->ch2.envelope.dead != 2 && !mTimingIsScheduled(audio->timing, &audio->ch2Event)) {
274 mTimingSchedule(audio->timing, &audio->ch2Event, 0);
275 }
276 }
277 *audio->nr52 &= ~0x0002;
278 *audio->nr52 |= audio->playingCh2 << 1;
279}
280
281void GBAudioWriteNR30(struct GBAudio* audio, uint8_t value) {
282 audio->ch3.enable = GBAudioRegisterBankGetEnable(value);
283 if (!audio->ch3.enable) {
284 audio->playingCh3 = false;
285 *audio->nr52 &= ~0x0004;
286 }
287}
288
289void GBAudioWriteNR31(struct GBAudio* audio, uint8_t value) {
290 audio->ch3.length = 256 - value;
291}
292
293void GBAudioWriteNR32(struct GBAudio* audio, uint8_t value) {
294 audio->ch3.volume = GBAudioRegisterBankVolumeGetVolumeGB(value);
295}
296
297void GBAudioWriteNR33(struct GBAudio* audio, uint8_t value) {
298 audio->ch3.rate &= 0x700;
299 audio->ch3.rate |= GBAudioRegisterControlGetRate(value);
300}
301
302void GBAudioWriteNR34(struct GBAudio* audio, uint8_t value) {
303 audio->ch3.rate &= 0xFF;
304 audio->ch3.rate |= GBAudioRegisterControlGetRate(value << 8);
305 bool wasStop = audio->ch3.stop;
306 audio->ch3.stop = GBAudioRegisterControlGetStop(value << 8);
307 if (!wasStop && audio->ch3.stop && audio->ch3.length && !(audio->frame & 1)) {
308 --audio->ch3.length;
309 if (audio->ch3.length == 0) {
310 audio->playingCh3 = false;
311 }
312 }
313 bool wasEnable = audio->playingCh3;
314 if (GBAudioRegisterControlIsRestart(value << 8)) {
315 audio->playingCh3 = audio->ch3.enable;
316 if (!audio->ch3.length) {
317 audio->ch3.length = 256;
318 if (audio->ch3.stop && !(audio->frame & 1)) {
319 --audio->ch3.length;
320 }
321 }
322
323 if (audio->style == GB_AUDIO_DMG && wasEnable && audio->playingCh3 && audio->ch3.readable) {
324 if (audio->ch3.window < 8) {
325 audio->ch3.wavedata8[0] = audio->ch3.wavedata8[audio->ch3.window >> 1];
326 } else {
327 audio->ch3.wavedata8[0] = audio->ch3.wavedata8[((audio->ch3.window >> 1) & ~3)];
328 audio->ch3.wavedata8[1] = audio->ch3.wavedata8[((audio->ch3.window >> 1) & ~3) + 1];
329 audio->ch3.wavedata8[2] = audio->ch3.wavedata8[((audio->ch3.window >> 1) & ~3) + 2];
330 audio->ch3.wavedata8[3] = audio->ch3.wavedata8[((audio->ch3.window >> 1) & ~3) + 3];
331 }
332 }
333 audio->ch3.window = 0;
334 }
335 mTimingDeschedule(audio->timing, &audio->ch3Fade);
336 mTimingDeschedule(audio->timing, &audio->ch3Event);
337 if (audio->playingCh3) {
338 audio->ch3.readable = audio->style != GB_AUDIO_DMG;
339 // TODO: Where does this cycle delay come from?
340 mTimingSchedule(audio->timing, &audio->ch3Event, audio->timingFactor * 4 + 2 * (2048 - audio->ch3.rate));
341 }
342 *audio->nr52 &= ~0x0004;
343 *audio->nr52 |= audio->playingCh3 << 2;
344}
345
346void GBAudioWriteNR41(struct GBAudio* audio, uint8_t value) {
347 _writeDuty(&audio->ch4.envelope, value);
348 audio->ch4.length = 64 - audio->ch4.envelope.length;
349}
350
351void GBAudioWriteNR42(struct GBAudio* audio, uint8_t value) {
352 if (!_writeEnvelope(&audio->ch4.envelope, value, audio->style)) {
353 mTimingDeschedule(audio->timing, &audio->ch4Event);
354 audio->playingCh4 = false;
355 *audio->nr52 &= ~0x0008;
356 }
357}
358
359void GBAudioWriteNR43(struct GBAudio* audio, uint8_t value) {
360 audio->ch4.ratio = GBAudioRegisterNoiseFeedbackGetRatio(value);
361 audio->ch4.frequency = GBAudioRegisterNoiseFeedbackGetFrequency(value);
362 audio->ch4.power = GBAudioRegisterNoiseFeedbackGetPower(value);
363}
364
365void GBAudioWriteNR44(struct GBAudio* audio, uint8_t value) {
366 bool wasStop = audio->ch4.stop;
367 audio->ch4.stop = GBAudioRegisterNoiseControlGetStop(value);
368 if (!wasStop && audio->ch4.stop && audio->ch4.length && !(audio->frame & 1)) {
369 --audio->ch4.length;
370 if (audio->ch4.length == 0) {
371 mTimingDeschedule(audio->timing, &audio->ch4Event);
372 audio->playingCh4 = false;
373 }
374 }
375 if (GBAudioRegisterNoiseControlIsRestart(value)) {
376 audio->playingCh4 = _resetEnvelope(&audio->ch4.envelope);
377
378 if (audio->ch4.power) {
379 audio->ch4.lfsr = 0x40;
380 } else {
381 audio->ch4.lfsr = 0x4000;
382 }
383 if (!audio->ch4.length) {
384 audio->ch4.length = 64;
385 if (audio->ch4.stop && !(audio->frame & 1)) {
386 --audio->ch4.length;
387 }
388 }
389 if (audio->playingCh4 && audio->ch4.envelope.dead != 2 && !mTimingIsScheduled(audio->timing, &audio->ch4Event)) {
390 mTimingSchedule(audio->timing, &audio->ch4Event, 0);
391 }
392 }
393 *audio->nr52 &= ~0x0008;
394 *audio->nr52 |= audio->playingCh4 << 3;
395}
396
397void GBAudioWriteNR50(struct GBAudio* audio, uint8_t value) {
398 audio->volumeRight = GBRegisterNR50GetVolumeRight(value);
399 audio->volumeLeft = GBRegisterNR50GetVolumeLeft(value);
400}
401
402void GBAudioWriteNR51(struct GBAudio* audio, uint8_t value) {
403 audio->ch1Right = GBRegisterNR51GetCh1Right(value);
404 audio->ch2Right = GBRegisterNR51GetCh2Right(value);
405 audio->ch3Right = GBRegisterNR51GetCh3Right(value);
406 audio->ch4Right = GBRegisterNR51GetCh4Right(value);
407 audio->ch1Left = GBRegisterNR51GetCh1Left(value);
408 audio->ch2Left = GBRegisterNR51GetCh2Left(value);
409 audio->ch3Left = GBRegisterNR51GetCh3Left(value);
410 audio->ch4Left = GBRegisterNR51GetCh4Left(value);
411}
412
413void GBAudioWriteNR52(struct GBAudio* audio, uint8_t value) {
414 bool wasEnable = audio->enable;
415 audio->enable = GBAudioEnableGetEnable(value);
416 if (!audio->enable) {
417 audio->playingCh1 = 0;
418 audio->playingCh2 = 0;
419 audio->playingCh3 = 0;
420 audio->playingCh4 = 0;
421 GBAudioWriteNR10(audio, 0);
422 GBAudioWriteNR12(audio, 0);
423 GBAudioWriteNR13(audio, 0);
424 GBAudioWriteNR14(audio, 0);
425 GBAudioWriteNR22(audio, 0);
426 GBAudioWriteNR23(audio, 0);
427 GBAudioWriteNR24(audio, 0);
428 GBAudioWriteNR30(audio, 0);
429 GBAudioWriteNR32(audio, 0);
430 GBAudioWriteNR33(audio, 0);
431 GBAudioWriteNR34(audio, 0);
432 GBAudioWriteNR42(audio, 0);
433 GBAudioWriteNR43(audio, 0);
434 GBAudioWriteNR44(audio, 0);
435 GBAudioWriteNR50(audio, 0);
436 GBAudioWriteNR51(audio, 0);
437 if (audio->style != GB_AUDIO_DMG) {
438 GBAudioWriteNR11(audio, 0);
439 GBAudioWriteNR21(audio, 0);
440 GBAudioWriteNR31(audio, 0);
441 GBAudioWriteNR41(audio, 0);
442 }
443
444 if (audio->p) {
445 audio->p->memory.io[REG_NR10] = 0;
446 audio->p->memory.io[REG_NR11] = 0;
447 audio->p->memory.io[REG_NR12] = 0;
448 audio->p->memory.io[REG_NR13] = 0;
449 audio->p->memory.io[REG_NR14] = 0;
450 audio->p->memory.io[REG_NR21] = 0;
451 audio->p->memory.io[REG_NR22] = 0;
452 audio->p->memory.io[REG_NR23] = 0;
453 audio->p->memory.io[REG_NR24] = 0;
454 audio->p->memory.io[REG_NR30] = 0;
455 audio->p->memory.io[REG_NR31] = 0;
456 audio->p->memory.io[REG_NR32] = 0;
457 audio->p->memory.io[REG_NR33] = 0;
458 audio->p->memory.io[REG_NR34] = 0;
459 audio->p->memory.io[REG_NR42] = 0;
460 audio->p->memory.io[REG_NR43] = 0;
461 audio->p->memory.io[REG_NR44] = 0;
462 audio->p->memory.io[REG_NR50] = 0;
463 audio->p->memory.io[REG_NR51] = 0;
464 if (audio->style != GB_AUDIO_DMG) {
465 audio->p->memory.io[REG_NR11] = 0;
466 audio->p->memory.io[REG_NR21] = 0;
467 audio->p->memory.io[REG_NR31] = 0;
468 audio->p->memory.io[REG_NR41] = 0;
469 }
470 }
471 *audio->nr52 &= ~0x000F;
472 } else if (!wasEnable) {
473 audio->frame = 7;
474 }
475}
476
477void _updateFrame(struct mTiming* timing, void* user, uint32_t cyclesLate) {
478 struct GBAudio* audio = user;
479 GBAudioUpdateFrame(audio, timing);
480 if (audio->style == GB_AUDIO_GBA) {
481 mTimingSchedule(timing, &audio->frameEvent, audio->timingFactor * FRAME_CYCLES - cyclesLate);
482 }
483}
484
485void GBAudioUpdateFrame(struct GBAudio* audio, struct mTiming* timing) {
486 int frame = (audio->frame + 1) & 7;
487 audio->frame = frame;
488
489 switch (frame) {
490 case 2:
491 case 6:
492 if (audio->ch1.sweep.enable) {
493 --audio->ch1.sweep.step;
494 if (audio->ch1.sweep.step == 0) {
495 audio->playingCh1 = _updateSweep(&audio->ch1, false);
496 *audio->nr52 &= ~0x0001;
497 *audio->nr52 |= audio->playingCh1;
498 }
499 }
500 // Fall through
501 case 0:
502 case 4:
503 if (audio->ch1.control.length && audio->ch1.control.stop) {
504 --audio->ch1.control.length;
505 if (audio->ch1.control.length == 0) {
506 mTimingDeschedule(timing, &audio->ch1Event);
507 audio->playingCh1 = 0;
508 *audio->nr52 &= ~0x0001;
509 }
510 }
511
512 if (audio->ch2.control.length && audio->ch2.control.stop) {
513 --audio->ch2.control.length;
514 if (audio->ch2.control.length == 0) {
515 mTimingDeschedule(timing, &audio->ch2Event);
516 audio->playingCh2 = 0;
517 *audio->nr52 &= ~0x0002;
518 }
519 }
520
521 if (audio->ch3.length && audio->ch3.stop) {
522 --audio->ch3.length;
523 if (audio->ch3.length == 0) {
524 mTimingDeschedule(timing, &audio->ch3Event);
525 audio->playingCh3 = 0;
526 *audio->nr52 &= ~0x0004;
527 }
528 }
529
530 if (audio->ch4.length && audio->ch4.stop) {
531 --audio->ch4.length;
532 if (audio->ch4.length == 0) {
533 mTimingDeschedule(timing, &audio->ch4Event);
534 audio->playingCh4 = 0;
535 *audio->nr52 &= ~0x0008;
536 }
537 }
538 break;
539 case 7:
540 if (audio->playingCh1 && !audio->ch1.envelope.dead) {
541 --audio->ch1.envelope.nextStep;
542 if (audio->ch1.envelope.nextStep == 0) {
543 _updateEnvelope(&audio->ch1.envelope);
544 if (audio->ch1.envelope.dead == 2) {
545 mTimingDeschedule(timing, &audio->ch1Event);
546 }
547 _updateSquareSample(&audio->ch1);
548 }
549 }
550
551 if (audio->playingCh2 && !audio->ch2.envelope.dead) {
552 --audio->ch2.envelope.nextStep;
553 if (audio->ch2.envelope.nextStep == 0) {
554 _updateEnvelope(&audio->ch2.envelope);
555 if (audio->ch2.envelope.dead == 2) {
556 mTimingDeschedule(timing, &audio->ch2Event);
557 }
558 _updateSquareSample(&audio->ch2);
559 }
560 }
561
562 if (audio->playingCh4 && !audio->ch4.envelope.dead) {
563 --audio->ch4.envelope.nextStep;
564 if (audio->ch4.envelope.nextStep == 0) {
565 int8_t sample = (audio->ch4.sample > 0) * 0x8;
566 _updateEnvelope(&audio->ch4.envelope);
567 if (audio->ch4.envelope.dead == 2) {
568 mTimingDeschedule(timing, &audio->ch4Event);
569 }
570 audio->ch4.sample = sample * audio->ch4.envelope.currentVolume;
571 }
572 }
573 break;
574 }
575}
576
577void GBAudioSamplePSG(struct GBAudio* audio, int16_t* left, int16_t* right) {
578 int sampleLeft = 0;
579 int sampleRight = 0;
580
581 if (audio->playingCh1 && !audio->forceDisableCh[0]) {
582 if (audio->ch1Left) {
583 sampleLeft += audio->ch1.sample;
584 }
585
586 if (audio->ch1Right) {
587 sampleRight += audio->ch1.sample;
588 }
589 }
590
591 if (audio->playingCh2 && !audio->forceDisableCh[1]) {
592 if (audio->ch2Left) {
593 sampleLeft += audio->ch2.sample;
594 }
595
596 if (audio->ch2Right) {
597 sampleRight += audio->ch2.sample;
598 }
599 }
600
601 if (audio->playingCh3 && !audio->forceDisableCh[2]) {
602 if (audio->ch3Left) {
603 sampleLeft += audio->ch3.sample;
604 }
605
606 if (audio->ch3Right) {
607 sampleRight += audio->ch3.sample;
608 }
609 }
610
611 if (audio->playingCh4 && !audio->forceDisableCh[3]) {
612 if (audio->ch4Left) {
613 sampleLeft += audio->ch4.sample;
614 }
615
616 if (audio->ch4Right) {
617 sampleRight += audio->ch4.sample;
618 }
619 }
620
621 int dcOffset = audio->style == GB_AUDIO_GBA ? 0 : 0x20A;
622 *left = (sampleLeft - dcOffset) * (1 + audio->volumeLeft);
623 *right = (sampleRight - dcOffset) * (1 + audio->volumeRight);
624}
625
626static void _sample(struct mTiming* timing, void* user, uint32_t cyclesLate) {
627 struct GBAudio* audio = user;
628 int16_t sampleLeft = 0;
629 int16_t sampleRight = 0;
630 GBAudioSamplePSG(audio, &sampleLeft, &sampleRight);
631 sampleLeft = (sampleLeft * audio->masterVolume * 9) >> 7;
632 sampleRight = (sampleRight * audio->masterVolume * 9) >> 7;
633
634 mCoreSyncLockAudio(audio->p->sync);
635 unsigned produced;
636
637 int16_t degradedLeft = sampleLeft - (audio->capLeft >> 16);
638 int16_t degradedRight = sampleRight - (audio->capRight >> 16);
639 audio->capLeft = (sampleLeft << 16) - degradedLeft * 65184;
640 audio->capRight = (sampleRight << 16) - degradedRight * 65184;
641 sampleLeft = degradedLeft;
642 sampleRight = degradedRight;
643
644 if ((size_t) blip_samples_avail(audio->left) < audio->samples) {
645 blip_add_delta(audio->left, audio->clock, sampleLeft - audio->lastLeft);
646 blip_add_delta(audio->right, audio->clock, sampleRight - audio->lastRight);
647 audio->clock += audio->sampleInterval;
648 if (audio->clock >= CLOCKS_PER_BLIP_FRAME) {
649 blip_end_frame(audio->left, CLOCKS_PER_BLIP_FRAME);
650 blip_end_frame(audio->right, CLOCKS_PER_BLIP_FRAME);
651 audio->clock -= CLOCKS_PER_BLIP_FRAME;
652 }
653 }
654 audio->lastLeft = sampleLeft;
655 audio->lastRight = sampleRight;
656 produced = blip_samples_avail(audio->left);
657 if (audio->p->stream && audio->p->stream->postAudioFrame) {
658 audio->p->stream->postAudioFrame(audio->p->stream, sampleLeft, sampleRight);
659 }
660 bool wait = produced >= audio->samples;
661 if (!mCoreSyncProduceAudio(audio->p->sync, audio->left, audio->samples)) {
662 // Interrupted
663 audio->p->earlyExit = true;
664 }
665
666 if (wait && audio->p->stream && audio->p->stream->postAudioBuffer) {
667 audio->p->stream->postAudioBuffer(audio->p->stream, audio->left, audio->right);
668 }
669 mTimingSchedule(timing, &audio->sampleEvent, audio->sampleInterval * audio->timingFactor - cyclesLate);
670}
671
672bool _resetEnvelope(struct GBAudioEnvelope* envelope) {
673 envelope->currentVolume = envelope->initialVolume;
674 _updateEnvelopeDead(envelope);
675 if (!envelope->dead) {
676 envelope->nextStep = envelope->stepTime;
677 }
678 return envelope->initialVolume || envelope->direction;
679}
680
681void _resetSweep(struct GBAudioSweep* sweep) {
682 sweep->step = sweep->time;
683 sweep->enable = (sweep->step != 8) || sweep->shift;
684 sweep->occurred = false;
685}
686
687bool _writeSweep(struct GBAudioSweep* sweep, uint8_t value) {
688 sweep->shift = GBAudioRegisterSquareSweepGetShift(value);
689 bool oldDirection = sweep->direction;
690 sweep->direction = GBAudioRegisterSquareSweepGetDirection(value);
691 bool on = true;
692 if (sweep->occurred && oldDirection && !sweep->direction) {
693 on = false;
694 }
695 sweep->occurred = false;
696 sweep->time = GBAudioRegisterSquareSweepGetTime(value);
697 if (!sweep->time) {
698 sweep->time = 8;
699 }
700 return on;
701}
702
703void _writeDuty(struct GBAudioEnvelope* envelope, uint8_t value) {
704 envelope->length = GBAudioRegisterDutyGetLength(value);
705 envelope->duty = GBAudioRegisterDutyGetDuty(value);
706}
707
708bool _writeEnvelope(struct GBAudioEnvelope* envelope, uint8_t value, enum GBAudioStyle style) {
709 envelope->stepTime = GBAudioRegisterSweepGetStepTime(value);
710 envelope->direction = GBAudioRegisterSweepGetDirection(value);
711 envelope->initialVolume = GBAudioRegisterSweepGetInitialVolume(value);
712 if (style == GB_AUDIO_DMG && !envelope->stepTime) {
713 // TODO: Improve "zombie" mode
714 ++envelope->currentVolume;
715 envelope->currentVolume &= 0xF;
716 }
717 _updateEnvelopeDead(envelope);
718 return (envelope->initialVolume || envelope->direction) && envelope->dead != 2;
719}
720
721static void _updateSquareSample(struct GBAudioSquareChannel* ch) {
722 ch->sample = ch->control.hi * ch->envelope.currentVolume * 0x8;
723}
724
725static int32_t _updateSquareChannel(struct GBAudioSquareChannel* ch) {
726 ch->control.hi = !ch->control.hi;
727 _updateSquareSample(ch);
728 int period = 4 * (2048 - ch->control.frequency);
729 switch (ch->envelope.duty) {
730 case 0:
731 return ch->control.hi ? period : period * 7;
732 case 1:
733 return ch->control.hi ? period * 2 : period * 6;
734 case 2:
735 return period * 4;
736 case 3:
737 return ch->control.hi ? period * 6 : period * 2;
738 default:
739 // This should never be hit
740 return period * 4;
741 }
742}
743
744static void _updateEnvelope(struct GBAudioEnvelope* envelope) {
745 if (envelope->direction) {
746 ++envelope->currentVolume;
747 } else {
748 --envelope->currentVolume;
749 }
750 if (envelope->currentVolume >= 15) {
751 envelope->currentVolume = 15;
752 envelope->dead = 1;
753 } else if (envelope->currentVolume <= 0) {
754 envelope->currentVolume = 0;
755 envelope->dead = 2;
756 } else {
757 envelope->nextStep = envelope->stepTime;
758 }
759}
760
761static void _updateEnvelopeDead(struct GBAudioEnvelope* envelope) {
762 if (!envelope->stepTime) {
763 envelope->dead = envelope->currentVolume ? 1 : 2;
764 } else if (!envelope->direction && !envelope->currentVolume) {
765 envelope->dead = 2;
766 } else if (envelope->direction && envelope->currentVolume == 0xF) {
767 envelope->dead = 1;
768 } else {
769 envelope->dead = 0;
770 }
771}
772
773static bool _updateSweep(struct GBAudioSquareChannel* ch, bool initial) {
774 if (initial || ch->sweep.time != 8) {
775 int frequency = ch->sweep.realFrequency;
776 if (ch->sweep.direction) {
777 frequency -= frequency >> ch->sweep.shift;
778 if (!initial && frequency >= 0) {
779 ch->control.frequency = frequency;
780 ch->sweep.realFrequency = frequency;
781 }
782 } else {
783 frequency += frequency >> ch->sweep.shift;
784 if (frequency < 2048) {
785 if (!initial && ch->sweep.shift) {
786 ch->control.frequency = frequency;
787 ch->sweep.realFrequency = frequency;
788 if (!_updateSweep(ch, true)) {
789 return false;
790 }
791 }
792 } else {
793 return false;
794 }
795 }
796 ch->sweep.occurred = true;
797 }
798 ch->sweep.step = ch->sweep.time;
799 return true;
800}
801
802static void _updateChannel1(struct mTiming* timing, void* user, uint32_t cyclesLate) {
803 struct GBAudio* audio = user;
804 struct GBAudioSquareChannel* ch = &audio->ch1;
805 int cycles = _updateSquareChannel(ch);
806 mTimingSchedule(timing, &audio->ch1Event, audio->timingFactor * cycles - cyclesLate);
807}
808
809static void _updateChannel2(struct mTiming* timing, void* user, uint32_t cyclesLate) {
810 struct GBAudio* audio = user;
811 struct GBAudioSquareChannel* ch = &audio->ch2;
812 int cycles = _updateSquareChannel(ch);
813 mTimingSchedule(timing, &audio->ch2Event, audio->timingFactor * cycles - cyclesLate);
814}
815
816static void _updateChannel3(struct mTiming* timing, void* user, uint32_t cyclesLate) {
817 struct GBAudio* audio = user;
818 struct GBAudioWaveChannel* ch = &audio->ch3;
819 int i;
820 int volume;
821 switch (ch->volume) {
822 case 0:
823 volume = 0;
824 break;
825 case 1:
826 volume = 4;
827 break;
828 case 2:
829 volume = 2;
830 break;
831 case 3:
832 volume = 1;
833 break;
834 default:
835 volume = 3;
836 break;
837 }
838 int start;
839 int end;
840 switch (audio->style) {
841 case GB_AUDIO_DMG:
842 default:
843 ++ch->window;
844 ch->window &= 0x1F;
845 ch->sample = ch->wavedata8[ch->window >> 1];
846 if (!(ch->window & 1)) {
847 ch->sample >>= 4;
848 }
849 ch->sample &= 0xF;
850 break;
851 case GB_AUDIO_GBA:
852 if (ch->size) {
853 start = 7;
854 end = 0;
855 } else if (ch->bank) {
856 start = 7;
857 end = 4;
858 } else {
859 start = 3;
860 end = 0;
861 }
862 uint32_t bitsCarry = ch->wavedata32[end] & 0x000000F0;
863 uint32_t bits;
864 for (i = start; i >= end; --i) {
865 bits = ch->wavedata32[i] & 0x000000F0;
866 ch->wavedata32[i] = ((ch->wavedata32[i] & 0x0F0F0F0F) << 4) | ((ch->wavedata32[i] & 0xF0F0F000) >> 12);
867 ch->wavedata32[i] |= bitsCarry << 20;
868 bitsCarry = bits;
869 }
870 ch->sample = bitsCarry >> 4;
871 break;
872 }
873 ch->sample *= volume * 2;
874 audio->ch3.readable = true;
875 if (audio->style == GB_AUDIO_DMG) {
876 mTimingDeschedule(audio->timing, &audio->ch3Fade);
877 mTimingSchedule(timing, &audio->ch3Fade, 2 - cyclesLate);
878 }
879 int cycles = 2 * (2048 - ch->rate);
880 mTimingSchedule(timing, &audio->ch3Event, audio->timingFactor * cycles - cyclesLate);
881}
882static void _fadeChannel3(struct mTiming* timing, void* user, uint32_t cyclesLate) {
883 UNUSED(timing);
884 UNUSED(cyclesLate);
885 struct GBAudio* audio = user;
886 audio->ch3.readable = false;
887}
888
889static void _updateChannel4(struct mTiming* timing, void* user, uint32_t cyclesLate) {
890 struct GBAudio* audio = user;
891 struct GBAudioNoiseChannel* ch = &audio->ch4;
892
893 int32_t baseCycles = ch->ratio ? 2 * ch->ratio : 1;
894 baseCycles <<= ch->frequency;
895 baseCycles *= 8 * audio->timingFactor;
896 int32_t cycles = 0;
897
898 do {
899 int lsb = ch->lfsr & 1;
900 ch->sample = lsb * 0x8;
901 ch->sample *= ch->envelope.currentVolume;
902 ch->lfsr >>= 1;
903 ch->lfsr ^= (lsb * 0x60) << (ch->power ? 0 : 8);
904 cycles += baseCycles;
905 } while (cycles + baseCycles < audio->sampleInterval);
906 mTimingSchedule(timing, &audio->ch4Event, cycles - cyclesLate);
907}
908
909void GBAudioPSGSerialize(const struct GBAudio* audio, struct GBSerializedPSGState* state, uint32_t* flagsOut) {
910 uint32_t flags = 0;
911 uint32_t ch1Flags = 0;
912 uint32_t ch2Flags = 0;
913 uint32_t ch4Flags = 0;
914
915 flags = GBSerializedAudioFlagsSetFrame(flags, audio->frame);
916 STORE_32LE(audio->frameEvent.when - mTimingCurrentTime(audio->timing), 0, &state->ch1.nextFrame);
917
918 flags = GBSerializedAudioFlagsSetCh1Volume(flags, audio->ch1.envelope.currentVolume);
919 flags = GBSerializedAudioFlagsSetCh1Dead(flags, audio->ch1.envelope.dead);
920 flags = GBSerializedAudioFlagsSetCh1Hi(flags, audio->ch1.control.hi);
921 flags = GBSerializedAudioFlagsSetCh1SweepEnabled(flags, audio->ch1.sweep.enable);
922 flags = GBSerializedAudioFlagsSetCh1SweepOccurred(flags, audio->ch1.sweep.occurred);
923 ch1Flags = GBSerializedAudioEnvelopeSetLength(ch1Flags, audio->ch1.control.length);
924 ch1Flags = GBSerializedAudioEnvelopeSetNextStep(ch1Flags, audio->ch1.envelope.nextStep);
925 ch1Flags = GBSerializedAudioEnvelopeSetFrequency(ch1Flags, audio->ch1.sweep.realFrequency);
926 STORE_32LE(ch1Flags, 0, &state->ch1.envelope);
927 STORE_32LE(audio->ch1Event.when - mTimingCurrentTime(audio->timing), 0, &state->ch1.nextEvent);
928
929 flags = GBSerializedAudioFlagsSetCh2Volume(flags, audio->ch2.envelope.currentVolume);
930 flags = GBSerializedAudioFlagsSetCh2Dead(flags, audio->ch2.envelope.dead);
931 flags = GBSerializedAudioFlagsSetCh2Hi(flags, audio->ch2.control.hi);
932 ch2Flags = GBSerializedAudioEnvelopeSetLength(ch2Flags, audio->ch2.control.length);
933 ch2Flags = GBSerializedAudioEnvelopeSetNextStep(ch2Flags, audio->ch2.envelope.nextStep);
934 STORE_32LE(ch2Flags, 0, &state->ch2.envelope);
935 STORE_32LE(audio->ch2Event.when - mTimingCurrentTime(audio->timing), 0, &state->ch2.nextEvent);
936
937 flags = GBSerializedAudioFlagsSetCh3Readable(flags, audio->ch3.readable);
938 memcpy(state->ch3.wavebanks, audio->ch3.wavedata32, sizeof(state->ch3.wavebanks));
939 STORE_16LE(audio->ch3.length, 0, &state->ch3.length);
940 STORE_32LE(audio->ch3Event.when - mTimingCurrentTime(audio->timing), 0, &state->ch3.nextEvent);
941 STORE_32LE(audio->ch3Fade.when - mTimingCurrentTime(audio->timing), 0, &state->ch1.nextCh3Fade);
942
943 flags = GBSerializedAudioFlagsSetCh4Volume(flags, audio->ch4.envelope.currentVolume);
944 flags = GBSerializedAudioFlagsSetCh4Dead(flags, audio->ch4.envelope.dead);
945 STORE_32LE(audio->ch4.lfsr, 0, &state->ch4.lfsr);
946 ch4Flags = GBSerializedAudioEnvelopeSetLength(ch4Flags, audio->ch4.length);
947 ch4Flags = GBSerializedAudioEnvelopeSetNextStep(ch4Flags, audio->ch4.envelope.nextStep);
948 STORE_32LE(ch4Flags, 0, &state->ch4.envelope);
949 STORE_32LE(audio->ch4Event.when - mTimingCurrentTime(audio->timing), 0, &state->ch4.nextEvent);
950
951 STORE_32LE(flags, 0, flagsOut);
952}
953
954void GBAudioPSGDeserialize(struct GBAudio* audio, const struct GBSerializedPSGState* state, const uint32_t* flagsIn) {
955 uint32_t flags;
956 uint32_t ch1Flags = 0;
957 uint32_t ch2Flags = 0;
958 uint32_t ch4Flags = 0;
959 uint32_t when;
960
961 audio->playingCh1 = !!(*audio->nr52 & 0x0001);
962 audio->playingCh2 = !!(*audio->nr52 & 0x0002);
963 audio->playingCh3 = !!(*audio->nr52 & 0x0004);
964 audio->playingCh4 = !!(*audio->nr52 & 0x0008);
965 audio->enable = GBAudioEnableGetEnable(*audio->nr52);
966
967 if (audio->style == GB_AUDIO_GBA) {
968 LOAD_32LE(when, 0, &state->ch1.nextFrame);
969 mTimingSchedule(audio->timing, &audio->frameEvent, when);
970 }
971
972 LOAD_32LE(flags, 0, flagsIn);
973 audio->frame = GBSerializedAudioFlagsGetFrame(flags);
974
975 LOAD_32LE(ch1Flags, 0, &state->ch1.envelope);
976 audio->ch1.envelope.currentVolume = GBSerializedAudioFlagsGetCh1Volume(flags);
977 audio->ch1.envelope.dead = GBSerializedAudioFlagsGetCh1Dead(flags);
978 audio->ch1.control.hi = GBSerializedAudioFlagsGetCh1Hi(flags);
979 audio->ch1.sweep.enable = GBSerializedAudioFlagsGetCh1SweepEnabled(flags);
980 audio->ch1.sweep.occurred = GBSerializedAudioFlagsGetCh1SweepOccurred(flags);
981 audio->ch1.control.length = GBSerializedAudioEnvelopeGetLength(ch1Flags);
982 audio->ch1.envelope.nextStep = GBSerializedAudioEnvelopeGetNextStep(ch1Flags);
983 audio->ch1.sweep.realFrequency = GBSerializedAudioEnvelopeGetFrequency(ch1Flags);
984 LOAD_32LE(when, 0, &state->ch1.nextEvent);
985 if (audio->ch1.envelope.dead < 2 && audio->playingCh1) {
986 mTimingSchedule(audio->timing, &audio->ch1Event, when);
987 }
988
989 LOAD_32LE(ch2Flags, 0, &state->ch2.envelope);
990 audio->ch2.envelope.currentVolume = GBSerializedAudioFlagsGetCh2Volume(flags);
991 audio->ch2.envelope.dead = GBSerializedAudioFlagsGetCh2Dead(flags);
992 audio->ch2.control.hi = GBSerializedAudioFlagsGetCh2Hi(flags);
993 audio->ch2.control.length = GBSerializedAudioEnvelopeGetLength(ch2Flags);
994 audio->ch2.envelope.nextStep = GBSerializedAudioEnvelopeGetNextStep(ch2Flags);
995 LOAD_32LE(when, 0, &state->ch2.nextEvent);
996 if (audio->ch2.envelope.dead < 2 && audio->playingCh2) {
997 mTimingSchedule(audio->timing, &audio->ch2Event, when);
998 }
999
1000 audio->ch3.readable = GBSerializedAudioFlagsGetCh3Readable(flags);
1001 // TODO: Big endian?
1002 memcpy(audio->ch3.wavedata32, state->ch3.wavebanks, sizeof(audio->ch3.wavedata32));
1003 LOAD_16LE(audio->ch3.length, 0, &state->ch3.length);
1004 LOAD_32LE(when, 0, &state->ch3.nextEvent);
1005 if (audio->playingCh3) {
1006 mTimingSchedule(audio->timing, &audio->ch3Event, when);
1007 }
1008 LOAD_32LE(when, 0, &state->ch1.nextCh3Fade);
1009 if (audio->ch3.readable && audio->style == GB_AUDIO_DMG) {
1010 mTimingSchedule(audio->timing, &audio->ch3Fade, when);
1011 }
1012
1013 LOAD_32LE(ch4Flags, 0, &state->ch4.envelope);
1014 audio->ch4.envelope.currentVolume = GBSerializedAudioFlagsGetCh4Volume(flags);
1015 audio->ch4.envelope.dead = GBSerializedAudioFlagsGetCh4Dead(flags);
1016 audio->ch4.length = GBSerializedAudioEnvelopeGetLength(ch4Flags);
1017 audio->ch4.envelope.nextStep = GBSerializedAudioEnvelopeGetNextStep(ch4Flags);
1018 LOAD_32LE(audio->ch4.lfsr, 0, &state->ch4.lfsr);
1019 LOAD_32LE(when, 0, &state->ch4.nextEvent);
1020 if (audio->ch4.envelope.dead < 2 && audio->playingCh4) {
1021 mTimingSchedule(audio->timing, &audio->ch4Event, when);
1022 }
1023}
1024
1025void GBAudioSerialize(const struct GBAudio* audio, struct GBSerializedState* state) {
1026 GBAudioPSGSerialize(audio, &state->audio.psg, &state->audio.flags);
1027 STORE_32LE(audio->capLeft, 0, &state->audio.capLeft);
1028 STORE_32LE(audio->capRight, 0, &state->audio.capRight);
1029 STORE_32LE(audio->sampleEvent.when - mTimingCurrentTime(audio->timing), 0, &state->audio.nextSample);
1030}
1031
1032void GBAudioDeserialize(struct GBAudio* audio, const struct GBSerializedState* state) {
1033 GBAudioPSGDeserialize(audio, &state->audio.psg, &state->audio.flags);
1034 LOAD_32LE(audio->capLeft, 0, &state->audio.capLeft);
1035 LOAD_32LE(audio->capRight, 0, &state->audio.capRight);
1036 uint32_t when;
1037 LOAD_32LE(when, 0, &state->audio.nextSample);
1038 mTimingSchedule(audio->timing, &audio->sampleEvent, when);
1039}