-
Notifications
You must be signed in to change notification settings - Fork 1
/
GameBoyAdvanceSound.as
1551 lines (1534 loc) · 52.7 KB
/
GameBoyAdvanceSound.as
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package {
import sound.GameBoyAdvanceFIFO;
import utils.MathsHelper;
import utils.ArrayHelper;
public class GameBoyAdvanceSound {
public var channel3PCM;
public var WAVERAM;
public var audioTicks;
public var audioIndex = 0;
public var downsampleInputLeft = 0;
public var downsampleInputRight = 0;
public var LSFR15Table;
public var LSFR7Table;
public var nr60 = 0;
public var nr61 = 0;
public var nr62;
public var nr63;
public var soundMasterEnabled;
public var mixerSoundBIAS;
public var channel1currentSampleLeft = 0;
public var channel1currentSampleLeftSecondary = 0;
public var channel1currentSampleLeftTrimary = 0;
public var channel2currentSampleLeft = 0;
public var channel2currentSampleLeftSecondary = 0;
public var channel2currentSampleLeftTrimary = 0;
public var channel3currentSampleLeft = 0;
public var channel3currentSampleLeftSecondary = 0;
public var channel4currentSampleLeft = 0;
public var channel4currentSampleLeftSecondary = 0;
public var channel1currentSampleRight = 0;
public var channel1currentSampleRightSecondary = 0;
public var channel1currentSampleRightTrimary = 0;
public var channel2currentSampleRight = 0;
public var channel2currentSampleRightSecondary = 0;
public var channel2currentSampleRightTrimary = 0;
public var channel3currentSampleRight = 0;
public var channel3currentSampleRightSecondary = 0;
public var channel4currentSampleRight = 0;
public var channel4currentSampleRightSecondary = 0;
public var CGBMixerOutputCacheLeft = 0;
public var CGBMixerOutputCacheLeftFolded = 0;
public var CGBMixerOutputCacheRight = 0;
public var CGBMixerOutputCacheRightFolded = 0;
public var AGBDirectSoundATimer = 0;
public var AGBDirectSoundBTimer = 0;
public var AGBDirectSoundA = 0;
public var AGBDirectSoundAFolded = 0;
public var AGBDirectSoundB = 0;
public var AGBDirectSoundBFolded = 0;
public var AGBDirectSoundAShifter = 0;
public var AGBDirectSoundBShifter = 0;
public var AGBDirectSoundALeftCanPlay = false;
public var AGBDirectSoundBLeftCanPlay = false;
public var AGBDirectSoundARightCanPlay = false;
public var AGBDirectSoundBRightCanPlay = false;
public var CGBOutputRatio = 2;
public var FIFOABuffer;
public var FIFOBBuffer;
public var IOCore;
public var emulatorCore;
public var audioResamplerFirstPassFactor;
//Disable
public var nr10 = 0;
public var channel1SweepFault = false;
public var channel1lastTimeSweep = 0;
public var channel1timeSweep = 0;
public var channel1frequencySweepDivider = 0;
public var channel1decreaseSweep = false;
//Clear NR11:
public var nr11 = 0;
public var channel1CachedDuty;
public var channel1totalLength = 0x40;
//Clear NR12:
public var nr12 = 0;
public var channel1envelopeVolume = 0;
//Clear NR13:
public var channel1frequency = 0;
public var channel1FrequencyTracker = 0x8000;
//Clear NR14:
public var nr14 = 0;
public var channel1consecutive = true;
public var channel1ShadowFrequency = 0x8000;
public var channel1canPlay = false;
public var channel1Enabled = false;
public var channel1envelopeSweeps = 0;
public var channel1envelopeSweepsLast = -1;
//Clear NR21:
public var nr21 = 0;
public var channel2CachedDuty;
public var channel2totalLength = 0x40;
//Clear NR22:
public var nr22 = 0;
public var channel2envelopeVolume = 0;
//Clear NR23:
public var nr23 = 0;
public var channel2frequency = 0;
public var channel2FrequencyTracker = 0x8000;
//Clear NR24:
public var nr24 = 0;
public var channel2consecutive = true;
public var channel2canPlay = false;
public var channel2Enabled = false;
public var channel2envelopeSweeps = 0;
public var channel2envelopeSweepsLast = -1;
//Clear NR30:
public var nr30 = 0;
public var channel3lastSampleLookup = 0;
public var channel3canPlay = false;
public var channel3WAVERAMBankSpecified = 0x20;
public var channel3WaveRAMBankSize = 0x1F;
//Clear NR31:
public var channel3totalLength = 0x100;
//Clear NR32:
public var nr32 = 0;
public var channel3patternType = 4;
//Clear NR33:
public var nr33 = 0;
public var channel3frequency = 0;
public var channel3FrequencyPeriod = 0x4000;
//Clear NR34:
public var nr34 = 0;
public var channel3consecutive = true;
public var channel3Enabled = false;
//Clear NR41:
public var channel4totalLength = 0x40;
//Clear NR42:
public var nr42 = 0;
public var channel4envelopeVolume = 0;
//Clear NR43:
public var nr43 = 0;
public var channel4FrequencyPeriod = 32;
public var channel4lastSampleLookup = 0;
public var channel4BitRange = 0x7FFF;
public var channel4VolumeShifter = 15;
public var channel4currentVolume = 0;
public var noiseSampleTable;
//Clear NR44:
public var nr44 = 0;
public var channel4consecutive = true;
public var channel4envelopeSweeps = 0;
public var channel4envelopeSweepsLast = -1;
public var channel4canPlay = false;
public var channel4Enabled = false;
//Clear NR50:
public var nr50 = 0;
public var VinLeftChannelMasterVolume = 1;
public var VinRightChannelMasterVolume = 1;
//Clear NR51:
public var nr51 = 0;
public var rightChannel1 = false;
public var rightChannel2 = false;
public var rightChannel3 = false;
public var rightChannel4 = false;
public var leftChannel1 = false;
public var leftChannel2 = false;
public var leftChannel3 = false;
public var leftChannel4 = false;
//Clear NR52:
public var nr52 = 0;
//public var soundMasterEnabled = false;
public var mixerOutputCacheLeft;
public var mixerOutputCacheRight;
public var audioClocksUntilNextEventCounter = 0;
public var audioClocksUntilNextEvent = 0;
public var sequencePosition = 0;
public var sequencerClocks = 0x8000;
public var channel1FrequencyCounter = 0;
public var channel1DutyTracker = 0;
public var channel2FrequencyCounter = 0;
public var channel2DutyTracker = 0;
public var channel3Counter = 0;
public var channel4Counter = 0;
public var PWMWidth = 0x200;
public var PWMWidthOld = 0x200;
public var PWMWidthShadow = 0x200;
public var PWMBitDepthMask = 0x3FE;
public var PWMBitDepthMaskShadow = 0x3FE;
//funcs
public var generateAudio;
public var audioInitialized;
public var channel1Swept;
public var channel1envelopeType;
public var channel2envelopeType;
public var channel4envelopeType;
public var cachedChannel3Sample;
public var cachedChannel4Sample;
public function GameBoyAdvanceSound(IOCore) {
// constructor code
this.IOCore = IOCore;
this.emulatorCore = this.IOCore.emulatorCore;
this.initializePAPU();
}
public var dutyLookup = [
[false, false, false, false, false, false, false, true],
[true, false, false, false, false, false, false, true],
[true, false, false, false, false, true, true, true],
[false, true, true, true, true, true, true, false]
];
public function initializePAPU() {
//Did the emulator core initialize us for output yet?
this.preprocessInitialization(false);
//Initialize start:
this.channel3PCM = ArrayHelper.buildArray(0x40);
this.WAVERAM = ArrayHelper.buildArray(0x20);
this.audioTicks = 0;
this.intializeWhiteNoise();
this.initializeAudioStartState();
}
public function initializeOutput(enabled, audioResamplerFirstPassFactor) {
this.preprocessInitialization(enabled);
this.audioIndex = 0;
this.downsampleInputLeft = 0;
this.downsampleInputRight = 0;
this.audioResamplerFirstPassFactor = audioResamplerFirstPassFactor;
}
public function intializeWhiteNoise() {
//Noise Sample Tables:
var randomFactor = 1;
//15-bit LSFR Cache Generation:
this.LSFR15Table = ArrayHelper.buildArray(0x80000);
var LSFR = 0x7FFF; //Seed value has all its bits set.
var LSFRShifted = 0x3FFF;
for (var index = 0; index < 0x8000; ++index) {
//Normalize the last LSFR value for usage:
randomFactor = 1 - (LSFR & 1); //Docs say it's the inverse.
//Cache the different volume level results:
this.LSFR15Table[0x08000 | index] = randomFactor;
this.LSFR15Table[0x10000 | index] = randomFactor * 0x2;
this.LSFR15Table[0x18000 | index] = randomFactor * 0x3;
this.LSFR15Table[0x20000 | index] = randomFactor * 0x4;
this.LSFR15Table[0x28000 | index] = randomFactor * 0x5;
this.LSFR15Table[0x30000 | index] = randomFactor * 0x6;
this.LSFR15Table[0x38000 | index] = randomFactor * 0x7;
this.LSFR15Table[0x40000 | index] = randomFactor * 0x8;
this.LSFR15Table[0x48000 | index] = randomFactor * 0x9;
this.LSFR15Table[0x50000 | index] = randomFactor * 0xA;
this.LSFR15Table[0x58000 | index] = randomFactor * 0xB;
this.LSFR15Table[0x60000 | index] = randomFactor * 0xC;
this.LSFR15Table[0x68000 | index] = randomFactor * 0xD;
this.LSFR15Table[0x70000 | index] = randomFactor * 0xE;
this.LSFR15Table[0x78000 | index] = randomFactor * 0xF;
//Recompute the LSFR algorithm:
LSFRShifted = LSFR >> 1;
LSFR = LSFRShifted | (((LSFRShifted ^ LSFR) & 0x1) << 14);
}
//7-bit LSFR Cache Generation:
this.LSFR7Table = ArrayHelper.buildArray(0x800);
LSFR = 0x7F; //Seed value has all its bits set.
for (index = 0; index < 0x80; ++index) {
//Normalize the last LSFR value for usage:
randomFactor = 1 - (LSFR & 1); //Docs say it's the inverse.
//Cache the different volume level results:
this.LSFR7Table[0x080 | index] = randomFactor;
this.LSFR7Table[0x100 | index] = randomFactor * 0x2;
this.LSFR7Table[0x180 | index] = randomFactor * 0x3;
this.LSFR7Table[0x200 | index] = randomFactor * 0x4;
this.LSFR7Table[0x280 | index] = randomFactor * 0x5;
this.LSFR7Table[0x300 | index] = randomFactor * 0x6;
this.LSFR7Table[0x380 | index] = randomFactor * 0x7;
this.LSFR7Table[0x400 | index] = randomFactor * 0x8;
this.LSFR7Table[0x480 | index] = randomFactor * 0x9;
this.LSFR7Table[0x500 | index] = randomFactor * 0xA;
this.LSFR7Table[0x580 | index] = randomFactor * 0xB;
this.LSFR7Table[0x600 | index] = randomFactor * 0xC;
this.LSFR7Table[0x680 | index] = randomFactor * 0xD;
this.LSFR7Table[0x700 | index] = randomFactor * 0xE;
this.LSFR7Table[0x780 | index] = randomFactor * 0xF;
//Recompute the LSFR algorithm:
LSFRShifted = LSFR >> 1;
LSFR = LSFRShifted | (((LSFRShifted ^ LSFR) & 0x1) << 6);
}
}
public function initializeAudioStartState() {
//NOTE: NR 60-63 never get reset in audio halting:
this.nr60 = 0;
this.nr61 = 0;
this.nr62 = (this.IOCore.BIOSFound && !this.emulatorCore.SKIPBoot) ? 0 : 0xFF;
this.nr63 = (this.IOCore.BIOSFound && !this.emulatorCore.SKIPBoot) ? 0 : 0x2;
this.soundMasterEnabled = (!this.IOCore.BIOSFound || this.emulatorCore.SKIPBoot);
this.mixerSoundBIAS = (this.IOCore.BIOSFound && !this.emulatorCore.SKIPBoot) ? 0 : 0x200;
this.channel1currentSampleLeft = 0;
this.channel1currentSampleLeftSecondary = 0;
this.channel1currentSampleLeftTrimary = 0;
this.channel2currentSampleLeft = 0;
this.channel2currentSampleLeftSecondary = 0;
this.channel2currentSampleLeftTrimary = 0;
this.channel3currentSampleLeft = 0;
this.channel3currentSampleLeftSecondary = 0;
this.channel4currentSampleLeft = 0;
this.channel4currentSampleLeftSecondary = 0;
this.channel1currentSampleRight = 0;
this.channel1currentSampleRightSecondary = 0;
this.channel1currentSampleRightTrimary = 0;
this.channel2currentSampleRight = 0;
this.channel2currentSampleRightSecondary = 0;
this.channel2currentSampleRightTrimary = 0;
this.channel3currentSampleRight = 0;
this.channel3currentSampleRightSecondary = 0;
this.channel4currentSampleRight = 0;
this.channel4currentSampleRightSecondary = 0;
this.CGBMixerOutputCacheLeft = 0;
this.CGBMixerOutputCacheLeftFolded = 0;
this.CGBMixerOutputCacheRight = 0;
this.CGBMixerOutputCacheRightFolded = 0;
this.AGBDirectSoundATimer = 0;
this.AGBDirectSoundBTimer = 0;
this.AGBDirectSoundA = 0;
this.AGBDirectSoundAFolded = 0;
this.AGBDirectSoundB = 0;
this.AGBDirectSoundBFolded = 0;
this.AGBDirectSoundAShifter = 0;
this.AGBDirectSoundBShifter = 0;
this.AGBDirectSoundALeftCanPlay = false;
this.AGBDirectSoundBLeftCanPlay = false;
this.AGBDirectSoundARightCanPlay = false;
this.AGBDirectSoundBRightCanPlay = false;
this.CGBOutputRatio = 2;
this.FIFOABuffer = new GameBoyAdvanceFIFO();
this.FIFOBBuffer = new GameBoyAdvanceFIFO();
this.AGBDirectSoundAFIFOClear();
this.AGBDirectSoundBFIFOClear();
this.audioDisabled(); //Clear legacy PAPU registers:
}
public function audioDisabled() {
//Clear NR10:
this.nr10 = 0;
this.channel1SweepFault = false;
this.channel1lastTimeSweep = 0;
this.channel1timeSweep = 0;
this.channel1frequencySweepDivider = 0;
this.channel1decreaseSweep = false;
//Clear NR11:
this.nr11 = 0;
this.channel1CachedDuty = this.dutyLookup[0];
this.channel1totalLength = 0x40;
//Clear NR12:
this.nr12 = 0;
this.channel1envelopeVolume = 0;
//Clear NR13:
this.channel1frequency = 0;
this.channel1FrequencyTracker = 0x8000;
//Clear NR14:
this.nr14 = 0;
this.channel1consecutive = true;
this.channel1ShadowFrequency = 0x8000;
this.channel1canPlay = false;
this.channel1Enabled = false;
this.channel1envelopeSweeps = 0;
this.channel1envelopeSweepsLast = -1;
//Clear NR21:
this.nr21 = 0;
this.channel2CachedDuty = this.dutyLookup[0];
this.channel2totalLength = 0x40;
//Clear NR22:
this.nr22 = 0;
this.channel2envelopeVolume = 0;
//Clear NR23:
this.nr23 = 0;
this.channel2frequency = 0;
this.channel2FrequencyTracker = 0x8000;
//Clear NR24:
this.nr24 = 0;
this.channel2consecutive = true;
this.channel2canPlay = false;
this.channel2Enabled = false;
this.channel2envelopeSweeps = 0;
this.channel2envelopeSweepsLast = -1;
//Clear NR30:
this.nr30 = 0;
this.channel3lastSampleLookup = 0;
this.channel3canPlay = false;
this.channel3WAVERAMBankSpecified = 0x20;
this.channel3WaveRAMBankSize = 0x1F;
//Clear NR31:
this.channel3totalLength = 0x100;
//Clear NR32:
this.nr32 = 0;
this.channel3patternType = 4;
//Clear NR33:
this.nr33 = 0;
this.channel3frequency = 0;
this.channel3FrequencyPeriod = 0x4000;
//Clear NR34:
this.nr34 = 0;
this.channel3consecutive = true;
this.channel3Enabled = false;
//Clear NR41:
this.channel4totalLength = 0x40;
//Clear NR42:
this.nr42 = 0;
this.channel4envelopeVolume = 0;
//Clear NR43:
this.nr43 = 0;
this.channel4FrequencyPeriod = 32;
this.channel4lastSampleLookup = 0;
this.channel4BitRange = 0x7FFF;
this.channel4VolumeShifter = 15;
this.channel4currentVolume = 0;
this.noiseSampleTable = this.LSFR15Table;
//Clear NR44:
this.nr44 = 0;
this.channel4consecutive = true;
this.channel4envelopeSweeps = 0;
this.channel4envelopeSweepsLast = -1;
this.channel4canPlay = false;
this.channel4Enabled = false;
//Clear NR50:
this.nr50 = 0;
this.VinLeftChannelMasterVolume = 1;
this.VinRightChannelMasterVolume = 1;
//Clear NR51:
this.nr51 = 0;
this.rightChannel1 = false;
this.rightChannel2 = false;
this.rightChannel3 = false;
this.rightChannel4 = false;
this.leftChannel1 = false;
this.leftChannel2 = false;
this.leftChannel3 = false;
this.leftChannel4 = false;
//Clear NR52:
this.nr52 = 0;
this.soundMasterEnabled = false;
this.mixerOutputCacheLeft = this.mixerSoundBIAS | 0;
this.mixerOutputCacheRight = this.mixerSoundBIAS | 0;
this.audioClocksUntilNextEventCounter = 0;
this.audioClocksUntilNextEvent = 0;
this.sequencePosition = 0;
this.sequencerClocks = 0x8000;
this.channel1FrequencyCounter = 0;
this.channel1DutyTracker = 0;
this.channel2FrequencyCounter = 0;
this.channel2DutyTracker = 0;
this.channel3Counter = 0;
this.channel4Counter = 0;
this.PWMWidth = 0x200;
this.PWMWidthOld = 0x200;
this.PWMWidthShadow = 0x200;
this.PWMBitDepthMask = 0x3FE;
this.PWMBitDepthMaskShadow = 0x3FE;
this.channel1OutputLevelCache();
this.channel2OutputLevelCache();
this.channel3UpdateCache();
this.channel4UpdateCache();
}
public function audioEnabled() {
//Set NR52:
this.nr52 = 0x80;
this.soundMasterEnabled = true;
}
public function addClocks(clocks) {
clocks = clocks | 0;
this.audioTicks = ((this.audioTicks | 0) + (clocks | 0)) | 0;
}
public function generateAudioSlow(numSamples) {
numSamples = numSamples | 0;
var multiplier = 0;
if (this.soundMasterEnabled && this.IOCore.systemStatus < 4) {
for (var clockUpTo = 0; (numSamples | 0) > 0;) {
clockUpTo = Math.min(this.PWMWidth | 0, numSamples | 0) | 0;
this.PWMWidth = ((this.PWMWidth | 0) - (clockUpTo | 0)) | 0;
numSamples = ((numSamples | 0) - (clockUpTo | 0)) | 0;
while ((clockUpTo | 0) > 0) {
multiplier = Math.min(clockUpTo | 0, ((this.audioResamplerFirstPassFactor | 0) - (this.audioIndex | 0)) | 0) | 0;
clockUpTo = ((clockUpTo | 0) - (multiplier | 0)) | 0;
this.audioIndex = ((this.audioIndex | 0) + (multiplier | 0)) | 0;
this.downsampleInputLeft = ((this.downsampleInputLeft | 0) + (((this.mixerOutputCacheLeft | 0) * (multiplier | 0)) | 0)) | 0;
this.downsampleInputRight = ((this.downsampleInputRight | 0) + (((this.mixerOutputCacheRight | 0) * (multiplier | 0)) | 0)) | 0;
if ((this.audioIndex | 0) == (this.audioResamplerFirstPassFactor | 0)) {
this.audioIndex = 0;
this.emulatorCore.outputAudio(this.downsampleInputLeft | 0, this.downsampleInputRight | 0);
this.downsampleInputLeft = 0;
this.downsampleInputRight = 0;
}
}
if ((this.PWMWidth | 0) == 0) {
this.computeNextPWMInterval();
this.PWMWidthOld = this.PWMWidthShadow | 0;
this.PWMWidth = this.PWMWidthShadow | 0;
}
}
}
else {
//SILENT OUTPUT:
while ((numSamples | 0) > 0) {
multiplier = Math.min(numSamples | 0, ((this.audioResamplerFirstPassFactor | 0) - (this.audioIndex | 0)) | 0) | 0;
numSamples = ((numSamples | 0) - (multiplier | 0)) | 0;
this.audioIndex = ((this.audioIndex | 0) + (multiplier | 0)) | 0;
if ((this.audioIndex | 0) == (this.audioResamplerFirstPassFactor | 0)) {
this.audioIndex = 0;
this.emulatorCore.outputAudio(this.downsampleInputLeft | 0, this.downsampleInputRight | 0);
this.downsampleInputLeft = 0;
this.downsampleInputRight = 0;
}
}
}
}
public function generateAudioOptimized(numSamples) {
trace("audio");
numSamples = numSamples | 0;
var multiplier = 0;
if (this.soundMasterEnabled && this.IOCore.systemStatus < 4) {
for (var clockUpTo = 0; (numSamples | 0) > 0;) {
clockUpTo = Math.min(this.PWMWidth | 0, numSamples | 0) | 0;
this.PWMWidth = ((this.PWMWidth | 0) - (clockUpTo | 0)) | 0;
numSamples = ((numSamples | 0) - (clockUpTo | 0)) | 0;
while ((clockUpTo | 0) > 0) {
multiplier = Math.min(clockUpTo | 0, ((this.audioResamplerFirstPassFactor | 0) - (this.audioIndex | 0)) | 0) | 0;
clockUpTo = ((clockUpTo | 0) - (multiplier | 0)) | 0;
this.audioIndex = ((this.audioIndex | 0) + (multiplier | 0)) | 0;
this.downsampleInputLeft = ((this.downsampleInputLeft | 0) + MathsHelper.imul(this.mixerOutputCacheLeft | 0, multiplier | 0)) | 0;
this.downsampleInputRight = ((this.downsampleInputRight | 0) + MathsHelper.imul(this.mixerOutputCacheRight | 0, multiplier | 0)) | 0;
if ((this.audioIndex | 0) == (this.audioResamplerFirstPassFactor | 0)) {
this.audioIndex = 0;
this.emulatorCore.outputAudio(this.downsampleInputLeft | 0, this.downsampleInputRight | 0);
this.downsampleInputLeft = 0;
this.downsampleInputRight = 0;
}
}
if ((this.PWMWidth | 0) == 0) {
this.computeNextPWMInterval();
this.PWMWidthOld = this.PWMWidthShadow | 0;
this.PWMWidth = this.PWMWidthShadow | 0;
}
}
}
else {
//SILENT OUTPUT:
while ((numSamples | 0) > 0) {
multiplier = Math.min(numSamples | 0, ((this.audioResamplerFirstPassFactor | 0) - (this.audioIndex | 0)) | 0) | 0;
numSamples = ((numSamples | 0) - (multiplier | 0)) | 0;
this.audioIndex = ((this.audioIndex | 0) + (multiplier | 0)) | 0;
if ((this.audioIndex | 0) == (this.audioResamplerFirstPassFactor | 0)) {
this.audioIndex = 0;
this.emulatorCore.outputAudio(this.downsampleInputLeft | 0, this.downsampleInputRight | 0);
this.downsampleInputLeft = 0;
this.downsampleInputRight = 0;
}
}
}
}
//Generate audio, but don't actually output it (Used for when sound is disabled by user/browser):
public function generateAudioFake(numSamples) {
numSamples = numSamples | 0;
if (this.soundMasterEnabled && this.IOCore.systemStatus < 4) {
for (var clockUpTo = 0; (numSamples | 0) > 0;) {
clockUpTo = Math.min(this.PWMWidth | 0, numSamples | 0) | 0;
this.PWMWidth = ((this.PWMWidth | 0) - (clockUpTo | 0)) | 0;
numSamples = ((numSamples | 0) - (clockUpTo | 0)) | 0;
if ((this.PWMWidth | 0) == 0) {
this.computeNextPWMInterval();
this.PWMWidthOld = this.PWMWidthShadow | 0;
this.PWMWidth = this.PWMWidthShadow | 0;
}
}
}
}
public function preprocessInitialization(audioInitialized) {
trace("init audio: " + audioInitialized);
if (audioInitialized) {
this.generateAudio = (MathsHelper.imul != null) ? this.generateAudioOptimized : this.generateAudioSlow;
this.audioInitialized = true;
}
else {
this.generateAudio = this.generateAudioFake;
this.audioInitialized = false;
}
}
public function audioJIT() {
//Audio Sample Generation Timing:
this.generateAudio(this.audioTicks | 0);
this.audioTicks = 0;
}
public function computeNextPWMInterval() {
//Clock down the PSG system:
for (var numSamples = this.PWMWidthOld | 0, clockUpTo = 0; numSamples > 0; numSamples = ((numSamples | 0) - 1) | 0) {
clockUpTo = Math.min(this.audioClocksUntilNextEventCounter | 0, this.sequencerClocks | 0, numSamples | 0) | 0;
this.audioClocksUntilNextEventCounter = ((this.audioClocksUntilNextEventCounter | 0) - (clockUpTo | 0)) | 0;
this.sequencerClocks = ((this.sequencerClocks | 0) - (clockUpTo | 0)) | 0;
numSamples = ((numSamples | 0) - (clockUpTo | 0)) | 0;
if ((this.sequencerClocks | 0) == 0) {
this.audioComputeSequencer();
this.sequencerClocks = 0x8000;
}
if ((this.audioClocksUntilNextEventCounter | 0) == 0) {
this.computeAudioChannels();
}
}
//Copy the new bit-depth mask for the next counter interval:
this.PWMBitDepthMask = this.PWMBitDepthMaskShadow | 0;
//Compute next sample for the PWM output:
this.channel1OutputLevelCache();
this.channel2OutputLevelCache();
this.channel3UpdateCache();
this.channel4UpdateCache();
this.CGBMixerOutputLevelCache();
this.mixerOutputLevelCache();
}
public function audioComputeSequencer() {
switch (this.sequencePosition++) {
case 0:
this.clockAudioLength();
break;
case 2:
this.clockAudioLength();
this.clockAudioSweep();
break;
case 4:
this.clockAudioLength();
break;
case 6:
this.clockAudioLength();
this.clockAudioSweep();
break;
case 7:
this.clockAudioEnvelope();
this.sequencePosition = 0;
}
}
public function clockAudioLength() {
//Channel 1:
if (this.channel1totalLength > 1) {
--this.channel1totalLength;
}
else if (this.channel1totalLength == 1) {
this.channel1totalLength = 0;
this.channel1EnableCheck();
this.nr52 &= 0xFE; //Channel #1 On Flag Off
}
//Channel 2:
if (this.channel2totalLength > 1) {
--this.channel2totalLength;
}
else if (this.channel2totalLength == 1) {
this.channel2totalLength = 0;
this.channel2EnableCheck();
this.nr52 &= 0xFD; //Channel #2 On Flag Off
}
//Channel 3:
if (this.channel3totalLength > 1) {
--this.channel3totalLength;
}
else if (this.channel3totalLength == 1) {
this.channel3totalLength = 0;
this.channel3EnableCheck();
this.nr52 &= 0xFB; //Channel #3 On Flag Off
}
//Channel 4:
if (this.channel4totalLength > 1) {
--this.channel4totalLength;
}
else if (this.channel4totalLength == 1) {
this.channel4totalLength = 0;
this.channel4EnableCheck();
this.nr52 &= 0xF7; //Channel #4 On Flag Off
}
}
public function clockAudioSweep() {
//Channel 1:
if (!this.channel1SweepFault && this.channel1timeSweep > 0) {
if (--this.channel1timeSweep == 0) {
this.runAudioSweep();
}
}
}
public function runAudioSweep() {
//Channel 1:
if (this.channel1lastTimeSweep > 0) {
if (this.channel1frequencySweepDivider > 0) {
this.channel1Swept = true;
if (this.channel1decreaseSweep) {
this.channel1ShadowFrequency -= this.channel1ShadowFrequency >> this.channel1frequencySweepDivider;
this.channel1frequency = this.channel1ShadowFrequency & 0x7FF;
this.channel1FrequencyTracker = (0x800 - this.channel1frequency) << 4;
}
else {
this.channel1ShadowFrequency += this.channel1ShadowFrequency >> this.channel1frequencySweepDivider;
this.channel1frequency = this.channel1ShadowFrequency;
if (this.channel1ShadowFrequency <= 0x7FF) {
this.channel1FrequencyTracker = (0x800 - this.channel1frequency) << 4;
//Run overflow check twice:
if ((this.channel1ShadowFrequency + (this.channel1ShadowFrequency >> this.channel1frequencySweepDivider)) > 0x7FF) {
this.channel1SweepFault = true;
this.channel1EnableCheck();
this.nr52 &= 0xFE; //Channel #1 On Flag Off
}
}
else {
this.channel1frequency &= 0x7FF;
this.channel1SweepFault = true;
this.channel1EnableCheck();
this.nr52 &= 0xFE; //Channel #1 On Flag Off
}
}
this.channel1timeSweep = this.channel1lastTimeSweep;
}
else {
//Channel has sweep disabled and timer becomes a length counter:
this.channel1SweepFault = true;
this.channel1EnableCheck();
}
}
}
public function channel1AudioSweepPerformDummy() {
//Channel 1:
if (this.channel1frequencySweepDivider > 0) {
if (!this.channel1decreaseSweep) {
var channel1ShadowFrequency = this.channel1ShadowFrequency + (this.channel1ShadowFrequency >> this.channel1frequencySweepDivider);
if (channel1ShadowFrequency <= 0x7FF) {
//Run overflow check twice:
if ((channel1ShadowFrequency + (channel1ShadowFrequency >> this.channel1frequencySweepDivider)) > 0x7FF) {
this.channel1SweepFault = true;
this.channel1EnableCheck();
this.nr52 &= 0xFE; //Channel #1 On Flag Off
}
}
else {
this.channel1SweepFault = true;
this.channel1EnableCheck();
this.nr52 &= 0xFE; //Channel #1 On Flag Off
}
}
}
}
public function clockAudioEnvelope() {
//Channel 1:
if (this.channel1envelopeSweepsLast > -1) {
if (this.channel1envelopeSweeps > 0) {
--this.channel1envelopeSweeps;
}
else {
if (!this.channel1envelopeType) {
if (this.channel1envelopeVolume > 0) {
--this.channel1envelopeVolume;
this.channel1envelopeSweeps = this.channel1envelopeSweepsLast;
}
else {
this.channel1envelopeSweepsLast = -1;
}
}
else if (this.channel1envelopeVolume < 0xF) {
++this.channel1envelopeVolume;
this.channel1envelopeSweeps = this.channel1envelopeSweepsLast;
}
else {
this.channel1envelopeSweepsLast = -1;
}
}
}
//Channel 2:
if (this.channel2envelopeSweepsLast > -1) {
if (this.channel2envelopeSweeps > 0) {
--this.channel2envelopeSweeps;
}
else {
if (!this.channel2envelopeType) {
if (this.channel2envelopeVolume > 0) {
--this.channel2envelopeVolume;
this.channel2envelopeSweeps = this.channel2envelopeSweepsLast;
}
else {
this.channel2envelopeSweepsLast = -1;
}
}
else if (this.channel2envelopeVolume < 0xF) {
++this.channel2envelopeVolume;
this.channel2envelopeSweeps = this.channel2envelopeSweepsLast;
}
else {
this.channel2envelopeSweepsLast = -1;
}
}
}
//Channel 4:
if (this.channel4envelopeSweepsLast > -1) {
if (this.channel4envelopeSweeps > 0) {
--this.channel4envelopeSweeps;
}
else {
if (!this.channel4envelopeType) {
if (this.channel4envelopeVolume > 0) {
this.channel4currentVolume = --this.channel4envelopeVolume << this.channel4VolumeShifter;
this.channel4envelopeSweeps = this.channel4envelopeSweepsLast;
}
else {
this.channel4envelopeSweepsLast = -1;
}
}
else if (this.channel4envelopeVolume < 0xF) {
this.channel4currentVolume = ++this.channel4envelopeVolume << this.channel4VolumeShifter;
this.channel4envelopeSweeps = this.channel4envelopeSweepsLast;
}
else {
this.channel4envelopeSweepsLast = -1;
}
}
}
}
public function computeAudioChannels() {
//Clock down the four audio channels to the next closest audio event:
this.channel1FrequencyCounter -= this.audioClocksUntilNextEvent;
this.channel2FrequencyCounter -= this.audioClocksUntilNextEvent;
this.channel3Counter -= this.audioClocksUntilNextEvent;
this.channel4Counter -= this.audioClocksUntilNextEvent;
//Channel 1 counter:
if (this.channel1FrequencyCounter == 0) {
this.channel1FrequencyCounter = this.channel1FrequencyTracker;
this.channel1DutyTracker = (this.channel1DutyTracker + 1) & 0x7;
}
//Channel 2 counter:
if (this.channel2FrequencyCounter == 0) {
this.channel2FrequencyCounter = this.channel2FrequencyTracker;
this.channel2DutyTracker = (this.channel2DutyTracker + 1) & 0x7;
}
//Channel 3 counter:
if (this.channel3Counter == 0) {
if (this.channel3canPlay) {
this.channel3lastSampleLookup = (this.channel3lastSampleLookup + 1) & this.channel3WaveRAMBankSize;
}
this.channel3Counter = this.channel3FrequencyPeriod;
}
//Channel 4 counter:
if (this.channel4Counter == 0) {
this.channel4lastSampleLookup = (this.channel4lastSampleLookup + 1) & this.channel4BitRange;
this.channel4Counter = this.channel4FrequencyPeriod;
}
//Find the number of clocks to next closest counter event:
this.audioClocksUntilNextEventCounter = this.audioClocksUntilNextEvent = Math.min(this.channel1FrequencyCounter, this.channel2FrequencyCounter, this.channel3Counter, this.channel4Counter);
}
public function channel1EnableCheck() {
this.channel1Enabled = ((this.channel1consecutive || this.channel1totalLength > 0) && !this.channel1SweepFault && this.channel1canPlay);
}
public function channel1VolumeEnableCheck() {
this.channel1canPlay = (this.nr12 > 7);
this.channel1EnableCheck();
}
public function channel1OutputLevelCache() {
this.channel1currentSampleLeft = (this.leftChannel1) ? this.channel1envelopeVolume : 0;
this.channel1currentSampleRight = (this.rightChannel1) ? this.channel1envelopeVolume : 0;
this.channel1OutputLevelSecondaryCache();
}
public function channel1OutputLevelSecondaryCache() {
if (this.channel1Enabled) {
this.channel1currentSampleLeftSecondary = this.channel1currentSampleLeft;
this.channel1currentSampleRightSecondary = this.channel1currentSampleRight;
}
else {
this.channel1currentSampleLeftSecondary = 0;
this.channel1currentSampleRightSecondary = 0;
}
this.channel1OutputLevelTrimaryCache();
}
public function channel1OutputLevelTrimaryCache() {
if (this.channel1CachedDuty[this.channel1DutyTracker]) {
this.channel1currentSampleLeftTrimary = this.channel1currentSampleLeftSecondary;
this.channel1currentSampleRightTrimary = this.channel1currentSampleRightSecondary;
}
else {
this.channel1currentSampleLeftTrimary = 0;
this.channel1currentSampleRightTrimary = 0;
}
}
public function channel2EnableCheck() {
this.channel2Enabled = ((this.channel2consecutive || this.channel2totalLength > 0) && this.channel2canPlay);
}
public function channel2VolumeEnableCheck() {
this.channel2canPlay = (this.nr22 > 7);
this.channel2EnableCheck();
}
public function channel2OutputLevelCache() {
this.channel2currentSampleLeft = (this.leftChannel2) ? this.channel2envelopeVolume : 0;
this.channel2currentSampleRight = (this.rightChannel2) ? this.channel2envelopeVolume : 0;
this.channel2OutputLevelSecondaryCache();
}
public function channel2OutputLevelSecondaryCache() {
if (this.channel2Enabled) {
this.channel2currentSampleLeftSecondary = this.channel2currentSampleLeft;
this.channel2currentSampleRightSecondary = this.channel2currentSampleRight;
}
else {
this.channel2currentSampleLeftSecondary = 0;
this.channel2currentSampleRightSecondary = 0;
}
this.channel2OutputLevelTrimaryCache();
}
public function channel2OutputLevelTrimaryCache() {
if (this.channel2CachedDuty[this.channel2DutyTracker]) {
this.channel2currentSampleLeftTrimary = this.channel2currentSampleLeftSecondary;
this.channel2currentSampleRightTrimary = this.channel2currentSampleRightSecondary;
}
else {
this.channel2currentSampleLeftTrimary = 0;
this.channel2currentSampleRightTrimary = 0;
}
}
public function channel3EnableCheck() {
this.channel3Enabled = (/*this.channel3canPlay && */(this.channel3consecutive || this.channel3totalLength > 0));
}
public function channel3OutputLevelCache() {
this.channel3currentSampleLeft = (this.leftChannel3) ? this.cachedChannel3Sample : 0;
this.channel3currentSampleRight = (this.rightChannel3) ? this.cachedChannel3Sample : 0;
this.channel3OutputLevelSecondaryCache();
}
public function channel3OutputLevelSecondaryCache() {
if (this.channel3Enabled) {
this.channel3currentSampleLeftSecondary = this.channel3currentSampleLeft;
this.channel3currentSampleRightSecondary = this.channel3currentSampleRight;
}
else {
this.channel3currentSampleLeftSecondary = 0;
this.channel3currentSampleRightSecondary = 0;
}
}
public function channel4EnableCheck() {
this.channel4Enabled = ((this.channel4consecutive || this.channel4totalLength > 0) && this.channel4canPlay);
}
public function channel4VolumeEnableCheck() {
this.channel4canPlay = (this.nr42 > 7);
this.channel4EnableCheck();
}
public function channel4OutputLevelCache() {
this.channel4currentSampleLeft = (this.leftChannel4) ? this.cachedChannel4Sample : 0;
this.channel4currentSampleRight = (this.rightChannel4) ? this.cachedChannel4Sample : 0;
this.channel4OutputLevelSecondaryCache();
}
public function channel4OutputLevelSecondaryCache() {
if (this.channel4Enabled) {
this.channel4currentSampleLeftSecondary = this.channel4currentSampleLeft;
this.channel4currentSampleRightSecondary = this.channel4currentSampleRight;
}
else {
this.channel4currentSampleLeftSecondary = 0;
this.channel4currentSampleRightSecondary = 0;
}
}
public function CGBMixerOutputLevelCache() {
this.CGBMixerOutputCacheLeft = (this.channel1currentSampleLeftTrimary + this.channel2currentSampleLeftTrimary + this.channel3currentSampleLeftSecondary + this.channel4currentSampleLeftSecondary) * this.VinLeftChannelMasterVolume;
this.CGBMixerOutputCacheRight = (this.channel1currentSampleRightTrimary + this.channel2currentSampleRightTrimary + this.channel3currentSampleRightSecondary + this.channel4currentSampleRightSecondary) * this.VinRightChannelMasterVolume;
this.CGBFolder();
}
public function channel3UpdateCache() {
if (this.channel3patternType < 5) {
this.cachedChannel3Sample = this.channel3PCM[this.channel3lastSampleLookup] >> this.channel3patternType;
}
else {
this.cachedChannel3Sample = (this.channel3PCM[this.channel3lastSampleLookup] * 0.75) | 0;
}
this.channel3OutputLevelCache();
}
public function writeWAVE(address, data) {
if (this.channel3canPlay) {
this.audioJIT();
}
address += this.channel3WAVERAMBankSpecified;
this.WAVERAM[address] = data;
address <<= 1;
this.channel3PCM[address] = data >> 4;
this.channel3PCM[address | 1] = data & 0xF;
}
public function readWAVE(address) {
return this.WAVERAM[address + this.channel3WAVERAMBankSpecified];
}
public function channel4UpdateCache() {
this.cachedChannel4Sample = this.noiseSampleTable[this.channel4currentVolume | this.channel4lastSampleLookup];
this.channel4OutputLevelCache();
}
public function writeFIFOA(data) {
data = data | 0;
this.FIFOABuffer.push(data | 0);
if (this.FIFOABuffer.requestingDMA()) {
this.IOCore.dma.soundFIFOARequest();
}
}
public function checkFIFOAPendingSignal() {
if (this.FIFOABuffer.requestingDMA()) {
this.IOCore.dma.soundFIFOARequest();
}
}
public function checkFIFOBPendingSignal() {
if (this.FIFOBBuffer.requestingDMA()) {
this.IOCore.dma.soundFIFOBRequest();
}
}
public function writeFIFOB(data) {
data = data | 0;
this.FIFOBBuffer.push(data | 0);
if (this.FIFOBBuffer.requestingDMA()) {