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.

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