-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfm64.cpp
1782 lines (1722 loc) · 58.5 KB
/
fm64.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
/*
* File: fm64.cpp
*
* DX7/DX21/DX11-series compatible FM oscillator
*
* 2020-2021 (c) Oleg Burdaev
* mailto: [email protected]
*
*/
#include "userosc.h"
#include "fixed_mathq.h"
#include "arm.h"
//#define OP6 //6-operator support
//#define OP4 //4-operator support
//#define WF32 //all 8 DX11 waveforms from PCM32 wavebank
//#define WF16 //all 8 DX11 waveforms from PCM16 wavebank
//#define WF8 //all 8 DX11 waveforms runtime generated from half-sine
//#define WF4 //4 first DX11 waveforms runtime generated from half-sine
//#define WF2 //2 first DX11 waveforms runtime generated from half-sine
//#define WFROM //logue SDK wave banks A-F
//#define WFGEN //generated waveforms
//#define OPSIX //enable KORG Opsix extensions
//#define SY77 //enable SY77 extensions
#define TWEAK_ALG //use reserved bits for extended algorithms count support
#define TWEAK_WF //use reserved bits for extended waveforms count support
//#define SHAPE_LFO //map Shape LFO to parameters (~28-40 bytes)
//#define PEG //pitch EG enable (~530-600 bytes)
//#define PEG_RATE_LUT //PEG Rate from LUT close to DX7, instead of approximated function (~44-176 bytes)
#define FINE_TUNE //16-bit precision for cents/detune
//#define KIT_MODE //key tracking to voice (- ~112 bytes)
#define SPLIT_ZONES 3
//#define MOD16 //16-bit mod matrix processing
//#define ROLLOUT //Inner loop rollout
#ifdef MOD16
#define FEEDBACK_COUNT 2 //second feedback is mandatory and 'free' for 16-bit mod matrix
#endif
#ifndef FEEDBACK_COUNT
#define FEEDBACK_COUNT 1
#endif
#define FAST_POWF //native logue-sdk pow2f must be fixed, lost precesion (- ~6K bytes)
//#define FASTER_POWF //not very precise (- ~6.3K bytes)
#if defined(FAST_POWF)
#define POWF(a,b) fastpowf(a,b)
#define POW2F(a) fastpow2f(a)
#elif defined(FASTER_POWF)
#define POWF(a,b) fasterpowf(a,b)
#define POW2F(a) fasterpow2f(a)
#else
#define POWF(a,b) powf(a,b)
#define POW2F(a) powf(2.f,a)
#endif
#include "fm64.h"
#include "custom_param.h"
CUSTOM_PARAM_INIT(
#ifdef KIT_MODE
CUSTOM_PARAM_ID(7),
CUSTOM_PARAM_ID(10),
CUSTOM_PARAM_ID(5),
CUSTOM_PARAM_ID(11),
CUSTOM_PARAM_ID(6),
CUSTOM_PARAM_ID(12),
#else
CUSTOM_PARAM_ID(2),
CUSTOM_PARAM_ID(122),
CUSTOM_PARAM_ID(77),
CUSTOM_PARAM_ID(24),
CUSTOM_PARAM_ID(22),
#ifdef WFBITS
#ifdef WFGEN
CUSTOM_PARAM_ID(136),
#else
CUSTOM_PARAM_ID(131),
#endif
#else
CUSTOM_PARAM_ID(25),
#endif
#endif
CUSTOM_PARAM_ID(1),
CUSTOM_PARAM_ID(17)
);
#if defined(WFGEN)
#define FORMAT_PCM16
#define SAMPLE_COUNT 256
#define SAMPLE_GUARD
#define WAVEBANK_NO_HOOKS
#define WAVE_COUNT OPERATOR_COUNT
#define WAVE_COUNT_X OPERATOR_COUNT
#define WAVE_COUNT_Y 1
#include "wavebank.h"
#define WFBITS 7
#elif defined(WFROM)
#define WAVE_COUNT k_waves_all_cnt
#define WFBITS 7
#elif defined(WF16x2)
#include "waveforms16x2.h"
#define WFBITS 4
#elif defined(WF32)
#include "waveforms32.h"
#define WFBITS 3
#elif defined(WF16)
#include "waveforms16.h"
#define WFBITS 3
#elif defined(WF8)
#define OSC_SIN_Q15_LUT
#include "waveforms.h"
#define WAVE_COUNT 8
#define WFBITS 3
#elif defined(WF4)
#define OSC_SIN_Q15_LUT
#include "waveforms.h"
#define WAVE_COUNT 4
#define WFBITS 2
#elif defined(WF2)
#define OSC_SIN_Q15_LUT
#include "waveforms.h"
#define WAVE_COUNT 2
#define WFBITS 1
#endif
#ifdef WFGEN
#define WAVEFORM_COUNT 126
#else
#define WAVEFORM_COUNT WAVE_COUNT
#endif
#ifdef WAVE_PINCH
#define WAVE_PINCH_PARAMS , s_wavewidth[i * 2], s_wavewidth[i * 2 + 1]
#else
#define WAVE_PINCH_PARAMS
#endif
#ifdef WFBITS
#if defined(WFROM)
#define OSC_FUNC(a) osc_wave_scanf(s_waveform[i], a)
#elif defined(WFGEN)
#define OSC_FUNC(a) osc_wavebank(a, i WAVE_PINCH_PARAMS)
#else
#define OSC_FUNC(a) osc_wavebank(a, s_waveform[i] WAVE_PINCH_PARAMS)
#endif
#else
#define OSC_FUNC(a) osc_sinq(a WAVE_PINCH_PARAMS)
#if defined(WFSIN32)
#define OSC_SIN_Q31_LUT //use pre-calculated Q31 LUT instead of converted from firmware float, saves ~96 bytes of code
#elif defined(WFSIN16)
#define OSC_SIN_Q15_LUT //use pre-calculated Q31 LUT instead of converted from firmware float, saves ~96 bytes of code
#endif
#define OSC_SIN_Q
#endif
#include "osc_apiq.h"
// #define EGLUT //use precalculated EG LUT, saves ~140 bytes of code
#define USE_Q31
#if defined(EGLUT)
#include "eglut.h"
#define EG_LUT_SHR 20
#elif defined(EGLUT11)
#include "eglut11.h"
#define EG_LUT_SHR 19
#elif defined(EGLUT12)
#include "eglut12.h"
#define EG_LUT_SHR 18
#elif defined(EGLUT13)
#include "eglut13.h"
#define EG_LUT_SHR 17
#endif
#if defined(EGLUTX15)
#define param_eglut(a,b) (ldrsh_lsl((int32_t)eg_lut, usat_asr(31, q31add(a,b), (EG_LUT_SHR + 1)), 1) << 16)
#elif defined(EGLUTX16)
#define param_eglut(a,b) (ldrh_lsl((int32_t)eg_lut, usat_asr(31, q31add(a,b), (EG_LUT_SHR + 1)), 1) << 15)
#else
#define param_eglut(a,b) (ldr_lsl((int32_t)eg_lut, usat_asr(31, q31add(a,b), (EG_LUT_SHR + 1)), 2))
#endif
#ifdef USE_Q31_PITCH
typedef q31_t pitch_t;
#define f32_to_pitch(a) f32_to_q31(a)
#define pitch_to_phase(a) (a)
#define pitch_mul(a,b) q31mul(a,b)
#else
typedef float_t pitch_t;
#define f32_to_pitch(a) (a)
#define pitch_to_phase(a) f32_to_q31(a)
#define pitch_mul(a,b) ((a)*(b))
#endif
#define FEEDBACK_RECIP 0x00FFFFFF // <1/128 - pre-multiplied by 2 for simplified Q31 multiply by always positive
#define FEEDBACK_RECIPF .00390625f // 1/256 - pre-multiplied by 2 for simplified Q31 multiply by always positive
#define LEVEL_SCALE_FACTOR 0x01000000 // -0.7525749892dB/96dB === 1/128
//#define DX7_SAMPLING_FREQ 49096.545017211284821233588006932f // 1/20.368032usec
//#define DX7_TO_LOGUE_FREQ 0.977665536f // 48000/49096.545
//-.0325870980969347836053763275142f // log2(DX7_TO_LOGUE_FREQ)
#define EG_FREQ_CORRECT .0325870980969347836053763275142f // log2(1/DX7_TO_LOGUE_FREQ)
#define DX7_RATE_EXP_FACTOR .16f // ? 16/99 = .16(16)
#define DX11_RATE_EXP_FACTOR .505f
#define DX11_RELEASE_RATE_EXP_FACTOR 1.04f
//#define DX7_ATTACK_RATE_FACTOR 5.0200803e-7f // 1/(41.5*48000)
//#define DX7_DECAY_RATE_FACTOR -5.5778670e-8f // -1/(9*41.5*48000)
//#define DX7_ATTACK_RATE1_FACTOR 5.0261359e-7f // 1/(41.45*48000)
//#define DX7_DECAY_RATE1_FACTOR -8.3768932e-8f // -1/(6*41.45*48000)
#define DX7_ATTACK_RATE_FACTOR 3.3507573e-7f // 1/(1.5*41.45*48000)
#define DX7_DECAY_RATE_FACTOR -5.5845955e-8f // -1/(1.5*6*41.45*48000)
#define DX7_RATE1_FACTOR 1.5f
//#define DX7_ATTACK_RATE_FACTOR 4.8773035424164220513138759143079e-7f // 1/(2^21 * DX7_TO_LOGUE_FREQ) = 1/(2^(21 - EG_FREQ_CORRECT)
//#define DX7_ATTACK_RATE_FACTOR 20.9674129f
//#define DX7_ATTACK_RATE_FACTOR (21.f - EG_FREQ_CORRECT)
// 2^24 samples @49k = 2^24 / 49k seconds = 2^24 * 48k / (48k * 49k) seconds = 2^24 * 48K / 49K samples @ 48K
//#define DX7_DECAY_RATE_FACTOR -6.0966294280205275641423448928849e-8f // -1/(2^24 * DX7_TO_LOGUE_FREQ)
//#define DX7_DECAY_RATE_FACTOR 23.9674129f
//#define DX7_DECAY_RATE_FACTOR (24.f - EG_FREQ_CORRECT)
//#define DX7_HOLD_RATE_FACTOR .51142234392928421688784987507221f // 1/(2^1 * DX7_TO_LOGUE_FREQ)
//#define DX7_HOLD_RATE_FACTOR 0.9674129f
//#define DX7_HOLD_RATE_FACTOR (1.f - EG_FREQ_CORRECT)
//#define RATE_SCALING_FACTOR .061421131f
//#define RATE_SCALING_FACTOR .041666667f
//#define RATE_SCALING_FACTOR .065040650f // 1/24 * 64/41
#define RATE_SCALING_FACTOR .445291664f // reversed from measures for current curve function
#define DX7_RATE_SCALING_FACTOR .142857143f // 1/7
#define DX11_RATE_SCALING_FACTOR .333333333f // 1/3
//#define DX7_LEVEL_SCALE_FACTOR 0.0267740885f // 109.(6)/4096
//#define DX7_LEVEL_SCALE_FACTOR 0.0222222222f // 1/45
#define DX7_LEVEL_SCALE_FACTOR 0.0200686664f
#define DX11_LEVEL_SCALE_FACTOR 0.0149253731f // 1/(103-36) C1...G6
//#define LEVEL_SCALE_FACTORF 0.0078740157f // 1/127
#define LEVEL_SCALE_FACTORF 0.0078125f // 1/128
#define LEVEL_SCALE_FACTOR_DB 0.0103810253f // 1/96dB
#define DX11_TO_DX7_LEVEL_SCALE_FACTOR 6.6f //99/15
#define DX11_MAX_LEVEL 15
//#define FREQ_FACTOR .08860606f // (9.772 - 1)/99
#define PEG_SCALE 0x00600000 // 48/128 * 256 * 65536
#define PEG_RATE_SCALE 196.38618f; // ~ 192 >> 24 semitones per sample at 49096.545
#ifdef MOD16
#ifdef CUSTOM_ALGORITHM_COUNT
#define CUSTOM_MI_SCALE_FACTOR 0x139CA3E //normalized for 0-100 range of opsix
//#define CUSTOM_OUT_SCALE_FACTOR 0x147AE14 //^
#endif
#define MI_SCALE_FACTOR 0x7A93
static const uint8_t *s_algorithm;
static q15_t s_opval[OPERATOR_COUNT + FEEDBACK_COUNT * 2];
static q15_t s_modmatrix[OPERATOR_COUNT][OPERATOR_COUNT + FEEDBACK_COUNT];
static q15_t s_comp[OPERATOR_COUNT];
static q15_t compensation[] = {
0x7FFF,
0x3FFF,
0x2AAA,
0x1FFF,
0x1999,
0x1555
};
#else
#define MI_SCALE_FACTOR 0x7A92BE8B // 3.830413123f >> 2
static uint8_t s_algorithm[OPERATOR_COUNT] = {0};
static q31_t s_opval[OPERATOR_COUNT + FEEDBACK_COUNT * 2];
static q31_t s_comp[OPERATOR_COUNT];
static q31_t compensation[] = {
0x7FFFFFFF,
0x3FFFFFFF,
0x2AAAAAAA,
0x1FFFFFFF,
0x19999999,
0x15555555
};
#endif
static uint8_t s_algorithm_idx;
static int8_t s_algorithm_offset = 0;
static uint8_t s_algorithm_select = 0;
#define FINE_TUNE_FACTOR 65536.f
static uint8_t s_split_point[SPLIT_ZONES - 1] = {0};
static int8_t s_zone_transpose[SPLIT_ZONES] = {0};
static int8_t s_zone_voice_shift[SPLIT_ZONES] = {0};
static int8_t s_zone_transposed = 0;
#ifndef KIT_MODE
static uint8_t s_kit_voice = 0;
static int8_t s_voice[SPLIT_ZONES] = {0};
#endif
static int8_t s_level_offset[OPERATOR_COUNT + 3] = {0};
static int8_t s_kls_offset[OPERATOR_COUNT + 3] = {0};
static int8_t s_kvs_offset[OPERATOR_COUNT + 3] = {0};
static int8_t s_egrate_offset[OPERATOR_COUNT + 3] = {0};
static int8_t s_krs_offset[OPERATOR_COUNT + 3] = {0};
static int8_t s_detune_offset[OPERATOR_COUNT + 3] = {0};
#ifdef WAVE_PINCH
static int8_t s_waveform_pinch[OPERATOR_COUNT + 3] = {0};
#endif
#ifdef OP6
static uint8_t s_level_scale[OPERATOR_COUNT + 3] = {100, 100, 100, 100, 100, 100, 100, 100, 100};
static uint8_t s_kls_scale[OPERATOR_COUNT + 3] = {100, 100, 100, 100, 100, 100, 100, 100, 100};
static uint8_t s_kvs_scale[OPERATOR_COUNT + 3] = {100, 100, 100, 100, 100, 100, 100, 100, 100};
static uint8_t s_egrate_scale[OPERATOR_COUNT + 3] = {100, 100, 100, 100, 100, 100, 100, 100, 100};
static uint8_t s_krs_scale[OPERATOR_COUNT + 3] = {100, 100, 100, 100, 100, 100, 100, 100, 100};
static uint8_t s_detune_scale[OPERATOR_COUNT + 3] = {100, 100, 100, 100, 100, 100, 100, 100, 100};
#else
static uint8_t s_level_scale[OPERATOR_COUNT + 3] = {100, 100, 100, 100, 100, 100, 100};
static uint8_t s_kls_scale[OPERATOR_COUNT + 3] = {100, 100, 100, 100, 100, 100, 100};
static uint8_t s_kvs_scale[OPERATOR_COUNT + 3] = {100, 100, 100, 100, 100, 100, 100};
static uint8_t s_egrate_scale[OPERATOR_COUNT + 3] = {100, 100, 100, 100, 100, 100, 100};
static uint8_t s_krs_scale[OPERATOR_COUNT + 3] = {100, 100, 100, 100, 100, 100, 100};
static uint8_t s_detune_scale[OPERATOR_COUNT + 3] = {100, 100, 100, 100, 100, 100, 100};
#endif
static float s_feedback_offset[FEEDBACK_COUNT] = {0.f};
#if FEEDBACK_COUNT == 2
static float s_feedback_scale[FEEDBACK_COUNT] = {1.f, 1.f};
#else
static float s_feedback_scale[FEEDBACK_COUNT] = {1.f};
#endif
static uint8_t s_feedback_route[FEEDBACK_COUNT] = {0};
static uint8_t s_feedback_level[FEEDBACK_COUNT] = {0};
#ifdef WFBITS
#ifdef OP6
static int8_t s_waveform_offset[OPERATOR_COUNT + 3 + 4] = {0};
#else
static int8_t s_waveform_offset[OPERATOR_COUNT + 3 + 3] = {0};
#endif
#endif
static int8_t s_left_depth[OPERATOR_COUNT];
static int8_t s_right_depth[OPERATOR_COUNT];
static uint8_t s_pitchfreq[OPERATOR_COUNT];
static uint8_t s_egstage[OPERATOR_COUNT];
static uint8_t s_kvs[OPERATOR_COUNT];
static uint8_t s_break_point[OPERATOR_COUNT];
static uint8_t s_left_curve[OPERATOR_COUNT];
static uint8_t s_right_curve[OPERATOR_COUNT];
static uint8_t s_opi;
static int8_t s_detune[OPERATOR_COUNT];
static int8_t s_transpose;
static uint32_t s_sample_num;
static uint32_t s_sample_count[OPERATOR_COUNT][EG_STAGE_COUNT * 2];
static q31_t s_velocity = 0;
static int8_t s_op_level[OPERATOR_COUNT];
static float s_op_rate_scale[OPERATOR_COUNT];
#ifdef WFBITS
static uint8_t s_op_waveform[OPERATOR_COUNT];
#ifdef WFROM
static const float * s_waveform[OPERATOR_COUNT];
#else
static uint32_t s_waveform[OPERATOR_COUNT];
#ifdef WFGEN
#ifdef OP6
static uint32_t s_waveform_current[OPERATOR_COUNT] = {0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF};
#else
static uint32_t s_waveform_current[OPERATOR_COUNT] = {0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF};
#endif
#endif
#endif
#endif
static uint8_t s_egrate[OPERATOR_COUNT][EG_STAGE_COUNT];
static q31_t s_egsrate[OPERATOR_COUNT][EG_STAGE_COUNT * 2];
static float s_egsrate_recip[OPERATOR_COUNT][2];
static q31_t s_eglevel[OPERATOR_COUNT][EG_STAGE_COUNT];
static q31_t s_egval[OPERATOR_COUNT];
static q31_t s_oplevel[OPERATOR_COUNT];
static q31_t s_outlevel[OPERATOR_COUNT];
#ifdef OP6
static float s_klslevel[OPERATOR_COUNT] = {LEVEL_SCALE_FACTOR_DB, LEVEL_SCALE_FACTOR_DB, LEVEL_SCALE_FACTOR_DB, LEVEL_SCALE_FACTOR_DB, LEVEL_SCALE_FACTOR_DB, LEVEL_SCALE_FACTOR_DB};
static float s_krslevel[OPERATOR_COUNT] = {RATE_SCALING_FACTOR, RATE_SCALING_FACTOR, RATE_SCALING_FACTOR, RATE_SCALING_FACTOR, RATE_SCALING_FACTOR, RATE_SCALING_FACTOR};
static float s_egratelevel[OPERATOR_COUNT] = {1.f, 1.f, 1.f, 1.f, 1.f, 1.f};
#ifdef WAVE_PINCH
static q31_t s_wavewidth[OPERATOR_COUNT * 2] = {0x7FFFFFFF, 0x01000000, 0x7FFFFFFF, 0x01000000, 0x7FFFFFFF, 0x01000000, 0x7FFFFFFF, 0x01000000, 0x7FFFFFFF, 0x01000000, 0x7FFFFFFF, 0x01000000};
#endif
#else
static float s_klslevel[OPERATOR_COUNT] = {LEVEL_SCALE_FACTOR_DB, LEVEL_SCALE_FACTOR_DB, LEVEL_SCALE_FACTOR_DB, LEVEL_SCALE_FACTOR_DB};
static float s_krslevel[OPERATOR_COUNT] = {RATE_SCALING_FACTOR, RATE_SCALING_FACTOR, RATE_SCALING_FACTOR, RATE_SCALING_FACTOR};
static float s_egratelevel[OPERATOR_COUNT] = {1.f, 1.f, 1.f, 1.f};
#ifdef WAVE_PINCH
static q31_t s_wavewidth[OPERATOR_COUNT * 2] = {0x7FFFFFFF, 0x01000000, 0x7FFFFFFF, 0x01000000, 0x7FFFFFFF, 0x01000000, 0x7FFFFFFF, 0x01000000};
#endif
#endif
static int16_t s_klsoffset[OPERATOR_COUNT] = {0};
static int16_t s_egrateoffset[OPERATOR_COUNT] = {0};
static float s_krsoffset[OPERATOR_COUNT] = {0.f};
static q31_t s_level_scaling[OPERATOR_COUNT];
static q31_t s_kvslevel[OPERATOR_COUNT];
static q31_t s_velocitylevel[OPERATOR_COUNT];
static float s_attack_rate_exp_factor;
static float s_release_rate_exp_factor;
static float s_level_scale_factor;
static q31_t s_feedback[FEEDBACK_COUNT];
static uint8_t s_feedback_src[FEEDBACK_COUNT];
static uint8_t s_feedback_src_alg[FEEDBACK_COUNT];
static uint8_t s_feedback_dst_alg[FEEDBACK_COUNT];
#ifdef PEG
static int32_t s_pegrate[PEG_STAGE_COUNT + 1];
static int32_t s_peglevel[PEG_STAGE_COUNT];
static uint32_t s_peg_sample_count[PEG_STAGE_COUNT];
static int32_t s_pegval;
static float s_pegrate_releaserecip;
static uint8_t s_pegstage;
static uint8_t s_peg_stage_start;
#endif
static pitch_t s_oppitch[OPERATOR_COUNT];
static q31_t s_phase[OPERATOR_COUNT];
enum {
state_running = 0,
state_noteon = 1,
state_noteoff = 2,
state_wave_changed = 4
};
#ifdef WFGEN
static uint32_t s_state = state_wave_changed;
#else
static uint32_t s_state = 0;
#endif
float paramScale(uint8_t *param, uint32_t opidx) {
return .000001f * param[opidx] * param[((s_algorithm[opidx] & ALG_OUT_MASK) >> 7) + OPERATOR_COUNT] * param[OPERATOR_COUNT + 2];
}
int32_t paramOffset(int8_t *param, uint32_t opidx) {
return param[opidx] + param[((s_algorithm[opidx] & ALG_OUT_MASK) >> 7) + OPERATOR_COUNT] + param[OPERATOR_COUNT + 2];
}
static inline __attribute__((optimize("Ofast"), always_inline))
void setOpLevel(uint32_t opidx) {
// make it non-negative and apply -96dB to further fit EG level
s_oplevel[opidx] = q31sub((usat_lsl(31, q31add(s_level_scaling[opidx], s_velocitylevel[opidx]), 0)), 0x7F000000);
}
void setOutLevel() {
for (uint32_t i = 0; i < OPERATOR_COUNT; i++)
// saturate Out Level to 0dB offset of Q31
s_outlevel[i] = q31add(f32_to_q31(scale_level(clipminmaxi32(0, s_op_level[i] + paramOffset(s_level_offset, i), 99)) * paramScale(s_level_scale, i) * LEVEL_SCALE_FACTORF), 0x00FFFFFF);
}
void setKvsLevel() {
for (uint32_t i = 0; i < OPERATOR_COUNT; i++)
s_kvslevel[i] = f32_to_q31(clipminmaxf(0.f, s_kvs[i] + paramOffset(s_kvs_offset, i) * 0.07f, 7.f) * paramScale(s_kvs_scale, i) * .015625f);
}
static inline __attribute__((optimize("Ofast"), always_inline))
void setVelocityLevel() {
for (uint32_t i = 0; i < OPERATOR_COUNT; i++) {
// Velocity * KVS
s_velocitylevel[i] = smmul(s_velocity, s_kvslevel[i]) << 7;
setOpLevel(i);
}
}
/*
void setLevel() {
for (uint32_t i = 0; i < OPERATOR_COUNT; i++) {
// saturate Out Level to 0dB offset of Q31
s_oplevel[i] = q31add(f32_to_q31(scale_level(clipminmaxi32(0, s_op_level[i] + paramOffset(s_level_offset, i), 99)) * paramScale(s_level_scale, i) * LEVEL_SCALE_FACTORF), 0x00FFFFFF);
// saturate with KLS
s_oplevel[i] = q31add(s_oplevel[i], s_level_scaling[i]);
// adjust 0dB level to fit positive Velocity * KVS and add them
s_oplevel[i] = q31add(q31sub(s_oplevel[i], 0x07000000), f32_to_q31(s_velocity * clipminmaxf(0.f, s_kvs[i] + paramOffset(s_kvs_offset, i) * 0.07f, 7.f) * paramScale(s_kvs_scale, i)));
// make it non-negative and apply -96dB to further fit EG level
s_oplevel[i] = q31sub((usat_lsl(31, s_oplevel[i], 0)), 0x7F000000);
}
}
*/
#ifdef WFBITS
void setWaveform() {
for (uint32_t i = 0; i < OPERATOR_COUNT; i++) {
#ifdef WFROM
s_waveform[i] = wavesAll[clipminmaxi32(
0, s_op_waveform[i] +
paramOffset(s_waveform_offset, i) +
(i & 0x01 ? (s_waveform_offset[OPERATOR_COUNT + 3 + (i >> 1)] / 10) : (s_waveform_offset[OPERATOR_COUNT + 3 + (i >> 1)] % 10)) +
((s_algorithm[i] & ALG_OUT_MASK) ? (s_waveform_offset[sizeof(s_waveform_offset) / sizeof(*s_waveform_offset) - 1] / 10) : (s_waveform_offset[sizeof(s_waveform_offset) / sizeof(*s_waveform_offset) - 1] % 10)),
WAVEFORM_COUNT - 1
)];
#else
s_waveform[i] = clipminmaxi32(
0, s_op_waveform[i] +
paramOffset(s_waveform_offset, i) +
(i & 0x01 ? (s_waveform_offset[OPERATOR_COUNT + 3 + (i >> 1)] / 10) : (s_waveform_offset[OPERATOR_COUNT + 3 + (i >> 1)] % 10)) +
((s_algorithm[i] & ALG_OUT_MASK) ? (s_waveform_offset[sizeof(s_waveform_offset) / sizeof(*s_waveform_offset) - 1] / 10) : (s_waveform_offset[sizeof(s_waveform_offset) / sizeof(*s_waveform_offset) - 1] % 10)),
WAVEFORM_COUNT - 1
);
#ifdef WFGEN
if (s_waveform[i] != s_waveform_current[i])
s_state |= state_wave_changed;
#endif
#endif
}
}
#endif
void setFeedback(uint32_t idx) {
float value = clipmaxf(s_feedback_level[idx] + s_feedback_offset[idx], 7.f);
s_feedback[idx] = value <= 0.f ? 0 : f32_to_q31(POW2F(value * s_feedback_scale[idx]) * FEEDBACK_RECIPF);
}
void setFeedbackRoute(uint32_t idx) {
uint32_t dst;
if (s_feedback_route[idx] == 0) {
s_feedback_src[idx] = s_feedback_src_alg[idx];
dst = s_feedback_dst_alg[idx];
} else {
s_feedback_src[idx] = OPERATOR_COUNT - clipminmaxi32(1, s_feedback_route[idx] / 10, OPERATOR_COUNT);
dst = OPERATOR_COUNT - clipminmaxi32(1, s_feedback_route[idx] % 10, OPERATOR_COUNT);
}
for (uint32_t i = 0; i < OPERATOR_COUNT; i++) {
#ifdef MOD16
if (i == dst)
s_modmatrix[i][OPERATOR_COUNT + idx] = MI_SCALE_FACTOR;
else
s_modmatrix[i][OPERATOR_COUNT + idx] = 0;
#else
if (i == dst)
s_algorithm[i] |= ALG_FBK_MASK << idx;
else
s_algorithm[i] &= ~(ALG_FBK_MASK << idx);
#endif
}
}
void setAlgorithm() {
int32_t comp = 0;
#ifdef MOD16
#ifdef CUSTOM_ALGORITHM_COUNT
uint32_t algidx = (s_algorithm_select == 0 ? s_algorithm_idx : s_algorithm_select - 1) + s_algorithm_offset;
if (algidx < ALGORITHM_COUNT)
s_algorithm = dx7_algorithm[clipminmaxi32(0, algidx, ALGORITHM_COUNT + CUSTOM_ALGORITHM_COUNT - 1)];
#else
s_algorithm = dx7_algorithm[clipminmaxi32(0, (s_algorithm_select == 0 ? s_algorithm_idx : s_algorithm_select - 1) + s_algorithm_offset, ALGORITHM_COUNT - 1)];
#endif
#endif
s_feedback_dst_alg[0] = OPERATOR_COUNT;
#if FEEDBACK_COUNT == 2
s_feedback_dst_alg[1] = OPERATOR_COUNT;
#endif
for (uint32_t i = 0; i < OPERATOR_COUNT; i++) {
#ifndef MOD16
s_algorithm[i] = dx7_algorithm[clipminmaxi32(0, (s_algorithm_select == 0 ? s_algorithm_idx : s_algorithm_select - 1) + s_algorithm_offset, ALGORITHM_COUNT - 1)][i];
#endif
#ifdef CUSTOM_ALGORITHM_COUNT
if (algidx < ALGORITHM_COUNT) {
#endif
for (uint32_t fbidx = 0; fbidx < FEEDBACK_COUNT; fbidx++) {
if (s_algorithm[i] & (ALG_FBK_MASK << fbidx)) {
s_feedback_src_alg[fbidx] = s_algorithm[i] & (ALG_FBK_MASK - 1);
s_feedback_dst_alg[fbidx] = i;
#ifndef MOD16
s_algorithm[i] &= ~(ALG_FBK_MASK - 1);
#endif
}
}
if (s_algorithm[i] & ALG_OUT_MASK)
comp++;
#ifdef CUSTOM_ALGORITHM_COUNT
} else {
comp += custom_algorithm[algidx - ALGORITHM_COUNT][i][OPERATOR_COUNT];
}
#endif
#ifdef MOD16
for (uint32_t j = 0; j < OPERATOR_COUNT; j++) {
#ifdef CUSTOM_ALGORITHM_COUNT
if (algidx < ALGORITHM_COUNT) {
#endif
if ((j < (OPERATOR_COUNT - 1)) && (s_algorithm[i] & (1 << j)) && !(s_algorithm[i] & (ALG_FBK_MASK + ALG_FBK2_MASK)))
s_modmatrix[i][j] = MI_SCALE_FACTOR;
else
s_modmatrix[i][j] = 0;
#ifdef CUSTOM_ALGORITHM_COUNT
} else {
s_modmatrix[i][j] = (custom_algorithm[algidx - ALGORITHM_COUNT][i][j] * CUSTOM_MI_SCALE_FACTOR) >> 16;
}
#endif
}
#endif
}
setFeedbackRoute(0);
#if FEEDBACK_COUNT == 2
setFeedbackRoute(1);
#endif
#ifdef CUSTOM_ALGORITHM_COUNT
if (algidx >= ALGORITHM_COUNT)
comp = 0x7FFFFFFF / comp;
#endif
for (uint32_t i = 0; i < OPERATOR_COUNT; i++) {
#ifdef CUSTOM_ALGORITHM_COUNT
if (algidx < ALGORITHM_COUNT) {
#endif
if (s_algorithm[i] & ALG_OUT_MASK)
s_comp[i] = compensation[comp - 1];
else
s_comp[i] = 0;
#ifdef CUSTOM_ALGORITHM_COUNT
} else {
// s_comp[i] = (custom_algorithm[algidx - ALGORITHM_COUNT][i][OPERATOR_COUNT] * CUSTOM_OUT_SCALE_FACTOR) >> 16;
s_comp[i] = (custom_algorithm[algidx - ALGORITHM_COUNT][i][OPERATOR_COUNT] * comp) >> 16;
}
#endif
}
#ifdef WFBITS
setWaveform();
#endif
}
static inline __attribute__((optimize("Ofast"), always_inline))
void initvoice(int32_t voice_index) {
voice_index %= BANK_COUNT * BANK_SIZE;
if (voice_index < 0)
voice_index += BANK_COUNT * BANK_SIZE;
if (dx_voices[0][voice_index].dx7.vnam[0]) {
#ifdef OP6
const dx7_voice_t *voice = &dx_voices[0][voice_index].dx7;
s_opi = voice->opi;
s_algorithm_idx = voice->als;
s_transpose = voice->trnp - TRANSPOSE_CENTER;
s_feedback_level[0] = voice->fbl;
#ifdef PEG
s_peg_stage_start = PEG_STAGE_COUNT - DX7_PEG_STAGE_COUNT;
for (uint32_t i = s_peg_stage_start; i < PEG_STAGE_COUNT; i++) {
s_peglevel[i] = scale_pitch_level(voice->pl[i - s_peg_stage_start]) * PEG_SCALE;
s_pegrate[i] = scale_pitch_rate(voice->pr[i - s_peg_stage_start]) * PEG_RATE_SCALE;
}
#endif
for (uint32_t i = 0; i < OPERATOR_COUNT; i++) {
s_pitchfreq[i] = !voice->op[i].pm;
s_detune[i] = (voice->op[i].pd - DX7_DETUNE_CENTER) * 3;
#ifdef WFBITS
#ifdef TWEAK_WF
s_op_waveform[i] = voice->op[i].osw & ((1 << WFBITS) - 1);
#else
s_op_waveform[i] = 0;
#endif
#endif
s_phase[i] = 0;
//todo: check dx7 D1/D2/R rates
for (uint32_t j = 0; j < EG_STAGE_COUNT; j++) {
s_egrate[i][j] = voice->op[i].r[j];
s_eglevel[i][j] = scale_level(voice->op[i].l[j]) * LEVEL_SCALE_FACTOR;
}
if (s_pitchfreq[i])
s_oppitch[i] = f32_to_pitch(((voice->op[i].pc == 0 ? .5f : voice->op[i].pc) * (1.f + voice->op[i].pf * .01f)));
else
s_oppitch[i] = f32_to_pitch(fastexpf(M_LN10 * ((voice->op[i].pc & 3) + voice->op[i].pf * .01f)) * k_samplerate_recipf);
s_kvs[i] = voice->op[i].ts;
s_op_rate_scale[i] = voice->op[i].rs * DX7_RATE_SCALING_FACTOR;
s_op_level[i] = voice->op[i].tl;
s_break_point[i] = voice->op[i].bp + NOTE_A_1;
//fold negative/position curves into curve depth sign
s_left_depth[i] = voice->op[i].ld;
s_right_depth[i] = voice->op[i].rd;
if (voice->op[i].lc < 2) {
s_left_curve[i] = voice->op[i].lc;
} else {
s_left_curve[i] = 5 - voice->op[i].lc;
}
if (voice->op[i].rc < 2) {
s_right_curve[i] = voice->op[i].rc;
} else {
s_right_curve[i] = 5 - voice->op[i].rc;
}
}
s_attack_rate_exp_factor = DX7_RATE_EXP_FACTOR;
s_release_rate_exp_factor = DX7_RATE_EXP_FACTOR;
s_level_scale_factor = DX7_LEVEL_SCALE_FACTOR;
#endif
} else {
#ifdef OP4
const dx11_voice_t *voice = &dx_voices[0][voice_index].dx11;
#ifdef TWEAK_ALG
s_algorithm_idx = dx11_algorithm_lut[voice->alg + (voice->alghi << 3)];
#else
s_algorithm_idx = dx11_algorithm_lut[voice->alg];
#endif
s_opi = 0;
s_transpose = voice->trps - TRANSPOSE_CENTER;
s_feedback_level[0] = voice->fbl;
#ifdef PEG
s_peg_stage_start = PEG_STAGE_COUNT - DX11_PEG_STAGE_COUNT;
for (uint32_t i = s_peg_stage_start; i < PEG_STAGE_COUNT; i++) {
s_peglevel[i] = scale_pitch_level(voice->pl[i - s_peg_stage_start]) * PEG_SCALE;
s_pegrate[i] = scale_pitch_rate(voice->pr[i - s_peg_stage_start]) * PEG_RATE_SCALE;
}
#endif
for (uint32_t k = DX11_OPERATOR_COUNT; k--;) {
uint32_t i;
if (s_algorithm_idx == 7)
i = dx11_alg3_op_lut[k];
else
i = k;
s_pitchfreq[i] = !voice->opadd[i].fixrg;
s_detune[i] = (voice->op[i].det - DX11_DETUNE_CENTER) * 3;
#ifdef WFBITS
s_op_waveform[i] = voice->opadd[i].osw & ((1 << WFBITS) - 1);
#endif
s_phase[i] = 0;
//todo: check dx11 rates
for (uint32_t j = 0; j < EG_STAGE_COUNT; j++) {
s_egrate[i][j] = j == (EG_STAGE_COUNT - 1) && voice->op[i].r[j] == 0 ? 1 : voice->op[i].r[j]; //zero release rate workaround from TX81Z
s_eglevel[i][j] = f32_to_q31(1.f - (1.f - (j == 0 ? 1.f : (j == 1 || (j == 2 && voice->op[i].r[j] == 0)) ? scale_level(voice->op[i].d1l * DX11_TO_DX7_LEVEL_SCALE_FACTOR) * LEVEL_SCALE_FACTOR : 0.f)) / (1 << (i != 3 ? voice->opadd[i].egsft : 0)));
}
//todo: Fine freq ratio
//https://github.com/mamedev/mame/blob/master/src/devices/sound/ym2151.cpp
//dt2_tab = { 0/768, 384/768, 500/768, 608/768 }
//pitch = 2 ^ (key/12 + LFO in + dt2_tab[op dt2]) * op ratio + keytracked op detune * op ratio
if (s_pitchfreq[i])
s_oppitch[i] = f32_to_pitch(dx11_ratio_lut[voice->op[i].f]);
// s_oppitch[i] = log2f(dx11_ratio_lut[voice->op[i].f]) * 256.f * 12.f;
else
s_oppitch[i] = f32_to_pitch(((((voice->op[i].f & 0x3C) << 2) + voice->opadd[i].fine + (voice->op[i].f < 4 ? 8 : 0)) << voice->opadd[i].fixrg) * k_samplerate_recipf);
// s_oppitch[i] = log2f(((((voice->op[i].f & 0x3C) << 2) + voice->opadd[i].fine + (voice->op[i].f < 4 ? 8 : 0)) << voice->opadd[i].fixrg)) * 256.f * 12.f;
s_kvs[i] = voice->op[i].kvs;
s_op_rate_scale[i] = voice->op[i].rs * DX11_RATE_SCALING_FACTOR;
s_op_level[i] = voice->op[i].out;
s_left_depth[i] = 0;
s_right_depth[i] = - voice->op[i].ls;
s_left_curve[i] = 0;
s_right_curve[i] = 0;
s_break_point[i] = NOTE_C1;
}
s_attack_rate_exp_factor = DX11_RATE_EXP_FACTOR;
s_release_rate_exp_factor = DX11_RELEASE_RATE_EXP_FACTOR;
s_level_scale_factor = DX11_LEVEL_SCALE_FACTOR;
#ifdef OP6
s_op_level[4] = 0;
s_op_level[5] = 0;
s_kvs[4] = 0;
s_kvs[5] = 0;
for (uint32_t j = 0; j < EG_STAGE_COUNT; j++) {
s_egrate[4][j] = 0;
s_egrate[5][j] = 0;
s_eglevel[4][j] = 0;
s_eglevel[5][j] = 0;
}
#endif
#endif
}
setAlgorithm();
setOutLevel();
setKvsLevel();
setVelocityLevel();
for (uint32_t i = 0; i < FEEDBACK_COUNT; i++)
setFeedback(i);
for (uint32_t i = 0; i < OPERATOR_COUNT + FEEDBACK_COUNT * 2; i++)
s_opval[i] = 0;
for (uint32_t i = 0; i < OPERATOR_COUNT; i++) {
s_sample_count[i][EG_STAGE_COUNT - 1] = 0xFFFFFFFF;
s_egsrate[i][EG_STAGE_COUNT - 1] = 0;
s_egstage[i] = EG_STAGE_COUNT - 1;
s_egval[i] = 0;
}
#ifdef PEG
uint32_t samples = 0;
int32_t dl;
for (uint32_t i = s_peg_stage_start; i < PEG_STAGE_COUNT - 1; i++) {
dl = (s_peglevel[i] - s_peglevel[i != s_peg_stage_start ? i - 1 : PEG_STAGE_COUNT - 1]);
if (dl < 0)
s_pegrate[i] = -s_pegrate[i];
samples += dl / s_pegrate[i];
s_peg_sample_count[i] = samples;
}
s_pegrate[PEG_STAGE_COUNT] = s_pegrate[PEG_STAGE_COUNT - 1];
s_pegrate_releaserecip = 1.f / s_pegrate[PEG_STAGE_COUNT];
s_pegrate[PEG_STAGE_COUNT - 1] = 0;
s_peg_sample_count[PEG_STAGE_COUNT - 1] = 0xFFFFFFFF;
s_pegstage = PEG_STAGE_COUNT - 1;
s_pegval = 0;
#endif
}
#ifdef WFROM
void OSC_INIT(__attribute__((unused)) uint32_t platform, __attribute__((unused)) uint32_t api)
{
osc_wave_init_all();
}
#endif
void OSC_CYCLE(const user_osc_param_t * const params, int32_t *yn, const uint32_t frames)
{
if (s_state) {
#ifdef WFGEN
if ((s_state & state_wave_changed) != 0) {
uint32_t i = 0;
for (i = 0; i < OPERATOR_COUNT && s_waveform[i] == s_waveform_current[i]; i++);
if (i < OPERATOR_COUNT) {
osc_wavebank_preload(i, s_waveform[i]);
s_waveform_current[i] = s_waveform[i];
for (uint32_t f = frames; f--; *yn++ = 0);
} else
s_state &= ~state_wave_changed;
return;
/*
for (uint32_t i = 0; i < OPERATOR_COUNT; i++) {
if (s_waveform[i] != s_waveform_current[i]) {
osc_wavebank_preload(i, s_waveform[i]);
s_waveform_current[i] = s_waveform[i];
}
}
for (uint32_t f = frames; f--; *yn++ = 0);
s_state &= ~state_wave_changed;
return;
*/
}
#endif
if (s_state == state_noteon) {
for (uint32_t i = 0; i < OPERATOR_COUNT; i++) {
for (uint32_t j = 0; j < EG_STAGE_COUNT - 1; j++) {
s_egsrate[i][j] = s_egsrate[i][j + EG_STAGE_COUNT];
s_sample_count[i][j] = s_sample_count[i][j + EG_STAGE_COUNT];
}
s_egstage[i] = 0;
if (s_opi)
s_phase[i] = 0;
//todo: to reset or not to reset - that is the question (stick with the operator phase init)
s_opval[i] = 0;
s_egval[i] = s_eglevel[i][EG_STAGE_COUNT - 1];
// setLevel();
// make it non-negative and apply -96dB to further fit EG level
// s_oplevel[i] = q31sub((usat_lsl(31, q31add(s_level_scaling[i], s_velocitylevel[i]), 0)), 0x7F000000);
setOpLevel(i);
}
s_sample_num = 0;
#ifdef PEG
s_pegval = s_peglevel[PEG_STAGE_COUNT - 1];
s_pegstage = s_peg_stage_start;
#endif
s_state &= ~state_noteon;
} else {
int32_t dl;
uint32_t samples;
for (uint32_t i = 0; i < OPERATOR_COUNT; i++) {
samples = s_sample_num;
dl = s_eglevel[i][EG_STAGE_COUNT - 1] - s_egval[i];
if (dl != 0) {
if (dl < 0) {
samples += dl * s_egsrate_recip[i][0];
} else {
s_egsrate[i][EG_STAGE_COUNT - 1] = s_egsrate[i][EG_STAGE_COUNT * 2 - 1];
samples += dl * s_egsrate_recip[i][1];
}
} else {
s_egsrate[i][EG_STAGE_COUNT - 1] = 0;
}
s_sample_count[i][EG_STAGE_COUNT - 1] = samples;
s_egstage[i] = EG_STAGE_COUNT - 1;
}
#ifdef PEG
dl = s_peglevel[PEG_STAGE_COUNT - 1] - s_pegval;
if (dl < 0) {
s_pegrate[PEG_STAGE_COUNT - 1] = - s_pegrate[PEG_STAGE_COUNT];
s_peg_sample_count[PEG_STAGE_COUNT - 1] = s_sample_num - dl * s_pegrate_releaserecip;
} else {
s_pegrate[PEG_STAGE_COUNT - 1] = s_pegrate[PEG_STAGE_COUNT];
s_peg_sample_count[PEG_STAGE_COUNT - 1] = s_sample_num + dl * s_pegrate_releaserecip;
}
s_pegstage = PEG_STAGE_COUNT - 1;
#endif
s_state &= ~(state_noteoff | state_noteon);
}
}
q31_t osc_out, modw0;
q31_t opw0[OPERATOR_COUNT];
#ifdef FINE_TUNE
uint32_t pitch = params->pitch << 16;
#else
uint32_t pitch = params->pitch;
#endif
#ifndef KIT_MODE
if (s_kit_voice)
#endif
#ifdef FINE_TUNE
pitch = KIT_CENTER << 24;
#else
pitch = KIT_CENTER << 8;
#endif
// int32_t pitch = params->pitch + s_transpose;
#ifdef PEG
#ifdef FINE_TUNE
pitch += s_pegval;
#else
pitch += s_pegval >> 16;
#endif
#endif
// pitch_t basew0 = f32_to_pitch(osc_w0f_for_note((pitch >> 8) + s_transpose, pitch & 0xFF));
pitch_t basew0;
for (uint32_t i = 0; i < OPERATOR_COUNT; i++) {
if (s_pitchfreq[i]) {
#ifdef FINE_TUNE
uint32_t p;
p = pitch + (s_detune[i] + paramOffset(s_detune_offset, i) * 2.56f) * paramScale(s_detune_scale, i) * FINE_TUNE_FACTOR;
uint8_t note = clipmini32(0, (p >> 24) + s_zone_transposed);
basew0 = f32_to_pitch(clipmaxf(linintf((p & 0xFFFFFF) * 5.9604645e-8f, osc_notehzf(note), osc_notehzf(note + 1)), k_note_max_hz) * k_samplerate_recipf);
#else
basew0 = f32_to_pitch(osc_w0f_for_note(((pitch + s_detune[i]) >> 8) + s_transpose, (pitch + s_detune[i]) & 0xFF));
#endif
opw0[i] = pitch_to_phase(pitch_mul(s_oppitch[i], basew0));
} else
opw0[i] = pitch_to_phase(s_oppitch[i]);
/*
int32_t p = s_oppitch[i] + s_detune[i];
if (s_pitchfreq[i])
p += pitch;
p = usat(p, 16);
opw0[i] = f32_to_q31(osc_w0f_for_note(p >> 8, p & 0xFF));
*/
}
q31_t * __restrict y = (q31_t *)yn;
///ldr r10, &s_opval
///ldm r10, {r0, r1, r2, r3}
for (uint32_t f = frames; f--; y++) {
#ifdef ROLLOUT
modw0 = 0;
__asm__ volatile ( \
"lsls r1, %[s_algorithm_i], #26\n" \
"itt mi\n" \
"ldrmi.w r1, [%[s_opval], #24]\n" \
"addmi %[modw0], %[modw0], r1\n" \
: [modw0] "+r" (modw0) \
: [s_algorithm_i] "l" (s_algorithm[0]), [s_opval] "r" (s_opval) \
: "r1" \
);
modw0 = ((smmul(modw0, MI_SCALE_FACTOR)) << 3) + s_phase[0];
#ifdef WFBITS
s_opval[0] = smmul(osc_wavebank(modw0, s_waveform[0]), param_eglut(s_egval[0], s_oplevel[0])) << 1;
#else
s_opval[0] = smmul(osc_sinq(modw0), param_eglut(s_egval[0], s_oplevel[0])) << 1;
#endif
modw0 = 0;
__asm__ volatile ( \
"lsls r1, %[s_algorithm_i], #26\n" \
"itt mi\n" \
"ldrmi.w r1, [%[s_opval], #24]\n" \
"addmi %[modw0], %[modw0], r1\n" \
"lsls r1, %[s_algorithm_i], #31\n" \
"itt mi\n" \
"ldrmi.w r1, [%[s_opval], #0]\n" \
"addmi %[modw0], %[modw0], r1\n" \
: [modw0] "+r" (modw0) \
: [s_algorithm_i] "l" (s_algorithm[1]), [s_opval] "r" (s_opval) \
: "r1" \
);
modw0 = ((smmul(modw0, MI_SCALE_FACTOR)) << 3) + s_phase[1];
#ifdef WFBITS
s_opval[1] = smmul(osc_wavebank(modw0, s_waveform[1]), param_eglut(s_egval[1], s_oplevel[1])) << 1;
#else
s_opval[1] = smmul(osc_sinq(modw0), param_eglut(s_egval[1], s_oplevel[1])) << 1;
#endif
modw0 = 0;
__asm__ volatile ( \
"lsls r1, %[s_algorithm_i], #26\n" \
"itt mi\n" \
"ldrmi.w r1, [%[s_opval], #24]\n" \
"addmi %[modw0], %[modw0], r1\n" \
"lsls r1, %[s_algorithm_i], #30\n" \
"itt mi\n" \
"ldrmi.w r1, [%[s_opval], #4]\n" \
"addmi %[modw0], %[modw0], r1\n" \
"lsls r1, %[s_algorithm_i], #31\n" \
"itt mi\n" \
"ldrmi.w r1, [%[s_opval], #0]\n" \
"addmi %[modw0], %[modw0], r1\n" \
"end%=:\n" \
: [modw0] "+r" (modw0) \
: [s_algorithm_i] "l" (s_algorithm[2]), [s_opval] "r" (s_opval) \
: "r1" \
);
modw0 = ((smmul(modw0, MI_SCALE_FACTOR)) << 3) + s_phase[2];
#ifdef WFBITS
s_opval[2] = smmul(osc_wavebank(modw0, s_waveform[2]), param_eglut(s_egval[2], s_oplevel[2])) << 1;
#else
s_opval[2] = smmul(osc_sinq(modw0), param_eglut(s_egval[2], s_oplevel[2])) << 1;
#endif
modw0 = 0;
__asm__ volatile ( \
"lsls r1, %[s_algorithm_i], #26\n" \
"itt mi\n" \
"ldrmi.w r1, [%[s_opval], #24]\n" \
"addmi %[modw0], %[modw0], r1\n" \
"lsls r1, %[s_algorithm_i], #29\n" \