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.

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