-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompiler.py
More file actions
352 lines (303 loc) · 11 KB
/
Copy pathcompiler.py
File metadata and controls
352 lines (303 loc) · 11 KB
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
# compiler.py
# PromptTracker v1.4 - "Clean Instrument Mapping"
# Fixes:
# - No placeholder "empty" instruments (only real instruments are written)
# - Proper XM order table + song length
# - Separate instrument name (nice) vs sample filename
# - More robust instrument header padding (exact 263 bytes when sample exists)
# - WAV validation: only PCM 16-bit (sampwidth=2)
import wave
import struct
import math
from pathlib import Path
# --- PATHS ---
ROOT = Path(__file__).resolve().parent
PATTERNS_DIR = ROOT / "patterns"
OUT_DIR = ROOT / "output"
SAMPLES_DIR = ROOT / "samples"
KITS_DIR = ROOT / "kits"
for d in [PATTERNS_DIR, OUT_DIR, SAMPLES_DIR, KITS_DIR]:
d.mkdir(parents=True, exist_ok=True)
XM_BASE_FREQ = 8363.0
NOTE_NAMES = {"C": 0, "C#": 1, "D": 2, "D#": 3, "E": 4, "F": 5, "F#": 6, "G": 7, "G#": 8, "A": 9, "A#": 10, "B": 11}
DEFAULT_MAP = {
1: "kick.wav",
2: "snare.wav",
3: "hihat.wav",
4: "bass.wav",
5: "lead.wav",
6: "pad.wav",
}
# --- NAME HELPERS ---
def _xm_name_20(s: str, pad_byte: bytes = b"\x00") -> bytes:
b = s.encode("ascii", "ignore")[:20]
return b.ljust(20, pad_byte)
def _xm_name_22(s: str, pad_byte: bytes = b"\x00") -> bytes:
b = s.encode("ascii", "ignore")[:22]
return b.ljust(22, pad_byte)
def _nice_inst_name_from_filename(fn: str) -> str:
# "BD_Techno_909.wav" -> "BD_TECHNO_909"
stem = Path(fn).stem
stem = stem.strip().replace(" ", "_").upper()
return stem[:22] if stem else "INST"
# --- WAV ---
def read_wav(filename: str):
path = SAMPLES_DIR / filename.strip()
if not path.exists():
print(f"[WARN] WAV not found: {filename}")
return [], 44100
try:
with wave.open(str(path), "rb") as wf:
nch = wf.getnchannels()
sw = wf.getsampwidth()
fr = wf.getframerate()
nframes = wf.getnframes()
if sw != 2:
# Only PCM 16-bit supported in this compiler
print(f"[WARN] WAV not 16-bit PCM (sampwidth={sw}): {filename}")
return [], fr if fr else 44100
raw = wf.readframes(nframes)
samples = list(struct.unpack(f"<{len(raw)//2}h", raw))
if nch == 2:
samples = samples[0::2] # simple mono downmix (left channel)
return samples, fr
except Exception as e:
print(f"[WARN] WAV read failed: {filename} -> {e}")
return [], 44100
# --- MAP PARSER ---
def parse_map_file(map_filename: str | None):
if not map_filename:
return DEFAULT_MAP
path = KITS_DIR / map_filename
if not path.exists():
print(f"[WARN] Kit not found: {map_filename}. Using DEFAULT_MAP.")
return DEFAULT_MAP
mapping = {}
try:
for line in path.read_text(encoding="utf-8").splitlines():
line = line.strip()
if not line or line.startswith("#"):
continue
if "=" in line:
k, v = line.split("=", 1)
k = k.strip()
v = v.strip()
if k.isdigit():
mapping[int(k)] = v
return mapping if mapping else DEFAULT_MAP
except Exception as e:
print(f"[WARN] Kit read failed: {map_filename} -> {e}")
return DEFAULT_MAP
# --- PT PARSER ---
def parse_pt(text: str):
bpm, speed, rows, kit = 125, 6, 32, None
lines = text.splitlines()
# Header
for line in lines:
line = line.strip()
if not line or line.startswith("#"):
continue
if ":" in line and "|" not in line:
k, v = [x.strip() for x in line.split(":", 1)]
ku = k.upper()
if ku == "BPM":
bpm = int(v)
elif ku == "SPEED":
speed = int(v)
elif ku == "ROWS":
rows = int(v)
elif ku == "KIT":
kit = v
inst_map = parse_map_file(kit)
# Grid
grid = {}
max_ch = 4
for line in lines:
if "|" in line and any(c.isdigit() for c in line[:3]):
parts = [p.strip() for p in line.split("|")]
try:
r = int(parts[0])
cells = parts[1:]
max_ch = max(max_ch, len(cells))
grid[r] = cells
except:
pass
# Matrix
matrix = [[(0, 0, 0, 0, 0) for _ in range(max_ch)] for _ in range(rows)]
for r, cells in grid.items():
if r >= rows:
continue
for ch, txt in enumerate(cells):
if ch >= max_ch:
break
toks = txt.split()
if len(toks) < 3:
continue
try:
inst = int(toks[0]) if toks[0].isdigit() else 0
note = 0
if toks[1] != "---":
n0 = toks[1][0].upper()
semi = NOTE_NAMES.get(n0 + ("#" if "#" in toks[1] else ""), 0)
octv = int(toks[1][-1])
note = 1 + (octv * 12 + semi)
vol = 0x10 + int(toks[2], 16) if toks[2] != "--" else 0
# v1.1 rule: only store events that have a note
if note > 0:
matrix[r][ch] = (note, inst, vol, 0, 0)
except:
pass
return matrix, bpm, speed, inst_map, max_ch
# --- XM INSTRUMENT WRITER (ROBUST PADDING) ---
def _write_instrument(xm_buf: bytearray, inst_name: str, sample_name: str, samples: list[int], rate: int):
slen = len(samples) * 2 # bytes
has_sample = 1 if slen > 0 else 0
if not has_sample:
# Minimal instrument header (29 bytes)
h_size = 29
hdr = bytearray()
hdr += struct.pack("<I", h_size)
hdr += _xm_name_22(inst_name)
hdr += b"\x00"
hdr += struct.pack("<H", 0)
hdr += bytes(h_size - len(hdr))
xm_buf += hdr
return
# Full instrument header (263 bytes)
h_size = 263
hdr = bytearray()
hdr += struct.pack("<I", h_size)
hdr += _xm_name_22(inst_name)
hdr += b"\x00"
hdr += struct.pack("<H", 1) # number of samples
hdr += struct.pack("<I", 40) # sample header size
hdr += bytes([0] * 96) # sample map (all -> sample 0)
hdr += bytes([0] * 48) # vol env points
hdr += bytes([0] * 48) # pan env points
hdr += bytes([0] * 14) # env/vibrato fields (all zero)
hdr += struct.pack("<H", 0) # fadeout
hdr += struct.pack("<H", 0) # reserved
if len(hdr) < h_size:
hdr += bytes(h_size - len(hdr))
else:
hdr = hdr[:h_size]
xm_buf += hdr
# Pitch math (basic)
semi = 12.0 * math.log(rate / XM_BASE_FREQ, 2) if rate > 0 else 0.0
rel = int(round(semi))
fine = int(round((semi - rel) * 128))
fine = max(-128, min(127, fine))
# Sample header (40 bytes)
xm_buf += struct.pack("<I", slen) # length
xm_buf += struct.pack("<I", 0) # loop start
xm_buf += struct.pack("<I", 0) # loop length
xm_buf += struct.pack("<B", 64) # volume
xm_buf += struct.pack("<b", fine) # finetune
xm_buf += struct.pack("<B", 16) # type (bit4=16-bit)
xm_buf += struct.pack("<B", 128) # panning
xm_buf += struct.pack("<b", rel) # rel note
xm_buf += struct.pack("<B", 0) # reserved
xm_buf += _xm_name_22(sample_name) # sample name (22)
# Delta-encode sample data (16-bit)
prev = 0
for s in samples:
d = s - prev
d = ((d + 32768) & 0xFFFF) - 32768
xm_buf += struct.pack("<h", d)
prev = s
# --- XM BUILDER ---
def build_xm(pt_content: str) -> bytes:
grid, bpm, speed, inst_map, channels = parse_pt(pt_content)
# 1) Build real instruments only (no placeholders)
# ID -> (instrument index in XM, 1-based)
idx_map = {}
instruments = []
for inst_id in sorted(inst_map.keys()):
wav_name = inst_map[inst_id]
samples, rate = read_wav(wav_name)
# If wav is missing or invalid, skip instrument (keeps XM clean)
if not samples:
print(f"[WARN] Skipping instrument {inst_id} (no samples): {wav_name}")
continue
inst_name = _nice_inst_name_from_filename(wav_name)
instruments.append({
"inst_id": inst_id,
"inst_name": inst_name,
"wav_name": wav_name,
"samples": samples,
"rate": rate,
})
idx_map[inst_id] = len(instruments) # 1-based index
# 2) XM header
xm = bytearray()
xm += b"Extended Module: " + _xm_name_20("PromptTracker v1.4", b"\x00")
xm += b"\x1A" + _xm_name_20("FastTracker v2.00", b"\x00")
xm += struct.pack("<H", 0x0104) # version
xm += struct.pack("<I", 276) # header size
song_length = 1
restart_pos = 0
num_patterns = 1
num_instruments = len(instruments)
flags = 1 # 1 = linear frequency table (common)
xm += struct.pack("<H", song_length)
xm += struct.pack("<H", restart_pos)
xm += struct.pack("<H", channels)
xm += struct.pack("<H", num_patterns)
xm += struct.pack("<H", num_instruments)
xm += struct.pack("<H", flags)
xm += struct.pack("<H", speed)
xm += struct.pack("<H", bpm)
# Order table (256 bytes): play pattern 0 then rest
order = bytearray([0]) + bytearray([0] * 255)
xm += order
# 3) Pattern data
pdata = bytearray()
for row in grid:
for n, inst_id, v, f, fp in row:
real_inst = idx_map.get(inst_id, 0) if inst_id > 0 else 0
mask = 0x80
pack = bytearray()
if n > 0:
mask |= 1
pack.append(n)
if real_inst > 0:
mask |= 2
pack.append(real_inst)
if v > 0:
mask |= 4
pack.append(v)
if n == 0 and real_inst == 0 and v == 0:
pdata += b"\x80"
else:
pdata += bytes([mask]) + pack
# Pattern header (length 9, packing type 0)
xm += struct.pack("<I", 9) # pattern header length
xm += struct.pack("<B", 0) # packing type
xm += struct.pack("<H", len(grid)) # number of rows
xm += struct.pack("<H", len(pdata)) # packed data size
xm += pdata
# 4) Instruments + samples
for it in instruments:
_write_instrument(
xm_buf=xm,
inst_name=it["inst_name"],
sample_name=it["wav_name"],
samples=it["samples"],
rate=it["rate"],
)
return bytes(xm)
# --- MAIN ---
if __name__ == "__main__":
pts = list(PATTERNS_DIR.glob("*.pt"))
if not pts:
print("[ERROR] No .pt files in /patterns")
raise SystemExit(1)
for p in pts:
print(f"Compiling: {p.name} ...")
try:
data = build_xm(p.read_text(encoding="utf-8"))
out_path = OUT_DIR / (p.stem + ".xm")
out_path.write_bytes(data)
print(f" OK -> output/{out_path.name}")
except Exception as e:
print(f" ERROR -> {e}")