rigforge_pipewire.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 pedalboard_connection(
  61. port_from,
  62. port_to,
  63. capture_ports,
  64. playback_ports,
  65. resolve_host_port,
  66. ):
  67. """Resolve a pedalboard edge to a JACK or concrete PipeWire link."""
  68. source_path = port_from.removeprefix("/graph/")
  69. destination_path = port_to.removeprefix("/graph/")
  70. captures = dict(capture_ports)
  71. playbacks = dict(playback_ports)
  72. source_is_capture = source_path in captures
  73. destination_is_playback = destination_path in playbacks
  74. if "/" in source_path and not source_is_capture:
  75. captures = {}
  76. if "/" in destination_path and not destination_is_playback:
  77. playbacks = {}
  78. pipewire_link = boundary_connection(
  79. port_from,
  80. port_to,
  81. captures,
  82. playbacks,
  83. lambda port: resolve_pedalboard_port(port, resolve_host_port),
  84. )
  85. if pipewire_link is not None:
  86. return "pipewire", pipewire_link
  87. source = resolve_pedalboard_port(port_from, resolve_host_port)
  88. destination = resolve_pedalboard_port(port_to, resolve_host_port)
  89. if source is None or destination is None:
  90. return None
  91. return "jack", (source, destination)
  92. def _link_status(source, destination, runner):
  93. try:
  94. result = runner(
  95. ["pw-link", "-l"],
  96. check=False,
  97. capture_output=True,
  98. text=True,
  99. )
  100. except OSError:
  101. return None
  102. if result.returncode != 0:
  103. return None
  104. current_port = None
  105. for line in result.stdout.splitlines():
  106. if line.startswith(" |-> "):
  107. if current_port == source and line.removeprefix(" |-> ") == destination:
  108. return True
  109. elif line.startswith(" |<- "):
  110. if current_port == destination and line.removeprefix(" |<- ") == source:
  111. return True
  112. else:
  113. current_port = line.strip()
  114. return False
  115. def connect_link(
  116. source,
  117. destination,
  118. runner=subprocess.run,
  119. retries=0,
  120. retry_interval=0.1,
  121. ):
  122. for attempt in range(retries + 1):
  123. try:
  124. result = runner(
  125. ["pw-link", source, destination],
  126. check=False,
  127. capture_output=True,
  128. text=True,
  129. )
  130. except OSError:
  131. return False
  132. if result.returncode == 0 or _link_status(source, destination, runner) is True:
  133. return True
  134. if attempt < retries:
  135. time.sleep(retry_interval)
  136. return False
  137. def disconnect_link(source, destination, runner=subprocess.run):
  138. try:
  139. result = runner(
  140. ["pw-link", "-d", source, destination],
  141. check=False,
  142. capture_output=True,
  143. text=True,
  144. )
  145. except OSError:
  146. return False
  147. return result.returncode == 0 or _link_status(source, destination, runner) is False