mod_ui_pipewire.py 5.9 KB

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