-
Notifications
You must be signed in to change notification settings - Fork 18
/
emu2413.c
1710 lines (1461 loc) · 39.7 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-2019
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).
2015 12-13 : Version 0.62 -- Changed own integer types to C99 stdint.h types.
2016 09-06 : Version 0.63 -- Support per-channel output.
2019 05-24 : Version 0.65 -- Fix YM2413 and VRC7 patches.
2019 10-13 : Version 0.70 -- Force to dump before keyon
-- Dump size changed from to 8 bytes per voice.
-- Replaced snare, hi-hat, top-cym generator,
with reference to Jarek Burczynski's ymf262.c.
References:
fmopl.c -- 1999,2000 written by Tatsuyuki Satoh (MAME development).
fmopl.c(fixed) -- (C) 2002 Jarek Burczynski.
ymf262.c -- Jarek Burczynski.
s_opl.c -- 2001 written by Mamiya (NEZplug development).
fmgen.cpp -- 1999,2000 written by cisc.
fmpac.ill -- 2000 created by NARUTO.
VRC7 tones -- https://siliconpr0n.org/archive/doku.php?id=vendor:yamaha:opl2#opll_vrc7_patch_format
MSX-Datapack
YMU757 data sheet
YM2143 data sheet
**************************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "emu2413.h"
#define OPLL_TONE_NUM 3
static uint8_t default_inst[OPLL_TONE_NUM][(16 + 3) * 8] = {
{
#include "2413tone.h"
},
{
#include "vrc7tone.h"
},
{
#include "281btone.h"
}
};
/* 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)*(int32_t)(EG_STEP/DB_STEP))
#define TL2EG(d) ((d)*(int32_t)(TL_STEP/EG_STEP))
#define SL2EG(d) ((d)*(int32_t)(SL_STEP/EG_STEP))
#define DB_POS(x) (uint32_t)((x)/DB_STEP)
#define DB_NEG(x) (uint32_t)(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.068835788302951
#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:(uint32_t)((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 uint32_t clk = 844451141;
/* Sampling rate */
static uint32_t rate = 3354932;
/* WaveTable for each envelope amp */
static uint16_t fullsintable[PG_WIDTH];
static uint16_t halfsintable[PG_WIDTH];
static uint16_t *waveform[2] = { fullsintable, halfsintable };
/* LFO Table */
static int32_t pmtable[PM_PG_WIDTH];
static int32_t amtable[AM_PG_WIDTH];
/* Phase delta for LFO */
static uint32_t pm_dphase;
static uint32_t am_dphase;
/* dB to Liner table */
static int16_t DB2LIN_TABLE[(DB_MUTE + DB_MUTE) * 2];
/* Liner to Log curve conversion table (for Attack rate). */
static uint16_t 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 uint32_t dphaseARTable[16][16];
/* Phase incr table for Decay and Release */
static uint32_t dphaseDRTable[16][16];
/* KSL + TL Table */
static uint32_t tllTable[16][8][1 << TL_BITS][4];
static int32_t rksTable[2][8][2];
/* Phase incr table for PG */
static uint32_t dphaseTable[512][8][16];
/***************************************************
Create tables
****************************************************/
static inline int32_t
Min (int32_t i, int32_t j)
{
if (i < j)
return i;
else
return j;
}
/* Table for AR to LogCurve. */
static void
makeAdjustTable (void)
{
int32_t i;
AR_ADJUST_TABLE[0] = (1 << EG_BITS) - 1;
for (i = 1; i < (1<<EG_BITS); i++)
AR_ADJUST_TABLE[i] = (uint16_t) ((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)
{
int32_t i;
for (i = 0; i < DB_MUTE + DB_MUTE; i++)
{
DB2LIN_TABLE[i] = (int16_t) ((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] = (int16_t) (-DB2LIN_TABLE[i]);
}
}
/* Liner(+0.0 - +1.0) to dB((1<<DB_BITS) - 1 -- 0) */
static int32_t
lin2db (double d)
{
if (d == 0)
return (DB_MUTE - 1);
else
return Min (-(int32_t) (20.0 * log10 (d) / DB_STEP), DB_MUTE-1); /* 0 -- 127 */
}
/* Sin Table */
static void
makeSinTable (void)
{
int32_t i;
for (i = 0; i < PG_WIDTH / 4; i++)
{
fullsintable[i] = (uint32_t) 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] = (uint32_t) (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)
{
int32_t i;
for (i = 0; i < PM_PG_WIDTH; i++)
pmtable[i] = (int32_t) ((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)
{
int32_t i;
for (i = 0; i < AM_PG_WIDTH; i++)
amtable[i] = (int32_t) ((double) AM_DEPTH / 2 / DB_STEP * (1.0 + saw (2.0 * PI * i / PM_PG_WIDTH)));
}
/* Phase increment counter table */
static void
makeDphaseTable (void)
{
uint32_t fnum, block, ML;
uint32_t 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)
};
int32_t tmp;
int32_t 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 = (int32_t) (kltable[fnum] - dB2 (3.000) * (7 - block));
if (tmp <= 0)
tllTable[fnum][block][TL][KL] = TL2EG (TL);
else
tllTable[fnum][block][TL][KL] = (uint32_t) ((tmp >> (3 - KL)) / EG_STEP) + TL2EG (TL);
}
}
}
/* Rate Table for Attack */
static void
makeDphaseARTable (void)
{
int32_t AR, Rks, RM, RL;
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:
dphaseARTable[AR][Rks] = RATE_ADJUST ((3 * (RL + 4) << (RM + 1)));
break;
}
}
}
/* Rate Table for Decay and Release */
static void
makeDphaseDRTable (void)
{
int32_t DR, Rks, RM, RL;
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:
dphaseDRTable[DR][Rks] = RATE_ADJUST ((RL + 4) << (RM - 1));
break;
}
}
}
static void
makeRksTable (void)
{
int32_t 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 uint8_t * 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 (int32_t type, int32_t num, OPLL_PATCH * patch)
{
OPLL_dump2patch (default_inst[type] + num * 8, patch);
}
static void
makeDefaultPatch ()
{
int32_t 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 uint8_t * dump)
{
OPLL_PATCH patch[2];
int i;
for (i = 0; i < 19; i++)
{
OPLL_dump2patch (dump + i * 8, 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, uint8_t * dump)
{
dump[0] = (uint8_t) ((patch[0].AM << 7) + (patch[0].PM << 6) + (patch[0].EG << 5) + (patch[0].KR << 4) + patch[0].ML);
dump[1] = (uint8_t) ((patch[1].AM << 7) + (patch[1].PM << 6) + (patch[1].EG << 5) + (patch[1].KR << 4) + patch[1].ML);
dump[2] = (uint8_t) ((patch[0].KL << 6) + patch[0].TL);
dump[3] = (uint8_t) ((patch[1].KL << 6) + (patch[1].WF << 4) + (patch[0].WF << 3) + patch[0].FB);
dump[4] = (uint8_t) ((patch[0].AR << 4) + patch[0].DR);
dump[5] = (uint8_t) ((patch[1].AR << 4) + patch[1].DR);
dump[6] = (uint8_t) ((patch[0].SL << 4) + patch[0].RR);
dump[7] = (uint8_t) ((patch[1].SL << 4) + patch[1].RR);
}
/************************************************************
Calc Parameters
************************************************************/
static inline uint32_t
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[14][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 */
static inline void
slotOn (OPLL_SLOT * slot)
{
slot->eg_mode = SETTLE;
UPDATE_EG(slot);
}
/* Slot key off */
static inline 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 */
static inline void
keyOn (OPLL * opll, int32_t 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 */
static inline void
keyOff (OPLL * opll, int32_t i)
{
if (opll->slot_on_flag[i * 2 + 1])
slotOff (CAR(opll,i));
opll->key_status[i] = 0;
}
static inline void
keyOn_BD (OPLL * opll)
{
keyOn (opll, 6);
}
static inline void
keyOn_SD (OPLL * opll)
{
if (!opll->slot_on_flag[SLOT_SD])
slotOn (CAR(opll,7));
}
static inline void
keyOn_TOM (OPLL * opll)
{
if (!opll->slot_on_flag[SLOT_TOM])
slotOn (MOD(opll,8));
}
static inline void
keyOn_HH (OPLL * opll)
{
if (!opll->slot_on_flag[SLOT_HH])
slotOn (MOD(opll,7));
}
static inline void
keyOn_CYM (OPLL * opll)
{
if (!opll->slot_on_flag[SLOT_CYM])
slotOn (CAR(opll,8));
}
/* Drum key off */
static inline void
keyOff_BD (OPLL * opll)
{
keyOff (opll, 6);
}
static inline void
keyOff_SD (OPLL * opll)
{
if (opll->slot_on_flag[SLOT_SD])
slotOff (CAR(opll,7));
}
static inline void
keyOff_TOM (OPLL * opll)
{
if (opll->slot_on_flag[SLOT_TOM])
slotOff (MOD(opll,8));
}
static inline void
keyOff_HH (OPLL * opll)
{
if (opll->slot_on_flag[SLOT_HH])
slotOff (MOD(opll,7));
}
static inline void
keyOff_CYM (OPLL * opll)
{
if (opll->slot_on_flag[SLOT_CYM])
slotOff (CAR(opll,8));
}
/* Change a voice */
static inline void
setPatch (OPLL * opll, int32_t i, int32_t 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 */
static inline void
setSlotPatch (OPLL_SLOT * slot, OPLL_PATCH * patch)
{
slot->patch = patch;
}
/* Set sustine parameter */
static inline void
setSustine (OPLL * opll, int32_t c, int32_t sustine)
{
CAR(opll,c)->sustine = sustine;
if (MOD(opll,c)->type)
MOD(opll,c)->sustine = sustine;
}
/* Volume : 6bit ( Volume register << 2 ) */
static inline void
setVolume (OPLL * opll, int32_t c, int32_t volume)
{
CAR(opll,c)->volume = volume;
}
static inline void
setSlotVolume (OPLL_SLOT * slot, int32_t volume)
{
slot->volume = volume;
}
/* Set F-Number ( fnum : 9bit ) */
static inline void
setFnumber (OPLL * opll, int32_t c, int32_t fnum)
{
CAR(opll,c)->fnum = fnum;
MOD(opll,c)->fnum = fnum;
}
/* Set Block data (block : 3bit ) */
static inline void
setBlock (OPLL * opll, int32_t c, int32_t block)
{
CAR(opll,c)->block = block;
MOD(opll,c)->block = block;
}
/* Change Rhythm Mode */
static inline 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]);
}
}
static inline 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, int32_t 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 = (uint32_t) RATE_ADJUST (PM_SPEED * PM_DP_WIDTH / (clk / 72));
am_dphase = (uint32_t) RATE_ADJUST (AM_SPEED * AM_DP_WIDTH / (clk / 72));
}
static void
maketables (uint32_t c, uint32_t r)
{
if (c != clk)
{
clk = c;
makePmTable ();
makeAmTable ();
makeDB2LinTable ();
makeAdjustTable ();
makeTllTable ();
makeRksTable ();
makeSinTable ();
makeDefaultPatch ();
}
if (r != rate)
{
rate = r;
internal_refresh ();
}
}
OPLL *
OPLL_new (uint32_t clk, uint32_t rate)
{
OPLL *opll;
int32_t 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;
}
void
OPLL_delete (OPLL * opll)
{
free (opll);
}
/* Reset patch datas by system default. */
void
OPLL_reset_patch (OPLL * opll, int32_t type)
{
int32_t i;
for (i = 0; i < 19 * 2; i++)
OPLL_copyPatch (opll, i, &default_patch[type % OPLL_TONE_NUM][i]);
}
/* Reset whole of OPLL except patch datas. */
void
OPLL_reset (OPLL * opll)
{
int32_t i;
if (!opll)
return;
opll->adr = 0;
opll->out = 0;
opll->pm_phase = 0;
opll->am_phase = 0;
opll->noise_seed = 0xffff;
opll->mask = 0;
for (i = 0; i <18; i++)
OPLL_SLOT_reset(&opll->slot[i], i%2);
for (i = 0; i < 9; i++)
{
opll->key_status[i] = 0;
setPatch (opll, i, 0);
}
for (i = 0; i < 0x40; i++)
OPLL_writeReg (opll, i, 0);
opll->realstep = (uint32_t) ((1 << 31) / rate);
opll->opllstep = (uint32_t) ((1 << 31) / (clk / 72));
opll->oplltime = 0;
for (i = 0; i < 14; i++)
opll->pan[i] = 2;
}
/* Force Refresh (When external program changes some parameters). */
void
OPLL_forceRefresh (OPLL * opll)
{
int32_t i;
if (opll == NULL)
return;
for (i = 0; i < 9; i++)
setPatch(opll,i,opll->patch_number[i]);
for (i = 0; i < 18; i++)
{
UPDATE_PG (&opll->slot[i]);
UPDATE_RKS (&opll->slot[i]);
UPDATE_TLL (&opll->slot[i]);
UPDATE_WF (&opll->slot[i]);
UPDATE_EG (&opll->slot[i]);
}
}
void
OPLL_set_rate (OPLL * opll, uint32_t r)
{
if (opll->quality)
rate = 49716;
else
rate = r;
internal_refresh ();
rate = r;
}