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 "audio.h"
7
8#include "core/sync.h"
9#include "gb/gb.h"
10#include "gb/io.h"
11
12#define FRAME_CYCLES (DMG_LR35902_FREQUENCY >> 9)
13
14static const int CLOCKS_PER_BLIP_FRAME = 0x1000;
15static const unsigned BLIP_BUFFER_SIZE = 0x4000;
16
17static void _writeDuty(struct GBAudioEnvelope* envelope, uint8_t value);
18static bool _writeSweep(struct GBAudioEnvelope* envelope, uint8_t value);
19static int32_t _updateSquareChannel(struct GBAudioSquareControl* envelope, int duty);
20static void _updateEnvelope(struct GBAudioEnvelope* envelope);
21static bool _updateSweep(struct GBAudioChannel1* ch);
22static int32_t _updateChannel1(struct GBAudioChannel1* ch);
23static int32_t _updateChannel2(struct GBAudioChannel2* ch);
24static int32_t _updateChannel3(struct GBAudioChannel3* ch);
25static int32_t _updateChannel4(struct GBAudioChannel4* ch);
26static void _sample(struct GBAudio* audio, int32_t cycles);
27
28void GBAudioInit(struct GBAudio* audio, size_t samples) {
29 audio->samples = samples;
30 audio->left = blip_new(BLIP_BUFFER_SIZE);
31 audio->right = blip_new(BLIP_BUFFER_SIZE);
32 audio->clockRate = DMG_LR35902_FREQUENCY;
33 // Guess too large; we hang producing extra samples if we guess too low
34 blip_set_rates(audio->left, DMG_LR35902_FREQUENCY, 96000);
35 blip_set_rates(audio->right, DMG_LR35902_FREQUENCY, 96000);
36 audio->forceDisableCh[0] = false;
37 audio->forceDisableCh[1] = false;
38 audio->forceDisableCh[2] = false;
39 audio->forceDisableCh[3] = false;
40}
41
42void GBAudioDeinit(struct GBAudio* audio) {
43 blip_delete(audio->left);
44 blip_delete(audio->right);
45}
46
47void GBAudioReset(struct GBAudio* audio) {
48 audio->nextEvent = 0;
49 audio->nextCh1 = 0;
50 audio->nextCh2 = 0;
51 audio->nextCh3 = 0;
52 audio->nextCh4 = 0;
53 audio->ch1 = (struct GBAudioChannel1) { };
54 audio->ch2 = (struct GBAudioChannel2) { };
55 audio->ch3 = (struct GBAudioChannel3) { .bank = 0 };
56 audio->ch4 = (struct GBAudioChannel4) { };
57 audio->eventDiff = 0;
58 audio->nextFrame = 0;
59 audio->frame = 0;
60 audio->nextSample = 0;
61 audio->sampleInterval = 128;
62 audio->volumeRight = 0;
63 audio->volumeLeft = 0;
64 audio->ch1Right = false;
65 audio->ch2Right = false;
66 audio->ch3Right = false;
67 audio->ch4Right = false;
68 audio->ch1Left = false;
69 audio->ch2Left = false;
70 audio->ch3Left = false;
71 audio->ch4Left = false;
72 audio->playingCh1 = false;
73 audio->playingCh2 = false;
74 audio->playingCh3 = false;
75 audio->playingCh4 = false;
76}
77
78void GBAudioWriteNR10(struct GBAudio* audio, uint8_t value) {
79 audio->ch1.shift = GBAudioRegisterSquareSweepGetShift(value);
80 audio->ch1.direction = GBAudioRegisterSquareSweepGetDirection(value);
81 audio->ch1.time = GBAudioRegisterSquareSweepGetTime(value);
82}
83
84void GBAudioWriteNR11(struct GBAudio* audio, uint8_t value) {
85 _writeDuty(&audio->ch1.envelope, value);
86 audio->ch1.control.length = 64 - audio->ch1.envelope.length;
87}
88
89void GBAudioWriteNR12(struct GBAudio* audio, uint8_t value) {
90 if (!_writeSweep(&audio->ch1.envelope, value)) {
91 audio->playingCh1 = false;
92 // TODO: Don't need p
93 if (audio->p) {
94 audio->p->memory.io[REG_NR52] &= ~0x0001;
95 }
96 }
97}
98
99void GBAudioWriteNR13(struct GBAudio* audio, uint8_t value) {
100 audio->ch1.control.frequency &= 0x700;
101 audio->ch1.control.frequency |= GBAudioRegisterControlGetFrequency(value);
102}
103
104void GBAudioWriteNR14(struct GBAudio* audio, uint8_t value) {
105 audio->ch1.control.frequency &= 0xFF;
106 audio->ch1.control.frequency |= GBAudioRegisterControlGetFrequency(value << 8);
107 audio->ch1.control.stop = GBAudioRegisterControlGetStop(value << 8);
108 if (GBAudioRegisterControlIsRestart(value << 8)) {
109 if (audio->nextEvent == INT_MAX) {
110 audio->eventDiff = 0;
111 }
112 if (!audio->playingCh1) {
113 audio->nextCh1 = audio->eventDiff;
114 }
115 audio->playingCh1 = audio->ch1.envelope.initialVolume || audio->ch1.envelope.direction;
116 audio->ch1.envelope.currentVolume = audio->ch1.envelope.initialVolume;
117 if (audio->ch1.envelope.currentVolume > 0) {
118 audio->ch1.envelope.dead = 0;
119 }
120 audio->ch1.sweepStep = audio->ch1.time;
121 if (!audio->ch1.control.length) {
122 audio->ch1.control.length = 64;
123 }
124 if (audio->playingCh1 && audio->ch1.shift) {
125 audio->playingCh1 = _updateSweep(&audio->ch1);
126 }
127 audio->nextEvent = 0;
128 }
129}
130
131void GBAudioWriteNR21(struct GBAudio* audio, uint8_t value) {
132 _writeDuty(&audio->ch2.envelope, value);
133 audio->ch2.control.length = 64 - audio->ch2.envelope.length;
134}
135
136void GBAudioWriteNR22(struct GBAudio* audio, uint8_t value) {
137 if (!_writeSweep(&audio->ch2.envelope, value)) {
138 audio->playingCh2 = false;
139 // TODO: Don't need p
140 if (audio->p) {
141 audio->p->memory.io[REG_NR52] &= ~0x0002;
142 }
143 }
144}
145
146void GBAudioWriteNR23(struct GBAudio* audio, uint8_t value) {
147 audio->ch2.control.frequency &= 0x700;
148 audio->ch2.control.frequency |= GBAudioRegisterControlGetFrequency(value);
149}
150
151void GBAudioWriteNR24(struct GBAudio* audio, uint8_t value) {
152 audio->ch2.control.frequency &= 0xFF;
153 audio->ch2.control.frequency |= GBAudioRegisterControlGetFrequency(value << 8);
154 audio->ch2.control.stop = GBAudioRegisterControlGetStop(value << 8);
155 if (GBAudioRegisterControlIsRestart(value << 8)) {
156 audio->playingCh2 = audio->ch2.envelope.initialVolume || audio->ch2.envelope.direction;
157 audio->ch2.envelope.currentVolume = audio->ch2.envelope.initialVolume;
158 if (audio->ch2.envelope.currentVolume > 0) {
159 audio->ch2.envelope.dead = 0;
160 }
161 if (audio->nextEvent == INT_MAX) {
162 audio->eventDiff = 0;
163 }
164 if (!audio->playingCh2) {
165 audio->nextCh2 = audio->eventDiff;
166 }
167 if (!audio->ch2.control.length) {
168 audio->ch2.control.length = 64;
169 }
170 audio->nextEvent = 0;
171 }
172}
173
174void GBAudioWriteNR30(struct GBAudio* audio, uint8_t value) {
175 audio->ch3.enable = GBAudioRegisterBankGetEnable(value);
176 if (!audio->ch3.enable) {
177 audio->playingCh3 = false;
178 // TODO: Don't need p
179 if (audio->p) {
180 audio->p->memory.io[REG_NR52] &= ~0x0004;
181 }
182 }
183}
184
185void GBAudioWriteNR31(struct GBAudio* audio, uint8_t value) {
186 audio->ch3.length = value;
187 audio->ch3.lengthShadow = 256 - value;
188}
189
190void GBAudioWriteNR32(struct GBAudio* audio, uint8_t value) {
191 audio->ch3.volume = GBAudioRegisterBankVolumeGetVolumeGB(value);
192}
193
194void GBAudioWriteNR33(struct GBAudio* audio, uint8_t value) {
195 audio->ch3.rate &= 0x700;
196 audio->ch3.rate |= GBAudioRegisterControlGetRate(value);
197}
198
199void GBAudioWriteNR34(struct GBAudio* audio, uint8_t value) {
200 audio->ch3.rate &= 0xFF;
201 audio->ch3.rate |= GBAudioRegisterControlGetRate(value << 8);
202 audio->ch3.stop = GBAudioRegisterControlGetStop(value << 8);
203 if (GBAudioRegisterControlIsRestart(value << 8)) {
204 audio->playingCh3 = audio->ch3.enable;
205 if (!audio->ch3.lengthShadow) {
206 audio->ch3.lengthShadow = 256;
207 }
208 }
209 if (audio->playingCh3) {
210 if (audio->nextEvent == INT_MAX) {
211 audio->eventDiff = 0;
212 }
213 audio->nextCh3 = audio->eventDiff;
214 audio->nextEvent = 0;
215 }
216}
217
218void GBAudioWriteNR41(struct GBAudio* audio, uint8_t value) {
219 _writeDuty(&audio->ch4.envelope, value);
220 audio->ch4.length = 64 - audio->ch4.envelope.length;
221}
222
223void GBAudioWriteNR42(struct GBAudio* audio, uint8_t value) {
224 if (!_writeSweep(&audio->ch4.envelope, value)) {
225 audio->playingCh4 = false;
226 // TODO: Don't need p
227 if (audio->p) {
228 audio->p->memory.io[REG_NR52] &= ~0x0008;
229 }
230 }
231}
232
233void GBAudioWriteNR43(struct GBAudio* audio, uint8_t value) {
234 audio->ch4.ratio = GBAudioRegisterNoiseFeedbackGetRatio(value);
235 audio->ch4.frequency = GBAudioRegisterNoiseFeedbackGetFrequency(value);
236 audio->ch4.power = GBAudioRegisterNoiseFeedbackGetPower(value);
237}
238
239void GBAudioWriteNR44(struct GBAudio* audio, uint8_t value) {
240 audio->ch4.stop = GBAudioRegisterNoiseControlGetStop(value);
241 if (GBAudioRegisterNoiseControlIsRestart(value)) {
242 audio->playingCh4 = audio->ch4.envelope.initialVolume || audio->ch4.envelope.direction;
243 audio->ch4.envelope.currentVolume = audio->ch4.envelope.initialVolume;
244 if (audio->ch4.envelope.currentVolume > 0) {
245 audio->ch4.envelope.dead = 0;
246 }
247 if (audio->ch4.power) {
248 audio->ch4.lfsr = 0x40;
249 } else {
250 audio->ch4.lfsr = 0x4000;
251 }
252 if (audio->nextEvent == INT_MAX) {
253 audio->eventDiff = 0;
254 }
255 if (!audio->playingCh4) {
256 audio->nextCh4 = audio->eventDiff;
257 }
258 if (!audio->ch4.length) {
259 audio->ch4.length = 64;
260 }
261 audio->nextEvent = 0;
262 }
263}
264
265void GBAudioWriteNR50(struct GBAudio* audio, uint8_t value) {
266 audio->volumeRight = GBRegisterNR50GetVolumeRight(value);
267 audio->volumeLeft = GBRegisterNR50GetVolumeLeft(value);
268}
269
270void GBAudioWriteNR51(struct GBAudio* audio, uint8_t value) {
271 audio->ch1Right = GBRegisterNR51GetCh1Right(value);
272 audio->ch2Right = GBRegisterNR51GetCh2Right(value);
273 audio->ch3Right = GBRegisterNR51GetCh3Right(value);
274 audio->ch4Right = GBRegisterNR51GetCh4Right(value);
275 audio->ch1Left = GBRegisterNR51GetCh1Left(value);
276 audio->ch2Left = GBRegisterNR51GetCh2Left(value);
277 audio->ch3Left = GBRegisterNR51GetCh3Left(value);
278 audio->ch4Left = GBRegisterNR51GetCh4Left(value);
279}
280
281void GBAudioWriteNR52(struct GBAudio* audio, uint8_t value) {
282 audio->enable = GBAudioEnableGetEnable(value);
283 if (!audio->enable) {
284 audio->playingCh1 = 0;
285 audio->playingCh2 = 0;
286 audio->playingCh3 = 0;
287 audio->playingCh4 = 0;
288 GBAudioWriteNR10(audio, 0);
289 GBAudioWriteNR11(audio, 0);
290 GBAudioWriteNR12(audio, 0);
291 GBAudioWriteNR13(audio, 0);
292 GBAudioWriteNR14(audio, 0);
293 GBAudioWriteNR21(audio, 0);
294 GBAudioWriteNR22(audio, 0);
295 GBAudioWriteNR23(audio, 0);
296 GBAudioWriteNR24(audio, 0);
297 GBAudioWriteNR30(audio, 0);
298 GBAudioWriteNR31(audio, 0);
299 GBAudioWriteNR32(audio, 0);
300 GBAudioWriteNR33(audio, 0);
301 GBAudioWriteNR34(audio, 0);
302 GBAudioWriteNR41(audio, 0);
303 GBAudioWriteNR42(audio, 0);
304 GBAudioWriteNR43(audio, 0);
305 GBAudioWriteNR44(audio, 0);
306 GBAudioWriteNR50(audio, 0);
307 GBAudioWriteNR51(audio, 0);
308 if (audio->p) {
309 audio->p->memory.io[REG_NR10] = 0;
310 audio->p->memory.io[REG_NR11] = 0;
311 audio->p->memory.io[REG_NR12] = 0;
312 audio->p->memory.io[REG_NR13] = 0;
313 audio->p->memory.io[REG_NR14] = 0;
314 audio->p->memory.io[REG_NR21] = 0;
315 audio->p->memory.io[REG_NR22] = 0;
316 audio->p->memory.io[REG_NR23] = 0;
317 audio->p->memory.io[REG_NR24] = 0;
318 audio->p->memory.io[REG_NR30] = 0;
319 audio->p->memory.io[REG_NR31] = 0;
320 audio->p->memory.io[REG_NR32] = 0;
321 audio->p->memory.io[REG_NR33] = 0;
322 audio->p->memory.io[REG_NR34] = 0;
323 audio->p->memory.io[REG_NR41] = 0;
324 audio->p->memory.io[REG_NR42] = 0;
325 audio->p->memory.io[REG_NR43] = 0;
326 audio->p->memory.io[REG_NR44] = 0;
327 audio->p->memory.io[REG_NR50] = 0;
328 audio->p->memory.io[REG_NR51] = 0;
329 audio->p->memory.io[REG_NR52] &= ~0x000F;
330 }
331 }
332}
333
334int32_t GBAudioProcessEvents(struct GBAudio* audio, int32_t cycles) {
335 if (audio->nextEvent == INT_MAX) {
336 return INT_MAX;
337 }
338 audio->nextEvent -= cycles;
339 audio->eventDiff += cycles;
340 while (audio->nextEvent <= 0) {
341 audio->nextEvent = INT_MAX;
342 if (audio->enable) {
343 audio->nextFrame -= audio->eventDiff;
344 int frame = -1;
345 if (audio->nextFrame <= 0) {
346 frame = (audio->frame + 1) & 7;
347 audio->frame = frame;
348 audio->nextFrame += FRAME_CYCLES;
349 if (audio->nextFrame < audio->nextEvent) {
350 audio->nextEvent = audio->nextFrame;
351 }
352 }
353
354 if (audio->playingCh1) {
355 audio->nextCh1 -= audio->eventDiff;
356 if (!audio->ch1.envelope.dead) {
357 if (frame == 7) {
358 --audio->ch1.envelope.nextStep;
359 if (audio->ch1.envelope.nextStep == 0) {
360 int8_t sample = audio->ch1.control.hi * 0x10 - 0x8;
361 _updateEnvelope(&audio->ch1.envelope);
362 audio->ch1.sample = sample * audio->ch1.envelope.currentVolume;
363 }
364 }
365
366 if (audio->ch1.sweepStep && (frame & 3) == 2) {
367 --audio->ch1.sweepStep;
368 if (audio->ch1.sweepStep == 0) {
369 audio->playingCh1 = _updateSweep(&audio->ch1);
370 }
371 }
372 }
373
374 if (audio->nextCh1 <= 0) {
375 audio->nextCh1 += _updateChannel1(&audio->ch1);
376 }
377 if (audio->nextCh1 < audio->nextEvent) {
378 audio->nextEvent = audio->nextCh1;
379 }
380 }
381
382 if (audio->ch1.control.length && audio->ch1.control.stop && !(frame & 1)) {
383 --audio->ch1.control.length;
384 if (audio->ch1.control.length == 0) {
385 audio->playingCh1 = 0;
386 }
387 }
388
389 if (audio->playingCh2) {
390 audio->nextCh2 -= audio->eventDiff;
391 if (!audio->ch2.envelope.dead && frame == 7) {
392 --audio->ch2.envelope.nextStep;
393 if (audio->ch2.envelope.nextStep == 0) {
394 int8_t sample = audio->ch2.control.hi * 0x10 - 0x8;
395 _updateEnvelope(&audio->ch2.envelope);
396 audio->ch2.sample = sample * audio->ch2.envelope.currentVolume;
397 }
398 }
399
400 if (audio->nextCh2 <= 0) {
401 audio->nextCh2 += _updateChannel2(&audio->ch2);
402 }
403 if (audio->nextCh2 < audio->nextEvent) {
404 audio->nextEvent = audio->nextCh2;
405 }
406 }
407
408 if (audio->ch2.control.length && audio->ch2.control.stop && !(frame & 1)) {
409 --audio->ch2.control.length;
410 if (audio->ch2.control.length == 0) {
411 audio->playingCh2 = 0;
412 }
413 }
414
415 if (audio->playingCh3) {
416 audio->nextCh3 -= audio->eventDiff;
417 if (audio->nextCh3 <= 0) {
418 audio->nextCh3 += _updateChannel3(&audio->ch3);
419 }
420 if (audio->nextCh3 < audio->nextEvent) {
421 audio->nextEvent = audio->nextCh3;
422 }
423 }
424
425 if (audio->ch3.lengthShadow && audio->ch3.stop && !(frame & 1)) {
426 --audio->ch3.lengthShadow;
427 if (audio->ch3.lengthShadow == 0) {
428 audio->playingCh3 = 0;
429 }
430 }
431
432 if (audio->playingCh4) {
433 audio->nextCh4 -= audio->eventDiff;
434 if (!audio->ch4.envelope.dead && frame == 7) {
435 --audio->ch4.envelope.nextStep;
436 if (audio->ch4.envelope.nextStep == 0) {
437 int8_t sample = (audio->ch4.sample >> 31) * 0x8;
438 _updateEnvelope(&audio->ch4.envelope);
439 audio->ch4.sample = sample * audio->ch4.envelope.currentVolume;
440 }
441 }
442
443 if (audio->nextCh4 <= 0) {
444 audio->nextCh4 += _updateChannel4(&audio->ch4);
445 }
446 if (audio->nextCh4 < audio->nextEvent) {
447 audio->nextEvent = audio->nextCh4;
448 }
449 }
450
451 if (audio->ch4.length && audio->ch4.stop && !(frame & 1)) {
452 --audio->ch4.length;
453 if (audio->ch4.length == 0) {
454 audio->playingCh4 = 0;
455 }
456 }
457 }
458
459 if (audio->p) {
460 audio->p->memory.io[REG_NR52] &= ~0x000F;
461 audio->p->memory.io[REG_NR52] |= audio->playingCh1;
462 audio->p->memory.io[REG_NR52] |= audio->playingCh2 << 1;
463 audio->p->memory.io[REG_NR52] |= audio->playingCh3 << 2;
464 audio->p->memory.io[REG_NR52] |= audio->playingCh4 << 3;
465 audio->nextSample -= audio->eventDiff;
466 if (audio->nextSample <= 0) {
467 _sample(audio, audio->sampleInterval);
468 audio->nextSample += audio->sampleInterval;
469 }
470
471 if (audio->nextSample < audio->nextEvent) {
472 audio->nextEvent = audio->nextSample;
473 }
474 }
475 audio->eventDiff = 0;
476 }
477 return audio->nextEvent;
478}
479
480void GBAudioSamplePSG(struct GBAudio* audio, int16_t* left, int16_t* right) {
481 int sampleLeft = 0;
482 int sampleRight = 0;
483
484 if (audio->playingCh1 && !audio->forceDisableCh[0]) {
485 if (audio->ch1Left) {
486 sampleLeft += audio->ch1.sample;
487 }
488
489 if (audio->ch1Right) {
490 sampleRight += audio->ch1.sample;
491 }
492 }
493
494 if (audio->playingCh2 && !audio->forceDisableCh[1]) {
495 if (audio->ch2Left) {
496 sampleLeft += audio->ch2.sample;
497 }
498
499 if (audio->ch2Right) {
500 sampleRight += audio->ch2.sample;
501 }
502 }
503
504 if (audio->playingCh3 && !audio->forceDisableCh[2]) {
505 if (audio->ch3Left) {
506 sampleLeft += audio->ch3.sample;
507 }
508
509 if (audio->ch3Right) {
510 sampleRight += audio->ch3.sample;
511 }
512 }
513
514 if (audio->playingCh4 && !audio->forceDisableCh[3]) {
515 if (audio->ch4Left) {
516 sampleLeft += audio->ch4.sample;
517 }
518
519 if (audio->ch4Right) {
520 sampleRight += audio->ch4.sample;
521 }
522 }
523
524 *left = sampleLeft * (1 + audio->volumeLeft);
525 *right = sampleRight * (1 + audio->volumeRight);
526}
527
528void _sample(struct GBAudio* audio, int32_t cycles) {
529 int16_t sampleLeft = 0;
530 int16_t sampleRight = 0;
531 GBAudioSamplePSG(audio, &sampleLeft, &sampleRight);
532 sampleLeft <<= 1;
533 sampleRight <<= 1;
534
535 mCoreSyncLockAudio(audio->p->sync);
536 unsigned produced;
537 if ((size_t) blip_samples_avail(audio->left) < audio->samples) {
538 blip_add_delta(audio->left, audio->clock, sampleLeft - audio->lastLeft);
539 blip_add_delta(audio->right, audio->clock, sampleRight - audio->lastRight);
540 audio->lastLeft = sampleLeft;
541 audio->lastRight = sampleRight;
542 audio->clock += cycles;
543 if (audio->clock >= CLOCKS_PER_BLIP_FRAME) {
544 blip_end_frame(audio->left, audio->clock);
545 blip_end_frame(audio->right, audio->clock);
546 audio->clock -= CLOCKS_PER_BLIP_FRAME;
547 }
548 }
549 produced = blip_samples_avail(audio->left);
550 bool wait = produced >= audio->samples;
551 mCoreSyncProduceAudio(audio->p->sync, wait);
552 // TODO: Put AVStream back
553}
554
555void _writeDuty(struct GBAudioEnvelope* envelope, uint8_t value) {
556 envelope->length = GBAudioRegisterDutyGetLength(value);
557 envelope->duty = GBAudioRegisterDutyGetDuty(value);
558}
559
560bool _writeSweep(struct GBAudioEnvelope* envelope, uint8_t value) {
561 envelope->stepTime = GBAudioRegisterSweepGetStepTime(value);
562 envelope->direction = GBAudioRegisterSweepGetDirection(value);
563 envelope->initialVolume = GBAudioRegisterSweepGetInitialVolume(value);
564 envelope->dead = 0;
565 envelope->nextStep = envelope->stepTime;
566 return envelope->initialVolume || envelope->direction;
567}
568
569static int32_t _updateSquareChannel(struct GBAudioSquareControl* control, int duty) {
570 control->hi = !control->hi;
571 int period = 4 * (2048 - control->frequency);
572 switch (duty) {
573 case 0:
574 return control->hi ? period : period * 7;
575 case 1:
576 return control->hi ? period * 2 : period * 6;
577 case 2:
578 return period * 4;
579 case 3:
580 return control->hi ? period * 6 : period * 2;
581 default:
582 // This should never be hit
583 return period * 4;
584 }
585}
586
587static void _updateEnvelope(struct GBAudioEnvelope* envelope) {
588 if (envelope->direction) {
589 ++envelope->currentVolume;
590 } else {
591 --envelope->currentVolume;
592 }
593 if (envelope->currentVolume >= 15) {
594 envelope->currentVolume = 15;
595 } else if (envelope->currentVolume <= 0) {
596 envelope->currentVolume = 0;
597 envelope->dead = 1;
598 } else {
599 envelope->nextStep = envelope->stepTime;
600 }
601}
602
603static bool _updateSweep(struct GBAudioChannel1* ch) {
604 if (ch->direction) {
605 int frequency = ch->control.frequency;
606 frequency -= frequency >> ch->shift;
607 if (frequency >= 0) {
608 ch->control.frequency = frequency;
609 }
610 } else {
611 int frequency = ch->control.frequency;
612 frequency += frequency >> ch->shift;
613 if (frequency < 2048) {
614 ch->control.frequency = frequency;
615 } else {
616 return false;
617 }
618 }
619 ch->sweepStep = ch->time;
620 return true;
621}
622
623static int32_t _updateChannel1(struct GBAudioChannel1* ch) {
624 int timing = _updateSquareChannel(&ch->control, ch->envelope.duty);
625 ch->sample = ch->control.hi * 0x10 - 0x8;
626 ch->sample *= ch->envelope.currentVolume;
627 return timing;
628}
629
630static int32_t _updateChannel2(struct GBAudioChannel2* ch) {
631 int timing = _updateSquareChannel(&ch->control, ch->envelope.duty);
632 ch->sample = ch->control.hi * 0x10 - 0x8;
633 ch->sample *= ch->envelope.currentVolume;
634 return timing;
635}
636
637static int32_t _updateChannel3(struct GBAudioChannel3* ch) {
638 int i;
639 int start;
640 int end;
641 int volume;
642 switch (ch->volume) {
643 case 0:
644 volume = 0;
645 break;
646 case 1:
647 volume = 4;
648 break;
649 case 2:
650 volume = 2;
651 break;
652 case 3:
653 volume = 1;
654 break;
655 default:
656 volume = 3;
657 break;
658 }
659 if (ch->size) {
660 start = 7;
661 end = 0;
662 } else if (ch->bank) {
663 start = 7;
664 end = 4;
665 } else {
666 start = 3;
667 end = 0;
668 }
669 uint32_t bitsCarry = ch->wavedata[end] & 0x000000F0;
670 uint32_t bits;
671 for (i = start; i >= end; --i) {
672 bits = ch->wavedata[i] & 0x000000F0;
673 ch->wavedata[i] = ((ch->wavedata[i] & 0x0F0F0F0F) << 4) | ((ch->wavedata[i] & 0xF0F0F000) >> 12);
674 ch->wavedata[i] |= bitsCarry << 20;
675 bitsCarry = bits;
676 }
677 ch->sample = bitsCarry >> 4;
678 ch->sample -= 8;
679 ch->sample *= volume * 4;
680 return 2 * (2048 - ch->rate);
681}
682
683static int32_t _updateChannel4(struct GBAudioChannel4* ch) {
684 int lsb = ch->lfsr & 1;
685 ch->sample = lsb * 0x10 - 0x8;
686 ch->sample *= ch->envelope.currentVolume;
687 ch->lfsr >>= 1;
688 ch->lfsr ^= (lsb * 0x60) << (ch->power ? 0 : 8);
689 int timing = ch->ratio ? 2 * ch->ratio : 1;
690 timing <<= ch->frequency;
691 timing *= 8;
692 return timing;
693}