-
Notifications
You must be signed in to change notification settings - Fork 0
/
ym2612.js
1701 lines (1608 loc) · 58.6 KB
/
ym2612.js
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
function YM2612() {
if (!this instanceof YM2612) return new YM2612();
this.version = 0x101;
this.start = 0;
this.count = 0;
this.chip = null;
}
(function(Y){
"use strict";
/**** CONFIG ****/
var cfg = {
hq_fm:0, // force 53kHz sampling rate
dac_bits:8, // DAC width
maxcalc:0, // for logging, # total chan_calc ops to log
debug:0, // for logging
debugLocal:0,
debugArr:[],
mode:0, // 0=gpgx, 1=vb/scale
strict:0 // abort on bad input if true
};
/**** GLOBALS ****/
var _YM = { //// used if cfg.mode = 1 (i.e. scale the tables to a ratio of clock) +neo
"FREQ_SH":16, // 16.16 fixed point (freq calcs)
"EG_SH":16, // 16.16 fixed point (env gen timing)
"LFO_SH":24, // 8.24 fixed point (lfo calcs)
"TIMER_SH":16 // 16.16 fixed point (timers calcs)
};
_YM.FREQ_MASK = (1<<_YM.FREQ_SH)-1;
/**** ENVELOPE GENERATOR ****/
var _ENV = {
"BITS":10,
"MIN_ATT_INDEX":0
};
_ENV.LEN = 1<<_ENV.BITS;
_ENV.STEP = 128.0/_ENV.LEN;
_ENV.MAX_ATT_INDEX = _ENV.LEN-1;
var _EG = {
'ATT':4,
'DEC':3,
'SUS':2,
'REL':1,
'OFF':0
};
/**** PHASE GENERATOR (detune mask) ****/
var _DT = {
"BITS":17
};
_DT.LEN = 1<<_DT.BITS;
_DT.MASK = _DT.LEN-1;
/**** OPERATOR UNIT ****/
var _SIN = {
"BITS":10
};
_SIN.LEN = 1<<_SIN.BITS;
_SIN.MASK = _SIN.LEN-1;
var _TL = {
"BITS":14
};
_TL.RES_LEN = 256; // sinus resolution
_TL.TAB_LEN = 13*2*_TL.RES_LEN; // 13 = sinus amplitude bits, 2 = sinus sign bit
_TL.tab = new Array(_TL.TAB_LEN);
_ENV.QUIET = _TL.TAB_LEN>>3;
/* sin waveform table in 'decibel' scale */
_YM.sin = new Array(_SIN.LEN);
/* sustain level table (3dB per step) */
/* bit0, bit1, bit2, bit3, bit4, bit5, bit6 */
/* 1, 2, 4, 8, 16, 32, 64 (value)*/
/* 0.75, 1.5, 3, 6, 12, 24, 48 (dB)*/
/* 0 - 15: 0, 3, 6, 9,12,15,18,21,24,27,30,33,36,39,42,93 (dB)*/
/* attenuation value (10 bits) = (SL << 2) << 3 */
_YM.sl = (function(){
var SC = function(db){return (db*4.0/_ENV.STEP)|0;};
return [
SC(0), SC(1), SC(2), SC(3), SC(4), SC(5), SC(6), SC(7),
SC(8), SC(9), SC(10), SC(11), SC(12), SC(13), SC(14), SC(31)
];
})();
_EG.RATE_STEPS = 8;
_EG.inc = [ // 19*_EG.RATE_STEPS
/*cycle:0 1 2 3 4 5 6 7*/
/* 0 */ 0,1, 0,1, 0,1, 0,1, /* rates 00..11 0 (increment by 0 or 1) */
/* 1 */ 0,1, 0,1, 1,1, 0,1, /* rates 00..11 1 */
/* 2 */ 0,1, 1,1, 0,1, 1,1, /* rates 00..11 2 */
/* 3 */ 0,1, 1,1, 1,1, 1,1, /* rates 00..11 3 */
/* 4 */ 1,1, 1,1, 1,1, 1,1, /* rate 12 0 (increment by 1) */
/* 5 */ 1,1, 1,2, 1,1, 1,2, /* rate 12 1 */
/* 6 */ 1,2, 1,2, 1,2, 1,2, /* rate 12 2 */
/* 7 */ 1,2, 2,2, 1,2, 2,2, /* rate 12 3 */
/* 8 */ 2,2, 2,2, 2,2, 2,2, /* rate 13 0 (increment by 2) */
/* 9 */ 2,2, 2,4, 2,2, 2,4, /* rate 13 1 */
/*10 */ 2,4, 2,4, 2,4, 2,4, /* rate 13 2 */
/*11 */ 2,4, 4,4, 2,4, 4,4, /* rate 13 3 */
/*12 */ 4,4, 4,4, 4,4, 4,4, /* rate 14 0 (increment by 4) */
/*13 */ 4,4, 4,8, 4,4, 4,8, /* rate 14 1 */
/*14 */ 4,8, 4,8, 4,8, 4,8, /* rate 14 2 */
/*15 */ 4,8, 8,8, 4,8, 8,8, /* rate 14 3 */
/*16 */ 8,8, 8,8, 8,8, 8,8, /* rates 15 0, 15 1, 15 2, 15 3 (increment by 8) */
/*17 */ 16,16,16,16,16,16,16,16, /* rates 15 2, 15 3 for attack */
/*18 */ 0,0, 0,0, 0,0, 0,0, /* infinity rates for attack and decay(s) */
];
_EG.rate_select = (function(){
var O = function(a){return (a*_EG.RATE_STEPS)|0;};
return [ // env gen rates - 32+64 rates+32 RKS
/* 32 infinite time rates (same as Rate 0) */
O(18),O(18),O(18),O(18),O(18),O(18),O(18),O(18),
O(18),O(18),O(18),O(18),O(18),O(18),O(18),O(18),
O(18),O(18),O(18),O(18),O(18),O(18),O(18),O(18),
O(18),O(18),O(18),O(18),O(18),O(18),O(18),O(18),
/* rates 00-11 */
/*
O( 0),O( 1),O( 2),O( 3),
O( 0),O( 1),O( 2),O( 3),
*/
O(18),O(18),O( 0),O( 0),
O( 0),O( 0),O( 2),O( 2), // Nemesis's tests
O( 0),O( 1),O( 2),O( 3),
O( 0),O( 1),O( 2),O( 3),
O( 0),O( 1),O( 2),O( 3),
O( 0),O( 1),O( 2),O( 3),
O( 0),O( 1),O( 2),O( 3),
O( 0),O( 1),O( 2),O( 3),
O( 0),O( 1),O( 2),O( 3),
O( 0),O( 1),O( 2),O( 3),
O( 0),O( 1),O( 2),O( 3),
O( 0),O( 1),O( 2),O( 3),
/* rate 12 */
O( 4),O( 5),O( 6),O( 7),
/* rate 13 */
O( 8),O( 9),O(10),O(11),
/* rate 14 */
O(12),O(13),O(14),O(15),
/* rate 15 */
O(16),O(16),O(16),O(16),
/* 32 dummy rates (same as 15 3) */
O(16),O(16),O(16),O(16),O(16),O(16),O(16),O(16),
O(16),O(16),O(16),O(16),O(16),O(16),O(16),O(16),
O(16),O(16),O(16),O(16),O(16),O(16),O(16),O(16),
O(16),O(16),O(16),O(16),O(16),O(16),O(16),O(16)
];
})();
/*rate 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15*/
/*shift 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 0, 0, 0, 0 */
/*mask 2047, 1023, 511, 255, 127, 63, 31, 15, 7, 3, 1, 0, 0, 0, 0, 0 */
_EG.rate_shift = (function(){
var O = function(a){return (a)|0;};
return [ // env gen counter shifts - 32+64 rates+32 RKS
/* 32 infinite time rates */
/* O(0),O(0),O(0),O(0),O(0),O(0),O(0),O(0),
O(0),O(0),O(0),O(0),O(0),O(0),O(0),O(0),
O(0),O(0),O(0),O(0),O(0),O(0),O(0),O(0),
O(0),O(0),O(0),O(0),O(0),O(0),O(0),O(0), */
/* fixed (should be the same as rate 0, even if it makes no difference since increment value is 0 for these rates) */
O(11),O(11),O(11),O(11),O(11),O(11),O(11),O(11),
O(11),O(11),O(11),O(11),O(11),O(11),O(11),O(11),
O(11),O(11),O(11),O(11),O(11),O(11),O(11),O(11),
O(11),O(11),O(11),O(11),O(11),O(11),O(11),O(11),
/* rates 00-11 */
O(11),O(11),O(11),O(11),
O(10),O(10),O(10),O(10),
O( 9),O( 9),O( 9),O( 9),
O( 8),O( 8),O( 8),O( 8),
O( 7),O( 7),O( 7),O( 7),
O( 6),O( 6),O( 6),O( 6),
O( 5),O( 5),O( 5),O( 5),
O( 4),O( 4),O( 4),O( 4),
O( 3),O( 3),O( 3),O( 3),
O( 2),O( 2),O( 2),O( 2),
O( 1),O( 1),O( 1),O( 1),
O( 0),O( 0),O( 0),O( 0),
/* rate 12 */
O( 0),O( 0),O( 0),O( 0),
/* rate 13 */
O( 0),O( 0),O( 0),O( 0),
/* rate 14 */
O( 0),O( 0),O( 0),O( 0),
/* rate 15 */
O( 0),O( 0),O( 0),O( 0),
/* 32 dummy rates (same as 15 3) */
O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),
O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),
O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),
O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0),O( 0)
];
})();
_DT.tab = [ // 4*32
/* this is YM2151 and YM2612 phase increment data (in 10.10 fixed point format)*/
/* FD=0 */
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* FD=1 */
0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2,
2, 3, 3, 3, 4, 4, 4, 5, 5, 6, 6, 7, 8, 8, 8, 8,
/* FD=2 */
1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5,
5, 6, 6, 7, 8, 8, 9,10,11,12,13,14,16,16,16,16,
/* FD=3 */
2, 2, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 6, 6, 7,
8 , 8, 9,10,11,12,13,14,16,17,19,20,22,22,22,22
];
/* OPN key frequency number -> key code follow table */
/* fnum higher 4bit -> keycode lower 2bit */
var OPN = {
"fktable":[0,0,0,0,0,0,0,1,2,3,3,3,3,3,3,3],
};
var LFO = {
/* 8 LFO speed parameters */
/* each value represents number of samples that one LFO level will last for */
"samples_per_step":[108, 77, 71, 67, 62, 44, 8, 5],
/*There are 4 different LFO AM depths available, they are:
0 dB, 1.4 dB, 5.9 dB, 11.8 dB
Here is how it is generated (in EG steps):
11.8 dB = 0, 2, 4, 6, 8, 10,12,14,16...126,126,124,122,120,118,....4,2,0
5.9 dB = 0, 1, 2, 3, 4, 5, 6, 7, 8....63, 63, 62, 61, 60, 59,.....2,1,0
1.4 dB = 0, 0, 0, 0, 1, 1, 1, 1, 2,...15, 15, 15, 15, 14, 14,.....0,0,0
(1.4 dB is loosing precision as you can see)
It's implemented as generator from 0..126 with step 2 then a shift
right N times, where N is:
8 for 0 dB
3 for 1.4 dB
1 for 5.9 dB
0 for 11.8 dB
*/
"ams_depth_shift":[8,3,1,0],
/*There are 8 different LFO PM depths available, they are:
0, 3.4, 6.7, 10, 14, 20, 40, 80 (cents)
Modulation level at each depth depends on F-NUMBER bits: 4,5,6,7,8,9,10
(bits 8,9,10 = FNUM MSB from OCT/FNUM register)
Here we store only first quarter (positive one) of full waveform.
Full table (lfo_pm_table) containing all 128 waveforms is build
at run (init) time.
One value in table below represents 4 (four) basic LFO steps
(1 PM step = 4 AM steps).
For example:
at LFO SPEED=0 (which is 108 samples per basic LFO step)
one value from "lfo_pm_output" table lasts for 432 consecutive
samples (4*108=432) and one full LFO waveform cycle lasts for 13824
samples (32*432=13824; 32 because we store only a quarter of whole
waveform in the table below)
*/
"pm_output":[ // [7*8][8]
/* 7 bits meaningful (of F-NUMBER), 8 LFO output levels per one depth (out of 32), 8 LFO depths */
/* FNUM BIT 4: 000 0001xxxx */
/* DEPTH 0 */ [0, 0, 0, 0, 0, 0, 0, 0],
/* DEPTH 1 */ [0, 0, 0, 0, 0, 0, 0, 0],
/* DEPTH 2 */ [0, 0, 0, 0, 0, 0, 0, 0],
/* DEPTH 3 */ [0, 0, 0, 0, 0, 0, 0, 0],
/* DEPTH 4 */ [0, 0, 0, 0, 0, 0, 0, 0],
/* DEPTH 5 */ [0, 0, 0, 0, 0, 0, 0, 0],
/* DEPTH 6 */ [0, 0, 0, 0, 0, 0, 0, 0],
/* DEPTH 7 */ [0, 0, 0, 0, 1, 1, 1, 1],
/* FNUM BIT 5: 000 0010xxxx */
/* DEPTH 0 */ [0, 0, 0, 0, 0, 0, 0, 0],
/* DEPTH 1 */ [0, 0, 0, 0, 0, 0, 0, 0],
/* DEPTH 2 */ [0, 0, 0, 0, 0, 0, 0, 0],
/* DEPTH 3 */ [0, 0, 0, 0, 0, 0, 0, 0],
/* DEPTH 4 */ [0, 0, 0, 0, 0, 0, 0, 0],
/* DEPTH 5 */ [0, 0, 0, 0, 0, 0, 0, 0],
/* DEPTH 6 */ [0, 0, 0, 0, 1, 1, 1, 1],
/* DEPTH 7 */ [0, 0, 1, 1, 2, 2, 2, 3],
/* FNUM BIT 6: 000 0100xxxx */
/* DEPTH 0 */ [0, 0, 0, 0, 0, 0, 0, 0],
/* DEPTH 1 */ [0, 0, 0, 0, 0, 0, 0, 0],
/* DEPTH 2 */ [0, 0, 0, 0, 0, 0, 0, 0],
/* DEPTH 3 */ [0, 0, 0, 0, 0, 0, 0, 0],
/* DEPTH 4 */ [0, 0, 0, 0, 0, 0, 0, 1],
/* DEPTH 5 */ [0, 0, 0, 0, 1, 1, 1, 1],
/* DEPTH 6 */ [0, 0, 1, 1, 2, 2, 2, 3],
/* DEPTH 7 */ [0, 0, 2, 3, 4, 4, 5, 6],
/* FNUM BIT 7: 000 1000xxxx */
/* DEPTH 0 */ [0, 0, 0, 0, 0, 0, 0, 0],
/* DEPTH 1 */ [0, 0, 0, 0, 0, 0, 0, 0],
/* DEPTH 2 */ [0, 0, 0, 0, 0, 0, 1, 1],
/* DEPTH 3 */ [0, 0, 0, 0, 1, 1, 1, 1],
/* DEPTH 4 */ [0, 0, 0, 1, 1, 1, 1, 2],
/* DEPTH 5 */ [0, 0, 1, 1, 2, 2, 2, 3],
/* DEPTH 6 */ [0, 0, 2, 3, 4, 4, 5, 6],
/* DEPTH 7 */ [0, 0, 4, 6, 8, 8, 0xa, 0xc],
/* FNUM BIT 8: 001 0000xxxx */
/* DEPTH 0 */ [0, 0, 0, 0, 0, 0, 0, 0],
/* DEPTH 1 */ [0, 0, 0, 0, 1, 1, 1, 1],
/* DEPTH 2 */ [0, 0, 0, 1, 1, 1, 2, 2],
/* DEPTH 3 */ [0, 0, 1, 1, 2, 2, 3, 3],
/* DEPTH 4 */ [0, 0, 1, 2, 2, 2, 3, 4],
/* DEPTH 5 */ [0, 0, 2, 3, 4, 4, 5, 6],
/* DEPTH 6 */ [0, 0, 4, 6, 8, 8, 0xa, 0xc],
/* DEPTH 7 */ [0, 0, 8, 0xc,0x10,0x10,0x14,0x18],
/* FNUM BIT 9: 010 0000xxxx */
/* DEPTH 0 */ [0, 0, 0, 0, 0, 0, 0, 0],
/* DEPTH 1 */ [0, 0, 0, 0, 2, 2, 2, 2],
/* DEPTH 2 */ [0, 0, 0, 2, 2, 2, 4, 4],
/* DEPTH 3 */ [0, 0, 2, 2, 4, 4, 6, 6],
/* DEPTH 4 */ [0, 0, 2, 4, 4, 4, 6, 8],
/* DEPTH 5 */ [0, 0, 4, 6, 8, 8, 0xa, 0xc],
/* DEPTH 6 */ [0, 0, 8, 0xc,0x10,0x10,0x14,0x18],
/* DEPTH 7 */ [0, 0,0x10,0x18,0x20,0x20,0x28,0x30],
/* FNUM BIT10: 100 0000xxxx */
/* DEPTH 0 */ [0, 0, 0, 0, 0, 0, 0, 0],
/* DEPTH 1 */ [0, 0, 0, 0, 4, 4, 4, 4],
/* DEPTH 2 */ [0, 0, 0, 4, 4, 4, 8, 8],
/* DEPTH 3 */ [0, 0, 4, 4, 8, 8, 0xc, 0xc],
/* DEPTH 4 */ [0, 0, 4, 8, 8, 8, 0xc,0x10],
/* DEPTH 5 */ [0, 0, 8, 0xc,0x10,0x10,0x14,0x18],
/* DEPTH 6 */ [0, 0,0x10,0x18,0x20,0x20,0x28,0x30],
/* DEPTH 7 */ [0, 0,0x20,0x30,0x40,0x40,0x50,0x60]
],
/* all 128 LFO PM waveforms */
/* 128 combinations of 7 bits meaningful (of F-NUMBER), 8 LFO depths, 32 LFO output levels per one depth */
"pm_table":new Array(128*8*32)
};
OPN.CHAN = function(N){return N&0x3;};
OPN.SLOT = function(N){return (N>>2)&0x3;};
/* slot number */
var _SLOT = [0,2,1,3];
/**** END GLOBALS ****/
/**** FM STRUCTS based on genplus-gx ****/
function FM_SLOT() {
this.DT = -1; // index into ym2612.OPN.ST.dt_tab, formerly INT32* detune: dt_tab[DT]
this.KSR = 0; // UINT8 key scale rate: 3-KSR
function _rate() {
this.ar = 0; // UINT32 attack rate
this.d1r = 0; // UINT32 decay rate
this.d2r = 0; // UINT32 sustain rate
this.rr = 0; // UINT32 release rate
this.ksr = 0; // UINT8 key scale rate: kcode>>(3-KSR)
this.mul = 1; // UINT32 multiple: ML_TABLE[ML]
this.init = function() {
this.ar = 0;
this.d1r = 0;
this.d2r = 0;
this.rr = 0;
this.ksr = 0;
this.mul = 1;
};
this.toString = function(){return ["MUL="+this.mul,"KS="+this.ksr,"AR="+this.ar,"D1R="+this.d1r,"D2R="+this.d2r,"RR="+this.rr].join(',');};
}
this.rate = new _rate;
// phase generator
this.phase = 0; // UINT32 phase counter
this.Incr = 0; // INT32 phase step
// envelope generator
this.state = 0; // UINT8 phase type
this.tl = 0; // UINT32 total level: TL<<3
this.volume = 0; // UINT32 envelope counter
this.sl = 0; // UINT32 sustain level: sl_table[SL]
this.vol_out = 0; // UINT32 current output from EG (without AM from LFO)
function _eg() {
this.ar=0; // UINT8
this.d1r=0; // UINT8
this.d2r=0; // UINT8
this.rr=0; // UINT8
this.init = function(){
this.ar=0; // UINT8
this.d1r=0; // UINT8
this.d2r=0; // UINT8
this.rr=0; // UINT8
};
}
this.eg = {
sh:new _eg, // state
sel:new _eg,
init:function(){this.sh.init();this.sel.init();}
};
this.ssg = 0; // UINT8 ssg-eg waveform
this.ssgn = 0; // UINT8 ssg-eg negated output
this.key = 0; // UINT8 0 = last key was KEY_OFF, 1 = KEY_ON
// lfo
this.AMmask = 0; // UINT32 AM enable flag
this.reset = function() {
this.Incr = -1;
this.key = 0;
this.phase = 0;
this.ssgn = 0;
this.state = _EG.OFF;
this.volume = _ENV.MAX_ATT_INDEX;
this.vol_out = _ENV.MAX_ATT_INDEX;
this.out[0] = 0, this.out[1] = 0;
};
this.debug = {
"dt1mul":0, "dt1":0, "mul":0,
"tl":0,
"ksar":0, "ks":0, "ar":0,
"amd1r":0, "am":0, "d1r":0,
"d2r":0,
"slrr":0, "sl":0, "rr":0
};
this.debug.toString = function(){return [
"TL:"+(this.tl),
"DT1:"+this.dt1,
"MUL:"+this.mul,
"KS:"+this.ks,"AR:"+this.ar,
"AM:"+this.am,"D1R:"+this.d1r,
"D2R:"+this.d2r,
"SL:"+(this.sl),"RR:"+this.rr
].join(',');};
this.out = [0,0]; // replace FM_CH.op1_out +neo
this.toString = function(){return "OP{"+this.debug.toString()+"}";};
}
function FM_CH() {
this.SLOT = [ // four slots/ops
new FM_SLOT(),
new FM_SLOT(),
new FM_SLOT(),
new FM_SLOT()
];
this.ALGO = 0; // UINT8 algorithm
this.FB = 0; // UINT8 feedback shift
this.op1_out = [0,0]; // INT32 op1 output for feedback (stereo)
this.connect = ['x','x','x','x']; // SLOT output pointers, formerly INT32*[4]
this.mem = {
connect:'mem', // INT32* where to put the delayed sample (MEM)
value:0 // INT32 delated sample (MEM) value
};
this.pms = 0; // INT32 channel PMS
this.ams = 0; // UINT8 channel AMS
this.fc = 0; // UINT32 fnum,blk adjusted to sample rate
this.kcode = 0; // UINT8 key code
this.block_fnum = 0; // UINT32 current blk/fnum value for this slot
this.fn_h = 0; // replaces FM_ST.fn_h
this.outputs = {
"m1":0,
"m2":0,
"c1":0,
"c2":0,
"mem":0,
"x":0,
"out":0 // replaces out_fm[ch]
};
this.canCSM = 0; // replaces hardcoded check against CH3
this.canDAC = 0; // replaces hardcoded check against CH6
this.muted = 0;
this.pan = [0,0]; // replaces FM_OPN.pan
this.reset = function() {
this.mem.value = 0, this.op1_out[0] = 0, this.op1_out[1] = 0;
var s = this.SLOT.length; while (--s>-1) this.SLOT[s].reset();
};
this.toString = function(){return "CH{"+[
"ALGO:"+this.ALGO,"FB:"+this.FB,
"PMS:"+this.pms,"AMS:"+this.ams,
"SLOTS["+this.SLOT.join(',')+"]"
].join(',')+"}";};
}
function FM_ST(c, r) {
this.address = 0; // UINT16 address register
this.status = 0; // UINT8 status flag
this.mode = 0; // UINT32 CSM/3SLOT mode
//this.fn_h = 0; // UINT8 freq latch
this.timer_base = 1;
this.TA = 0; // INT32 timer a value
this.TAL = 0; // INT32 timer a base
this.TAC = 0; // INT32 timer a counter
this.TB = 0; // INT32 timer b value
this.TBL = 0; // INT32 timer b base
this.TBC = 0; // INT32 timer b counter
this.dt_tab = [ // INT32[8][32] detune table
new Array(32),
new Array(32),
new Array(32),
new Array(32),
new Array(32),
new Array(32),
new Array(32),
new Array(32)
];
this.clock = c||7670448;
this.rate = r||44100;
}
function FM_3SLOT() {
this.fc = [0,0,0]; // UINT32[3] fnum3,blk3 calculated
this.fn_h = 0; // UINT8 freq3 latch
this.kcode = [0,0,0]; // UINT8[3] key code
this.block_fnum = [0,0,0]; // UINT32[3] current fnum value for this slot
this.key_csm = 0; // UINT8 CSM mode KEY_ON flag
}
function FM_OPN(c, r) {
this.ST = new FM_ST(c,r);
this.SL3 = new FM_3SLOT;
//this.pan = new Array(6*2); // UINT[6*2] fm channels output masks (0xffffffff = enable)
function _timer() {
this.cnt = 0;
this.timer = 0;
this.timer_add = 0; // vb
this.timer_overflow = 0; // vb unused for eg
this.init = function() {
this.cnt = 0; // current phase counter (UINT32 for eg, UINT8 for lfo)
this.timer = 0; // UINT32
this.timer_add = 0; // UINT32 step of timer
this.timer_overflow = 0; // UINT32 timer overflows every N samples
};
}
this.eg = new _timer();
this.lfo = new _timer();
this.lfo.AM = 0; // UINT32 current lfo AM step
this.lfo.PM = 0; // UINT32 current lfo PM step
this.fn = {"table":new Array(4096), "max":0};
}
function YMX(c,r) {
this.CH = [new FM_CH, new FM_CH, new FM_CH, new FM_CH, new FM_CH, new FM_CH];
this.CH[2].canCSM = 1;
this.CH[5].canDAC = 1;
this.dacen = 0; // UINT8
this.dacout = 0; // INT32
this.OPN = new FM_OPN(c,r);
this.toString = function(){return "YM[\n"+this.CH.join(',\n')+"\n]";};
}
/**** END FM STRUCTS ****/
/**** FM DEFS based on genplus-gx ****/
/* current chip state */
//_YM.m2 = 0; _YM.c1 = 0; _YM.c2 = 0; // INT32 phase modulation input for ops 2,3,4
//_YM.mem = 0; // INT32 one sample delay memory
//_YM.out_fm = [0,0,0,0,0,0,0,0]; // INT32[8] outputs of working channels // REPLACED BY FM_CH.out
_YM.bitmask = 0; //UINT32 working channels output bitmasking (DAC quantization)
FM_SLOT.prototype.keyOn = function(x,csm) {
if (!this.key&&!x.OPN.SL3.key_csm) {
this.phase = 0; /* restart Phase Generator */
this.ssgn = 0; /* reset SSG-EG inversion flag */
if ((this.rate.ar+this.rate.ksr)<94) /*32+62*/
this.state = (this.volume<=_ENV.MIN_ATT_INDEX)?(this.sl===_ENV.MIN_ATT_INDEX?_EG.SUS:_EG.DEC):_EG.ATT;
else {
this.volume = _ENV.MIN_ATT_INDEX; /* force attenuation level to 0 */
this.state = (this.sl===_ENV.MIN_ATT_INDEX)?_EG.SUS:_EG.DEC;
}
/* recalculate EG output */
if ((this.ssg&0x08)>0&&(this.ssgn^(this.ssg&0x04))>0) this.vol_out = this.tl+((0x200-this.volume)&_ENV.MAX_ATT_INDEX);
else this.vol_out = this.tl+(this.volume|0);
}
if (!csm) this.key = 1;
};
FM_CH.prototype.keyOn = function(x,s) {this.SLOT[s].keyOn(x,0);};
FM_SLOT.prototype.keyOff = function(x,csm) {
if ((csm&&!this.key)||(!csm&&this.key&&!x.OPN.SL3.key_csm)) {
if (this.state>_EG.REL) {
this.state = _EG.REL; /* phase -> Release */
/* SSG-EG specific update */
if ((this.ssg&0x08)>0) {
/* convert EG attenuation level */
if ((this.ssgn^(this.ssg&0x04))>0) this.volume = (0x200-this.volume)|0;
/* force EG attenuation level */
if (this.volume>=0x200) {
this.volume = _ENV.MAX_ATT_INDEX;
this.state = _EG.OFF;
}
this.vol_out = this.tl+(this.volume|0); /* recalculate EG output */
}
}
}
if (!csm) this.key = 0;
};
FM_CH.prototype.keyOff = function(x,s) {this.SLOT[s].keyOff(x,0);};
FM_CH.prototype.keyOnCSM = function(x,s) {this.SLOT[s].keyOn(x,1);};
FM_CH.prototype.keyOffCSM = function(x,s) {this.SLOT[s].keyOff(x,1);};
FM_CH.prototype.keyControlCSM = function(x) {
this.keyOnCSM(_SLOT[0]);
this.keyOnCSM(_SLOT[1]);
this.keyOnCSM(_SLOT[2]);
this.keyOnCSM(_SLOT[3]);
x.OPN.SL3.key_csm = 1;
};
function INTERNAL_TIMER_A(x) {
if ((x.OPN.ST.mode&0x01)>0) {
if (cfg.mode) x.OPN.ST.TAC -= x.OPN.ST.timer_base; // vb
else --x.OPN.ST.TAC; // gpgx
if (x.OPN.ST.TAC<=0) {
/* set status (if enabled) */
if ((x.OPN.ST.mode&0x04)>0) x.OPN.ST.status |= 0x01;
/* reload the counter */
if (cfg.mode&&x.OPN.ST.TAL) x.OPN.ST.TAC += x.OPN.ST.TAL; // vb
else x.OPN.ST.TAC = x.OPN.ST.TAL; // gpgx
/* CSM mode auto key on */
if ((x.OPN.ST.mode & 0xC0)===0x80) x.CH[2].keyControlCSM();
}
}
}
function INTERNAL_TIMER_B(x, step) {
if ((x.OPN.ST.mode & 0x02)>0) {
if (cfg.mode) x.OPN.ST.TBC -= x.OPN.ST.timer_base*step; // vb
else x.OPN.ST.TBC -= step; // gpgx
if (x.OPN.ST.TBC <= 0) {
/* set status (if enabled) */
if ((x.OPN.ST.mode & 0x08)>0) x.OPN.ST.status |= 0x02;
/* reload the counter */
if (x.OPN.ST.TBL) x.OPN.ST.TBC += x.OPN.ST.TBL;
else x.OPN.ST.TBC = x.OPN.ST.TBL;
}
}
}
/* OPN Mode Register Write */
function set_timers(x,v) {
/* b7 = CSM MODE */
/* b6 = 3 slot mode */
/* b5 = reset b */
/* b4 = reset a */
/* b3 = timer enable b */
/* b2 = timer enable a */
/* b1 = load b */
/* b0 = load a */
if (((x.OPN.ST.mode^v)&0xc0)>0) {
x.CH[2].SLOT[_SLOT[0]].Incr = -1; // phase increment need to be recalculated
// csm mode disabled and csm keyon active
if (((v&0xc0)!==0x80)&&x.OPN.SL3.key_csm) {
// csm mode keyoff
x.CH[2].keyOffCSM(_SLOT[0]);
x.CH[2].keyOffCSM(_SLOT[1]);
x.CH[2].keyOffCSM(_SLOT[2]);
x.CH[2].keyOffCSM(_SLOT[3]);
x.OPN.SL3.key_csm = 0;
}
}
// reload timers
if ((v&1)&&!(x.OPN.ST.mode&1)) x.OPN.ST.TAC = x.OPN.ST.TAL;
if ((v&2)&&!(x.OPN.ST.mode&2)) x.OPN.ST.TBC = x.OPN.ST.TBL;
// reset timers flags
x.OPN.ST.status &= ~v>>4;
x.OPN.ST.mode = v;
}
/* set algorithm connection */
FM_CH.prototype.setupConnection = function() {
var carrier = 'out';
var o = {m1:0, m2:2, c1:1, c2:3}, mem = this.mem.connect;
switch (this.ALGO) {
case 0:
/* M1---C1---MEM---M2---C2---OUT */
this.connect[o.m1] = 'c1';
this.connect[o.c1] = 'mem';
this.connect[o.m2] = 'c2';
//if (mem!=='mem') this.connect[o[mem]] = 'm2';
this.mem.connect = 'm2';
break;
case 1:
/* M1------+-MEM---M2---C2---OUT */
/* C1-+ */
this.connect[o.m1] = 'mem';
this.connect[o.c1] = 'mem';
this.connect[o.m2] = 'c2';
//if (mem!=='mem') this.connect[o[mem]] = 'm2';
this.mem.connect = 'm2';
break;
case 2:
/* M1-----------------+-C2---OUT */
/* C1---MEM---M2-+ */
this.connect[o.m1] = 'c2';
this.connect[o.c1] = 'mem';
this.connect[o.m2] = 'c2';
//if (mem!=='mem') this.connect[o[mem]] = 'm2';
this.mem.connect = 'm2';
break;
case 3:
/* M1---C1---MEM------+-C2---OUT */
/* M2-+ */
this.connect[o.m1] = 'c1';
this.connect[o.c1] = 'mem';
this.connect[o.m2] = 'c2';
//if (mem!=='mem') this.connect[o[mem]] = 'c2';
this.mem.connect = 'c2';
break;
case 4:
/* M1---C1-+-OUT */
/* M2---C2-+ */
/* MEM: not used */
this.connect[o.m1] = 'c1';
this.connect[o.c1] = carrier;
this.connect[o.m2] = 'c2';
//if (mem!=='mem') this.connect[o[mem]] = 'mem';
this.mem.connect = 'mem';
break;
case 5:
/* +----C1----+ */
/* M1-+-MEM---M2-+-OUT */
/* +----C2----+ */
this.connect[o.m1] = 'x';
this.connect[o.c1] = carrier;
this.connect[o.m2] = carrier;
//if (mem!=='mem') this.connect[o[mem]] = 'm2';
this.mem.connect = 'm2';
break;
case 6:
/* M1---C1-+ */
/* M2-+-OUT */
/* C2-+ */
/* MEM: not used */
this.connect[o.m1] = 'c1';
this.connect[o.c1] = carrier;
this.connect[o.m2] = carrier;
//if (mem!=='mem') this.connect[o[mem]] = 'mem';
this.mem.connect = 'mem';
break;
case 7:
/* M1-+ */
/* C1-+-OUT */
/* M2-+ */
/* C2-+ */
/* MEM: not used*/
this.connect[o.m1] = carrier;
this.connect[o.c1] = carrier;
this.connect[o.m2] = carrier;
//if (mem!=='mem') this.connect[o[mem]] = 'mem';
this.mem.connect = 'mem';
break;
default:
if (cfg.strict) throw new Error("CH::setup_connection - unsupported algorithm ("+this.ALGO+")");
else break;
}
this.connect[3] = carrier;
};
/* set detune & multiple */
FM_SLOT.prototype.set_det_mul = function(x,v) {
this.rate.mul = ((v&0x0f)>0)?((v&0x0f)<<1):1;
this.DT = (v>>4)&7;//x.OPN.ST.dt_tab[(v>>4)&7];
this.debug.dt1mul = v&0xff;
this.debug.dt1 = this.DT;
this.debug.mul = v&0x0f;
};
FM_CH.prototype.set_det_mul = function(x,s,v) {
this.SLOT[s].set_det_mul(x,v);
this.SLOT[_SLOT[0]].Incr = -1;
};
/* set total level */
FM_SLOT.prototype.set_tl = function(v) {
this.debug.tl = (v&0x7f);
this.tl = (this.debug.tl)<<(_ENV.BITS-7); // 7-bit tl
// recalculate eg output
if ((this.ssg&0x08)>0&&((this.ssgn^(this.ssg&0x04))>0)&&this.state>_EG.REL)
this.vol_out = this.tl+(((0x200-this.volume)|0)&_ENV.MAX_ATT_INDEX);
else
this.vol_out = this.tl+((this.volume)|0);
};
FM_CH.prototype.set_tl = function(s,v) {this.SLOT[s].set_tl(v);};
/* set attack rate & key scale */
FM_SLOT.prototype.set_ar_ksr = function(v) {
this.debug.ksar = v&0xff;
this.debug.ks = v>>6;
this.debug.ar = v&0x1f;
var old_ksr = this.KSR|0;
this.rate.ar = ((this.debug.ar)>0)?32+((this.debug.ar)<<1):0;
this.KSR = 3-(this.debug.ks);
/* Even if it seems unnecessary to do it here, it could happen that KSR and KC */
/* are modified but the resulted SLOT->ksr value (kc >> SLOT->KSR) remains unchanged. */
/* In such case, Attack Rate would not be recalculated by "refresh_fc_eg_slot". */
/* This fixes the intro of "The Adventures of Batman & Robin" (Eke-Eke) */
if ((this.rate.ar+this.rate.ksr)<94) { /*32+62*/
var q = (this.rate.ar+this.rate.ksr)|0;
this.eg.sh.ar = _EG.rate_shift[q];
this.eg.sel.ar = _EG.rate_select[q];
}
else { /* verified by Nemesis on real hardware (Attack phase is blocked) */
this.eg.sh.ar = 0;
this.eg.sel.ar = 18*_EG.RATE_STEPS;
}
return this.KSR!==old_ksr;
};
FM_CH.prototype.set_ar_ksr = function(s,v) {if (this.SLOT[s].set_ar_ksr(v)) this.SLOT[_SLOT[0]].Incr = -1;};
/* set decay rate */
FM_SLOT.prototype.set_dr = function(v) {
this.debug.amd1r = v&0xff;
this.debug.am = v&0x80;
this.debug.d1r = v&0x1f;
this.rate.d1r = ((this.debug.d1r)>0)?32+((this.debug.d1r)<<1):0;
var q = (this.rate.d1r+this.rate.ksr)|0;
this.eg.sh.d1r = _EG.rate_shift[q];
this.eg.sel.d1r = _EG.rate_select[q];
};
FM_CH.prototype.set_dr = function(s,v) {this.SLOT[s].set_dr(v);};
/* set sustain rate */
FM_SLOT.prototype.set_sr = function(v) {
this.debug.d2r = v&0x1f;
this.rate.d2r = ((this.debug.d2r)>0)?32+((this.debug.d2r)<<1):0;
var q = (this.rate.d2r+this.rate.ksr)|0;
this.eg.sh.d2r = _EG.rate_shift[q];
this.eg.sel.d2r = _EG.rate_select[q];
};
FM_CH.prototype.set_sr = function(s,v) {this.SLOT[s].set_sr(v);};
/* set release rate */
FM_SLOT.prototype.set_sl_rr = function(v) {
this.debug.slrr = v&0xff;
this.debug.sl = (v>>4)&0x0f;
this.debug.rr = v&0x0f;
this.sl = _YM.sl[this.debug.sl];
// check eg state changes
if (this.state===_EG.DEC&&this.volume>=(this.sl|0)) this.state = _EG.SUS;
this.rate.rr = 34+((this.debug.rr)<<2);
var q = (this.rate.rr+this.rate.ksr)|0;
this.eg.sh.rr = _EG.rate_shift[q];
this.eg.sel.rr = _EG.rate_select[q];
};
FM_CH.prototype.set_sl_rr = function(s,v) {this.SLOT[s].set_sl_rr(v);};
/* advance LFO to next sample */
function advance_lfo(x) {
var _upd;
if (cfg.mode) _upd = function(o) { // vb
while (o.lfo.timer>=o.lfo.timer_overflow) {
o.lfo.timer -= o.lfo.timer_overflow;
o.lfo.cnt = (o.lfo.cnt+1)&127; /* There are 128 LFO steps */
/* triangle (inverted) */
/* AM: from 126 to 0 step -2, 0 to 126 step +2 */
if (o.lfo.cnt<64) o.lfo.AM = (o.lfo.cnt^63)<<1;
else o.lfo.AM = (o.lfo.cnt&63)<<1;
o.lfo.PM = o.lfo.cnt>>2; /* PM works with 4 times slower clock */
}
};
else _upd = function(o) { // gpgx
if (o.lfo.timer>o.lfo.timer_overflow) {
o.lfo.timer = 0;
o.lfo.cnt = (o.lfo.cnt+1)&127; /* There are 128 LFO steps */
/* triangle (inverted) */
/* AM: from 126 to 0 step -2, 0 to 126 step +2 */
if (o.lfo.cnt<64) o.lfo.AM = (o.lfo.cnt^63)<<1;
else o.lfo.AM = (o.lfo.cnt&63)<<1;
o.lfo.PM = o.lfo.cnt>>2; /* PM works with 4 times slower clock */
}
};
if (x.OPN.lfo.timer_overflow) { /* LFO enabled ? */
/* increment LFO timer (every samples) */
if (cfg.mode) x.OPN.lfo.timer += x.OPN.lfo.timer_add; // vb
else ++x.OPN.lfo.timer; // gpgx
/* when LFO is enabled, one level will last for 108, 77, 71, 67, 62, 44, 8 or 5 samples */
_upd(x.OPN);
}
}
FM_SLOT.prototype.advance_eg = function(eg_cnt) {
switch (this.state) {
case _EG.ATT: /* attack phase */
if (!(eg_cnt&((1<<this.eg.sh.ar)-1))) {
this.volume += (~this.volume*(_EG.inc[this.eg.sel.ar+((eg_cnt>>this.eg.sh.ar)&7)]))>>4; /* update attenuation level */
/* check phase transition*/
if (this.volume<=_ENV.MIN_ATT_INDEX) {
this.volume = _ENV.MIN_ATT_INDEX;
this.state = (this.sl===_ENV.MIN_ATT_INDEX)?_EG.SUS:_EG.DEC; /* special case where SL=0 */
}
/* recalculate EG output */
if ((this.ssg&0x08)>0&&(this.ssgn^(this.ssg&0x04))>0) this.vol_out = this.tl+(((0x200-this.volume)|0)&_ENV.MAX_ATT_INDEX); /* SSG-EG Output Inversion */
else this.vol_out = this.tl+(this.volume|0);
}
break;
case _EG.DEC: /* decay phase */
if (!(eg_cnt&((1<<this.eg.sh.d1r)-1))) {
if ((this.ssg&0x08)>0) { /* SSG EG type */
/* update attenuation level */
if (this.volume<0x200) {
this.volume += _EG.inc[this.eg.sel.d1r+((eg_cnt>>this.eg.sh.d1r)&7)]<<2;
/* recalculate EG output */
if ((this.ssgn^(this.ssg&0x04))>0) this.vol_out = this.tl+(((0x200-this.volume)|0)&_ENV.MAX_ATT_INDEX); /* SSG-EG Output Inversion */
else this.vol_out = this.tl+(this.volume|0);
}
}
else {
this.volume += (_EG.inc[this.eg.sel.d1r+((eg_cnt>>this.eg.sh.d1r)&7)]);
this.vol_out = this.tl+(this.volume|0); /* recalculate EG output */
}
/* check phase transition*/
if (this.volume>=(this.sl|0)) this.state = _EG.SUS;
}
break;
case _EG.SUS: /* sustain phase */
if (!(eg_cnt&((1<<this.eg.sh.d2r)-1))) {
/* SSG EG type */
if ((this.ssg&0x08)>0) {
/* update attenuation level */
if (this.volume<0x200) {
this.volume += _EG.inc[this.eg.sel.d2r+((eg_cnt>>this.eg.sh.d2r)&7)]<<2;
/* recalculate EG output */
if ((this.ssgn^(this.ssg&0x04))>0) this.vol_out = this.tl+(((0x200-this.volume)|0)&_ENV.MAX_ATT_INDEX); /* SSG-EG Output Inversion */
else this.vol_out = this.tl+(this.volume|0);
}
}
else {
/* update attenuation level */
this.volume += (_EG.inc[this.eg.sel.d2r+((eg_cnt>>this.eg.sh.d2r)&7)]);
/* check phase transition*/
if (this.volume>=_ENV.MAX_ATT_INDEX) this.volume = _ENV.MAX_ATT_INDEX; /* do not change SLOT->state (verified on real chip) */
this.vol_out = this.tl+(this.volume|0); /* recalculate EG output */
}
}
break;
case _EG.REL: /* release phase */
if (!(eg_cnt&((1<<this.eg.sh.rr)-1))) {
/* SSG EG type */
if ((this.ssg&0x08)>0) {
/* update attenuation level */
if (this.volume<0x200) {
this.volume += _EG.inc[this.eg.sel.rr+((eg_cnt>>this.eg.sh.rr)&7)]<<2;
/* check phase transition*/
if (this.volume>=0x200) {
this.volume = _ENV.MAX_ATT_INDEX;
this.state = _EG.OFF;
}
}
}
else {
/* update attenuation level */
this.volume += (_EG.inc[this.eg.sel.rr+((eg_cnt>>this.eg.sh.rr)&7)]);
/* check phase transition*/
if (this.volume>=_ENV.MAX_ATT_INDEX) {
this.volume = _ENV.MAX_ATT_INDEX;
this.state = _EG.OFF;
}
}
this.vol_out = this.tl+(this.volume|0); /* recalculate EG output */
}
break;
default:
if (cfg.strict) throw new Error("FM_SLOT::advance_eg - unsupported state ("+this.state+")");
else break;
}
};
FM_CH.prototype.advance_eg = function(eg_cnt) {var j = this.SLOT.length; while (--j>-1) this.SLOT[j].advance_eg(eg_cnt);};
function advance_eg_channels(x, eg_cnt) {var i = x.CH.length; while (--i>-1) x.CH[i].advance_eg(eg_cnt);}
/* SSG-EG update process */
/* The behavior is based upon Nemesis tests on real hardware */
/* This is actually executed before each samples */
FM_SLOT.prototype.update_ssg_eg = function() {
/* detect SSG-EG transition */
/* this is not required during release phase as the attenuation has been forced to MAX and output invert flag is not used */
/* if an Attack Phase is programmed, inversion can occur on each sample */
if ((this.ssg&0x08)>0&&this.volume>=0x200&&this.state>_EG.REL) {
if ((this.ssg&0x01)>0) { /* bit 0 = hold SSG-EG */
if ((this.ssg&0x02)>0) this.ssgn = 4; /* set inversion flag */
if (this.state!==_EG.ATT&&!(this.ssgn^(this.ssg&0x04))) this.volume = _ENV.MAX_ATT_INDEX; /* force attenuation level during decay phases */
}
else { /* loop SSG-EG */
/* toggle output inversion flag or reset Phase Generator */
if ((this.ssg&0x02)>0) this.ssgn ^= 4;
else this.phase = 0;
/* same as Key ON */
if (this.state!==_EG.ATT) {
if ((this.rate.ar+this.rate.ksr)<94) /*32+62*/
this.state = (this.volume<=_ENV.MIN_ATT_INDEX)?
(this.sl===_ENV.MIN_ATT_INDEX?_EG.SUS:_EG.DEC):