patch_mod_ui_rigforge.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  1. #!/usr/bin/env python3
  2. """Apply the small Rigforge integration patch to the pinned mod-ui source tree."""
  3. import sys
  4. from pathlib import Path
  5. def replace_once(path, original, replacement):
  6. text = path.read_text(encoding="utf-8")
  7. if replacement in text:
  8. return
  9. if text.count(original) != 1:
  10. raise RuntimeError(f"expected one upstream match in {path}")
  11. path.write_text(text.replace(original, replacement), encoding="utf-8")
  12. def replace_if_present(path, original, replacement):
  13. text = path.read_text(encoding="utf-8")
  14. if (replacement and replacement in text) or original not in text:
  15. return
  16. if text.count(original) != 1:
  17. raise RuntimeError(f"expected at most one upstream match in {path}")
  18. path.write_text(text.replace(original, replacement), encoding="utf-8")
  19. def replace_host_method(path, method_name, replacement):
  20. """Replace a complete Host method, independent of its upstream body variant."""
  21. text = path.read_text(encoding="utf-8")
  22. marker = f" def {method_name}("
  23. start = text.find(marker)
  24. if start < 0:
  25. raise RuntimeError(f"could not find Host.{method_name} in {path}")
  26. next_method = text.find("\n def ", start + len(marker))
  27. end = len(text) if next_method < 0 else next_method + 1
  28. replacement = replacement.rstrip() + "\n\n"
  29. updated = text[:start] + replacement + text[end:]
  30. if updated != text:
  31. path.write_text(updated, encoding="utf-8")
  32. def patch_host_connection_methods(host):
  33. """Normalize manual connection handling across old Rigforge patch variants."""
  34. replace_host_method(
  35. host,
  36. "connect",
  37. ''' def connect(self, port_from, port_to, callback):
  38. if (port_from, port_to) in self.connections:
  39. logging.info("[host] NOTE: Requested connection already exists")
  40. callback(True)
  41. return
  42. resolved = pedalboard_connection(
  43. port_from, port_to, self.rigforge_capture_ports,
  44. self.rigforge_playback_ports, self._fix_host_connection_port
  45. )
  46. if resolved is not None and resolved[0] == "pipewire":
  47. pipewire_link = resolved[1]
  48. ok = connect_link(*pipewire_link)
  49. callback(ok)
  50. if ok:
  51. self.connections.append((port_from, port_to))
  52. self.pedalboard_modified = True
  53. self.msg_callback("connect %s %s" % (port_from, port_to))
  54. else:
  55. logging.error(
  56. "[host] PipeWire backend failed to connect ports: '%s' => '%s'",
  57. pipewire_link[0], pipewire_link[1]
  58. )
  59. return
  60. def host_callback(ok):
  61. callback(ok)
  62. if ok:
  63. self.connections.append((port_from, port_to))
  64. self.msg_callback("connect %s %s" % (port_from, port_to))
  65. else:
  66. logging.error("[host] backend failed to connect ports: '%s' => '%s'", port_from, port_to)
  67. if resolved is not None:
  68. port_from_2, port_to_2 = resolved[1]
  69. else:
  70. port_from_2 = self._fix_host_connection_port(port_from)
  71. port_to_2 = self._fix_host_connection_port(port_to)
  72. self.send_modified(
  73. "connect %s %s" % (port_from_2, port_to_2),
  74. host_callback,
  75. datatype='boolean'
  76. )''',
  77. )
  78. text = host.read_text(encoding="utf-8")
  79. if " def _rigforge_pipewire_boundary(" in text:
  80. replace_host_method(
  81. host,
  82. "_rigforge_pipewire_boundary",
  83. ''' def _rigforge_pipewire_boundary(self, port_from, port_to):
  84. resolved = pedalboard_connection(
  85. port_from, port_to, self.rigforge_capture_ports,
  86. self.rigforge_playback_ports, self._fix_host_connection_port
  87. )
  88. if resolved is not None and resolved[0] == "pipewire":
  89. return resolved[1]
  90. return None''',
  91. )
  92. replace_host_method(
  93. host,
  94. "disconnect",
  95. ''' def disconnect(self, port_from, port_to, callback):
  96. resolved = pedalboard_connection(
  97. port_from, port_to, self.rigforge_capture_ports,
  98. self.rigforge_playback_ports, self._fix_host_connection_port
  99. )
  100. if resolved is not None and resolved[0] == "pipewire":
  101. pipewire_link = resolved[1]
  102. ok = disconnect_link(*pipewire_link)
  103. callback(ok)
  104. if ok:
  105. self.msg_callback("disconnect %s %s" % (port_from, port_to))
  106. self.pedalboard_modified = True
  107. try:
  108. self.connections.remove((port_from, port_to))
  109. except ValueError:
  110. logging.info(
  111. "[host] Requested '%s' => '%s' connection was not in the UI state",
  112. port_from, port_to
  113. )
  114. else:
  115. logging.error(
  116. "[host] PipeWire backend failed to disconnect ports: '%s' => '%s'",
  117. pipewire_link[0], pipewire_link[1]
  118. )
  119. return
  120. def host_callback(ok):
  121. # Upstream disconnect failures are not fatal to the UI state.
  122. callback(True)
  123. self.msg_callback("disconnect %s %s" % (port_from, port_to))
  124. if not ok:
  125. logging.error("[host] disconnect '%s' => '%s' failed", port_from, port_to)
  126. self.pedalboard_modified = True
  127. try:
  128. self.connections.remove((port_from, port_to))
  129. except ValueError:
  130. logging.info(
  131. "[host] NOTE: Requested '%s' => '%s' connection doesn't exist",
  132. port_from, port_to
  133. )
  134. if resolved is not None:
  135. port_from_2, port_to_2 = resolved[1]
  136. else:
  137. try:
  138. port_from_2 = self._fix_host_connection_port(port_from)
  139. except Exception:
  140. logging.info("[host] NOTE: Requested '%s' source port doesn't exist, assume disconnected", port_from)
  141. return host_callback(True)
  142. try:
  143. port_to_2 = self._fix_host_connection_port(port_to)
  144. except Exception:
  145. logging.info("[host] NOTE: Requested '%s' target port doesn't exist, assume disconnected", port_to)
  146. return host_callback(True)
  147. if len(self.connections) == 0:
  148. return host_callback(False)
  149. self.send_modified(
  150. "disconnect %s %s" % (port_from_2, port_to_2),
  151. host_callback,
  152. datatype='boolean'
  153. )''',
  154. )
  155. def patch_hardware_count_reporting(app):
  156. """Report Rigforge's four mapped boundaries, not only hardware RDF ports."""
  157. javascript = app / "html" / "js" / "pedalboard.js"
  158. source_text = javascript.read_text(encoding="utf-8")
  159. if "window.rigforgeBoundaryChannels = 4;" not in source_text:
  160. source_text = "window.rigforgeBoundaryChannels = 4;\n" + source_text
  161. javascript.write_text(source_text, encoding="utf-8")
  162. replace_if_present(
  163. javascript,
  164. """ for (var i=1, count=data.hardware.audio_ins; i<=count; i++) {""",
  165. """ for (var i=1, count=(window.rigforgeBoundaryChannels || data.hardware.audio_ins); i<=count; i++) {""",
  166. )
  167. replace_if_present(
  168. javascript,
  169. """ for (var i=1, count=data.hardware.audio_outs; i<=count; i++) {""",
  170. """ for (var i=1, count=(window.rigforgeBoundaryChannels || data.hardware.audio_outs); i<=count; i++) {""",
  171. )
  172. text = javascript.read_text(encoding="utf-8")
  173. text = text.replace(
  174. "if (data.hardware) {",
  175. "if (data.hardware || data.plugins) {",
  176. 1,
  177. )
  178. if text != javascript.read_text(encoding="utf-8"):
  179. if not text.endswith("\n"):
  180. text += "\n"
  181. javascript.write_text(text, encoding="utf-8")
  182. def patch_load_connections(host):
  183. """Make pedalboard restore use the same resolution as manual UI actions."""
  184. text = host.read_text(encoding="utf-8")
  185. original_text = text
  186. resolver_marker = " def _fix_host_connection_port(self, port):"
  187. resolver_start = text.find(resolver_marker)
  188. if resolver_start < 0:
  189. raise RuntimeError(f"could not locate Host._fix_host_connection_port in {host}")
  190. resolver_end = text.find(
  191. "\n def ", resolver_start + len(resolver_marker)
  192. )
  193. if resolver_end < 0:
  194. raise RuntimeError(f"could not locate end of Host._fix_host_connection_port in {host}")
  195. resolver_method = text[resolver_start:resolver_end]
  196. if (
  197. "# Rigforge bare boundary port alias; web_connect removes /graph/."
  198. not in resolver_method
  199. ):
  200. marker = ' data = port.split("/")\n'
  201. if resolver_method.count(marker) != 1:
  202. raise RuntimeError(f"could not identify port resolver body in {host}")
  203. updated_resolver = resolver_method.replace(
  204. marker,
  205. ''' # Rigforge bare boundary port alias; web_connect removes /graph/.
  206. if port in self.rigforge_capture_ports:
  207. return self.rigforge_capture_ports[port]
  208. if port in self.rigforge_playback_ports:
  209. return self.rigforge_playback_ports[port]
  210. data = port.split("/")
  211. if len(data) == 2 and data[0] == "" and (
  212. data[1] in self.rigforge_capture_ports
  213. or data[1] in self.rigforge_playback_ports
  214. ):
  215. symbol = data[1]
  216. return self.rigforge_capture_ports.get(
  217. symbol, self.rigforge_playback_ports.get(symbol)
  218. )
  219. ''',
  220. 1,
  221. )
  222. if updated_resolver == resolver_method:
  223. raise RuntimeError(f"could not add Rigforge alias to {host}")
  224. text = text[:resolver_start] + updated_resolver + text[resolver_end:]
  225. method_marker = " def load_pb_connections("
  226. method_start = text.find(method_marker)
  227. if method_start < 0:
  228. raise RuntimeError(f"could not find Host.load_pb_connections in {host}")
  229. block_start = text.find(" if doConnectionNow:", method_start)
  230. block_end = text.find(
  231. "\n elif aliasname1 is not None or aliasname2 is not None:",
  232. block_start,
  233. )
  234. if block_start < 0 or block_end < 0:
  235. raise RuntimeError(
  236. f"could not identify mod-ui pedalboard connection restoration block in {host}"
  237. )
  238. replacement = ''' if doConnectionNow:
  239. resolved = pedalboard_connection(
  240. port_from, port_to, self.rigforge_capture_ports,
  241. self.rigforge_playback_ports, self._fix_host_connection_port
  242. )
  243. if (
  244. port_from.removeprefix("/graph/") == "capture_4"
  245. and port_to.removeprefix("/graph/") == "playback_4"
  246. and self.rigforge_capture_ports
  247. and self.rigforge_playback_ports
  248. ):
  249. resolved = (
  250. "pipewire",
  251. (
  252. self.rigforge_capture_ports["capture_4"],
  253. self.rigforge_playback_ports["playback_4"],
  254. ),
  255. )
  256. if resolved is not None and resolved[0] == "pipewire":
  257. self._rigforge_restore_pipewire_boundary(
  258. resolved[1],
  259. port_from,
  260. port_to,
  261. attempts_left=60
  262. )
  263. continue
  264. try:
  265. if resolved is not None:
  266. port_from_2, port_to_2 = resolved[1]
  267. else:
  268. port_from_2 = self._fix_host_connection_port(port_from)
  269. port_to_2 = self._fix_host_connection_port(port_to)
  270. except Exception:
  271. continue
  272. self.send_notmodified("connect %s %s" % (port_from_2, port_to_2))
  273. self.connections.append((port_from, port_to))
  274. self.msg_callback("connect %s %s" % (port_from, port_to))'''
  275. updated = text[:block_start] + replacement + text[block_end:]
  276. restore_method = ''' def _rigforge_restore_pipewire_boundary(
  277. self, pipewire_link, port_from, port_to, attempts_left
  278. ):
  279. if not hasattr(self, "_rigforge_restore_attempts"):
  280. self._rigforge_restore_attempts = {}
  281. edge = (port_from, port_to)
  282. attempts_left = min(
  283. attempts_left, self._rigforge_restore_attempts.get(edge, 60)
  284. )
  285. if not (
  286. self.rigforge_capture_ports
  287. and self.rigforge_playback_ports
  288. and all(
  289. "capture_%d" % index in self.audioportsIn
  290. for index in range(1, len(self.rigforge_capture_ports) + 1)
  291. )
  292. and all(
  293. "playback_%d" % index in self.audioportsOut
  294. for index in range(1, len(self.rigforge_playback_ports) + 1)
  295. )
  296. ):
  297. ready = False
  298. else:
  299. result = subprocess.run(
  300. ["pw-link", "-o"],
  301. check=False,
  302. capture_output=True,
  303. text=True,
  304. )
  305. ready = result.returncode == 0 and all(
  306. port in result.stdout.splitlines()
  307. for port in (
  308. *self.rigforge_capture_ports.values(),
  309. *self.rigforge_playback_ports.values(),
  310. )
  311. )
  312. if edge in self.connections:
  313. self._rigforge_restore_attempts.pop(edge, None)
  314. return
  315. if not ready and attempts_left == 60:
  316. logging.error(
  317. "[host] PipeWire boundary readiness failed: inputs=%r outputs=%r "
  318. "configured_capture=%r configured_playback=%r",
  319. self.audioportsIn,
  320. self.audioportsOut,
  321. sorted(self.rigforge_capture_ports),
  322. sorted(self.rigforge_playback_ports),
  323. )
  324. if ready and connect_link(*pipewire_link):
  325. self._rigforge_restore_attempts.pop(edge, None)
  326. if edge not in self.connections:
  327. self.connections.append(edge)
  328. self.msg_callback("connect %s %s" % (port_from, port_to))
  329. return
  330. if not ready and edge not in self._rigforge_restore_attempts:
  331. logging.error(
  332. "[host] PipeWire boundary ports are not ready for restore: '%s' => '%s' "
  333. "(capture ports: %s; playback ports: %s)",
  334. pipewire_link[0],
  335. pipewire_link[1],
  336. sorted(self.rigforge_capture_ports.values()),
  337. sorted(self.rigforge_playback_ports.values()),
  338. )
  339. if attempts_left <= 1:
  340. logging.error(
  341. "[host] PipeWire backend failed to restore ports: '%s' => '%s'",
  342. pipewire_link[0], pipewire_link[1]
  343. )
  344. return
  345. self._rigforge_restore_attempts[edge] = attempts_left - 1
  346. IOLoop.instance().call_later(
  347. 0.5,
  348. self._rigforge_restore_pipewire_boundary,
  349. pipewire_link,
  350. port_from,
  351. port_to,
  352. attempts_left - 1
  353. )
  354. '''
  355. restore_marker = " def _rigforge_restore_pipewire_boundary("
  356. restore_start = updated.find(restore_marker)
  357. if restore_start >= 0:
  358. restore_end = updated.find("\n def ", restore_start + len(restore_marker))
  359. if restore_end < 0:
  360. raise RuntimeError(f"could not locate end of restore method in {host}")
  361. updated = updated[:restore_start] + restore_method.rstrip() + "\n\n" + updated[restore_end + 1:]
  362. else:
  363. marker = " def load_pb_connections("
  364. position = updated.find(marker)
  365. if position < 0:
  366. raise RuntimeError(f"could not insert PipeWire restore method in {host}")
  367. updated = updated[:position] + restore_method + updated[position:]
  368. if updated != original_text:
  369. host.write_text(updated, encoding="utf-8")
  370. def apply(source_root):
  371. app = Path(source_root)
  372. patch_hardware_count_reporting(app)
  373. helper = Path(__file__).with_name("mod_ui_pipewire.py")
  374. (app / "mod" / "rigforge_pipewire.py").write_bytes(helper.read_bytes())
  375. replace_if_present(
  376. app / "mod" / "host.py",
  377. "from mod import (\n",
  378. "from mod.rigforge_pipewire import boundary_connection, connect_link, disconnect_link, pedalboard_connection\n"
  379. "from mod import (\n",
  380. )
  381. replace_if_present(
  382. app / "mod" / "host.py",
  383. "import os, json, socket, time, logging, sys\n",
  384. "import os, json, socket, time, logging, sys, subprocess\n",
  385. )
  386. replace_if_present(
  387. app / "mod" / "host.py",
  388. ''' self.jack_hw_capture_prefix = "mod-host:out" if self.descriptor.get('has_noisegate', False) else "system:capture_"
  389. ''',
  390. ''' self.jack_hw_capture_prefix = "mod-host:out" if self.descriptor.get('has_noisegate', False) else "system:capture_"
  391. self.rigforge_capture_ports = {
  392. "capture_%d" % (index + 1): port
  393. for index, port in enumerate(
  394. os.environ.get("MOD_RIGFORGE_CAPTURE_PORTS", "").split(",")
  395. )
  396. if port
  397. }
  398. self.rigforge_playback_ports = {
  399. "playback_%d" % (index + 1): port
  400. for index, port in enumerate(
  401. os.environ.get("MOD_RIGFORGE_PLAYBACK_PORTS", "").split(",")
  402. )
  403. if port
  404. }
  405. ''',
  406. )
  407. replace_if_present(
  408. app / "mod" / "host.py",
  409. ''' self.rigforge_capture_port = os.environ.get("MOD_RIGFORGE_CAPTURE_PORT")
  410. self.rigforge_playback_port = os.environ.get("MOD_RIGFORGE_PLAYBACK_PORT")
  411. ''',
  412. "",
  413. )
  414. replace_if_present(
  415. app / "mod" / "host.py",
  416. ''' if self.rigforge_capture_port and self.rigforge_playback_port:
  417. # These graph symbols map to Rigforge's stable virtual PipeWire boundary ports.
  418. self.audioportsIn = ["capture_1"]
  419. self.audioportsOut = ["playback_1"]
  420. ''',
  421. ''' if self.rigforge_capture_ports and self.rigforge_playback_ports:
  422. # These graph symbols map to Rigforge's stable virtual PipeWire boundary ports.
  423. self.audioportsIn = [
  424. "capture_%d" % index
  425. for index in range(1, len(self.rigforge_capture_ports) + 1)
  426. ]
  427. self.audioportsOut = [
  428. "playback_%d" % index
  429. for index in range(1, len(self.rigforge_playback_ports) + 1)
  430. ]
  431. ''',
  432. )
  433. replace_once(
  434. app / "mod" / "host.py",
  435. ''' for port in get_jack_hardware_ports(True, False):
  436. client_name, port_name = port.split(":",1)
  437. if client_name == "mod-spi2jack":
  438. cv_port_name = CV_PREFIX + port_name
  439. self.cvportsIn.append(cv_port_name)
  440. self.addressings.add_hw_cv_port('/cv/graph/' + cv_port_name)
  441. else:
  442. self.audioportsIn.append(port_name)
  443. for port in get_jack_hardware_ports(True, True):
  444. client_name, port_name = port.split(":",1)
  445. if client_name == "mod-jack2spi":
  446. self.cvportsOut.append(CV_PREFIX + port_name)
  447. else:
  448. self.audioportsOut.append(port_name)
  449. ''',
  450. ''' if self.rigforge_capture_ports and self.rigforge_playback_ports:
  451. # These graph symbols map to Rigforge's stable virtual PipeWire boundary ports.
  452. self.audioportsIn = [
  453. "capture_%d" % index
  454. for index in range(1, len(self.rigforge_capture_ports) + 1)
  455. ]
  456. self.audioportsOut = [
  457. "playback_%d" % index
  458. for index in range(1, len(self.rigforge_playback_ports) + 1)
  459. ]
  460. else:
  461. for port in get_jack_hardware_ports(True, False):
  462. client_name, port_name = port.split(":",1)
  463. if client_name == "mod-spi2jack":
  464. cv_port_name = CV_PREFIX + port_name
  465. self.cvportsIn.append(cv_port_name)
  466. self.addressings.add_hw_cv_port('/cv/graph/' + cv_port_name)
  467. else:
  468. self.audioportsIn.append(port_name)
  469. for port in get_jack_hardware_ports(True, True):
  470. client_name, port_name = port.split(":",1)
  471. if client_name == "mod-jack2spi":
  472. self.cvportsOut.append(CV_PREFIX + port_name)
  473. else:
  474. self.audioportsOut.append(port_name)
  475. ''',
  476. )
  477. replace_if_present(
  478. app / "mod" / "host.py",
  479. ''' if data[2] == "capture_1" and self.rigforge_capture_port:
  480. return self.rigforge_capture_port
  481. if data[2] == "playback_1" and self.rigforge_playback_port:
  482. return self.rigforge_playback_port
  483. ''',
  484. ''' if data[2] in self.rigforge_capture_ports:
  485. return self.rigforge_capture_ports[data[2]]
  486. if data[2] in self.rigforge_playback_ports:
  487. return self.rigforge_playback_ports[data[2]]
  488. ''',
  489. )
  490. replace_if_present(
  491. app / "mod" / "host.py",
  492. "import os, json, socket, time, logging, sys\n",
  493. "import os, json, socket, time, logging, sys, subprocess\n",
  494. )
  495. host_file = app / "mod" / "host.py"
  496. patch_load_connections(host_file)
  497. patch_host_connection_methods(host_file)
  498. replace_once(
  499. host_file,
  500. ''' if bundlepath and (bundlepath.startswith(LV2_PEDALBOARDS_DIR) or
  501. bundlepath.startswith(LV2_FACTORY_PEDALBOARDS_DIR)):
  502. save_last_bank_and_pedalboard(self.bank_id, bundlepath)
  503. else:
  504. save_last_bank_and_pedalboard(0, "")
  505. ''',
  506. ''' if bundlepath and (
  507. bundlepath.startswith(LV2_PEDALBOARDS_DIR)
  508. or bundlepath.startswith(LV2_FACTORY_PEDALBOARDS_DIR)
  509. or bundlepath.startswith(os.path.join(DATA_DIR, "references"))
  510. ):
  511. save_last_bank_and_pedalboard(self.bank_id, bundlepath)
  512. else:
  513. save_last_bank_and_pedalboard(0, "")
  514. ''',
  515. )
  516. replace_if_present(
  517. app / "mod" / "session.py",
  518. ''' # Set a plugin block position within the canvas
  519. ''',
  520. ''' def ws_rigforge_patch_set(self, request_id, instance, uri, valuetype, valuedata, ws):
  521. if valuetype != "p":
  522. ws.write_message("rigforge_patch_set_ack %s 0" % request_id)
  523. return
  524. def completed(ok):
  525. status = 1 if ok else 0
  526. if ok:
  527. self.msg_callback_broadcast(
  528. "patch_set %s 1 %s %c %s" % (instance, uri, valuetype, valuedata),
  529. ws,
  530. )
  531. ws.write_message("rigforge_patch_set_ack %s %d" % (request_id, status))
  532. self.host.patch_set(instance, uri, valuedata, completed)
  533. # Set a plugin block position within the canvas
  534. ''',
  535. )
  536. replace_if_present(
  537. app / "mod" / "webserver.py",
  538. ''' elif cmd == "plugin_pos":
  539. ''',
  540. ''' elif cmd == "rigforge_patch_set":
  541. data = data[1].split(" ", 4)
  542. if len(data) != 5:
  543. self.write_message("rigforge_patch_set_ack invalid 0")
  544. return
  545. request_id, inst, uri, vtype, value = data
  546. if not request_id.isalnum():
  547. self.write_message("rigforge_patch_set_ack invalid 0")
  548. return
  549. SESSION.ws_rigforge_patch_set(
  550. request_id, inst, uri, vtype, value, self
  551. )
  552. elif cmd == "plugin_pos":
  553. ''',
  554. )
  555. replace_if_present(
  556. app / "server.py",
  557. "os.environ['MOD_DEVICE_WEBSERVER_PORT'] = '8888'",
  558. "os.environ.setdefault('MOD_DEVICE_WEBSERVER_PORT', '8888')",
  559. )
  560. replace_if_present(
  561. app / "server.py",
  562. "os.environ['MOD_DEFAULT_PEDALBOARD'] = os.path.join(ROOT, 'default.pedalboard')",
  563. "os.environ.setdefault('MOD_DEFAULT_PEDALBOARD', os.path.join(ROOT, 'default.pedalboard'))",
  564. )
  565. replace_if_present(
  566. app / "html" / "js" / "pedalboard.js",
  567. ''' var captureTitles = {
  568. 1: 'Guitar - Mod-UI Input',
  569. 2: 'Bass - Mod-UI Input',
  570. 3: 'Micro - Mod-UI Input',
  571. 4: 'Synth - Mod-UI Input'
  572. }
  573. var captureTitle = captureTitles[i] || ('Hardware Capture ' + i)
  574. var hw = $('<div class="ignore-arrive hardware-output" mod-port-index="' + i + '" title="' + captureTitle + '">')
  575. ''',
  576. ''' var captureTitles = {
  577. 1: 'Guitare - Entrée mod-ui',
  578. 2: 'Basse - Entrée mod-ui',
  579. 3: 'Micro - Entrée mod-ui',
  580. 4: 'Synthé - Entrée mod-ui'
  581. }
  582. var captureTitle = captureTitles[i] || ('Hardware Capture ' + i)
  583. var hw = $('<div class="ignore-arrive hardware-output" mod-port-index="' + i + '" title="' + captureTitle + '">')
  584. ''',
  585. )
  586. replace_if_present(
  587. app / "html" / "js" / "pedalboard.js",
  588. ''' var playbackTitles = {
  589. 1: 'Guitar - Mod-UI Output',
  590. 2: 'Bass - Mod-UI Output',
  591. 3: 'Micro - Mod-UI Output',
  592. 4: 'Synth - Mod-UI Output'
  593. }
  594. var playbackTitle = playbackTitles[i] || ('Hardware Playback ' + i)
  595. var hw = $('<div class="ignore-arrive hardware-input" mod-port-index="' + i + '" title="' + playbackTitle + '">')
  596. ''',
  597. ''' var playbackTitles = {
  598. 1: 'Guitare - Sortie mod-ui',
  599. 2: 'Basse - Sortie mod-ui',
  600. 3: 'Micro - Sortie mod-ui',
  601. 4: 'Synthé - Sortie mod-ui'
  602. }
  603. var playbackTitle = playbackTitles[i] || ('Hardware Playback ' + i)
  604. var hw = $('<div class="ignore-arrive hardware-input" mod-port-index="' + i + '" title="' + playbackTitle + '">')
  605. ''',
  606. )
  607. replace_if_present(
  608. app / "html" / "js" / "pedalboard.js",
  609. ''' var captureTitle = (i === 1) ? 'Rigforge Mod-Host Input' : ('Hardware Capture ' + i)
  610. var hw = $('<div class="ignore-arrive hardware-output" mod-port-index="' + i + '" title="' + captureTitle + '">')
  611. ''',
  612. ''' var captureTitles = {
  613. 1: 'Guitare - Entrée mod-ui',
  614. 2: 'Basse - Entrée mod-ui',
  615. 3: 'Micro - Entrée mod-ui',
  616. 4: 'Synthé - Entrée mod-ui'
  617. }
  618. var captureTitle = captureTitles[i] || ('Hardware Capture ' + i)
  619. var hw = $('<div class="ignore-arrive hardware-output" mod-port-index="' + i + '" title="' + captureTitle + '">')
  620. ''',
  621. )
  622. replace_if_present(
  623. app / "html" / "js" / "pedalboard.js",
  624. ''' var playbackTitle = (i === 1) ? 'Rigforge Mod-Host Output' : ('Hardware Playback ' + i)
  625. var hw = $('<div class="ignore-arrive hardware-input" mod-port-index="' + i + '" title="' + playbackTitle + '">')
  626. ''',
  627. ''' var playbackTitles = {
  628. 1: 'Guitare - Sortie mod-ui',
  629. 2: 'Basse - Sortie mod-ui',
  630. 3: 'Micro - Sortie mod-ui',
  631. 4: 'Synthé - Sortie mod-ui'
  632. }
  633. var playbackTitle = playbackTitles[i] || ('Hardware Playback ' + i)
  634. var hw = $('<div class="ignore-arrive hardware-input" mod-port-index="' + i + '" title="' + playbackTitle + '">')
  635. ''',
  636. )
  637. replace_if_present(
  638. app / "html" / "js" / "pedalboard.js",
  639. ''' var hw = $('<div class="ignore-arrive hardware-output" mod-port-index="' + i + '" title="Hardware Capture ' + i + '">')
  640. ''',
  641. ''' var captureTitles = {
  642. 1: 'Guitare - Entrée mod-ui',
  643. 2: 'Basse - Entrée mod-ui',
  644. 3: 'Micro - Entrée mod-ui',
  645. 4: 'Synthé - Entrée mod-ui'
  646. }
  647. var captureTitle = captureTitles[i] || ('Hardware Capture ' + i)
  648. var hw = $('<div class="ignore-arrive hardware-output" mod-port-index="' + i + '" title="' + captureTitle + '">')
  649. ''',
  650. )
  651. replace_if_present(
  652. app / "html" / "js" / "pedalboard.js",
  653. ''' var hw = $('<div class="ignore-arrive hardware-input" mod-port-index="' + i + '" title="Hardware Playback ' + i + '">')
  654. ''',
  655. ''' var playbackTitles = {
  656. 1: 'Guitare - Sortie mod-ui',
  657. 2: 'Basse - Sortie mod-ui',
  658. 3: 'Micro - Sortie mod-ui',
  659. 4: 'Synthé - Sortie mod-ui'
  660. }
  661. var playbackTitle = playbackTitles[i] || ('Hardware Playback ' + i)
  662. var hw = $('<div class="ignore-arrive hardware-input" mod-port-index="' + i + '" title="' + playbackTitle + '">')
  663. ''',
  664. )
  665. replace_if_present(
  666. app / "html" / "js" / "pedalboard.js",
  667. "symbol = (i < 3) ? ('/graph/capture_' + i) : ('/graph/audio_from_slave_' + (i-2))",
  668. "symbol = '/graph/capture_' + i",
  669. )
  670. replace_if_present(
  671. app / "html" / "js" / "pedalboard.js",
  672. "symbol = (i < 3) ? ('/graph/playback_' + i) : ('/graph/audio_to_slave_' + (i-2))",
  673. "symbol = '/graph/playback_' + i",
  674. )
  675. js_file = app / "html" / "js" / "pedalboard.js"
  676. if not js_file.read_bytes().endswith(b"\n"):
  677. js_file.write_bytes(js_file.read_bytes() + b"\n")
  678. if __name__ == "__main__":
  679. if len(sys.argv) != 2:
  680. raise SystemExit("Usage: patch_mod_ui_rigforge.py path/to/mod-ui/app")
  681. apply(Path(sys.argv[1]))