forked from grzegorz-kraszewski/libdigibooster3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.c
executable file
·2392 lines (1904 loc) · 69.4 KB
/
player.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
/*-----------------*/
/* libdigibooster3 */
/*-----------------*/
/*
Copyright (c) 2014, Grzegorz Kraszewski
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
This software is provided by the copyright holders and contributors "as is" and
any express or implied warranties, including, but not limited to the implied
warranties of merchantability and fitness for a particular purpose are
disclaimed. In no event shall the copyright owner or contributors be liable for
any direct, indirect, incidental, special, exemplary, or consequential damages
(including, but not limited to, procurement of substitute goods or services;
loss of use, data, or profits; or business interruption) however caused and
on any theory of liability, whether in contract strict liability or tort
(including negligence or otherwise) arising in any way out of the use of this
software, even if advised of the possibility of such damage.
*/
/* DBM play code */
#include "libdigibooster3.h"
#include "dsp.h"
#include "player.h"
#define porta_to_note(me) ((me->Cmd1 == 3) || (me->Cmd1 == 5) || (me->Cmd2 == 3) || (me->Cmd2 == 5))
uint32_t MusicScale[96] =
{
// 0 +1 +2 +3 +4 +5 +6 +7 finetune
65536, 66011, 66489, 66971, 67456, 67945, 68438, 68933, // C
69433, 69936, 70443, 70953, 71468, 71985, 72507, 73032, // C#
73562, 74095, 74632, 75172, 75717, 76266, 76819, 77375, // D
77936, 78501, 79069, 79642, 80220, 80801, 81386, 81976, // D#
82570, 83169, 83771, 84378, 84990, 85606, 86226, 86851, // E
87480, 88114, 88752, 89396, 90043, 90696, 91353, 92015, // F
92682, 93354, 94030, 94711, 95398, 96089, 96785, 97487, // F#
98193, 98905, 99621, 100340, 101070, 101800, 102540, 103280, // G
104032, 104786, 105545, 106310, 107080, 107856, 108638, 109425, // G#
110218, 111017, 111821, 112631, 113448, 114270, 115098, 115932, // A
116772, 117618, 118470, 119329, 120194, 121065, 121942, 122825, // A#
123715, 124612, 125515, 126425, 127341, 128263, 129193, 130129 // H
};
uint32_t SPorta1[1] = { 65536 };
uint32_t SPorta2[2] = { 65536, 65773 };
uint32_t SPorta3[3] = { 65536, 65694, 65852 };
uint32_t SPorta4[4] = { 65536, 65654, 65773, 65892 };
uint32_t SPorta5[5] = { 65536, 65631, 65726, 65821, 65916 };
uint32_t SPorta6[6] = { 65536, 65615, 65694, 65773, 65852, 65932 };
uint32_t SPorta7[7] = { 65536, 65604, 65671, 65739, 65807, 65875, 65943 };
uint32_t SPorta8[8] = { 65536, 65595, 65654, 65714, 65773, 65832, 65892, 65951 };
uint32_t SPorta9[9] = { 65536, 65589, 65641, 65694, 65747, 65799, 65852, 65905, 65958 };
uint32_t SPorta10[10] = { 65536, 65583, 65631, 65678, 65726, 65773, 65821, 65868, 65916, 65963 };
uint32_t SPorta11[11] = { 65536, 65579, 65622, 65665, 65708, 65751, 65795, 65838, 65881, 65924, 65968 };
uint32_t SPorta12[12] = { 65536, 65575, 65615, 65654, 65694, 65733, 65773, 65813, 65852, 65892, 65932,
65971 };
uint32_t SPorta13[13] = { 65536, 65572, 65609, 65645, 65682, 65718, 65755, 65791, 65828, 65864, 65901,
65938, 65974 };
uint32_t SPorta14[14] = { 65536, 65570, 65604, 65637, 65671, 65705, 65739, 65773, 65807, 65841, 65875,
65909, 65943, 65977 };
uint32_t SPorta15[15] = { 65536, 65568, 65599, 65631, 65662, 65694, 65726, 65757, 65789, 65821, 65852,
65884, 65916, 65947, 65979 };
uint32_t SPorta16[16] = { 65536, 65566, 65595, 65625, 65654, 65684, 65714, 65743, 65773, 65803, 65832,
65862, 65892, 65922, 65951, 65981 };
uint32_t SPorta17[17] = { 65536, 65564, 65592, 65620, 65647, 65675, 65703, 65731, 65759, 65787, 65815,
65843, 65871, 65899, 65927, 65955, 65983 };
uint32_t SPorta18[18] = { 65536, 65562, 65589, 65615, 65641, 65668, 65694, 65720, 65747, 65773, 65799,
65826, 65852, 65879, 65905, 65932, 65958, 65984 };
uint32_t SPorta19[19] = { 65536, 65561, 65586, 65611, 65636, 65661, 65686, 65711, 65736, 65761, 65786,
65811, 65836, 65861, 65886, 65911, 65936, 65961, 65986 };
uint32_t SPorta20[20] = { 65536, 65560, 65583, 65607, 65631, 65654, 65678, 65702, 65726, 65749, 65773,
65797, 65821, 65844, 65868, 65892, 65916, 65939, 65963, 65987 };
uint32_t SPorta21[21] = { 65536, 65559, 65581, 65604, 65626, 65649, 65671, 65694, 65717, 65739, 65762,
65784, 65807, 65830, 65852, 65875, 65898, 65920, 65943, 65966, 65988 };
uint32_t SPorta22[22] = { 65536, 65558, 65579, 65601, 65622, 65644, 65665, 65687, 65708, 65730, 65751,
65773, 65795, 65816, 65838, 65859, 65881, 65903, 65924, 65946, 65968, 65989 };
uint32_t SPorta23[23] = { 65536, 65557, 65577, 65598, 65618, 65639, 65660, 65680, 65701, 65721, 65742,
65763, 65783, 65804, 65825, 65845, 65866, 65887, 65907, 65928, 65949, 65969,
65990 };
uint32_t SPorta24[24] = { 65536, 65556, 65575, 65595, 65615, 65635, 65654, 65674, 65694, 65714, 65733,
65753, 65773, 65793, 65813, 65832, 65852, 65872, 65892, 65912, 65932, 65951,
65971, 65991 };
uint32_t SPorta25[25] = { 65536, 65555, 65574, 65593, 65612, 65631, 65650, 65669, 65688, 65707, 65726,
65745, 65764, 65783, 65802, 65821, 65840, 65859, 65878, 65897, 65916, 65935,
65954, 65973, 65992 };
uint32_t SPorta26[26] = { 65536, 65554, 65572, 65591, 65609, 65627, 65645, 65664, 65682, 65700, 65718,
65737, 65755, 65773, 65791, 65810, 65828, 65846, 65864, 65883, 65901, 65919,
65938, 65956, 65974, 65993 };
uint32_t SPorta27[27] = { 65536, 65554, 65571, 65589, 65606, 65624, 65641, 65659, 65676, 65694, 65711,
65729, 65747, 65764, 65782, 65799, 65817, 65835, 65852, 65870, 65887, 65905,
65923, 65940, 65958, 65976, 65993 };
uint32_t SPorta28[28] = { 65536, 65553, 65570, 65587, 65604, 65621, 65637, 65654, 65671, 65688, 65705,
65722, 65739, 65756, 65773, 65790, 65807, 65824, 65841, 65858, 65875, 65892,
65909, 65926, 65943, 65960, 65977, 65994 };
uint32_t SPorta29[29] = { 65536, 65552, 65569, 65585, 65601, 65618, 65634, 65650, 65667, 65683, 65699,
65716, 65732, 65748, 65765, 65781, 65798, 65814, 65830, 65847, 65863, 65880,
65896, 65912, 65929, 65945, 65962, 65978, 65994 };
uint32_t SPorta30[30] = { 65536, 65552, 65568, 65583, 65599, 65615, 65631, 65647, 65662, 65678, 65694,
65710, 65726, 65741, 65757, 65773, 65789, 65805, 65821, 65836, 65852, 65868,
65884, 65900, 65916, 65932, 65947, 65963, 65979, 65995 };
uint32_t SPorta31[31] = { 65536, 65551, 65567, 65582, 65597, 65612, 65628, 65643, 65658, 65674, 65689,
65704, 65719, 65735, 65750, 65765, 65781, 65796, 65811, 65827, 65842, 65857,
65873, 65888, 65903, 65919, 65934, 65949, 65965, 65980, 65996 };
uint32_t* SmoothPorta[32] = { NULL, SPorta1, SPorta2, SPorta3, SPorta4, SPorta5,
SPorta6, SPorta7, SPorta8, SPorta9, SPorta10, SPorta11, SPorta12, SPorta13,
SPorta14, SPorta15, SPorta16, SPorta17, SPorta18, SPorta19, SPorta20, SPorta21,
SPorta22, SPorta23, SPorta24, SPorta25, SPorta26, SPorta27, SPorta28, SPorta29,
SPorta30, SPorta31 };
int16_t Vibrato[64] = {
0, 25, 50, 74, 98, 121, 142, 162,
181, 197, 213, 226, 237, 245, 251, 255,
256, 255, 251, 245, 237, 226, 213, 197,
181, 162, 142, 121, 98, 74, 50, 25,
0, -25, -50, -74, -98, -121, -142, -162,
-181, -197, -213, -226, -237, -245, -251, -255,
-256, -255, -251, -245, -237, -226, -213, -197,
-181, -162, -142, -121, -98, -74, -50, -25
};
uint32_t DbVals[6] = { 65536, 73562, 82570, 92682, 104032, 116772 };
#define LOOPDIR_FORWARD 0
#define LOOPDIR_BACKWARD 1
#define ITERATE_LIST(listptr, type, var) \
for (var = (type)((struct MinList*)listptr)->mlh_Head; \
((struct MinNode*)var)->mln_Succ; \
var = (type)((struct MinNode*)var)->mln_Succ)
#define INIT_LIST(listptr) \
((struct MinList*)listptr)->mlh_Head = (struct MinNode*)&((struct MinList*)listptr)->mlh_Tail; \
((struct MinList*)listptr)->mlh_Tail = NULL; \
((struct MinList*)listptr)->mlh_TailPred = (struct MinNode*)&((struct MinList*)listptr)->mlh_Head;
//==============================================================================================
// bcd2bin()
//==============================================================================================
uint8_t bcd2bin(uint8_t x)
{
uint8_t r = 0;
if ((x >> 4) < 10) r = (x >> 4) * 10;
else return 0;
if ((x & 0xF) < 10) r += (x & 0xF);
else return 0;
return r;
}
//==============================================================================================
// msynth_dsp_dispose_chain()
//==============================================================================================
void msynth_dsp_dispose_chain(struct MinList *chain)
{
struct DSPObject *dspo;
while ((dspo = (struct DSPObject*)DB3RemHead(chain)) != NULL)
{
dspo->dsp_dispose(dspo);
}
}
//==============================================================================================
// msynth_dsp_set_instr_attrs()
//==============================================================================================
void msynth_dsp_set_instr_attrs(struct ModTrack *mt, struct DSPTag *tags)
{
struct DSPObject *dspo;
ITERATE_LIST(&mt->DSPInstrChain, struct DSPObject*, dspo)
{
dspo->dsp_set(dspo, tags);
}
}
//==============================================================================================
// msynth_dsp_set_track_attrs()
//==============================================================================================
void msynth_dsp_set_track_attrs(struct ModTrack *mt, struct DSPTag *tags)
{
struct DSPObject *dspo;
ITERATE_LIST(&mt->DSPTrackChain, struct DSPObject*, dspo)
{
dspo->dsp_set(dspo, tags);
}
}
//==============================================================================================
// msynth_dsp_get_instr_attr()
//==============================================================================================
int32_t msynth_dsp_get_instr_attr(struct ModTrack *mt, uint32_t tag)
{
struct DSPObject *dspo;
int32_t value = 0;
ITERATE_LIST(&mt->DSPInstrChain, struct DSPObject*, dspo)
{
if (dspo->dsp_get(dspo, tag, &value)) return value;
}
return 0;
}
//==============================================================================================
// msynth_dsp_get_track_attr()
//==============================================================================================
int32_t msynth_dsp_get_track_attr(struct ModTrack *mt, uint32_t tag)
{
struct DSPObject *dspo;
int32_t value = 0;
ITERATE_LIST(&mt->DSPTrackChain, struct DSPObject*, dspo)
{
if (dspo->dsp_get(dspo, tag, &value)) return value;
}
return 0;
}
//==============================================================================================
// msynth_trigger()
//==============================================================================================
void msynth_trigger(struct DB3Module *m, struct ModTrack *mt)
{
struct DSPObject *last;
mt->VolEnvCurrent = 16384;
mt->PanEnvCurrent = 0;
// Reset envelopes interpolators.
mt->VolEnv.Section = 0;
mt->VolEnv.TickCtr = 0;
if (mt->VolEnv.Index != 0xFFFF)
{
mt->VolEnv.SustainA = m->VolEnvs[mt->VolEnv.Index].SustainA;
mt->VolEnv.SustainB = m->VolEnvs[mt->VolEnv.Index].SustainB;
mt->VolEnv.LoopEnd = m->VolEnvs[mt->VolEnv.Index].LoopLast;
}
mt->PanEnv.Section = 0;
mt->PanEnv.TickCtr = 0;
if (mt->PanEnv.Index != 0xFFFF)
{
mt->PanEnv.SustainA = m->PanEnvs[mt->PanEnv.Index].SustainA;
mt->PanEnv.SustainB = m->PanEnvs[mt->PanEnv.Index].SustainB;
mt->PanEnv.LoopEnd = m->PanEnvs[mt->PanEnv.Index].LoopLast;
}
// Set sample offset and loop direction. Flush any buffers.
if ((last = (struct DSPObject*)mt->DSPInstrChain.mlh_TailPred) != NULL)
{
last->dsp_flush(last);
if (mt->PlayBackwards)
{
struct DSPTag tags[3] = {
{ DSPA_ReversePlay, TRUE }, // must be before DSPA_SampleOffset
{ DSPA_SampleOffset, mt->TrigOffset },
{ 0, 0 }
};
msynth_dsp_set_instr_attrs(mt, tags);
}
else
{
struct DSPTag tags[3] = {
{ DSPA_ReversePlay, FALSE }, // must be before DSPA_SampleOffset
{ DSPA_SampleOffset, mt->TrigOffset },
{ 0, 0 }
};
msynth_dsp_set_instr_attrs(mt, tags);
}
mt->VibratoCounter = 0;
mt->TrigOffset = 0;
mt->IsOn = 1;
}
}
//==============================================================================================
// msynth_instrument()
//==============================================================================================
int msynth_instrument(struct ModSynth *msyn, struct ModTrack *mt, int instr)
{
struct DB3ModInstr *mi;
msynth_dsp_dispose_chain(&mt->DSPInstrChain);
mt->Instr = 0;
mt->IsOn = 0;
/*-----------------------------------------------------------------*/
/* Added 20.11.2009: Some modules contain triggers of non-existing */
/* instruments. Such instruments should be silently ignored. */
/*-----------------------------------------------------------------*/
if (instr > msyn->Mod->NumInstr) return FALSE;
mi = msyn->Mod->Instruments[instr - 1];
/*--------------------------------*/
/* Enable envelope interpolators. */
/*--------------------------------*/
mt->VolEnv.Index = mi->VolEnv;
mt->PanEnv.Index = mi->PanEnv;
switch (mi->Type)
{
case ITYPE_SAMPLE:
{
struct DB3ModInstrS *mis = (struct DB3ModInstrS*)mi;
struct DB3ModSample *ms = msyn->Mod->Samples[mis->SampleNum];
struct DSPObject *unroller, *resampler, *panoramizer;
// There may be instruments with proper, but empty samples. If such an
// instrument is triggered, just turn the channel off to the next trigger.
if (!ms || !ms->Data || !ms->Frames) return FALSE;
if ((mis->Flags & IF_LOOP_MASK) == IF_NO_LOOP)
{
unroller = dsp_sampled_instr_new(ms->Data, 0, 0, 0, ms->Frames, IF_NO_LOOP);
}
else // Forward or pingpong loop. I assume mis->LoopLen > 0.
{
unroller = dsp_sampled_instr_new(ms->Data, mis->LoopStart,
mis->LoopLen, 0x7FFFFFFF, ms->Frames, mis->Flags & IF_LOOP_MASK);
}
resampler = dsp_resampler20_new();
panoramizer = dsp_panoramizer_new(msyn->PanPhaseTable);
if (unroller && resampler && panoramizer)
{
DB3AddTail(&mt->DSPInstrChain, (struct MinNode*)unroller);
DB3AddTail(&mt->DSPInstrChain, (struct MinNode*)resampler);
DB3AddTail(&mt->DSPInstrChain, (struct MinNode*)panoramizer);
mt->Instr = instr;
return TRUE;
}
if (unroller) unroller->dsp_dispose(unroller);
if (resampler) resampler->dsp_dispose(resampler);
if (panoramizer) panoramizer->dsp_dispose(panoramizer);
}
break;
}
return FALSE;
}
//==============================================================================================
// msynth_pitch()
//==============================================================================================
// Sets the pitch for the current instrument on the track. Does neither set nor
// retrigger the instrument. 'pitch' parameter is in finetune unit prescaled by
// current module speed. 0 of pitch is C-0 note.
void msynth_pitch(struct ModSynth *msyn, struct ModTrack *mt, uint16_t pitch)
{
if (mt->Instr)
{
struct DB3ModInstr *mi = msyn->Mod->Instruments[mt->Instr - 1];
if (mi)
{
switch (mi->Type)
{
case ITYPE_SAMPLE:
{
struct DB3ModInstrS *mis = (struct DB3ModInstrS*)mi;
uint32_t samplestep, alpha, beta;
uint64_t samplestep64 = 0;
uint16_t f_tune, s_porta, octave = 0;
struct DSPTag tags[2] = {{ DSPA_ResamplerRatio, 0 }, { 0, 0 }};
s_porta = pitch % msyn->Speed;
f_tune = pitch / msyn->Speed;
alpha = SmoothPorta[msyn->Speed][s_porta];
f_tune -= 96;
while (f_tune >= 96)
{
f_tune -= 96;
octave++;
}
beta = MusicScale[f_tune];
samplestep64 = (uint64_t)mis->C3Freq * beta * alpha;
samplestep64 >>= 19 - octave;
samplestep = (uint32_t)(samplestep64 / msyn->MixFreq);
tags[0].dspt_data = samplestep;
msynth_dsp_set_instr_attrs(mt, tags);
}
}
}
}
}
//==============================================================================================
// msynth_defvolume()
//==============================================================================================
void msynth_defvolume(struct ModSynth *msyn, struct ModTrack *mt)
{
struct DB3ModInstr *minst;
if (mt->Instr)
{
minst = msyn->Mod->Instruments[mt->Instr - 1];
mt->Volume = minst->Volume * msyn->Speed;
mt->Panning = minst->Panning * msyn->Speed;
// Added 05.11.2009. Volume reset should also restart panning and volume
// envelopes for the instrument.
// Reset envelopes interpolators.
mt->VolEnv.Section = 0;
mt->VolEnv.TickCtr = 0;
mt->PanEnv.Section = 0;
mt->PanEnv.TickCtr = 0;
}
}
//==============================================================================================
// msynth_reset_delayed()
//==============================================================================================
void msynth_reset_delayed(struct ModSynth *msyn)
{
msyn->DelPattBreak = -1;
msyn->DelPattJump = -1;
msyn->DelLoop = -1;
msyn->DelModuleEnd = 0;
}
//==============================================================================================
// msynth_reset_loop()
//==============================================================================================
void msynth_reset_loop(struct ModSynth *msyn)
{
msyn->LoopCounter = 0;
msyn->LoopOrder = 0;
msyn->LoopRow = 0;
}
//==============================================================================================
// msynth_apply_delayed()
//==============================================================================================
void msynth_apply_delayed(struct ModSynth *msyn)
{
struct DB3ModSong *song = msyn->Mod->Songs[msyn->Song];
// Position jump.
if (msyn->DelPattJump != -1)
{
if (msyn->DelPattJump < song->NumOrders) msyn->Order = msyn->DelPattJump;
else msyn->Order = 0;
msyn->Pattern = song->PlayList[msyn->Order];
msyn->Row = 0;
}
// Pattern break.
if (msyn->DelPattBreak != -1)
{
struct DB3ModPatt *mpatt;
if ((msyn->DelPattJump == -1) && (msyn->Row > 0))
{
if (++msyn->Order >= song->NumOrders) msyn->Order = 0;
}
msyn->Pattern = song->PlayList[msyn->Order];
mpatt = msyn->Mod->Patterns[msyn->Pattern];
if (msyn->DelPattBreak < mpatt->NumRows) msyn->Row = msyn->DelPattBreak;
else msyn->Row = mpatt->NumRows - 1;
}
// Loops.
if (msyn->DelLoop != -1)
{
msyn->Order = msyn->LoopOrder;
msyn->Pattern = song->PlayList[msyn->Order];
msyn->Row = msyn->LoopRow;
}
// Module end.
if (msyn->DelModuleEnd) msyn->PatternDelay = 0x7FFFFFFF;
// Reset all delayed things.
msynth_reset_delayed(msyn);
}
//==============================================================================================
// msynth_echo_on_for_track()
//==============================================================================================
// Used for both standard, global echo and new per-track echo. The only difference is echo type,
// as per-track echo parameters default to standard ones. Echo is inserted as the last object in
// the track DSP chain. Echo should not be added more than one time, so if DSPTYPE_ECHO object
// already exists in the track DSP chain, this function does nothing.
void msynth_echo_on_for_track(struct ModSynth *msyn, struct ModTrack *mt, int type)
{
struct DSPObject *echo;
// checking echo module existence
if (msynth_dsp_get_track_attr(mt, DSPA_EchoType) > 0) return;
// adding echo
echo = dsp_echo_new(msyn->MixFreq, type);
if (echo)
{
DB3AddTail(&mt->DSPTrackChain, (struct MinNode*)echo);
mt->EchoType = type;
}
}
//==============================================================================================
// msynth_echo_off_for_track
//==============================================================================================
// Searches the track DSP chain for [the first] DSPTYPE_ECHO object and removes it. If there is
// no DSPTYPE_ECHO object in the track DSP chain, the function does nothing. The function
// removes echo only if it matches passed 'type'.
inline void msynth_echo_off_for_track(struct ModTrack *mt, int type)
{
struct DSPObject *obj;
int echo_type;
// the check below exits the function either if there is no echo in the chain, or echo type
// does not match
echo_type = msynth_dsp_get_track_attr(mt, DSPA_EchoType);
if (echo_type != type) return;
// locate echo object and remove it
ITERATE_LIST(&mt->DSPTrackChain, struct DSPObject*, obj)
{
if (obj->dsp_type == DSPTYPE_ECHO)
{
DB3Remove((struct MinNode*)obj);
obj->dsp_dispose(obj);
break;
}
}
}
//==============================================================================================
// msynth_echo_on_for_all_tracks()
//==============================================================================================
// Adds old style (global controlled) echo for all tracks not having any echo.
void msynth_echo_on_for_all_tracks(struct ModSynth *msyn)
{
int track_number;
for (track_number = 0; track_number < msyn->Mod->NumTracks; track_number++)
{
msynth_echo_on_for_track(msyn, &msyn->Tracks[track_number], DSPV_EchoType_Old);
}
}
//==============================================================================================
// msynth_echo_off_for_all_tracks()
//==============================================================================================
// It only switches off the standard echo. New style echo must be switched off for each track
// with V21 command.
void msynth_echo_off_for_all_tracks(struct ModSynth *msyn)
{
int track_number;
for (track_number = 0; track_number < msyn->Mod->NumTracks; track_number++)
{
msynth_echo_off_for_track(&msyn->Tracks[track_number], DSPV_EchoType_Old);
}
}
//==============================================================================================
// msynth_change_echo_params()
//==============================================================================================
// This function checks the echo type in the track. If there is no echo, or echo is
// DSPV_EchoType_Old, echo parameters are set for all tracks with old echo. If echo is
// DSPV_EchoType_New, parameters are changed for this track only.
void msynth_change_echo_params(struct ModSynth *msyn, struct ModTrack *mt)
{
struct DSPTag tags[5];
tags[0].dspt_tag = DSPA_EchoFeedback; tags[0].dspt_data = mt->EchoFeedback;
tags[1].dspt_tag = DSPA_EchoMix; tags[1].dspt_data = mt->EchoMix;
tags[2].dspt_tag = DSPA_EchoCross; tags[2].dspt_data = mt->EchoCross;
tags[3].dspt_tag = DSPA_EchoDelay; tags[3].dspt_data = mt->EchoDelay;
tags[4].dspt_tag = TAG_END;
if (msynth_dsp_get_track_attr(mt, DSPA_EchoType) == DSPV_EchoType_New) msynth_dsp_set_track_attrs(mt, tags);
else
{
int track_num;
struct ModTrack *mt2;
for (track_num = 0; track_num < msyn->Mod->NumTracks; track_num++)
{
mt2 = &msyn->Tracks[track_num];
if (msynth_dsp_get_track_attr(mt2, DSPA_EchoType) == DSPV_EchoType_Old)
{
mt2->EchoFeedback = mt->EchoFeedback;
mt2->EchoMix = mt->EchoMix;
mt2->EchoCross = mt->EchoCross;
mt2->EchoDelay = mt->EchoDelay;
msynth_dsp_set_track_attrs(mt2, tags);
}
}
}
}
//==============================================================================================
// msynth_effect_exx()
//==============================================================================================
void msynth_effect_exx(struct ModSynth *msyn, struct ModTrack *mt, uint8_t param)
{
switch (param >> 4)
{
/* ====== E1x - FINE PORTAMENTO UP ====== */
case 0x1:
mt->Pitch += (param & 0xF) * msyn->Speed;
if (mt->Pitch > msyn->MaxPitch) mt->Pitch = msyn->MaxPitch;
break;
/* ====== E2x - FINE PORTAMENTO DOWN ====== */
case 0x2:
mt->Pitch -= (param & 0xF) * msyn->Speed;
if (mt->Pitch < msyn->MinPitch) mt->Pitch = msyn->MinPitch;
break;
/* ====== E3x - PLAY BACKWARDS ====== */
case 0x3:
if (mt->TrigCounter != 0x7FFF) mt->PlayBackwards = TRUE;
break;
/* ====== E4x - CHANNEL CONTROL A ====== */
case 0x4:
if (param == 0x40) /* E40 - track mute */
{
mt->IsOn = FALSE;
}
break;
/* ====== E6x - PLAY LOOP ========== */
case 0x6:
if (param & 0xF)
{
if (msyn->LoopCounter == 0)
{
msyn->LoopCounter = param & 0xF;
msyn->DelLoop = 0;
}
else
{
if (--msyn->LoopCounter > 0) msyn->DelLoop = 0;
else msynth_reset_loop(msyn);
}
}
else /* E60 */
{
if (msyn->LoopCounter == 0)
{
msyn->LoopOrder = msyn->Order;
msyn->LoopRow = msyn->Row;
}
}
break;
/* ====== E7x - COARSE SAMPLE OFFSET ====== */
case 0x7:
mt->TrigOffset += (int32_t)(param & 0xF) << 16;
break;
/* ====== E8x - COARSE PANNING ====== */
case 0x8:
mt->Panning = (((int16_t)(param & 0xF) << 4) - 128) * msyn->Speed;
break;
/* ====== E9x - NOTE RETRIGGER ====== */
case 0x9:
mt->Retrigger = param & 0xF;
break;
/* ====== EAx - FINE VOLUME SLIDE UP ====== */
case 0xA:
mt->Volume += (param & 0xF) * msyn->Speed;
if (mt->Volume > msyn->MaxVolume) mt->Volume = msyn->MaxVolume;
break;
/* ====== EBx - FINE VOLUME SLIDE DOWN ====== */
case 0xB:
mt->Volume -= (param & 0xF) * msyn->Speed;
if (mt->Volume < msyn->MinVolume) mt->Volume = msyn->MinVolume;
break;
/* ====== ECx - NOTE CUT ====== */
case 0xC:
mt->CutCounter = param & 0xF;
break;
/* ====== EDx - NOTE DELAY ====== */
case 0xD:
mt->TrigCounter = param & 0xF;
break;
/* ====== EEx - PATTERN DELAY ====== */
case 0xE:
msyn->PatternDelay = param & 0x0F;
break;
}
}
//==============================================================================================
// msynth_effect()
//==============================================================================================
void msynth_effect(struct ModSynth *msyn, struct ModTrack *mt, uint8_t cmd, uint8_t param)
{
switch (cmd)
{
/* ====== 0xx - APPREGIO ====== */
case 0x0:
mt->ApprTable[1] = ((param >> 4) << 3) * msyn->Speed;
mt->ApprTable[2] = ((param & 0xF) << 3) * msyn->Speed;
break;
/* ====== 1xx - PORTAMENTO UP (includes 1Fx) ====== */
case 0x1:
{
if (!param) param = mt->Old.PortaUp; // 100, reuse old parameter
else mt->Old.PortaUp = param;
if (param < 0xF0) mt->PitchDelta += param * msyn->Speed;
else mt->PitchDelta += param & 0x0F; // smooth 1Fx
}
break;
/* ====== 2xx - PORTAMENTO DOWN (includes 2Fx) ====== */
case 0x2:
{
if (!param) param = mt->Old.PortaDown; // 200, reuse old parameter
else mt->Old.PortaDown = param;
if (param < 0xF0) mt->PitchDelta -= param * msyn->Speed;
else mt->PitchDelta -= param & 0x0F; // smooth 2Fx
}
break;
/* ====== 3xx - PORTA TO NOTE ====== */
case 0x3:
{
int16_t porta_target;
if (!param) param = mt->Old.PortaSpeed;
else mt->Old.PortaSpeed = param;
porta_target = mt->Porta3Target * msyn->Speed;
if (porta_target >= mt->Pitch) mt->Porta3Delta += param * msyn->Speed;
else mt->Porta3Delta -= param * msyn->Speed;
}
break;
/* ====== 4xx - VIBRATO ======= */
case 0x4:
if (!param) param = mt->Old.Vibrato;
else mt->Old.Vibrato = param;
mt->VibratoSpeed = param >> 4;
mt->VibratoDepth = (param & 0xF) * msyn->Speed;
break;
/* ====== 5xx - PORTA TO NOTE + VOLUME SLIDE ====== */
case 0x5:
{
int16_t porta_target, porta_speed, p0, p1;
if (!param) param = mt->Old.VolSlide5;
else mt->Old.VolSlide5 = param;
porta_speed = mt->Old.PortaSpeed;
porta_target = mt->Porta3Target * msyn->Speed;
if (porta_target >= mt->Pitch) mt->Porta3Delta += porta_speed * msyn->Speed;
else mt->Porta3Delta -= porta_speed * msyn->Speed;
p0 = param >> 4;
p1 = param & 0xF;
if ((p0 == 0) || (p1 == 0)) // Normal 50x/5x0
{
if (p0) mt->VolumeDelta += p0 * msyn->Speed;
if (p1) mt->VolumeDelta -= p1 * msyn->Speed;
}
else
{
if (p1 == 0xF) mt->VolumeDelta += p0;
else if (p0 == 0xF) mt->VolumeDelta -= p1;
}
}
break;
/* ====== 6xx - VIBRATO + VOLUME SLIDE ======= */
case 0x6:
{
int16_t p0, p1;
if (!param) param = mt->Old.Vibrato6;
else mt->Old.Vibrato6 = param;
mt->VibratoSpeed = mt->Old.Vibrato >> 4;
mt->VibratoDepth = (mt->Old.Vibrato & 0xF) * msyn->Speed;
p0 = param >> 4;
p1 = param & 0xF;
if ((p0 == 0) || (p1 == 0)) // Normal 60x/6x0
{
if (p0) mt->VolumeDelta += p0 * msyn->Speed;
if (p1) mt->VolumeDelta -= p1 * msyn->Speed;
}
else
{
if (p1 == 0xF) mt->VolumeDelta += p0;
else if (p0 == 0xF) mt->VolumeDelta -= p1;
}
}
break;
/* ====== 8xx - SET PANNING ====== */
case 0x8:
mt->Panning = ((int16_t)param - 128) * msyn->Speed;
break;
/* ====== 9xx - SAMPLE OFFSET ====== */
case 0x9:
mt->TrigOffset += (int32_t)param << 8;
break;
/* ====== Axx - VOLUME SLIDE (includes AxF and AFx) ====== */
case 0xA:
{
int16_t p0, p1;
if (!param) param = mt->Old.VolSlide; // A00, reuse old parameter
else mt->Old.VolSlide = param;
p0 = param >> 4;
p1 = param & 0xF;
if ((p0 == 0) || (p1 == 0)) // Normal A0x/Ax0
{
if (p0) mt->VolumeDelta += p0 * msyn->Speed;
if (p1) mt->VolumeDelta -= p1 * msyn->Speed;
}
else
{
if (p1 == 0xF) mt->VolumeDelta += p0;
else if (p0 == 0xF) mt->VolumeDelta -= p1;
}
}
break;
/* ====== Bxx - POSITION JUMP ====== */
case 0xB:
msyn->DelPattJump = param;
break;
/* ====== Cxx - INSTRUMENT VOLUME ====== */
case 0xC:
if (param <= 0x40) mt->Volume = param * msyn->Speed;
break;
/* ====== Dxx - PATTERN BREAK ====== */
case 0xD:
msyn->DelPattBreak = bcd2bin(param);
break;
/* ====== Exx - MISC EFFECTS ====== */
case 0xE:
msynth_effect_exx(msyn, mt, param);
break;
/* ====== Gxx - GLOBAL VOLUME ====== */
case 0x10:
if (param <= 0x40) msyn->GlobalVolume = param;
break;
/* ====== Hxx - GLOBAL VOLUME SLIDE ====== */
case 0x11: