rigforge_pipewire.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. source_path = port_from.removeprefix("/graph/")
  13. capture_symbol = source_path.rsplit("/", 1)[-1]
  14. capture_port = capture_ports.get(capture_symbol)
  15. if capture_port:
  16. host_port = resolve_pedalboard_port(
  17. port_to, lambda candidate: (
  18. resolve_host_port(candidate)
  19. or resolve_host_port("/graph/" + candidate.removeprefix("/graph/"))
  20. )
  21. )
  22. if host_port:
  23. return capture_port, host_port
  24. return None
  25. destination_path = port_to.removeprefix("/graph/")
  26. playback_symbol = destination_path.rsplit("/", 1)[-1]
  27. playback_port = playback_ports.get(playback_symbol)
  28. if playback_port:
  29. host_port = resolve_pedalboard_port(
  30. port_from, lambda candidate: (
  31. resolve_host_port(candidate)
  32. or resolve_host_port("/graph/" + candidate.removeprefix("/graph/"))
  33. )
  34. )
  35. if host_port:
  36. return host_port, playback_port
  37. return None
  38. return None
  39. def resolve_pedalboard_port(port, resolve_host_port):
  40. """Resolve Ingen-style graph ports and LV2 effect_N aliases for pw-link."""
  41. graph_path = port.removeprefix("/graph/")
  42. if "/" not in graph_path:
  43. if graph_path.startswith("capture_") or graph_path.startswith("playback_"):
  44. return resolve_host_port("/graph/" + graph_path)
  45. return resolve_host_port(port)
  46. instance, symbol = graph_path.split("/", 1)
  47. effect_index = {
  48. "rigforge_nam": 0,
  49. "rigforge_ir": 1,
  50. "rigforge_voice_high_pass": 2,
  51. "rigforge_voice_compressor": 3,
  52. "rigforge_voice_de_esser": 4,
  53. "rigforge_voice_reverb": 5,
  54. }.get(instance)
  55. if effect_index is None:
  56. return resolve_host_port(port)
  57. effect_ports = {
  58. "rigforge_nam": {"input": "input", "output": "output"},
  59. "rigforge_ir": {"in": "in", "out": "out"},
  60. "rigforge_voice_high_pass": {"in": "in", "out": "out"},
  61. "rigforge_voice_compressor": {"in": "in", "out": "out"},
  62. "rigforge_voice_de_esser": {"in": "in", "out": "out"},
  63. "rigforge_voice_reverb": {
  64. "input_l": "lv2_audio_in_1",
  65. "output_l": "lv2_audio_out_1",
  66. "input_r": "lv2_audio_in_2",
  67. "output_r": "lv2_audio_out_2",
  68. },
  69. }
  70. resolved_symbol = effect_ports.get(instance, {}).get(symbol)
  71. return f"effect_{effect_index}:{resolved_symbol}" if resolved_symbol else None
  72. def is_rigforge_managed_boundary_port(port):
  73. """Identify one of the four Rigforge PipeWire endpoints from either API path."""
  74. graph_path = port.removeprefix("/graph/")
  75. if "/" in graph_path:
  76. return False
  77. return graph_path in {
  78. *(f"capture_{index}" for index in range(1, 5)),
  79. *(f"playback_{index}" for index in range(1, 5)),
  80. }
  81. def pedalboard_connection(
  82. port_from,
  83. port_to,
  84. capture_ports,
  85. playback_ports,
  86. resolve_host_port,
  87. ):
  88. """Resolve a pedalboard edge to a JACK or concrete PipeWire link."""
  89. source_path = port_from.removeprefix("/graph/")
  90. destination_path = port_to.removeprefix("/graph/")
  91. captures = dict(capture_ports)
  92. playbacks = dict(playback_ports)
  93. source_is_capture = source_path in captures
  94. destination_is_playback = destination_path in playbacks
  95. if "/" in source_path and not source_is_capture:
  96. captures = {}
  97. if "/" in destination_path and not destination_is_playback:
  98. playbacks = {}
  99. # mod-ui's HTTP route passes bare hardware symbols ("capture_3"), but the
  100. # saved-board loader passes "/graph/capture_3". Preserve the latter marker:
  101. # if the opposite endpoint cannot be resolved as a Rigforge plugin port,
  102. # never guess a JACK/system alias for a stable PipeWire boundary.
  103. boundary_endpoint = (
  104. is_rigforge_managed_boundary_port(port_from)
  105. or is_rigforge_managed_boundary_port(port_to)
  106. )
  107. pipewire_link = boundary_connection(
  108. port_from, port_to, captures, playbacks, resolve_host_port
  109. )
  110. if pipewire_link is None and boundary_endpoint:
  111. pipewire_link = boundary_connection(
  112. port_from,
  113. port_to,
  114. captures,
  115. playbacks,
  116. lambda port: (
  117. resolve_pedalboard_port(port, resolve_host_port)
  118. or resolve_host_port("/graph/" + port.removeprefix("/graph/"))
  119. ),
  120. )
  121. if (
  122. pipewire_link is None
  123. and source_path in captures
  124. and is_rigforge_managed_boundary_port(port_from)
  125. and destination_path.startswith("rigforge_")
  126. ):
  127. pipewire_link = (
  128. captures[source_path],
  129. resolve_pedalboard_port(port_to, resolve_host_port),
  130. )
  131. if (
  132. pipewire_link is None
  133. and destination_path in playbacks
  134. and is_rigforge_managed_boundary_port(port_to)
  135. and source_path.startswith("rigforge_")
  136. ):
  137. pipewire_link = (
  138. resolve_pedalboard_port(port_from, resolve_host_port),
  139. playbacks[destination_path],
  140. )
  141. if pipewire_link is not None:
  142. return "pipewire", pipewire_link
  143. if boundary_endpoint:
  144. return None
  145. source = resolve_pedalboard_port(port_from, resolve_host_port)
  146. destination = resolve_pedalboard_port(port_to, resolve_host_port)
  147. if source is None or destination is None:
  148. return None
  149. return "jack", (source, destination)
  150. def _link_status(source, destination, runner):
  151. try:
  152. result = runner(
  153. ["pw-link", "-l"],
  154. check=False,
  155. capture_output=True,
  156. text=True,
  157. )
  158. except OSError:
  159. return None
  160. if result.returncode != 0:
  161. return None
  162. current_port = None
  163. for line in result.stdout.splitlines():
  164. if line.startswith(" |-> "):
  165. if current_port == source and line.removeprefix(" |-> ") == destination:
  166. return True
  167. elif line.startswith(" |<- "):
  168. if current_port == destination and line.removeprefix(" |<- ") == source:
  169. return True
  170. else:
  171. current_port = line.strip()
  172. return False
  173. def connect_link(
  174. source,
  175. destination,
  176. runner=subprocess.run,
  177. retries=0,
  178. retry_interval=0.1,
  179. ):
  180. for attempt in range(retries + 1):
  181. try:
  182. result = runner(
  183. ["pw-link", source, destination],
  184. check=False,
  185. capture_output=True,
  186. text=True,
  187. )
  188. except OSError:
  189. return False
  190. if result.returncode == 0 or _link_status(source, destination, runner) is True:
  191. return True
  192. if attempt < retries:
  193. time.sleep(retry_interval)
  194. return False
  195. def disconnect_link(source, destination, runner=subprocess.run):
  196. try:
  197. result = runner(
  198. ["pw-link", "-d", source, destination],
  199. check=False,
  200. capture_output=True,
  201. text=True,
  202. )
  203. except OSError:
  204. return False
  205. return result.returncode == 0 or _link_status(source, destination, runner) is False