|
@@ -8,68 +8,7 @@ import sys
|
|
|
import time
|
|
import time
|
|
|
from pathlib import Path
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
-import yaml
|
|
|
|
|
-
|
|
|
|
|
from mod_ui_websocket import ModUiWebSocketClient
|
|
from mod_ui_websocket import ModUiWebSocketClient
|
|
|
-from observe_pipewire import list_ports, parse_pw_link_output
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-def mod_ui_audio_boundary_links(state):
|
|
|
|
|
- chain = state["routing"]["mod_host_chains"]["guitar"]
|
|
|
|
|
- processors = chain["processors"]
|
|
|
|
|
- if not processors:
|
|
|
|
|
- raise ValueError("the mod-ui guitar chain has no processors")
|
|
|
|
|
- return (
|
|
|
|
|
- (chain["source_port"], processors[0]["input_port"]),
|
|
|
|
|
- (processors[-1]["output_port"], chain["mod_ui_output_port"]),
|
|
|
|
|
- )
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-def ensure_mod_ui_audio_links(state, runner=subprocess.run):
|
|
|
|
|
- """Restore only the two board-to-boundary audio cables owned by mod-ui."""
|
|
|
|
|
- result = runner(
|
|
|
|
|
- ["pw-link", "-l"],
|
|
|
|
|
- check=True,
|
|
|
|
|
- capture_output=True,
|
|
|
|
|
- text=True,
|
|
|
|
|
- )
|
|
|
|
|
- links = parse_pw_link_output(result.stdout)
|
|
|
|
|
- expected = mod_ui_audio_boundary_links(state)
|
|
|
|
|
- missing = [edge for edge in expected if edge not in links]
|
|
|
|
|
- if not missing:
|
|
|
|
|
- return 0
|
|
|
|
|
-
|
|
|
|
|
- outputs = list_ports(runner, "-o")
|
|
|
|
|
- inputs = list_ports(runner, "-i")
|
|
|
|
|
- for source, destination in missing:
|
|
|
|
|
- if source not in outputs:
|
|
|
|
|
- raise RuntimeError(f"mod-ui audio source port is unavailable: {source}")
|
|
|
|
|
- if destination not in inputs:
|
|
|
|
|
- raise RuntimeError(f"mod-ui audio destination port is unavailable: {destination}")
|
|
|
|
|
- runner(
|
|
|
|
|
- ["pw-link", source, destination],
|
|
|
|
|
- check=True,
|
|
|
|
|
- capture_output=True,
|
|
|
|
|
- text=True,
|
|
|
|
|
- )
|
|
|
|
|
- print(f"Restored mod-ui audio cable: {source} -> {destination}")
|
|
|
|
|
-
|
|
|
|
|
- result = runner(
|
|
|
|
|
- ["pw-link", "-l"],
|
|
|
|
|
- check=True,
|
|
|
|
|
- capture_output=True,
|
|
|
|
|
- text=True,
|
|
|
|
|
- )
|
|
|
|
|
- remaining = [
|
|
|
|
|
- edge for edge in expected
|
|
|
|
|
- if edge not in parse_pw_link_output(result.stdout)
|
|
|
|
|
- ]
|
|
|
|
|
- if remaining:
|
|
|
|
|
- source, destination = remaining[0]
|
|
|
|
|
- raise RuntimeError(
|
|
|
|
|
- f"mod-ui audio cable is still missing after repair: {source} -> {destination}"
|
|
|
|
|
- )
|
|
|
|
|
- return len(missing)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def run_service(
|
|
def run_service(
|
|
@@ -79,9 +18,7 @@ def run_service(
|
|
|
port,
|
|
port,
|
|
|
websocket_path,
|
|
websocket_path,
|
|
|
timeout,
|
|
timeout,
|
|
|
- state,
|
|
|
|
|
popen=subprocess.Popen,
|
|
popen=subprocess.Popen,
|
|
|
- pw_link_runner=subprocess.run,
|
|
|
|
|
):
|
|
):
|
|
|
process = popen([str(pw_jack), str(launcher)])
|
|
process = popen([str(pw_jack), str(launcher)])
|
|
|
stopping = [False]
|
|
stopping = [False]
|
|
@@ -93,8 +30,6 @@ def run_service(
|
|
|
old_int = signal.signal(signal.SIGINT, request_stop)
|
|
old_int = signal.signal(signal.SIGINT, request_stop)
|
|
|
client = ModUiWebSocketClient(host, port, websocket_path, timeout=2)
|
|
client = ModUiWebSocketClient(host, port, websocket_path, timeout=2)
|
|
|
deadline = time.monotonic() + timeout
|
|
deadline = time.monotonic() + timeout
|
|
|
- repair_audio_links = False
|
|
|
|
|
- next_audio_link_repair = 0.0
|
|
|
|
|
try:
|
|
try:
|
|
|
while not stopping[0]:
|
|
while not stopping[0]:
|
|
|
if process.poll() is not None:
|
|
if process.poll() is not None:
|
|
@@ -116,7 +51,6 @@ def run_service(
|
|
|
raise RuntimeError(f"mod-ui launcher exited with status {process.returncode}")
|
|
raise RuntimeError(f"mod-ui launcher exited with status {process.returncode}")
|
|
|
message = client.serve_once(timeout=0.5)
|
|
message = client.serve_once(timeout=0.5)
|
|
|
if message and message.startswith("loading_end "):
|
|
if message and message.startswith("loading_end "):
|
|
|
- repair_audio_links = True
|
|
|
|
|
break
|
|
break
|
|
|
if time.monotonic() >= deadline:
|
|
if time.monotonic() >= deadline:
|
|
|
raise RuntimeError("mod-ui did not finish loading a pedalboard before timeout")
|
|
raise RuntimeError("mod-ui did not finish loading a pedalboard before timeout")
|
|
@@ -124,19 +58,7 @@ def run_service(
|
|
|
while not stopping[0]:
|
|
while not stopping[0]:
|
|
|
if process.poll() is not None:
|
|
if process.poll() is not None:
|
|
|
raise RuntimeError(f"mod-ui launcher exited with status {process.returncode}")
|
|
raise RuntimeError(f"mod-ui launcher exited with status {process.returncode}")
|
|
|
- message = client.serve_once(timeout=0.5)
|
|
|
|
|
- if message and message.startswith("loading_end "):
|
|
|
|
|
- repair_audio_links = True
|
|
|
|
|
- next_audio_link_repair = 0.0
|
|
|
|
|
- if repair_audio_links and time.monotonic() >= next_audio_link_repair:
|
|
|
|
|
- try:
|
|
|
|
|
- repaired = ensure_mod_ui_audio_links(state, runner=pw_link_runner)
|
|
|
|
|
- if repaired == 0:
|
|
|
|
|
- print("mod-ui audio boundary cables are already connected")
|
|
|
|
|
- repair_audio_links = False
|
|
|
|
|
- except (OSError, subprocess.CalledProcessError, RuntimeError, ValueError) as error:
|
|
|
|
|
- print(f"ERROR: could not ensure mod-ui audio boundary cables: {error}", file=sys.stderr)
|
|
|
|
|
- next_audio_link_repair = time.monotonic() + 2.0
|
|
|
|
|
|
|
+ client.serve_once(timeout=0.5)
|
|
|
finally:
|
|
finally:
|
|
|
signal.signal(signal.SIGTERM, old_term)
|
|
signal.signal(signal.SIGTERM, old_term)
|
|
|
signal.signal(signal.SIGINT, old_int)
|
|
signal.signal(signal.SIGINT, old_int)
|
|
@@ -167,8 +89,6 @@ def main(argv=None):
|
|
|
print(f"ERROR: pw-jack is missing: {args.pw_jack}", file=sys.stderr)
|
|
print(f"ERROR: pw-jack is missing: {args.pw_jack}", file=sys.stderr)
|
|
|
return 1
|
|
return 1
|
|
|
try:
|
|
try:
|
|
|
- with (root / "config" / "desired-state.yaml").open(encoding="utf-8") as stream:
|
|
|
|
|
- state = yaml.safe_load(stream)
|
|
|
|
|
run_service(
|
|
run_service(
|
|
|
args.launcher,
|
|
args.launcher,
|
|
|
args.pw_jack,
|
|
args.pw_jack,
|
|
@@ -176,7 +96,6 @@ def main(argv=None):
|
|
|
args.port,
|
|
args.port,
|
|
|
args.websocket_path,
|
|
args.websocket_path,
|
|
|
args.timeout,
|
|
args.timeout,
|
|
|
- state,
|
|
|
|
|
)
|
|
)
|
|
|
except (OSError, RuntimeError, ValueError) as error:
|
|
except (OSError, RuntimeError, ValueError) as error:
|
|
|
print(f"ERROR: {error}", file=sys.stderr)
|
|
print(f"ERROR: {error}", file=sys.stderr)
|