-
Notifications
You must be signed in to change notification settings - Fork 0
/
emu2413.cpp
1332 lines (1121 loc) · 33.9 KB
/
emu2413.cpp
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 v1.5.7
* https://github.com/digital-sound-antiques/emu2413
* Copyright (C) 2020 Mitsutaka Okazaki
*
* This source refers to the following documents. The author would like to thank all the authors who have
* contributed to the writing of them.
* - [YM2413 notes](http://www.smspower.org/Development/YM2413) by andete
* - ymf262.c by Jarek Burczynski
* - [VRC7 presets](https://siliconpr0n.org/archive/doku.php?id=vendor:yamaha:opl2#opll_vrc7_patch_format) by Nuke.YKT
* - YMF281B presets by Chabin
*/
#include "emu2413.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef INLINE
#if defined(_MSC_VER)
#define INLINE __inline
#elif defined(__GNUC__)
#define INLINE __inline__
#else
#define INLINE inline
#endif
#endif
// Only 2 channels for now!
// Panic if it is not 2 channels!
static int16_t rateConvBuffer[2][LW];
static int16_t* rateConvBufferChannels[2] = { rateConvBuffer[0], rateConvBuffer[1] };
static uint16_t halfsin_table[PG_WIDTH];
static uint16_t *wave_table_map[2] = {fullsin_table, halfsin_table};
enum __OPLL_EG_STATE { ATTACK, DECAY, SUSTAIN, RELEASE, DAMP, UNKNOWN };
//static uint32_t tll_table[8 * 16][1 << TL_BITS][4];
static uint32_t (*tll_table)[1 << TL_BITS][4];
static int32_t rks_table[8 * 2][2];
static OPLL_PATCH null_patch = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
static OPLL_PATCH default_patch[OPLL_TONE_NUM][(16 + 3) * 2];
/* don't forget min/max is defined as a macro in stdlib.h of Visual C. */
#ifndef min
static INLINE int min(int i, int j) {
return (i < j) ? i : j;
}
#endif
#ifndef max
static INLINE int max(int i, int j) {
return (i > j) ? i : j;
}
#endif
/* f_inp: input frequency. f_out: output frequencey, ch: number of channels */
void OPLL_RateConv_new(OPLL_RateConv* conv, double f_inp, double f_out, int ch) {
int i;
conv->ch = ch;
conv->f_ratio = f_inp / f_out;
//conv->buf = malloc(sizeof(void *) * ch);
// Make sure ch = 2!
// Anything other than that, do not compile!!
if (conv->ch != 2) panic("conv->ch must be only 2 for now!");
conv->buf = rateConvBufferChannels;
//for (i = 0; i < ch; i++) {
//conv->buf[i] = malloc(sizeof(conv->buf[0][0]) * LW);
//rateConvBufferChannels[i] = rateConvBuffer[i];
//}
/* create sinc_table for positive 0 <= x < LW/2 */
static preCalculateSincTable<int16_t, sizeof(int16_t) * SINC_RESO * LW / 2> sinc_tbl = preCalculateSincTable<int16_t, sizeof(int16_t) * SINC_RESO * LW / 2>(f_inp,f_out);
conv->sinc_table = sinc_tbl.table;
// conv->sinc_table = malloc(sizeof(conv->sinc_table[0]) * SINC_RESO * LW / 2);
// for (i = 0; i < SINC_RESO * LW / 2; i++) {
// const double x = (double)i / SINC_RESO;
// if (f_out < f_inp) {
// /* for downsampling */
// conv->sinc_table[i] = (int16_t)((1 << SINC_AMP_BITS) * windowed_sinc(x / conv->f_ratio) / conv->f_ratio);
// } else {
// /* for upsampling */
// conv->sinc_table[i] = (int16_t)((1 << SINC_AMP_BITS) * windowed_sinc(x));
// }
// }
//return conv;
}
static INLINE int16_t lookup_sinc_table(int16_t *table, double x) {
int16_t index = (int16_t)(x * SINC_RESO);
if (index < 0)
index = -index;
return table[min(SINC_RESO * LW / 2 - 1, index)];
}
void OPLL_RateConv_reset(OPLL_RateConv *conv) {
int i;
conv->timer = 0;
for (i = 0; i < conv->ch; i++) {
memset(conv->buf[i], 0, sizeof(conv->buf[i][0]) * LW);
}
}
/* put original data to this converter at f_inp. */
void OPLL_RateConv_putData(OPLL_RateConv *conv, int ch, int16_t data) {
int16_t *buf = conv->buf[ch];
int i;
for (i = 0; i < LW - 1; i++) {
buf[i] = buf[i + 1];
}
buf[LW - 1] = data;
}
/* get resampled data from this converter at f_out. */
/* this function must be called f_out / f_inp times per one putData call. */
int16_t OPLL_RateConv_getData(OPLL_RateConv *conv, int ch)
{
int16_t *buf = conv->buf[ch];
int32_t sum = 0;
int k;
double dn;
conv->timer += conv->f_ratio;
dn = conv->timer - floor(conv->timer);
conv->timer = dn;
#if USE_FAST_RATE_CONV
return (buf[0] + buf[1] + buf[2]) / 3;
#else
for (k = 0; k < LW; k++)
{
double x = ((double)k - (LW / 2 - 1)) - dn;
sum += buf[k] * lookup_sinc_table(conv->sinc_table, x);
}
return sum >> SINC_AMP_BITS;
#endif
}
// void OPLL_RateConv_delete(OPLL_RateConv *conv) {
// int i;
// for (i = 0; i < conv->ch; i++) {
// free(conv->buf[i]);
// }
// free(conv->buf);
// free(conv->sinc_table);
// free(conv);
// }
/***************************************************
Create tables
****************************************************/
static void makeSinTable(void) {
int x;
for (x = 0; x < PG_WIDTH / 4; x++) {
fullsin_table[PG_WIDTH / 4 + x] = fullsin_table[PG_WIDTH / 4 - x - 1];
}
for (x = 0; x < PG_WIDTH / 2; x++) {
fullsin_table[PG_WIDTH / 2 + x] = 0x8000 | fullsin_table[x];
}
for (x = 0; x < PG_WIDTH / 2; x++)
halfsin_table[x] = fullsin_table[x];
for (x = PG_WIDTH / 2; x < PG_WIDTH; x++)
halfsin_table[x] = 0xfff;
}
static void makeTllTable(void) {
static preCalculateTllTable<uint32_t> tllTable = preCalculateTllTable<uint32_t>();
tll_table = tllTable.table;
// 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) {
// tll_table[(block << 4) | fnum][TL][KL] = TL2EG(TL);
// } else {
// tmp = (int32_t)(kl_table[fnum] - dB2(3.000) * (7 - block));
// if (tmp <= 0)
// tll_table[(block << 4) | fnum][TL][KL] = TL2EG(TL);
// else
// tll_table[(block << 4) | fnum][TL][KL] = (uint32_t)((tmp >> (3 - KL)) / EG_STEP) + TL2EG(TL);
// }
// }
// }
// }
// }
}
static void makeRksTable(void) {
int fnum8, block;
for (fnum8 = 0; fnum8 < 2; fnum8++)
for (block = 0; block < 8; block++) {
rks_table[(block << 1) | fnum8][1] = (block << 1) + fnum8;
rks_table[(block << 1) | fnum8][0] = block >> 1;
}
}
static void makeDefaultPatch() {
int 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]);
}
static uint8_t table_initialized = 0;
static void initializeTables() {
makeTllTable();
makeRksTable();
makeSinTable();
makeDefaultPatch();
table_initialized = 1;
}
/*********************************************************
Synthesizing
*********************************************************/
#define SLOT_BD1 12
#define SLOT_BD2 13
#define SLOT_HH 14
#define SLOT_SD 15
#define SLOT_TOM 16
#define SLOT_CYM 17
/* utility macros */
#define MOD(o, x) (&(o)->slot[(x) << 1])
#define CAR(o, x) (&(o)->slot[((x) << 1) | 1])
#define BIT(s, b) (((s) >> (b)) & 1)
#if OPLL_DEBUG
static void _debug_print_patch(OPLL_SLOT *slot) {
OPLL_PATCH *p = slot->patch;
printf("[slot#%d am:%d pm:%d eg:%d kr:%d ml:%d kl:%d tl:%d ws:%d fb:%d A:%d D:%d S:%d R:%d]\n", slot->number, //
p->AM, p->PM, p->EG, p->KR, p->ML, //
p->KL, p->TL, p->WS, p->FB, //
p->AR, p->DR, p->SL, p->RR);
}
static char *_debug_eg_state_name(OPLL_SLOT *slot) {
switch (slot->eg_state) {
case ATTACK:
return "attack";
case DECAY:
return "decay";
case SUSTAIN:
return "sustain";
case RELEASE:
return "release";
case DAMP:
return "damp";
default:
return "unknown";
}
}
static INLINE void _debug_print_slot_info(OPLL_SLOT *slot) {
char *name = _debug_eg_state_name(slot);
printf("[slot#%d state:%s fnum:%03x rate:%d-%d]\n", slot->number, name, slot->blk_fnum, slot->eg_rate_h,
slot->eg_rate_l);
_debug_print_patch(slot);
fflush(stdout);
}
#endif
static INLINE int get_parameter_rate(OPLL_SLOT *slot) {
if ((slot->type & 1) == 0 && slot->key_flag == 0) {
return 0;
}
switch (slot->eg_state) {
case ATTACK:
return slot->patch->AR;
case DECAY:
return slot->patch->DR;
case SUSTAIN:
return slot->patch->EG ? 0 : slot->patch->RR;
case RELEASE:
if (slot->sus_flag) {
return 5;
} else if (slot->patch->EG) {
return slot->patch->RR;
} else {
return 7;
}
case DAMP:
return DAMPER_RATE;
default:
return 0;
}
}
enum SLOT_UPDATE_FLAG {
UPDATE_WS = 1,
UPDATE_TLL = 2,
UPDATE_RKS = 4,
UPDATE_EG = 8,
UPDATE_ALL = 255,
};
static INLINE void request_update(OPLL_SLOT *slot, int flag) { slot->update_requests |= flag; }
static void commit_slot_update(OPLL_SLOT *slot) {
#if OPLL_DEBUG
if (slot->last_eg_state != slot->eg_state) {
_debug_print_slot_info(slot);
slot->last_eg_state = slot->eg_state;
}
#endif
if (slot->update_requests & UPDATE_WS) {
slot->wave_table = wave_table_map[slot->patch->WS];
}
if (slot->update_requests & UPDATE_TLL) {
if ((slot->type & 1) == 0) {
slot->tll = tll_table[slot->blk_fnum >> 5][slot->patch->TL][slot->patch->KL];
} else {
slot->tll = tll_table[slot->blk_fnum >> 5][slot->volume][slot->patch->KL];
}
}
if (slot->update_requests & UPDATE_RKS) {
slot->rks = rks_table[slot->blk_fnum >> 8][slot->patch->KR];
}
if (slot->update_requests & (UPDATE_RKS | UPDATE_EG)) {
int p_rate = get_parameter_rate(slot);
if (p_rate == 0) {
slot->eg_shift = 0;
slot->eg_rate_h = 0;
slot->eg_rate_l = 0;
return;
}
slot->eg_rate_h = min(15, p_rate + (slot->rks >> 2));
slot->eg_rate_l = slot->rks & 3;
if (slot->eg_state == ATTACK) {
slot->eg_shift = (0 < slot->eg_rate_h && slot->eg_rate_h < 12) ? (13 - slot->eg_rate_h) : 0;
} else {
slot->eg_shift = (slot->eg_rate_h < 13) ? (13 - slot->eg_rate_h) : 0;
}
}
slot->update_requests = 0;
}
static void reset_slot(OPLL_SLOT *slot, int number) {
slot->number = number;
slot->type = number % 2;
slot->pg_keep = 0;
slot->wave_table = wave_table_map[0];
slot->pg_phase = 0;
slot->output[0] = 0;
slot->output[1] = 0;
slot->eg_state = RELEASE;
slot->eg_shift = 0;
slot->rks = 0;
slot->tll = 0;
slot->key_flag = 0;
slot->sus_flag = 0;
slot->blk_fnum = 0;
slot->blk = 0;
slot->fnum = 0;
slot->volume = 0;
slot->pg_out = 0;
slot->eg_out = EG_MUTE;
slot->patch = &null_patch;
}
static INLINE void slotOn(OPLL *opll, int i) {
OPLL_SLOT *slot = &opll->slot[i];
slot->key_flag = 1;
slot->eg_state = DAMP;
request_update(slot, UPDATE_EG);
}
static INLINE void slotOff(OPLL *opll, int i) {
OPLL_SLOT *slot = &opll->slot[i];
slot->key_flag = 0;
if (slot->type & 1) {
slot->eg_state = RELEASE;
request_update(slot, UPDATE_EG);
}
}
static INLINE void update_key_status(OPLL *opll) {
const uint8_t r14 = opll->reg[0x0e];
const uint8_t rhythm_mode = BIT(r14, 5);
uint32_t new_slot_key_status = 0;
uint32_t updated_status;
int ch;
for (ch = 0; ch < 9; ch++)
if (opll->reg[0x20 + ch] & 0x10)
new_slot_key_status |= 3 << (ch * 2);
if (rhythm_mode) {
if (r14 & 0x10)
new_slot_key_status |= 3 << SLOT_BD1;
if (r14 & 0x01)
new_slot_key_status |= 1 << SLOT_HH;
if (r14 & 0x08)
new_slot_key_status |= 1 << SLOT_SD;
if (r14 & 0x04)
new_slot_key_status |= 1 << SLOT_TOM;
if (r14 & 0x02)
new_slot_key_status |= 1 << SLOT_CYM;
}
updated_status = opll->slot_key_status ^ new_slot_key_status;
if (updated_status) {
int i;
for (i = 0; i < 18; i++)
if (BIT(updated_status, i)) {
if (BIT(new_slot_key_status, i)) {
slotOn(opll, i);
} else {
slotOff(opll, i);
}
}
}
opll->slot_key_status = new_slot_key_status;
}
static INLINE void set_patch(OPLL *opll, int32_t ch, int32_t num) {
opll->patch_number[ch] = num;
MOD(opll, ch)->patch = &opll->patch[num * 2 + 0];
CAR(opll, ch)->patch = &opll->patch[num * 2 + 1];
request_update(MOD(opll, ch), UPDATE_ALL);
request_update(CAR(opll, ch), UPDATE_ALL);
}
static INLINE void set_sus_flag(OPLL *opll, int ch, int flag) {
CAR(opll, ch)->sus_flag = flag;
request_update(CAR(opll, ch), UPDATE_EG);
if (MOD(opll, ch)->type & 1) {
MOD(opll, ch)->sus_flag = flag;
request_update(MOD(opll, ch), UPDATE_EG);
}
}
/* set volume ( volume : 6bit, register value << 2 ) */
static INLINE void set_volume(OPLL *opll, int ch, int volume) {
CAR(opll, ch)->volume = volume;
request_update(CAR(opll, ch), UPDATE_TLL);
}
static INLINE void set_slot_volume(OPLL_SLOT *slot, int volume) {
slot->volume = volume;
request_update(slot, UPDATE_TLL);
}
/* set f-Nnmber ( fnum : 9bit ) */
static INLINE void set_fnumber(OPLL *opll, int ch, int fnum) {
OPLL_SLOT *car = CAR(opll, ch);
OPLL_SLOT *mod = MOD(opll, ch);
car->fnum = fnum;
car->blk_fnum = (car->blk_fnum & 0xe00) | (fnum & 0x1ff);
mod->fnum = fnum;
mod->blk_fnum = (mod->blk_fnum & 0xe00) | (fnum & 0x1ff);
request_update(car, UPDATE_EG | UPDATE_RKS | UPDATE_TLL);
request_update(mod, UPDATE_EG | UPDATE_RKS | UPDATE_TLL);
}
/* set block data (blk : 3bit ) */
static INLINE void set_block(OPLL *opll, int ch, int blk) {
OPLL_SLOT *car = CAR(opll, ch);
OPLL_SLOT *mod = MOD(opll, ch);
car->blk = blk;
car->blk_fnum = ((blk & 7) << 9) | (car->blk_fnum & 0x1ff);
mod->blk = blk;
mod->blk_fnum = ((blk & 7) << 9) | (mod->blk_fnum & 0x1ff);
request_update(car, UPDATE_EG | UPDATE_RKS | UPDATE_TLL);
request_update(mod, UPDATE_EG | UPDATE_RKS | UPDATE_TLL);
}
static INLINE void update_rhythm_mode(OPLL *opll) {
const uint8_t new_rhythm_mode = (opll->reg[0x0e] >> 5) & 1;
if (opll->rhythm_mode != new_rhythm_mode) {
if (new_rhythm_mode) {
opll->slot[SLOT_HH].type = 3;
opll->slot[SLOT_HH].pg_keep = 1;
opll->slot[SLOT_SD].type = 3;
opll->slot[SLOT_TOM].type = 3;
opll->slot[SLOT_CYM].type = 3;
opll->slot[SLOT_CYM].pg_keep = 1;
set_patch(opll, 6, 16);
set_patch(opll, 7, 17);
set_patch(opll, 8, 18);
set_slot_volume(&opll->slot[SLOT_HH], ((opll->reg[0x37] >> 4) & 15) << 2);
set_slot_volume(&opll->slot[SLOT_TOM], ((opll->reg[0x38] >> 4) & 15) << 2);
} else {
opll->slot[SLOT_HH].type = 0;
opll->slot[SLOT_HH].pg_keep = 0;
opll->slot[SLOT_SD].type = 1;
opll->slot[SLOT_TOM].type = 0;
opll->slot[SLOT_CYM].type = 1;
opll->slot[SLOT_CYM].pg_keep = 0;
set_patch(opll, 6, opll->reg[0x36] >> 4);
set_patch(opll, 7, opll->reg[0x37] >> 4);
set_patch(opll, 8, opll->reg[0x38] >> 4);
}
}
opll->rhythm_mode = new_rhythm_mode;
}
static void update_ampm(OPLL *opll) {
if (opll->test_flag & 2) {
opll->pm_phase = 0;
opll->am_phase = 0;
} else {
opll->pm_phase += (opll->test_flag & 8) ? 1024 : 1;
opll->am_phase += (opll->test_flag & 8) ? 64 : 1;
}
opll->lfo_am = am_table[(opll->am_phase >> 6) % sizeof(am_table)];
}
static void update_noise(OPLL *opll, int cycle) {
int i;
for (i = 0; i < cycle; i++) {
if (opll->noise & 1) {
opll->noise ^= 0x800200;
}
opll->noise >>= 1;
}
}
static void update_short_noise(OPLL *opll) {
const uint32_t pg_hh = opll->slot[SLOT_HH].pg_out;
const uint32_t pg_cym = opll->slot[SLOT_CYM].pg_out;
const uint8_t h_bit2 = BIT(pg_hh, PG_BITS - 8);
const uint8_t h_bit7 = BIT(pg_hh, PG_BITS - 3);
const uint8_t h_bit3 = BIT(pg_hh, PG_BITS - 7);
const uint8_t c_bit3 = BIT(pg_cym, PG_BITS - 7);
const uint8_t c_bit5 = BIT(pg_cym, PG_BITS - 5);
opll->short_noise = (h_bit2 ^ h_bit7) | (h_bit3 ^ c_bit5) | (c_bit3 ^ c_bit5);
}
static INLINE void calc_phase(OPLL_SLOT *slot, int32_t pm_phase, uint8_t reset) {
const int8_t pm = slot->patch->PM ? pm_table[(slot->fnum >> 6) & 7][(pm_phase >> 10) & 7] : 0;
if (reset) {
slot->pg_phase = 0;
}
slot->pg_phase += (((slot->fnum & 0x1ff) * 2 + pm) * ml_table[slot->patch->ML]) << slot->blk >> 2;
slot->pg_phase &= (DP_WIDTH - 1);
slot->pg_out = slot->pg_phase >> DP_BASE_BITS;
}
static INLINE uint8_t lookup_attack_step(OPLL_SLOT *slot, uint32_t counter) {
int index;
switch (slot->eg_rate_h) {
case 12:
index = (counter & 0xc) >> 1;
return 4 - eg_step_tables[slot->eg_rate_l][index];
case 13:
index = (counter & 0xc) >> 1;
return 3 - eg_step_tables[slot->eg_rate_l][index];
case 14:
index = (counter & 0xc) >> 1;
return 2 - eg_step_tables[slot->eg_rate_l][index];
case 0:
case 15:
return 0;
default:
index = counter >> slot->eg_shift;
return eg_step_tables[slot->eg_rate_l][index & 7] ? 4 : 0;
}
}
static INLINE uint8_t lookup_decay_step(OPLL_SLOT *slot, uint32_t counter) {
int index;
switch (slot->eg_rate_h) {
case 0:
return 0;
case 13:
index = ((counter & 0xc) >> 1) | (counter & 1);
return eg_step_tables[slot->eg_rate_l][index];
case 14:
index = ((counter & 0xc) >> 1);
return eg_step_tables[slot->eg_rate_l][index] + 1;
case 15:
return 2;
default:
index = counter >> slot->eg_shift;
return eg_step_tables[slot->eg_rate_l][index & 7];
}
}
static INLINE void start_envelope(OPLL_SLOT *slot) {
if (min(15, slot->patch->AR + (slot->rks >> 2)) == 15) {
slot->eg_state = DECAY;
slot->eg_out = 0;
} else {
slot->eg_state = ATTACK;
slot->eg_out = EG_MUTE;
}
request_update(slot, UPDATE_EG);
}
static INLINE void calc_envelope(OPLL_SLOT *slot, OPLL_SLOT *buddy, uint16_t eg_counter, uint8_t test) {
uint32_t mask = (1 << slot->eg_shift) - 1;
uint8_t s;
if (slot->eg_state == ATTACK) {
if (0 < slot->eg_out && 0 < slot->eg_rate_h && (eg_counter & mask & ~3) == 0) {
s = lookup_attack_step(slot, eg_counter);
if (0 < s) {
slot->eg_out = max(0, ((int)slot->eg_out - (slot->eg_out >> s) - 1));
}
}
} else {
if (slot->eg_rate_h > 0 && (eg_counter & mask) == 0) {
slot->eg_out = min(EG_MUTE, slot->eg_out + lookup_decay_step(slot, eg_counter));
}
}
switch (slot->eg_state) {
case DAMP:
if (slot->eg_out >= EG_MUTE) {
start_envelope(slot);
if (slot->type & 1) {
if (!slot->pg_keep) {
slot->pg_phase = 0;
}
if (buddy && !buddy->pg_keep) {
buddy->pg_phase = 0;
}
}
}
break;
case ATTACK:
if (slot->eg_out == 0) {
slot->eg_state = DECAY;
request_update(slot, UPDATE_EG);
}
break;
case DECAY:
if ((slot->eg_out >> 3) == slot->patch->SL) {
slot->eg_state = SUSTAIN;
request_update(slot, UPDATE_EG);
}
break;
case SUSTAIN:
case RELEASE:
default:
break;
}
if (test) {
slot->eg_out = 0;
}
}
static void update_slots(OPLL *opll) {
int i;
opll->eg_counter++;
for (i = 0; i < 18; i++) {
OPLL_SLOT *slot = &opll->slot[i];
OPLL_SLOT *buddy = NULL;
if (slot->type == 0) {
buddy = &opll->slot[i + 1];
}
if (slot->type == 1) {
buddy = &opll->slot[i - 1];
}
if (slot->update_requests) {
commit_slot_update(slot);
}
calc_envelope(slot, buddy, opll->eg_counter, opll->test_flag & 1);
calc_phase(slot, opll->pm_phase, opll->test_flag & 4);
}
}
/* output: -4095...4095 */
static INLINE int16_t lookup_exp_table(uint16_t i) {
/* from andete's expression */
int16_t t = (exp_table[(i & 0xff) ^ 0xff] + 1024);
int16_t res = t >> ((i & 0x7f00) >> 8);
return ((i & 0x8000) ? ~res : res) << 1;
}
static INLINE int16_t to_linear(uint16_t h, OPLL_SLOT *slot, int16_t am) {
uint16_t att;
if (slot->eg_out >= EG_MAX)
return 0;
att = min(EG_MAX, (slot->eg_out + slot->tll + am)) << 4;
return lookup_exp_table(h + att);
}
static INLINE int16_t calc_slot_car(OPLL *opll, int ch, int16_t fm) {
OPLL_SLOT *slot = CAR(opll, ch);
uint8_t am = slot->patch->AM ? opll->lfo_am : 0;
slot->output[1] = slot->output[0];
slot->output[0] = to_linear(slot->wave_table[(slot->pg_out + 2 * (fm >> 1)) & (PG_WIDTH - 1)], slot, am);
return slot->output[0];
}
static INLINE int16_t calc_slot_mod(OPLL *opll, int ch) {
OPLL_SLOT *slot = MOD(opll, ch);
int16_t fm = slot->patch->FB > 0 ? (slot->output[1] + slot->output[0]) >> (9 - slot->patch->FB) : 0;
uint8_t am = slot->patch->AM ? opll->lfo_am : 0;
slot->output[1] = slot->output[0];
slot->output[0] = to_linear(slot->wave_table[(slot->pg_out + fm) & (PG_WIDTH - 1)], slot, am);
return slot->output[0];
}
static INLINE int16_t calc_slot_tom(OPLL *opll) {
OPLL_SLOT *slot = MOD(opll, 8);
return to_linear(slot->wave_table[slot->pg_out], slot, 0);
}
/* Specify phase offset directly based on 10-bit (1024-length) sine table */
#define _PD(phase) ((PG_BITS < 10) ? (phase >> (10 - PG_BITS)) : (phase << (PG_BITS - 10)))
static INLINE int16_t calc_slot_snare(OPLL *opll) {
OPLL_SLOT *slot = CAR(opll, 7);
uint32_t phase;
if (BIT(slot->pg_out, PG_BITS - 2))
phase = (opll->noise & 1) ? _PD(0x300) : _PD(0x200);
else
phase = (opll->noise & 1) ? _PD(0x0) : _PD(0x100);
return to_linear(slot->wave_table[phase], slot, 0);
}
static INLINE int16_t calc_slot_cym(OPLL *opll) {
OPLL_SLOT *slot = CAR(opll, 8);
uint32_t phase = opll->short_noise ? _PD(0x300) : _PD(0x100);
return to_linear(slot->wave_table[phase], slot, 0);
}
static INLINE int16_t calc_slot_hat(OPLL *opll) {
OPLL_SLOT *slot = MOD(opll, 7);
uint32_t phase;
if (opll->short_noise)
phase = (opll->noise & 1) ? _PD(0x2d0) : _PD(0x234);
else
phase = (opll->noise & 1) ? _PD(0x34) : _PD(0xd0);
return to_linear(slot->wave_table[phase], slot, 0);
}
#define _MO(x) (-(x) >> 1)
#define _RO(x) (x)
static void update_output(OPLL *opll) {
int16_t *out;
int i;
update_ampm(opll);
update_short_noise(opll);
update_slots(opll);
out = opll->ch_out;
/* CH1-6 */
for (i = 0; i < 6; i++) {
if (!(opll->mask & OPLL_MASK_CH(i))) {
out[i] = _MO(calc_slot_car(opll, i, calc_slot_mod(opll, i)));
}
}
/* CH7 */
if (!opll->rhythm_mode) {
if (!(opll->mask & OPLL_MASK_CH(6))) {
out[6] = _MO(calc_slot_car(opll, 6, calc_slot_mod(opll, 6)));
}
} else {
if (!(opll->mask & OPLL_MASK_BD)) {
out[9] = _RO(calc_slot_car(opll, 6, calc_slot_mod(opll, 6)));
}
}
update_noise(opll, 14);
/* CH8 */
if (!opll->rhythm_mode) {
if (!(opll->mask & OPLL_MASK_CH(7))) {
out[7] = _MO(calc_slot_car(opll, 7, calc_slot_mod(opll, 7)));
}
} else {
if (!(opll->mask & OPLL_MASK_HH)) {
out[10] = _RO(calc_slot_hat(opll));
}
if (!(opll->mask & OPLL_MASK_SD)) {
out[11] = _RO(calc_slot_snare(opll));
}
}
update_noise(opll, 2);
/* CH9 */
if (!opll->rhythm_mode) {
if (!(opll->mask & OPLL_MASK_CH(8))) {
out[8] = _MO(calc_slot_car(opll, 8, calc_slot_mod(opll, 8)));
}
} else {
if (!(opll->mask & OPLL_MASK_TOM)) {
out[12] = _RO(calc_slot_tom(opll));
}
if (!(opll->mask & OPLL_MASK_CYM)) {
out[13] = _RO(calc_slot_cym(opll));
}
}
update_noise(opll, 2);
}
INLINE static void mix_output(OPLL *opll) {
int16_t out = 0;
int i;
for (i = 0; i < 14; i++) {
out += opll->ch_out[i];
}
if (opll->conv) {
OPLL_RateConv_putData(opll->conv, 0, out);
} else {
opll->mix_out[0] = out;
}
}
INLINE static void mix_output_stereo(OPLL *opll) {
int16_t *out = opll->mix_out;
int i;
out[0] = out[1] = 0;
for (i = 0; i < 14; i++) {
if (opll->pan[i] & 2)
out[0] += (int16_t)(opll->ch_out[i] * opll->pan_fine[i][0]);
if (opll->pan[i] & 1)
out[1] += (int16_t)(opll->ch_out[i] * opll->pan_fine[i][1]);
}
if (opll->conv) {
OPLL_RateConv_putData(opll->conv, 0, out[0]);
OPLL_RateConv_putData(opll->conv, 1, out[1]);
}
}
/***********************************************************
External Interfaces
***********************************************************/
void OPLL_new(OPLL* opll, OPLL_RateConv* rateConvPtr, bool internalRateConvert, uint32_t clk, uint32_t rate) {
int i;
if (!table_initialized) {
initializeTables();
}
//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->clk = clk;
opll->rate = rate;
opll->mask = 0;
opll->conv = nullptr;
opll->mix_out[0] = 0;
opll->mix_out[1] = 0;
OPLL_reset(opll, rateConvPtr, internalRateConvert);
OPLL_setChipType(opll, 0);
OPLL_resetPatch(opll, 0);
//return opll;
}
// void OPLL_delete(OPLL *opll) {
// if (opll->conv) {
// OPLL_RateConv_delete(opll->conv);
// opll->conv = NULL;
// }
// free(opll);
// }
static void reset_rate_conversion_params(OPLL *opll, OPLL_RateConv *rateConvPtr)
{
if(rateConvPtr == nullptr)
panic("RateConv is not initialized!");
opll->conv = rateConvPtr;
const double f_out = opll->rate;
const double f_inp = opll->clk / 72.0;
opll->out_time = 0;
opll->out_step = f_inp;
opll->inp_step = f_out;
if (floor(f_inp) != f_out && floor(f_inp + 0.5) != f_out)
{
OPLL_RateConv_new(opll->conv, f_inp, f_out, 2);
}
if (opll->conv)
{
OPLL_RateConv_reset(opll->conv);
}
}
void OPLL_reset(OPLL* opll, OPLL_RateConv* rateConvPtr, bool internalRateConvert) {
int i;
if (!opll)
return;
opll->adr = 0;
opll->pm_phase = 0;
opll->am_phase = 0;
opll->noise = 0x1;
opll->mask = 0;
opll->rhythm_mode = 0;
opll->slot_key_status = 0;
opll->eg_counter = 0;
if (internalRateConvert) reset_rate_conversion_params(opll, rateConvPtr);
for (i = 0; i < 18; i++)
reset_slot(&opll->slot[i], i);
for (i = 0; i < 9; i++) {
set_patch(opll, i, 0);
}
for (i = 0; i < 0x40; i++)
OPLL_writeReg(opll, i, 0);
for (i = 0; i < 15; i++) {
opll->pan[i] = 3;
opll->pan_fine[i][1] = opll->pan_fine[i][0] = 1.0f;
}
for (i = 0; i < 14; i++) {
opll->ch_out[i] = 0;
}
}
void OPLL_forceRefresh(OPLL *opll) {
int i;