#!/usr/bin/env python3 # SPDX-License-Identifier: GPL-3.0-only # Copyright (C) 2026 Leo Kuroshita (@kurogedelic), Hügelton Instruments # Copyright (C) 2026 Juan Ignacio Antonucci (Juan Jiant) """(0.9.7, flash) GEN's drum maps packed: firmware/src/gen_tables.c keeps them as text (DRUM_MAP: a genre's eight lanes, the throws' pools HAT_POOL OH_POOL RS_POOL CB_POOL TM_POOL: 16 characters a lane, '.' 0, '1'..'9', 'X'; "" no lane), read here, not compiled. Out: each distinct lane once, 16 levels a nibble (GM_LANE[k]: step 2i in the high nibble of byte i, '.' 0, '1'..'9' 1..9, 'X' 10; lane 0 the empty one), and the tables as lane numbers (DRUM_MAP_I[DM_..][8], HAT_POOL_I ..). gen.c reads them through gm_level (the same levels as the text gave: glevel). Also DICE's bars (firmware/src/song_dice.c DRUM_BAR, DRUM_BAR_KICK: '.' 'x' a hit, 'a' accented, 'g' a ghost): each distinct lane once at 2 bits a step (DB_LANE: step 4i in the top bits of byte i, '.' 0 'x' 1 'a' 2 'g' 3), the table as lane numbers (DRUM_BAR_I; song_dice.c db_char), in their own header (song_dice.c comes before gen_tables.c). And GEN's bass archetypes (gen_tables.c BASS_ARCH: a note and a flag a step as text, NOTES and FLAGS below): a byte a step, the note's code | the flag's << 4 (BA_STEP), each archetype its first step and bars (gen.c ba_note, ba_flag). gen_drum_maps.py GEN_TABLES.c SONG_DICE.c OUT.h DICE_OUT.h BASS_OUT.h""" import re import sys from pathlib import Path LEVEL = {".": 0, "X": 10, **{str(d): d for d in range(1, 10)}} NOTES = ".=R23456789ALNSx" # (gen.c BA_NOTES: the same order) FLAGS = ".asg" def main(src, dice, out, dout, bout): s = Path(src).read_text() m = re.search(r"static const char \*const DRUM_MAP\[\]\[8\] = \{(.*?)\n\};", s, re.S) rows = re.findall(r"\[(DM_\w+)\]\s*=\s*\{([^}]*)\}", m.group(1)) pools = re.findall(r"static const char \*const (\w+_POOL)\[(\d+)\] = \{(.*?)\};", s, re.S) lanes, idx = [""], {"": 0} def lane(x): if x not in idx: assert len(x) == 16 and set(x) <= set(LEVEL), f"gen_drum_maps: lane {x!r}" assert set(x) != {"."}, "an all-rest lane would read as none" idx[x] = len(lanes) lanes.append(x) return idx[x] table = [] for name, body in rows: ls = re.findall(r'"([^"]*)"', body) assert len(ls) == 8, f"gen_drum_maps: {name} has {len(ls)} lanes" table.append((name, [lane(x) for x in ls])) ptab = [] for name, n, body in pools: ls = re.findall(r'"([^"]*)"', body) assert len(ls) == int(n), f"gen_drum_maps: {name}" ptab.append((name, [lane(x) for x in ls])) assert len(lanes) <= 256 L = ["/* generated by tools/gen_drum_maps.py from firmware/src/gen_tables.c (DRUM_MAP and the pools, as text there): */", "/* each lane once, 16 levels a nibble ('.' 0, '1'..'9', 'X' 10; lane 0: none); the tables as lane numbers */", "#pragma once", "#include ", f"static const uint8_t GM_LANE[{len(lanes)}][8] = {{"] for x in lanes: v = [LEVEL[c] for c in x] if x else [0] * 16 L.append(" {" + ", ".join(f"0x{v[2 * i] << 4 | v[2 * i + 1]:02X}" for i in range(8)) + "}," + (f" /* {x} */" if x else " /* (none) */")) L.append("};") L.append("static const uint8_t DRUM_MAP_I[][8] = {") for name, v in table: L.append(f" [{name}] = {{{', '.join(map(str, v))}}},") L.append("};") for name, v in ptab: L.append(f"static const uint8_t {name}_I[{len(v)}] = {{{', '.join(map(str, v))}}};") size = 8 * len(lanes) + 8 * len(table) + sum(len(v) for _, v in ptab) # DICE's bars (0.9.17: gone with song_dice.c's own generator; the header stays, empty, for an older source) d = Path(dice).read_text() m = re.search(r"static const char \*const DRUM_BAR\[\]\[8\] = \{(.*?)\n\};", d, re.S) Path(out).write_text("\n".join(L) + "\n") bl, btab = [""], [] if m: brows = re.findall(r"\[(DS_\w+)\]\s*=\s*\{([^}]*)\}", m.group(1)) kick = re.findall(r'"([^"]*)"', re.search(r"static const char \*const DRUM_BAR_KICK\[1\] = \{(.*?)\};", d, re.S).group(1)) bi = {"": 0} code = {".": 0, "x": 1, "a": 2, "g": 3} def blane(x): if x not in bi: assert len(x) == 16 and set(x) <= set(code), f"gen_drum_maps: bar lane {x!r}" assert set(x) != {"."}, "an all-rest lane would read as none" bi[x] = len(bl) bl.append(x) return bi[x] for name, body in brows: ls = re.findall(r'"([^"]*)"', body) assert len(ls) == 8, f"gen_drum_maps: {name} has {len(ls)} lanes" btab.append((name, [blane(x) for x in ls])) kk = [blane(x) for x in kick] L = ["/* generated by tools/gen_drum_maps.py from firmware/src/song_dice.c (DRUM_BAR, DRUM_BAR_KICK, as text there): */", "/* each lane once, 2 bits a step ('.' 0 'x' 1 'a' 2 'g' 3; lane 0: none); the table as lane numbers */", "#pragma once", "#include "] if m: L.append(f"static const uint8_t DB_LANE[{len(bl)}][4] = {{") for x in bl: v = [code[c] for c in x] if x else [0] * 16 L.append(" {" + ", ".join(f"0x{v[4 * i] << 6 | v[4 * i + 1] << 4 | v[4 * i + 2] << 2 | v[4 * i + 3]:02X}" for i in range(4)) + "}," + (f" /* {x} */" if x else " /* (none) */")) L.append("};") L.append("static const uint8_t DRUM_BAR_I[][8] = {") for name, v in btab: L.append(f" [{name}] = {{{', '.join(map(str, v))}}},") L.append("};") L.append(f"#define DRUM_BAR_KICK_I {kk[0]}u") Path(dout).write_text("\n".join(L) + "\n") size2 = 4 * len(bl) + 8 * len(btab) m = re.search(r"static const barch_t BASS_ARCH\[BA_N\] = \{(.*?)\n\};", s, re.S) arch = re.findall(r"\[(BA_\w+)\]\s*=\s*\{(\"[^\"]*\"|0),\s*(\"[^\"]*\"|0),\s*(\d+)\}", m.group(1)) steps, rows3 = [], [] for name, n, f, bars in arch: bars = int(bars) if n == "0": rows3.append((name, 0, bars)) continue n, f = n.strip('"'), f.strip('"') if f != "0" else "." * (16 * bars) assert len(n) == len(f) == 16 * bars and set(n) <= set(NOTES) and set(f) <= set(FLAGS), f"gen_drum_maps: {name}" rows3.append((name, len(steps), bars)) steps += [NOTES.index(a) | FLAGS.index(b) << 4 for a, b in zip(n, f)] assert len(steps) < 65536 L = ["/* generated by tools/gen_drum_maps.py from firmware/src/gen_tables.c (BASS_ARCH, as text there): a byte a step, */", f"/* the note's code in \"{NOTES}\" | the flag's in \"{FLAGS}\" << 4; each archetype its first step, its bars */", "#pragma once", "#include ", f"static const uint8_t BA_STEP[{len(steps)}] = {{"] for i in range(0, len(steps), 24): L.append(" " + ", ".join(f"0x{v:02X}" for v in steps[i:i + 24]) + ",") L += ["};", "static const barch_t BASS_ARCH[BA_N] = {"] for name, at, bars in rows3: L.append(f" [{name}] = {{{at}, {bars}}},") L.append("};") Path(bout).write_text("\n".join(L) + "\n") size3 = len(steps) + 4 * len(rows3) print(f"drum maps: {len(table)} maps, {len(ptab)} pools, {len(lanes) - 1} distinct lanes, {size} B; DICE's bars: " f"{len(btab)}, {len(bl) - 1} distinct lanes, {size2} B; bass archetypes {len(rows3)}, {size3} B -> {out}, {dout}, {bout}") return 0 if __name__ == "__main__": sys.exit(main(*sys.argv[1:6]))