| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356 |
- #!/usr/bin/env python3
- """Generate Rigforge's reference guitar pedalboard bundle for mod-ui."""
- import argparse
- import hashlib
- import json
- import os
- import shutil
- import sys
- import tempfile
- from pathlib import Path, PurePosixPath
- import yaml
- from validate_desired_state import safe_relative_path, validate_state
- ROOT = Path(__file__).resolve().parents[1]
- def turtle_string(value):
- return json.dumps(str(value), ensure_ascii=False)
- def resolved_asset(repo_root, relative):
- if not safe_relative_path(relative):
- raise ValueError(f"unsafe pedalboard asset path: {relative!r}")
- path = (repo_root / Path(*PurePosixPath(relative).parts)).resolve()
- if not path.is_relative_to(repo_root.resolve()) or not path.is_file():
- raise FileNotFoundError(f"pedalboard asset is missing or outside repository: {path}")
- return path
- def generate_bundle_files(state, repo_root=ROOT, asset_root=None):
- errors = validate_state(state)
- if errors:
- raise ValueError("invalid desired state: " + "; ".join(errors))
- repo_root = Path(repo_root).resolve()
- asset_root = Path(asset_root or repo_root / "artifacts" / "mod-ui" / "arm64" / "current" / "app" / "default.pedalboard")
- routing = state["routing"]["mod_host_chains"]["guitar"]
- nam, ir = routing["processors"]
- nam_path = resolved_asset(repo_root, nam["asset_path"])
- ir_path = resolved_asset(repo_root, ir["asset_path"])
- output_level = nam["parameters"]["output_level"]
- nam_instance = nam["mod_ui_instance"].removeprefix("/graph/")
- ir_instance = ir["mod_ui_instance"].removeprefix("/graph/")
- nam_model_uri = nam["file_property"]
- ir_file_uri = ir["file_property"]
- ttl = f"""\
- @prefix atom: <http://lv2plug.in/ns/ext/atom#> .
- @prefix doap: <http://usefulinc.com/ns/doap#> .
- @prefix ingen: <http://drobilla.net/ns/ingen#> .
- @prefix lv2: <http://lv2plug.in/ns/lv2core#> .
- @prefix midi: <http://lv2plug.in/ns/ext/midi#> .
- @prefix mod: <http://moddevices.com/ns/mod#> .
- @prefix patch: <http://lv2plug.in/ns/ext/patch#> .
- @prefix pedal: <http://moddevices.com/ns/modpedal#> .
- @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
- @prefix state: <http://lv2plug.in/ns/ext/state#> .
- @prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
- <capture_1>
- lv2:index 5 ;
- lv2:name "Rigforge Mod-Host Input" ;
- lv2:portProperty lv2:connectionOptional ;
- lv2:symbol "capture_1" ;
- a lv2:AudioPort, lv2:InputPort .
- <playback_1>
- lv2:index 6 ;
- lv2:name "Rigforge Mod-Host Output" ;
- lv2:portProperty lv2:connectionOptional ;
- lv2:symbol "playback_1" ;
- a lv2:AudioPort, lv2:OutputPort .
- <:bpb>
- ingen:value 4.0 ;
- lv2:index 0 ;
- a lv2:ControlPort, lv2:InputPort .
- <:bpm>
- ingen:value 120.0 ;
- lv2:index 1 ;
- a lv2:ControlPort, lv2:InputPort .
- <:rolling>
- ingen:value 0 ;
- lv2:index 2 ;
- a lv2:ControlPort, lv2:InputPort .
- <control_in>
- atom:bufferType atom:Sequence ;
- atom:supports midi:MidiEvent ;
- lv2:index 3 ;
- lv2:name "Control In" ;
- lv2:portProperty lv2:connectionOptional ;
- lv2:symbol "control_in" ;
- <http://lv2plug.in/ns/ext/resize-port#minimumSize> 4096 ;
- a atom:AtomPort, lv2:InputPort .
- <control_out>
- atom:bufferType atom:Sequence ;
- atom:supports midi:MidiEvent ;
- lv2:index 4 ;
- lv2:name "Control Out" ;
- lv2:portProperty lv2:connectionOptional ;
- lv2:symbol "control_out" ;
- <http://lv2plug.in/ns/ext/resize-port#minimumSize> 4096 ;
- a atom:AtomPort, lv2:OutputPort .
- <midi_separated_mode>
- ingen:value 0 ;
- lv2:index 7 ;
- a atom:AtomPort, lv2:InputPort .
- <midi_loopback>
- ingen:value 0 ;
- lv2:index 8 ;
- a atom:AtomPort, lv2:InputPort .
- <{nam_instance}>
- ingen:canvasX 160.0 ;
- ingen:canvasY 280.0 ;
- ingen:enabled true ;
- ingen:polyphonic false ;
- lv2:microVersion 0 ;
- lv2:minorVersion 0 ;
- mod:builderVersion 0 ;
- mod:releaseNumber 0 ;
- pedal:instanceLabel "NAM" ;
- pedal:instanceNumber {nam["instance"]} ;
- pedal:performanceVisible true ;
- pedal:performanceIndex 0 ;
- lv2:prototype <{nam["plugin_uri"]}> ;
- lv2:port <{nam_instance}/control>,
- <{nam_instance}/notify>,
- <{nam_instance}/input>,
- <{nam_instance}/output>,
- <{nam_instance}/input_level>,
- <{nam_instance}/output_level>,
- <{nam_instance}/quality_scale>,
- <{nam_instance}/:bypass> ;
- state:state [ <{nam_model_uri}> {turtle_string(str(nam_path))}^^atom:Path ] ;
- a ingen:Block .
- <{nam_instance}/control>
- atom:bufferType atom:Sequence ;
- atom:supports patch:Message ;
- lv2:designation lv2:control ;
- lv2:index 0 ;
- lv2:symbol "control" ;
- a atom:AtomPort, lv2:InputPort .
- <{nam_instance}/notify>
- atom:bufferType atom:Sequence ;
- atom:supports patch:Message ;
- lv2:designation lv2:control ;
- lv2:index 1 ;
- lv2:symbol "notify" ;
- a atom:AtomPort, lv2:OutputPort .
- <{nam_instance}/input>
- a lv2:AudioPort, lv2:InputPort .
- <{nam_instance}/output>
- a lv2:AudioPort, lv2:OutputPort .
- <{nam_instance}/input_level>
- ingen:value 0.0 ;
- a lv2:ControlPort, lv2:InputPort .
- <{nam_instance}/output_level>
- ingen:value {output_level} ;
- a lv2:ControlPort, lv2:InputPort .
- <{nam_instance}/quality_scale>
- ingen:value 1.0 ;
- a lv2:ControlPort, lv2:InputPort .
- <{nam_instance}/:bypass>
- ingen:value 0 ;
- a lv2:ControlPort, lv2:InputPort .
- <{ir_instance}>
- ingen:canvasX 620.0 ;
- ingen:canvasY 280.0 ;
- ingen:enabled true ;
- ingen:polyphonic false ;
- lv2:microVersion 0 ;
- lv2:minorVersion 0 ;
- mod:builderVersion 0 ;
- mod:releaseNumber 0 ;
- pedal:instanceLabel "Cabinet IR" ;
- pedal:instanceNumber {ir["instance"]} ;
- pedal:performanceVisible true ;
- pedal:performanceIndex 1 ;
- lv2:prototype <{ir["plugin_uri"]}> ;
- lv2:port <{ir_instance}/control>,
- <{ir_instance}/notify>,
- <{ir_instance}/in>,
- <{ir_instance}/out>,
- <{ir_instance}/:bypass> ;
- state:state [ <{ir_file_uri}> {turtle_string(str(ir_path))}^^atom:Path ] ;
- a ingen:Block .
- <{ir_instance}/control>
- atom:bufferType atom:Sequence ;
- atom:supports patch:Message ;
- lv2:designation lv2:control ;
- a atom:AtomPort, lv2:InputPort .
- <{ir_instance}/notify>
- atom:bufferType atom:Sequence ;
- atom:supports patch:Message ;
- lv2:designation lv2:control ;
- a atom:AtomPort, lv2:OutputPort .
- <{ir_instance}/in>
- a lv2:AudioPort, lv2:InputPort .
- <{ir_instance}/out>
- a lv2:AudioPort, lv2:OutputPort .
- <{ir_instance}/:bypass>
- ingen:value 0 ;
- a lv2:ControlPort, lv2:InputPort .
- <>
- doap:name "Rigforge Guitar NAM and IR" ;
- pedal:unitName "Rigforge" ;
- pedal:unitModel "Generic" ;
- pedal:width 1200 ;
- pedal:height 700 ;
- pedal:addressings <addressings.json> ;
- pedal:screenshot <screenshot.png> ;
- pedal:thumbnail <thumbnail.png> ;
- pedal:formatVersion 2 ;
- pedal:version 1 ;
- ingen:polyphony 1 ;
- ingen:arc _:b1, _:b2, _:b3 ;
- ingen:block <{nam_instance}>, <{ir_instance}> ;
- lv2:port <:bpb>, <:bpm>, <:rolling>,
- <control_in>, <control_out>,
- <capture_1>, <playback_1>,
- <midi_separated_mode>, <midi_loopback> ;
- lv2:extensionData <http://lv2plug.in/ns/ext/state#interface> ;
- a lv2:Plugin, ingen:Graph, pedal:Pedalboard .
- _:b1 ingen:tail <capture_1> ;
- ingen:head <{nam_instance}/input> .
- _:b2 ingen:tail <{nam_instance}/output> ;
- ingen:head <{ir_instance}/in> .
- _:b3 ingen:tail <{ir_instance}/out> ;
- ingen:head <playback_1> .
- """
- files = {
- "manifest.ttl": (
- "@prefix ingen: <http://drobilla.net/ns/ingen#> .\n"
- "@prefix lv2: <http://lv2plug.in/ns/lv2core#> .\n"
- "@prefix pedal: <http://moddevices.com/ns/modpedal#> .\n"
- "@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n\n"
- "<rigforge-guitar.ttl>\n"
- " lv2:prototype ingen:GraphPrototype ;\n"
- " a lv2:Plugin, ingen:Graph, pedal:Pedalboard ;\n"
- " rdfs:seeAlso <rigforge-guitar.ttl> .\n"
- ).encode("utf-8"),
- "rigforge-guitar.ttl": ttl.encode("utf-8"),
- "addressings.json": b"{}\n",
- "snapshots.json": b'{"current":0,"snapshots":[]}\n',
- }
- for image_name in ("screenshot.png", "thumbnail.png"):
- image = asset_root / image_name
- if not image.is_file():
- raise FileNotFoundError(f"mod-ui default board image is missing: {image}")
- files[image_name] = image.read_bytes()
- return files
- def write_bundle(output, files, overwrite=False):
- output = Path(output)
- if output.exists():
- if output.is_symlink() or not output.is_dir():
- raise RuntimeError(f"pedalboard output is not a regular directory: {output}")
- existing = {}
- for path in output.rglob("*"):
- if path.is_symlink():
- raise RuntimeError(f"refusing symlink in pedalboard output: {path}")
- if path.is_file():
- existing[path.relative_to(output).as_posix()] = path.read_bytes()
- if existing == files:
- return
- if not overwrite:
- raise RuntimeError(f"pedalboard output already exists and differs: {output}")
- output.parent.mkdir(parents=True, exist_ok=True)
- temporary = Path(tempfile.mkdtemp(prefix=f".{output.name}.", dir=output.parent))
- try:
- for relative, content in files.items():
- path = temporary / relative
- path.parent.mkdir(parents=True, exist_ok=True)
- path.write_bytes(content)
- if output.exists():
- backup = output.with_name(output.name + ".rigforge-backup")
- if backup.exists():
- raise RuntimeError(f"pedalboard recovery path already exists: {backup}")
- os.replace(output, backup)
- try:
- os.replace(temporary, output)
- except OSError:
- os.replace(backup, output)
- raise
- else:
- os.replace(temporary, output)
- finally:
- if temporary.exists():
- shutil.rmtree(temporary)
- def main(argv=None):
- parser = argparse.ArgumentParser(description=__doc__)
- parser.add_argument("--state", type=Path, default=ROOT / "config" / "desired-state.yaml")
- parser.add_argument("--repo-root", type=Path, default=ROOT)
- parser.add_argument("--output", type=Path)
- parser.add_argument("--force", action="store_true", help="replace an existing differing generated bundle")
- args = parser.parse_args(argv)
- try:
- state = yaml.safe_load(args.state.read_text(encoding="utf-8"))
- files = generate_bundle_files(state, args.repo_root)
- output = args.output
- if output is None:
- digest = hashlib.sha256()
- for name, content in sorted(files.items()):
- digest.update(name.encode("utf-8") + b"\0" + content + b"\0")
- reference_directory = state["deployment"]["mod_ui"]["reference_directory"]
- output = (
- Path.home()
- / reference_directory
- / f"rigforge-guitar-{digest.hexdigest()[:16]}.pedalboard"
- )
- write_bundle(output, files, overwrite=args.force)
- except (OSError, ValueError, RuntimeError, KeyError, TypeError, yaml.YAMLError) as error:
- print(f"ERROR: {error}", file=sys.stderr)
- return 1
- print(f"Generated mod-ui pedalboard: {output}")
- return 0
- if __name__ == "__main__":
- raise SystemExit(main())
|