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.

467 lines
18KB

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