-
Notifications
You must be signed in to change notification settings - Fork 3
/
JUDASS3M.C
1735 lines (1631 loc) · 67.7 KB
/
JUDASS3M.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
/*
* JUDAS Screamtracker 3 Module loading/playing
*
* Not supported:
* - stereo samples (not supported by ST3 either)
*
* Changes:
* V2.01 Arpeggio command must use the previous value if infobyte = 0
* V2.02 Removed checking of read error in instrument loading
* V2.03 Don't use toneportamento if no note previously on that channel
* Moved tables only used by S3Ms here
* V2.04 Added support for stereo enable/disable & "panning magic" to loading
* Added surround panning command "X??" (Undocumented, CP supports it)
* Vibrato command must use old speed even if depth changes if nybble1=0
* Allow 0 at header.byte1a, previously only 1Ah was accepted
* Kill sound if it is portamented higher than HIGHPERIOD, which is now 1
* Portamento up and down use the same previous value
* Scaled default left-right panning to 1/2 original to prevent earache
* Sample offset command uses the previous value if parameter is 00
* V2.06 Phase of the sine vibrato rotated 180 degrees
* Note cut didn't work at all, now it does!
* Added song looping control (might have removed an old, fatal bug
* too!) Moving to an already played song position is treated as
* looping. This may cause false alarms in some weird songs.
* Added support for vumeters (smp structure address provided).
* Added support for the more precise volume
* Added song rewinding/forwarding
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mem.h>
#include "judas.h"
#include "judastbl.h"
#ifdef __DJGPP__
#define HANDLE_PRAGMA_PACK_PUSH_POP 1
#endif
#define S3M_MAXCHANNELS 32
#define S3M_MAXPATTERNS 100
#define S3M_MAXINSTRUMENTS 100
#define S3M_MAXORDER 256
#define HIGHPERIOD 1
#define LOWPERIOD 29015
#define S3M_LEFT 64
#define S3M_RIGHT 191
#pragma pack(push,1)
typedef struct
{
unsigned char songname[28];
unsigned char byte1a;
unsigned char filetype;
unsigned short unused1;
unsigned short ordernum;
unsigned short instnum;
unsigned short pattnum;
unsigned short flags;
unsigned short createdwith;
unsigned short fileversion;
unsigned char id[4];
unsigned char globalvol;
unsigned char initialspeed;
unsigned char initialtempo;
unsigned char mastermult;
unsigned char ultraclicks;
unsigned char panningmagic;
unsigned char unused[8];
unsigned short special;
unsigned char chsettings[S3M_MAXCHANNELS];
} S3M_HEADER;
typedef struct
{
unsigned char type;
unsigned char dosname[12];
unsigned char paraptrhigh;
unsigned short paraptr;
unsigned length;
unsigned loopstart;
unsigned loopend;
signed char vol;
unsigned char disknum;
unsigned char pack;
unsigned char flags;
unsigned c2rate;
unsigned unused;
unsigned short guspos;
unsigned short int512;
unsigned lastused;
unsigned char name[28];
unsigned char id[4];
} S3M_INSTRUMENT;
typedef struct
{
unsigned char note;
unsigned char instrument;
unsigned char voleffect;
unsigned char effect;
unsigned char effectdata;
} NOTE;
#pragma pack(pop)
typedef struct
{
SAMPLE *sp;
signed char vol;
unsigned c2rate;
} INSTRUMENT;
typedef struct
{
INSTRUMENT *ip;
SAMPLE *sp;
unsigned char newnote;
unsigned char note;
unsigned char instrument;
unsigned char newinstrument;
unsigned char voleffect;
unsigned char effect;
unsigned char effectdata;
unsigned char nybble1;
unsigned char nybble2;
signed char notevol;
signed char vol;
unsigned short c2rate;
short baseperiod;
short period;
short targetperiod;
unsigned char tp;
unsigned char tpspeed;
unsigned char volspeedup;
unsigned char volspeeddown;
unsigned char portaspeed;
unsigned char vibratotype;
unsigned char vibratospeed;
unsigned char vibratodepth;
unsigned char vibratophase;
unsigned char sampleoffset;
unsigned char usesampleoffset;
unsigned char tremolotype;
unsigned char tremolospeed;
unsigned char tremolodepth;
unsigned char tremolophase;
unsigned char retrigcount;
unsigned char retriginterval;
unsigned char retrigvolchange;
unsigned char tremorcount;
unsigned char tremorontime;
unsigned char tremorofftime;
unsigned char tremorstatus;
unsigned char arpeggiointerval;
} TRACK;
/* Prototypes */
int judas_loads3m(char *name);
void judas_frees3m(void);
void judas_plays3m(int rounds);
void judas_stops3m(void);
unsigned char judas_gets3mpos(void);
unsigned char judas_gets3mline(void);
unsigned char judas_gets3mtick(void);
unsigned char judas_gets3mchannels(void);
char *judas_gets3mname(void);
void judas_forwards3m(void);
void judas_rewinds3m(void);
static void judas_mutechannelss3m();
static int init_s3mplayer(void);
static void s3mplayer_code_lock_start(void);
static void s3mplayer(void);
static void startnewnote(TRACK *tptr, CHANNEL *chptr);
static void extendedcommand(TRACK *tptr, CHANNEL *chptr);
static void volslide(TRACK *tptr);
static void s3mplayer_code_lock_end(void);
/* Period table for S3Ms */
unsigned short s3mperiodtable[] =
{
1712, 1616, 1524, 1440, 1356, 1280, 1208, 1140, 1076, 1016, 960, 907
};
/* S3M Set Finetune values */
unsigned short s3mfinetunetable[] =
{
7895, 7941, 7985, 8046, 8107, 8169, 8232, 8280, 8363, 8413, 8463, 8529,
8581, 8651, 8723, 8757
};
static S3M_HEADER header;
static char s3m_loaded = 0;
static char s3mplayer_firsttime = 1;
static char s3m_channels;
static unsigned char s3m_chpanning[S3M_MAXCHANNELS];
static unsigned char s3m_order[S3M_MAXORDER];
static char s3m_poshasbeenplayed[S3M_MAXORDER];
static unsigned short instparapointers[S3M_MAXINSTRUMENTS];
static unsigned short pattparapointers[S3M_MAXINSTRUMENTS];
static unsigned char defaultchannelpanpos[S3M_MAXCHANNELS];
static INSTRUMENT s3m_instrument[S3M_MAXINSTRUMENTS];
static TRACK s3m_track[S3M_MAXCHANNELS];
static unsigned s3m_pattlength;
static unsigned char *s3m_pattptr = NULL;
static unsigned short loadpattlen;
static unsigned short s3m_oldpos;
static unsigned short s3m_pos;
static unsigned short s3m_line;
static unsigned short s3m_ticktempo;
static unsigned short s3m_tickcount;
static unsigned char s3m_globalvol;
static unsigned char s3m_patternbreak;
static unsigned char s3m_patterndelay;
static unsigned char s3m_patternloopline;
static unsigned char s3m_patternloopcount;
static int s3m_roundcounter;
static int s3m_wind = 0;
static int s3m_windingpause = 0;
/* External variables */
extern unsigned char judas_bpmtempo;
extern unsigned judas_bpmcount;
extern void (*judas_player)(void);
/* External variables */
extern unsigned char judas_bpmtempo;
extern unsigned judas_bpmcount;
extern void (*judas_player)(void);
void judas_forwards3m(void)
{
s3m_wind = 1;
}
void judas_rewinds3m(void)
{
s3m_wind = -1;
}
int judas_loads3m(char *name)
{
int handle;
int count;
unsigned char chconverttable[S3M_MAXCHANNELS];
INSTRUMENT *ip;
S3M_INSTRUMENT *instbuffer;
/* Don't waste memory if Nosound */
judas_error = JUDAS_OK;
if (judas_device == DEV_NOSOUND) return 1;
judas_error = JUDAS_OUT_OF_MEMORY;
if (s3mplayer_firsttime)
{
if (!init_s3mplayer()) return 0;
}
judas_frees3m();
/*
* Try to open the file
*/
judas_error = JUDAS_OPEN_ERROR;
handle = judas_open(name);
if (handle == -1)
{
return 0;
}
/*
* Read in the header
*/
judas_error = JUDAS_READ_ERROR;
if (judas_read(handle, &header, sizeof header) != sizeof header)
{
judas_close(handle);
return 0;
}
/*
* Check it is a S3M
*/
judas_error = JUDAS_WRONG_FORMAT;
if ((header.byte1a != 0x1a && header.byte1a != 0) || (header.filetype != 0x10) || (memcmp("SCRM", header.id, 4)))
{
judas_close(handle);
return 0;
}
header.byte1a = 0x0; /* Serves as the songname endzero from now on */
s3m_channels = 0;
for (count = 0; count < S3M_MAXCHANNELS; count++)
{
chconverttable[count] = 0xff;
if (header.chsettings[count] < 16)
{
chconverttable[count] = s3m_channels;
/* Stereo may be disabled */
if (header.mastermult & 128) {
if (header.chsettings[count] & 8) s3m_chpanning[(unsigned char)s3m_channels] = S3M_RIGHT;
else s3m_chpanning[(unsigned char)s3m_channels] = S3M_LEFT;
} else s3m_chpanning[(unsigned char)s3m_channels] = MIDDLE;
s3m_channels++;
}
}
/*
* Quit, if not enough channels
*/
if (s3m_channels > CHANNELS)
{
judas_error = JUDAS_OUT_OF_CHANNELS;
s3m_channels = CHANNELS;
judas_close(handle);
return 0;
}
/*
* Read in and check the orderlist
*/
memset(&s3m_order, 255, sizeof s3m_order);
judas_error = JUDAS_READ_ERROR;
if (judas_read(handle, &s3m_order, header.ordernum) != header.ordernum)
{
judas_close(handle);
return 0;
}
judas_error = JUDAS_WRONG_FORMAT;
{
int firstpattern = S3M_MAXORDER+666;
int firstloop = S3M_MAXORDER+666;
for (count = header.ordernum-1; count>=0; count--)
{
if (s3m_order[count] != 254)
{
if (s3m_order[count] == 255) firstloop = count;
else firstpattern = count;
}
}
if (firstpattern >= firstloop)
{
/*
* There was no patterns in the order list!!
*/
judas_close(handle);
return 0;
}
}
/*
* Read in the instrument & pattern parapointers
*/
judas_error = JUDAS_READ_ERROR;
if (judas_read(handle, &instparapointers, header.instnum * sizeof(short)) != header.instnum * sizeof(short))
{
judas_close(handle);
return 0;
}
if (judas_read(handle, &pattparapointers, header.pattnum * sizeof(short)) != header.pattnum * sizeof(short))
{
judas_close(handle);
return 0;
}
/*
* Reserve intermediate buffer for S3M format instruments (to prevent
* backwards seeks patterns & samples are loaded later)
*/
judas_error = JUDAS_OUT_OF_MEMORY;
instbuffer = malloc(header.instnum * sizeof(S3M_INSTRUMENT));
if (!instbuffer)
{
judas_close(handle);
return 0;
}
/*
* Read in and use default channel pan positions (panning magic) if enabled
*/
if (header.panningmagic == 252)
{
if (judas_read(handle, &defaultchannelpanpos, s3m_channels * sizeof(char)) != s3m_channels * sizeof(char))
{
judas_close(handle);
return 0;
}
for (count = 0; count < s3m_channels; count++)
{
if (defaultchannelpanpos[count] & 32) {
/* Channel panning pos defined */
s3m_chpanning[count] = ((defaultchannelpanpos[count] & 15) << 4) |
(defaultchannelpanpos[count] & 15);
} else {
/* Panning pos for this channel not defined. Use default */
switch (s3m_chpanning[count])
{
case MIDDLE:
s3m_chpanning[count] = 0x77; /* 128 would be faster though */
break;
case S3M_LEFT:
s3m_chpanning[count] = 0x33;
break;
case S3M_RIGHT:
s3m_chpanning[count] = 0xBB;
break;
}
}
}
}
/*
* Read instruments
*/
for (count = 0; count < header.instnum; count++)
{
S3M_INSTRUMENT *s3mip = instbuffer + count;
/*
* Seek to correct pos to find instrument
*/
judas_error = JUDAS_READ_ERROR;
if (judas_seek(handle, instparapointers[count] << 4, SEEK_SET) == -1)
{
free(instbuffer);
judas_close(handle);
return 0;
}
/*
* Read the instrument
*/
if (judas_read(handle, s3mip, sizeof (S3M_INSTRUMENT)) != sizeof (S3M_INSTRUMENT))
{
free(instbuffer);
judas_close(handle);
return 0;
}
if (s3mip->flags & 4)
{
s3mip->length <<= 1;
s3mip->loopstart <<= 1;
s3mip->loopend <<= 1;
}
}
/*
* Reserve memory for patterns
*/
judas_error = JUDAS_OUT_OF_MEMORY;
s3m_pattlength = 64 * sizeof(NOTE) * s3m_channels;
s3m_pattptr = locked_malloc(s3m_pattlength * header.pattnum);
if (!s3m_pattptr)
{
free(instbuffer);
judas_close(handle);
return 0;
}
/*
* Empty all the patterns
*/
{
NOTE *noteptr = (NOTE *)s3m_pattptr;
for (count = header.pattnum * s3m_channels * 64; count; count--)
{
noteptr->note = 0xff;
noteptr->voleffect = 0xff;
noteptr->instrument = 0x0;
noteptr->effect = 0xff;
noteptr++;
}
}
/*
* Read in & unpack patterns
*/
for (count = 0; count < header.pattnum; count++)
{
int rowcount;
unsigned char *packbuffer;
NOTE *destptr;
unsigned char *srcptr;
/*
* HUUDAN.S3M has weird patternparapointers that
* point to offset is 0! Since offset 0 contains
* the S3M header there cannot be any correct pattern.
* Seeking to offset 0 would cause a backwards seek, which is
* bad if you're using a compressed datafile!
*/
if (pattparapointers[count])
{
/*
* Seek to correct pos to find pattern
*/
judas_error = JUDAS_READ_ERROR;
if (judas_seek(handle, pattparapointers[count] << 4, SEEK_SET) == -1)
{
free(instbuffer);
judas_frees3m();
judas_close(handle);
return 0;
}
if (judas_read(handle, &loadpattlen, sizeof loadpattlen) != sizeof loadpattlen)
{
free(instbuffer);
judas_frees3m();
judas_close(handle);
return 0;
}
if (loadpattlen > 2)
{
/*
* Weird, packed data length has the length indicator
* word included (I really checked this out!)
*/
loadpattlen -= 2;
judas_error = JUDAS_OUT_OF_MEMORY;
packbuffer = malloc(loadpattlen);
memset(packbuffer, 0, loadpattlen);
if (!packbuffer)
{
free(instbuffer);
judas_frees3m();
judas_close(handle);
return 0;
}
judas_error = JUDAS_READ_ERROR;
if (judas_read(handle, packbuffer, loadpattlen) != loadpattlen)
{
free(instbuffer);
free(packbuffer);
judas_frees3m();
judas_close(handle);
return 0;
}
srcptr = packbuffer;
destptr = (NOTE *)(&s3m_pattptr[count * s3m_pattlength]);
for (rowcount = 0; rowcount < 64; rowcount++)
{
unsigned char control;
for(;;)
{
control = *srcptr++;
if (control)
{
unsigned char channel = chconverttable[control & 31];
if (control & 32)
{
if (channel < s3m_channels)
{
if (*srcptr < 254)
{
destptr[channel].note = (*srcptr & 15) + (*srcptr >> 4) * 12;
}
else destptr[channel].note = *srcptr;
srcptr++;
destptr[channel].instrument = *srcptr++;
}
else srcptr += 2;
}
if (control & 64)
{
if (channel < s3m_channels)
{
destptr[channel].voleffect = *srcptr++;
}
else srcptr++;
}
if (control & 128)
{
if (channel < s3m_channels)
{
destptr[channel].effect = *srcptr++;
destptr[channel].effectdata = *srcptr++;
}
else srcptr += 2;
}
}
else break;
}
destptr += s3m_channels;
}
free(packbuffer);
}
}
}
/*
* Now read samples
*/
ip = &s3m_instrument[0];
for (count = 0; count < header.instnum; count++)
{
S3M_INSTRUMENT *s3mip = instbuffer + count;
ip->vol = s3mip->vol;
ip->c2rate = s3mip->c2rate;
/*
* Alloc & read sample (if there is one)
*/
if ((s3mip->type == 1) && (s3mip->length))
{
judas_error = JUDAS_OUT_OF_MEMORY;
ip->sp = judas_allocsample(s3mip->length);
if (!ip->sp)
{
free(instbuffer);
judas_frees3m();
judas_close(handle);
return 0;
}
/*
* Seek & read sample
*/
judas_seek(handle, (s3mip->paraptr | (s3mip->paraptrhigh << 16)) << 4, SEEK_SET);
judas_read(handle, ip->sp->start, s3mip->length);
/*
* Looped or non-looped?
*/
if ((s3mip->flags & 1) && (s3mip->loopstart != s3mip->loopend))
{
ip->sp->voicemode = VM_LOOP | VM_ON;
ip->sp->repeat = ip->sp->start + s3mip->loopstart;
ip->sp->end = ip->sp->start + s3mip->loopend;
}
else
{
ip->sp->voicemode = VM_ONESHOT | VM_ON;
ip->sp->repeat = ip->sp->start;
ip->sp->end = ip->sp->start + s3mip->length;
}
/*
* Sixteenbit?
*/
if (s3mip->flags & 4)
{
ip->sp->voicemode |= VM_16BIT;
}
/*
* Convert to signed if necessary
*/
if (header.fileversion == 2)
{
if (s3mip->flags & 4)
{
unsigned short *cptr = (unsigned short *)ip->sp->start;
unsigned ccount = s3mip->length >> 1;
while (ccount)
{
*cptr += 0x8000;
cptr++;
ccount--;
}
}
else
{
unsigned char *cptr = ip->sp->start;
unsigned ccount = s3mip->length;
while (ccount)
{
*cptr += 0x80;
cptr++;
ccount--;
}
}
}
/*
* All done, just correct interpolation
*/
judas_ipcorrect(ip->sp);
}
ip++;
}
free(instbuffer);
judas_error = JUDAS_OK;
judas_close(handle);
s3m_loaded = 1;
return 1;
}
void judas_frees3m(void)
{
int count;
INSTRUMENT *ip = &s3m_instrument[0];
if (s3mplayer_firsttime)
{
if (!init_s3mplayer()) return;
}
judas_stops3m();
s3m_loaded = 0;
if (s3m_pattptr)
{
locked_free(s3m_pattptr);
s3m_pattptr = NULL;
}
for (count = 0; count < S3M_MAXINSTRUMENTS; count++)
{
if (ip->sp)
{
judas_freesample(ip->sp);
ip->sp = NULL;
}
ip++;
}
}
unsigned char judas_gets3mpos(void)
{
return s3m_pos;
}
unsigned char judas_gets3mline(void)
{
return s3m_line;
}
unsigned char judas_gets3mtick(void)
{
return s3m_tickcount;
}
unsigned char judas_gets3mchannels(void)
{
return s3m_channels;
}
char *judas_gets3mname(void)
{
if (s3m_loaded) return header.songname;
else return NULL;
}
static int init_s3mplayer(void)
{
int count;
INSTRUMENT *ip = &s3m_instrument[0];
s3m_channels = 0;
for (count = 0; count < S3M_MAXINSTRUMENTS; count++)
{
ip->sp = NULL;
ip++;
}
if (!judas_locktables()) return 0;
if (!judas_memlock(&s3mplayer_code_lock_start, (int)&s3mplayer_code_lock_end - (int)&s3mplayer_code_lock_start)) return 0;
if (!judas_memlock(&s3mperiodtable, sizeof s3mperiodtable)) return 0;
if (!judas_memlock(&s3mfinetunetable, sizeof s3mfinetunetable)) return 0;
if (!judas_memlock(&header, sizeof header)) return 0;
if (!judas_memlock(&s3m_loaded, sizeof s3m_loaded)) return 0;
if (!judas_memlock(&s3m_channels, sizeof s3m_channels)) return 0;
if (!judas_memlock(&s3m_chpanning, sizeof s3m_chpanning)) return 0;
if (!judas_memlock(&s3m_poshasbeenplayed, sizeof s3m_poshasbeenplayed)) return 0;
if (!judas_memlock(&s3m_order, sizeof s3m_order)) return 0;
if (!judas_memlock(&s3m_instrument, sizeof s3m_instrument)) return 0;
if (!judas_memlock(&s3m_track, sizeof s3m_track)) return 0;
if (!judas_memlock(&s3m_pattlength, sizeof s3m_pattlength)) return 0;
if (!judas_memlock(&s3m_pattptr, sizeof s3m_pattptr)) return 0;
if (!judas_memlock(&s3m_oldpos, sizeof s3m_oldpos)) return 0;
if (!judas_memlock(&s3m_pos, sizeof s3m_pos)) return 0;
if (!judas_memlock(&s3m_line, sizeof s3m_line)) return 0;
if (!judas_memlock(&s3m_ticktempo, sizeof s3m_ticktempo)) return 0;
if (!judas_memlock(&s3m_tickcount, sizeof s3m_tickcount)) return 0;
if (!judas_memlock(&s3m_globalvol, sizeof s3m_globalvol)) return 0;
if (!judas_memlock(&s3m_patternbreak, sizeof s3m_patternbreak)) return 0;
if (!judas_memlock(&s3m_patterndelay, sizeof s3m_patterndelay)) return 0;
if (!judas_memlock(&s3m_patternloopline, sizeof s3m_patternloopline)) return 0;
if (!judas_memlock(&s3m_patternloopcount, sizeof s3m_patternloopcount)) return 0;
if (!judas_memlock(&s3m_roundcounter, sizeof s3m_roundcounter)) return 0;
if (!judas_memlock(&s3m_wind, sizeof s3m_wind)) return 0;
if (!judas_memlock(&s3m_windingpause, sizeof s3m_windingpause)) return 0;
s3mplayer_firsttime = 0;
return 1;
}
static void s3mplayer_code_lock_start(void)
{
}
void judas_mutechannelss3m(void)
{
int count;
TRACK *tptr = &s3m_track[0];
CHANNEL *chptr = &judas_channel[0];
for (count = 0; count < s3m_channels; count++)
{
chptr->smp = NULL;
chptr->voicemode = VM_OFF;
chptr->panning = s3m_chpanning[count];
tptr->ip = &s3m_instrument[0];
tptr->sp = NULL;
tptr->c2rate = 8363;
tptr->newnote = 0;
tptr->instrument = 0;
tptr->note = 0;
tptr->notevol = 0;
tptr->vol = 0;
tptr->effect = 0;
tptr->effectdata = 0;
tptr->nybble1 = 0;
tptr->nybble2 = 0;
tptr->voleffect = 0;
tptr->tp = 0;
tptr->tpspeed = 0;
tptr->volspeedup = 0;
tptr->volspeeddown = 0;
tptr->retriginterval = 0;
tptr->arpeggiointerval = 0;
tptr->retrigvolchange = 0;
tptr->sampleoffset = 0;
tptr->usesampleoffset = 0;
tptr->vibratotype = 0;
tptr->tremolotype = 0;
chptr++;
tptr++;
}
}
void judas_plays3m(int rounds)
{
int count;
if (!s3m_loaded) return;
judas_player = NULL;
s3m_roundcounter = rounds;
s3m_wind = 0;
s3m_pos = 0;
s3m_oldpos = s3m_pos;
s3m_line = 0;
s3m_tickcount = 0;
s3m_ticktempo = header.initialspeed;
s3m_globalvol = header.globalvol;
s3m_patterndelay = 0;
judas_bpmcount = 0;
judas_bpmtempo = header.initialtempo;
for (count = 1; count < S3M_MAXORDER; count++)
{
s3m_poshasbeenplayed[count] = 0;
}
s3m_poshasbeenplayed[0] = 1;
judas_mutechannelss3m();
judas_player = &s3mplayer;
}
void judas_stops3m(void)
{
int count;
CHANNEL *chptr = &judas_channel[0];
if (s3m_channels > CHANNELS) s3m_channels = CHANNELS;
if (!s3m_loaded) return;
judas_player = NULL;
for (count = s3m_channels; count > 0; count--)
{
chptr->smp = NULL;
chptr->voicemode = VM_OFF;
chptr++;
}
}
void s3mplayer(void)
{
TRACK *tptr = &s3m_track[0];
CHANNEL *chptr = &judas_channel[0];
int count;
/*
* Song winding
*/
if (s3m_wind > 0) {
s3m_wind = 0;
if (s3m_pos < header.ordernum-1) {
int count;
for (count = s3m_pos+1; count < header.ordernum; count++)
{
if (s3m_order[count] != 254 && s3m_order[count] != 255) break;
}
if (count != header.ordernum) {
s3m_pos++;
s3m_oldpos = s3m_pos;
s3m_line = 0;
s3m_tickcount = 0;
s3m_patterndelay = 0;
for (count = 0; count < header.ordernum; count++)
{
s3m_poshasbeenplayed[count] = 0;
}
s3m_poshasbeenplayed[s3m_pos] = 1;
judas_mutechannelss3m();
s3m_windingpause = 1;
return;
}
}
} else
if (s3m_wind < 0) {
s3m_wind = 0;
if (s3m_pos > 0) {
int count;
s3m_pos--;
s3m_oldpos = s3m_pos;
s3m_line = 0;
s3m_tickcount = 0;
s3m_patterndelay = 0;
for (count = 0; count < header.ordernum; count++)
{
s3m_poshasbeenplayed[count] = 0;
}
s3m_poshasbeenplayed[s3m_pos] = 1;
judas_mutechannelss3m();
s3m_windingpause = 1;
return;
}
}
if (s3m_windingpause) {
s3m_windingpause = 0;
return;
}
/*
* Set new notes or do something else?
*/
if ((!s3m_tickcount) && (!s3m_patterndelay))
{
NOTE *noteptr;
if (s3m_order[s3m_pos] == 254)
{
while (s3m_order[s3m_pos] == 254)
{
s3m_pos++;
if (s3m_pos >= header.ordernum) {
s3m_pos = 0;
s3m_oldpos = s3m_pos;
if (s3m_roundcounter != 1)
{
int count;
if (s3m_roundcounter) s3m_roundcounter--;
for (count = 0; count < header.ordernum; count++)
{
s3m_poshasbeenplayed[count] = 0;
}
s3m_poshasbeenplayed[s3m_pos] = 1;
}
else {
judas_stops3m();
return;
}
} else s3m_oldpos = S3M_MAXORDER+666;
}
}
if (s3m_order[s3m_pos] == 255)
{
s3m_pos = 0;
s3m_oldpos = s3m_pos;
if (s3m_roundcounter != 1)
{
int count;
if (s3m_roundcounter) s3m_roundcounter--;
for (count = 0; count < header.ordernum; count++)
{
s3m_poshasbeenplayed[count] = 0;
}
s3m_poshasbeenplayed[s3m_pos] = 1;
}
else {
judas_stops3m();
return;
}
while (s3m_order[s3m_pos] == 254)
{
s3m_pos++;
if (s3m_pos >= header.ordernum)
{
judas_stops3m();
return;
}
}
}
/*
* Song looping detection & control!
*/
if (s3m_pos != s3m_oldpos)
{
if (s3m_poshasbeenplayed[s3m_pos])
{
if (s3m_roundcounter != 1)
{
int count;
if (s3m_roundcounter) s3m_roundcounter--;
for (count = 0; count < header.ordernum; count++)
{
s3m_poshasbeenplayed[count] = 0;
}
s3m_poshasbeenplayed[s3m_pos] = 1;
}
else {
judas_stops3m();
return;
}
}
else s3m_poshasbeenplayed[s3m_pos] = 1;
}
noteptr = (NOTE *)&s3m_pattptr[s3m_pattlength * s3m_order[s3m_pos] + sizeof(NOTE) * s3m_line * s3m_channels];