#!/usr/bin/env python3 """Adds one element kind to src/core/AlsSkeleton.h's Live 12 stream (round 190ja), leaving every other kind byte for byte as tools/als_skeleton.py wrote it (that tool needs the local sets of rounds 175a / 183c / 190ec to run again). The kind comes from a device preset Live saved (.adv: the device element right under ), stripped by als_skeleton.py's strip(): member names and default values in Live's order, lists emptied, user values cleared. usage: tools/als_skeleton_add.py [--header src/core/AlsSkeleton.h] round 190ja: InstrumentVector from Live 12.4's Core Library "Defaults/Instruments/Wavetable.adv" """ import argparse, gzip, os, re, sys, zlib, xml.etree.ElementTree as ET sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) from als_skeleton import strip, text def main(): p = argparse.ArgumentParser() p.add_argument('kind') p.add_argument('preset') p.add_argument('--header', default='src/core/AlsSkeleton.h') a = p.parse_args() raw = open(a.preset, 'rb').read() if raw[:2] == b'\x1f\x8b': raw = gzip.decompress(raw) device = list(ET.fromstring(raw))[0] if device.tag != a.kind: raise SystemExit(f'the preset holds {device.tag}, not {a.kind}') src = open(a.header).read() m = re.search(r'inline constexpr std::size_t live12Size = (\d+);\ninline constexpr std::uint8_t live12\[(\d+)\] = \{\n(.*?)\n\};', src, re.S) if not m: raise SystemExit('no live12 stream in the header') data = bytes(int(x, 16) for x in re.findall(r'0x([0-9a-f]{2})', m.group(3))) body = zlib.decompress(data).decode('utf-8') assert len(body.encode('utf-8')) == int(m.group(1)) kind = f'' if kind in body: start = body.index(kind) end = body.index('', start) + len('') body = body[:start] + body[end:] add = kind + re.sub(r'>\s+<', '><', text(strip(device))) + '' body = body.replace('', add + '') z = zlib.compress(body.encode('utf-8'), 9) rows = '\n'.join(' ' + ', '.join(f'0x{b:02x}' for b in z[i:i + 24]) + ',' for i in range(0, len(z), 24)) new = (f'inline constexpr std::size_t live12Size = {len(body.encode("utf-8"))};\n' f'inline constexpr std::uint8_t live12[{len(z)}] = {{\n{rows}\n}};') src = src[:m.start()] + new + src[m.end():] if '(rounds 175a, 183c, 190ec)' in src: src = src.replace('// Generated by tools/als_skeleton.py (rounds 175a, 183c, 190ec)', '// Generated by tools/als_skeleton.py (rounds 175a, 183c, 190ec; InstrumentVector added by tools/als_skeleton_add.py, 190ja)') open(a.header, 'w').write(src) print(f'{a.kind}: added; Live 12 stream {len(body)} bytes of XML, {len(z)} compressed') if __name__ == '__main__': main()