als_skeleton_add.py 2.8 KB

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