src/gba/gba-audio.c (view raw)
1#include "gba-audio.h"
2
3#include "gba.h"
4#include "gba-io.h"
5#include "gba-serialize.h"
6#include "gba-thread.h"
7
8#include <limits.h>
9
10const unsigned GBA_AUDIO_SAMPLES = 512;
11const unsigned GBA_AUDIO_FIFO_SIZE = 8 * sizeof(int32_t);
12#define SWEEP_CYCLES (GBA_ARM7TDMI_FREQUENCY / 128)
13
14static int32_t _updateSquareChannel(struct GBAAudioSquareControl* envelope, int duty);
15static void _updateEnvelope(struct GBAAudioEnvelope* envelope);
16static int _updateSweep(struct GBAAudioChannel1* ch);
17static int32_t _updateChannel1(struct GBAAudioChannel1* ch);
18static int32_t _updateChannel2(struct GBAAudioChannel2* ch);
19static int32_t _updateChannel3(struct GBAAudioChannel3* ch);
20static int32_t _updateChannel4(struct GBAAudioChannel4* ch);
21static void _sample(struct GBAAudio* audio);
22
23void GBAAudioInit(struct GBAAudio* audio) {
24 audio->nextEvent = 0;
25 audio->nextCh1 = 0;
26 audio->nextCh2 = 0;
27 audio->nextCh3 = 0;
28 audio->nextCh4 = 0;
29 audio->ch1.sweep.time = 0;
30 audio->ch1.envelope.nextStep = INT_MAX;
31 audio->ch1.control.nextStep = 0;
32 audio->ch1.control.endTime = 0;
33 audio->ch1.nextSweep = INT_MAX;
34 audio->ch1.sample = 0;
35 audio->ch2.envelope.nextStep = INT_MAX;
36 audio->ch2.control.nextStep = 0;
37 audio->ch2.control.endTime = 0;
38 audio->ch2.sample = 0;
39 audio->ch3.bank.packed = 0;
40 audio->ch3.control.endTime = 0;
41 audio->ch3.sample = 0;
42 audio->ch4.sample = 0;
43 audio->ch4.envelope.nextStep = INT_MAX;
44 audio->eventDiff = 0;
45 audio->nextSample = 0;
46 audio->sampleRate = 0x8000;
47 audio->soundcntLo = 0;
48 audio->soundcntHi = 0;
49 audio->soundcntX = 0;
50 audio->sampleInterval = GBA_ARM7TDMI_FREQUENCY / audio->sampleRate;
51
52 CircleBufferInit(&audio->left, GBA_AUDIO_SAMPLES * sizeof(int32_t));
53 CircleBufferInit(&audio->right, GBA_AUDIO_SAMPLES * sizeof(int32_t));
54 CircleBufferInit(&audio->chA.fifo, GBA_AUDIO_FIFO_SIZE);
55 CircleBufferInit(&audio->chB.fifo, GBA_AUDIO_FIFO_SIZE);
56}
57
58void GBAAudioDeinit(struct GBAAudio* audio) {
59 CircleBufferDeinit(&audio->left);
60 CircleBufferDeinit(&audio->right);
61 CircleBufferDeinit(&audio->chA.fifo);
62 CircleBufferDeinit(&audio->chB.fifo);
63}
64
65int32_t GBAAudioProcessEvents(struct GBAAudio* audio, int32_t cycles) {
66 audio->nextEvent -= cycles;
67 audio->eventDiff += cycles;
68 while (audio->nextEvent <= 0) {
69 audio->nextEvent = INT_MAX;
70 if (audio->enable) {
71 if (audio->playingCh1 && !audio->ch1.envelope.dead) {
72 audio->nextCh1 -= audio->eventDiff;
73 if (audio->ch1.envelope.nextStep != INT_MAX) {
74 audio->ch1.envelope.nextStep -= audio->eventDiff;
75 if (audio->ch1.envelope.nextStep <= 0) {
76 int8_t sample = audio->ch1.control.hi * 0x10 - 0x8;
77 _updateEnvelope(&audio->ch1.envelope);
78 if (audio->ch1.envelope.nextStep < audio->nextEvent) {
79 audio->nextEvent = audio->ch1.envelope.nextStep;
80 }
81 audio->ch1.sample = sample * audio->ch1.envelope.currentVolume;
82 }
83 }
84
85 if (audio->ch1.nextSweep != INT_MAX) {
86 audio->ch1.nextSweep -= audio->eventDiff;
87 if (audio->ch1.nextSweep <= 0) {
88 audio->playingCh1 = _updateSweep(&audio->ch1);
89 if (audio->ch1.nextSweep < audio->nextEvent) {
90 audio->nextEvent = audio->ch1.nextSweep;
91 }
92 }
93 }
94
95 if (audio->nextCh1 <= 0) {
96 audio->nextCh1 += _updateChannel1(&audio->ch1);
97 if (audio->nextCh1 < audio->nextEvent) {
98 audio->nextEvent = audio->nextCh1;
99 }
100 }
101
102 if (audio->ch1.control.stop) {
103 audio->ch1.control.endTime -= audio->eventDiff;
104 if (audio->ch1.control.endTime <= 0) {
105 audio->playingCh1 = 0;
106 }
107 }
108 }
109
110 if (audio->playingCh2 && !audio->ch2.envelope.dead) {
111 audio->nextCh2 -= audio->eventDiff;
112 if (audio->ch2.envelope.nextStep != INT_MAX) {
113 audio->ch2.envelope.nextStep -= audio->eventDiff;
114 if (audio->ch2.envelope.nextStep <= 0) {
115 int8_t sample = audio->ch2.control.hi * 0x10 - 0x8;
116 _updateEnvelope(&audio->ch2.envelope);
117 if (audio->ch2.envelope.nextStep < audio->nextEvent) {
118 audio->nextEvent = audio->ch2.envelope.nextStep;
119 }
120 audio->ch2.sample = sample * audio->ch2.envelope.currentVolume;
121 }
122 }
123
124 if (audio->nextCh2 <= 0) {
125 audio->nextCh2 += _updateChannel2(&audio->ch2);
126 if (audio->nextCh2 < audio->nextEvent) {
127 audio->nextEvent = audio->nextCh2;
128 }
129 }
130
131 if (audio->ch2.control.stop) {
132 audio->ch2.control.endTime -= audio->eventDiff;
133 if (audio->ch2.control.endTime <= 0) {
134 audio->playingCh2 = 0;
135 }
136 }
137 }
138
139 if (audio->playingCh3) {
140 audio->nextCh3 -= audio->eventDiff;
141 if (audio->nextCh3 <= 0) {
142 audio->nextCh3 += _updateChannel3(&audio->ch3);
143 if (audio->nextCh3 < audio->nextEvent) {
144 audio->nextEvent = audio->nextCh3;
145 }
146 }
147
148 if (audio->ch3.control.stop) {
149 audio->ch3.control.endTime -= audio->eventDiff;
150 if (audio->ch3.control.endTime <= 0) {
151 audio->playingCh3 = 0;
152 }
153 }
154 }
155
156 if (audio->playingCh4 && !audio->ch4.envelope.dead) {
157 audio->nextCh4 -= audio->eventDiff;
158 if (audio->ch4.envelope.nextStep != INT_MAX) {
159 audio->ch4.envelope.nextStep -= audio->eventDiff;
160 if (audio->ch4.envelope.nextStep <= 0) {
161 int8_t sample = (audio->ch4.sample >> 31) * 0x8;
162 _updateEnvelope(&audio->ch4.envelope);
163 if (audio->ch4.envelope.nextStep < audio->nextEvent) {
164 audio->nextEvent = audio->ch4.envelope.nextStep;
165 }
166 audio->ch4.sample = sample * audio->ch4.envelope.currentVolume;
167 }
168 }
169
170 if (audio->nextCh4 <= 0) {
171 audio->nextCh4 += _updateChannel4(&audio->ch4);
172 if (audio->nextCh4 < audio->nextEvent) {
173 audio->nextEvent = audio->nextCh4;
174 }
175 }
176
177 if (audio->ch4.control.stop) {
178 audio->ch4.control.endTime -= audio->eventDiff;
179 if (audio->ch4.control.endTime <= 0) {
180 audio->playingCh4 = 0;
181 }
182 }
183 }
184 }
185
186 audio->nextSample -= audio->eventDiff;
187 if (audio->nextSample <= 0) {
188 _sample(audio);
189 audio->nextSample += audio->sampleInterval;
190 }
191
192 if (audio->nextSample < audio->nextEvent) {
193 audio->nextEvent = audio->nextSample;
194 }
195 audio->eventDiff = 0;
196 }
197 return audio->nextEvent;
198}
199
200void GBAAudioScheduleFifoDma(struct GBAAudio* audio, int number, struct GBADMA* info) {
201 switch (info->dest) {
202 case BASE_IO | REG_FIFO_A_LO:
203 audio->chA.dmaSource = number;
204 break;
205 case BASE_IO | REG_FIFO_B_LO:
206 audio->chB.dmaSource = number;
207 break;
208 default:
209 GBALog(audio->p, GBA_LOG_GAME_ERROR, "Invalid FIFO destination: 0x%08X", info->dest);
210 return;
211 }
212 info->dstControl = DMA_FIXED;
213}
214
215void GBAAudioWriteSOUND1CNT_LO(struct GBAAudio* audio, uint16_t value) {
216 audio->ch1.sweep.packed = value;
217 if (audio->ch1.sweep.time) {
218 audio->ch1.nextSweep = audio->ch1.sweep.time * SWEEP_CYCLES;
219 } else {
220 audio->ch1.nextSweep = INT_MAX;
221 }
222}
223
224void GBAAudioWriteSOUND1CNT_HI(struct GBAAudio* audio, uint16_t value) {
225 audio->ch1.envelope.packed = value;
226 audio->ch1.envelope.dead = 0;
227 if (audio->ch1.envelope.stepTime) {
228 audio->ch1.envelope.nextStep = 0;
229 } else {
230 audio->ch1.envelope.nextStep = INT_MAX;
231 if (audio->ch1.envelope.initialVolume == 0) {
232 audio->ch1.envelope.dead = 1;
233 audio->ch1.sample = 0;
234 }
235 }
236}
237
238void GBAAudioWriteSOUND1CNT_X(struct GBAAudio* audio, uint16_t value) {
239 audio->ch1.control.packed = value;
240 audio->ch1.control.endTime = (GBA_ARM7TDMI_FREQUENCY * (64 - audio->ch1.envelope.length)) >> 8;
241 if (audio->ch1.control.restart) {
242 if (audio->ch1.sweep.time) {
243 audio->ch1.nextSweep = audio->ch1.sweep.time * SWEEP_CYCLES;
244 } else {
245 audio->ch1.nextSweep = INT_MAX;
246 }
247 if (!audio->playingCh1) {
248 audio->nextCh1 = 0;
249 }
250 audio->playingCh1 = 1;
251 if (audio->ch1.envelope.stepTime) {
252 audio->ch1.envelope.nextStep = 0;
253 } else {
254 audio->ch1.envelope.nextStep = INT_MAX;
255 }
256 audio->ch1.envelope.currentVolume = audio->ch1.envelope.initialVolume;
257 if (audio->ch1.envelope.stepTime) {
258 audio->ch1.envelope.nextStep = 0;
259 } else {
260 audio->ch1.envelope.nextStep = INT_MAX;
261 }
262 }
263}
264
265void GBAAudioWriteSOUND2CNT_LO(struct GBAAudio* audio, uint16_t value) {
266 audio->ch2.envelope.packed = value;
267 audio->ch2.envelope.dead = 0;
268 if (audio->ch2.envelope.stepTime) {
269 audio->ch2.envelope.nextStep = 0;
270 } else {
271 audio->ch2.envelope.nextStep = INT_MAX;
272 if (audio->ch2.envelope.initialVolume == 0) {
273 audio->ch2.envelope.dead = 1;
274 audio->ch2.sample = 0;
275 }
276 }
277}
278
279void GBAAudioWriteSOUND2CNT_HI(struct GBAAudio* audio, uint16_t value) {
280 audio->ch2.control.packed = value;
281 audio->ch1.control.endTime = (GBA_ARM7TDMI_FREQUENCY * (64 - audio->ch2.envelope.length)) >> 8;
282 if (audio->ch2.control.restart) {
283 audio->playingCh2 = 1;
284 audio->ch2.envelope.currentVolume = audio->ch2.envelope.initialVolume;
285 if (audio->ch2.envelope.stepTime) {
286 audio->ch2.envelope.nextStep = 0;
287 } else {
288 audio->ch2.envelope.nextStep = INT_MAX;
289 }
290 audio->nextCh2 = 0;
291 }
292}
293
294void GBAAudioWriteSOUND3CNT_LO(struct GBAAudio* audio, uint16_t value) {
295 audio->ch3.bank.packed = value;
296 if (audio->ch3.control.endTime >= 0) {
297 audio->playingCh3 = audio->ch3.bank.enable;
298 }
299}
300
301void GBAAudioWriteSOUND3CNT_HI(struct GBAAudio* audio, uint16_t value) {
302 audio->ch3.wave.packed = value;
303}
304
305void GBAAudioWriteSOUND3CNT_X(struct GBAAudio* audio, uint16_t value) {
306 audio->ch3.control.packed = value;
307 audio->ch3.control.endTime = (GBA_ARM7TDMI_FREQUENCY * (256 - audio->ch3.wave.length)) >> 8;
308 if (audio->ch3.control.restart) {
309 audio->playingCh3 = audio->ch3.bank.enable;
310 }
311}
312
313void GBAAudioWriteSOUND4CNT_LO(struct GBAAudio* audio, uint16_t value) {
314 audio->ch4.envelope.packed = value;
315 audio->ch4.envelope.dead = 0;
316 if (audio->ch4.envelope.stepTime) {
317 audio->ch4.envelope.nextStep = 0;
318 } else {
319 audio->ch4.envelope.nextStep = INT_MAX;
320 if (audio->ch4.envelope.initialVolume == 0) {
321 audio->ch4.envelope.dead = 1;
322 audio->ch4.sample = 0;
323 }
324 }
325}
326
327void GBAAudioWriteSOUND4CNT_HI(struct GBAAudio* audio, uint16_t value) {
328 audio->ch4.control.packed = value;
329 audio->ch4.control.endTime = (GBA_ARM7TDMI_FREQUENCY * (64 - audio->ch4.envelope.length)) >> 8;
330 if (audio->ch4.control.restart) {
331 audio->playingCh4 = 1;
332 audio->ch4.envelope.currentVolume = audio->ch4.envelope.initialVolume;
333 if (audio->ch4.envelope.stepTime) {
334 audio->ch4.envelope.nextStep = 0;
335 } else {
336 audio->ch4.envelope.nextStep = INT_MAX;
337 }
338 if (audio->ch4.control.power) {
339 audio->ch4.lfsr = 0x40;
340 } else {
341 audio->ch4.lfsr = 0x4000;
342 }
343 audio->nextCh4 = 0;
344 }
345}
346
347void GBAAudioWriteSOUNDCNT_LO(struct GBAAudio* audio, uint16_t value) {
348 audio->soundcntLo = value;
349}
350
351void GBAAudioWriteSOUNDCNT_HI(struct GBAAudio* audio, uint16_t value) {
352 audio->soundcntHi = value;
353}
354
355void GBAAudioWriteSOUNDCNT_X(struct GBAAudio* audio, uint16_t value) {
356 audio->soundcntX = (value & 0x80) | (audio->soundcntX & 0x0F);
357}
358
359void GBAAudioWriteWaveRAM(struct GBAAudio* audio, int address, uint32_t value) {
360 audio->ch3.wavedata[address | (!audio->ch3.bank.bank * 4)] = value;
361}
362
363void GBAAudioWriteFIFO(struct GBAAudio* audio, int address, uint32_t value) {
364 struct CircleBuffer* fifo;
365 switch (address) {
366 case REG_FIFO_A_LO:
367 fifo = &audio->chA.fifo;
368 break;
369 case REG_FIFO_B_LO:
370 fifo = &audio->chB.fifo;
371 break;
372 default:
373 GBALog(audio->p, GBA_LOG_ERROR, "Bad FIFO write to address 0x%03x", address);
374 return;
375 }
376 while (!CircleBufferWrite32(fifo, value)) {
377 int32_t dummy;
378 CircleBufferRead32(fifo, &dummy);
379 }
380}
381
382void GBAAudioSampleFIFO(struct GBAAudio* audio, int fifoId, int32_t cycles) {
383 struct GBAAudioFIFO* channel;
384 if (fifoId == 0) {
385 channel = &audio->chA;
386 } else if (fifoId == 1) {
387 channel = &audio->chB;
388 } else {
389 GBALog(audio->p, GBA_LOG_ERROR, "Bad FIFO write to address 0x%03x", fifoId);
390 return;
391 }
392 if (CircleBufferSize(&channel->fifo) <= 4 * sizeof(int32_t)) {
393 struct GBADMA* dma = &audio->p->memory.dma[channel->dmaSource];
394 dma->nextCount = 4;
395 dma->nextEvent = 0;
396 GBAMemoryUpdateDMAs(&audio->p->memory, -cycles);
397 }
398 CircleBufferRead8(&channel->fifo, &channel->sample);
399}
400
401unsigned GBAAudioCopy(struct GBAAudio* audio, void* left, void* right, unsigned nSamples) {
402 GBASyncLockAudio(audio->p->sync);
403 unsigned read = 0;
404 if (left) {
405 unsigned readL = CircleBufferRead(&audio->left, left, nSamples * sizeof(int32_t)) >> 2;
406 if (readL < nSamples) {
407 memset((int32_t*) left + readL, 0, nSamples - readL);
408 }
409 read = readL;
410 }
411 if (right) {
412 unsigned readR = CircleBufferRead(&audio->right, right, nSamples * sizeof(int32_t)) >> 2;
413 if (readR < nSamples) {
414 memset((int32_t*) right + readR, 0, nSamples - readR);
415 }
416 read = read >= readR ? read : readR;
417 }
418 GBASyncConsumeAudio(audio->p->sync);
419 return read;
420}
421
422static int32_t _updateSquareChannel(struct GBAAudioSquareControl* control, int duty) {
423 control->hi = !control->hi;
424 int period = 16 * (2048 - control->frequency);
425 switch (duty) {
426 case 0:
427 return control->hi ? period : period * 7;
428 case 1:
429 return control->hi ? period * 2 : period * 6;
430 case 2:
431 return period * 4;
432 case 3:
433 return control->hi ? period * 6 : period * 2;
434 default:
435 // This should never be hit
436 return period * 4;
437 }
438}
439
440static void _updateEnvelope(struct GBAAudioEnvelope* envelope) {
441 if (envelope->direction) {
442 ++envelope->currentVolume;
443 } else {
444 --envelope->currentVolume;
445 }
446 if (envelope->currentVolume >= 15) {
447 envelope->currentVolume = 15;
448 envelope->nextStep = INT_MAX;
449 } else if (envelope->currentVolume <= 0) {
450 envelope->currentVolume = 0;
451 envelope->dead = 1;
452 envelope->nextStep = INT_MAX;
453 } else {
454 envelope->nextStep += envelope->stepTime * (GBA_ARM7TDMI_FREQUENCY >> 6);
455 }
456}
457
458static int _updateSweep(struct GBAAudioChannel1* ch) {
459 if (ch->sweep.direction) {
460 int frequency = ch->control.frequency;
461 frequency -= frequency >> ch->sweep.shift;
462 if (frequency >= 0) {
463 ch->control.frequency = frequency;
464 }
465 } else {
466 int frequency = ch->control.frequency;
467 frequency += frequency >> ch->sweep.shift;
468 if (frequency < 2048) {
469 ch->control.frequency = frequency;
470 } else {
471 return 0;
472 }
473 }
474 ch->nextSweep += ch->sweep.time * SWEEP_CYCLES;
475 return 1;
476}
477
478static int32_t _updateChannel1(struct GBAAudioChannel1* ch) {
479 int timing = _updateSquareChannel(&ch->control, ch->envelope.duty);
480 ch->sample = ch->control.hi * 0x10 - 0x8;
481 ch->sample *= ch->envelope.currentVolume;
482 return timing;
483}
484
485static int32_t _updateChannel2(struct GBAAudioChannel2* ch) {
486 int timing = _updateSquareChannel(&ch->control, ch->envelope.duty);
487 ch->sample = ch->control.hi * 0x10 - 0x8;
488 ch->sample *= ch->envelope.currentVolume;
489 return timing;
490}
491
492static int32_t _updateChannel3(struct GBAAudioChannel3* ch) {
493 int i;
494 int start;
495 int end;
496 int volume;
497 switch (ch->wave.volume) {
498 case 0:
499 volume = 0;
500 break;
501 case 1:
502 volume = 4;
503 break;
504 case 2:
505 volume = 2;
506 break;
507 case 3:
508 volume = 1;
509 break;
510 default:
511 volume = 3;
512 break;
513 }
514 if (ch->bank.size) {
515 start = 7;
516 end = 0;
517 } else if (ch->bank.bank) {
518 start = 7;
519 end = 4;
520 } else {
521 start = 3;
522 end = 0;
523 }
524 uint32_t bitsCarry = ch->wavedata[end] & 0xF0000000;
525 uint32_t bits;
526 for (i = start; i >= end; --i) {
527 bits = ch->wavedata[i] & 0xF0000000;
528 ch->wavedata[i] <<= 4;
529 ch->wavedata[i] |= bitsCarry >> 28;
530 bitsCarry = bits;
531 }
532 ch->sample = ((bitsCarry >> 26) - 0x20) * volume;
533 return 8 * (2048 - ch->control.rate);
534}
535
536static int32_t _updateChannel4(struct GBAAudioChannel4* ch) {
537 int lsb = ch->lfsr & 1;
538 ch->sample = lsb * 0x10 - 0x8;
539 ch->sample *= ch->envelope.currentVolume;
540 ch->lfsr >>= 1;
541 ch->lfsr ^= (lsb * 0x60) << (ch->control.power ? 0 : 8);
542 int timing = ch->control.ratio ? 2 * ch->control.ratio : 1;
543 timing <<= ch->control.frequency;
544 timing *= 32;
545 return timing;
546}
547
548static void _sample(struct GBAAudio* audio) {
549 int32_t sampleLeft = 0;
550 int32_t sampleRight = 0;
551 int psgShift = 6 - audio->volume;
552
553 if (audio->ch1Left) {
554 sampleLeft += audio->ch1.sample;
555 }
556
557 if (audio->ch1Right) {
558 sampleRight += audio->ch1.sample;
559 }
560
561 if (audio->ch2Left) {
562 sampleLeft += audio->ch2.sample;
563 }
564
565 if (audio->ch2Right) {
566 sampleRight += audio->ch2.sample;
567 }
568
569 if (audio->ch3Left) {
570 sampleLeft += audio->ch3.sample;
571 }
572
573 if (audio->ch3Right) {
574 sampleRight += audio->ch3.sample;
575 }
576
577 if (audio->ch4Left) {
578 sampleLeft += audio->ch4.sample;
579 }
580
581 if (audio->ch4Right) {
582 sampleRight += audio->ch4.sample;
583 }
584
585 sampleLeft = (sampleLeft * (1 + audio->volumeLeft)) >> psgShift;
586 sampleRight = (sampleRight * (1 + audio->volumeRight)) >> psgShift;
587
588 if (audio->chALeft) {
589 sampleLeft += (audio->chA.sample << 2) >> !audio->volumeChA;
590 }
591
592 if (audio->chARight) {
593 sampleRight += (audio->chA.sample << 2) >> !audio->volumeChA;
594 }
595
596 if (audio->chBLeft) {
597 sampleLeft += (audio->chB.sample << 2) >> !audio->volumeChB;
598 }
599
600 if (audio->chBRight) {
601 sampleRight += (audio->chB.sample << 2) >> !audio->volumeChB;
602 }
603
604 GBASyncLockAudio(audio->p->sync);
605 CircleBufferWrite32(&audio->left, sampleLeft << 5);
606 CircleBufferWrite32(&audio->right, sampleRight << 5);
607 unsigned produced = CircleBufferSize(&audio->left);
608 GBASyncProduceAudio(audio->p->sync, produced >= GBA_AUDIO_SAMPLES * 3);
609}
610
611void GBAAudioSerialize(const struct GBAAudio* audio, struct GBASerializedState* state) {
612 state->audio.ch1Volume = audio->ch1.envelope.currentVolume;
613 state->audio.ch1Dead = audio->ch1.envelope.dead;
614 state->audio.ch1Hi = audio->ch1.control.hi;
615 state->audio.ch1.envelopeNextStep = audio->ch1.envelope.nextStep;
616 state->audio.ch1.waveNextStep = audio->ch1.control.nextStep;
617 state->audio.ch1.sweepNextStep = audio->ch1.nextSweep;
618 state->audio.ch1.endTime = audio->ch1.control.endTime;
619 state->audio.ch1.nextEvent = audio->nextCh1;
620
621 state->audio.ch2Volume = audio->ch2.envelope.currentVolume;
622 state->audio.ch2Dead = audio->ch2.envelope.dead;
623 state->audio.ch2Hi = audio->ch2.control.hi;
624 state->audio.ch2.envelopeNextStep = audio->ch2.envelope.nextStep;
625 state->audio.ch2.waveNextStep = audio->ch2.control.nextStep;
626 state->audio.ch2.endTime = audio->ch2.control.endTime;
627 state->audio.ch2.nextEvent = audio->nextCh2;
628
629 memcpy(state->audio.ch3.wavebanks, audio->ch3.wavedata, sizeof(state->audio.ch3.wavebanks));
630 state->audio.ch3.endTime = audio->ch3.control.endTime;
631 state->audio.ch3.nextEvent = audio->nextCh3;
632
633 state->audio.ch4Volume = audio->ch4.envelope.currentVolume;
634 state->audio.ch4Dead = audio->ch4.envelope.dead;
635 state->audio.ch4.envelopeNextStep = audio->ch4.envelope.nextStep;
636 state->audio.ch4.lfsr = audio->ch4.lfsr;
637 state->audio.ch4.endTime = audio->ch4.control.endTime;
638 state->audio.ch4.nextEvent = audio->nextCh4;
639
640 state->audio.nextEvent = audio->nextEvent;
641 state->audio.eventDiff = audio->eventDiff;
642 state->audio.nextSample = audio->nextSample;
643}
644
645void GBAAudioDeserialize(struct GBAAudio* audio, const struct GBASerializedState* state) {
646 audio->ch1.envelope.currentVolume = state->audio.ch1Volume;
647 audio->ch1.envelope.dead = state->audio.ch1Dead;
648 audio->ch1.control.hi = state->audio.ch1Hi;
649 audio->ch1.envelope.nextStep = state->audio.ch1.envelopeNextStep;
650 audio->ch1.control.nextStep = state->audio.ch1.waveNextStep;
651 audio->ch1.nextSweep = state->audio.ch1.sweepNextStep;
652 audio->ch1.control.endTime = state->audio.ch1.endTime;
653 audio->nextCh1 = state->audio.ch1.nextEvent;
654
655 audio->ch2.envelope.currentVolume = state->audio.ch2Volume;
656 audio->ch2.envelope.dead = state->audio.ch2Dead;
657 audio->ch2.control.hi = state->audio.ch2Hi;
658 audio->ch2.envelope.nextStep = state->audio.ch2.envelopeNextStep;
659 audio->ch2.control.nextStep = state->audio.ch2.waveNextStep;
660 audio->ch2.control.endTime = state->audio.ch2.endTime;
661 audio->nextCh2 = state->audio.ch2.nextEvent;
662
663 memcpy(audio->ch3.wavedata, state->audio.ch3.wavebanks, sizeof(audio->ch3.wavedata));
664 audio->ch3.control.endTime = state->audio.ch3.endTime;
665 audio->nextCh3 = state->audio.ch3.nextEvent;
666
667 audio->ch4.envelope.currentVolume = state->audio.ch4Volume;
668 audio->ch4.envelope.dead = state->audio.ch4Dead;
669 audio->ch4.envelope.nextStep = state->audio.ch4.envelopeNextStep;
670 audio->ch4.lfsr = state->audio.ch4.lfsr;
671 audio->ch4.control.endTime = state->audio.ch4.endTime;
672 audio->nextCh4 = state->audio.ch4.nextEvent;
673
674 audio->nextEvent = state->audio.nextEvent;
675 audio->eventDiff = state->audio.eventDiff;
676 audio->nextSample = state->audio.nextSample;
677}