-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMODPlayer.bas
2313 lines (1825 loc) · 101 KB
/
MODPlayer.bas
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
'-----------------------------------------------------------------------------------------------------------------------
' MOD Player Library
' Copyright (c) 2024 Samuel Gomes
'-----------------------------------------------------------------------------------------------------------------------
$INCLUDEONCE
'$INCLUDE:'MODPlayer.bi'
'-----------------------------------------------------------------------------------------------------------------------
' Small test code for debugging the library
'-----------------------------------------------------------------------------------------------------------------------
'$CONSOLE
'$DEBUG
'$ASSERTS:CONSOLE
'DO
' DIM fileName AS STRING: fileName = _OPENFILEDIALOG$("Open", , "*.mod|*.MOD|*.mtm|*.MTM|*.s3m|*.S3M", "Music Module Files")
' IF NOT _FILEEXISTS(fileName) THEN EXIT DO
' DIM AS _BYTE pause, repeat
' IF MODPlayer_LoadFromDisk(fileName) THEN
' MODPlayer_Play
' DIM k AS LONG
' DO
' MODPlayer_Update SOFTSYNTH_SOUND_BUFFER_TIME_DEFAULT
' k = _KEYHIT
' SELECT CASE k
' CASE 32
' pause = NOT pause
' MODPlayer_Pause pause
' _KEYCLEAR
' CASE 108, 76
' repeat = NOT repeat
' MODPlayer_Loop repeat
' _KEYCLEAR
' END SELECT
' LOCATE 1, 1
' PRINT USING "Order: ### / ### Pattern: ### / ### Row: ## / 63 BPM: ### Speed: ###"; MODPlayer_GetPosition; MODPlayer_GetOrders - 1; __Order(__Song.orderPosition); __Song.patterns - 1; __Song.patternRow; __Song.BPM; __Song.speed
' PRINT USING "Buffer Time: #####ms Loop: ##"; SoftSynth_GetBufferedSoundTime * 1000; repeat;
' _LIMIT 60
' LOOP WHILE k <> 27 AND MODPlayer_IsPlaying
' MODPlayer_Stop
' END IF
'LOOP
'END
'-----------------------------------------------------------------------------------------------------------------------
' Loads all required LUTs from DATA
SUB __MODPlayer_LoadTables
SHARED __Song AS __SongType
SHARED __PeriodTable() AS _UNSIGNED INTEGER
SHARED __SineTable() AS _UNSIGNED _BYTE
SHARED __InvertLoopSpeedTable() AS _UNSIGNED _BYTE
' Load the period table
RESTORE PeriodTab
READ __Song.periodTableMax ' read the size
__Song.periodTableMax = __Song.periodTableMax - 1 ' change to ubound
REDIM __PeriodTable(0 TO __Song.periodTableMax) AS _UNSIGNED INTEGER ' allocate size elements
' Now read size values
DIM i AS LONG: FOR i = 0 TO __Song.periodTableMax
READ __PeriodTable(i)
NEXT
' Load the sine table
RESTORE SineTab
DIM s AS LONG: READ s
REDIM __SineTable(0 TO s - 1) AS _UNSIGNED _BYTE
FOR i = 0 TO s - 1
READ __SineTable(i)
NEXT
' Load the invert loop table
RESTORE ILSpdTab
READ s
REDIM __InvertLoopSpeedTable(0 TO s - 1) AS _UNSIGNED _BYTE
FOR i = 0 TO s - 1
READ __InvertLoopSpeedTable(i)
NEXT
' Amiga period table data for 11 octaves
PeriodTab:
DATA 134
DATA 27392,25856,24384,23040,21696,20480,19328,18240,17216,16256,15360,14496
DATA 13696,12928,12192,11520,10848,10240,9664,9120,8608,8128,7680,7248
DATA 6848,6464,6096,5760,5424,5120,4832,4560,4304,4064,3840,3624
DATA 3424,3232,3048,2880,2712,2560,2416,2280,2152,2032,1920,1812
DATA 1712,1616,1524,1440,1356,1280,1208,1140,1076,1016,960,906
DATA 856,808,762,720,678,640,604,570,538,508,480,453
DATA 428,404,381,360,339,320,302,285,269,254,240,226
DATA 214,202,190,180,170,160,151,143,135,127,120,113
DATA 107,101,95,90,85,80,75,71,67,63,60,56
DATA 53,50,47,45,42,40,37,35,33,31,30,28
DATA 26,25,23,22,21,20,18,17,16,15,15,14
DATA 0,0
DATA NaN
' Sine table data for tremolo & vibrato
SineTab:
DATA 32
DATA 0,24,49,74,97,120,141,161,180,197,212,224,235,244,250,253
DATA 255,253,250,244,235,224,212,197,180,161,141,120,97,74,49,24
DATA NaN
' Invert loop speed table data for EFx
ILSpdTab:
DATA 16
DATA 0,5,6,7,8,10,11,13,16,19,22,26,32,43,64,128
DATA NaN
END SUB
' This resets all song properties to defaults
' We do this so that every tune load begins in a known consistent state
SUB __MODPlayer_InitializeSong
SHARED __Song AS __SongType
__Song.caption = _STR_EMPTY
__Song.subtype = _STR_EMPTY
__Song.comment = _STR_EMPTY
__Song.channels = NULL
__Song.instruments = NULL
__Song.orders = NULL
__Song.rows = NULL
__Song.endJumpOrder = NULL
__Song.patterns = NULL
__Song.orderPosition = NULL
__Song.patternRow = NULL
__Song.tickPattern = NULL
__Song.tickPatternRow = NULL
__Song.isLooping = _FALSE
__Song.isPlaying = _FALSE
__Song.isPaused = _FALSE
__Song.patternDelay = NULL
__Song.periodTableMax = NULL
__Song.speed = NULL
__Song.BPM = NULL
__Song.defaultSpeed = __SONG_SPEED_DEFAULT ' set this to default MOD speed
__Song.defaultBPM = __SONG_BPM_DEFAULT ' set this to default MOD BPM
__Song.tick = NULL
__Song.tempoTimerValue = NULL
__Song.framesPerTick = NULL
__Song.activeChannels = NULL
__Song.useAmigaLPF = _FALSE
__Song.useST2Vibrato = _FALSE
__Song.useST2Tempo = _FALSE
__Song.useAmigaSlides = _FALSE
__Song.useVolumeOptimization = _FALSE
__Song.useAmigaLimits = _FALSE
__Song.useFilterSFX = _FALSE
__Song.useST300VolumeSlides = _FALSE
__Song.hasSpecialCustomData = _FALSE
END SUB
' Loads an S3M file into memory and prepares all required globals
FUNCTION __MODPlayer_LoadS3M%% (buffer AS STRING)
SHARED __Song AS __SongType
SHARED __Order() AS _UNSIGNED INTEGER
SHARED __Pattern() AS __NoteType
SHARED __Instrument() AS __InstrumentType
SHARED __Channel() AS __ChannelType
' Initialize the softsynth sample mixer
IF NOT SoftSynth_Initialize THEN EXIT FUNCTION
__MODPlayer_InitializeSong ' just in case something is playing
' Open the buffer as a StringFile
DIM memFile AS StringFileType
StringFile_Create memFile, buffer
' Seek to offset 44 (2Ch) in the file & read the file signature
StringFile_Seek memFile, 44
__Song.subtype = StringFile_ReadString(memFile, 4) ' 4 bytes
' Check if this is really an S3M file
IF __Song.subtype <> "SCRM" THEN EXIT FUNCTION
' Seek to the beginning of the file and get the song title
StringFile_Seek memFile, 0
__Song.caption = String_MakePrintable(StringFile_ReadString(memFile, 28)) ' 28 bytes
' Skip past the DOS EOF marker byte, file type byte and expansion / reserved word
StringFile_Seek memFile, StringFile_GetPosition(memFile) + 4 ' 4 bytes
' Read the song orders (note that this count includes 254 & 255 marker orders)
__Song.orders = StringFile_ReadInteger(memFile) ' TODO: this includes markers!
' Read the number of instruments
__Song.instruments = StringFile_ReadInteger(memFile)
' Read the number of patterns
__Song.patterns = StringFile_ReadInteger(memFile) ' TODO: includes marker patterns!
' Read the 16-bit flags
DIM word1 AS _UNSIGNED INTEGER: word1 = StringFile_ReadInteger(memFile)
' Set individual flags
' TODO: Check and discard members which are not really needed
__Song.useST2Vibrato = _READBIT(word1, 0)
__Song.useST2Tempo = _READBIT(word1, 1)
__Song.useAmigaSlides = _READBIT(word1, 2)
__Song.useVolumeOptimization = _READBIT(word1, 3)
__Song.useAmigaLimits = _READBIT(word1, 4)
__Song.useFilterSFX = _READBIT(word1, 5)
__Song.useST300VolumeSlides = _READBIT(word1, 6)
__Song.hasSpecialCustomData = _READBIT(word1, 7)
'_ECHO "useST2Vibrato =" + STR$(__Song.useST2Vibrato)
'_ECHO "useST2Tempo =" + STR$(__Song.useST2Tempo)
'_ECHO "useAmigaSlides =" + STR$(__Song.useAmigaSlides)
'_ECHO "useVolumeOptimization =" + STR$(__Song.useVolumeOptimization)
'_ECHO "useAmigaLimits =" + STR$(__Song.useAmigaLimits)
'_ECHO "useFilterSFX =" + STR$(__Song.useFilterSFX)
'_ECHO "useST300VolumeSlides =" + STR$(__Song.useST300VolumeSlides)
'_ECHO "hasSpecialCustomData =" + STR$(__Song.hasSpecialCustomData)
' Read "Created with tracker / version" info
word1 = StringFile_ReadInteger(memFile)
'_ECHO "CWT/V = " + HEX$(word1)
' ST3.00 does volumeslides on EVERY tick. So we'll update the flag
__Song.useST300VolumeSlides = __Song.useST300VolumeSlides OR (word1 = &H1300)
'_ECHO "useST300VolumeSlides =" + STR$(__Song.useST300VolumeSlides)
' Read the sample format type
word1 = StringFile_ReadInteger(memFile)
'_ECHO "Sample Format =" + STR$(word1)
DIM isUnsignedFormat AS _BYTE: isUnsignedFormat = (word1 = 2)
'_ECHO "isUnsignedFormat =" + STR$(isUnsignedFormat)
' Skip the 4 byte signature
StringFile_Seek memFile, StringFile_GetPosition(memFile) + 4
' Read and set the global volume
DIM byte1 AS _UNSIGNED _BYTE: byte1 = StringFile_ReadByte(memFile)
'_ECHO "Global volume =" + STR$(byte1)
IF byte1 > __S3M_GLOBAL_VOLUME_MAX THEN byte1 = __S3M_GLOBAL_VOLUME_MAX
SoftSynth_SetGlobalVolume byte1 / __S3M_GLOBAL_VOLUME_MAX
'_ECHO "SoftSynth global volume =" + STR$(SoftSynth_GetGlobalVolume)
' Read the initial speed value
__Song.defaultSpeed = StringFile_ReadByte(memFile)
'_ECHO "Initial speed =" + STR$(__Song.defaultSpeed)
' Read the initial BPM
__Song.defaultBPM = StringFile_ReadByte(memFile)
'_ECHO "Initial BPM =" + STR$(__Song.defaultBPM)
IF __Song.defaultBPM = 0 THEN __Song.defaultBPM = __SONG_BPM_DEFAULT ' avoid division by zero
' Read SoundBlaster master volume crap and check if the stereo frag is set
byte1 = StringFile_ReadByte(memFile)
'_ECHO "SoundBlaster master volume =" + STR$(byte1)
DIM isStereo AS _BYTE: isStereo = _READBIT(byte1, 7)
'_ECHO "isStereo =" + STR$(isStereo)
' Skip Ultrasound ultra-click removal crap
StringFile_Seek memFile, StringFile_GetPosition(memFile) + 1 ' 1 byte
' Read and store if we have to load default pan values later
byte1 = StringFile_ReadByte(memFile)
'_ECHO "Default panning =" + STR$(byte1)
DIM useDefaultPanning AS _BYTE: useDefaultPanning = (byte1 = 252)
'_ECHO "useDefaultPanning =" + STR$(useDefaultPanning)
' Skip 8 reserved bytes and the 2 byte special parapointer
StringFile_Seek memFile, StringFile_GetPosition(memFile) + 10
' Load channel info and count total channels
DIM channelInfo(0 TO __MTM_S3M_CHANNEL_MAX) AS _UNSIGNED _BYTE ' channel info that we'll use later
DIM i AS LONG: FOR i = 0 TO __MTM_S3M_CHANNEL_MAX
channelInfo(i) = StringFile_ReadByte(memFile)
' Check if the channel is enabled
' 0 <= x <= 7: Left PCM channel 1-8 (Lx)
' 8 <= x <= 15: Right PCM channel 1-8 (Rx)
' 16 <= x <= 24: Adlib/OPL2 #1 melody (Ax)
' 25 <= x <= 29: Adlib/OPL2 #1 drums (Ax)
' Channel is enabled if bit 7 is not set
IF NOT _READBIT(channelInfo(i), 7) THEN __Song.channels = i + 1 ' change to count
'_ECHO "Channel info" + STR$(i) + " =" + STR$(channelInfo(i))
NEXT i
'_ECHO "Channels =" + STR$(__Song.channels)
' Resize the channel array
REDIM __Channel(0 TO __Song.channels - 1) AS __ChannelType
' Allocate the number of SoftSynth voices we need
SoftSynth_SetTotalVoices __Song.channels
' Set voice panning positions
FOR i = 0 TO __Song.channels - 1
' 0 <= x <= 7: Left PCM channel 1-8 (Lx)
' 8 <= x <= 15: Right PCM channel 1-8 (Rx)
' 16 <= x <= 24: Adlib/OPL2 #1 melody (Ax)
' 25 <= x <= 29: Adlib/OPL2 #1 drums (Ax)
SELECT CASE channelInfo(i)
CASE 0 TO 7
SoftSynth_SetVoiceBalance i, -__CHANNEL_STEREO_SEPARATION ' pan to the left
__Channel(i).subtype = __INSTRUMENT_PCM
CASE 8 TO 15
SoftSynth_SetVoiceBalance i, __CHANNEL_STEREO_SEPARATION ' pan it to the right
__Channel(i).subtype = __INSTRUMENT_PCM
CASE 16 TO 24
__Channel(i).subtype = __INSTRUMENT_FM_MELODY ' FM melody channel
CASE 25 TO 29
__Channel(i).subtype = __INSTRUMENT_FM_BASSDRUM ' FM drums channel
END SELECT
'_ECHO "Channel " + STR$(i) + " enabled =" + STR$(NOT _READBIT(channelInfo(i), 7))
'_ECHO "Channel " + STR$(i) + " panning =" + STR$(SoftSynth_GetVoiceBalance(i)) + " , subtype =" + STR$(__Channel(i).subtype)
NEXT i
' Resize the order array
REDIM __Order(0 TO __Song.orders - 1) AS _UNSIGNED INTEGER
' Read order list
' Note: We'll need to handle 254 & 255 marker orders correctly in the player
FOR i = 0 TO __Song.orders - 1
__Order(i) = StringFile_ReadByte(memFile)
'_ECHO "Order" + STR$(i) + " =" + STR$(__Order(i))
NEXT i
' Load and convert instrument parapointers
DIM instrumentPointer(0 TO __Song.instruments - 1) AS _UNSIGNED LONG
FOR i = 0 TO __Song.instruments - 1
instrumentPointer(i) = _SHL(StringFile_ReadInteger(memFile), 4) ' convert to real file offset (x 16)
'_ECHO "Instrument" + STR$(i) + " is at" + STR$(instrumentPointer(i))
NEXT i
' Load and convert pattern parapointers
DIM patternPointer(0 TO __Song.patterns - 1) AS _UNSIGNED LONG
FOR i = 0 TO __Song.patterns - 1
patternPointer(i) = _SHL(StringFile_ReadInteger(memFile), 4) ' convert to real file offset (x 16)
'_ECHO "Pattern" + STR$(i) + " is at" + STR$(patternPointer(i))
NEXT i
' Load the panning table if it is present
IF useDefaultPanning THEN
' We have a total of 32 pan positions in the file
FOR i = 0 TO __MTM_S3M_CHANNEL_MAX
byte1 = StringFile_ReadByte(memFile)
' Only set the pan position if the channel is there
IF i < __Song.channels THEN
' WTF! Ewww!
IF _READBIT(byte1, 4) THEN ' if bit 4 is set - byte1 AND &H10
SoftSynth_SetVoiceBalance i, (byte1 AND 15) / 15! * 2! - SOFTSYNTH_VOICE_PAN_RIGHT ' pan = (x / 15) * 2 - 1
'_ECHO "Channel " + STR$(i) + " panning =" + STR$(SoftSynth_GetVoiceBalance(i))
END IF
END IF
NEXT i
END IF
' Set everything to mono if stereo flag is not set. This is so retarded! Sigh!
IF NOT isStereo THEN
FOR i = 0 TO __Song.channels - 1
SoftSynth_SetVoiceBalance i, 0!
'_ECHO "Channel " + STR$(i) + " panning =" + STR$(SoftSynth_GetVoiceBalance(i))
NEXT i
END IF
' Resize the instruments array
REDIM __Instrument(0 TO __Song.instruments - 1) AS __InstrumentType
DIM isSampleCompressed(0 TO __Song.instruments - 1) AS _BYTE ' flags that tells us if we need to decode ADPCM samples
' Go through the instrument data and load whatever we need
FOR i = 0 TO __Song.instruments - 1
'_ECHO "Instrument:" + STR$(i)
' Seek to the correct position in the file to read the instrument info
StringFile_Seek memFile, instrumentPointer(i)
' Read the instrument type
__Instrument(i).subtype = StringFile_ReadByte(memFile)
'_ECHO " Type =" + STR$(__Instrument(i).subtype)
' Read the instrument file name. We'll replace it with the title later on if needed
__Instrument(i).caption = String_MakePrintable(String_ToBStr(StringFile_ReadString(memFile, 12)))
'_ECHO " File name = " + __Instrument(i).caption
IF __Instrument(i).subtype > __INSTRUMENT_PCM THEN ' this is an FM instrument
'TODO: Implement FM instrument loading support
ERROR _ERR_FEATURE_UNAVAILABLE
ELSE ' this is a standard PCM or blank instrument
' Read and convert the actual address to the PCM instrument sample data
' Convert this to a long. Eww! WTF!
byte1 = StringFile_ReadByte(memFile)
word1 = StringFile_ReadInteger(memFile)
instrumentPointer(i) = _SHL(_SHL(byte1, 16) + word1, 4) ' we'll re-use instrumentPointer array for this
'_ECHO "Sample" + STR$(i) + " is at" + STR$(instrumentPointer(i))
' Read the length
__Instrument(i).length = StringFile_ReadLong(memFile)
'_ECHO " Length =" + STR$(__Instrument(i).length)
' Read loop start
__Instrument(i).loopStart = StringFile_ReadLong(memFile)
'_ECHO " Loop start =" + STR$(__Instrument(i).loopStart)
' Read loop end
__Instrument(i).loopEnd = StringFile_ReadLong(memFile)
'_ECHO " Loop end =" + STR$(__Instrument(i).loopEnd)
' Read volume
__Instrument(i).volume = StringFile_ReadByte(memFile)
IF __Instrument(i).volume > __INSTRUMENT_VOLUME_MAX THEN __Instrument(i).volume = __INSTRUMENT_VOLUME_MAX
'_ECHO " Volume =" + STR$(__Instrument(i).volume)
' Skip 1 reserved byte
StringFile_Seek memFile, StringFile_GetPosition(memFile) + 1
' 0 = PCM, 1 = DP30ADPCM (WTF is this?), 3 = ADPCM (this is mostly ModPlug), 4 = also ADPCM (apparently; per milkyplay)
isSampleCompressed(i) = StringFile_ReadByte(memFile) > 0 ' we'll attempt to decompress anything that is not PCM. Oh well...
' bits: 0 = loop, 1 = stereo (chans not interleaved! fuck fuck fuck), 2 = 16-bit samples (little endian)
byte1 = StringFile_ReadByte(memFile)
IF _READBIT(byte1, 0) THEN
__Instrument(i).playMode = SOFTSYNTH_VOICE_PLAY_FORWARD_LOOP
ELSE
__Instrument(i).playMode = SOFTSYNTH_VOICE_PLAY_FORWARD
END IF
'_ECHO " Playmode =" + STR$(__Instrument(i).playMode)
IF _READBIT(byte1, 1) THEN
__Instrument(i).channels = 2
isSampleCompressed(i) = _FALSE ' per OpenMPT stereo samples cannot be compressed
ELSE
__Instrument(i).channels = 1
END IF
'_ECHO " Channels =" + STR$(__Instrument(i).channels)
IF _READBIT(byte1, 2) THEN
__Instrument(i).bytesPerSample = _SIZE_OF_INTEGER
isSampleCompressed(i) = _FALSE ' per OpenMPT 16-bit samples cannot be compressed
ELSE
__Instrument(i).bytesPerSample = _SIZE_OF_BYTE
END IF
'_ECHO " Bytes / sample =" + STR$(__Instrument(i).bytesPerSample)
' Read C2SPD (only the low word is used)
__Instrument(i).c2Spd = StringFile_ReadLong(memFile) AND &HFFFF
'_ECHO " C2SPD =" + STR$(__Instrument(i).c2Spd)
' Skip 12 useless bytes
StringFile_Seek memFile, StringFile_GetPosition(memFile) + 12
' Store the instrument name if it is not empty
DIM instrumentName AS STRING: instrumentName = String_MakePrintable(StringFile_ReadString(memFile, 28))
IF LEN(_TRIM$(instrumentName)) <> NULL THEN __Instrument(i).caption = instrumentName
'_ECHO " Instrument name = " + __Instrument(i).caption
' Read and verify the 'SCRS' label. This will ensure we are reading the file correctly somewhat until this point
IF StringFile_ReadString(memFile, 4) <> "SCRS" AND __INSTRUMENT_PCM = __Instrument(i).subtype THEN EXIT FUNCTION
' Validate a few things
IF __Instrument(i).playMode = SOFTSYNTH_VOICE_PLAY_FORWARD_LOOP AND __Instrument(i).loopEnd > 2 THEN
IF __Instrument(i).loopEnd > __Instrument(i).length THEN __Instrument(i).loopEnd = __Instrument(i).length
ELSE
__Instrument(i).playMode = SOFTSYNTH_VOICE_PLAY_FORWARD
__Instrument(i).loopStart = 0
__Instrument(i).loopEnd = __Instrument(i).length
END IF
END IF
NEXT i
__Song.rows = __MOD_S3M_ROWS ' S3M specific value
' Resize the pattern data array
REDIM __Pattern(0 TO __Song.patterns - 1, 0 TO __Song.rows - 1, 0 TO __Song.channels - 1) AS __NoteType
' Load pattern data and convert it to MOD / MTM style
FOR i = 0 TO __Song.patterns - 1
' Set the entire pattern to defaults first
DIM row AS LONG: FOR row = 0 TO __Song.rows - 1
DIM chan AS LONG: FOR chan = 0 TO __Song.channels - 1
__Pattern(i, row, chan).note = __NOTE_NONE
__Pattern(i, row, chan).instrument = 0
__Pattern(i, row, chan).effect = 0
__Pattern(i, row, chan).operand = 0
__Pattern(i, row, chan).volume = __NOTE_NO_VOLUME
NEXT chan
NEXT row
' Seek to the correct position in the file to read the pattern data
StringFile_Seek memFile, patternPointer(i) + 2 ' + 2 here is for the packed data size that we'll conveniently ignore
row = 0
WHILE row < __Song.rows
DIM flags AS _UNSIGNED _BYTE: flags = StringFile_ReadByte(memFile)
IF flags THEN ' we have some data; unpack it
chan = flags AND 31 ' get the channel number
IF chan >= __Song.channels _ORELSE _READBIT(channelInfo(chan), 7) THEN
' Invalid channel: we'll ignore the remaining packed data
IF flags AND 32 THEN ' ignore the note
StringFile_Seek memFile, StringFile_GetPosition(memFile) + 2
END IF
IF flags AND 64 THEN ' ignore the volume data
StringFile_Seek memFile, StringFile_GetPosition(memFile) + 1
END IF
IF flags AND 128 THEN ' ignore the effect data
StringFile_Seek memFile, StringFile_GetPosition(memFile) + 2
END IF
ELSE
IF flags AND 32 THEN ' we have a note
word1 = StringFile_ReadByte(memFile)
SELECT CASE word1
CASE 255
__Pattern(i, row, chan).note = __NOTE_NONE
CASE 254
__Pattern(i, row, chan).note = __NOTE_KEY_OFF
CASE ELSE
__Pattern(i, row, chan).note = _SHR(word1, 4) * 12 + (word1 AND &HF)
END SELECT
__Pattern(i, row, chan).instrument = StringFile_ReadByte(memFile)
'IF __Pattern(i, row, chan).instrument >= __Song.instruments THEN _ECHO "Invalid instrument:" + STR$(__Pattern(i, row, chan).instrument)
END IF
IF flags AND 64 THEN ' we have volume data
__Pattern(i, row, chan).volume = StringFile_ReadByte(memFile)
END IF
IF flags AND 128 THEN ' we have effect data
__Pattern(i, row, chan).effect = StringFile_ReadByte(memFile)
__Pattern(i, row, chan).operand = StringFile_ReadByte(memFile)
' Convert S3M effects to MOD effects
SELECT CASE __Pattern(i, row, chan).effect
CASE &H0 ' None
__Pattern(i, row, chan).operand = &H0 ' get rid of any false arpeggio if there is any param
CASE &H1 ' Axx Set Speed
__Pattern(i, row, chan).effect = __MOD_FX_SPEED
CASE &H2 ' Bxx Position Jumpp
__Pattern(i, row, chan).effect = __MOD_FX_POSITION_JUMP
CASE &H3 ' Cxx Pattern Break
__Pattern(i, row, chan).effect = __MOD_FX_PATTERN_BREAK
CASE &H4 ' Dxy Volume Slide or Fine Volume Slide
__Pattern(i, row, chan).effect = __MOD_FX_VOLUME_FINE_SLIDE
CASE &H5 ' Exx Portamento Down or Fine Portamento Down or Extra Fine Portamento Down
__Pattern(i, row, chan).effect = __MOD_FX_PORTAMENTO_EXTRA_FINE_DOWN
CASE &H6 ' Fxx Portamento Up or Fine Portamento Up or Extra Fine Portamento Up
__Pattern(i, row, chan).effect = __MOD_FX_PORTAMENTO_EXTRA_FINE_UP
CASE &H7 ' Gxx Tone Portamento
__Pattern(i, row, chan).effect = __MOD_FX_PORTAMENTO
CASE &H8 ' Hxy Vibrato
__Pattern(i, row, chan).effect = __MOD_FX_VIBRATO
CASE &H9 ' Ixy Tremor
__Pattern(i, row, chan).effect = __MOD_FX_TREMOR
CASE &HA ' Jxy Arpeggio
__Pattern(i, row, chan).effect = __MOD_FX_ARPEGGIO
CASE &HB ' Kxy Volume Slide + Vibrato
__Pattern(i, row, chan).effect = __MOD_FX_VIBRATO_VOLUME_FINE_SLIDE
CASE &HC ' Lxy Volume Slide + Tone Portamento
__Pattern(i, row, chan).effect = __MOD_FX_PORTAMENTO_VOLUME_FINE_SLIDE
CASE &HD ' Mxx Set Channel Volume
__Pattern(i, row, chan).effect = __MOD_FX_CHANNEL_VOLUME
CASE &HE ' Nxy Channel Volume Slide
__Pattern(i, row, chan).effect = __MOD_FX_CHANNEL_VOLUME_SLIDE
CASE &HF ' Oxx Sample Offset
__Pattern(i, row, chan).effect = __MOD_FX_SAMPLE_OFFSET
CASE &H10 ' Pxy Panning Slide or Fine Panning Slide
__Pattern(i, row, chan).effect = __MOD_FX_PANNING_FINE_SLIDE
CASE &H11 ' Qxy Retrigger + Volume Slide
__Pattern(i, row, chan).effect = __MOD_FX_NOTE_RETRIGGER_VOLUME_SLIDE
CASE &H12 ' Rxy Tremolo
__Pattern(i, row, chan).effect = __MOD_FX_TREMOLO
CASE &H13 ' Sxy Special commands
SELECT CASE _SHR(__Pattern(i, row, chan).operand, 4)
CASE &H1 ' S1x Glissando Control
__Pattern(i, row, chan).effect = __MOD_FX_EXTENDED
__Pattern(i, row, chan).operand = _SHL(__MOD_FX_EXTENDED_GLISSANDO_CONTROL, 4) OR (__Pattern(i, row, chan).operand AND &HF)
CASE &H2 ' S2x Set Finetune
__Pattern(i, row, chan).effect = __MOD_FX_EXTENDED
__Pattern(i, row, chan).operand = _SHL(__MOD_FX_EXTENDED_FINETUNE, 4) OR (__Pattern(i, row, chan).operand AND &HF)
CASE &H3 ' S3x Set Vibrato Waveform
__Pattern(i, row, chan).effect = __MOD_FX_EXTENDED
__Pattern(i, row, chan).operand = _SHL(__MOD_FX_EXTENDED_VIBRATO_WAVEFORM, 4) OR (__Pattern(i, row, chan).operand AND &HF)
CASE &H4 ' S4x Set Tremolo Waveform
__Pattern(i, row, chan).effect = __MOD_FX_EXTENDED
__Pattern(i, row, chan).operand = _SHL(__MOD_FX_EXTENDED_TREMOLO_WAVEFORM, 4) OR (__Pattern(i, row, chan).operand AND &HF)
CASE &H5 ' S5x Set Panbrello Waveform
__Pattern(i, row, chan).effect = __MOD_FX_PANBRELLO_WAVEFORM
__Pattern(i, row, chan).operand = __Pattern(i, row, chan).operand AND &HF
CASE &H6 ' S6x Fine Pattern Delay
__Pattern(i, row, chan).effect = __MOD_FX_PATTERN_FINE_DELAY
__Pattern(i, row, chan).operand = __Pattern(i, row, chan).operand AND &HF
CASE &H8 ' S8x Set Panning
__Pattern(i, row, chan).effect = __MOD_FX_EXTENDED
__Pattern(i, row, chan).operand = _SHL(__MOD_FX_EXTENDED_PANNING_4, 4) OR (__Pattern(i, row, chan).operand AND &HF)
CASE &H9 ' S9x Sound Control
__Pattern(i, row, chan).effect = __MOD_FX_SOUND_CONTROL
__Pattern(i, row, chan).operand = __Pattern(i, row, chan).operand AND &HF
CASE &HA ' SAx High Offset
__Pattern(i, row, chan).effect = __MOD_FX_HIGH_OFFSET
__Pattern(i, row, chan).operand = __Pattern(i, row, chan).operand AND &HF
CASE &HB ' SB0 Pattern Loop Start
__Pattern(i, row, chan).effect = __MOD_FX_EXTENDED
__Pattern(i, row, chan).operand = _SHL(__MOD_FX_EXTENDED_PATTERN_LOOP, 4) OR (__Pattern(i, row, chan).operand AND &HF) ' SBx
CASE &HC ' SCx Note Cut
__Pattern(i, row, chan).effect = __MOD_FX_EXTENDED
__Pattern(i, row, chan).operand = _SHL(__MOD_FX_EXTENDED_NOTE_CUT, 4) OR (__Pattern(i, row, chan).operand AND &HF) ' SCx
CASE &HD ' SDx Note Delay
__Pattern(i, row, chan).effect = __MOD_FX_EXTENDED
__Pattern(i, row, chan).operand = _SHL(__MOD_FX_EXTENDED_NOTE_DELAY, 4) OR (__Pattern(i, row, chan).operand AND &HF) ' SDx
CASE &HE ' SEx Pattern Delay
__Pattern(i, row, chan).effect = __MOD_FX_EXTENDED
__Pattern(i, row, chan).operand = _SHL(__MOD_FX_EXTENDED_PATTERN_DELAY, 4) OR (__Pattern(i, row, chan).operand AND &HF) ' SEx
'CASE ELSE
' ' Trap for unhandled stuff
' _ECHO "Uhandled special effect:" + STR$(__Pattern(i, row, chan).operand) + " |" + STR$(_SHR(__Pattern(i, row, chan).operand, 4))
END SELECT
CASE &H14 ' Txx Tempo
__Pattern(i, row, chan).effect = __MOD_FX_TEMPO
CASE &H15 ' Uxy Fine Vibrato
__Pattern(i, row, chan).effect = __MOD_FX_VIBRATO_FINE
CASE &H16 ' Vxx Set Global Volume
__Pattern(i, row, chan).effect = __MOD_FX_GLOBAL_VOLUME
CASE &H17 ' Wxy Global Volume Slide
__Pattern(i, row, chan).effect = __MOD_FX_GLOBAL_VOLUME_SLIDE
CASE &H18 ' Xxx Set Panning
IF __Pattern(i, row, chan).operand <= 128 THEN
' Ranges from 0 (left) to 128 (right)
__Pattern(i, row, chan).effect = __MOD_FX_PANNING_8
__Pattern(i, row, chan).operand = (__Pattern(i, row, chan).operand * 255&) \ 128&
ELSE
' If it is anything else, then we'll simply ignore the effect
__Pattern(i, row, chan).effect = &H0
__Pattern(i, row, chan).operand = &H0
END IF
CASE &H19 ' Yxy Panbrello
__Pattern(i, row, chan).effect = __MOD_FX_PANBRELLO
CASE &H20 ' Zxx MIDI Macro
__Pattern(i, row, chan).effect = __MOD_FX_MIDI_MACRO
'CASE ELSE
' ' Trap for unhandled stuff
' _ECHO "Uhandled effect:" + STR$(__Pattern(i, row, chan).effect)
END SELECT
END IF
END IF
ELSE ' end of row; move to the next one
row = row + 1
END IF
WEND
NEXT i
' Read the PCM instrument sample data
FOR i = 0 TO __Song.instruments - 1
IF __Instrument(i).subtype > __INSTRUMENT_PCM THEN
' TODO: Can this happen? Probably a corrupt file?
ERROR _ERR_FEATURE_UNAVAILABLE
ELSE
' Seek to the correct position in the file to read the PCM instrument sample data
StringFile_Seek memFile, instrumentPointer(i)
DIM AS STRING sampBuf, tempBuf
' Load the data
IF isSampleCompressed(i) THEN
' Decode ADPCM samples before sending it to the SoftSynth!
DIM compressionTable AS STRING * 16: compressionTable = StringFile_ReadString(memFile, LEN(compressionTable))
tempBuf = StringFile_ReadString(memFile, _SHR(__Instrument(i).length + 1, 1))
sampBuf = STRING$(LEN(tempBuf) * 2, NULL)
AudioConv_ConvertADPCM4ToS8 _OFFSET(tempBuf), LEN(tempBuf), compressionTable, _OFFSET(sampBuf)
'_ECHO "Decompressed to S8:" + STR$(i)
ELSE
sampBuf = StringFile_ReadString(memFile, __Instrument(i).length)
' Convert unsigned samples to signed if unsigned flag was set
' This is not needed if the sample is ADPCM4 compressed
IF isUnsignedFormat THEN
IF __Instrument(i).bytesPerSample = _SIZE_OF_INTEGER THEN
' Apparently 16-bit audio data is also unsigned when the unsigned flag is set! Fuck!
AudioConv_ConvertU16ToS16 _OFFSET(sampBuf), LEN(sampBuf) \ _SIZE_OF_INTEGER
'_ECHO "Converted to S16:" + STR$(i)
ELSE
' Else we'll assume these are 8-bit samples
AudioConv_ConvertU8ToS8 _OFFSET(sampBuf), LEN(sampBuf)
'_ECHO "Converted to S8:" + STR$(i)
END IF
END IF
' Stereo sample data is not interleaved! This should be fixed before sending it to the SoftSynth!
' Again, this is not needed if the sample is ADPCM4 compressed
IF __Instrument(i).channels = 2 THEN
tempBuf = STRING$(LEN(sampBuf), NULL)
IF __Instrument(i).bytesPerSample = _SIZE_OF_INTEGER THEN
AudioConv_ConvertDualMonoToStereoS16 _OFFSET(sampBuf), LEN(sampBuf) \ _SIZE_OF_INTEGER, _OFFSET(tempBuf)
'_ECHO "Fixed S16 stereo data:" + STR$(i)
ELSE
AudioConv_ConvertDualMonoToStereoS8 _OFFSET(sampBuf), LEN(sampBuf), _OFFSET(tempBuf)
'_ECHO "Fixed S8 stereo data:" + STR$(i)
END IF
sampBuf = tempBuf
END IF
END IF
' Load sample size bytes of data and send it to our softsynth sample manager
' Sounds with zero frames will not cause any issues
SoftSynth_LoadSound i, sampBuf, __Instrument(i).bytesPerSample, __Instrument(i).channels
END IF
NEXT i
' Load all needed LUTs
__MODPlayer_LoadTables
' What a fucked up format!
__MODPlayer_LoadS3M = _TRUE
END FUNCTION
' Loads an MTM file into memory and prepairs all required globals
FUNCTION __MODPlayer_LoadMTM%% (buffer AS STRING)
SHARED __Song AS __SongType
SHARED __Order() AS _UNSIGNED INTEGER
SHARED __Pattern() AS __NoteType
SHARED __Instrument() AS __InstrumentType
SHARED __Channel() AS __ChannelType
' Initialize the softsynth sample mixer
IF NOT SoftSynth_Initialize THEN EXIT FUNCTION
__MODPlayer_InitializeSong ' just in case something is playing
' Open the buffer as a StringFile
DIM memFile AS StringFileType
StringFile_Create memFile, buffer
' Read the file signature
__Song.subtype = StringFile_ReadString(memFile, 4) ' read 4 bytes. Last byte is the version
' Check if this is really an MTM file
IF LEFT$(__Song.subtype, 3) <> "MTM" THEN EXIT FUNCTION
' Change the FourCC so that it is completely printable
MID$(__Song.subtype, 4, 1) = HEX$(ASC(__Song.subtype, 4) - 15)
' Read the MTM song title (20 bytes)
__Song.caption = String_MakePrintable(StringFile_ReadString(memFile, 20)) ' MTM song name is 20 bytes
' Read the number of tracks saved
DIM numTracks AS _UNSIGNED INTEGER: numTracks = StringFile_ReadInteger(memFile)
' Read the highest pattern number saved
__Song.patterns = StringFile_ReadByte(memFile) + 1 ' convert to count / length
' Read the last order to play
__Song.orders = StringFile_ReadByte(memFile) + 1 ' convert to count / length
' Read length of the extra comment field
DIM commentLen AS _UNSIGNED INTEGER: commentLen = StringFile_ReadInteger(memFile)
' Read the number of instruments
__Song.instruments = StringFile_ReadByte(memFile)
' Read the attribute byte and discard it
DIM byte1 AS _UNSIGNED _BYTE: byte1 = StringFile_ReadByte(memFile)
' Read the beats per track (row count)
__Song.rows = StringFile_ReadByte(memFile)
' Read the number of channels
__Song.channels = StringFile_ReadByte(memFile)
' Sanity check
IF numTracks = 0 OR __Song.instruments = 0 OR __Song.rows = 0 OR __Song.channels = 0 THEN EXIT FUNCTION
' Resize the channel array
REDIM __Channel(0 TO __Song.channels - 1) AS __ChannelType
' Allocate the number of softsynth voices we need
SoftSynth_SetTotalVoices __Song.channels
' Read the panning positions
DIM i AS LONG: FOR i = 0 TO __MTM_S3M_CHANNEL_MAX
byte1 = StringFile_ReadByte(memFile) ' read the raw value
' Adjust and save the values per our mixer requirements
IF i < __Song.channels AND byte1 < 16 THEN
' __CHANNEL_PCM = 0 so all MTM channels are by default PCM
SoftSynth_SetVoiceBalance i, (byte1 / 15!) * 2! - SOFTSYNTH_VOICE_PAN_RIGHT ' pan = (x / 15) * 2 - 1
END IF
NEXT i
' Resize the instruments array
REDIM __Instrument(0 TO __Song.instruments - 1) AS __InstrumentType
' Read the instruments information
FOR i = 0 TO __Song.instruments - 1
__Instrument(i).subtype = __INSTRUMENT_PCM ' this format only uses PCM instruments
' Read the sample name
__Instrument(i).caption = String_MakePrintable(StringFile_ReadString(memFile, 22)) ' MTM sample names are 22 bytes long
' Read sample length
__Instrument(i).length = StringFile_ReadLong(memFile)
' Read loop start
__Instrument(i).loopStart = StringFile_ReadLong(memFile)
IF __Instrument(i).loopStart < 0 OR __Instrument(i).loopStart >= __Instrument(i).length THEN __Instrument(i).loopStart = 0 ' sanity check
' Read loop end
__Instrument(i).loopEnd = StringFile_ReadLong(memFile)
IF __Instrument(i).loopEnd < 0 OR __Instrument(i).loopEnd >= __Instrument(i).length THEN
__Instrument(i).loopEnd = __Instrument(i).length ' sanity check
END IF
' Set sound to looping and fix loop points if needed
IF __Instrument(i).loopEnd - __Instrument(i).loopStart > 1 THEN ' we need 2 frames minimum to mark the sound as looping (TODO: check if this is normal)
__Instrument(i).playMode = SOFTSYNTH_VOICE_PLAY_FORWARD_LOOP
ELSE
__Instrument(i).playMode = SOFTSYNTH_VOICE_PLAY_FORWARD
__Instrument(i).loopStart = 0
__Instrument(i).loopEnd = __Instrument(i).length
END IF
' Read finetune
__Instrument(i).c2Spd = __MODPlayer_GetC2Spd(StringFile_ReadByte(memFile)) ' convert finetune to c2spd
' Read volume
__Instrument(i).volume = StringFile_ReadByte(memFile)
IF __Instrument(i).volume > __INSTRUMENT_VOLUME_MAX THEN __Instrument(i).volume = __INSTRUMENT_VOLUME_MAX ' MTM uses MOD volume specs.
' Read attribute
byte1 = StringFile_ReadByte(memFile)
__Instrument(i).bytesPerSample = _SIZE_OF_BYTE + _SIZE_OF_BYTE * (byte1 AND &H1) ' 1 if 8-bit else 2 if 16-bit
__Instrument(i).channels = 1 ' all MTM sounds are mono
NEXT i
' Resize the order array (MTMs like MODs always have a 128 byte long order table)
REDIM __Order(0 TO __MOD_MTM_ORDER_MAX) AS _UNSIGNED INTEGER
' Read order list
FOR i = 0 TO __MOD_MTM_ORDER_MAX ' MTMs like MODs always have a 128 byte long order table
__Order(i) = StringFile_ReadByte(memFile)
NEXT
' Read and convert track data
DIM mtmTrack(0 TO numTracks - 1, 0 TO __Song.rows - 1) AS __NoteType, j AS LONG
DIM AS _UNSIGNED _BYTE byte2, byte3
FOR i = 0 TO numTracks - 1
FOR j = 0 TO __Song.rows - 1
' Read 3 bytes
byte1 = StringFile_ReadByte(memFile)
byte2 = StringFile_ReadByte(memFile)
byte3 = StringFile_ReadByte(memFile)
' +----------+----------+----------+
' | BYTE 0 | BYTE 1 | BYTE 2 |
' | ppppppii | iiiieeee | aaaaaaaa |
' +----------+----------+----------+
' p = pitch value (0 = no pitch stated)
' i = instrument number (0 = no instrument number)
' e = effect number
' a = effect argument
mtmTrack(i, j).note = _SHR(byte1, 2)
IF mtmTrack(i, j).note = 0 THEN
mtmTrack(i, j).note = __NOTE_NONE
ELSE
mtmTrack(i, j).note = mtmTrack(i, j).note + 24
END IF
mtmTrack(i, j).instrument = _SHL(byte1 AND &H3, 4) OR _SHR(byte2, 4)
IF mtmTrack(i, j).instrument > __Song.instruments THEN mtmTrack(i, j).instrument = 0 ' sanity check
mtmTrack(i, j).effect = byte2 AND &HF
mtmTrack(i, j).operand = byte3
mtmTrack(i, j).volume = __NOTE_NO_VOLUME
' MTM fix: when the effect is volume-slide, slide-up always overrides slide-down
IF mtmTrack(i, j).effect = &HA AND (mtmTrack(i, j).operand AND &HF0) <> 0 THEN
mtmTrack(i, j).operand = mtmTrack(i, j).operand AND &HF0
END IF
NEXT j
NEXT i
' Resize the pattern data array
REDIM __Pattern(0 TO __Song.patterns - 1, 0 TO __Song.rows - 1, 0 TO __Song.channels - 1) AS __NoteType
' Read track sequencing data and assemble that to our pattern data
DIM k AS LONG, w AS _UNSIGNED INTEGER
FOR i = 0 TO __Song.patterns - 1
FOR j = 0 TO __MTM_S3M_CHANNEL_MAX ' MTM files stores data for 32 channels irrespective of the actual channels used
' Read the data
w = StringFile_ReadInteger(memFile)
IF j >= __Song.channels THEN _CONTINUE ' ignore excess channel information
IF w THEN
FOR k = 0 TO __Song.rows - 1
__Pattern(i, k, j) = mtmTrack(w - 1, k)
NEXT k
ELSE
' Populate empty channel
FOR k = 0 TO __Song.rows - 1
__Pattern(i, k, j).note = __NOTE_NONE
__Pattern(i, k, j).instrument = 0
__Pattern(i, k, j).effect = 0
__Pattern(i, k, j).operand = 0
__Pattern(i, k, j).volume = __NOTE_NO_VOLUME
NEXT k
END IF
NEXT j
NEXT i
' Read the tune comment
__Song.comment = StringFile_ReadString(memFile, commentLen) ' read the comment and leave it untouched
' Load the instruments
FOR i = 0 TO __Song.instruments - 1
DIM sampBuf AS STRING: sampBuf = StringFile_ReadString(memFile, __Instrument(i).length)
' Convert 8-bit unsigned samples to 8-bit signed
IF __Instrument(i).bytesPerSample = _SIZE_OF_BYTE THEN AudioConv_ConvertU8ToS8 _OFFSET(sampBuf), LEN(sampBuf)
' Load sample size bytes of data and send it to our softsynth sample manager
SoftSynth_LoadSound i, sampBuf, __Instrument(i).bytesPerSample, __Instrument(i).channels
NEXT i
' Load all needed LUTs
__MODPlayer_LoadTables
__MODPlayer_LoadMTM = _TRUE
END FUNCTION
' Loads the MOD file into memory and prepares all required globals
FUNCTION __MODPlayer_LoadMOD%% (buffer AS STRING)
SHARED __Song AS __SongType