| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611 |
- #!/usr/bin/env python3
- """Apply the small Rigforge integration patch to the pinned mod-ui source tree."""
- import sys
- from pathlib import Path
- def replace_once(path, original, replacement):
- text = path.read_text(encoding="utf-8")
- if replacement in text:
- return
- if text.count(original) != 1:
- raise RuntimeError(f"expected one upstream match in {path}")
- path.write_text(text.replace(original, replacement), encoding="utf-8")
- def replace_if_present(path, original, replacement):
- text = path.read_text(encoding="utf-8")
- if (replacement and replacement in text) or original not in text:
- return
- if text.count(original) != 1:
- raise RuntimeError(f"expected at most one upstream match in {path}")
- path.write_text(text.replace(original, replacement), encoding="utf-8")
- def apply(source_root):
- app = Path(source_root)
- helper = Path(__file__).with_name("mod_ui_pipewire.py")
- (app / "mod" / "rigforge_pipewire.py").write_bytes(helper.read_bytes())
- replace_if_present(
- app / "mod" / "host.py",
- "from mod import (\n",
- "from mod.rigforge_pipewire import boundary_connection, connect_link, disconnect_link, pedalboard_connection\n"
- "from mod import (\n",
- )
- replace_if_present(
- app / "mod" / "host.py",
- ''' self.jack_hw_capture_prefix = "mod-host:out" if self.descriptor.get('has_noisegate', False) else "system:capture_"
- ''',
- ''' self.jack_hw_capture_prefix = "mod-host:out" if self.descriptor.get('has_noisegate', False) else "system:capture_"
- self.rigforge_capture_ports = {
- "capture_%d" % (index + 1): port
- for index, port in enumerate(
- os.environ.get("MOD_RIGFORGE_CAPTURE_PORTS", "").split(",")
- )
- if port
- }
- self.rigforge_playback_ports = {
- "playback_%d" % (index + 1): port
- for index, port in enumerate(
- os.environ.get("MOD_RIGFORGE_PLAYBACK_PORTS", "").split(",")
- )
- if port
- }
- ''',
- )
- replace_if_present(
- app / "mod" / "host.py",
- ''' self.rigforge_capture_port = os.environ.get("MOD_RIGFORGE_CAPTURE_PORT")
- self.rigforge_playback_port = os.environ.get("MOD_RIGFORGE_PLAYBACK_PORT")
- ''',
- "",
- )
- replace_if_present(
- app / "mod" / "host.py",
- ''' if self.rigforge_capture_port and self.rigforge_playback_port:
- # These graph symbols map to Rigforge's stable virtual PipeWire boundary ports.
- self.audioportsIn = ["capture_1"]
- self.audioportsOut = ["playback_1"]
- ''',
- ''' if self.rigforge_capture_ports and self.rigforge_playback_ports:
- # These graph symbols map to Rigforge's stable virtual PipeWire boundary ports.
- self.audioportsIn = [
- "capture_%d" % index
- for index in range(1, len(self.rigforge_capture_ports) + 1)
- ]
- self.audioportsOut = [
- "playback_%d" % index
- for index in range(1, len(self.rigforge_playback_ports) + 1)
- ]
- ''',
- )
- replace_once(
- app / "mod" / "host.py",
- ''' for port in get_jack_hardware_ports(True, False):
- client_name, port_name = port.split(":",1)
- if client_name == "mod-spi2jack":
- cv_port_name = CV_PREFIX + port_name
- self.cvportsIn.append(cv_port_name)
- self.addressings.add_hw_cv_port('/cv/graph/' + cv_port_name)
- else:
- self.audioportsIn.append(port_name)
- for port in get_jack_hardware_ports(True, True):
- client_name, port_name = port.split(":",1)
- if client_name == "mod-jack2spi":
- self.cvportsOut.append(CV_PREFIX + port_name)
- else:
- self.audioportsOut.append(port_name)
- ''',
- ''' if self.rigforge_capture_ports and self.rigforge_playback_ports:
- # These graph symbols map to Rigforge's stable virtual PipeWire boundary ports.
- self.audioportsIn = [
- "capture_%d" % index
- for index in range(1, len(self.rigforge_capture_ports) + 1)
- ]
- self.audioportsOut = [
- "playback_%d" % index
- for index in range(1, len(self.rigforge_playback_ports) + 1)
- ]
- else:
- for port in get_jack_hardware_ports(True, False):
- client_name, port_name = port.split(":",1)
- if client_name == "mod-spi2jack":
- cv_port_name = CV_PREFIX + port_name
- self.cvportsIn.append(cv_port_name)
- self.addressings.add_hw_cv_port('/cv/graph/' + cv_port_name)
- else:
- self.audioportsIn.append(port_name)
- for port in get_jack_hardware_ports(True, True):
- client_name, port_name = port.split(":",1)
- if client_name == "mod-jack2spi":
- self.cvportsOut.append(CV_PREFIX + port_name)
- else:
- self.audioportsOut.append(port_name)
- ''',
- )
- replace_if_present(
- app / "mod" / "host.py",
- ''' if data[2] == "capture_1" and self.rigforge_capture_port:
- return self.rigforge_capture_port
- if data[2] == "playback_1" and self.rigforge_playback_port:
- return self.rigforge_playback_port
- ''',
- ''' if data[2] in self.rigforge_capture_ports:
- return self.rigforge_capture_ports[data[2]]
- if data[2] in self.rigforge_playback_ports:
- return self.rigforge_playback_ports[data[2]]
- ''',
- )
- replace_if_present(
- app / "mod" / "host.py",
- ''' if len(data) == 3:
- # Handle special cases
- if data[2] == "serial_midi_in":
- ''' + " \n",
- ''' if len(data) == 3:
- # Handle special cases
- if data[2] in self.rigforge_capture_ports:
- return self.rigforge_capture_ports[data[2]]
- if data[2] in self.rigforge_playback_ports:
- return self.rigforge_playback_ports[data[2]]
- if data[2] == "serial_midi_in":
- ''',
- )
- replace_if_present(
- app / "mod" / "host.py",
- ''' def connect(self, port_from, port_to, callback):
- if (port_from, port_to) in self.connections:
- ''',
- ''' def _rigforge_pipewire_boundary(self, port_from, port_to):
- return boundary_connection(
- port_from,
- port_to,
- self.rigforge_capture_ports,
- self.rigforge_playback_ports,
- self._fix_host_connection_port,
- )
- def connect(self, port_from, port_to, callback):
- if (port_from, port_to) in self.connections:
- ''',
- )
- replace_if_present(
- app / "mod" / "host.py",
- ''' def host_callback(ok):
- callback(ok)
- if ok:
- self.connections.append((port_from, port_to))
- self.msg_callback("connect %s %s" % (port_from, port_to))
- else:
- logging.error("[host] backend failed to connect ports: '%s' => '%s'", port_from, port_to)
- self.send_modified("connect %s %s" % (self._fix_host_connection_port(port_from),
- self._fix_host_connection_port(port_to)), host_callback)
- ''',
- ''' resolved = pedalboard_connection(
- port_from, port_to, self.rigforge_capture_ports,
- self.rigforge_playback_ports, self._fix_host_connection_port
- )
- if resolved is not None and resolved[0] == "pipewire":
- pipewire_link = resolved[1]
- ok = connect_link(*pipewire_link)
- callback(ok)
- if ok:
- self.connections.append((port_from, port_to))
- self.pedalboard_modified = True
- self.msg_callback("connect %s %s" % (port_from, port_to))
- else:
- logging.error(
- "[host] PipeWire backend failed to connect ports: '%s' => '%s'",
- pipewire_link[0], pipewire_link[1]
- )
- return
- def host_callback(ok):
- callback(ok)
- if ok:
- self.connections.append((port_from, port_to))
- self.msg_callback("connect %s %s" % (port_from, port_to))
- else:
- logging.error("[host] backend failed to connect ports: '%s' => '%s'", port_from, port_to)
- if resolved is not None:
- port_from_2, port_to_2 = resolved[1]
- else:
- port_from_2 = self._fix_host_connection_port(port_from)
- port_to_2 = self._fix_host_connection_port(port_to)
- self.send_modified("connect %s %s" % (port_from_2, port_to_2), host_callback)
- ''',
- )
- replace_once(
- app / "mod" / "host.py",
- ''' def disconnect(self, port_from, port_to, callback):
- def host_callback(ok):
- ''',
- ''' def disconnect(self, port_from, port_to, callback):
- pipewire_link = self._rigforge_pipewire_boundary(port_from, port_to)
- if pipewire_link is not None:
- ok = disconnect_link(*pipewire_link)
- callback(ok)
- if ok:
- self.msg_callback("disconnect %s %s" % (port_from, port_to))
- self.pedalboard_modified = True
- try:
- self.connections.remove((port_from, port_to))
- except ValueError:
- logging.info(
- "[host] Requested '%s' => '%s' connection was not in the UI state",
- port_from, port_to
- )
- else:
- logging.error(
- "[host] PipeWire backend failed to disconnect ports: '%s' => '%s'",
- pipewire_link[0], pipewire_link[1]
- )
- return
- def host_callback(ok):
- ''',
- )
- replace_if_present(
- app / "mod" / "host.py",
- ''' def load_pb_connections(self, connections, mappedOldMidiIns, mappedOldMidiOuts,
- mappedNewMidiIns, mappedNewMidiOuts):
- ''',
- ''' def _rigforge_restore_pipewire_boundary(
- self, pipewire_link, port_from, port_to, connection_list, attempts_left
- ):
- if self.connections is not connection_list:
- return
- if connect_link(*pipewire_link):
- edge = (port_from, port_to)
- if edge not in connection_list:
- connection_list.append(edge)
- self.msg_callback("connect %s %s" % (port_from, port_to))
- return
- if attempts_left <= 1:
- logging.error(
- "[host] PipeWire backend failed to restore ports: '%s' => '%s'",
- pipewire_link[0], pipewire_link[1]
- )
- return
- IOLoop.instance().call_later(
- 0.5,
- self._rigforge_restore_pipewire_boundary,
- pipewire_link,
- port_from,
- port_to,
- connection_list,
- attempts_left - 1
- )
- def load_pb_connections(self, connections, mappedOldMidiIns, mappedOldMidiOuts,
- mappedNewMidiIns, mappedNewMidiOuts):
- ''',
- )
- load_connections_original = ''' pipewire_link = self._rigforge_pipewire_boundary(port_from, port_to)
- if pipewire_link is not None:
- self._rigforge_restore_pipewire_boundary(
- pipewire_link,
- port_from,
- port_to,
- self.connections,
- attempts_left=60
- )
- continue
- self.send_notmodified("connect %s %s" % (port_from_2, port_to_2))
- self.connections.append((port_from, port_to))
- self.msg_callback("connect %s %s" % (port_from, port_to))
- '''
- load_connections_patched = ''' resolved = pedalboard_connection(
- port_from, port_to, self.rigforge_capture_ports,
- self.rigforge_playback_ports, self._fix_host_connection_port
- )
- if resolved is not None and resolved[0] == "pipewire":
- pipewire_link = resolved[1]
- self._rigforge_restore_pipewire_boundary(
- pipewire_link,
- port_from,
- port_to,
- self.connections,
- attempts_left=60
- )
- continue
- if resolved is not None:
- port_from_2, port_to_2 = resolved[1]
- self.send_notmodified("connect %s %s" % (port_from_2, port_to_2))
- self.connections.append((port_from, port_to))
- self.msg_callback("connect %s %s" % (port_from, port_to))
- '''
- load_connections_old = ''' pipewire_link = self._rigforge_pipewire_boundary(port_from, port_to)
- if pipewire_link is not None:
- self._rigforge_restore_pipewire_boundary(
- pipewire_link,
- port_from,
- port_to,
- self.connections,
- attempts_left=60
- )
- continue
- self.send_notmodified("connect %s %s" % (port_from_2, port_to_2))
- self.connections.append((port_from, port_to))
- '''
- text = (app / "mod" / "host.py").read_text(encoding="utf-8")
- if load_connections_patched not in text:
- if text.count(load_connections_original) == 1:
- original = load_connections_original
- elif text.count(load_connections_old) == 1:
- original = load_connections_old
- else:
- marker = " def load_pb_connections(self, connections, mappedOldMidiIns"
- start = text.find(marker)
- body = text.find(" pipewire_link = self._rigforge_pipewire_boundary(port_from, port_to)", start)
- if start < 0 or body < 0:
- raise RuntimeError("could not identify mod-ui pedalboard connection restoration block")
- end = text.find("\n\n elif aliasname1", body)
- if end < 0:
- raise RuntimeError("could not find end of mod-ui pedalboard connection restoration block")
- original = text[body:end]
- replace_once(
- app / "mod" / "host.py",
- original,
- load_connections_patched.rstrip("\n"),
- )
- replace_if_present(
- app / "mod" / "host.py",
- ''' if len(data) == 3:
- # Handle special cases
- if data[2] == "serial_midi_in":
- ''',
- ''' if len(data) == 3:
- # Handle special cases
- if data[2] in self.rigforge_capture_ports:
- return self.rigforge_capture_ports[data[2]]
- if data[2] in self.rigforge_playback_ports:
- return self.rigforge_playback_ports[data[2]]
- if data[2] == "serial_midi_in":
- ''',
- )
- replace_if_present(
- app / "mod" / "host.py",
- ''' def host_callback(ok):
- callback(ok)
- if ok:
- self.connections.append((port_from, port_to))
- self.msg_callback("connect %s %s" % (port_from, port_to))
- else:
- logging.error("[host] backend failed to connect ports: '%s' => '%s'", port_from, port_to)
- self.send_modified("connect %s %s" % (self._fix_host_connection_port(port_from),
- self._fix_host_connection_port(port_to)), host_callback)
- ''',
- ''' pipewire_link = self._rigforge_pipewire_boundary(port_from, port_to)
- if pipewire_link is not None:
- ok = connect_link(*pipewire_link)
- callback(ok)
- if ok:
- self.connections.append((port_from, port_to))
- self.pedalboard_modified = True
- self.msg_callback("connect %s %s" % (port_from, port_to))
- else:
- logging.error(
- "[host] PipeWire backend failed to connect ports: '%s' => '%s'",
- pipewire_link[0], pipewire_link[1]
- )
- return
- def host_callback(ok):
- callback(ok)
- if ok:
- self.connections.append((port_from, port_to))
- self.msg_callback("connect %s %s" % (port_from, port_to))
- else:
- logging.error("[host] backend failed to connect ports: '%s' => '%s'", port_from, port_to)
- self.send_modified("connect %s %s" % (self._fix_host_connection_port(port_from),
- self._fix_host_connection_port(port_to)), host_callback)
- ''',
- )
- replace_once(
- app / "mod" / "host.py",
- ''' def disconnect(self, port_from, port_to, callback):
- def host_callback(ok):
- ''',
- ''' def disconnect(self, port_from, port_to, callback):
- pipewire_link = self._rigforge_pipewire_boundary(port_from, port_to)
- if pipewire_link is not None:
- ok = disconnect_link(*pipewire_link)
- callback(ok)
- if ok:
- self.msg_callback("disconnect %s %s" % (port_from, port_to))
- self.pedalboard_modified = True
- try:
- self.connections.remove((port_from, port_to))
- except ValueError:
- logging.info(
- "[host] Requested '%s' => '%s' connection was not in the UI state",
- port_from, port_to
- )
- else:
- logging.error(
- "[host] PipeWire backend failed to disconnect ports: '%s' => '%s'",
- pipewire_link[0], pipewire_link[1]
- )
- return
- def host_callback(ok):
- ''',
- )
- replace_if_present(
- app / "mod" / "session.py",
- ''' # Set a plugin block position within the canvas
- ''',
- ''' def ws_rigforge_patch_set(self, request_id, instance, uri, valuetype, valuedata, ws):
- if valuetype != "p":
- ws.write_message("rigforge_patch_set_ack %s 0" % request_id)
- return
- def completed(ok):
- status = 1 if ok else 0
- if ok:
- self.msg_callback_broadcast(
- "patch_set %s 1 %s %c %s" % (instance, uri, valuetype, valuedata),
- ws,
- )
- ws.write_message("rigforge_patch_set_ack %s %d" % (request_id, status))
- self.host.patch_set(instance, uri, valuedata, completed)
- # Set a plugin block position within the canvas
- ''',
- )
- replace_if_present(
- app / "mod" / "webserver.py",
- ''' elif cmd == "plugin_pos":
- ''',
- ''' elif cmd == "rigforge_patch_set":
- data = data[1].split(" ", 4)
- if len(data) != 5:
- self.write_message("rigforge_patch_set_ack invalid 0")
- return
- request_id, inst, uri, vtype, value = data
- if not request_id.isalnum():
- self.write_message("rigforge_patch_set_ack invalid 0")
- return
- SESSION.ws_rigforge_patch_set(
- request_id, inst, uri, vtype, value, self
- )
- elif cmd == "plugin_pos":
- ''',
- )
- replace_if_present(
- app / "server.py",
- "os.environ['MOD_DEVICE_WEBSERVER_PORT'] = '8888'",
- "os.environ.setdefault('MOD_DEVICE_WEBSERVER_PORT', '8888')",
- )
- replace_if_present(
- app / "server.py",
- "os.environ['MOD_DEFAULT_PEDALBOARD'] = os.path.join(ROOT, 'default.pedalboard')",
- "os.environ.setdefault('MOD_DEFAULT_PEDALBOARD', os.path.join(ROOT, 'default.pedalboard'))",
- )
- replace_if_present(
- app / "html" / "js" / "pedalboard.js",
- ''' var captureTitles = {
- 1: 'Guitar - Mod-UI Input',
- 2: 'Bass - Mod-UI Input',
- 3: 'Micro - Mod-UI Input',
- 4: 'Synth - Mod-UI Input'
- }
- var captureTitle = captureTitles[i] || ('Hardware Capture ' + i)
- var hw = $('<div class="ignore-arrive hardware-output" mod-port-index="' + i + '" title="' + captureTitle + '">')
- ''',
- ''' var captureTitles = {
- 1: 'Guitare - Entrée mod-ui',
- 2: 'Basse - Entrée mod-ui',
- 3: 'Micro - Entrée mod-ui',
- 4: 'Synthé - Entrée mod-ui'
- }
- var captureTitle = captureTitles[i] || ('Hardware Capture ' + i)
- var hw = $('<div class="ignore-arrive hardware-output" mod-port-index="' + i + '" title="' + captureTitle + '">')
- ''',
- )
- replace_if_present(
- app / "html" / "js" / "pedalboard.js",
- ''' var playbackTitles = {
- 1: 'Guitar - Mod-UI Output',
- 2: 'Bass - Mod-UI Output',
- 3: 'Micro - Mod-UI Output',
- 4: 'Synth - Mod-UI Output'
- }
- var playbackTitle = playbackTitles[i] || ('Hardware Playback ' + i)
- var hw = $('<div class="ignore-arrive hardware-input" mod-port-index="' + i + '" title="' + playbackTitle + '">')
- ''',
- ''' var playbackTitles = {
- 1: 'Guitare - Sortie mod-ui',
- 2: 'Basse - Sortie mod-ui',
- 3: 'Micro - Sortie mod-ui',
- 4: 'Synthé - Sortie mod-ui'
- }
- var playbackTitle = playbackTitles[i] || ('Hardware Playback ' + i)
- var hw = $('<div class="ignore-arrive hardware-input" mod-port-index="' + i + '" title="' + playbackTitle + '">')
- ''',
- )
- replace_if_present(
- app / "html" / "js" / "pedalboard.js",
- ''' var captureTitle = (i === 1) ? 'Rigforge Mod-Host Input' : ('Hardware Capture ' + i)
- var hw = $('<div class="ignore-arrive hardware-output" mod-port-index="' + i + '" title="' + captureTitle + '">')
- ''',
- ''' var captureTitles = {
- 1: 'Guitare - Entrée mod-ui',
- 2: 'Basse - Entrée mod-ui',
- 3: 'Micro - Entrée mod-ui',
- 4: 'Synthé - Entrée mod-ui'
- }
- var captureTitle = captureTitles[i] || ('Hardware Capture ' + i)
- var hw = $('<div class="ignore-arrive hardware-output" mod-port-index="' + i + '" title="' + captureTitle + '">')
- ''',
- )
- replace_if_present(
- app / "html" / "js" / "pedalboard.js",
- ''' var playbackTitle = (i === 1) ? 'Rigforge Mod-Host Output' : ('Hardware Playback ' + i)
- var hw = $('<div class="ignore-arrive hardware-input" mod-port-index="' + i + '" title="' + playbackTitle + '">')
- ''',
- ''' var playbackTitles = {
- 1: 'Guitare - Sortie mod-ui',
- 2: 'Basse - Sortie mod-ui',
- 3: 'Micro - Sortie mod-ui',
- 4: 'Synthé - Sortie mod-ui'
- }
- var playbackTitle = playbackTitles[i] || ('Hardware Playback ' + i)
- var hw = $('<div class="ignore-arrive hardware-input" mod-port-index="' + i + '" title="' + playbackTitle + '">')
- ''',
- )
- replace_once(
- app / "html" / "js" / "pedalboard.js",
- ''' var hw = $('<div class="ignore-arrive hardware-output" mod-port-index="' + i + '" title="Hardware Capture ' + i + '">')
- ''',
- ''' var captureTitles = {
- 1: 'Guitare - Entrée mod-ui',
- 2: 'Basse - Entrée mod-ui',
- 3: 'Micro - Entrée mod-ui',
- 4: 'Synthé - Entrée mod-ui'
- }
- var captureTitle = captureTitles[i] || ('Hardware Capture ' + i)
- var hw = $('<div class="ignore-arrive hardware-output" mod-port-index="' + i + '" title="' + captureTitle + '">')
- ''',
- )
- replace_once(
- app / "html" / "js" / "pedalboard.js",
- ''' var hw = $('<div class="ignore-arrive hardware-input" mod-port-index="' + i + '" title="Hardware Playback ' + i + '">')
- ''',
- ''' var playbackTitles = {
- 1: 'Guitare - Sortie mod-ui',
- 2: 'Basse - Sortie mod-ui',
- 3: 'Micro - Sortie mod-ui',
- 4: 'Synthé - Sortie mod-ui'
- }
- var playbackTitle = playbackTitles[i] || ('Hardware Playback ' + i)
- var hw = $('<div class="ignore-arrive hardware-input" mod-port-index="' + i + '" title="' + playbackTitle + '">')
- ''',
- )
- js_file = app / "html" / "js" / "pedalboard.js"
- if not js_file.read_bytes().endswith(b"\n"):
- js_file.write_bytes(js_file.read_bytes() + b"\n")
- if __name__ == "__main__":
- if len(sys.argv) != 2:
- raise SystemExit("Usage: patch_mod_ui_rigforge.py path/to/mod-ui/app")
- apply(Path(sys.argv[1]))
|