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.

604 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. paramComment = self.readlineblock().replace("\r", "\n")
  221. paramGroupName = self.readlineblock().replace("\r", "\n")
  222. paramInfo = {
  223. 'name': paramName,
  224. 'symbol': "",
  225. 'unit': paramUnit,
  226. 'comment': paramComment,
  227. 'groupName': paramGroupName,
  228. 'scalePointCount': 0,
  229. }
  230. self.host._set_parameterInfo(pluginId, paramId, paramInfo)
  231. paramData = {
  232. 'type': paramType,
  233. 'hints': paramHints,
  234. 'index': paramId,
  235. 'rindex': -1,
  236. 'midiCC': midiCC,
  237. 'midiChannel': midiChannel
  238. }
  239. self.host._set_parameterData(pluginId, paramId, paramData)
  240. elif msg.startswith("PARAMETER_RANGES_"):
  241. pluginId, paramId = [int(i) for i in msg.replace("PARAMETER_RANGES_", "").split(":")]
  242. def_, min_, max_, step, stepSmall, stepLarge = [float(i) for i in self.readlineblock().split(":")]
  243. paramRanges = {
  244. 'def': def_,
  245. 'min': min_,
  246. 'max': max_,
  247. 'step': step,
  248. 'stepSmall': stepSmall,
  249. 'stepLarge': stepLarge
  250. }
  251. self.host._set_parameterRanges(pluginId, paramId, paramRanges)
  252. elif msg.startswith("PROGRAM_COUNT_"):
  253. pluginId, count, current = [int(i) for i in msg.replace("PROGRAM_COUNT_", "").split(":")]
  254. self.host._set_programCount(pluginId, count)
  255. self.host._set_currentProgram(pluginId, current)
  256. elif msg.startswith("PROGRAM_NAME_"):
  257. pluginId, progId = [int(i) for i in msg.replace("PROGRAM_NAME_", "").split(":")]
  258. progName = self.readlineblock().replace("\r", "\n")
  259. self.host._set_programName(pluginId, progId, progName)
  260. elif msg.startswith("MIDI_PROGRAM_COUNT_"):
  261. pluginId, count, current = [int(i) for i in msg.replace("MIDI_PROGRAM_COUNT_", "").split(":")]
  262. self.host._set_midiProgramCount(pluginId, count)
  263. self.host._set_currentMidiProgram(pluginId, current)
  264. elif msg.startswith("MIDI_PROGRAM_DATA_"):
  265. pluginId, midiProgId = [int(i) for i in msg.replace("MIDI_PROGRAM_DATA_", "").split(":")]
  266. bank, program = [int(i) for i in self.readlineblock().split(":")]
  267. name = self.readlineblock().replace("\r", "\n")
  268. self.host._set_midiProgramData(pluginId, midiProgId, {'bank': bank, 'program': program, 'name': name})
  269. elif msg.startswith("CUSTOM_DATA_COUNT_"):
  270. pluginId, count = [int(i) for i in msg.replace("CUSTOM_DATA_COUNT_", "").split(":")]
  271. self.host._set_customDataCount(pluginId, count)
  272. elif msg.startswith("CUSTOM_DATA_"):
  273. pluginId, customDataId = [int(i) for i in msg.replace("CUSTOM_DATA_", "").split(":")]
  274. type_ = self.readlineblock().replace("\r", "\n")
  275. key = self.readlineblock().replace("\r", "\n")
  276. value = self.readlineblock().replace("\r", "\n")
  277. self.host._set_customData(pluginId, customDataId, {'type': type_, 'key': key, 'value': value})
  278. elif msg == "osc-urls":
  279. tcp = self.readlineblock().replace("\r", "\n")
  280. udp = self.readlineblock().replace("\r", "\n")
  281. self.host.fOscTCP = tcp
  282. self.host.fOscUDP = udp
  283. elif msg == "max-plugin-number":
  284. maxnum = int(self.readlineblock())
  285. self.host.fMaxPluginNumber = maxnum
  286. elif msg == "buffer-size":
  287. bufsize = int(self.readlineblock())
  288. self.host.fBufferSize = bufsize
  289. elif msg == "sample-rate":
  290. srate = float(self.readlineblock())
  291. self.host.fSampleRate = srate
  292. elif msg == "error":
  293. error = self.readlineblock().replace("\r", "\n")
  294. engineCallback(self.host, ENGINE_CALLBACK_ERROR, 0, 0, 0, 0, 0.0, error)
  295. elif msg == "show":
  296. self.fFirstInit = False
  297. self.uiShow()
  298. elif msg == "focus":
  299. self.uiFocus()
  300. elif msg == "hide":
  301. self.uiHide()
  302. elif msg == "quit":
  303. self.fQuitReceived = True
  304. self.uiQuit()
  305. elif msg == "uiTitle":
  306. uiTitle = self.readlineblock().replace("\r", "\n")
  307. self.uiTitleChanged(uiTitle)
  308. else:
  309. print("unknown message: \"" + msg + "\"")
  310. # ------------------------------------------------------------------------------------------------------------
  311. # Embed Widget
  312. class QEmbedWidget(QWidget):
  313. def __init__(self, winId):
  314. QWidget.__init__(self)
  315. self.setAttribute(Qt.WA_LayoutUsesWidgetRect)
  316. self.move(0, 0)
  317. self.fPos = (0, 0)
  318. self.fWinId = 0
  319. def finalSetup(self, gui, winId):
  320. self.fWinId = int(self.winId())
  321. gui.ui.centralwidget.installEventFilter(self)
  322. gui.ui.menubar.installEventFilter(self)
  323. gCarla.utils.x11_reparent_window(self.fWinId, winId)
  324. self.show()
  325. def fixPosition(self):
  326. pos = gCarla.utils.x11_get_window_pos(self.fWinId)
  327. if self.fPos == pos:
  328. return
  329. self.fPos = pos
  330. self.move(pos[0], pos[1])
  331. gCarla.utils.x11_move_window(self.fWinId, pos[2], pos[3])
  332. def eventFilter(self, obj, ev):
  333. if isinstance(ev, QMouseEvent):
  334. self.fixPosition()
  335. return False
  336. def enterEvent(self, ev):
  337. self.fixPosition()
  338. QWidget.enterEvent(self, ev)
  339. # ------------------------------------------------------------------------------------------------------------
  340. # Embed plugin UI
  341. class CarlaEmbedW(QEmbedWidget):
  342. def __init__(self, host, winId, isPatchbay):
  343. QEmbedWidget.__init__(self, winId)
  344. if False:
  345. host = CarlaHostPlugin()
  346. self.host = host
  347. self.fWinId = winId
  348. self.setFixedSize(1024, 712)
  349. self.fLayout = QVBoxLayout(self)
  350. self.fLayout.setContentsMargins(0, 0, 0, 0)
  351. self.fLayout.setSpacing(0)
  352. self.setLayout(self.fLayout)
  353. self.gui = CarlaMiniW(host, isPatchbay, self)
  354. self.gui.hide()
  355. self.gui.ui.act_file_quit.setEnabled(False)
  356. self.gui.ui.act_file_quit.setVisible(False)
  357. self.fShortcutActions = []
  358. self.addShortcutActions(self.gui.ui.menu_File.actions())
  359. self.addShortcutActions(self.gui.ui.menu_Plugin.actions())
  360. self.addShortcutActions(self.gui.ui.menu_PluginMacros.actions())
  361. self.addShortcutActions(self.gui.ui.menu_Settings.actions())
  362. self.addShortcutActions(self.gui.ui.menu_Help.actions())
  363. if self.host.processMode == ENGINE_PROCESS_MODE_PATCHBAY:
  364. self.addShortcutActions(self.gui.ui.menu_Canvas.actions())
  365. self.addShortcutActions(self.gui.ui.menu_Canvas_Zoom.actions())
  366. self.addWidget(self.gui.ui.menubar)
  367. self.addLine()
  368. self.addWidget(self.gui.ui.toolBar)
  369. if self.host.processMode == ENGINE_PROCESS_MODE_PATCHBAY:
  370. self.addLine()
  371. self.fCentralSplitter = QSplitter(self)
  372. policy = self.fCentralSplitter.sizePolicy()
  373. policy.setVerticalStretch(1)
  374. self.fCentralSplitter.setSizePolicy(policy)
  375. self.addCentralWidget(self.gui.ui.dockWidget)
  376. self.addCentralWidget(self.gui.centralWidget())
  377. self.fLayout.addWidget(self.fCentralSplitter)
  378. self.finalSetup(self.gui, winId)
  379. def addShortcutActions(self, actions):
  380. for action in actions:
  381. if not action.shortcut().isEmpty():
  382. self.fShortcutActions.append(action)
  383. def addWidget(self, widget):
  384. widget.setParent(self)
  385. self.fLayout.addWidget(widget)
  386. def addCentralWidget(self, widget):
  387. widget.setParent(self)
  388. self.fCentralSplitter.addWidget(widget)
  389. def addLine(self):
  390. line = QFrame(self)
  391. line.setFrameShadow(QFrame.Sunken)
  392. line.setFrameShape(QFrame.HLine)
  393. line.setLineWidth(0)
  394. line.setMidLineWidth(1)
  395. self.fLayout.addWidget(line)
  396. def keyPressEvent(self, event):
  397. modifiers = event.modifiers()
  398. modifiersStr = ""
  399. if modifiers & Qt.ShiftModifier:
  400. modifiersStr += "Shift+"
  401. if modifiers & Qt.ControlModifier:
  402. modifiersStr += "Ctrl+"
  403. if modifiers & Qt.AltModifier:
  404. modifiersStr += "Alt+"
  405. if modifiers & Qt.MetaModifier:
  406. modifiersStr += "Meta+"
  407. keyStr = QKeySequence(event.key()).toString()
  408. keySeq = QKeySequence(modifiersStr + keyStr)
  409. for action in self.fShortcutActions:
  410. if not action.isEnabled():
  411. continue
  412. if keySeq.matches(action.shortcut()) != QKeySequence.ExactMatch:
  413. continue
  414. event.accept()
  415. action.trigger()
  416. return
  417. QEmbedWidget.keyPressEvent(self, event)
  418. def showEvent(self, event):
  419. QEmbedWidget.showEvent(self, event)
  420. # set our gui as parent for all plugins UIs
  421. if self.host.manageUIs:
  422. winIdStr = "%x" % self.fWinId
  423. self.host.set_engine_option(ENGINE_OPTION_FRONTEND_WIN_ID, 0, winIdStr)
  424. def hideEvent(self, event):
  425. # disable parent
  426. self.host.set_engine_option(ENGINE_OPTION_FRONTEND_WIN_ID, 0, "0")
  427. QEmbedWidget.hideEvent(self, event)
  428. def closeEvent(self, event):
  429. self.gui.close()
  430. self.gui.closeExternalUI()
  431. QEmbedWidget.closeEvent(self, event)
  432. # there might be other qt windows open which will block carla-plugin from quitting
  433. app.quit()
  434. def setLoadRDFsNeeded(self):
  435. self.gui.setLoadRDFsNeeded()
  436. # ------------------------------------------------------------------------------------------------------------
  437. # Main
  438. if __name__ == '__main__':
  439. # -------------------------------------------------------------
  440. # App initialization
  441. app = CarlaApplication("Carla2-Plugin")
  442. # -------------------------------------------------------------
  443. # Set-up custom signal handling
  444. setUpSignals()
  445. # -------------------------------------------------------------
  446. # Init host backend
  447. isPatchbay = sys.argv[0].rsplit(os.path.sep)[-1].lower().replace(".exe","") == "carla-plugin-patchbay"
  448. host = initHost("Carla-Plugin", None, False, True, True, PluginHost)
  449. host.processMode = ENGINE_PROCESS_MODE_PATCHBAY if isPatchbay else ENGINE_PROCESS_MODE_CONTINUOUS_RACK
  450. host.processModeForced = True
  451. host.nextProcessMode = host.processMode
  452. loadHostSettings(host)
  453. # -------------------------------------------------------------
  454. # Create GUI
  455. try:
  456. winId = int(os.getenv("CARLA_PLUGIN_EMBED_WINID"))
  457. except:
  458. winId = 0
  459. gCarla.utils.setenv("CARLA_PLUGIN_EMBED_WINID", "0")
  460. if LINUX and winId != 0:
  461. gui = CarlaEmbedW(host, winId, isPatchbay)
  462. else:
  463. gui = CarlaMiniW(host, isPatchbay)
  464. # -------------------------------------------------------------
  465. # App-Loop
  466. app.exit_exec()