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.

475 lines
17KB

  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Carla plugin host (plugin UI)
  4. # Copyright (C) 2013-2014 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 (Custom Stuff)
  19. from carla_host import *
  20. from externalui import ExternalUI
  21. # ------------------------------------------------------------------------------------------------------------
  22. # Host Plugin object
  23. class PluginHost(CarlaHostQtPlugin):
  24. def __init__(self):
  25. CarlaHostQtPlugin.__init__(self)
  26. if False:
  27. # kdevelop likes this :)
  28. self.fExternalUI = ExternalUI()
  29. # ---------------------------------------------------------------
  30. self.fExternalUI = None
  31. # -------------------------------------------------------------------
  32. def setExternalUI(self, extUI):
  33. self.fExternalUI = extUI
  34. def sendMsg(self, lines):
  35. if self.fExternalUI is None:
  36. return False
  37. self.fExternalUI.send(lines)
  38. return True
  39. # -------------------------------------------------------------------
  40. def engine_init(self, driverName, clientName):
  41. return True
  42. def engine_close(self):
  43. return True
  44. def engine_idle(self):
  45. self.fExternalUI.idleExternalUI()
  46. def is_engine_running(self):
  47. return self.fExternalUI.isRunning()
  48. def set_engine_about_to_close(self):
  49. return
  50. # ------------------------------------------------------------------------------------------------------------
  51. # Main Window
  52. class CarlaMiniW(ExternalUI, HostWindow):
  53. def __init__(self, host, parent=None):
  54. ExternalUI.__init__(self)
  55. HostWindow.__init__(self, host, sys.argv[0].lower().endswith("/carla-plugin-patchbay"), parent)
  56. self.host = host
  57. if False:
  58. # kdevelop likes this :)
  59. host = PluginHost()
  60. self.host = host
  61. host.setExternalUI(self)
  62. self.fFirstInit = True
  63. self.setWindowTitle(self.fUiName)
  64. self.ready()
  65. # -------------------------------------------------------------------
  66. # ExternalUI Callbacks
  67. def uiShow(self):
  68. if self.parent() is not None:
  69. return
  70. self.show()
  71. def uiFocus(self):
  72. if self.parent() is not None:
  73. return
  74. self.setWindowState((self.windowState() & ~Qt.WindowMinimized) | Qt.WindowActive)
  75. self.show()
  76. self.raise_()
  77. self.activateWindow()
  78. def uiHide(self):
  79. if self.parent() is not None:
  80. return
  81. self.hide()
  82. def uiQuit(self):
  83. self.closeExternalUI()
  84. self.close()
  85. app.quit()
  86. def uiTitleChanged(self, uiTitle):
  87. self.setWindowTitle(uiTitle)
  88. # -------------------------------------------------------------------
  89. # Qt events
  90. def closeEvent(self, event):
  91. self.closeExternalUI()
  92. HostWindow.closeEvent(self, event)
  93. # -------------------------------------------------------------------
  94. # Custom callback
  95. def msgCallback(self, msg):
  96. try:
  97. self.msgCallback2(msg)
  98. except:
  99. print("msgCallback error, skipped for", msg)
  100. def msgCallback2(self, msg):
  101. msg = charPtrToString(msg)
  102. #if not msg:
  103. #return
  104. if msg.startswith("PEAKS_"):
  105. pluginId = int(msg.replace("PEAKS_", ""))
  106. in1, in2, out1, out2 = [float(i) for i in self.readlineblock().split(":")]
  107. self.host._set_peaks(pluginId, in1, in2, out1, out2)
  108. elif msg.startswith("PARAMVAL_"):
  109. pluginId, paramId = [int(i) for i in msg.replace("PARAMVAL_", "").split(":")]
  110. paramValue = float(self.readlineblock())
  111. if paramId < 0:
  112. self.host._set_internalValue(pluginId, paramId, paramValue)
  113. else:
  114. self.host._set_parameterValue(pluginId, paramId, paramValue)
  115. elif msg.startswith("ENGINE_CALLBACK_"):
  116. action = int(msg.replace("ENGINE_CALLBACK_", ""))
  117. pluginId = int(self.readlineblock())
  118. value1 = int(self.readlineblock())
  119. value2 = int(self.readlineblock())
  120. value3 = float(self.readlineblock())
  121. valueStr = self.readlineblock().replace("\r", "\n")
  122. if action == ENGINE_CALLBACK_PLUGIN_RENAMED:
  123. self.host._set_pluginName(pluginId, valueStr)
  124. elif action == ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED:
  125. if value1 < 0:
  126. self.host._set_internalValue(pluginId, value1, value3)
  127. else:
  128. self.host._set_parameterValue(pluginId, value1, value3)
  129. elif action == ENGINE_CALLBACK_PARAMETER_DEFAULT_CHANGED:
  130. self.host._set_parameterDefault(pluginId, value1, value3)
  131. elif action == ENGINE_CALLBACK_PARAMETER_MIDI_CC_CHANGED:
  132. self.host._set_parameterMidiCC(pluginId, value1, value2)
  133. elif action == ENGINE_CALLBACK_PARAMETER_MIDI_CHANNEL_CHANGED:
  134. self.host._set_parameterMidiChannel(pluginId, value1, value2)
  135. elif action == ENGINE_CALLBACK_PROGRAM_CHANGED:
  136. self.host._set_currentProgram(pluginId, value1)
  137. elif action == ENGINE_CALLBACK_MIDI_PROGRAM_CHANGED:
  138. self.host._set_currentMidiProgram(pluginId, value1)
  139. engineCallback(self.host, action, pluginId, value1, value2, value3, valueStr)
  140. elif msg.startswith("ENGINE_OPTION_"):
  141. option = int(msg.replace("ENGINE_OPTION_", ""))
  142. forced = bool(self.readlineblock() == "true")
  143. value = self.readlineblock()
  144. if self.fFirstInit and not forced:
  145. return
  146. if option == ENGINE_OPTION_PROCESS_MODE:
  147. self.host.processMode = int(value)
  148. elif option == ENGINE_OPTION_TRANSPORT_MODE:
  149. self.host.transportMode = int(value)
  150. elif option == ENGINE_OPTION_FORCE_STEREO:
  151. self.host.forceStereo = bool(value == "true")
  152. elif option == ENGINE_OPTION_PREFER_PLUGIN_BRIDGES:
  153. self.host.preferPluginBridges = bool(value == "true")
  154. elif option == ENGINE_OPTION_PREFER_UI_BRIDGES:
  155. self.host.preferUIBridges = bool(value == "true")
  156. elif option == ENGINE_OPTION_UIS_ALWAYS_ON_TOP:
  157. self.host.uisAlwaysOnTop = bool(value == "true")
  158. elif option == ENGINE_OPTION_MAX_PARAMETERS:
  159. self.host.maxParameters = int(value)
  160. elif option == ENGINE_OPTION_UI_BRIDGES_TIMEOUT:
  161. self.host.uiBridgesTimeout = int(value)
  162. elif option == ENGINE_OPTION_PATH_BINARIES:
  163. self.host.pathBinaries = value
  164. elif option == ENGINE_OPTION_PATH_RESOURCES:
  165. self.host.pathResources = value
  166. elif msg.startswith("PLUGIN_INFO_"):
  167. pluginId = int(msg.replace("PLUGIN_INFO_", ""))
  168. self.host._add(pluginId)
  169. type_, category, hints, uniqueId, optsAvail, optsEnabled = [int(i) for i in self.readlineblock().split(":")]
  170. filename = self.readlineblock().replace("\r", "\n")
  171. name = self.readlineblock().replace("\r", "\n")
  172. iconName = self.readlineblock().replace("\r", "\n")
  173. realName = self.readlineblock().replace("\r", "\n")
  174. label = self.readlineblock().replace("\r", "\n")
  175. maker = self.readlineblock().replace("\r", "\n")
  176. copyright = self.readlineblock().replace("\r", "\n")
  177. pinfo = {
  178. 'type': type_,
  179. 'category': category,
  180. 'hints': hints,
  181. 'optionsAvailable': optsAvail,
  182. 'optionsEnabled': optsEnabled,
  183. 'filename': filename,
  184. 'name': name,
  185. 'label': label,
  186. 'maker': maker,
  187. 'copyright': copyright,
  188. 'iconName': iconName,
  189. 'patchbayClientId': 0,
  190. 'uniqueId': uniqueId
  191. }
  192. self.host._set_pluginInfo(pluginId, pinfo)
  193. self.host._set_pluginRealName(pluginId, realName)
  194. elif msg.startswith("AUDIO_COUNT_"):
  195. pluginId, ins, outs = [int(i) for i in msg.replace("AUDIO_COUNT_", "").split(":")]
  196. self.host._set_audioCountInfo(pluginId, {'ins': ins, 'outs': outs})
  197. elif msg.startswith("MIDI_COUNT_"):
  198. pluginId, ins, outs = [int(i) for i in msg.replace("MIDI_COUNT_", "").split(":")]
  199. self.host._set_midiCountInfo(pluginId, {'ins': ins, 'outs': outs})
  200. elif msg.startswith("PARAMETER_COUNT_"):
  201. pluginId, ins, outs, count = [int(i) for i in msg.replace("PARAMETER_COUNT_", "").split(":")]
  202. self.host._set_parameterCountInfo(pluginId, count, {'ins': ins, 'outs': outs})
  203. elif msg.startswith("PARAMETER_DATA_"):
  204. pluginId, paramId = [int(i) for i in msg.replace("PARAMETER_DATA_", "").split(":")]
  205. paramType, paramHints, midiChannel, midiCC = [int(i) for i in self.readlineblock().split(":")]
  206. paramName = self.readlineblock().replace("\r", "\n")
  207. paramUnit = self.readlineblock().replace("\r", "\n")
  208. paramInfo = {
  209. 'name': paramName,
  210. 'symbol': "",
  211. 'unit': paramUnit,
  212. 'scalePointCount': 0,
  213. }
  214. self.host._set_parameterInfo(pluginId, paramId, paramInfo)
  215. paramData = {
  216. 'type': paramType,
  217. 'hints': paramHints,
  218. 'index': paramId,
  219. 'rindex': -1,
  220. 'midiCC': midiCC,
  221. 'midiChannel': midiChannel
  222. }
  223. self.host._set_parameterData(pluginId, paramId, paramData)
  224. elif msg.startswith("PARAMETER_RANGES_"):
  225. pluginId, paramId = [int(i) for i in msg.replace("PARAMETER_RANGES_", "").split(":")]
  226. def_, min_, max_, step, stepSmall, stepLarge = [float(i) for i in self.readlineblock().split(":")]
  227. paramRanges = {
  228. 'def': def_,
  229. 'min': min_,
  230. 'max': max_,
  231. 'step': step,
  232. 'stepSmall': stepSmall,
  233. 'stepLarge': stepLarge
  234. }
  235. self.host._set_parameterRanges(pluginId, paramId, paramRanges)
  236. elif msg.startswith("PROGRAM_COUNT_"):
  237. pluginId, count, current = [int(i) for i in msg.replace("PROGRAM_COUNT_", "").split(":")]
  238. self.host._set_programCount(pluginId, count)
  239. self.host._set_currentProgram(pluginId, current)
  240. elif msg.startswith("PROGRAM_NAME_"):
  241. pluginId, progId = [int(i) for i in msg.replace("PROGRAM_NAME_", "").split(":")]
  242. progName = self.readlineblock().replace("\r", "\n")
  243. self.host._set_programName(pluginId, progId, progName)
  244. elif msg.startswith("MIDI_PROGRAM_COUNT_"):
  245. pluginId, count, current = [int(i) for i in msg.replace("MIDI_PROGRAM_COUNT_", "").split(":")]
  246. self.host._set_midiProgramCount(pluginId, count)
  247. self.host._set_currentMidiProgram(pluginId, current)
  248. elif msg.startswith("MIDI_PROGRAM_DATA_"):
  249. pluginId, midiProgId = [int(i) for i in msg.replace("MIDI_PROGRAM_DATA_", "").split(":")]
  250. bank, program = [int(i) for i in self.readlineblock().split(":")]
  251. name = self.readlineblock().replace("\r", "\n")
  252. self.host._set_midiProgramData(pluginId, midiProgId, {'bank': bank, 'program': program, 'name': name})
  253. elif msg == "complete-license":
  254. license = self.readlineblock().replace("\r", "\n")
  255. self.host.fCompleteLicenseText = license
  256. elif msg == "juce-version":
  257. version = self.readlineblock().replace("\r", "\n")
  258. self.host.fJuceVersion = version
  259. elif msg == "file-exts":
  260. exts = self.readlineblock().replace("\r", "\n")
  261. self.host.fSupportedFileExts = exts
  262. # only now we know the supported extensions
  263. self.fDirModel.setNameFilters(exts.split(";"))
  264. elif msg == "max-plugin-number":
  265. maxnum = int(self.readlineblock())
  266. self.host.fMaxPluginNumber = maxnum
  267. elif msg == "buffer-size":
  268. bufsize = int(self.readlineblock())
  269. self.host.fBufferSize = bufsize
  270. elif msg == "sample-rate":
  271. srate = float(self.readlineblock())
  272. self.host.fSampleRate = srate
  273. elif msg == "transport":
  274. playing = bool(self.readlineblock() == "true")
  275. frame, bar, beat, tick = [int(i) for i in self.readlineblock().split(":")]
  276. bpm = float(self.readlineblock())
  277. self.host._set_transport(playing, frame, bar, beat, tick, bpm)
  278. elif msg == "error":
  279. error = self.readlineblock().replace("\r", "\n")
  280. engineCallback(self.host, ENGINE_CALLBACK_ERROR, 0, 0, 0, 0.0, error)
  281. elif msg == "show":
  282. self.fFirstInit = False
  283. self.uiShow()
  284. elif msg == "focus":
  285. self.uiFocus()
  286. elif msg == "hide":
  287. self.uiHide()
  288. elif msg == "quit":
  289. self.fQuitReceived = True
  290. self.uiQuit()
  291. elif msg == "uiTitle":
  292. uiTitle = self.readlineblock().replace("\r", "\n")
  293. self.uiTitleChanged(uiTitle)
  294. else:
  295. print("unknown message: \"" + msg + "\"")
  296. # ------------------------------------------------------------------------------------------------------------
  297. # Embed plugin UI
  298. if LINUX and not config_UseQt5:
  299. from PyQt4.QtGui import QHBoxLayout, QX11EmbedWidget
  300. class CarlaEmbedW(QX11EmbedWidget):
  301. def __init__(self, host, winId):
  302. QX11EmbedWidget.__init__(self)
  303. self.host = host
  304. self.fWinId = winId
  305. self.fLayout = QVBoxLayout(self)
  306. self.fLayout.setContentsMargins(0, 0, 0, 0)
  307. self.fLayout.setSpacing(0)
  308. self.setLayout(self.fLayout)
  309. gui = CarlaMiniW(host, self)
  310. gui.hide()
  311. gui.ui.act_file_quit.setEnabled(False)
  312. gui.ui.menu_File.setEnabled(False)
  313. gui.ui.menu_File.setVisible(False)
  314. #menuBar = gui.menuBar()
  315. #menuBar.removeAction(gui.ui.menu_File.menuAction())
  316. self.addWidget(gui.menuBar())
  317. self.addLine()
  318. self.addWidget(gui.ui.toolBar)
  319. self.addLine()
  320. self.addWidget(gui.centralWidget())
  321. self.setFixedSize(740, 512)
  322. self.embedInto(winId)
  323. self.show()
  324. def addWidget(self, widget):
  325. widget.setParent(self)
  326. self.fLayout.addWidget(widget)
  327. def addLine(self):
  328. line = QFrame(self)
  329. line.setFrameShadow(QFrame.Sunken)
  330. line.setFrameShape(QFrame.HLine)
  331. line.setLineWidth(0)
  332. line.setMidLineWidth(1)
  333. self.fLayout.addWidget(line)
  334. def showEvent(self, event):
  335. QX11EmbedWidget.showEvent(self, event)
  336. # set our gui as parent for all plugins UIs
  337. winIdStr = "%x" % self.fWinId
  338. self.host.set_engine_option(ENGINE_OPTION_FRONTEND_WIN_ID, 0, winIdStr)
  339. def hideEvent(self, event):
  340. # disable parent
  341. self.host.set_engine_option(ENGINE_OPTION_FRONTEND_WIN_ID, 0, "0")
  342. QX11EmbedWidget.hideEvent(self, event)
  343. # ------------------------------------------------------------------------------------------------------------
  344. # Main
  345. if __name__ == '__main__':
  346. # -------------------------------------------------------------
  347. # App initialization
  348. app = CarlaApplication("Carla2-Plugin")
  349. # -------------------------------------------------------------
  350. # Set-up custom signal handling
  351. setUpSignals()
  352. # -------------------------------------------------------------
  353. # Init host backend
  354. host = initHost("Carla-Plugin", PluginHost, False, True, True)
  355. host.processMode = ENGINE_PROCESS_MODE_PATCHBAY if sys.argv[0].lower().endswith("/carla-plugin-patchbay") else ENGINE_PROCESS_MODE_CONTINUOUS_RACK
  356. host.processModeForced = True
  357. loadHostSettings(host)
  358. # -------------------------------------------------------------
  359. # Create GUI
  360. try:
  361. winId = int(os.getenv("CARLA_PLUGIN_EMBED_WINID"))
  362. except:
  363. winId = 0
  364. gCarla.utils.setenv("CARLA_PLUGIN_EMBED_WINID", "0")
  365. if LINUX and winId != 0 and not config_UseQt5:
  366. gui = CarlaEmbedW(host, winId)
  367. else:
  368. gui = CarlaMiniW(host)
  369. # -------------------------------------------------------------
  370. # App-Loop
  371. app.exit_exec()