Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

602 lines
21KB

  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Carla plugin host (plugin UI)
  4. # Copyright (C) 2013-2019 Filipe Coelho <falktx@falktx.com>
  5. #
  6. # This program is free software; you can redistribute it and/or
  7. # modify it under the terms of the GNU General Public License as
  8. # published by the Free Software Foundation; either version 2 of
  9. # the License, or any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # For a full copy of the GNU General Public License see the GPL.txt file
  17. # ------------------------------------------------------------------------------------------------------------
  18. # Imports (Global)
  19. from PyQt5.QtGui import QKeySequence, QMouseEvent
  20. from PyQt5.QtWidgets import QSplitter
  21. # ------------------------------------------------------------------------------------------------------------
  22. # Imports (Custom Stuff)
  23. from carla_host import *
  24. from externalui import ExternalUI
  25. # ------------------------------------------------------------------------------------------------------------
  26. # Host Plugin object
  27. class PluginHost(CarlaHostQtPlugin):
  28. def __init__(self):
  29. CarlaHostQtPlugin.__init__(self)
  30. if False:
  31. # kdevelop likes this :)
  32. self.fExternalUI = ExternalUI()
  33. # ---------------------------------------------------------------
  34. self.fExternalUI = None
  35. # -------------------------------------------------------------------
  36. def setExternalUI(self, extUI):
  37. self.fExternalUI = extUI
  38. def sendMsg(self, lines):
  39. if self.fExternalUI is None:
  40. return False
  41. return self.fExternalUI.send(lines)
  42. # -------------------------------------------------------------------
  43. def engine_init(self, driverName, clientName):
  44. return True
  45. def engine_close(self):
  46. return True
  47. def engine_idle(self):
  48. self.fExternalUI.idleExternalUI()
  49. def is_engine_running(self):
  50. if self.fExternalUI is None:
  51. return False
  52. return self.fExternalUI.isRunning()
  53. def set_engine_about_to_close(self):
  54. return True
  55. def get_host_osc_url_tcp(self):
  56. return self.tr("(OSC TCP port not provided in Plugin version)")
  57. # ------------------------------------------------------------------------------------------------------------
  58. # Main Window
  59. class CarlaMiniW(ExternalUI, HostWindow):
  60. def __init__(self, host, isPatchbay, parent=None):
  61. ExternalUI.__init__(self)
  62. HostWindow.__init__(self, host, isPatchbay, parent)
  63. if False:
  64. # kdevelop likes this :)
  65. host = PluginHost()
  66. self.host = host
  67. host.setExternalUI(self)
  68. self.fFirstInit = True
  69. self.setWindowTitle(self.fUiName)
  70. self.ready()
  71. # Override this as it can be called from several places.
  72. # We really need to close all UIs as events are driven by host idle which is only available when UI is visible
  73. def closeExternalUI(self):
  74. for i in reversed(range(self.fPluginCount)):
  75. self.host.show_custom_ui(i, False)
  76. ExternalUI.closeExternalUI(self)
  77. # -------------------------------------------------------------------
  78. # ExternalUI Callbacks
  79. def uiShow(self):
  80. if self.parent() is not None:
  81. return
  82. self.show()
  83. def uiFocus(self):
  84. if self.parent() is not None:
  85. return
  86. self.setWindowState((self.windowState() & ~Qt.WindowMinimized) | Qt.WindowActive)
  87. self.show()
  88. self.raise_()
  89. self.activateWindow()
  90. def uiHide(self):
  91. if self.parent() is not None:
  92. return
  93. self.hide()
  94. def uiQuit(self):
  95. self.closeExternalUI()
  96. self.close()
  97. if self != gui:
  98. gui.close()
  99. # there might be other qt windows open which will block carla-plugin from quitting
  100. app.quit()
  101. def uiTitleChanged(self, uiTitle):
  102. self.setWindowTitle(uiTitle)
  103. # -------------------------------------------------------------------
  104. # Qt events
  105. def closeEvent(self, event):
  106. self.closeExternalUI()
  107. HostWindow.closeEvent(self, event)
  108. # there might be other qt windows open which will block carla-plugin from quitting
  109. app.quit()
  110. # -------------------------------------------------------------------
  111. # Custom callback
  112. def msgCallback(self, msg):
  113. try:
  114. self.msgCallback2(msg)
  115. except:
  116. print("msgCallback error, skipped for", msg)
  117. def msgCallback2(self, msg):
  118. msg = charPtrToString(msg)
  119. #if not msg:
  120. #return
  121. if msg == "runtime-info":
  122. values = self.readlineblock().split(":")
  123. load = float(values[0])
  124. xruns = int(values[1])
  125. self.host._set_runtime_info(load, xruns)
  126. elif msg == "transport":
  127. playing = bool(self.readlineblock() == "true")
  128. frame, bar, beat, tick = [int(i) for i in self.readlineblock().split(":")]
  129. bpm = float(self.readlineblock())
  130. self.host._set_transport(playing, frame, bar, beat, tick, bpm)
  131. elif msg.startswith("PEAKS_"):
  132. pluginId = int(msg.replace("PEAKS_", ""))
  133. in1, in2, out1, out2 = [float(i) for i in self.readlineblock().split(":")]
  134. self.host._set_peaks(pluginId, in1, in2, out1, out2)
  135. elif msg.startswith("PARAMVAL_"):
  136. pluginId, paramId = [int(i) for i in msg.replace("PARAMVAL_", "").split(":")]
  137. paramValue = float(self.readlineblock())
  138. if paramId < 0:
  139. self.host._set_internalValue(pluginId, paramId, paramValue)
  140. else:
  141. self.host._set_parameterValue(pluginId, paramId, paramValue)
  142. elif msg.startswith("ENGINE_CALLBACK_"):
  143. action = int(msg.replace("ENGINE_CALLBACK_", ""))
  144. pluginId = int(self.readlineblock())
  145. value1 = int(self.readlineblock())
  146. value2 = int(self.readlineblock())
  147. value3 = int(self.readlineblock())
  148. valuef = float(self.readlineblock())
  149. valueStr = self.readlineblock().replace("\r", "\n")
  150. self.host._setViaCallback(action, pluginId, value1, value2, value3, valuef, valueStr)
  151. engineCallback(self.host, action, pluginId, value1, value2, value3, valuef, valueStr)
  152. elif msg.startswith("ENGINE_OPTION_"):
  153. option = int(msg.replace("ENGINE_OPTION_", ""))
  154. forced = bool(self.readlineblock() == "true")
  155. value = self.readlineblock()
  156. if self.fFirstInit and not forced:
  157. return
  158. if option == ENGINE_OPTION_PROCESS_MODE:
  159. self.host.processMode = int(value)
  160. elif option == ENGINE_OPTION_TRANSPORT_MODE:
  161. self.host.transportMode = int(value)
  162. elif option == ENGINE_OPTION_FORCE_STEREO:
  163. self.host.forceStereo = bool(value == "true")
  164. elif option == ENGINE_OPTION_PREFER_PLUGIN_BRIDGES:
  165. self.host.preferPluginBridges = bool(value == "true")
  166. elif option == ENGINE_OPTION_PREFER_UI_BRIDGES:
  167. self.host.preferUIBridges = bool(value == "true")
  168. elif option == ENGINE_OPTION_UIS_ALWAYS_ON_TOP:
  169. self.host.uisAlwaysOnTop = bool(value == "true")
  170. elif option == ENGINE_OPTION_MAX_PARAMETERS:
  171. self.host.maxParameters = int(value)
  172. elif option == ENGINE_OPTION_UI_BRIDGES_TIMEOUT:
  173. self.host.uiBridgesTimeout = int(value)
  174. elif option == ENGINE_OPTION_PATH_BINARIES:
  175. self.host.pathBinaries = value
  176. elif option == ENGINE_OPTION_PATH_RESOURCES:
  177. self.host.pathResources = value
  178. elif msg.startswith("PLUGIN_INFO_"):
  179. pluginId = int(msg.replace("PLUGIN_INFO_", ""))
  180. self.host._add(pluginId)
  181. type_, category, hints, uniqueId, optsAvail, optsEnabled = [int(i) for i in self.readlineblock().split(":")]
  182. filename = self.readlineblock().replace("\r", "\n")
  183. name = self.readlineblock().replace("\r", "\n")
  184. iconName = self.readlineblock().replace("\r", "\n")
  185. realName = self.readlineblock().replace("\r", "\n")
  186. label = self.readlineblock().replace("\r", "\n")
  187. maker = self.readlineblock().replace("\r", "\n")
  188. copyright = self.readlineblock().replace("\r", "\n")
  189. pinfo = {
  190. 'type': type_,
  191. 'category': category,
  192. 'hints': hints,
  193. 'optionsAvailable': optsAvail,
  194. 'optionsEnabled': optsEnabled,
  195. 'filename': filename,
  196. 'name': name,
  197. 'label': label,
  198. 'maker': maker,
  199. 'copyright': copyright,
  200. 'iconName': iconName,
  201. 'patchbayClientId': 0,
  202. 'uniqueId': uniqueId
  203. }
  204. self.host._set_pluginInfo(pluginId, pinfo)
  205. self.host._set_pluginRealName(pluginId, realName)
  206. elif msg.startswith("AUDIO_COUNT_"):
  207. pluginId, ins, outs = [int(i) for i in msg.replace("AUDIO_COUNT_", "").split(":")]
  208. self.host._set_audioCountInfo(pluginId, {'ins': ins, 'outs': outs})
  209. elif msg.startswith("MIDI_COUNT_"):
  210. pluginId, ins, outs = [int(i) for i in msg.replace("MIDI_COUNT_", "").split(":")]
  211. self.host._set_midiCountInfo(pluginId, {'ins': ins, 'outs': outs})
  212. elif msg.startswith("PARAMETER_COUNT_"):
  213. pluginId, ins, outs, count = [int(i) for i in msg.replace("PARAMETER_COUNT_", "").split(":")]
  214. self.host._set_parameterCountInfo(pluginId, count, {'ins': ins, 'outs': outs})
  215. elif msg.startswith("PARAMETER_DATA_"):
  216. pluginId, paramId = [int(i) for i in msg.replace("PARAMETER_DATA_", "").split(":")]
  217. paramType, paramHints, midiChannel, midiCC = [int(i) for i in self.readlineblock().split(":")]
  218. paramName = self.readlineblock().replace("\r", "\n")
  219. paramUnit = self.readlineblock().replace("\r", "\n")
  220. paramInfo = {
  221. 'name': paramName,
  222. 'symbol': "",
  223. 'unit': paramUnit,
  224. 'comment': paramUnit,
  225. 'groupName': paramUnit,
  226. 'scalePointCount': 0,
  227. }
  228. self.host._set_parameterInfo(pluginId, paramId, paramInfo)
  229. paramData = {
  230. 'type': paramType,
  231. 'hints': paramHints,
  232. 'index': paramId,
  233. 'rindex': -1,
  234. 'midiCC': midiCC,
  235. 'midiChannel': midiChannel
  236. }
  237. self.host._set_parameterData(pluginId, paramId, paramData)
  238. elif msg.startswith("PARAMETER_RANGES_"):
  239. pluginId, paramId = [int(i) for i in msg.replace("PARAMETER_RANGES_", "").split(":")]
  240. def_, min_, max_, step, stepSmall, stepLarge = [float(i) for i in self.readlineblock().split(":")]
  241. paramRanges = {
  242. 'def': def_,
  243. 'min': min_,
  244. 'max': max_,
  245. 'step': step,
  246. 'stepSmall': stepSmall,
  247. 'stepLarge': stepLarge
  248. }
  249. self.host._set_parameterRanges(pluginId, paramId, paramRanges)
  250. elif msg.startswith("PROGRAM_COUNT_"):
  251. pluginId, count, current = [int(i) for i in msg.replace("PROGRAM_COUNT_", "").split(":")]
  252. self.host._set_programCount(pluginId, count)
  253. self.host._set_currentProgram(pluginId, current)
  254. elif msg.startswith("PROGRAM_NAME_"):
  255. pluginId, progId = [int(i) for i in msg.replace("PROGRAM_NAME_", "").split(":")]
  256. progName = self.readlineblock().replace("\r", "\n")
  257. self.host._set_programName(pluginId, progId, progName)
  258. elif msg.startswith("MIDI_PROGRAM_COUNT_"):
  259. pluginId, count, current = [int(i) for i in msg.replace("MIDI_PROGRAM_COUNT_", "").split(":")]
  260. self.host._set_midiProgramCount(pluginId, count)
  261. self.host._set_currentMidiProgram(pluginId, current)
  262. elif msg.startswith("MIDI_PROGRAM_DATA_"):
  263. pluginId, midiProgId = [int(i) for i in msg.replace("MIDI_PROGRAM_DATA_", "").split(":")]
  264. bank, program = [int(i) for i in self.readlineblock().split(":")]
  265. name = self.readlineblock().replace("\r", "\n")
  266. self.host._set_midiProgramData(pluginId, midiProgId, {'bank': bank, 'program': program, 'name': name})
  267. elif msg.startswith("CUSTOM_DATA_COUNT_"):
  268. pluginId, count = [int(i) for i in msg.replace("CUSTOM_DATA_COUNT_", "").split(":")]
  269. self.host._set_customDataCount(pluginId, count)
  270. elif msg.startswith("CUSTOM_DATA_"):
  271. pluginId, customDataId = [int(i) for i in msg.replace("CUSTOM_DATA_", "").split(":")]
  272. type_ = self.readlineblock().replace("\r", "\n")
  273. key = self.readlineblock().replace("\r", "\n")
  274. value = self.readlineblock().replace("\r", "\n")
  275. self.host._set_customData(pluginId, customDataId, {'type': type_, 'key': key, 'value': value})
  276. elif msg == "osc-urls":
  277. tcp = self.readlineblock().replace("\r", "\n")
  278. udp = self.readlineblock().replace("\r", "\n")
  279. self.host.fOscTCP = tcp
  280. self.host.fOscUDP = udp
  281. elif msg == "max-plugin-number":
  282. maxnum = int(self.readlineblock())
  283. self.host.fMaxPluginNumber = maxnum
  284. elif msg == "buffer-size":
  285. bufsize = int(self.readlineblock())
  286. self.host.fBufferSize = bufsize
  287. elif msg == "sample-rate":
  288. srate = float(self.readlineblock())
  289. self.host.fSampleRate = srate
  290. elif msg == "error":
  291. error = self.readlineblock().replace("\r", "\n")
  292. engineCallback(self.host, ENGINE_CALLBACK_ERROR, 0, 0, 0, 0, 0.0, error)
  293. elif msg == "show":
  294. self.fFirstInit = False
  295. self.uiShow()
  296. elif msg == "focus":
  297. self.uiFocus()
  298. elif msg == "hide":
  299. self.uiHide()
  300. elif msg == "quit":
  301. self.fQuitReceived = True
  302. self.uiQuit()
  303. elif msg == "uiTitle":
  304. uiTitle = self.readlineblock().replace("\r", "\n")
  305. self.uiTitleChanged(uiTitle)
  306. else:
  307. print("unknown message: \"" + msg + "\"")
  308. # ------------------------------------------------------------------------------------------------------------
  309. # Embed Widget
  310. class QEmbedWidget(QWidget):
  311. def __init__(self, winId):
  312. QWidget.__init__(self)
  313. self.setAttribute(Qt.WA_LayoutUsesWidgetRect)
  314. self.move(0, 0)
  315. self.fPos = (0, 0)
  316. self.fWinId = 0
  317. def finalSetup(self, gui, winId):
  318. self.fWinId = int(self.winId())
  319. gui.ui.centralwidget.installEventFilter(self)
  320. gui.ui.menubar.installEventFilter(self)
  321. gCarla.utils.x11_reparent_window(self.fWinId, winId)
  322. self.show()
  323. def fixPosition(self):
  324. pos = gCarla.utils.x11_get_window_pos(self.fWinId)
  325. if self.fPos == pos:
  326. return
  327. self.fPos = pos
  328. self.move(pos[0], pos[1])
  329. gCarla.utils.x11_move_window(self.fWinId, pos[2], pos[3])
  330. def eventFilter(self, obj, ev):
  331. if isinstance(ev, QMouseEvent):
  332. self.fixPosition()
  333. return False
  334. def enterEvent(self, ev):
  335. self.fixPosition()
  336. QWidget.enterEvent(self, ev)
  337. # ------------------------------------------------------------------------------------------------------------
  338. # Embed plugin UI
  339. class CarlaEmbedW(QEmbedWidget):
  340. def __init__(self, host, winId, isPatchbay):
  341. QEmbedWidget.__init__(self, winId)
  342. if False:
  343. host = CarlaHostPlugin()
  344. self.host = host
  345. self.fWinId = winId
  346. self.setFixedSize(1024, 712)
  347. self.fLayout = QVBoxLayout(self)
  348. self.fLayout.setContentsMargins(0, 0, 0, 0)
  349. self.fLayout.setSpacing(0)
  350. self.setLayout(self.fLayout)
  351. self.gui = CarlaMiniW(host, isPatchbay, self)
  352. self.gui.hide()
  353. self.gui.ui.act_file_quit.setEnabled(False)
  354. self.gui.ui.act_file_quit.setVisible(False)
  355. self.fShortcutActions = []
  356. self.addShortcutActions(self.gui.ui.menu_File.actions())
  357. self.addShortcutActions(self.gui.ui.menu_Plugin.actions())
  358. self.addShortcutActions(self.gui.ui.menu_PluginMacros.actions())
  359. self.addShortcutActions(self.gui.ui.menu_Settings.actions())
  360. self.addShortcutActions(self.gui.ui.menu_Help.actions())
  361. if self.host.processMode == ENGINE_PROCESS_MODE_PATCHBAY:
  362. self.addShortcutActions(self.gui.ui.menu_Canvas.actions())
  363. self.addShortcutActions(self.gui.ui.menu_Canvas_Zoom.actions())
  364. self.addWidget(self.gui.ui.menubar)
  365. self.addLine()
  366. self.addWidget(self.gui.ui.toolBar)
  367. if self.host.processMode == ENGINE_PROCESS_MODE_PATCHBAY:
  368. self.addLine()
  369. self.fCentralSplitter = QSplitter(self)
  370. policy = self.fCentralSplitter.sizePolicy()
  371. policy.setVerticalStretch(1)
  372. self.fCentralSplitter.setSizePolicy(policy)
  373. self.addCentralWidget(self.gui.ui.dockWidget)
  374. self.addCentralWidget(self.gui.centralWidget())
  375. self.fLayout.addWidget(self.fCentralSplitter)
  376. self.finalSetup(self.gui, winId)
  377. def addShortcutActions(self, actions):
  378. for action in actions:
  379. if not action.shortcut().isEmpty():
  380. self.fShortcutActions.append(action)
  381. def addWidget(self, widget):
  382. widget.setParent(self)
  383. self.fLayout.addWidget(widget)
  384. def addCentralWidget(self, widget):
  385. widget.setParent(self)
  386. self.fCentralSplitter.addWidget(widget)
  387. def addLine(self):
  388. line = QFrame(self)
  389. line.setFrameShadow(QFrame.Sunken)
  390. line.setFrameShape(QFrame.HLine)
  391. line.setLineWidth(0)
  392. line.setMidLineWidth(1)
  393. self.fLayout.addWidget(line)
  394. def keyPressEvent(self, event):
  395. modifiers = event.modifiers()
  396. modifiersStr = ""
  397. if modifiers & Qt.ShiftModifier:
  398. modifiersStr += "Shift+"
  399. if modifiers & Qt.ControlModifier:
  400. modifiersStr += "Ctrl+"
  401. if modifiers & Qt.AltModifier:
  402. modifiersStr += "Alt+"
  403. if modifiers & Qt.MetaModifier:
  404. modifiersStr += "Meta+"
  405. keyStr = QKeySequence(event.key()).toString()
  406. keySeq = QKeySequence(modifiersStr + keyStr)
  407. for action in self.fShortcutActions:
  408. if not action.isEnabled():
  409. continue
  410. if keySeq.matches(action.shortcut()) != QKeySequence.ExactMatch:
  411. continue
  412. event.accept()
  413. action.trigger()
  414. return
  415. QEmbedWidget.keyPressEvent(self, event)
  416. def showEvent(self, event):
  417. QEmbedWidget.showEvent(self, event)
  418. # set our gui as parent for all plugins UIs
  419. if self.host.manageUIs:
  420. winIdStr = "%x" % self.fWinId
  421. self.host.set_engine_option(ENGINE_OPTION_FRONTEND_WIN_ID, 0, winIdStr)
  422. def hideEvent(self, event):
  423. # disable parent
  424. self.host.set_engine_option(ENGINE_OPTION_FRONTEND_WIN_ID, 0, "0")
  425. QEmbedWidget.hideEvent(self, event)
  426. def closeEvent(self, event):
  427. self.gui.close()
  428. self.gui.closeExternalUI()
  429. QEmbedWidget.closeEvent(self, event)
  430. # there might be other qt windows open which will block carla-plugin from quitting
  431. app.quit()
  432. def setLoadRDFsNeeded(self):
  433. self.gui.setLoadRDFsNeeded()
  434. # ------------------------------------------------------------------------------------------------------------
  435. # Main
  436. if __name__ == '__main__':
  437. # -------------------------------------------------------------
  438. # App initialization
  439. app = CarlaApplication("Carla2-Plugin")
  440. # -------------------------------------------------------------
  441. # Set-up custom signal handling
  442. setUpSignals()
  443. # -------------------------------------------------------------
  444. # Init host backend
  445. isPatchbay = sys.argv[0].rsplit(os.path.sep)[-1].lower().replace(".exe","") == "carla-plugin-patchbay"
  446. host = initHost("Carla-Plugin", None, False, True, True, PluginHost)
  447. host.processMode = ENGINE_PROCESS_MODE_PATCHBAY if isPatchbay else ENGINE_PROCESS_MODE_CONTINUOUS_RACK
  448. host.processModeForced = True
  449. host.nextProcessMode = host.processMode
  450. loadHostSettings(host)
  451. # -------------------------------------------------------------
  452. # Create GUI
  453. try:
  454. winId = int(os.getenv("CARLA_PLUGIN_EMBED_WINID"))
  455. except:
  456. winId = 0
  457. gCarla.utils.setenv("CARLA_PLUGIN_EMBED_WINID", "0")
  458. if LINUX and winId != 0:
  459. gui = CarlaEmbedW(host, winId, isPatchbay)
  460. else:
  461. gui = CarlaMiniW(host, isPatchbay)
  462. # -------------------------------------------------------------
  463. # App-Loop
  464. app.exit_exec()