rigforge_pipewire.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. "lv2_audio_in_1": "lv2_audio_in_1",
  69. "lv2_audio_out_1": "lv2_audio_out_1",
  70. "lv2_audio_in_2": "lv2_audio_in_2",
  71. "lv2_audio_out_2": "lv2_audio_out_2",
  72. },
  73. }
  74. resolved_symbol = effect_ports.get(instance, {}).get(symbol)
  75. return f"effect_{effect_index}:{resolved_symbol}" if resolved_symbol else None
  76. def is_rigforge_managed_boundary_port(port):
  77. """Identify one of the four Rigforge PipeWire endpoints from either API path."""
  78. graph_path = port.removeprefix("/graph/")
  79. if "/" in graph_path:
  80. return False
  81. return graph_path in {
  82. *(f"capture_{index}" for index in range(1, 5)),
  83. *(f"playback_{index}" for index in range(1, 5)),
  84. }
  85. def pedalboard_connection(
  86. port_from,
  87. port_to,
  88. capture_ports,
  89. playback_ports,
  90. resolve_host_port,
  91. ):
  92. """Resolve a pedalboard edge to a JACK or concrete PipeWire link."""
  93. source_path = port_from.removeprefix("/graph/")
  94. destination_path = port_to.removeprefix("/graph/")
  95. captures = dict(capture_ports)
  96. playbacks = dict(playback_ports)
  97. source_is_capture = source_path in captures
  98. destination_is_playback = destination_path in playbacks
  99. if "/" in source_path and not source_is_capture:
  100. captures = {}
  101. if "/" in destination_path and not destination_is_playback:
  102. playbacks = {}
  103. # mod-ui's effect/connect route may pass bare boundary symbols and its
  104. # generic host-port resolver can turn playback_4 into system:playback_4.
  105. # Resolve an explicit Rigforge boundary pair before trying JACK aliases.
  106. if source_path in captures and destination_path in playbacks:
  107. return "pipewire", (captures[source_path], playbacks[destination_path])
  108. # mod-ui's HTTP route passes bare hardware symbols ("capture_3"), but the
  109. # saved-board loader passes "/graph/capture_3". Preserve the latter marker:
  110. # if the opposite endpoint cannot be resolved as a Rigforge plugin port,
  111. # never guess a JACK/system alias for a stable PipeWire boundary.
  112. boundary_endpoint = (
  113. is_rigforge_managed_boundary_port(port_from)
  114. or is_rigforge_managed_boundary_port(port_to)
  115. )
  116. pipewire_link = boundary_connection(
  117. port_from, port_to, captures, playbacks, resolve_host_port
  118. )
  119. if pipewire_link is None and boundary_endpoint:
  120. pipewire_link = boundary_connection(
  121. port_from,
  122. port_to,
  123. captures,
  124. playbacks,
  125. lambda port: (
  126. resolve_pedalboard_port(port, resolve_host_port)
  127. or resolve_host_port("/graph/" + port.removeprefix("/graph/"))
  128. ),
  129. )
  130. if (
  131. pipewire_link is None
  132. and source_path in captures
  133. and is_rigforge_managed_boundary_port(port_from)
  134. and destination_path.startswith("rigforge_")
  135. ):
  136. resolved_destination = resolve_pedalboard_port(port_to, resolve_host_port)
  137. if resolved_destination is not None:
  138. pipewire_link = (captures[source_path], resolved_destination)
  139. if (
  140. pipewire_link is None
  141. and destination_path in playbacks
  142. and is_rigforge_managed_boundary_port(port_to)
  143. and source_path.startswith("rigforge_")
  144. ):
  145. resolved_source = resolve_pedalboard_port(port_from, resolve_host_port)
  146. if resolved_source is not None:
  147. pipewire_link = (resolved_source, playbacks[destination_path])
  148. if pipewire_link is not None:
  149. return "pipewire", pipewire_link
  150. if boundary_endpoint:
  151. return None
  152. source = resolve_pedalboard_port(port_from, resolve_host_port)
  153. destination = resolve_pedalboard_port(port_to, resolve_host_port)
  154. if source is None or destination is None:
  155. return None
  156. return "jack", (source, destination)
  157. def _link_status(source, destination, runner):
  158. try:
  159. result = runner(
  160. ["pw-link", "-l"],
  161. check=False,
  162. capture_output=True,
  163. text=True,
  164. )
  165. except OSError:
  166. return None
  167. if result.returncode != 0:
  168. return None
  169. current_port = None
  170. for line in result.stdout.splitlines():
  171. if line.startswith(" |-> "):
  172. if current_port == source and line.removeprefix(" |-> ") == destination:
  173. return True
  174. elif line.startswith(" |<- "):
  175. if current_port == destination and line.removeprefix(" |<- ") == source:
  176. return True
  177. else:
  178. current_port = line.strip()
  179. return False
  180. def connect_link(
  181. source,
  182. destination,
  183. runner=subprocess.run,
  184. retries=0,
  185. retry_interval=0.1,
  186. ):
  187. for attempt in range(retries + 1):
  188. try:
  189. result = runner(
  190. ["pw-link", source, destination],
  191. check=False,
  192. capture_output=True,
  193. text=True,
  194. )
  195. except OSError:
  196. return False
  197. if result.returncode == 0 or _link_status(source, destination, runner) is True:
  198. return True
  199. if attempt < retries:
  200. time.sleep(retry_interval)
  201. return False
  202. def disconnect_link(source, destination, runner=subprocess.run):
  203. try:
  204. result = runner(
  205. ["pw-link", "-d", source, destination],
  206. check=False,
  207. capture_output=True,
  208. text=True,
  209. )
  210. except OSError:
  211. return False
  212. return result.returncode == 0 or _link_status(source, destination, runner) is False