-
Notifications
You must be signed in to change notification settings - Fork 0
/
ym2413.js
1342 lines (1264 loc) · 44.4 KB
/
ym2413.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 YM2413() {
if (!this instanceof YM2413) return new YM2413();
this.version = 0x100;
this.start = 0;
this.count = 0;
this.chip = null;
}
(function(Y){
"use strict";
/**** CONFIG ****/
var cfg = {
maxcalc:0, // for logging, # total chan_calc ops to log
debug:0, // for logging
debugLocal:0,
debugArr:[],
strict:0 // abort on bad input if true
};
/**** GLOBALS ****/
var _YM = { //////////// old?
"FREQ_SH":16, /* 16.16 fixed point (frequency calculations) */
"EG_SH":16, /* 16.16 fixed point (EG timing) */
"LFO_SH":24, /* 8.24 fixed point (LFO calculations) */
"TIMER_SH":16 // 16.16 fixed point (timers calcs)
};
_YM.FREQ_MASK = (1<<_YM.FREQ_SH)-1;
/* envelope output entries */
var _ENV = {
"BITS":10,
"MIN_ATT_INDEX":0
};
_ENV.LEN = 1<<_ENV.BITS;
_ENV.STEP = 128.0/_ENV.LEN;
_ENV.MAX_ATT_INDEX = (1<<(_ENV.BITS-2))-1; /*255*/
/* sinwave entries */
var _SIN = {
"BITS":10
};
_SIN.LEN = 1<<_SIN.BITS;
_SIN.MASK = _SIN.LEN-1;
var _TL = {
"BITS":14
};
_TL.RES_LEN = 256; /* 8 bits addressing (real chip) */
/* TL_TAB_LEN is calculated as:
* 11 - sinus amplitude bits (Y axis)
* 2 - sinus sign bit (Y axis)
* TL_RES_LEN - sinus resolution (X axis)
*/
_TL.TAB_LEN = 11*2*_TL.RES_LEN;
_TL.tab = new Array(_TL.TAB_LEN);
/* Envelope Generator phases */
var _EG = {
'DMP':5,
'ATT':4,
'DEC':3,
'SUS':2,
'REL':1,
'OFF':0
};
_ENV.QUIET = _TL.TAB_LEN>>5;
/* register number to channel number , slot offset */
var _SLOT = [0,1];
/* key scale level */
/* table is 3dB/octave, DV converts this into 6dB/octave */
/* 0.1875 is bit 0 weight of the envelope counter (volume) expressed in the 'decibel' scale */
_YM.ksl = (function(){
var DV = 0.1875;
return [
/* OCT 0 */
0.000/DV, 0.000/DV, 0.000/DV, 0.000/DV,
0.000/DV, 0.000/DV, 0.000/DV, 0.000/DV,
0.000/DV, 0.000/DV, 0.000/DV, 0.000/DV,
0.000/DV, 0.000/DV, 0.000/DV, 0.000/DV,
/* OCT 1 */
0.000/DV, 0.000/DV, 0.000/DV, 0.000/DV,
0.000/DV, 0.000/DV, 0.000/DV, 0.000/DV,
0.000/DV, 0.750/DV, 1.125/DV, 1.500/DV,
1.875/DV, 2.250/DV, 2.625/DV, 3.000/DV,
/* OCT 2 */
0.000/DV, 0.000/DV, 0.000/DV, 0.000/DV,
0.000/DV, 1.125/DV, 1.875/DV, 2.625/DV,
3.000/DV, 3.750/DV, 4.125/DV, 4.500/DV,
4.875/DV, 5.250/DV, 5.625/DV, 6.000/DV,
/* OCT 3 */
0.000/DV, 0.000/DV, 0.000/DV, 1.875/DV,
3.000/DV, 4.125/DV, 4.875/DV, 5.625/DV,
6.000/DV, 6.750/DV, 7.125/DV, 7.500/DV,
7.875/DV, 8.250/DV, 8.625/DV, 9.000/DV,
/* OCT 4 */
0.000/DV, 0.000/DV, 3.000/DV, 4.875/DV,
6.000/DV, 7.125/DV, 7.875/DV, 8.625/DV,
9.000/DV, 9.750/DV,10.125/DV,10.500/DV,
10.875/DV,11.250/DV,11.625/DV,12.000/DV,
/* OCT 5 */
0.000/DV, 3.000/DV, 6.000/DV, 7.875/DV,
9.000/DV,10.125/DV,10.875/DV,11.625/DV,
12.000/DV,12.750/DV,13.125/DV,13.500/DV,
13.875/DV,14.250/DV,14.625/DV,15.000/DV,
/* OCT 6 */
0.000/DV, 6.000/DV, 9.000/DV,10.875/DV,
12.000/DV,13.125/DV,13.875/DV,14.625/DV,
15.000/DV,15.750/DV,16.125/DV,16.500/DV,
16.875/DV,17.250/DV,17.625/DV,18.000/DV,
/* OCT 7 */
0.000/DV, 9.000/DV,12.000/DV,13.875/DV,
15.000/DV,16.125/DV,16.875/DV,17.625/DV,
18.000/DV,18.750/DV,19.125/DV,19.500/DV,
19.875/DV,20.250/DV,20.625/DV,21.000/DV
];
})();
/* sustain level table (3dB per step) */
/* 0 - 15: 0, 3, 6, 9,12,15,18,21,24,27,30,33,36,39,42,45 (dB)*/
_YM.sl = (function(){
var SC = function(db){return (db*1.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 = [ // 15*_EG.RATE_STEPS
/*cycle:0 1 2 3 4 5 6 7*/
/* 0 */ 0,1, 0,1, 0,1, 0,1, /* rates 00..12 0 (increment by 0 or 1) */
/* 1 */ 0,1, 0,1, 1,1, 0,1, /* rates 00..12 1 */
/* 2 */ 0,1, 1,1, 0,1, 1,1, /* rates 00..12 2 */
/* 3 */ 0,1, 1,1, 1,1, 1,1, /* rates 00..12 3 */
/* 4 */ 1,1, 1,1, 1,1, 1,1, /* rate 13 0 (increment by 1) */
/* 5 */ 1,1, 1,2, 1,1, 1,2, /* rate 13 1 */
/* 6 */ 1,2, 1,2, 1,2, 1,2, /* rate 13 2 */
/* 7 */ 1,2, 2,2, 1,2, 2,2, /* rate 13 3 */
/* 8 */ 2,2, 2,2, 2,2, 2,2, /* rate 14 0 (increment by 2) */
/* 9 */ 2,2, 2,4, 2,2, 2,4, /* rate 14 1 */
/*10 */ 2,4, 2,4, 2,4, 2,4, /* rate 14 2 */
/*11 */ 2,4, 4,4, 2,4, 4,4, /* rate 14 3 */
/*12 */ 4,4, 4,4, 4,4, 4,4, /* rates 15 0, 15 1, 15 2, 15 3 (increment by 4) */
/*13 */ 8,8, 8,8, 8,8, 8,8, /* rates 15 2, 15 3 for attack */
/*14 */ 0,0, 0,0, 0,0, 0,0, /* infinity rates for attack and decay(s) */
];
/*note that there is no O(13) in this table - it's directly in the code */
_EG.rate_select = (function(){
var O = function(a){return (a*_EG.RATE_STEPS)|0;};
return [ /* Envelope Generator rates (16 + 64 rates + 16 RKS) */
/* 16 infinite time rates */
O(14),O(14),O(14),O(14),O(14),O(14),O(14),O(14),
O(14),O(14),O(14),O(14),O(14),O(14),O(14),O(14),
/* rates 00-12 */
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),
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 13 */
O( 4),O( 5),O( 6),O( 7),
/* rate 14 */
O( 8),O( 9),O(10),O(11),
/* rate 15 */
O(12),O(12),O(12),O(12),
/* 16 dummy rates (same as 15 3) */
O(12),O(12),O(12),O(12),O(12),O(12),O(12),O(12),
O(12),O(12),O(12),O(12),O(12),O(12),O(12),O(12)
];
})();
/*rate 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 */
/*shift 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 0, 0 */
/*mask 8191, 4095, 2047, 1023, 511, 255, 127, 63, 31, 15, 7, 3, 1, 0, 0, 0 */
_EG.rate_shift = (function(){
var O = function(a){return (a)|0;};
return [ /* Envelope Generator counter shifts (16 + 64 rates + 16 RKS) */
/* 16 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),
/* rates 00-12 */
O(13),O(13),O(13),O(13),
O(12),O(12),O(12),O(12),
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),
/* 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),
/* 16 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)
];
})();
_YM.mul = (function(){
var O = function(a){return (2*a)|0;};
return [
/* 1/2, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,10,12,12,15,15 */
O(0.5),O( 1),O( 2),O( 3), O( 4),O( 5),O( 6),O( 7),
O( 8),O( 9),O(10),O(11), O(12),O(13),O(14),O(15)
];
})();
/* sin waveform table in 'decibel' scale */
/* two waveforms on OPLL type chips */
_YM.sin = new Array(_SIN.LEN<<1);
/* LFO Amplitude Modulation table (verified on real YM3812)
27 output levels (triangle waveform); 1 level takes one of: 192, 256 or 448 samples
Length: 210 elements.
Each of the elements has to be repeated
exactly 64 times (on 64 consecutive samples).
The whole table takes: 64 * 210 = 13440 samples.
We use data>>1, until we find what it really is on real chip...
*/
var _LFO = {
"AM_TAB_LEN":210,
"am_table":[
0,0,0,0,0,0,0,
1,1,1,1,
2,2,2,2,
3,3,3,3,
4,4,4,4,
5,5,5,5,
6,6,6,6,
7,7,7,7,
8,8,8,8,
9,9,9,9,
10,10,10,10,
11,11,11,11,
12,12,12,12,
13,13,13,13,
14,14,14,14,
15,15,15,15,
16,16,16,16,
17,17,17,17,
18,18,18,18,
19,19,19,19,
20,20,20,20,
21,21,21,21,
22,22,22,22,
23,23,23,23,
24,24,24,24,
25,25,25,25,
26,26,26,
25,25,25,25,
24,24,24,24,
23,23,23,23,
22,22,22,22,
21,21,21,21,
20,20,20,20,
19,19,19,19,
18,18,18,18,
17,17,17,17,
16,16,16,16,
15,15,15,15,
14,14,14,14,
13,13,13,13,
12,12,12,12,
11,11,11,11,
10,10,10,10,
9,9,9,9,
8,8,8,8,
7,7,7,7,
6,6,6,6,
5,5,5,5,
4,4,4,4,
3,3,3,3,
2,2,2,2,
1,1,1,1
],
"pm_table":[ /* LFO Phase Modulation table (verified on real YM2413) */
/* FNUM2/FNUM = 0 00xxxxxx (0x0000) */
0, 0, 0, 0, 0, 0, 0, 0,
/* FNUM2/FNUM = 0 01xxxxxx (0x0040) */
1, 0, 0, 0,-1, 0, 0, 0,
/* FNUM2/FNUM = 0 10xxxxxx (0x0080) */
2, 1, 0,-1,-2,-1, 0, 1,
/* FNUM2/FNUM = 0 11xxxxxx (0x00C0) */
3, 1, 0,-1,-3,-1, 0, 1,
/* FNUM2/FNUM = 1 00xxxxxx (0x0100) */
4, 2, 0,-2,-4,-2, 0, 2,
/* FNUM2/FNUM = 1 01xxxxxx (0x0140) */
5, 2, 0,-2,-5,-2, 0, 2,
/* FNUM2/FNUM = 1 10xxxxxx (0x0180) */
6, 3, 0,-3,-6,-3, 0, 3,
/* FNUM2/FNUM = 1 11xxxxxx (0x01C0) */
7, 3, 0,-3,-7,-3, 0, 3
],
"AM":0, // UINT32
"PM":0 // INT32
};
/* This is not 100% perfect yet but very close */
/*
- multi parameters are 100% correct (instruments and drums)
- LFO PM and AM enable are 100% correct
- waveform DC and DM select are 100% correct
*/
_YM.table = [ // [19][8]
/* MULT MULT modTL DcDmFb AR/DR AR/DR SL/RR SL/RR */
/* 0 1 2 3 4 5 6 7 */
[0x49, 0x4c, 0x4c, 0x12, 0x00, 0x00, 0x00, 0x00 ], /* 0 */
[0x61, 0x61, 0x1e, 0x17, 0xf0, 0x78, 0x00, 0x17 ], /* 1 */
/* [0x13, 0x41, 0x16, 0x0e, 0xfd, 0xf4 ,0x23, 0x23,],*/ /* 2 */
[0x13, 0x41, 0x1e, 0x0d, 0xd7, 0xf7, 0x13, 0x13 ], /* 2 */
[0x13, 0x01, 0x99, 0x04, 0xf2, 0xf4, 0x11, 0x23 ], /* 3 */
[0x21, 0x61, 0x1b, 0x07, 0xaf, 0x64, 0x40, 0x27 ], /* 4 */
/*{0x22, 0x21, 0x1e, 0x09, 0xf0, 0x76, 0x08, 0x28 }, */ /* 5 */
[0x22, 0x21, 0x1e, 0x06, 0xf0, 0x75, 0x08, 0x18 ], /* 5 */
/*{0x31, 0x22, 0x16, 0x09, 0x90, 0x7f, 0x00, 0x08 }, */ /* 6 */
[0x31, 0x22, 0x16, 0x05, 0x90, 0x71, 0x00, 0x13 ], /* 6 */
[0x21, 0x61, 0x1d, 0x07, 0x82, 0x80, 0x10, 0x17 ], /* 7 */
[0x23, 0x21, 0x2d, 0x16, 0xc0, 0x70, 0x07, 0x07 ], /* 8 */
[0x61, 0x61, 0x1b, 0x06, 0x64, 0x65, 0x10, 0x17 ], /* 9 */
/* {0x61, 0x61, 0x0c, 0x08, 0x85, 0xa0, 0x79, 0x07 }, */ /* A */
[0x61, 0x61, 0x0c, 0x18, 0x85, 0xf0, 0x70, 0x07 ], /* A */
[0x23, 0x01, 0x07, 0x11, 0xf0, 0xa4, 0x00, 0x22 ], /* B */
[0x97, 0xc1, 0x24, 0x07, 0xff, 0xf8, 0x22, 0x12 ], /* C */
/* {0x61, 0x10, 0x0c, 0x08, 0xf2, 0xc4, 0x40, 0xc8 }, */ /* D */
[0x61, 0x10, 0x0c, 0x05, 0xf2, 0xf4, 0x40, 0x44 ], /* D */
[0x01, 0x01, 0x55, 0x03, 0xf3, 0x92, 0xf3, 0xf3 ], /* E */
[0x61, 0x41, 0x89, 0x03, 0xf1, 0xf4, 0xf0, 0x13 ], /* F */
/* drum instruments definitions */
/* MULTI MULTI modTL xxx AR/DR AR/DR SL/RR SL/RR */
/* 0 1 2 3 4 5 6 7 */
[0x01, 0x01, 0x16, 0x00, 0xfd, 0xf8, 0x2f, 0x6d ],/* BD(multi verified, modTL verified, mod env - verified(close), carr. env verifed) */
[0x01, 0x01, 0x00, 0x00, 0xd8, 0xd8, 0xf9, 0xf8 ],/* HH(multi verified), SD(multi not used) */
[0x05, 0x01, 0x00, 0x00, 0xf8, 0xba, 0x49, 0x55 ]/* TOM(multi,env verified), TOP CYM(multi verified, env verified) */
];
/**** END GLOBALS ****/
/**** FM STRUCTS based on genplus-gx ****/
function FM_SLOT() {
this.KSR = 0; /* key scale rate */
function _rate() {
this.ar = 0; /* attack rate: AR<<2 */
this.d1r = 0; /* decay rate: DR<<2 */
//this.d2r = 0;
this.rr = 0; /* release rate:RR<<2 */
this.ksr = 0; /* key scale rate: kcode>>KSR */
this.mul = 1; /* multiple: mul_tab[ML] */
this.init = function() {
this.ar = 0;
this.d1r = 0;
//this.d2r = 0;
this.rr = 0;
this.ksr = 0;
this.mul = 1;
};
}
this.rate = new _rate;
/* Phase Generator */
this.phase = 0;
this.freq = 0;
this.fb_shift = 0;
this.op1_out = [0,0];
/* Envelope Generator */
this.eg_type = 0; /* percussive/nonpercussive mode */
this.state = 0; /* phase type */
this.tl = 0; /* total level: TL << 2 */
this.tll = 0; /* adjusted now TL */
this.volume = 0; /* envelope counter */
this.sl = 0; /* sustain level: sl_tab[SL] */
function _eg() {
this.dp=0; // UINT8
this.ar=0; // UINT8
this.d1r=0; // UINT8
//this.d2r=0; // UINT8
this.rr=0; // UINT8
this.rs=0; // UINT8
this.init = function(){
this.dp=0; // UINT8
this.ar=0; // UINT8
this.d1r=0; // UINT8
//this.d2r=0; // UINT8
this.rr=0; // UINT8
this.rs=0; // UINT8
};
}
this.eg = {
sh:new _eg, // state
sel:new _eg,
init:function(){this.sh.init();this.sel.init();}
};
this.key = 0; /* 0 = KEY OFF, >0 = KEY ON */
this.AMmask = 0; /* LFO Amplitude Modulation enable mask */
this.vib = 0; /* LFO Phase Modulation enable flag (active high)*/
this.wavetable = 0; /* waveform select */
}
function FM_CH() {
this.SLOT = [
new FM_SLOT(),
new FM_SLOT()
];
this.block_fnum = 0; /* block+fnum */
this.fc = 0; /* Freq. freqement base */
this.ksl_base = 0; /* KeyScaleLevel Base step */
this.kcode = 0; /* key code (for key scaling) */
this.sus = 0; /* sus on/off (release speed in percussive mode) */
this.rhythmType = [0,0]; // to replace hardcoded rhythm; 0=n/a, 1=bd, 2=hh, 3=sn, 4=tt, 5=cy
this.out = [0,0]; // replaces output[normal, rhythm]
this.muted = 0;
}
function FM_OPLL(c, r) {
function _timer() {
this.cnt = 0;
this.timer = 0; // unused for lfo
this.timer_add = 0; // aka lfo_*_inc
this.timer_overflow = 0; // unused for lfo
this.init = function() {
this.cnt = 0; /* global envelope generator counter */
this.timer = 0; /* global envelope generator counter works at frequency = chipclock/72 */
this.timer_add = 0; /* step of eg_timer */
this.timer_overflow = 0; /* envelope generator timer overlfows every 1 sample (on real chip) */
};
}
this.eg = new _timer();
this.lfo = {};
this.lfo.am = new _timer(); // lfo_am_*
this.lfo.pm = new _timer(); // lfo_pm_*
this.noise = {
"rng":0, /* 23 bit noise shift register */
"phase":0, /* current noise 'phase' (formerly noise_p) */
"period":0 /* current noise period (formerly noise_f) */
}
this.address = 0; /* address register */
this.status = 0; /* status flag */
this.clock = c||3579545; /* master clock (Hz) */
this.rate = r||44100; /* sampling rate (Hz) */
}
function YMX(c,r) {
this.CH = [
new FM_CH, new FM_CH, new FM_CH,
new FM_CH, new FM_CH, new FM_CH,
new FM_CH, new FM_CH, new FM_CH
]; /* OPLL chips have 9 channels */
this.instvol_r = new Array(9); /* instrument/volume (or volume/volume in percussive mode) */
this.rhythm = 0; /* Rhythm mode */
/* instrument settings */
/*
0-user instrument
1-15 - fixed instruments
16 -bass drum settings
17,18 - other percussion instruments
*/
this.inst_tab = new Array(19); // UINT8[19][8]
this.fn = {"table":new Array(1024)}; /* fnumber->increment counter */
this.OPLL = new FM_OPLL(c,r);
}
/**** END FM STRUCTS ****/
/**** FM DEFS based on genplus-gx ****/
_YM.output = [0,0];
/* advance LFO to next sample */
function advance_lfo(x) {
x.OPLL.lfo.am.cnt += x.OPLL.lfo.am.timer_add;
if (x.OPLL.lfo.am.cnt>=(_LFO.AM_TAB_LEN<<_YM.LFO_SH)) /* lfo_am_table is 210 elements long */
x.OPLL.lfo.am.cnt -= (_LFO.AM_TAB_LEN<<_YM.LFO_SH);
_LFO.AM = _LFO.am_table[x.OPLL.lfo.am.cnt>>_YM.LFO_SH]>>1;
x.OPLL.lfo.pm.cnt += x.OPLL.lfo.pm.timer_add;
_LFO.PM = (x.OPLL.lfo.pm.cnt>>_YM.LFO_SH)&7;
}
FM_SLOT.prototype.advance_eg = function(eg_cnt, isCarrier, canRhythm, isRhythm, sus) {
switch (this.state) {
case _EG.DMP: /* dump phase */
/*dump phase is performed by both operators in each channel*/
/*when CARRIER envelope gets down to zero level,
** phases in BOTH operators are reset (at the same time ?)
*/
if (!(eg_cnt&((1<<this.eg.sh.dp)-1))) {
this.volume += _EG.inc[
this.eg.sel.dp+
((eg_cnt>>this.eg.sh.dp)&7)
];
if (this.volume>=_ENV.MAX_ATT_INDEX) {
this.volume = _ENV.MAX_ATT_INDEX;
this.state = _EG.ATT;
this.phase = 0; /* restart Phase Generator */
}
}
break;
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)
])>>2;
if (this.volume<=_ENV.MIN_ATT_INDEX) {
this.volume = _ENV.MIN_ATT_INDEX;
this.state = _EG.DEC;
}
}
break;
case _EG.DEC: /* decay phase */
if (!(eg_cnt&((1<<this.eg.sh.d1r)-1))) {
this.volume += _EG.inc[
this.eg.sel.d1r+
((eg_cnt>>this.eg.sh.d1r)&7)
];
if (this.volume>=this.sl) {
this.state = _EG.SUS;
}
}
break;
case _EG.SUS: /* sustain phase */
/* this is important behaviour:
one can change percusive/non-percussive modes on the fly and
the chip will remain in sustain phase - verified on real YM3812 */
if (this.eg_type) {} /* non-percussive mode (sustained tone), do nothing */
else { /* percussive mode */
/* during sustain phase chip adds Release Rate (in percussive mode) */
if (!(eg_cnt&((1<<this.eg.sh.rr)-1))) {
this.volume += _EG.inc[
this.eg.sel.rr+
((eg_cnt>>this.eg.sh.rr)&7)
];
if (this.volume>=_ENV.MAX_ATT_INDEX) {
this.volume = _ENV.MAX_ATT_INDEX;
}
}
/* else do nothing in sustain phase */
}
break;
case _EG.REL: /* release phase */
/* allowed are only carriers in melody mode and rhythm slots in rhythm mode */
/*This table shows which operators and on what conditions are allowed to perform EG_REL:
(a) - always perform EG_REL
(n) - never perform EG_REL
(r) - perform EG_REL in Rhythm mode ONLY
0: 0 (n), 1 (a)
1: 2 (n), 3 (a)
2: 4 (n), 5 (a)
3: 6 (n), 7 (a)
4: 8 (n), 9 (a)
5: 10(n), 11(a)
6: 12(r), 13(a)
7: 14(r), 15(a)
8: 16(r), 17(a)
*/
if (isCarrier||(canRhythm&&isRhythm)) { /* exclude modulators in melody channels from performing anything in this mode*/
if (this.eg_type) { /* non-percussive mode (sustained tone) */
if (sus) { /*and use RS when SUS = ON*/
if (!(eg_cnt&((1<<this.eg.sh.rs)-1))) {
this.volume += _EG.inc[
this.eg.sel.rs+
((eg_cnt>>this.eg.sh.rs)&7)
];
if (this.volume>=_ENV.MAX_ATT_INDEX) {
this.volume = _ENV.MAX_ATT_INDEX;
this.state = _EG.OFF;
}
}
}
else { /* use RR when SUS = OFF*/
if (!(eg_cnt&((1<<this.eg.sh.rr)-1))) {
this.volume += _EG.inc[
this.eg.sel.rr+
((eg_cnt>>this.eg.sh.rr)&7)
];
if (this.volume>=_ENV.MAX_ATT_INDEX) {
this.volume = _ENV.MAX_ATT_INDEX;
this.state = _EG.OFF;
}
}
}
}
else { /* percussive mode */
if (!(eg_cnt&((1<<this.eg.sh.rs)-1))) {
this.volume += _EG.inc[
this.eg.sel.rs+
((eg_cnt>>this.eg.sh.rs)&7)
];
if (this.volume>=_ENV.MAX_ATT_INDEX) {
this.volume = _ENV.MAX_ATT_INDEX;
this.state = _EG.OFF;
}
}
}
}
break;
default:
if (cfg.strict) throw new Error("FM_SLOT::advance_eg("+eg_cnt+") - unsupported state ("+this.state+")");
break;
}
};
FM_CH.prototype.advance_eg = function(eg_cnt, canRhythm, isRhythm) {
this.SLOT[_SLOT[0]].advance_eg(eg_cnt, 0, canRhythm, isRhythm, this.sus);
this.SLOT[_SLOT[1]].advance_eg(eg_cnt, 1, canRhythm, isRhythm, this.sus);
};
FM_SLOT.prototype.advance_lfo = function(x, block_fnum) {
var blk, fnum_lfo, offset;
if (this.vib) { /* Phase Generator */
fnum_lfo = ((block_fnum&0x01c0)>>6)<<3;
block_fnum = block_fnum<<1;
offset = _LFO.pm_table[_LFO.PM+fnum_lfo];
if (offset) { /* LFO phase modulation active */
block_fnum += offset;
blk = (block_fnum&0x1c00)>>10;
this.phase += (x.fn.table[block_fnum&0x03ff]>>(7-blk))*this.rate.mul;
}
else { /* LFO phase modulation = zero */
this.phase += this.freq;
}
}
else { /* LFO phase modulation = zero */
this.phase += this.freq;
}
};
FM_CH.prototype.advance_lfo = function(x) {
this.SLOT[_SLOT[0]].advance_lfo(x, this.block_fnum);
this.SLOT[_SLOT[1]].advance_lfo(x, this.block_fnum);
};
/* advance to next sample */
function advance(ym) {
var i, r = !!(ym.rhythm&0x20);
ym.OPLL.eg.timer += ym.OPLL.eg.timer_add;
if (cfg.debug>10) console.log("advance",ym.OPLL.eg.timer,ym.OPLL.eg.timer_overflow);
while (ym.OPLL.eg.timer>=ym.OPLL.eg.timer_overflow) {
ym.OPLL.eg.timer -= ym.OPLL.eg.timer_overflow;
++ym.OPLL.eg.cnt;
for (i=0; i<9; ++i) { //9ch*2op
//_advance_slot_eg(ym, i, 0);
//_advance_slot_eg(ym, i, 1);
ym.CH[i].advance_eg(ym.OPLL.eg.cnt, r, i>=6);
}
}
for (i=0; i<9; ++i) { // 9ch*2op
//_advance_slot_lfo(ym, i, 0);
//_advance_slot_lfo(ym, i, 1);
ym.CH[i].advance_lfo(ym);
}
/* The Noise Generator of the YM3812 is 23-bit shift register.
* Period is equal to 2^23-2 samples.
* Register works at sampling frequency of the chip, so output
* can change on every sample.
*
* Output of the register and input to the bit 22 is:
* bit0 XOR bit14 XOR bit15 XOR bit22
*
* Simply use bit 22 as the noise output.
*/
ym.OPLL.noise.phase += ym.OPLL.noise.period;
i = ym.OPLL.noise.phase>>_YM.FREQ_SH; /* number of events (shifts of the shift register) */
ym.OPLL.noise.phase &= _YM.FREQ_MASK;
while (i) {
/*
Instead of doing all the logic operations above, we
use a trick here (and use bit 0 as the noise output).
The difference is only that the noise bit changes one
step ahead. This doesn't matter since we don't know
what is real state of the noise_rng after the reset.
*/
if ((ym.OPLL.noise.rng&1)>0) ym.OPLL.noise.rng ^= 0x800302;
ym.OPLL.noise.rng >>= 1;
--i;
}
}
function op_calc(phase, env, pm, tab, fb) {
var p = (env<<5)+_YM.sin[tab+
((((phase&~_YM.FREQ_MASK)+(pm<<(fb?0:17)))>>_YM.FREQ_SH)&_SIN.MASK)
];
if (p>=_TL.TAB_LEN) return 0;
else return _TL.tab[p];
}
FM_SLOT.prototype.calcVol = function(){return this.tll+((this.volume|0)+(_LFO.AM&this.AMmask));};
/* calculate output */
FM_CH.prototype.calculate = function(){
var env, out, pm, i = 0, s;
/* SLOT 1 */
s = _SLOT[i]; env = this.SLOT[s].calcVol();
out = (this.SLOT[s].op1_out[0]+this.SLOT[s].op1_out[1])|0;
this.SLOT[s].op1_out[0] = this.SLOT[s].op1_out[1]|0;
pm = this.SLOT[s].op1_out[0];
this.SLOT[s].op1_out[1] = 0;
if (env<_ENV.QUIET) {
if (!this.SLOT[s].fb_shift) out = 0;
this.SLOT[s].op1_out[1] = op_calc(this.SLOT[s].phase, env, (out<<this.SLOT[s].fb_shift), this.SLOT[s].wavetable, 1);
}
/* SLOT 2 */
s = _SLOT[++i]; env = this.SLOT[s].calcVol();
if (env<_ENV.QUIET) {
this.out[0] += op_calc(this.SLOT[s].phase, env, pm, this.SLOT[s].wavetable, 0);
// TODO: replace _YM.output with FM_CH.out here
}
};
/*
operators used in the rhythm sounds generation process:
Envelope Generator:
channel operator register number Bass High Snare Tom Top
/ slot number TL ARDR SLRR Wave Drum Hat Drum Tom Cymbal
6 / 0 12 50 70 90 f0 +
6 / 1 15 53 73 93 f3 +
7 / 0 13 51 71 91 f1 +
7 / 1 16 54 74 94 f4 +
8 / 0 14 52 72 92 f2 +
8 / 1 17 55 75 95 f5 +
Phase Generator:
channel operator register number Bass High Snare Tom Top
/ slot number MULTIPLE Drum Hat Drum Tom Cymbal
6 / 0 12 30 +
6 / 1 15 33 +
7 / 0 13 31 + + +
7 / 1 16 34 ----- n o t u s e d -----
8 / 0 14 32 +
8 / 1 17 35 + +
channel operator register number Bass High Snare Tom Top
number number BLK/FNUM2 FNUM Drum Hat Drum Tom Cymbal
6 12,15 B6 A6 +
7 13,16 B7 A7 + + +
8 14,17 B8 A8 + + +
*/
/* calculate rhythm */
function calculateRhythm(x, n){
if (cfg.debug>10) console.log("rhythm_calc",n);
var out, env, pm; /* pm = phase modulation input (SLOT 2) */
var c, s;
/* Bass Drum (verified on real YM3812):
- depends on the channel 6 'connect' register:
when connect = 0 it works the same as in normal (non-rhythm) mode (op1->op2->out)
when connect = 1 _only_ operator 2 is present on output (op2->out), operator 1 is ignored
- output sample always is multiplied by 2
*/
/* SLOT 1 */
s = 0, c = 6;
env = x.CH[c].SLOT[_SLOT[s]].calcVol();
out = x.CH[c].SLOT[_SLOT[s]].op1_out[0]+ x.CH[c].SLOT[_SLOT[s]].op1_out[1];
x.CH[c].SLOT[_SLOT[s]].op1_out[0] = x.CH[c].SLOT[_SLOT[s]].op1_out[1];
pm = x.CH[c].SLOT[_SLOT[s]].op1_out[0];
x.CH[c].SLOT[_SLOT[s]].op1_out[1] = 0;
if (env<_ENV.QUIET) {
if (!x.CH[c].SLOT[_SLOT[s]].fb_shift) out = 0;
x.CH[c].SLOT[_SLOT[s]].op1_out[1] = op_calc(x.CH[c].SLOT[_SLOT[s]].phase, env, out<<x.CH[c].SLOT[_SLOT[s]].fb_shift, x.CH[c].SLOT[_SLOT[s]].wavetable, 1);
}
/* SLOT 2 */
++s; env = x.CH[c].SLOT[_SLOT[s]].calcVol();
if (env<_ENV.QUIET) {
x.CH[c].out[1] += op_calc(x.CH[c].SLOT[_SLOT[s]].phase, env, pm, x.CH[c].SLOT[_SLOT[s]].wavetable, 0);
// TODO: replace _YM.output with FM_CH.out here
}
var p;
var psh7 = x.CH[7].SLOT[_SLOT[0]].phase>>_YM.FREQ_SH,
/* base frequency derived from operator 1 in channel 7 */
b7 = (psh7>>7)&1, b3 = (psh7>>3)&1, b2 = (psh7>>2)&1, r1 = (b2^b7)|b3;
var psh8 = x.CH[8].SLOT[_SLOT[1]].phase>>_YM.FREQ_SH,
/* enable gate based on frequency of operator 2 in channel 8 */
b5e = (psh8>>5)&1, b3e = (psh8>>3)&1, r2 = (b5e|b3e);
/* High Hat (verified on real YM3812) */
s = 0, c = 7; env = x.CH[c].SLOT[_SLOT[s]].calcVol();
if (env<_ENV.QUIET) {
/* high hat phase generation:
phase = d0 or 234 (based on frequency only)
phase = 34 or 2d0 (based on noise)
*/
/* when res1 = 0 phase = 0x000 | 0xd0; */
/* when res1 = 1 phase = 0x200 | (0xd0>>2); */
p = r1?(0x200|(0xd0>>2)):0xd0;
/* when res2 = 0 pass the phase from calculation above (res1); */
/* when res2 = 1 phase = 0x200 | (0xd0>>2); */
if (r2) p = (0x200|(0xd0>>2));
if (p&0x200) {
/* when phase & 0x200 is set and noise=1 then phase = 0x200|0xd0 */
/* when phase & 0x200 is set and noise=0 then phase = 0x200|(0xd0>>2), ie no change */
if (n) p = 0x200|0xd0;
}
else {
/* when phase & 0x200 is clear and noise=1 then phase = 0xd0>>2 */
/* when phase & 0x200 is clear and noise=0 then phase = 0xd0, ie no change */
if (n) p = 0xd0>>2;
}
x.CH[c].out[1] += op_calc(p<<_YM.FREQ_SH, env, 0, x.CH[c].SLOT[_SLOT[s]].wavetable, 0);
// TODO: replace _YM.output with FM_CH.out here
}
/* Snare Drum (verified on real YM3812) */
++s; env = x.CH[c].SLOT[_SLOT[s]].calcVol();
if (env<_ENV.QUIET) {
/* base frequency derived from operator 1 in channel 7 */
var b8 = ((x.CH[c].SLOT[_SLOT[0]].phase>>_YM.FREQ_SH)>>8)&1;
/* when bit8 = 0 phase = 0x100; */
/* when bit8 = 1 phase = 0x200; */
p = b8?0x200:0x100;
/* Noise bit XOR'es phase by 0x100 */
/* when noisebit = 0 pass the phase from calculation above */
/* when noisebit = 1 phase ^= 0x100; */
/* in other words: phase ^= (noisebit<<8); */
if (n) p ^= 0x100;
x.CH[c].out[1] += op_calc(p<<_YM.FREQ_SH, env, 0, x.CH[c].SLOT[_SLOT[s]].wavetable, 0);
// TODO: replace _YM.output with FM_CH.out here
}
/* Tom Tom (verified on real YM3812) */
s = 0, c = 8; env = x.CH[c].SLOT[_SLOT[s]].calcVol();
if (env<_ENV.QUIET) {
x.CH[c].out[1] += op_calc(x.CH[c].SLOT[_SLOT[s]].phase, env, 0, x.CH[c].SLOT[_SLOT[s]].wavetable, 0);
// TODO: replace _YM.output with FM_CH.out here
}
/* Top Cymbal (verified on real YM2413) */
++s; env = x.CH[c].SLOT[_SLOT[s]].calcVol();
if (env<_ENV.QUIET) {
/* when res1 = 0 phase = 0x000 | 0x100; */
/* when res1 = 1 phase = 0x200 | 0x100; */
p = r1?0x300:0x100;
/* when res2 = 0 pass the phase from calculation above (res1); */
/* when res2 = 1 phase = 0x200 | 0x100; */
if (r2) p = 0x300;
x.CH[c].out[1] += op_calc(p<<_YM.FREQ_SH, env, 0, x.CH[c].SLOT[_SLOT[s]].wavetable, 0);
// TODO: replace _YM.output with FM_CH.out here
}
};
/* generic table initialize */
function init_tables(ym) {
if (cfg.debug) console.log("init_tables",ym.CH.length);
var d, i, x; // signed int
var n; // signed int
var o, m; // double
var q, z;
/* build Linear Power Table */
var tmp = (_ENV.STEP/32.0), sh = (1<<16), rl2 = _TL.RES_LEN<<1;
for (x=0; x<_TL.RES_LEN; ++x) {
m = sh/Math.pow(2, (x+1)*tmp);
//m = m|0; // m = Math.floor(m); // extraneous, folded into next calculation +neo
/* we never reach (1<<16) here due to the (x+1) */
/* result fits within 16 bits at maximum */
//n = m|0; /* 16 bits here */
//n >>= 4; /* 12 bits here */
n = (m|0)>>4;
if (n&1) n = (n>>1)+1; /* round to nearest */
else n = n>>1;
/* 11 bits here (rounded) */
z = x<<1;
_TL.tab[z+0] = n;
_TL.tab[z+1] = -n;
for (i=1; i<11; ++i) {
q = (z+0+i*rl2)|0;
_TL.tab[q] = _TL.tab[z]>>i;
_TL.tab[q+1] = -_TL.tab[q];
}
}
//console.log("TL_TABLE",_TL.tab.join(", "));
/* build Logarithmic Sinus table */
q = Math.PI/_SIN.LEN, z = 8.0/Math.log(2.0), tmp = 2.0/(_ENV.STEP*0.25), d = (1<<(_SIN.BITS-1));
for (i=0; i<_SIN.LEN; ++i) { /* non-standard sinus */
m = Math.sin(((i<<1)+1)*q); /* checked against the real chip */
/* we never reach zero here due to ((i*2)+1) */
/* convert to 'decibels' */
if (m>0.0) o = Math.log(1.0/m)*z;
else o = Math.log(-1.0/m)*z;
//o = o/(_ENV.STEP/4); // folded into next calculation +neo
n = (o*tmp)|0; //n = (2.0*o)|0;
if (n&1) n = (n>>1)+1; /* round to nearest */
else n = n>>1;
/* waveform 0: standard sinus */
_YM.sin[i] = (n<<1)+(m>=0.0?0:1);
/* waveform 1: __ __ */
/* / \____/ \____*/
/* output only first half of the sinus waveform (positive one) */
if ((i&d)>0) _YM.sin[_SIN.LEN+i] = _TL.TAB_LEN;
else _YM.sin[_SIN.LEN+i] = _YM.sin[i];
}
}
var OPLL = {};
OPLL.init = function(x, sc) {
var i, fsh = 64.0*sc*(1<<(_YM.FREQ_SH-10)), /* -10 because chip works with 10.10 fixed point, while we use 16.16 */
lsh = sc*(1<<_YM.LFO_SH),
esh = (1<<_YM.EG_SH);
for (i=0; i<1024; ++i) { /* make fnumber -> increment counter table */
x.fn.table[i] = (i*fsh)|0; /* OPLL (YM2413) phase increment counter = 18bit */
}
/* Amplitude modulation: 27 output levels (triangle waveform); 1 level takes one of: 192, 256 or 448 samples */
x.OPLL.lfo.am.timer_add = (lsh/64.0)|0; /* One entry from LFO_AM_TABLE lasts for 64 samples */
x.OPLL.lfo.pm.timer_add = (lsh/1024.0)|0; /* Vibrato: 8 output levels (triangle waveform); 1 level takes 1024 samples */
x.OPLL.noise.period = ((1<<_YM.FREQ_SH)*sc/1.0)|0; /* Noise generator: a step takes 1 sample */
x.OPLL.eg.timer_add = (esh*sc/1.0)|0;
x.OPLL.eg.timer_overflow = (esh)|0;
};
FM_SLOT.prototype.keyOn = function(ks) {
if (!this.key) this.state = _EG.DMP; /* do NOT restart Phase Generator (verified on real YM2413); phase -> Dump */
this.key = (this.key|ks)|0;
};
FM_SLOT.prototype.keyOff = function(kc) {
if (this.key) {
this.key = (this.key&kc)|0;
if (!this.key) {
if (this.state>_EG.REL) this.state = _EG.REL; /* phase -> Release */
}
}
};
FM_SLOT.prototype.calc_fc = function(fc, kc, sus) {
var ksr, rs, dp, q;
this.freq = fc*this.rate.mul; /* (frequency) phase increment counter */
ksr = (kc>>this.KSR)|0;
if (this.rate.ksr!=ksr) {
this.rate.ksr = ksr;
q = this.rate.ar+ksr;
if (q<78) { /* calculate envelope generator rates */ // 16+62
this.eg.sh.ar = _EG.rate_shift[q];
this.eg.sel.ar = _EG.rate_select[q];
}
else {
this.eg.sh.ar = 0;
this.eg.sel.ar = 13*_EG.RATE_STEPS;
}
q = this.rate.d1r+ksr;
this.eg.sh.d1r = _EG.rate_shift[q];
this.eg.sel.d1r = _EG.rate_select[q];
q = this.rate.rr+ksr;
this.eg.sh.rr = _EG.rate_shift[q];
this.eg.sel.rr = _EG.rate_select[q];
}
//if (sus) rs = 36; // 16+(5<<2)
//else rs = 44; // 16+(7<<2)
rs = 16+((sus?5:7)<<2);
q = rs+ksr;
this.eg.sh.rs = _EG.rate_shift[q];
this.eg.sel.rs = _EG.rate_select[q];
dp = 16+(13<<2);
q = dp+ksr;
this.eg.sh.dp = _EG.rate_shift[q];
this.eg.sel.dp = _EG.rate_select[q];
};
/* set multi,am,vib,EG-TYP,KSR,mul */
FM_CH.prototype.set_mul = function(s, v) {
this.SLOT[s].rate.mul = _YM.mul[v&0x0f];
this.SLOT[s].KSR = ((v&0x10)>0)?0:2;
this.SLOT[s].eg_type = (v&0x20)|0;
this.SLOT[s].vib = (v&0x40)|0;
this.SLOT[s].AMmask = ((v&0x80)>0)?~0:0;
this.SLOT[s].calc_fc(this.fc, this.kcode, this.sus);