-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathreplayer.c
1587 lines (1321 loc) · 36.6 KB
/
replayer.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
/*
** 8bb:
** Port of AHX 2.3d-sp3's replayer.
**
** This is a port of the replayer found in AHX 2.3d-sp3's tracker code,
** not the small external replayer binary. There are some minor
** differences between them.
*/
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <math.h> // ceil()
#include "replayer.h"
static const uint8_t waveOffsets[6] =
{
0x00,0x04,0x04+0x08,0x04+0x08+0x10,0x04+0x08+0x10+0x20,0x04+0x08+0x10+0x20+0x40
};
/* 8bb:
** Added this. 129 words from before the periodTable in AHX 2.3d-sp3 (68020 version).
** This is needed because the final note can accidentally be -1 .. -129, which is not
** safe when accesing the periodTable.
*/
static const int16_t beforePeriodTable_68020[129] =
{
0xF6F2,0xEEEA,0xE6E3,0x201B,0x1612,0x0E0A,0x0603,0x00FD,0xFAF8,0xF6F4,
0xF2F1,0x100D,0x0A08,0x0604,0x0201,0x00FF,0xFEFE,0xFEFE,0xFEFF,0x4A30,
0x0170,0x0000,0x0027,0x66FF,0x0000,0x00B2,0x4A30,0x0170,0x0000,0x0026,
0x6712,0x3770,0x0170,0x0000,0x0064,0x0006,0x51F0,0x0170,0x0000,0x0026,
0x4A30,0x0170,0x0000,0x0022,0x67FF,0x0000,0x007C,0x48E7,0x3F68,0x2470,
0x0170,0x0000,0x005C,0x0C30,0x0003,0x0170,0x0000,0x0014,0x67FF,0x0000,
0x0042,0x7C01,0x7405,0x9430,0x0170,0x0000,0x0015,0xE56E,0xCCFC,0x0005,
0x5346,0x2270,0x0170,0x0000,0x0060,0x7E01,0x7400,0x1430,0x0170,0x0000,
0x0015,0xE52F,0x5347,0x2619,0x24C3,0x51CF,0xFFFA,0x51CE,0xFFDE,0x60FF,
0x0000,0x0016,0x2270,0x0170,0x0000,0x0060,0x7E4F,0x24D9,0x24D9,0x51CF,
0xFFFA,0x4CDF,0x16FC,0x51F0,0x0170,0x0000,0x0022,0x3770,0x0170,0x0000,
0x0066,0x0008,0x4E75,0x377C,0x0000,0x0008,0x4E75,0x0004,0x0000,0x0001,
0x0000,0x0015,0x4C70,0x0015,0x4D6C,0x000E,0xA9C4,0x0015,0x5E68
};
static const int16_t periodTable[1+60] =
{
0,
3424,3232,3048,2880,2712,2560,2416,2280,2152,2032,1920,1812,
1712,1616,1524,1440,1356,1280,1208,1140,1076,1016, 960, 906,
856, 808, 762, 720, 678, 640, 604, 570, 538, 508, 480, 453,
428, 404, 381, 360, 339, 320, 302, 285, 269, 254, 240, 226,
214, 202, 190, 180, 170, 160, 151, 143, 135, 127, 120, 113
};
static const int16_t vibTable[64] =
{
0, 24, 49, 74, 97, 120, 141, 161,
180, 197, 212, 224, 235, 244, 250, 253,
255, 253, 250, 244, 235, 224, 212, 197,
180, 161, 141, 120, 97, 74, 49, 24,
0, -24, -49, -74, -97,-120,-141,-161,
-180,-197,-212,-224,-235,-244,-250,-253,
-255,-253,-250,-244,-235,-224,-212,-197,
-180,-161,-141,-120, -97, -74, -49, -24
};
// 8bb: globalized
volatile bool isRecordingToWAV;
song_t song;
waveforms_t *waves; // 8bb: dword-aligned from malloc()
uint8_t ahxErrCode;
// ------------
// 8bb: loader.c
bool ahxInitWaves(void);
void ahxFreeWaves(void);
// -----------
static void SetUpAudioChannels(void) // 8bb: only call this while mixer is locked!
{
plyVoiceTemp_t *ch;
paulaSetDMACON(0); // 8bb: stop all Paula voice DMAs
ch = song.pvt;
for (int32_t i = 0; i < PAULA_VOICES; i++, ch++)
{
ch->audioPointer = waves->currentVoice[i];
paulaSetPeriod(i, 0x88);
paulaSetData(i, ch->audioPointer);
paulaSetVolume(i, 0);
paulaSetLength(i, 0x280 / 2);
}
paulaSetDMACON(0x8000 | 15); // 8bb: start all Paula voice DMAs
}
static void InitVoiceXTemp(plyVoiceTemp_t *ch) // 8bb: only call this while mixer is locked!
{
int8_t *oldAudioPointer = ch->audioPointer;
memset(ch, 0, sizeof (plyVoiceTemp_t));
ch->TrackMasterVolume = 64;
ch->squareSignum = 1;
ch->squareLowerLimit = 1;
ch->squareUpperLimit = 63;
ch->audioPointer = oldAudioPointer;
}
static void ahxQuietAudios(void)
{
for (int32_t i = 0; i < PAULA_VOICES; i++)
paulaSetVolume(i, 0);
}
static void CopyWaveformToPaulaBuffer(plyVoiceTemp_t *ch) // 8bb: I put this code in an own function
{
// 8bb: audioPointer and audioSource buffers are dword-aligned, 32-bit access is safe
uint32_t *dst32 = (uint32_t *)ch->audioPointer;
if (ch->Waveform == 4-1) // 8bb: noise, copy in one go
{
const uint32_t *src32 = (const uint32_t *)ch->audioSource;
for (int32_t i = 0; i < 0x280/8; i++)
{
*dst32++ = *src32++;
*dst32++ = *src32++;
}
}
else
{
const int32_t length = (1 << (5 - ch->Wavelength)) * 5;
const int32_t copyLength = 1 << ch->Wavelength;
for (int32_t i = 0; i < length; i++)
{
const uint32_t *src32 = (const uint32_t *)ch->audioSource;
for (int32_t j = 0; j < copyLength; j++)
*dst32++ = *src32++;
}
}
}
static void SetAudio(int32_t chNum, plyVoiceTemp_t *ch)
{
// new PERIOD to plant ???
if (ch->PlantPeriod)
{
paulaSetPeriod(chNum, ch->audioPeriod);
ch->PlantPeriod = false;
}
// new FILTER or new WAVEFORM ???
if (ch->NewWaveform)
{
CopyWaveformToPaulaBuffer(ch);
ch->NewWaveform = false;
}
paulaSetVolume(chNum, ch->audioVolume);
}
static void ProcessStep(plyVoiceTemp_t *ch)
{
uint8_t note, instr, cmd, param;
ch->volumeSlideUp = 0; // means A cmd
ch->volumeSlideDown = 0; // means A cmd
if (ch->Track > song.highestTrack) // 8bb: added this (this is technically what happens in AHX on illegal tracks)
{
note = 0;
instr = 0;
cmd = 0;
param = 0;
}
else
{
const uint8_t *bytes = &song.TrackTable[((ch->Track << 6) + song.NoteNr) * 3];
note = (bytes[0] >> 2) & 0x3F;
instr = ((bytes[0] & 3) << 4) | (bytes[1] >> 4);
cmd = bytes[1] & 0xF;
param = bytes[2];
}
// Effect > E < - Enhanced Commands
if (cmd == 0xE)
{
uint8_t eCmd = param >> 4;
uint8_t eParam = param & 0xF;
if (eCmd == 0xC) // Effect > EC< - NoteCut
{
if (eParam < song.Tempo)
{
ch->NoteCutWait = eParam;
ch->NoteCutOn = true;
ch->HardCutRelease = false;
}
}
if (eCmd == 0xD) // Effect > ED< - NoteDelay
{
if (ch->NoteDelayOn)
{
ch->NoteDelayOn = false;
}
else if (eParam < song.Tempo)
{
ch->NoteDelayWait = eParam;
if (ch->NoteDelayWait != 0)
{
ch->NoteDelayOn = true;
return; // 8bb: yes, get out of here!
}
}
}
}
if (cmd == 0x0) // Effect > 0 < - PositionjumpHI
{
if (param != 0)
{
uint8_t pos = param & 0xF;
if (pos <= 9)
song.PosJump = (param & 0xF) << 8; // 8bb: yes, this clears the lower byte too!
}
}
// 8bb: effect 8 is for external timing/syncing (probably for games/demos), let's not include it
if (cmd == 0xD) // Effect > D < - Patternbreak
{
song.PosJump = song.PosNr + 1; // jump to next position (8bb: yes, it clears PosJump hi-byte)
song.PosJumpNote = ((param >> 4) * 10) + (param & 0xF);
if (song.PosJumpNote >= song.TrackLength)
song.PosJumpNote = 0;
song.PatternBreak = true;
}
if (cmd == 0xB) // Effect > B < - Positionjump
{
song.PosJump = (song.PosJump * 100) + ((param >> 4) * 10) + (param & 0xF);
song.PatternBreak = true;
}
if (cmd == 0xF) // Effect > F < - Set Tempo
{
song.Tempo = param;
// 8bb: added this for the WAV renderer
if (song.Tempo == 0)
isRecordingToWAV = false;
}
// Effect > 5 < - Volume Slide + Tone Portamento
// Effect > A < - Volume Slide
if (cmd == 0x5 || cmd == 0xA)
{
ch->volumeSlideDown = param & 0xF;
ch->volumeSlideUp = param >> 4;
}
// Instrument to initialize ?
if (instr > 0)
{
int16_t delta;
ch->perfSubVolume = 64;
// reset portamento
ch->periodPerfSlideSpeed = 0;
ch->periodSlidePeriod = 0;
ch->periodSlideLimit = 0;
// init adsr-envelope
instrument_t *ins = song.Instruments[instr-1];
if (ins == NULL) // 8bb: added this (this is technically what happens in AHX on illegal instruments)
ins = &song.EmptyInstrument;
ch->adsr = 0; // adsr starting at vol. 0!
ch->aFrames = ins->aFrames;
delta = ins->aVolume << 8;
if (ch->aFrames != 0)
delta /= ch->aFrames;
ch->aDelta = delta;
ch->dFrames = ins->dFrames;
delta = ((int8_t)ins->dVolume - (int8_t)ins->aVolume) << 8;
if (ch->dFrames != 0)
delta /= ch->dFrames;
ch->dDelta = delta;
ch->sFrames = ins->sFrames;
ch->rFrames = ins->rFrames;
delta = ((int8_t)ins->rVolume - (int8_t)ins->dVolume) << 8;
if (ch->rFrames != 0)
delta /= ch->rFrames;
ch->rDelta = delta;
// copy Instrument values
ch->Wavelength = ins->filterSpeedWavelength & 0b00000111;
if (ch->Wavelength > 5) // 8bb: safety bug-fix...
ch->Wavelength = 5;
ch->NoteMaxVolume = ins->Volume;
ch->vibratoCurrent = 0;
ch->vibratoDelay = ins->vibratoDelay;
ch->vibratoDepth = ins->vibratoDepth & 0b00001111;
ch->vibratoSpeed = ins->vibratoSpeed;
ch->VibratoPeriod = 0;
ch->HardCutRelease = !!(ins->vibratoDepth & 128);
ch->HardCut = (ins->vibratoDepth & 0b01110000) >> 4;
ch->IgnoreSquare = false; // don't ignore the 3xx...
ch->squareSlidingIn = false;
ch->squareWait = 0;
ch->squareOn = false;
uint8_t lowerLimit = ins->squareLowerLimit >> (5 - ch->Wavelength);
uint8_t upperLimit = ins->squareUpperLimit >> (5 - ch->Wavelength);
if (lowerLimit <= upperLimit)
{
ch->squareLowerLimit = lowerLimit;
ch->squareUpperLimit = upperLimit;
}
else
{
ch->squareLowerLimit = upperLimit;
ch->squareUpperLimit = lowerLimit;
}
ch->IgnoreFilter = 0;
ch->filterWait = 0;
ch->filterOn = false;
ch->filterSlidingIn = false;
ch->filterSpeed = ins->filterSpeedWavelength >> 3; // shift out wavelength!
lowerLimit = ins->filterLowerLimit;
upperLimit = ins->filterUpperLimit;
if (lowerLimit & 128) ch->filterSpeed |= 32;
if (upperLimit & 128) ch->filterSpeed |= 64;
lowerLimit &= ~128;
upperLimit &= ~128;
if (lowerLimit <= upperLimit)
{
ch->filterLowerLimit = lowerLimit;
ch->filterUpperLimit = upperLimit;
}
else
{
ch->filterLowerLimit = upperLimit;
ch->filterUpperLimit = lowerLimit;
}
ch->filterPos = 32; // std: no filter!
ch->perfWait = 0;
ch->perfSpeed = ins->perfSpeed;
ch->perfCurrent = 0;
ch->Instrument = ins;
ch->perfList = ins->perfList;
}
if (cmd == 0x9) // Effect > 9 < - Set Squarewave-Offset
{
ch->squarePos = param >> (5 - ch->Wavelength);
ch->PlantSquare = true; // now set relation...
ch->IgnoreSquare = true; // ignore next following 3xx cmd.
}
if (cmd == 0x4) // Effect > 4 < - Override Filter
{
if (param < 0x40)
ch->IgnoreFilter = param;
else
ch->filterPos = param - 0x40;
}
ch->periodSlideOn = false;
// Effect > 5 < - TonePortamento+Volume Slide
// Effect > 3 < - TonePortamento (periodSlide Up/Down w/ Limit)
if (cmd == 0x3 || cmd == 0x5)
{
if (cmd == 0x3 && param != 0)
ch->periodSlideSpeed = param;
bool doSlide = true;
if (note != 0)
{
int16_t periodLimit = periodTable[ch->TrackPeriod] - periodTable[note]; // (ABS) SLIDE LIMIT
const uint16_t test = periodLimit + ch->periodSlidePeriod;
if (test == 0) // c-1 -> c-1....
doSlide = false;
else
ch->periodSlideLimit = 0 - periodLimit; // neg/pos!!
}
if (doSlide)
{
ch->periodSlideOn = true;
ch->periodSlideWithLimit = true;
note = 0; // 8bb: don't trigger note
}
}
if (note != 0)
{
ch->TrackPeriod = note;
ch->PlantPeriod = true;
}
if (cmd == 0x1) // Effect > 1 < - Portamento Up (periodSlide Down)
{
ch->periodSlideSpeed = 0 - param;
ch->periodSlideOn = true;
ch->periodSlideWithLimit = false;
}
if (cmd == 0x2) // Effect > 2 < - Portamento Down (periodSlide Up)
{
ch->periodSlideSpeed = param;
ch->periodSlideOn = true;
ch->periodSlideWithLimit = false;
}
if (cmd == 0xE) // Effect > E < - Enhanced Commands
{
uint8_t eCmd = param >> 4;
uint8_t eParam = param & 0xF;
if (eCmd == 0x1) // Effect > E1< - FineSlide Up (periodFineSlide Down)
{
ch->periodSlidePeriod += 0 - eParam;
ch->PlantPeriod = true;
}
if (eCmd == 0x2) // Effect > E2< - FineSlide Down (periodFineSlide Up)
{
ch->periodSlidePeriod += eParam;
ch->PlantPeriod = true;
}
if (eCmd == 0x4) // Effect > E4< - Vibrato Control
ch->vibratoDepth = eParam;
if (eCmd == 0xA) // Effect > EA< - FineVolume Up
{
ch->NoteMaxVolume += eParam;
if (ch->NoteMaxVolume > 0x40)
ch->NoteMaxVolume = 0x40;
}
if (eCmd == 0xB) // Effect > EB< - FineVolume Down
{
ch->NoteMaxVolume -= eParam;
if ((int8_t)ch->NoteMaxVolume < 0)
ch->NoteMaxVolume = 0;
}
}
if (cmd == 0xC) // Effect > C < - Set Volume
{
int16_t p = param;
if (p <= 0x40)
{
ch->NoteMaxVolume = (uint8_t)p;
}
else
{
p -= 0x50;
if (p >= 0)
{
if (p <= 0x40)
{
// 8bb: set TrackMasterVolume for all channels
plyVoiceTemp_t *c = song.pvt;
for (int32_t i = 0; i < PAULA_VOICES; i++, c++)
c->TrackMasterVolume = (uint8_t)p;
}
else
{
p -= 0xA0-0x50;
if (p >= 0 && p <= 0x40)
ch->TrackMasterVolume = (uint8_t)p;
}
}
}
}
}
static void pListCommandParse(plyVoiceTemp_t *ch, uint8_t cmd, uint8_t param)
{
if (cmd == 0x0) // 8bb: Init Filter Modulation
{
if (param == 0)
return; // cmd 0-00 is STILL nuttin'
if (ch->IgnoreFilter > 0)
{
ch->filterPos = ch->IgnoreFilter; // 00..3F
ch->IgnoreFilter = 0;
}
else
{
ch->filterPos = param;
ch->NewWaveform = true;
}
}
else if (cmd == 0x1) // 8bb: Slide Up
{
ch->periodPerfSlideSpeed = param;
ch->periodPerfSlideOn = true;
}
else if (cmd == 0x2) // 8bb: Slide Down
{
ch->periodPerfSlideSpeed = 0 - param;
ch->periodPerfSlideOn = true;
}
else if (cmd == 0x3) // Init Square Modulation
{
if (ch->IgnoreSquare)
ch->IgnoreSquare = false;
else
ch->squarePos = param >> (5 - ch->Wavelength);
}
else if (cmd == 0x4) // Start/Stop Modulation
{
if (param == 0) // 400 is downwards-compatible, means modulate square!!
{
ch->squareOn ^= 1;
ch->squareInit = ch->squareOn;
ch->squareSignum = 1;
}
else
{
if (param & 0x0F)
{
ch->squareOn ^= 1; // any value? FILTER MOD!!
ch->squareInit = ch->squareOn;
ch->squareSignum = 1;
if ((param & 0x0F) == 0x0F) // filter +1 ???
ch->squareSignum = 0 - ch->squareSignum;
}
if (param & 0xF0)
{
ch->filterOn ^= 1; // any value? FILTER MOD!!
ch->filterInit = ch->filterOn;
ch->filterSignum = 1;
if ((param & 0xF0) == 0xF0) // filter +1 ???
ch->filterSignum = 0 - ch->filterSignum;
}
}
}
else if (cmd == 0x5) // Jump to Step [xx]
{
instrument_t *ins = ch->Instrument;
if (ins == NULL) // 8bb: safety bug-fix...
ins = &song.EmptyInstrument;
// 8bb: 4 bytes before perfList (this is apparently what AHX does...)
uint8_t *perfList = ins->perfList - 4;
/* 8bb: AHX quirk! There's no range check here.
** You should have 4*256 perfList bytes for every instrument.
** The bytes after 4*perfLength should be zeroed out in the loader.
**
** AHX does this, and it HAS to be done! Example: lead instrument on "GavinsQuest.ahx".
*/
ch->perfCurrent = param - 1; // 8bb: param-1 is correct (0 -> 255 = safe)
ch->perfList = &perfList[param << 2]; // 8bb: don't do param-1 here!
}
else if (cmd == 0x6) // Set Volume (Command C)
{
int16_t p = param;
if (p <= 0x40)
{
ch->NoteMaxVolume = (uint8_t)p;
}
else
{
p -= 0x50;
if (p >= 0)
{
if (p <= 0x40)
{
ch->perfSubVolume = (uint8_t)p;
}
else
{
p -= 0xA0-0x50;
if (p >= 0 && p <= 0x40)
ch->TrackMasterVolume = (uint8_t)p;
}
}
}
}
else if (cmd == 0x7) // Set Speed (Command F)
{
ch->perfSpeed = param;
ch->perfWait = param;
}
}
static void ProcessFrame(plyVoiceTemp_t *ch)
{
if (ch->HardCut != 0)
{
uint8_t track = ch->Track;
uint16_t noteNr = song.NoteNr + 1; // chk next note!
if (noteNr == song.TrackLength)
{
noteNr = 0; // note 0 from next pos!
track = ch->NextTrack;
}
const uint8_t *bytes = &song.TrackTable[((track << 6) + noteNr) * 3];
uint8_t nextInstr = ((bytes[0] & 3) << 4) | (bytes[1] >> 4);
if (nextInstr != 0)
{
int8_t range = song.Tempo - ch->HardCut; // range 1->7, tempo=6, hc=1, cut at tick 5, right
if (range < 0)
range = 0; // tempo=2, hc=7, cut at tick 0 (NOW!!)
if (!ch->NoteCutOn)
{
ch->NoteCutOn = true;
ch->NoteCutWait = range;
ch->HardCutReleaseF = 0 - (ch->NoteCutWait - song.Tempo);
}
ch->HardCut = 0;
}
}
if (ch->NoteCutOn)
{
if (ch->NoteCutWait == 0)
{
ch->NoteCutOn = false;
if (ch->HardCutRelease)
{
instrument_t *ins = ch->Instrument;
if (ins == NULL) // 8bb: safety bug-fix...
ins = &song.EmptyInstrument;
ch->rFrames = ch->HardCutReleaseF;
ch->rDelta = 0;
if (ch->rFrames > 0)
ch->rDelta = 0 - ((ch->adsr - (ins->rVolume << 8)) / ch->rFrames);
ch->aFrames = 0;
ch->dFrames = 0;
ch->sFrames = 0;
}
else
{
ch->NoteMaxVolume = 0;
}
}
ch->NoteCutWait--;
}
if (ch->NoteDelayOn)
{
if (ch->NoteDelayWait == 0)
ProcessStep(ch);
else
ch->NoteDelayWait--;
}
instrument_t *ins = ch->Instrument;
if (ins == NULL) // 8bb: safety bug-fix...
ins = &song.EmptyInstrument;
if (ch->aFrames != 0)
{
ch->adsr += ch->aDelta;
ch->aFrames--;
if (ch->aFrames == 0)
ch->adsr = ins->aVolume << 8;
}
else if (ch->dFrames != 0)
{
ch->adsr += ch->dDelta;
ch->dFrames--;
if (ch->dFrames == 0)
ch->adsr = ins->dVolume << 8;
}
else if (ch->sFrames != 0)
{
ch->sFrames--;
}
else if (ch->rFrames != 0)
{
ch->adsr += ch->rDelta;
ch->rFrames--;
if (ch->rFrames == 0)
ch->adsr = ins->rVolume << 8;
}
// Volume Slide Treatin'
ch->NoteMaxVolume -= ch->volumeSlideDown;
ch->NoteMaxVolume += ch->volumeSlideUp;
ch->NoteMaxVolume = CLAMP((int8_t)ch->NoteMaxVolume, 0, 0x40);
// Portamento Treatin' (periodSlide)
if (ch->periodSlideOn)
{
if (ch->periodSlideWithLimit)
{
int16_t speed = ch->periodSlideSpeed;
int16_t period = ch->periodSlidePeriod - ch->periodSlideLimit; // source-value
if (period != 0)
{
if (period > 0)
speed = -speed;
int16_t limitTest = (period + speed) ^ period;
if (limitTest >= 0)
ch->periodSlidePeriod += speed;
else
ch->periodSlidePeriod = ch->periodSlideLimit;
ch->PlantPeriod = true;
}
}
else
{
ch->periodSlidePeriod += ch->periodSlideSpeed; // normal 1er/2er period slide!
ch->PlantPeriod = true;
}
}
// Vibrato Treatin'
if (ch->vibratoDepth != 0)
{
if (ch->vibratoDelay != 0)
{
ch->vibratoDelay--;
}
else
{
ch->VibratoPeriod = (vibTable[ch->vibratoCurrent] * (int16_t)ch->vibratoDepth) >> 7;
ch->PlantPeriod = true;
ch->vibratoCurrent = (ch->vibratoCurrent + ch->vibratoSpeed) & 63;
}
}
// pList Treatin'
ins = ch->Instrument;
if (ins != NULL)
{
if (ch->perfCurrent == ins->perfLength)
{
if (ch->perfWait != 0)
ch->perfWait--;
else
ch->periodPerfSlideSpeed = 0; // only STOP sliding!!
}
else
{
/* 8bb: AHX QUIRK! Perf speed $80 results in no delay. This has to do with
** "sub.b #1,Dn | bgt.s .Delay". The BGT instruction will not branch if
** the register got overflown before the comparison (V flag set).
** WinAHX/AHX.cpp is not handling this correctly, porters beware!
**
** "Enchanted Friday Nights" by JazzCat is a song that depends on this quirk
** for the lead instrument to sound right.
*/
bool signedOverflow = (ch->perfWait == 128); // -128 as signed
ch->perfWait--;
if (signedOverflow || (int8_t)ch->perfWait <= 0) // 8bb: signed comparison is needed here
{
const uint8_t *bytes = ch->perfList;
uint8_t cmd2 = (bytes[0] >> 5) & 7;
uint8_t cmd1 = (bytes[0] >> 2) & 7;
uint8_t wave = ((bytes[0] << 1) & 6) | (bytes[1] >> 7);
bool fixed = (bytes[1] >> 6) & 1;
uint8_t note = bytes[1] & 0x3F;
uint8_t param1 = bytes[2];
uint8_t param2 = bytes[3];
// Check Waveform-Field from pList
if (wave != 0)
{
if (wave > 4) // 8bb: safety bug-fix...
wave = 0;
ch->Waveform = wave-1; // 0 to 3...
ch->NewWaveform = true; // New Waveform hit!
ch->periodPerfSlideSpeed = 0;
ch->periodPerfSlidePeriod = 0;
}
ch->periodPerfSlideOn = false;
pListCommandParse(ch, cmd1, param1); // Check Command 1 in pList
pListCommandParse(ch, cmd2, param2); // Check Command 2 in pList
// Check Note(Fixed)-Field from pList
if (note != 0)
{
ch->InstrPeriod = note;
ch->PlantPeriod = true;
ch->FixedNote = fixed;
}
// End of Treatin! Goto next entry for next step!
ch->perfList += 4;
ch->perfCurrent++;
ch->perfWait = ch->perfSpeed;
}
}
}
// ==========================================================================
// =========================== Treat Waveforms ==============================
// ==========================================================================
// =========================== And Modulations ==============================
// ==========================================================================
// perfPortamento Treatin' (periodPerfSlide)
if (ch->periodPerfSlideOn)
{
ch->periodPerfSlidePeriod -= ch->periodPerfSlideSpeed;
if (ch->periodPerfSlidePeriod != 0)
ch->PlantPeriod = true;
}
// Square Treatin' (Modulation-Stuff)
if (ch->Waveform == 3-1 && ch->squareOn)
{
ch->squareWait--;
if ((int8_t)ch->squareWait <= 0) // 8bb: signed comparison is needed here
{
if (ch->squareInit)
{
ch->squareInit = false;
// 8bb: signed comparison is needed here
if ((int8_t)ch->squarePos <= (int8_t)ch->squareLowerLimit)
{
ch->squareSlidingIn = true;
ch->squareSignum = 1;
}
else if ((int8_t)ch->squarePos >= (int8_t)ch->squareUpperLimit)
{
ch->squareSlidingIn = true;
ch->squareSignum = -1;
}
}
if (ch->squarePos == ch->squareLowerLimit || ch->squarePos == ch->squareUpperLimit)
{
if (ch->squareSlidingIn)
ch->squareSlidingIn = false;
else
ch->squareSignum = 0 - ch->squareSignum;
}
ch->squarePos += ch->squareSignum;
ch->PlantSquare = true; // when modulating, refresh square!!
ch->squareWait = ins->squareSpeed;
}
}
// Filter Treatin' (Modulation-Stuff)
if (ch->filterOn)
{
ch->filterWait--;
if ((int8_t)ch->filterWait <= 0) // 8bb: signed comparison is needed here
{
if (ch->filterInit)
{
ch->filterInit = false;
// 8bb: signed comparison is needed here
if ((int8_t)ch->filterPos <= (int8_t)ch->filterLowerLimit)
{
ch->filterSlidingIn = true;
ch->filterSignum = 1;
}
else if ((int8_t)ch->filterPos >= (int8_t)ch->filterUpperLimit)
{
ch->filterSlidingIn = true;
ch->filterSignum = -1;
}
}
int32_t cycles = 1;
if (ch->filterSpeed < 4) // 8bb: < 4 is correct, not < 3 like in WinAHX/AHX.cpp!
cycles = 5 - ch->filterSpeed;
for (int32_t i = 0; i < cycles; i++)
{
if (ch->filterPos == ch->filterLowerLimit || ch->filterPos == ch->filterUpperLimit)
{
if (ch->filterSlidingIn)
ch->filterSlidingIn = false;
else
ch->filterSignum = 0 - ch->filterSignum;
}
ch->filterPos += ch->filterSignum;
}
ch->NewWaveform = true;
ch->filterWait = ch->filterSpeed - 3;
if ((int8_t)ch->filterWait < 1)
ch->filterWait = 1;
}
}
// Square Treatin' (Calculation-Stuff)
if (ch->Waveform == 3-1 || ch->PlantSquare)
{
/* 8bb: Safety bug-fix for out-of-bounds filter positions.
** On original AHX, it can and will read outside of allocated AHX memory.
**
** Since there's no way to get this to sound right, I think it makes sense
** to just properly clamp it instead.
*/
const uint8_t filterPos = CLAMP(ch->filterPos, 1, 63);
const int8_t *src8 = (const int8_t *)&waves->squares[((int32_t)filterPos - 32) * WAV_FILTER_LENGTH]; // [email protected]
uint8_t whichSquare = ch->squarePos << (5 - ch->Wavelength);
if ((int8_t)whichSquare > 0x20)
{
whichSquare = 0x40 - whichSquare;
ch->SquareReverse = true;
}
whichSquare--;
if ((int8_t)whichSquare < 0)
whichSquare = 0;
src8 += whichSquare << 7; // *$80
song.WaveformTab[2] = ch->SquareTempBuffer;
const int32_t delta = (1 << 5) >> ch->Wavelength;
const int32_t cycles = (1 << ch->Wavelength) << 2; // 8bb: <<2 since we do bytes not dwords, unlike AHX
// And calc it, too!
for (int32_t i = 0; i < cycles; i++)
{
ch->SquareTempBuffer[i] = *src8;
src8 += delta;
}
ch->NewWaveform = true;
ch->PlantSquare = false; // enough mod. for this frame
}
// Noise Treatin'
if (ch->Waveform == 4-1)
ch->NewWaveform = true;
// Init the final audioPointer
if (ch->NewWaveform)
{