-
Notifications
You must be signed in to change notification settings - Fork 18
/
emu2413.c
1851 lines (1598 loc) · 45.4 KB
/
emu2413.c
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
/***********************************************************************************
emu2413.c -- YM2413 emulator written by Mitsutaka Okazaki 2001
2001 01-08 : Version 0.10 -- 1st version.
2001 01-15 : Version 0.20 -- semi-public version.
2001 01-16 : Version 0.30 -- 1st public version.
2001 01-17 : Version 0.31 -- Fixed bassdrum problem.
: Version 0.32 -- LPF implemented.
2001 01-18 : Version 0.33 -- Fixed the drum problem, refine the mix-down method.
-- Fixed the LFO bug.
2001 01-24 : Version 0.35 -- Fixed the drum problem,
support undocumented EG behavior.
2001 02-02 : Version 0.38 -- Improved the performance.
Fixed the hi-hat and cymbal model.
Fixed the default percussive datas.
Noise reduction.
Fixed the feedback problem.
2001 03-03 : Version 0.39 -- Fixed some drum bugs.
Improved the performance.
2001 03-04 : Version 0.40 -- Improved the feedback.
Change the default table size.
Clock and Rate can be changed during play.
2001 06-24 : Version 0.50 -- Improved the hi-hat and the cymbal tone.
Added VRC7 patch (OPLL_reset_patch is changed).
Fixed OPLL_reset() bug.
Added OPLL_setMask, OPLL_getMask and OPLL_toggleMask.
Added OPLL_writeIO.
2001 09-28 : Version 0.51 -- Removed the noise table.
2002 01-28 : Version 0.52 -- Added Stereo mode.
2002 02-07 : Version 0.53 -- Fixed some drum bugs.
2002 02-20 : Version 0.54 -- Added the best quality mode.
2002 03-02 : Version 0.55 -- Removed OPLL_init & OPLL_close.
2002 05-30 : Version 0.60 -- Fixed HH&CYM generator and all voice datas.
2004 04-10 : Version 0.61 -- Added YMF281B tone (defined by Chabin).
References:
fmopl.c -- 1999,2000 written by Tatsuyuki Satoh (MAME development).
fmopl.c(fixed) -- (C) 2002 Jarek Burczynski.
s_opl.c -- 2001 written by Mamiya (NEZplug development).
fmgen.cpp -- 1999,2000 written by cisc.
fmpac.ill -- 2000 created by NARUTO.
MSX-Datapack
YMU757 data sheet
YM2143 data sheet
**************************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "emu2413.h"
#ifdef EMU2413_COMPACTION
#define OPLL_TONE_NUM 1
static unsigned char default_inst[OPLL_TONE_NUM][(16 + 3) * 16] = {
{
#include "2413tone.h"
}
};
#else
#define OPLL_TONE_NUM 3
static unsigned char default_inst[OPLL_TONE_NUM][(16 + 3) * 16] = {
{
#include "2413tone.h"
},
{
#include "vrc7tone.h"
},
{
#include "281btone.h"
}
};
#endif
/* Size of Sintable ( 8 -- 18 can be used. 9 recommended.) */
#define PG_BITS 9
#define PG_WIDTH (1<<PG_BITS)
/* Phase increment counter */
#define DP_BITS 18
#define DP_WIDTH (1<<DP_BITS)
#define DP_BASE_BITS (DP_BITS - PG_BITS)
/* Dynamic range (Accuracy of sin table) */
#define DB_BITS 8
#define DB_STEP (48.0/(1<<DB_BITS))
#define DB_MUTE (1<<DB_BITS)
/* Dynamic range of envelope */
#define EG_STEP 0.375
#define EG_BITS 7
#define EG_MUTE (1<<EG_BITS)
/* Dynamic range of total level */
#define TL_STEP 0.75
#define TL_BITS 6
#define TL_MUTE (1<<TL_BITS)
/* Dynamic range of sustine level */
#define SL_STEP 3.0
#define SL_BITS 4
#define SL_MUTE (1<<SL_BITS)
#define EG2DB(d) ((d)*(e_int32)(EG_STEP/DB_STEP))
#define TL2EG(d) ((d)*(e_int32)(TL_STEP/EG_STEP))
#define SL2EG(d) ((d)*(e_int32)(SL_STEP/EG_STEP))
#define DB_POS(x) (e_uint32)((x)/DB_STEP)
#define DB_NEG(x) (e_uint32)(DB_MUTE+DB_MUTE+(x)/DB_STEP)
/* Bits for liner value */
#define DB2LIN_AMP_BITS 8
#define SLOT_AMP_BITS (DB2LIN_AMP_BITS)
/* Bits for envelope phase incremental counter */
#define EG_DP_BITS 22
#define EG_DP_WIDTH (1<<EG_DP_BITS)
/* Bits for Pitch and Amp modulator */
#define PM_PG_BITS 8
#define PM_PG_WIDTH (1<<PM_PG_BITS)
#define PM_DP_BITS 16
#define PM_DP_WIDTH (1<<PM_DP_BITS)
#define AM_PG_BITS 8
#define AM_PG_WIDTH (1<<AM_PG_BITS)
#define AM_DP_BITS 16
#define AM_DP_WIDTH (1<<AM_DP_BITS)
/* PM table is calcurated by PM_AMP * pow(2,PM_DEPTH*sin(x)/1200) */
#define PM_AMP_BITS 8
#define PM_AMP (1<<PM_AMP_BITS)
/* PM speed(Hz) and depth(cent) */
#define PM_SPEED 6.4
#define PM_DEPTH 13.75
/* AM speed(Hz) and depth(dB) */
#define AM_SPEED 3.6413
#define AM_DEPTH 4.875
/* Cut the lower b bit(s) off. */
#define HIGHBITS(c,b) ((c)>>(b))
/* Leave the lower b bit(s). */
#define LOWBITS(c,b) ((c)&((1<<(b))-1))
/* Expand x which is s bits to d bits. */
#define EXPAND_BITS(x,s,d) ((x)<<((d)-(s)))
/* Expand x which is s bits to d bits and fill expanded bits '1' */
#define EXPAND_BITS_X(x,s,d) (((x)<<((d)-(s)))|((1<<((d)-(s)))-1))
/* Adjust envelope speed which depends on sampling rate. */
#define RATE_ADJUST(x) (rate==49716?x:(e_uint32)((double)(x)*clk/72/rate + 0.5)) /* added 0.5 to round the value*/
#define MOD(o,x) (&(o)->slot[(x)<<1])
#define CAR(o,x) (&(o)->slot[((x)<<1)|1])
#define BIT(s,b) (((s)>>(b))&1)
/* Input clock */
static e_uint32 clk = 844451141;
/* Sampling rate */
static e_uint32 rate = 3354932;
/* WaveTable for each envelope amp */
static e_uint16 fullsintable[PG_WIDTH];
static e_uint16 halfsintable[PG_WIDTH];
static e_uint16 *waveform[2] = { fullsintable, halfsintable };
/* LFO Table */
static e_int32 pmtable[PM_PG_WIDTH];
static e_int32 amtable[AM_PG_WIDTH];
/* Phase delta for LFO */
static e_uint32 pm_dphase;
static e_uint32 am_dphase;
/* dB to Liner table */
static e_int16 DB2LIN_TABLE[(DB_MUTE + DB_MUTE) * 2];
/* Liner to Log curve conversion table (for Attack rate). */
static e_uint16 AR_ADJUST_TABLE[1 << EG_BITS];
/* Empty voice data */
static OPLL_PATCH null_patch = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
/* Basic voice Data */
static OPLL_PATCH default_patch[OPLL_TONE_NUM][(16 + 3) * 2];
/* Definition of envelope mode */
enum OPLL_EG_STATE
{ READY, ATTACK, DECAY, SUSHOLD, SUSTINE, RELEASE, SETTLE, FINISH };
/* Phase incr table for Attack */
static e_uint32 dphaseARTable[16][16];
/* Phase incr table for Decay and Release */
static e_uint32 dphaseDRTable[16][16];
/* KSL + TL Table */
static e_uint32 tllTable[16][8][1 << TL_BITS][4];
static e_int32 rksTable[2][8][2];
/* Phase incr table for PG */
static e_uint32 dphaseTable[512][8][16];
/***************************************************
Create tables
****************************************************/
INLINE static e_int32
Min (e_int32 i, e_int32 j)
{
if (i < j)
return i;
else
return j;
}
/* Table for AR to LogCurve. */
static void
makeAdjustTable (void)
{
e_int32 i;
AR_ADJUST_TABLE[0] = (1 << EG_BITS) - 1;
for (i = 1; i < (1<<EG_BITS); i++)
AR_ADJUST_TABLE[i] = (e_uint16) ((double) (1<<EG_BITS)-1 - ((1<<EG_BITS)-1)*log(i)/log(127));
}
/* Table for dB(0 -- (1<<DB_BITS)-1) to Liner(0 -- DB2LIN_AMP_WIDTH) */
static void
makeDB2LinTable (void)
{
e_int32 i;
for (i = 0; i < DB_MUTE + DB_MUTE; i++)
{
DB2LIN_TABLE[i] = (e_int16) ((double) ((1 << DB2LIN_AMP_BITS) - 1) * pow (10, -(double) i * DB_STEP / 20));
if (i >= DB_MUTE) DB2LIN_TABLE[i] = 0;
DB2LIN_TABLE[i + DB_MUTE + DB_MUTE] = (e_int16) (-DB2LIN_TABLE[i]);
}
}
/* Liner(+0.0 - +1.0) to dB((1<<DB_BITS) - 1 -- 0) */
static e_int32
lin2db (double d)
{
if (d == 0)
return (DB_MUTE - 1);
else
return Min (-(e_int32) (20.0 * log10 (d) / DB_STEP), DB_MUTE-1); /* 0 -- 127 */
}
/* Sin Table */
static void
makeSinTable (void)
{
e_int32 i;
for (i = 0; i < PG_WIDTH / 4; i++)
{
fullsintable[i] = (e_uint32) lin2db (sin (2.0 * PI * i / PG_WIDTH) );
}
for (i = 0; i < PG_WIDTH / 4; i++)
{
fullsintable[PG_WIDTH / 2 - 1 - i] = fullsintable[i];
}
for (i = 0; i < PG_WIDTH / 2; i++)
{
fullsintable[PG_WIDTH / 2 + i] = (e_uint32) (DB_MUTE + DB_MUTE + fullsintable[i]);
}
for (i = 0; i < PG_WIDTH / 2; i++)
halfsintable[i] = fullsintable[i];
for (i = PG_WIDTH / 2; i < PG_WIDTH; i++)
halfsintable[i] = fullsintable[0];
}
static double saw(double phase)
{
if(phase <= PI/2)
return phase * 2 / PI ;
else if(phase <= PI*3/2)
return 2.0 - ( phase * 2 / PI );
else
return -4.0 + phase * 2 / PI;
}
/* Table for Pitch Modulator */
static void
makePmTable (void)
{
e_int32 i;
for (i = 0; i < PM_PG_WIDTH; i++)
/* pmtable[i] = (e_int32) ((double) PM_AMP * pow (2, (double) PM_DEPTH * sin (2.0 * PI * i / PM_PG_WIDTH) / 1200)); */
pmtable[i] = (e_int32) ((double) PM_AMP * pow (2, (double) PM_DEPTH * saw (2.0 * PI * i / PM_PG_WIDTH) / 1200));
}
/* Table for Amp Modulator */
static void
makeAmTable (void)
{
e_int32 i;
for (i = 0; i < AM_PG_WIDTH; i++)
/* amtable[i] = (e_int32) ((double) AM_DEPTH / 2 / DB_STEP * (1.0 + sin (2.0 * PI * i / PM_PG_WIDTH))); */
amtable[i] = (e_int32) ((double) AM_DEPTH / 2 / DB_STEP * (1.0 + saw (2.0 * PI * i / PM_PG_WIDTH)));
}
/* Phase increment counter table */
static void
makeDphaseTable (void)
{
e_uint32 fnum, block, ML;
e_uint32 mltable[16] =
{ 1, 1 * 2, 2 * 2, 3 * 2, 4 * 2, 5 * 2, 6 * 2, 7 * 2, 8 * 2, 9 * 2, 10 * 2, 10 * 2, 12 * 2, 12 * 2, 15 * 2, 15 * 2 };
for (fnum = 0; fnum < 512; fnum++)
for (block = 0; block < 8; block++)
for (ML = 0; ML < 16; ML++)
dphaseTable[fnum][block][ML] = RATE_ADJUST (((fnum * mltable[ML]) << block) >> (20 - DP_BITS));
}
static void
makeTllTable (void)
{
#define dB2(x) ((x)*2)
static double kltable[16] = {
dB2 (0.000), dB2 (9.000), dB2 (12.000), dB2 (13.875), dB2 (15.000), dB2 (16.125), dB2 (16.875), dB2 (17.625),
dB2 (18.000), dB2 (18.750), dB2 (19.125), dB2 (19.500), dB2 (19.875), dB2 (20.250), dB2 (20.625), dB2 (21.000)
};
e_int32 tmp;
e_int32 fnum, block, TL, KL;
for (fnum = 0; fnum < 16; fnum++)
for (block = 0; block < 8; block++)
for (TL = 0; TL < 64; TL++)
for (KL = 0; KL < 4; KL++)
{
if (KL == 0)
{
tllTable[fnum][block][TL][KL] = TL2EG (TL);
}
else
{
tmp = (e_int32) (kltable[fnum] - dB2 (3.000) * (7 - block));
if (tmp <= 0)
tllTable[fnum][block][TL][KL] = TL2EG (TL);
else
tllTable[fnum][block][TL][KL] = (e_uint32) ((tmp >> (3 - KL)) / EG_STEP) + TL2EG (TL);
}
}
}
#ifdef USE_SPEC_ENV_SPEED
static double attacktime[16][4] = {
{0, 0, 0, 0},
{1730.15, 1400.60, 1153.43, 988.66},
{865.08, 700.30, 576.72, 494.33},
{432.54, 350.15, 288.36, 247.16},
{216.27, 175.07, 144.18, 123.58},
{108.13, 87.54, 72.09, 61.79},
{54.07, 43.77, 36.04, 30.90},
{27.03, 21.88, 18.02, 15.45},
{13.52, 10.94, 9.01, 7.72},
{6.76, 5.47, 4.51, 3.86},
{3.38, 2.74, 2.25, 1.93},
{1.69, 1.37, 1.13, 0.97},
{0.84, 0.70, 0.60, 0.54},
{0.50, 0.42, 0.34, 0.30},
{0.28, 0.22, 0.18, 0.14},
{0.00, 0.00, 0.00, 0.00}
};
static double decaytime[16][4] = {
{0, 0, 0, 0},
{20926.60, 16807.20, 14006.00, 12028.60},
{10463.30, 8403.58, 7002.98, 6014.32},
{5231.64, 4201.79, 3501.49, 3007.16},
{2615.82, 2100.89, 1750.75, 1503.58},
{1307.91, 1050.45, 875.37, 751.79},
{653.95, 525.22, 437.69, 375.90},
{326.98, 262.61, 218.84, 187.95},
{163.49, 131.31, 109.42, 93.97},
{81.74, 65.65, 54.71, 46.99},
{40.87, 32.83, 27.36, 23.49},
{20.44, 16.41, 13.68, 11.75},
{10.22, 8.21, 6.84, 5.87},
{5.11, 4.10, 3.42, 2.94},
{2.55, 2.05, 1.71, 1.47},
{1.27, 1.27, 1.27, 1.27}
};
#endif
/* Rate Table for Attack */
static void
makeDphaseARTable (void)
{
e_int32 AR, Rks, RM, RL;
#ifdef USE_SPEC_ENV_SPEED
e_uint32 attacktable[16][4];
for (RM = 0; RM < 16; RM++)
for (RL = 0; RL < 4; RL++)
{
if (RM == 0)
attacktable[RM][RL] = 0;
else if (RM == 15)
attacktable[RM][RL] = EG_DP_WIDTH;
else
attacktable[RM][RL] = (e_uint32) ((double) (1 << EG_DP_BITS) / (attacktime[RM][RL] * 3579545 / 72000));
}
#endif
for (AR = 0; AR < 16; AR++)
for (Rks = 0; Rks < 16; Rks++)
{
RM = AR + (Rks >> 2);
RL = Rks & 3;
if (RM > 15)
RM = 15;
switch (AR)
{
case 0:
dphaseARTable[AR][Rks] = 0;
break;
case 15:
dphaseARTable[AR][Rks] = 0;/*EG_DP_WIDTH;*/
break;
default:
#ifdef USE_SPEC_ENV_SPEED
dphaseARTable[AR][Rks] = RATE_ADJUST (attacktable[RM][RL]);
#else
dphaseARTable[AR][Rks] = RATE_ADJUST ((3 * (RL + 4) << (RM + 1)));
#endif
break;
}
}
}
/* Rate Table for Decay and Release */
static void
makeDphaseDRTable (void)
{
e_int32 DR, Rks, RM, RL;
#ifdef USE_SPEC_ENV_SPEED
e_uint32 decaytable[16][4];
for (RM = 0; RM < 16; RM++)
for (RL = 0; RL < 4; RL++)
if (RM == 0)
decaytable[RM][RL] = 0;
else
decaytable[RM][RL] = (e_uint32) ((double) (1 << EG_DP_BITS) / (decaytime[RM][RL] * 3579545 / 72000));
#endif
for (DR = 0; DR < 16; DR++)
for (Rks = 0; Rks < 16; Rks++)
{
RM = DR + (Rks >> 2);
RL = Rks & 3;
if (RM > 15)
RM = 15;
switch (DR)
{
case 0:
dphaseDRTable[DR][Rks] = 0;
break;
default:
#ifdef USE_SPEC_ENV_SPEED
dphaseDRTable[DR][Rks] = RATE_ADJUST (decaytable[RM][RL]);
#else
dphaseDRTable[DR][Rks] = RATE_ADJUST ((RL + 4) << (RM - 1));
#endif
break;
}
}
}
static void
makeRksTable (void)
{
e_int32 fnum8, block, KR;
for (fnum8 = 0; fnum8 < 2; fnum8++)
for (block = 0; block < 8; block++)
for (KR = 0; KR < 2; KR++)
{
if (KR != 0)
rksTable[fnum8][block][KR] = (block << 1) + fnum8;
else
rksTable[fnum8][block][KR] = block >> 1;
}
}
void
OPLL_dump2patch (const e_uint8 * dump, OPLL_PATCH * patch)
{
patch[0].AM = (dump[0] >> 7) & 1;
patch[1].AM = (dump[1] >> 7) & 1;
patch[0].PM = (dump[0] >> 6) & 1;
patch[1].PM = (dump[1] >> 6) & 1;
patch[0].EG = (dump[0] >> 5) & 1;
patch[1].EG = (dump[1] >> 5) & 1;
patch[0].KR = (dump[0] >> 4) & 1;
patch[1].KR = (dump[1] >> 4) & 1;
patch[0].ML = (dump[0]) & 15;
patch[1].ML = (dump[1]) & 15;
patch[0].KL = (dump[2] >> 6) & 3;
patch[1].KL = (dump[3] >> 6) & 3;
patch[0].TL = (dump[2]) & 63;
patch[0].FB = (dump[3]) & 7;
patch[0].WF = (dump[3] >> 3) & 1;
patch[1].WF = (dump[3] >> 4) & 1;
patch[0].AR = (dump[4] >> 4) & 15;
patch[1].AR = (dump[5] >> 4) & 15;
patch[0].DR = (dump[4]) & 15;
patch[1].DR = (dump[5]) & 15;
patch[0].SL = (dump[6] >> 4) & 15;
patch[1].SL = (dump[7] >> 4) & 15;
patch[0].RR = (dump[6]) & 15;
patch[1].RR = (dump[7]) & 15;
}
void
OPLL_getDefaultPatch (e_int32 type, e_int32 num, OPLL_PATCH * patch)
{
OPLL_dump2patch (default_inst[type] + num * 16, patch);
}
static void
makeDefaultPatch ()
{
e_int32 i, j;
for (i = 0; i < OPLL_TONE_NUM; i++)
for (j = 0; j < 19; j++)
OPLL_getDefaultPatch (i, j, &default_patch[i][j * 2]);
}
void
OPLL_setPatch (OPLL * opll, const e_uint8 * dump)
{
OPLL_PATCH patch[2];
int i;
for (i = 0; i < 19; i++)
{
OPLL_dump2patch (dump + i * 16, patch);
memcpy (&opll->patch[i*2+0], &patch[0], sizeof (OPLL_PATCH));
memcpy (&opll->patch[i*2+1], &patch[1], sizeof (OPLL_PATCH));
}
}
void
OPLL_patch2dump (const OPLL_PATCH * patch, e_uint8 * dump)
{
dump[0] = (e_uint8) ((patch[0].AM << 7) + (patch[0].PM << 6) + (patch[0].EG << 5) + (patch[0].KR << 4) + patch[0].ML);
dump[1] = (e_uint8) ((patch[1].AM << 7) + (patch[1].PM << 6) + (patch[1].EG << 5) + (patch[1].KR << 4) + patch[1].ML);
dump[2] = (e_uint8) ((patch[0].KL << 6) + patch[0].TL);
dump[3] = (e_uint8) ((patch[1].KL << 6) + (patch[1].WF << 4) + (patch[0].WF << 3) + patch[0].FB);
dump[4] = (e_uint8) ((patch[0].AR << 4) + patch[0].DR);
dump[5] = (e_uint8) ((patch[1].AR << 4) + patch[1].DR);
dump[6] = (e_uint8) ((patch[0].SL << 4) + patch[0].RR);
dump[7] = (e_uint8) ((patch[1].SL << 4) + patch[1].RR);
dump[8] = 0;
dump[9] = 0;
dump[10] = 0;
dump[11] = 0;
dump[12] = 0;
dump[13] = 0;
dump[14] = 0;
dump[15] = 0;
}
/************************************************************
Calc Parameters
************************************************************/
INLINE static e_uint32
calc_eg_dphase (OPLL_SLOT * slot)
{
switch (slot->eg_mode)
{
case ATTACK:
return dphaseARTable[slot->patch->AR][slot->rks];
case DECAY:
return dphaseDRTable[slot->patch->DR][slot->rks];
case SUSHOLD:
return 0;
case SUSTINE:
return dphaseDRTable[slot->patch->RR][slot->rks];
case RELEASE:
if (slot->sustine)
return dphaseDRTable[5][slot->rks];
else if (slot->patch->EG)
return dphaseDRTable[slot->patch->RR][slot->rks];
else
return dphaseDRTable[7][slot->rks];
case SETTLE:
return dphaseDRTable[15][0];
case FINISH:
return 0;
default:
return 0;
}
}
/*************************************************************
OPLL internal interfaces
*************************************************************/
#define SLOT_BD1 12
#define SLOT_BD2 13
#define SLOT_HH 14
#define SLOT_SD 15
#define SLOT_TOM 16
#define SLOT_CYM 17
#define UPDATE_PG(S) (S)->dphase = dphaseTable[(S)->fnum][(S)->block][(S)->patch->ML]
#define UPDATE_TLL(S)\
(((S)->type==0)?\
((S)->tll = tllTable[((S)->fnum)>>5][(S)->block][(S)->patch->TL][(S)->patch->KL]):\
((S)->tll = tllTable[((S)->fnum)>>5][(S)->block][(S)->volume][(S)->patch->KL]))
#define UPDATE_RKS(S) (S)->rks = rksTable[((S)->fnum)>>8][(S)->block][(S)->patch->KR]
#define UPDATE_WF(S) (S)->sintbl = waveform[(S)->patch->WF]
#define UPDATE_EG(S) (S)->eg_dphase = calc_eg_dphase(S)
#define UPDATE_ALL(S)\
UPDATE_PG(S);\
UPDATE_TLL(S);\
UPDATE_RKS(S);\
UPDATE_WF(S); \
UPDATE_EG(S) /* EG should be updated last. */
/* Slot key on */
INLINE static void
slotOn (OPLL_SLOT * slot)
{
slot->eg_mode = ATTACK;
slot->eg_phase = 0;
slot->phase = 0;
UPDATE_EG(slot);
}
/* Slot key on without reseting the phase */
INLINE static void
slotOn2 (OPLL_SLOT * slot)
{
slot->eg_mode = ATTACK;
slot->eg_phase = 0;
UPDATE_EG(slot);
}
/* Slot key off */
INLINE static void
slotOff (OPLL_SLOT * slot)
{
if (slot->eg_mode == ATTACK)
slot->eg_phase = EXPAND_BITS (AR_ADJUST_TABLE[HIGHBITS (slot->eg_phase, EG_DP_BITS - EG_BITS)], EG_BITS, EG_DP_BITS);
slot->eg_mode = RELEASE;
UPDATE_EG(slot);
}
/* Channel key on */
INLINE static void
keyOn (OPLL * opll, e_int32 i)
{
if (!opll->slot_on_flag[i * 2])
slotOn (MOD(opll,i));
if (!opll->slot_on_flag[i * 2 + 1])
slotOn (CAR(opll,i));
opll->key_status[i] = 1;
}
/* Channel key off */
INLINE static void
keyOff (OPLL * opll, e_int32 i)
{
if (opll->slot_on_flag[i * 2 + 1])
slotOff (CAR(opll,i));
opll->key_status[i] = 0;
}
INLINE static void
keyOn_BD (OPLL * opll)
{
keyOn (opll, 6);
}
INLINE static void
keyOn_SD (OPLL * opll)
{
if (!opll->slot_on_flag[SLOT_SD])
slotOn (CAR(opll,7));
}
INLINE static void
keyOn_TOM (OPLL * opll)
{
if (!opll->slot_on_flag[SLOT_TOM])
slotOn (MOD(opll,8));
}
INLINE static void
keyOn_HH (OPLL * opll)
{
if (!opll->slot_on_flag[SLOT_HH])
slotOn2 (MOD(opll,7));
}
INLINE static void
keyOn_CYM (OPLL * opll)
{
if (!opll->slot_on_flag[SLOT_CYM])
slotOn2 (CAR(opll,8));
}
/* Drum key off */
INLINE static void
keyOff_BD (OPLL * opll)
{
keyOff (opll, 6);
}
INLINE static void
keyOff_SD (OPLL * opll)
{
if (opll->slot_on_flag[SLOT_SD])
slotOff (CAR(opll,7));
}
INLINE static void
keyOff_TOM (OPLL * opll)
{
if (opll->slot_on_flag[SLOT_TOM])
slotOff (MOD(opll,8));
}
INLINE static void
keyOff_HH (OPLL * opll)
{
if (opll->slot_on_flag[SLOT_HH])
slotOff (MOD(opll,7));
}
INLINE static void
keyOff_CYM (OPLL * opll)
{
if (opll->slot_on_flag[SLOT_CYM])
slotOff (CAR(opll,8));
}
/* Change a voice */
INLINE static void
setPatch (OPLL * opll, e_int32 i, e_int32 num)
{
opll->patch_number[i] = num;
MOD(opll,i)->patch = &opll->patch[num * 2 + 0];
CAR(opll,i)->patch = &opll->patch[num * 2 + 1];
}
/* Change a rhythm voice */
INLINE static void
setSlotPatch (OPLL_SLOT * slot, OPLL_PATCH * patch)
{
slot->patch = patch;
}
/* Set sustine parameter */
INLINE static void
setSustine (OPLL * opll, e_int32 c, e_int32 sustine)
{
CAR(opll,c)->sustine = sustine;
if (MOD(opll,c)->type)
MOD(opll,c)->sustine = sustine;
}
/* Volume : 6bit ( Volume register << 2 ) */
INLINE static void
setVolume (OPLL * opll, e_int32 c, e_int32 volume)
{
CAR(opll,c)->volume = volume;
}
INLINE static void
setSlotVolume (OPLL_SLOT * slot, e_int32 volume)
{
slot->volume = volume;
}
/* Set F-Number ( fnum : 9bit ) */
INLINE static void
setFnumber (OPLL * opll, e_int32 c, e_int32 fnum)
{
CAR(opll,c)->fnum = fnum;
MOD(opll,c)->fnum = fnum;
}
/* Set Block data (block : 3bit ) */
INLINE static void
setBlock (OPLL * opll, e_int32 c, e_int32 block)
{
CAR(opll,c)->block = block;
MOD(opll,c)->block = block;
}
/* Change Rhythm Mode */
INLINE static void
update_rhythm_mode (OPLL * opll)
{
if (opll->patch_number[6] & 0x10)
{
if (!(opll->slot_on_flag[SLOT_BD2] | (opll->reg[0x0e] & 32)))
{
opll->slot[SLOT_BD1].eg_mode = FINISH;
opll->slot[SLOT_BD2].eg_mode = FINISH;
setPatch (opll, 6, opll->reg[0x36] >> 4);
}
}
else if (opll->reg[0x0e] & 32)
{
opll->patch_number[6] = 16;
opll->slot[SLOT_BD1].eg_mode = FINISH;
opll->slot[SLOT_BD2].eg_mode = FINISH;
setSlotPatch (&opll->slot[SLOT_BD1], &opll->patch[16 * 2 + 0]);
setSlotPatch (&opll->slot[SLOT_BD2], &opll->patch[16 * 2 + 1]);
}
if (opll->patch_number[7] & 0x10)
{
if (!((opll->slot_on_flag[SLOT_HH] && opll->slot_on_flag[SLOT_SD]) | (opll->reg[0x0e] & 32)))
{
opll->slot[SLOT_HH].type = 0;
opll->slot[SLOT_HH].eg_mode = FINISH;
opll->slot[SLOT_SD].eg_mode = FINISH;
setPatch (opll, 7, opll->reg[0x37] >> 4);
}
}
else if (opll->reg[0x0e] & 32)
{
opll->patch_number[7] = 17;
opll->slot[SLOT_HH].type = 1;
opll->slot[SLOT_HH].eg_mode = FINISH;
opll->slot[SLOT_SD].eg_mode = FINISH;
setSlotPatch (&opll->slot[SLOT_HH], &opll->patch[17 * 2 + 0]);
setSlotPatch (&opll->slot[SLOT_SD], &opll->patch[17 * 2 + 1]);
}
if (opll->patch_number[8] & 0x10)
{
if (!((opll->slot_on_flag[SLOT_CYM] && opll->slot_on_flag[SLOT_TOM]) | (opll->reg[0x0e] & 32)))
{
opll->slot[SLOT_TOM].type = 0;
opll->slot[SLOT_TOM].eg_mode = FINISH;
opll->slot[SLOT_CYM].eg_mode = FINISH;
setPatch (opll, 8, opll->reg[0x38] >> 4);
}
}
else if (opll->reg[0x0e] & 32)
{
opll->patch_number[8] = 18;
opll->slot[SLOT_TOM].type = 1;
opll->slot[SLOT_TOM].eg_mode = FINISH;
opll->slot[SLOT_CYM].eg_mode = FINISH;
setSlotPatch (&opll->slot[SLOT_TOM], &opll->patch[18 * 2 + 0]);
setSlotPatch (&opll->slot[SLOT_CYM], &opll->patch[18 * 2 + 1]);
}
}
INLINE static void
update_key_status (OPLL * opll)
{
int ch;
for (ch = 0; ch < 9; ch++)
opll->slot_on_flag[ch * 2] = opll->slot_on_flag[ch * 2 + 1] = (opll->reg[0x20 + ch]) & 0x10;
if (opll->reg[0x0e] & 32)
{
opll->slot_on_flag[SLOT_BD1] |= (opll->reg[0x0e] & 0x10);
opll->slot_on_flag[SLOT_BD2] |= (opll->reg[0x0e] & 0x10);
opll->slot_on_flag[SLOT_SD] |= (opll->reg[0x0e] & 0x08);
opll->slot_on_flag[SLOT_HH] |= (opll->reg[0x0e] & 0x01);
opll->slot_on_flag[SLOT_TOM] |= (opll->reg[0x0e] & 0x04);
opll->slot_on_flag[SLOT_CYM] |= (opll->reg[0x0e] & 0x02);
}
}
void
OPLL_copyPatch (OPLL * opll, e_int32 num, OPLL_PATCH * patch)
{
memcpy (&opll->patch[num], patch, sizeof (OPLL_PATCH));
}
/***********************************************************
Initializing
***********************************************************/
static void
OPLL_SLOT_reset (OPLL_SLOT * slot, int type)
{
slot->type = type;
slot->sintbl = waveform[0];
slot->phase = 0;
slot->dphase = 0;
slot->output[0] = 0;
slot->output[1] = 0;
slot->feedback = 0;
slot->eg_mode = FINISH;
slot->eg_phase = EG_DP_WIDTH;
slot->eg_dphase = 0;
slot->rks = 0;
slot->tll = 0;
slot->sustine = 0;
slot->fnum = 0;
slot->block = 0;
slot->volume = 0;
slot->pgout = 0;
slot->egout = 0;
slot->patch = &null_patch;
}
static void
internal_refresh (void)
{
makeDphaseTable ();
makeDphaseARTable ();
makeDphaseDRTable ();
pm_dphase = (e_uint32) RATE_ADJUST (PM_SPEED * PM_DP_WIDTH / (clk / 72));
am_dphase = (e_uint32) RATE_ADJUST (AM_SPEED * AM_DP_WIDTH / (clk / 72));
}
static void
maketables (e_uint32 c, e_uint32 r)
{
if (c != clk)
{
clk = c;
makePmTable ();
makeAmTable ();
makeDB2LinTable ();
makeAdjustTable ();
makeTllTable ();
makeRksTable ();
makeSinTable ();
makeDefaultPatch ();
}
if (r != rate)
{
rate = r;
internal_refresh ();
}
}
OPLL *
OPLL_new (e_uint32 clk, e_uint32 rate)
{
OPLL *opll;
e_int32 i;
maketables (clk, rate);
opll = (OPLL *) calloc (sizeof (OPLL), 1);
if (opll == NULL)
return NULL;
for (i = 0; i < 19 * 2; i++)
memcpy(&opll->patch[i],&null_patch,sizeof(OPLL_PATCH));
opll->mask = 0;
OPLL_reset (opll);
OPLL_reset_patch (opll, 0);
return opll;
}