| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- """PipeWire link helpers used by the Rigforge mod-ui host integration."""
- import subprocess
- import time
- def boundary_connection(
- port_from,
- port_to,
- capture_ports,
- playback_ports,
- resolve_host_port,
- ):
- """Translate mod-ui graph boundary cables to concrete PipeWire ports."""
- source_path = port_from.removeprefix("/graph/")
- capture_symbol = source_path.rsplit("/", 1)[-1]
- capture_port = capture_ports.get(capture_symbol)
- if capture_port:
- host_port = resolve_pedalboard_port(
- port_to, lambda candidate: (
- resolve_host_port(candidate)
- or resolve_host_port("/graph/" + candidate.removeprefix("/graph/"))
- )
- )
- if host_port:
- return capture_port, host_port
- return None
- destination_path = port_to.removeprefix("/graph/")
- playback_symbol = destination_path.rsplit("/", 1)[-1]
- playback_port = playback_ports.get(playback_symbol)
- if playback_port:
- host_port = resolve_pedalboard_port(
- port_from, lambda candidate: (
- resolve_host_port(candidate)
- or resolve_host_port("/graph/" + candidate.removeprefix("/graph/"))
- )
- )
- if host_port:
- return host_port, playback_port
- return None
- return None
- def resolve_pedalboard_port(port, resolve_host_port):
- """Resolve Ingen-style graph ports and LV2 effect_N aliases for pw-link."""
- graph_path = port.removeprefix("/graph/")
- if "/" not in graph_path:
- if graph_path.startswith("capture_") or graph_path.startswith("playback_"):
- return resolve_host_port("/graph/" + graph_path)
- return resolve_host_port(port)
- instance, symbol = graph_path.split("/", 1)
- effect_index = {
- "rigforge_nam": 0,
- "rigforge_ir": 1,
- "rigforge_voice_high_pass": 2,
- "rigforge_voice_compressor": 3,
- "rigforge_voice_de_esser": 4,
- "rigforge_voice_reverb": 5,
- }.get(instance)
- if effect_index is None:
- return resolve_host_port(port)
- effect_ports = {
- "rigforge_nam": {"input": "input", "output": "output"},
- "rigforge_ir": {"in": "in", "out": "out"},
- "rigforge_voice_high_pass": {"in": "in", "out": "out"},
- "rigforge_voice_compressor": {"in": "in", "out": "out"},
- "rigforge_voice_de_esser": {"in": "in", "out": "out"},
- "rigforge_voice_reverb": {
- "input_l": "lv2_audio_in_1",
- "output_l": "lv2_audio_out_1",
- "input_r": "lv2_audio_in_2",
- "output_r": "lv2_audio_out_2",
- },
- }
- resolved_symbol = effect_ports.get(instance, {}).get(symbol)
- return f"effect_{effect_index}:{resolved_symbol}" if resolved_symbol else None
- def is_rigforge_managed_boundary_port(port):
- """Identify one of the four Rigforge PipeWire endpoints from either API path."""
- graph_path = port.removeprefix("/graph/")
- if "/" in graph_path:
- return False
- return graph_path in {
- *(f"capture_{index}" for index in range(1, 5)),
- *(f"playback_{index}" for index in range(1, 5)),
- }
- def pedalboard_connection(
- port_from,
- port_to,
- capture_ports,
- playback_ports,
- resolve_host_port,
- ):
- """Resolve a pedalboard edge to a JACK or concrete PipeWire link."""
- source_path = port_from.removeprefix("/graph/")
- destination_path = port_to.removeprefix("/graph/")
- captures = dict(capture_ports)
- playbacks = dict(playback_ports)
- source_is_capture = source_path in captures
- destination_is_playback = destination_path in playbacks
- if "/" in source_path and not source_is_capture:
- captures = {}
- if "/" in destination_path and not destination_is_playback:
- playbacks = {}
- # mod-ui's HTTP route passes bare hardware symbols ("capture_3"), but the
- # saved-board loader passes "/graph/capture_3". Preserve the latter marker:
- # if the opposite endpoint cannot be resolved as a Rigforge plugin port,
- # never guess a JACK/system alias for a stable PipeWire boundary.
- boundary_endpoint = (
- is_rigforge_managed_boundary_port(port_from)
- or is_rigforge_managed_boundary_port(port_to)
- )
- pipewire_link = boundary_connection(
- port_from, port_to, captures, playbacks, resolve_host_port
- )
- if pipewire_link is None and boundary_endpoint:
- pipewire_link = boundary_connection(
- port_from,
- port_to,
- captures,
- playbacks,
- lambda port: (
- resolve_pedalboard_port(port, resolve_host_port)
- or resolve_host_port("/graph/" + port.removeprefix("/graph/"))
- ),
- )
- if (
- pipewire_link is None
- and source_path in captures
- and is_rigforge_managed_boundary_port(port_from)
- and destination_path.startswith("rigforge_")
- ):
- pipewire_link = (
- captures[source_path],
- resolve_pedalboard_port(port_to, resolve_host_port),
- )
- if (
- pipewire_link is None
- and destination_path in playbacks
- and is_rigforge_managed_boundary_port(port_to)
- and source_path.startswith("rigforge_")
- ):
- pipewire_link = (
- resolve_pedalboard_port(port_from, resolve_host_port),
- playbacks[destination_path],
- )
- if pipewire_link is not None:
- return "pipewire", pipewire_link
- if boundary_endpoint:
- return None
- source = resolve_pedalboard_port(port_from, resolve_host_port)
- destination = resolve_pedalboard_port(port_to, resolve_host_port)
- if source is None or destination is None:
- return None
- return "jack", (source, destination)
- def _link_status(source, destination, runner):
- try:
- result = runner(
- ["pw-link", "-l"],
- check=False,
- capture_output=True,
- text=True,
- )
- except OSError:
- return None
- if result.returncode != 0:
- return None
- current_port = None
- for line in result.stdout.splitlines():
- if line.startswith(" |-> "):
- if current_port == source and line.removeprefix(" |-> ") == destination:
- return True
- elif line.startswith(" |<- "):
- if current_port == destination and line.removeprefix(" |<- ") == source:
- return True
- else:
- current_port = line.strip()
- return False
- def connect_link(
- source,
- destination,
- runner=subprocess.run,
- retries=0,
- retry_interval=0.1,
- ):
- for attempt in range(retries + 1):
- try:
- result = runner(
- ["pw-link", source, destination],
- check=False,
- capture_output=True,
- text=True,
- )
- except OSError:
- return False
- if result.returncode == 0 or _link_status(source, destination, runner) is True:
- return True
- if attempt < retries:
- time.sleep(retry_interval)
- return False
- def disconnect_link(source, destination, runner=subprocess.run):
- try:
- result = runner(
- ["pw-link", "-d", source, destination],
- check=False,
- capture_output=True,
- text=True,
- )
- except OSError:
- return False
- return result.returncode == 0 or _link_status(source, destination, runner) is False
|