mod_ui_pipewire.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. """PipeWire link helpers used by the Rigforge mod-ui host integration."""
  2. import subprocess
  3. import time
  4. def boundary_connection(
  5. port_from,
  6. port_to,
  7. capture_ports,
  8. playback_ports,
  9. resolve_host_port,
  10. ):
  11. """Translate mod-ui graph boundary cables to concrete PipeWire ports."""
  12. capture_symbol = port_from.rsplit("/", 1)[-1]
  13. capture_port = capture_ports.get(capture_symbol)
  14. if capture_port:
  15. host_port = resolve_host_port(port_to)
  16. if host_port:
  17. return capture_port, host_port
  18. return None
  19. playback_symbol = port_to.rsplit("/", 1)[-1]
  20. playback_port = playback_ports.get(playback_symbol)
  21. if playback_port:
  22. host_port = resolve_host_port(port_from)
  23. if host_port:
  24. return host_port, playback_port
  25. return None
  26. return None
  27. def resolve_pedalboard_port(port, resolve_host_port):
  28. """Resolve Ingen-style graph ports and LV2 effect_N aliases for pw-link."""
  29. graph_path = port.removeprefix("/graph/")
  30. if "/" not in graph_path:
  31. return resolve_host_port(port)
  32. instance, symbol = graph_path.split("/", 1)
  33. effect_index = {
  34. "rigforge_voice_high_pass": 2,
  35. "rigforge_voice_compressor": 3,
  36. "rigforge_voice_de_esser": 4,
  37. "rigforge_voice_reverb": 5,
  38. }.get(instance)
  39. if effect_index is None:
  40. return resolve_host_port(port)
  41. effect_ports = {
  42. "rigforge_voice_high_pass": {"in": "in", "out": "out"},
  43. "rigforge_voice_compressor": {"in": "in", "out": "out"},
  44. "rigforge_voice_de_esser": {"in": "in", "out": "out"},
  45. "rigforge_voice_reverb": {"input_l": "input_l", "output_l": "output_l"},
  46. }
  47. resolved_symbol = effect_ports.get(instance, {}).get(symbol)
  48. return f"effect_{effect_index}:{resolved_symbol}" if resolved_symbol else None
  49. def _link_status(source, destination, runner):
  50. try:
  51. result = runner(
  52. ["pw-link", "-l"],
  53. check=False,
  54. capture_output=True,
  55. text=True,
  56. )
  57. except OSError:
  58. return None
  59. if result.returncode != 0:
  60. return None
  61. current_port = None
  62. for line in result.stdout.splitlines():
  63. if line.startswith(" |-> "):
  64. if current_port == source and line.removeprefix(" |-> ") == destination:
  65. return True
  66. elif line.startswith(" |<- "):
  67. if current_port == destination and line.removeprefix(" |<- ") == source:
  68. return True
  69. else:
  70. current_port = line.strip()
  71. return False
  72. def connect_link(
  73. source,
  74. destination,
  75. runner=subprocess.run,
  76. retries=0,
  77. retry_interval=0.1,
  78. ):
  79. for attempt in range(retries + 1):
  80. try:
  81. result = runner(
  82. ["pw-link", source, destination],
  83. check=False,
  84. capture_output=True,
  85. text=True,
  86. )
  87. except OSError:
  88. return False
  89. if result.returncode == 0 or _link_status(source, destination, runner) is True:
  90. return True
  91. if attempt < retries:
  92. time.sleep(retry_interval)
  93. return False
  94. def disconnect_link(source, destination, runner=subprocess.run):
  95. try:
  96. result = runner(
  97. ["pw-link", "-d", source, destination],
  98. check=False,
  99. capture_output=True,
  100. text=True,
  101. )
  102. except OSError:
  103. return False
  104. return result.returncode == 0 or _link_status(source, destination, runner) is False