mod_ui_pipewire.py 4.5 KB

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