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.

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