| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- """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."""
- capture_symbol = port_from.rsplit("/", 1)[-1]
- capture_port = capture_ports.get(capture_symbol)
- if capture_port:
- host_port = resolve_host_port(port_to)
- if host_port:
- return capture_port, host_port
- return None
- playback_symbol = port_to.rsplit("/", 1)[-1]
- playback_port = playback_ports.get(playback_symbol)
- if playback_port:
- host_port = resolve_host_port(port_from)
- 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:
- return resolve_host_port(port)
- instance, symbol = graph_path.split("/", 1)
- effect_index = {
- "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_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": "input_l", "output_l": "output_l"},
- }
- resolved_symbol = effect_ports.get(instance, {}).get(symbol)
- return f"effect_{effect_index}:{resolved_symbol}" if resolved_symbol else None
- 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
|