-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule.py
1119 lines (873 loc) · 37.1 KB
/
module.py
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
import os
import struct
from typing import Dict, Tuple, List, Optional
import numpy as np
TRACKER_NAME = "Stage Magician"
C_NOTE = 0x19
class ModuleGenerator:
def __init__(
self,
title: str,
pattern_data: np.ndarray,
sample_data: np.ndarray,
amplitude_data: np.ndarray,
speed: int = 16,
samples_per_instrument: int = 16,
loop_samples: bool = False,
max_rows: int = 256,
instrument_names: Optional[List[str]] = None
):
self.title = title
self.pattern_data = pattern_data
self.sample_data = sample_data
self.amplitude_data = amplitude_data
self.samples = self.calculate_number_of_samples()
self.samples_per_instrument = int(np.clip(samples_per_instrument, 1, self.samples))
self.speed = speed
self.channels = self.calculate_number_of_channels()
self.rows = self.calculate_number_of_rows(max_rows)
self.patterns = self.calculate_number_of_patterns()
self.instruments = self.calculate_number_of_instruments()
self.sample_length = self.sample_data.shape[-1]
self.bpm = self.calculate_bpm()
self.used_samples = self.get_used_samples()
self.instrument_names = instrument_names or []
self.instruments_map = self.generate_instruments_map()
self.loop_samples = loop_samples
self.sample_size = self.sample_data.shape[-1] + 2 * self.loop_samples
def save(self, path: os.PathLike):
content = self.generate()
with open(path, "wb") as f:
f.write(content)
@classmethod
def get_module_generator_class(cls, module_format: str):
if module_format == "xm":
return XMModuleGenerator
if module_format == "it":
return ITModuleGenerator
raise ValueError(f"Unsupported module format: {module_format}")
def calculate_number_of_samples(self) -> int:
return self.sample_data.shape[0]
def calculate_bpm(self) -> int:
raise NotImplementedError
def calculate_number_of_channels(self) -> int:
return self.pattern_data.shape[0]
def calculate_number_of_rows(self, max_rows: int) -> int:
raise NotImplementedError
def calculate_number_of_patterns(self) -> int:
length = self.pattern_data.shape[1]
return int(np.ceil(length / self.rows))
def calculate_number_of_instruments(self) -> int:
instruments = self.samples / self.samples_per_instrument
return int(np.ceil(instruments))
def get_used_samples(self) -> List[int]:
return sorted(np.unique(self.pattern_data[:, :, 1]) - 1)
def generate_instruments_map(self) -> Dict[int, Tuple[int, int]]:
instrument = 1
note = C_NOTE
i = 0
instruments_map = {}
for sample in range(1, self.samples + 1):
instruments_map[sample] = (instrument, note)
note += 1
i += 1
if i == self.samples_per_instrument:
i = 0
instrument += 1
note = C_NOTE
return instruments_map
def pad(self, string: str, length: int, pad_value: int = 0x00) -> bytes:
string = string[:length].encode()
return string + bytes([pad_value] * (length - len(string)))
def get_title(self) -> bytes:
raise NotImplementedError
def get_tracker_name(self) -> bytes:
raise NotImplementedError
def generate(self) -> bytes:
raise NotImplementedError
class XMModuleGenerator(ModuleGenerator):
HEADER = "Extended Module: "
BASE_NOTE = 0x35
def __init__(
self,
title: str,
pattern_data: np.ndarray,
sample_data: np.ndarray,
amplitude_data: np.ndarray,
speed: int = 16,
samples_per_instrument: int = 16,
loop_samples: bool = False,
max_rows: int = 256,
instrument_names: Optional[List[str]] = None
):
super().__init__(
title,
pattern_data,
sample_data,
amplitude_data,
speed,
samples_per_instrument,
loop_samples,
max_rows,
instrument_names
)
def calculate_number_of_samples(self) -> int:
return self.sample_data.shape[0]
def calculate_number_of_channels(self) -> int:
return self.pattern_data.shape[0]
def calculate_bpm(self) -> int:
bpm = round(110250 / self.sample_length)
return int(np.clip(bpm, 32, 511))
def calculate_number_of_rows(self, max_rows: int) -> int:
rows = int(65535 / (self.channels * 5))
return max(1, min(rows, max_rows))
def calculate_number_of_patterns(self) -> int:
length = self.pattern_data.shape[1]
return int(np.ceil(length / self.rows))
def calculate_number_of_instruments(self) -> int:
instruments = self.samples / self.samples_per_instrument
return int(np.ceil(instruments))
def pad(self, string: str, length: int, pad_value: int = 0x00) -> bytes:
string = string[:length].encode()
return string + bytes([pad_value] * (length - len(string)))
def get_header(self) -> bytes:
return self.HEADER.encode()
def get_title(self) -> bytes:
return self.pad(self.title, 20)
def get_stripped(self) -> bytes:
return struct.pack("B", 0x1A)
def get_tracker_name(self) -> bytes:
return self.pad(TRACKER_NAME, 20)
def get_version(self) -> bytes:
return struct.pack("BB", 0x04, 0x01)
def get_header_size(self) -> bytes:
return struct.pack("<4B", 0x14, 0x01, 0x00, 0x00)
def get_song_length(self) -> bytes:
return struct.pack("<H", self.patterns)
def get_restart_position(self) -> bytes:
return struct.pack("<H", 0)
def get_number_of_channels(self) -> bytes:
return struct.pack("<H", self.channels)
def get_number_of_patterns(self) -> bytes:
return struct.pack("<H", self.patterns)
def get_number_of_instruments(self) -> bytes:
return struct.pack("<H", self.instruments)
def get_flags(self) -> bytes:
return struct.pack("<H", 1)
def get_speed(self) -> bytes:
return struct.pack("<H", self.speed)
def get_bpm(self) -> bytes:
return struct.pack("<H", self.bpm)
def get_order_table(self) -> bytes:
order_table_size = 256
table = []
for i in range(order_table_size):
if i < self.patterns:
table.append(i)
else:
table.append(0)
return bytes(table)
def generate_header(self) -> bytes:
header = [
self.get_header(),
self.get_title(),
self.get_stripped(),
self.get_tracker_name(),
self.get_version(),
self.get_header_size(),
self.get_song_length(),
self.get_restart_position(),
self.get_number_of_channels(),
self.get_number_of_patterns(),
self.get_number_of_instruments(),
self.get_flags(),
self.get_speed(),
self.get_bpm(),
self.get_order_table()
]
return b"".join(header)
def get_pattern_header(self) -> bytes:
return struct.pack("<4B", 0x09, 0x00, 0x00, 0x00)
def get_packing_type(self) -> bytes:
return struct.pack("B", 0x00)
def get_rows(self) -> bytes:
return struct.pack("<H", self.rows)
def get_pattern_size(self, size: int) -> bytes:
return struct.pack("<H", size)
def get_pattern_data(self, pattern: int) -> Tuple[List[bytes], int]:
size = 0
offset = pattern * self.rows
lines = []
for line in range(self.rows):
for channel in range(self.channels):
x = offset + line
note = C_NOTE
instrument = -1
delay = -1
volume = -1
if x < self.pattern_data[channel].shape[0]:
volume, sample, delay = self.pattern_data[channel][x]
if sample > 0:
instrument, note = self.instruments_map[sample]
if delay == -1 or instrument == -1:
lines.append(struct.pack("B", 0x80))
size += 1
elif delay == 0:
lines.append(struct.pack("<4B", 0x87, note, instrument, volume + 16))
size += 4
else:
lines.append(struct.pack("<5B", note, instrument, volume + 16, 0x0E, 0xD0 + delay))
size += 5
return lines, size
def generate_pattern(self, pattern: int) -> bytes:
lines, size = self.get_pattern_data(pattern)
return b"".join([
self.get_pattern_header(),
self.get_packing_type(),
self.get_rows(),
self.get_pattern_size(size),
*lines
])
def generate_patterns(self) -> bytes:
return b"".join([
self.generate_pattern(pattern)
for pattern in range(self.patterns)
])
def get_instrument_size(self) -> bytes:
return struct.pack("<4B", 0x07, 0x01, 0x00, 0x00)
def get_instrument_name(self, instrument: int) -> bytes:
instrument_name = self.instrument_names[instrument] if instrument < len(self.instrument_names) else ""
return self.pad(instrument_name, 22)
def get_instrument_type(self) -> bytes:
return struct.pack("B", 0x00)
def get_number_of_samples(self, instrument: int) -> bytes:
if instrument * self.samples_per_instrument < self.samples:
samples = self.samples_per_instrument
else:
samples = self.samples - instrument * self.samples_per_instrument
return struct.pack("<H", samples)
def get_sample_header_size(self) -> bytes:
return struct.pack("<4B", 0x28, 0x00, 0x00, 0x00)
def get_keymap_assignment(self) -> bytes:
assignment = []
for i in range(96):
value = i - C_NOTE
if value < self.samples_per_instrument:
assignment.append(max(value + 1, 0))
else:
assignment.append(0)
return bytes(assignment)
def get_volume_envelope(self, volume: int = 0x40) -> bytes:
return struct.pack(
"<12B36B",
0x00, 0x00, volume, 0x00,
0x01, 0x00, volume, 0x00,
0x02, 0x00, 0x00, 0x00,
*(0x00, 0x00, 0x00, 0x00) * 9
)
def get_panning_envelope(self) -> bytes:
return struct.pack("<48B", *(0x00, 0x00, 0x00, 0x00) * 12)
def get_number_of_volume_envelope_points(self) -> bytes:
return struct.pack("B", 0x02)
def get_number_of_panning_envelope_points(self) -> bytes:
return struct.pack("B", 0x00)
def get_volume_sustain_point(self) -> bytes:
return struct.pack("B", 0x02)
def get_volume_envelope_loop(self) -> bytes:
return struct.pack("<H", 0)
def get_panning_sustain_point(self) -> bytes:
return struct.pack("B", 0x00)
def get_panning_envelope_loop(self) -> bytes:
return struct.pack("<H", 0)
def get_volume_type(self) -> bytes:
return struct.pack("B", 0x01)
def get_panning_type(self) -> bytes:
return struct.pack("B", 0x00)
def get_vibrato(self) -> bytes:
return struct.pack("<4B", 0x00, 0x00, 0x00, 0x00)
def get_volume_fadeout(self) -> bytes:
return struct.pack("<2B", 0x00, 0x04)
def get_reserved(self) -> bytes:
return struct.pack("<22B", *(0x00,) * 22)
def get_sample_length(self, sample: int) -> bytes:
sample_size = self.sample_size * 2 if sample in self.used_samples else 0
return struct.pack("<I", sample_size)
def get_sample_loop_start(self) -> bytes:
loop_start = self.sample_length * 2 if self.loop_samples else 0
return struct.pack("<I", loop_start)
def get_sample_loop_end(self) -> bytes:
loop_end = self.sample_size * 2 if self.loop_samples else 0
return struct.pack("<I", loop_end)
def get_sample_volume(self) -> bytes:
return struct.pack("B", 0x40)
def get_sample_finetune(self) -> bytes:
return struct.pack("B", 0xE0)
def get_sample_type(self) -> bytes:
return struct.pack("B", 0b0010000)
def get_sample_panning(self) -> bytes:
return struct.pack("B", 0x7C)
def get_sample_relative_note_number(self, sample) -> bytes:
value = (self.BASE_NOTE - sample % self.samples_per_instrument) % 256
return struct.pack("B", value)
def get_sample_compression(self) -> bytes:
return struct.pack("B", 0x00)
def get_sample_name(self, sample: int) -> bytes:
return self.pad("", 22)
def get_sample_data(self, sample: int) -> bytes:
if sample not in self.used_samples:
return b""
sample_data = self.sample_data[sample] * 16383.5
if self.loop_samples:
constant_value = sample_data[-1]
sample_data = np.pad(sample_data, (0, 2), mode="constant", constant_values=constant_value)
delta_array = np.diff(sample_data, prepend=sample_data[0]).astype(np.int16)
return delta_array.tobytes()
def get_sample_header(self, sample: int) -> bytes:
return b"".join([
self.get_sample_length(sample),
self.get_sample_loop_start(),
self.get_sample_loop_end(),
self.get_sample_volume(),
self.get_sample_finetune(),
self.get_sample_type(),
self.get_sample_panning(),
self.get_sample_relative_note_number(sample),
self.get_sample_compression(),
self.get_sample_name(sample)
])
def get_instrument(self, instrument: int) -> bytes:
volume = round(self.amplitude_data[instrument])
instrument_data = [
self.get_instrument_size(),
self.get_instrument_name(instrument),
self.get_instrument_type(),
self.get_number_of_samples(instrument),
self.get_sample_header_size(),
self.get_keymap_assignment(),
self.get_volume_envelope(volume),
self.get_panning_envelope(),
self.get_number_of_volume_envelope_points(),
self.get_number_of_panning_envelope_points(),
self.get_volume_sustain_point(),
self.get_volume_envelope_loop(),
self.get_panning_sustain_point(),
self.get_panning_envelope_loop(),
self.get_volume_type(),
self.get_panning_type(),
self.get_vibrato(),
self.get_volume_fadeout(),
self.get_reserved()
]
sample_headers = []
sample_data = []
for sample in range(self.samples_per_instrument):
offset = instrument * self.samples_per_instrument
if offset + sample >= self.samples:
break
sample_headers.append(self.get_sample_header(offset + sample))
sample_data.append(self.get_sample_data(offset + sample))
return b"".join(instrument_data + sample_headers + sample_data)
def generate_instruments(self) -> bytes:
return b"".join([
self.get_instrument(instrument)
for instrument in range(self.instruments)]
)
def generate(self) -> bytes:
header = self.generate_header()
patterns = self.generate_patterns()
instruments = self.generate_instruments()
return header + patterns + instruments
class ITModuleGenerator(ModuleGenerator):
HEADER = "IMPM"
HEADER_INSTRUMENT = "IMPI"
HEADER_SAMPLE = "IMPS"
HEADER_EXTENSION = "XTPM"
HEADER_OPENMPT = "OMPT"
STRUCT_HEADER_SIZE = 192
STRUCT_INSTRUMENT_SIZE = 554
STRUCT_INSTRUMENT_EXTENSION = 120
STRUCT_SAMPLE_SIZE = 80
STRUCT_PATTERN_SIZE = 8
RELATIVE_NOTE = 0x0B
BASE_NOTE = 0x23
BASE_FREQUENCY = 176400
def __init__(
self,
title: str,
pattern_data: np.ndarray,
sample_data: np.ndarray,
amplitude_data: np.ndarray,
speed: int = 16,
samples_per_instrument: int = 16,
loop_samples: bool = False,
max_rows: int = 256,
instrument_names: Optional[List[str]] = None
):
super().__init__(
title,
pattern_data,
sample_data,
amplitude_data,
speed,
samples_per_instrument,
loop_samples,
max_rows,
instrument_names
)
def calculate_number_of_rows(self, max_rows: int) -> int:
return int(np.clip(max_rows, 32, 200))
def calculate_bpm(self) -> int:
bpm = round(110250 / self.sample_length)
return int(np.clip(bpm, 32, 255))
def calculate_header_size(self) -> int:
return self.STRUCT_HEADER_SIZE + self.patterns * 5 + self.instruments * 4 + self.samples * 4
def calculate_instruments_offset(self) -> int:
return self.calculate_header_size()
def calculate_samples_offset(self) -> int:
extended_instruments = max(0, self.instruments + 1 - 256 // self.samples_per_instrument)
base_offset = self.calculate_instruments_offset() + self.instruments * self.STRUCT_INSTRUMENT_SIZE
offset = base_offset + extended_instruments * self.STRUCT_INSTRUMENT_EXTENSION
return offset
def calculate_patterns_offset(self) -> int:
return self.calculate_samples_offset() + self.samples * self.STRUCT_SAMPLE_SIZE
def calculate_sample_data_offset(self, pattern_sizes: List[int]) -> int:
return self.calculate_patterns_offset() + sum(pattern_sizes)
def get_header(self) -> bytes:
return self.HEADER.encode()
def get_title(self) -> bytes:
return self.pad(self.title, 26)
def get_pattern_highlight(self) -> bytes:
return struct.pack("<BB", 0x04, 0x10)
def get_number_of_patterns(self) -> bytes:
return struct.pack("<H", self.patterns)
def get_number_of_instruments(self) -> bytes:
return struct.pack("<H", self.instruments)
def get_number_of_samples(self) -> bytes:
return struct.pack("<H", self.samples)
def get_tracker_version(self) -> bytes:
return struct.pack("<I", 0x02145131)
def get_flags(self) -> bytes:
return struct.pack("<I", 0x0006004D)
def get_global_volume(self) -> bytes:
return struct.pack("B", 0x80)
def get_mix_volume(self) -> bytes:
return struct.pack("B", 0x30)
def get_speed(self) -> bytes:
return struct.pack("B", self.speed)
def get_bpm(self) -> bytes:
return struct.pack("B", self.bpm)
def get_pan_separation(self) -> bytes:
return struct.pack("B", 0)
def get_pitch_wheel_depth(self) -> bytes:
return struct.pack("B", 0)
def get_message_length(self) -> bytes:
return struct.pack("<H", 0x00)
def get_message_offset(self) -> bytes:
return struct.pack("<I", 0x0000)
def get_reserved(self) -> bytes:
return self.HEADER_OPENMPT.encode()
def get_initial_channel_pan(self) -> bytes:
return struct.pack("<64B", *(0x20,) * 64)
def get_initial_channel_volume(self) -> bytes:
return struct.pack("<64B", *(0x40,) * 64)
def get_order_table(self) -> bytes:
return bytes(range(self.patterns))
def get_instrument_offsets(self) -> bytes:
base_offset = self.calculate_instruments_offset()
total_offset = base_offset
offsets = []
for instrument in range(self.instruments):
offsets.append(total_offset)
total_offset += self.STRUCT_INSTRUMENT_SIZE
extension = ((instrument + 1) * self.samples_per_instrument) >= 256
total_offset += extension * self.STRUCT_INSTRUMENT_EXTENSION
return struct.pack(f"<{self.instruments}I", *offsets)
def get_sample_header_offsets(self) -> bytes:
base_offset = self.calculate_samples_offset()
offsets = [
base_offset + sample * self.STRUCT_SAMPLE_SIZE
for sample in range(self.samples)
]
return struct.pack(f"<{self.samples}I", *offsets)
def get_pattern_offsets(self, pattern_sizes: List[int]) -> bytes:
base_offset = self.calculate_patterns_offset()
offsets = []
total_size = 0
for pattern in range(self.patterns):
offset = base_offset + total_size
offsets.append(offset)
total_size += pattern_sizes[pattern]
return struct.pack(f"<{self.patterns}I", *offsets)
def generate_header(self, pattern_sizes: List[int]) -> bytes:
header = [
self.get_header(),
self.get_title(),
self.get_pattern_highlight(),
self.get_number_of_patterns(),
self.get_number_of_instruments(),
self.get_number_of_samples(),
self.get_number_of_patterns(),
self.get_tracker_version(),
self.get_flags(),
self.get_global_volume(),
self.get_mix_volume(),
self.get_speed(),
self.get_bpm(),
self.get_pan_separation(),
self.get_pitch_wheel_depth(),
self.get_message_length(),
self.get_message_offset(),
self.get_reserved(),
self.get_initial_channel_pan(),
self.get_initial_channel_volume(),
self.get_order_table(),
self.get_instrument_offsets(),
self.get_sample_header_offsets(),
self.get_pattern_offsets(pattern_sizes)
]
return b"".join(header)
def get_instrument_header(self) -> bytes:
return self.HEADER_INSTRUMENT.encode()
def get_instrument_filename(self) -> bytes:
return self.pad("", 12)
def get_instrument_reserved(self) -> bytes:
return struct.pack("B", 0)
def get_instrument_new_note_action(self) -> bytes:
return struct.pack("B", self.loop_samples * 3)
def get_instrument_duplicate_check_type(self) -> bytes:
return struct.pack("B", 0x00)
def get_instrument_duplicate_check_action(self) -> bytes:
return struct.pack("B", 0x02)
def get_instrument_fadeout(self) -> bytes:
return struct.pack("<H", 0x80)
def get_instrument_pitch_pan_separation(self) -> bytes:
return struct.pack("B", 0x00)
def get_instrument_pitch_pan_center(self) -> bytes:
return struct.pack("B", 0x3C)
def get_instrument_global_volume(self, instrument: int) -> bytes:
volume = round(2 * self.amplitude_data[instrument])
return struct.pack("B", volume)
def get_instrument_default_pan(self) -> bytes:
return struct.pack("B", 0xA0)
def get_instrument_random_volume_variation(self) -> bytes:
return struct.pack("B", 0x00)
def get_instrument_random_pan_variation(self) -> bytes:
return struct.pack("B", 0x00)
def get_instrument_tracker_version(self) -> bytes:
return struct.pack("<H", 0x5131)
def get_instrument_number_of_samples(self) -> bytes:
return struct.pack("B", self.samples_per_instrument)
def get_instrument_name(self, instrument: int) -> bytes:
instrument_name = self.instrument_names[instrument] if instrument < len(self.instrument_names) else ""
return self.pad(instrument_name, 26)
def get_instrument_initial_cutoff(self) -> bytes:
return struct.pack("B", 0x00)
def get_instrument_initial_resonance(self) -> bytes:
return struct.pack("B", 0x00)
def get_instrument_midi_channel(self) -> bytes:
return struct.pack("B", 0x00)
def get_instrument_midi_program(self) -> bytes:
return struct.pack("B", 0xFF)
def get_instrument_midi_bank(self) -> bytes:
return struct.pack("<H", 0xFFFF)
def get_relative_note(self, note: int) -> int:
return min(max(1, note - self.BASE_NOTE), self.samples_per_instrument)
def get_instrument_note_sample_keyboard_table(self, instrument: int) -> bytes:
table = []
sample_offset = instrument * self.samples_per_instrument
for note in range(120):
table.append(note)
offset = self.get_relative_note(note)
offset = (sample_offset + offset) % 256
table.append(offset)
return bytes(table)
def get_volume_envelope_flags(self) -> bytes:
return struct.pack("B", 0x01)
def get_volume_envelope_node_count(self) -> bytes:
return struct.pack("B", 0x03)
def get_volume_envelope_loop_start(self) -> bytes:
return struct.pack("B", 0x00)
def get_volume_envelope_loop_end(self) -> bytes:
return struct.pack("B", 0x01)
def get_volume_envelope_sustain_start(self) -> bytes:
return struct.pack("B", 0x01)
def get_volume_envelope_sustain_end(self) -> bytes:
return struct.pack("B", 0x01)
def get_volume_envelope_nodes(self) -> bytes:
return struct.pack(
"<75B",
0x40, 0x00, 0x00,
0x40, 0x01, 0x00,
*(0x00, 0x00, 0x00) * 23,
)
def get_envelope_trailing_byte(self) -> bytes:
return struct.pack("<1B", 0x00)
def get_instrument_extension(self, instrument: int) -> bytes:
base_sample = instrument * self.samples_per_instrument
extension = []
for note in range(120):
note = self.get_relative_note(note)
sample = base_sample + note
page = sample // 256
extension.append(page)
return struct.pack(f"<{self.STRUCT_INSTRUMENT_EXTENSION}B", *extension)
def get_envelope_trailing_bytes(self, instrument: int) -> bytes:
sample = (instrument + 1) * self.samples_per_instrument
if sample >= 256:
trailing_bytes = [
self.HEADER_EXTENSION.encode(),
self.get_instrument_extension(instrument),
]
return b"".join(trailing_bytes)
return struct.pack("<4B", *(0x00,) * 4)
def get_volume_envelope(self) -> bytes:
volume_envelope = [
self.get_volume_envelope_flags(),
self.get_volume_envelope_node_count(),
self.get_volume_envelope_loop_start(),
self.get_volume_envelope_loop_end(),
self.get_volume_envelope_sustain_start(),
self.get_volume_envelope_sustain_end(),
self.get_volume_envelope_nodes(),
self.get_envelope_trailing_byte()
]
return b"".join(volume_envelope)
def get_panning_and_pitch_envelope_flags(self) -> bytes:
return struct.pack("B", 0x00)
def get_panning_and_pitch_node_count(self) -> bytes:
return struct.pack("B", 0x02)
def get_panning_and_pitch_loop_start(self) -> bytes:
return struct.pack("B", 0x00)
def get_panning_and_pitch_loop_end(self) -> bytes:
return struct.pack("B", 0x01)
def get_panning_and_pitch_sustain_start(self) -> bytes:
return struct.pack("B", 0x01)
def get_panning_and_pitch_sustain_end(self) -> bytes:
return struct.pack("B", 0x01)
def get_panning_and_pitch_nodes(self) -> bytes:
return struct.pack(
"<75B",
0x00, 0x00, 0x00,
0x00, 0x02, 0x00,
*(0x00, 0x00, 0x00) * 23
)
def get_panning_and_pitch_envelope(self) -> bytes:
panning_and_pitch_envelope = [
self.get_panning_and_pitch_envelope_flags(),
self.get_panning_and_pitch_node_count(),
self.get_panning_and_pitch_loop_start(),
self.get_panning_and_pitch_loop_end(),
self.get_panning_and_pitch_sustain_start(),
self.get_panning_and_pitch_sustain_end(),
self.get_panning_and_pitch_nodes(),
self.get_envelope_trailing_byte()
]
return b"".join(panning_and_pitch_envelope)
def get_instrument_envelopes(self, instrument: int) -> bytes:
return b"".join([
self.get_volume_envelope(),
self.get_panning_and_pitch_envelope(),
self.get_panning_and_pitch_envelope(),
self.get_envelope_trailing_bytes(instrument)
])
def get_instrument(self, instrument: int) -> bytes:
instrument_data = [
self.get_instrument_header(),
self.get_instrument_filename(),
self.get_instrument_reserved(),
self.get_instrument_new_note_action(),
self.get_instrument_duplicate_check_type(),
self.get_instrument_duplicate_check_action(),
self.get_instrument_fadeout(),
self.get_instrument_pitch_pan_separation(),
self.get_instrument_pitch_pan_center(),
self.get_instrument_global_volume(instrument),
self.get_instrument_default_pan(),
self.get_instrument_random_volume_variation(),
self.get_instrument_random_pan_variation(),
self.get_instrument_tracker_version(),
self.get_instrument_number_of_samples(),
self.get_instrument_reserved(),
self.get_instrument_name(instrument),
self.get_instrument_initial_cutoff(),
self.get_instrument_initial_resonance(),
self.get_instrument_midi_channel(),
self.get_instrument_midi_program(),
self.get_instrument_midi_bank(),
self.get_instrument_note_sample_keyboard_table(instrument),
self.get_instrument_envelopes(instrument)
]
return b"".join(instrument_data)
def generate_instruments(self) -> bytes:
return b"".join([
self.get_instrument(instrument)
for instrument in range(self.instruments)]
)
def get_sample_header(self) -> bytes:
return self.HEADER_SAMPLE.encode()
def get_sample_filename(self) -> bytes:
return self.pad("", 12)
def get_sample_reserved(self) -> bytes:
return struct.pack("B", 0)
def get_sample_global_volume(self) -> bytes:
return struct.pack("B", 0x40)
def get_sample_flags(self) -> bytes:
flag = 0b00000011
if self.loop_samples:
flag |= 0b00010000
return struct.pack("B", flag)
def get_sample_default_volume(self) -> bytes:
return struct.pack("B", 0x40)
def get_sample_name(self, sample: int) -> bytes:
return self.pad("", 26)
def get_sample_data_flags(self) -> bytes:
flag = 0b00000001
return struct.pack("B", flag)
def get_sample_default_pan(self) -> bytes:
return struct.pack("B", 0x20)
def get_sample_length(self, sample: int) -> bytes:
sample_size = self.sample_size if sample in self.used_samples else 0
return struct.pack("<I", sample_size)
def get_sample_loop_start(self) -> bytes:
loop_start = self.sample_length if self.loop_samples else 0
return struct.pack("<I", loop_start)
def get_sample_loop_end(self) -> bytes:
loop_end = self.sample_size if self.loop_samples else 0
return struct.pack("<I", loop_end)
def get_sample_c5_speed(self, sample: int) -> bytes:
sample_index = sample % self.samples_per_instrument
frequency = round(self.BASE_FREQUENCY * np.power(2.0 ** (-sample_index), 1 / 12))
return struct.pack("<I", frequency)
def get_sample_sustain_start(self) -> bytes:
return struct.pack("<I", 0)
def get_sample_sustain_end(self) -> bytes:
return struct.pack("<I", 0)
def get_sample_pointer(self, sample: int, sample_data_offset: int) -> bytes:
used_samples = len([s for s in self.used_samples if 0 <= s < sample])
pointer = sample_data_offset + 2 * used_samples * self.sample_size
return struct.pack("<I", pointer)
def get_sample_vibrato_speed(self) -> bytes:
return struct.pack("B", 0)
def get_sample_vibrato_depth(self) -> bytes:
return struct.pack("B", 0)
def get_sample_vibrato_sweep(self) -> bytes:
return struct.pack("B", 0)
def get_sample_vibrato_waveform(self) -> bytes:
return struct.pack("B", 0)
def get_sample(self, sample: int, sample_data_offset: int) -> bytes:
sample_data = [
self.get_sample_header(),
self.get_sample_filename(),
self.get_sample_reserved(),
self.get_sample_global_volume(),
self.get_sample_flags(),
self.get_sample_default_volume(),
self.get_sample_name(sample),
self.get_sample_data_flags(),
self.get_sample_default_pan(),
self.get_sample_length(sample),
self.get_sample_loop_start(),
self.get_sample_loop_end(),
self.get_sample_c5_speed(sample),
self.get_sample_sustain_start(),
self.get_sample_sustain_end(),
self.get_sample_pointer(sample, sample_data_offset),
self.get_sample_vibrato_speed(),
self.get_sample_vibrato_depth(),
self.get_sample_vibrato_sweep(),
self.get_sample_vibrato_waveform()
]