-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathreplay.c
executable file
·4372 lines (3696 loc) · 121 KB
/
replay.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
#include <stdio.h>
#include <strings.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
#define __USE_BASETYPE__ 1
#include <system_includes.h>
#include "util.h"
#include "replay.h"
#include "tables.h"
#include "gui.h"
#include "undo.h"
extern BOOL quitting;
extern int32 defnotejump, definotejump;
APTR rp_mempool = NULL;
#ifndef __SDL_WRAPPER__
#define FREQ 48000
extern uint32 gui_tick_sig;
struct Task *rp_maintask = NULL;
struct Process *rp_subtask = NULL;
int32 rp_subtask_sig = -1;
struct MsgPort *rp_msgport = NULL;
uint32 rp_sigs = 0;
uint32 rp_freq = FREQ;
float64 rp_freqf = FREQ;
uint32 rp_audiobuflen;
int8 *rp_audiobuffer[2] = { NULL, NULL };
struct MsgPort *rp_replyport = NULL;
#endif
struct rp_command *rp_mainmsg = NULL;
struct List *rp_tunelist = NULL;
struct ahx_tune *rp_curtune = NULL;
struct SignalSemaphore *rp_list_ss = NULL;
enum {
STS_IDLE = 0,
STS_PLAYNOTE,
STS_PLAYPOS,
STS_PLAYSONG,
STS_RECORDING,
STS_CALCULATING,
STS_DEADED,
STS_PLAYROW
};
uint32 rp_state = STS_IDLE;
#ifdef __SDL_WRAPPER__
volatile uint32 rp_state_ack = STS_IDLE;
#endif
extern int32 pref_defstereo;
#ifndef __SDL_WRAPPER__
// Libraries and interfaces
struct Library *AHIBase = NULL;
struct AHIIFace *IAHI = NULL;
// AHI stuff
struct MsgPort *ahi_mp;
struct AHIRequest *ahi_io[2] = { NULL, NULL };
int32 ahi_dev = -1;
#else
#define FREQ 48000
#ifdef __linux__
#define OUTPUT_LEN ((FREQ/50)*2) /* Linux can't cope with buffer being too small, so we sacrifice GUI responsiveness... */
#else
#define OUTPUT_LEN (FREQ/50)
#endif
static struct ahx_tune *rp_playtune = NULL;
int16 rp_audiobuffer[OUTPUT_LEN*2];
uint32 rp_audiobuflen = OUTPUT_LEN*2;
#endif
/*
** Waves
*/
int8 waves[WAVES_SIZE];
uint32 panning_left[256], panning_right[256];
/*
static inline void clr_l( uint32 *src, uint32 longs)
{
do {
*src++ = 0;
longs--;
} while (longs > 0);
}
*/
void rp_GenPanningTables( void )
{
uint32 i;
float64 aa, ab;
// Sine based panning table
aa = (3.14159265f*2.0f)/4.0f; // Quarter of the way through the sinewave == top peak
ab = 0.0f; // Start of the climb from zero
for( i=0; i<256; i++ )
{
panning_left[i] = (uint32)(sin(aa)*255.0f);
panning_right[i] = (uint32)(sin(ab)*255.0f);
aa += (3.14159265*2.0f/4.0f)/256.0f;
ab += (3.14159265*2.0f/4.0f)/256.0f;
}
panning_left[255] = 0;
panning_right[0] = 0;
/*
// Original panning table (linear panning == poo)
for( i=0; i<256; i++ )
{
// Compensate for -128 to 127 range (ideally we want -128 to 128)
if( i < 64 )
panning_left[i] = 256-i;
else
panning_left[i] = 255-i;
if( i > 191 )
panning_right[i] = i+1;
else
panning_right[i] = i;
if( panning_left[i] == 128 ) panning_right[i] = 128;
if( panning_right[i] == 128 ) panning_left[i] = 128;
}
*/
}
void rp_GenSawtooth( int8 *buf, uint32 len )
{
uint32 i;
int32 val, add;
add = 256 / (len-1);
val = -128;
for( i=0; i<len; i++, val += add )
*buf++ = (int8)val;
}
void rp_GenTriangle( int8 *buf, uint32 len )
{
uint32 i;
int32 d2, d5, d1, d4;
int32 val;
const int8 *buf2;
d2 = len;
d5 = len >> 2;
d1 = 128/d5;
d4 = -(d2 >> 1);
val = 0;
for( i=0; i<d5; i++ )
{
*buf++ = val;
val += d1;
}
*buf++ = 0x7f;
if( d5 != 1 )
{
val = 128;
for( i=0; i<d5-1; i++ )
{
val -= d1;
*buf++ = val;
}
}
buf2 = buf + d4;
for( i=0; i<d5*2; i++ )
{
int8 c;
c = *buf2++;
if( c == 0x7f )
c = 0x80;
else
c = -c;
*buf++ = c;
}
}
void rp_GenSquare( int8 *buf )
{
uint32 i, j;
for( i=1; i<=0x20; i++ )
{
for( j=0; j<(0x40-i)*2; j++ )
*buf++ = 0x80;
for( j=0; j<i*2; j++ )
*buf++ = 0x7f;
}
}
static inline int32 clipshifted8(int32 in)
{
int16 top = (int16)(in >> 16);
if (top > 127) in = 127 << 16;
else if (top < -128) in = -(128 << 16);
return in;
}
void rp_GenFilterWaves( const int8 *buf, int8 *lowbuf, int8 *highbuf )
{
const int16 * mid_table = &filter_thing[0];
const int16 * low_table = &filter_thing[1395];
int32 freq;
int32 i;
for( i=0, freq = 25; i<31; i++, freq += 9 )
{
uint32 wv;
const int8 *a0 = buf;
for( wv=0; wv<6+6+0x20+1; wv++ )
{
int32 in, fre, high, mid, low;
uint32 j;
mid = *mid_table++ << 8;
low = *low_table++ << 8;
for( j=0; j<=lentab[wv]; j++ )
{
in = a0[j] << 16;
high = clipshifted8( in - mid - low );
fre = (high >> 8) * freq;
mid = clipshifted8(mid + fre);
fre = (mid >> 8) * freq;
low = clipshifted8(low + fre);
*highbuf++ = high >> 16;
*lowbuf++ = low >> 16;
}
a0 += lentab[wv]+1;
}
}
}
void rp_GenWhiteNoise( int8 *buf, uint32 len )
{
uint32 ays;
ays = 0x41595321;
do {
uint16 ax, bx;
int8 s;
s = ays;
if( ays & 0x100 )
{
s = 0x7f;
if( ays & 0x8000 )
s = 0x80;
}
*buf++ = s;
len--;
ays = (ays >> 5) | (ays << 27);
ays = (ays & 0xffffff00) | ((ays & 0xff) ^ 0x9a);
bx = ays;
ays = (ays << 2) | (ays >> 30);
ax = ays;
bx += ax;
ax ^= bx;
ays = (ays & 0xffff0000) | ax;
ays = (ays >> 3) | (ays << 29);
} while( len );
}
void rp_clear_instrument( struct ahx_instrument *ins )
{
int32 j;
ins->ins_Name[0] = 0;
ins->ins_Volume = 64;
ins->ins_WaveLength = 3;
ins->ins_FilterLowerLimit = 1;
ins->ins_FilterUpperLimit = 1;
ins->ins_FilterSpeed = 0;
ins->ins_SquareLowerLimit = 1;
ins->ins_SquareUpperLimit = 1;
ins->ins_SquareSpeed = 0;
ins->ins_VibratoDelay = 0;
ins->ins_VibratoSpeed = 0;
ins->ins_VibratoDepth = 0;
ins->ins_HardCutRelease = 0;
ins->ins_HardCutReleaseFrames = 0;
ins->ins_Envelope.aFrames = 1;
ins->ins_Envelope.aVolume = 0;
ins->ins_Envelope.dFrames = 1;
ins->ins_Envelope.dVolume = 0;
ins->ins_Envelope.sFrames = 1;
ins->ins_Envelope.rFrames = 1;
ins->ins_Envelope.rVolume = 0;
ins->ins_PList.pls_Speed = 0;
ins->ins_PList.pls_Length = 0;
for( j=0; j<256; j++ )
{
ins->ins_PList.pls_Entries[j].ple_Note = 0;
ins->ins_PList.pls_Entries[j].ple_Waveform = 0;
ins->ins_PList.pls_Entries[j].ple_Fixed = 0;
ins->ins_PList.pls_Entries[j].ple_FX[0] = 0;
ins->ins_PList.pls_Entries[j].ple_FXParam[0] = 0;
ins->ins_PList.pls_Entries[j].ple_FX[1] = 0;
ins->ins_PList.pls_Entries[j].ple_FXParam[1] = 0;
}
ins->ins_ptop = 0;
ins->ins_pcurx = 0;
ins->ins_pcury = 0;
}
void rp_clear_tune( struct ahx_tune *at )
{
int32 i, j;
int32 defgain[] = { 71, 72, 76, 85, 100 };
if( at == NULL ) return;
if( at == rp_curtune ) rp_stop();
free_undolists( at );
at->at_Name[0] = 0;
at->at_Time = 0;
at->at_ExactTime = 0;
at->at_LoopDetector = 0;
at->at_SongNum = 0;
at->at_Frequency = FREQ;
at->at_FreqF = (float64)FREQ;
at->at_WaveformTab[0] = &waves[WO_TRIANGLE_04];
at->at_WaveformTab[1] = &waves[WO_SAWTOOTH_04];
at->at_WaveformTab[3] = &waves[WO_WHITENOISE];
at->at_Restart = 0;
at->at_PositionNr = 1;
at->at_SpeedMultiplier = 1;
at->at_TrackLength = 64;
at->at_TrackNr = 0;
at->at_InstrumentNr = 0;
at->at_SubsongNr = 0;
at->at_PosJump = 0;
at->at_PlayingTime = 0;
at->at_Tempo = 6;
at->at_PosNr = 0;
at->at_NextPosNr = -1;
at->at_StepWaitFrames = 0;
at->at_NoteNr = 0;
at->at_PosJumpNote = 0;
at->at_GetNewPosition = 0;
at->at_PatternBreak = 0;
at->at_SongEndReached = 0;
at->at_posed_curs = 0;
at->at_tracked_curs = 0;
at->at_curins = 1;
at->at_drumpadnote = 25;
at->at_drumpadmode = FALSE;
at->at_topins = 1;
at->at_topinsb = 1;
at->at_curlch = 0;
at->at_Channels = 4;
at->at_baseins = 0;
at->at_curpanel = 0;
at->at_modified = FALSE;
at->at_defstereo = pref_defstereo;
at->at_defpanleft = stereopan_left[pref_defstereo];
at->at_defpanright = stereopan_right[pref_defstereo];
at->at_mixgain = (defgain[pref_defstereo]*256)/100;
at->at_mixgainP = defgain[pref_defstereo];
at->at_cbmarktrack = -1;
at->at_cbmarkstartnote = -1;
at->at_cbmarkendnote = -1;
at->at_cbmarkleftx = 0;
at->at_cbmarkrightx = 0;
at->at_cbpmarklft = -1;
at->at_cbpmarkmarklft = -1;
at->at_notejump = defnotejump;
at->at_inotejump = definotejump;
at->at_doing = D_IDLE;
at->at_editing = E_TRACK;
at->at_idoing = D_IDLE;
at->at_rempos[0] = 0;
at->at_rempos[1] = 16;
at->at_rempos[2] = 32;
at->at_rempos[3] = 48;
at->at_rempos[4] = 63;
for( i=0; i<MAX_CHANNELS; i++ )
{
at->at_Voices[i].vc_VUMeter = 0;
at->at_Voices[i].vc_SetTrackOn = 1;
}
for( i=0; i<MAX_CHANNELS; i+=4 )
{
at->at_Voices[i+0].vc_Pan = at->at_defpanleft;
at->at_Voices[i+0].vc_SetPan = at->at_defpanleft;
at->at_Voices[i+0].vc_PanMultLeft = panning_left[at->at_defpanleft];
at->at_Voices[i+0].vc_PanMultRight = panning_right[at->at_defpanleft];
at->at_Voices[i+1].vc_Pan = at->at_defpanright;
at->at_Voices[i+1].vc_SetPan = at->at_defpanright;
at->at_Voices[i+1].vc_PanMultLeft = panning_left[at->at_defpanright];
at->at_Voices[i+1].vc_PanMultRight = panning_right[at->at_defpanright];
at->at_Voices[i+2].vc_Pan = at->at_defpanright;
at->at_Voices[i+2].vc_SetPan = at->at_defpanright;
at->at_Voices[i+2].vc_PanMultLeft = panning_left[at->at_defpanright];
at->at_Voices[i+2].vc_PanMultRight = panning_right[at->at_defpanright];
at->at_Voices[i+3].vc_Pan = at->at_defpanleft;
at->at_Voices[i+3].vc_SetPan = at->at_defpanleft;
at->at_Voices[i+3].vc_PanMultLeft = panning_left[at->at_defpanleft];
at->at_Voices[i+3].vc_PanMultRight = panning_right[at->at_defpanleft];
}
for( i=0; i<256; i++ )
{
at->at_Subsongs[i] = 0;
for( j=0; j<64; j++ )
{
at->at_Tracks[i][j].stp_Note = 0;
at->at_Tracks[i][j].stp_Instrument = 0;
at->at_Tracks[i][j].stp_FX = 0;
at->at_Tracks[i][j].stp_FXParam = 0;
at->at_Tracks[i][j].stp_FXb = 0;
at->at_Tracks[i][j].stp_FXbParam = 0;
}
}
for( i=0; i<1000; i++ )
{
for( j=0; j<MAX_CHANNELS; j++ )
{
at->at_Positions[i].pos_Track[j] = 0;
at->at_Positions[i].pos_Transpose[j] = 0;
}
}
for( i=0; i<64; i++ )
rp_clear_instrument( &at->at_Instruments[i] );
at->at_ticks = at->at_secs = at->at_mins = at->at_hours = 0;
}
void rp_reset_some_shit( struct ahx_tune *at )
{
uint32 i;
at->at_ticks = at->at_secs = at->at_mins = at->at_hours = 0;
for( i=0; i<MAX_CHANNELS; i++ )
{
at->at_Voices[i].vc_Delta=1;
at->at_Voices[i].vc_OverrideTranspose = 1000; // 1.5
at->at_Voices[i].vc_SamplePos=at->at_Voices[i].vc_Track=at->at_Voices[i].vc_Transpose=at->at_Voices[i].vc_NextTrack = at->at_Voices[i].vc_NextTranspose = 0;
at->at_Voices[i].vc_ADSRVolume=at->at_Voices[i].vc_InstrPeriod=at->at_Voices[i].vc_TrackPeriod=at->at_Voices[i].vc_VibratoPeriod=at->at_Voices[i].vc_NoteMaxVolume=at->at_Voices[i].vc_PerfSubVolume=at->at_Voices[i].vc_TrackMasterVolume=0;
at->at_Voices[i].vc_NewWaveform=at->at_Voices[i].vc_Waveform=at->at_Voices[i].vc_PlantSquare=at->at_Voices[i].vc_PlantPeriod=at->at_Voices[i].vc_IgnoreSquare=0;
at->at_Voices[i].vc_TrackOn=at->at_Voices[i].vc_FixedNote=at->at_Voices[i].vc_VolumeSlideUp=at->at_Voices[i].vc_VolumeSlideDown=at->at_Voices[i].vc_HardCut=at->at_Voices[i].vc_HardCutRelease=at->at_Voices[i].vc_HardCutReleaseF=0;
at->at_Voices[i].vc_PeriodSlideSpeed=at->at_Voices[i].vc_PeriodSlidePeriod=at->at_Voices[i].vc_PeriodSlideLimit=at->at_Voices[i].vc_PeriodSlideOn=at->at_Voices[i].vc_PeriodSlideWithLimit=0;
at->at_Voices[i].vc_PeriodPerfSlideSpeed=at->at_Voices[i].vc_PeriodPerfSlidePeriod=at->at_Voices[i].vc_PeriodPerfSlideOn=at->at_Voices[i].vc_VibratoDelay=at->at_Voices[i].vc_VibratoCurrent=at->at_Voices[i].vc_VibratoDepth=at->at_Voices[i].vc_VibratoSpeed=0;
at->at_Voices[i].vc_SquareOn=at->at_Voices[i].vc_SquareInit=at->at_Voices[i].vc_SquareLowerLimit=at->at_Voices[i].vc_SquareUpperLimit=at->at_Voices[i].vc_SquarePos=at->at_Voices[i].vc_SquareSign=at->at_Voices[i].vc_SquareSlidingIn=at->at_Voices[i].vc_SquareReverse=0;
at->at_Voices[i].vc_FilterOn=at->at_Voices[i].vc_FilterInit=at->at_Voices[i].vc_FilterLowerLimit=at->at_Voices[i].vc_FilterUpperLimit=at->at_Voices[i].vc_FilterPos=at->at_Voices[i].vc_FilterSign=at->at_Voices[i].vc_FilterSpeed=at->at_Voices[i].vc_FilterSlidingIn=at->at_Voices[i].vc_IgnoreFilter=0;
at->at_Voices[i].vc_PerfCurrent=at->at_Voices[i].vc_PerfSpeed=at->at_Voices[i].vc_WaveLength=at->at_Voices[i].vc_NoteDelayOn=at->at_Voices[i].vc_NoteCutOn=0;
at->at_Voices[i].vc_AudioPeriod=at->at_Voices[i].vc_AudioVolume=at->at_Voices[i].vc_VoiceVolume=at->at_Voices[i].vc_VoicePeriod=at->at_Voices[i].vc_VoiceNum=at->at_Voices[i].vc_WNRandom=0;
at->at_Voices[i].vc_SquareWait=at->at_Voices[i].vc_FilterWait=at->at_Voices[i].vc_PerfWait=at->at_Voices[i].vc_NoteDelayWait=at->at_Voices[i].vc_NoteCutWait=0;
at->at_Voices[i].vc_PerfList=0;
at->at_Voices[i].vc_RingSamplePos=at->at_Voices[i].vc_RingDelta=at->at_Voices[i].vc_RingPlantPeriod=at->at_Voices[i].vc_RingAudioPeriod=at->at_Voices[i].vc_RingNewWaveform=at->at_Voices[i].vc_RingWaveform=at->at_Voices[i].vc_RingFixedPeriod=at->at_Voices[i].vc_RingBasePeriod=0;
at->at_Voices[i].vc_RingMixSource = NULL;
at->at_Voices[i].vc_RingAudioSource = NULL;
memset( &at->at_Voices[i].vc_SquareTempBuffer,0,0x80);
memset( &at->at_Voices[i].vc_ADSR,0,sizeof(struct ahx_envelope));
memset( &at->at_Voices[i].vc_VoiceBuffer,0,0x281);
memset( &at->at_Voices[i].vc_RingVoiceBuffer,0,0x281);
}
for( i=0; i<MAX_CHANNELS; i++ )
{
at->at_Voices[i].vc_WNRandom = 0x280;
at->at_Voices[i].vc_VoiceNum = i;
at->at_Voices[i].vc_TrackMasterVolume = 0x40;
at->at_Voices[i].vc_TrackOn = at->at_Voices[i].vc_SetTrackOn;
at->at_Voices[i].vc_MixSource = at->at_Voices[i].vc_VoiceBuffer;
}
}
BOOL rp_init_subsong( struct ahx_tune *at, uint32 nr )
{
uint32 PosNr, i;
if( nr > at->at_SubsongNr )
return FALSE;
at->at_SongNum = nr;
PosNr = 0;
if( nr ) PosNr = at->at_Subsongs[nr-1];
at->at_PosNr = PosNr;
at->at_PosJump = 0;
at->at_PatternBreak = 0;
at->at_NoteNr = 0;
at->at_PosJumpNote = 0;
at->at_Tempo = 6;
at->at_StepWaitFrames = 0;
at->at_GetNewPosition = 1;
at->at_SongEndReached = 0;
at->at_PlayingTime = 0;
at->at_curss = nr;
for( i=0; i<MAX_CHANNELS; i+=4 )
{
at->at_Voices[i+0].vc_Pan = at->at_defpanleft;
at->at_Voices[i+0].vc_SetPan = at->at_defpanleft;
at->at_Voices[i+0].vc_PanMultLeft = panning_left[at->at_defpanleft];
at->at_Voices[i+0].vc_PanMultRight = panning_right[at->at_defpanleft];
at->at_Voices[i+1].vc_Pan = at->at_defpanright;
at->at_Voices[i+1].vc_SetPan = at->at_defpanright;
at->at_Voices[i+1].vc_PanMultLeft = panning_left[at->at_defpanright];
at->at_Voices[i+1].vc_PanMultRight = panning_right[at->at_defpanright];
at->at_Voices[i+2].vc_Pan = at->at_defpanright;
at->at_Voices[i+2].vc_SetPan = at->at_defpanright;
at->at_Voices[i+2].vc_PanMultLeft = panning_left[at->at_defpanright];
at->at_Voices[i+2].vc_PanMultRight = panning_right[at->at_defpanright];
at->at_Voices[i+3].vc_Pan = at->at_defpanleft;
at->at_Voices[i+3].vc_SetPan = at->at_defpanleft;
at->at_Voices[i+3].vc_PanMultLeft = panning_left[at->at_defpanleft];
at->at_Voices[i+3].vc_PanMultRight = panning_right[at->at_defpanleft];
}
rp_reset_some_shit( at );
return TRUE;
}
struct ahx_tune *rp_new_tune( BOOL addtolist )
{
struct ahx_tune *at;
at = (struct ahx_tune *)allocnode(sizeof(struct ahx_tune));
if( !at )
{
printf( "Out of memory @ %s:%d\n", __FILE__, __LINE__ );
return NULL;
}
at->at_undolist = IExec->AllocSysObjectTags( ASOT_LIST, TAG_DONE );
if( !at->at_undolist )
{
IExec->FreeSysObject( ASOT_NODE, at );
printf( "Out of memory @ %s:%d\n", __FILE__, __LINE__ );
return NULL;
}
at->at_redolist = IExec->AllocSysObjectTags( ASOT_LIST, TAG_DONE );
if( !at->at_redolist )
{
IExec->FreeSysObject( ASOT_LIST, at->at_undolist );
IExec->FreeSysObject( ASOT_NODE, at );
printf( "Out of memory @ %s:%d\n", __FILE__, __LINE__ );
return NULL;
}
at->at_undomem = 0;
at->at_ln.ln_Succ = NULL;
at->at_ln.ln_Pred = NULL;
at->at_ln.ln_Name = &at->at_Name[0];
at->at_ln.ln_Pri = 0;
at->at_ln.ln_Type = NT_USER;
rp_clear_tune( at );
if( addtolist )
{
IExec->ObtainSemaphore( rp_list_ss );
IExec->AddTail( rp_tunelist, (struct Node *)at );
IExec->ReleaseSemaphore( rp_list_ss );
}
rp_init_subsong( at, 0 );
return at;
}
void rp_free_tune( struct ahx_tune *at )
{
IExec->ObtainSemaphore( rp_list_ss );
IExec->Remove( (struct Node *)at );
free_undolists( at );
IExec->FreeSysObject( ASOT_LIST, at->at_undolist );
IExec->FreeSysObject( ASOT_LIST, at->at_redolist );
IExec->FreeSysObject( ASOT_NODE, at );
IExec->ReleaseSemaphore( rp_list_ss );
}
void rp_free_all_tunes( void )
{
struct ahx_tune *at, *nat;
IExec->ObtainSemaphore( rp_list_ss );
at = (struct ahx_tune *)IExec->GetHead(rp_tunelist);
while( at )
{
nat = (struct ahx_tune *)IExec->GetSucc(&at->at_ln);
rp_free_tune( at );
at = nat;
}
IExec->ReleaseSemaphore( rp_list_ss );
}
void rp_save_hvl_ins( const TEXT *name, struct ahx_instrument *ins )
{
FILE *fh;
int8 *buf, *bptr;
int32 buflen, i;
buflen = 26+ins->ins_PList.pls_Length*5;
buf = IExec->AllocPooled( rp_mempool, buflen );
if( !buf )
{
printf( "Out of memory @ %s:%d\n", __FILE__, __LINE__ );
return;
}
for( i=0; i<buflen; i++ ) buf[i] = 0;
strcpy( (TEXT *)buf, "HVLI" );
buf[4] = ins->ins_Volume;
buf[5] = (ins->ins_FilterSpeed&0x1f)<<3;
buf[16] = (ins->ins_FilterSpeed&0x20)<<2;
buf[5] |= ins->ins_WaveLength&0x07;
buf[6] = ins->ins_Envelope.aFrames;
buf[7] = ins->ins_Envelope.aVolume;
buf[8] = ins->ins_Envelope.dFrames;
buf[9] = ins->ins_Envelope.dVolume;
buf[10] = ins->ins_Envelope.sFrames;
buf[11] = ins->ins_Envelope.rFrames;
buf[12] = ins->ins_Envelope.rVolume;
buf[16] |= ins->ins_FilterLowerLimit&0x7f;
buf[17] = ins->ins_VibratoDelay;
buf[18] = (ins->ins_HardCutReleaseFrames&0x07)<<4;
buf[18] |= (ins->ins_HardCutRelease&0x1)<<7;
buf[18] |= ins->ins_VibratoDepth&0x0f;
buf[19] = ins->ins_VibratoSpeed;
buf[20] = ins->ins_SquareLowerLimit;
buf[21] = ins->ins_SquareUpperLimit;
buf[22] = ins->ins_SquareSpeed;
buf[23] = ins->ins_FilterUpperLimit&0x3f;
buf[24] = ins->ins_PList.pls_Speed;
buf[25] = ins->ins_PList.pls_Length;
bptr = &buf[26];
for( i=0; i<ins->ins_PList.pls_Length; i++ )
{
bptr[0] = ins->ins_PList.pls_Entries[i].ple_FX[0]&0xf;
bptr[1] = (ins->ins_PList.pls_Entries[i].ple_FX[1]&0xf)<<3;
bptr[1] |= ins->ins_PList.pls_Entries[i].ple_Waveform&7;
bptr[2] = (ins->ins_PList.pls_Entries[i].ple_Fixed&1)<<6;
bptr[2] |= ins->ins_PList.pls_Entries[i].ple_Note&0x3f;
bptr[3] = ins->ins_PList.pls_Entries[i].ple_FXParam[0];
bptr[4] = ins->ins_PList.pls_Entries[i].ple_FXParam[1];
bptr += 5;
}
fh = fopen( name, "wb" );
if( !fh )
{
printf( "unable to open file\n" );
IExec->FreePooled( rp_mempool, buf, buflen );
return;
}
fwrite( buf, buflen, 1, fh );
fwrite( ins->ins_Name, strlen( ins->ins_Name )+1, 1, fh );
fclose( fh );
IExec->FreePooled( rp_mempool, buf, buflen );
}
void rp_save_ins( const TEXT *name, struct ahx_tune *at, int32 in )
{
FILE *fh;
int8 *buf, *bptr;
int32 buflen, i, k, l;
struct ahx_instrument *ins;
BOOL saveahxi;
#ifdef __SDL_WRAPPER__
char mkname[4096];
#endif
if( at == NULL ) return;
#ifdef __SDL_WRAPPER__
// Enforce file extension since windows likes them so much
// (although let them use a prefix if they want to keep it oldskool)
if (strncasecmp(name, "ins.", 4) != 0)
{
// No prefix...
i = strlen(name);
if ((i < 4) || (strcasecmp(&name[i-4], ".ins") != 0))
{
// No extension
strncpy(mkname, name, 4096);
strncat(mkname, ".ins", 4096);
mkname[4095] = 0;
name = mkname;
}
}
#endif
ins = &at->at_Instruments[in];
saveahxi = TRUE;
// Have to save as HVL instrument?
for( i=0; i<ins->ins_PList.pls_Length; i++ )
{
if( ( ins->ins_PList.pls_Entries[i].ple_FX[0] > 5 ) &&
( ins->ins_PList.pls_Entries[i].ple_FX[0] != 12 ) &&
( ins->ins_PList.pls_Entries[i].ple_FX[0] != 15 ) )
saveahxi = FALSE;
if( ( ins->ins_PList.pls_Entries[i].ple_FX[1] > 5 ) &&
( ins->ins_PList.pls_Entries[i].ple_FX[1] != 12 ) &&
( ins->ins_PList.pls_Entries[i].ple_FX[1] != 15 ) )
saveahxi = FALSE;
}
if( saveahxi == FALSE )
{
rp_save_hvl_ins( name, ins );
return;
}
buflen = 26+ins->ins_PList.pls_Length*4;
buf = IExec->AllocPooled( rp_mempool, buflen );
if( !buf )
{
printf( "Out of memory @ %s:%d\n", __FILE__, __LINE__ );
return;
}
for( i=0; i<buflen; i++ ) buf[i] = 0;
strcpy( (TEXT *)buf, "THXI" );
buf[4] = ins->ins_Volume;
buf[5] = (ins->ins_FilterSpeed&0x1f)<<3;
buf[16] = (ins->ins_FilterSpeed&0x20)<<2;
buf[5] |= ins->ins_WaveLength&0x07;
buf[6] = ins->ins_Envelope.aFrames;
buf[7] = ins->ins_Envelope.aVolume;
buf[8] = ins->ins_Envelope.dFrames;
buf[9] = ins->ins_Envelope.dVolume;
buf[10] = ins->ins_Envelope.sFrames;
buf[11] = ins->ins_Envelope.rFrames;
buf[12] = ins->ins_Envelope.rVolume;
buf[16] |= ins->ins_FilterLowerLimit&0x7f;
buf[17] = ins->ins_VibratoDelay;
buf[18] = (ins->ins_HardCutReleaseFrames&0x07)<<4;
buf[18] |= (ins->ins_HardCutRelease&0x1)<<7;
buf[18] |= ins->ins_VibratoDepth&0x0f;
buf[19] = ins->ins_VibratoSpeed;
buf[20] = ins->ins_SquareLowerLimit;
buf[21] = ins->ins_SquareUpperLimit;
buf[22] = ins->ins_SquareSpeed;
buf[23] = ins->ins_FilterUpperLimit&0x3f;
buf[24] = ins->ins_PList.pls_Speed;
buf[25] = ins->ins_PList.pls_Length;
bptr = &buf[26];
for( i=0; i<ins->ins_PList.pls_Length; i++ )
{
// Convert to AHX format (this means that HVL instruments could have more FX! w00t!)
k = ins->ins_PList.pls_Entries[i].ple_FX[1];
if( k == 12 ) k = 6;
if( k == 15 ) k = 7;
l = ins->ins_PList.pls_Entries[i].ple_FX[0];
if( l == 12 ) l = 6;
if( l == 15 ) l = 7;
bptr[0] = (k&7)<<5;
bptr[0] |= (l&7)<<2;
bptr[0] |= (ins->ins_PList.pls_Entries[i].ple_Waveform&6)>>1;
bptr[1] = (ins->ins_PList.pls_Entries[i].ple_Waveform&1)<<7;
bptr[1] |= (ins->ins_PList.pls_Entries[i].ple_Fixed&1)<<6;
bptr[1] |= ins->ins_PList.pls_Entries[i].ple_Note&0x3f;
bptr[2] = ins->ins_PList.pls_Entries[i].ple_FXParam[0];
bptr[3] = ins->ins_PList.pls_Entries[i].ple_FXParam[1];
bptr += 4;
}
fh = fopen( name, "wb" );
if( !fh )
{
printf( "unable to open file\n" );
IExec->FreePooled( rp_mempool, buf, buflen );
return;
}
fwrite( buf, buflen, 1, fh );
fwrite( ins->ins_Name, strlen( ins->ins_Name )+1, 1, fh );
fclose( fh );
IExec->FreePooled( rp_mempool, buf, buflen );
}
void rp_load_ins( const TEXT *name, struct ahx_tune *at, int32 in )
{
FILE *fh;
uint8 *buf;
const uint8 *bptr;
uint32 buflen, i, k, l;
struct ahx_instrument *ni;
BOOL ahxi;
if( at == NULL ) return;
fh = fopen(name, "rb");
if( !fh )
{
printf( "Can't open file\n" );
return;
}
fseek(fh, 0, SEEK_END);
buflen = ftell(fh);
fseek(fh, 0, SEEK_SET);
buf = IExec->AllocPooled( rp_mempool, buflen );
if( !buf )
{
fclose(fh);
printf( "Out of memory @ %s:%d\n", __FILE__, __LINE__ );
return;
}
if( fread( buf, buflen, 1, fh ) != 1 )
{
IExec->FreePooled( rp_mempool, buf, buflen );
fclose(fh);
printf( "Unable to read from file!\n" );
return;
}
fclose(fh);
ahxi = TRUE;
if( ( buf[0] == 'H' ) &&
( buf[1] == 'V' ) &&
( buf[2] == 'L' ) &&
( buf[3] == 'I' ) )
ahxi = FALSE;
if( ( ahxi == TRUE ) &&
( ( buf[0] != 'T' ) ||
( buf[1] != 'H' ) ||
( buf[2] != 'X' ) ||
( buf[3] != 'I' ) ) )
{
IExec->FreePooled( rp_mempool, buf, buflen );
printf( "Invalid file\n" );
return;
}
ni = &at->at_Instruments[in];
ni->ins_Volume = buf[4];
ni->ins_FilterSpeed = ((buf[5]>>3)&0x1f)|((buf[16]>>2)&0x20);
ni->ins_WaveLength = buf[5]&0x07;
ni->ins_Envelope.aFrames = buf[6];
ni->ins_Envelope.aVolume = buf[7];
ni->ins_Envelope.dFrames = buf[8];
ni->ins_Envelope.dVolume = buf[9];
ni->ins_Envelope.sFrames = buf[10];
ni->ins_Envelope.rFrames = buf[11];
ni->ins_Envelope.rVolume = buf[12];
ni->ins_FilterLowerLimit = buf[16]&0x7f;
ni->ins_VibratoDelay = buf[17];
ni->ins_HardCutReleaseFrames = (buf[18]>>4)&0x07;
ni->ins_HardCutRelease = buf[18]&0x80?1:0;
ni->ins_VibratoDepth = buf[18]&0x0f;
ni->ins_VibratoSpeed = buf[19];
ni->ins_SquareLowerLimit = buf[20];
ni->ins_SquareUpperLimit = buf[21];
ni->ins_SquareSpeed = buf[22];
ni->ins_FilterUpperLimit = buf[23]&0x3f;
ni->ins_PList.pls_Speed = buf[24];
ni->ins_PList.pls_Length = buf[25];
bptr = &buf[26];
if( ahxi )
{
for( i=0; i<ni->ins_PList.pls_Length; i++ )
{
k = (bptr[0]>>5)&7;
if( k == 6 ) k = 12;
if( k == 7 ) k = 15;
l = (bptr[0]>>2)&7;
if( l == 6 ) l = 12;
if( l == 7 ) l = 15;
ni->ins_PList.pls_Entries[i].ple_FX[1] = k;
ni->ins_PList.pls_Entries[i].ple_FX[0] = l;
ni->ins_PList.pls_Entries[i].ple_Waveform = ((bptr[0]<<1)&6) | (bptr[1]>>7);
ni->ins_PList.pls_Entries[i].ple_Fixed = (bptr[1]>>6)&1;
ni->ins_PList.pls_Entries[i].ple_Note = bptr[1]&0x3f;
ni->ins_PList.pls_Entries[i].ple_FXParam[0] = bptr[2];
ni->ins_PList.pls_Entries[i].ple_FXParam[1] = bptr[3];
bptr += 4;
}
} else {
for( i=0; i<ni->ins_PList.pls_Length; i++ )
{
ni->ins_PList.pls_Entries[i].ple_FX[0] = bptr[0]&0xf;
ni->ins_PList.pls_Entries[i].ple_FX[1] = (bptr[1]>>3)&0xf;
ni->ins_PList.pls_Entries[i].ple_Waveform = bptr[1]&7;
ni->ins_PList.pls_Entries[i].ple_Fixed = (bptr[2]>>6)&1;
ni->ins_PList.pls_Entries[i].ple_Note = bptr[2]&0x3f;
ni->ins_PList.pls_Entries[i].ple_FXParam[0] = bptr[3];
ni->ins_PList.pls_Entries[i].ple_FXParam[1] = bptr[4];
bptr += 5;
}
}
while( i < 256 )
{
ni->ins_PList.pls_Entries[i].ple_FX[0] = 0;
ni->ins_PList.pls_Entries[i].ple_FX[1] = 0;
ni->ins_PList.pls_Entries[i].ple_FXParam[0] = 0;
ni->ins_PList.pls_Entries[i].ple_FXParam[1] = 0;
ni->ins_PList.pls_Entries[i].ple_Waveform = 0;
ni->ins_PList.pls_Entries[i].ple_Fixed = 0;
ni->ins_PList.pls_Entries[i].ple_Note = 0;
i++;
}
ni->ins_ptop = 0;
ni->ins_pcurx = 0;
ni->ins_pcury = 0;
strncpy( ni->ins_Name, (TEXT *)bptr, 127 );
ni->ins_Name[127] = '\0';
// printf( "Loaded '%s'!\n", ni->ins_Name );
}
uint32 rp_ahx_test( const struct ahx_tune *at )
{
int32 i, j;
int32 hvlfeats;
const struct ahx_instrument *in;
const struct ahx_step *sp;
// First, check if the tune adheres to AHX limitations
hvlfeats = 0;
// >4 channels?
if( at->at_Channels > 4 ) hvlfeats |= SWF_MANYCHANS;
// Check instruments only use 1, 2, 3, 4, 5, C & F commands
for( i=1; i<64; i++ )
{
in = &at->at_Instruments[i];
for( j=0; j<in->ins_PList.pls_Length; j++ )