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.

424 lines
15KB

  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Carla plugin host (plugin UI)
  4. # Copyright (C) 2013-2019 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 (Global)
  19. from PyQt5.QtGui import QKeySequence
  20. from PyQt5.QtWidgets import QHBoxLayout
  21. # ------------------------------------------------------------------------------------------------------------
  22. # Imports (Custom Stuff)
  23. from carla_host import *
  24. from externalui import ExternalUI
  25. # ------------------------------------------------------------------------------------------------------------
  26. # Host Plugin object
  27. class PluginHost(CarlaHostQtPlugin):
  28. def __init__(self):
  29. CarlaHostQtPlugin.__init__(self)
  30. if False:
  31. # kdevelop likes this :)
  32. self.fExternalUI = ExternalUI()
  33. # ---------------------------------------------------------------
  34. self.fExternalUI = None
  35. # -------------------------------------------------------------------
  36. def setExternalUI(self, extUI):
  37. self.fExternalUI = extUI
  38. def sendMsg(self, lines):
  39. if self.fExternalUI is None:
  40. return False
  41. return self.fExternalUI.send(lines)
  42. # -------------------------------------------------------------------
  43. def engine_init(self, driverName, clientName):
  44. return True
  45. def engine_close(self):
  46. return True
  47. def engine_idle(self):
  48. self.fExternalUI.idleExternalUI()
  49. def is_engine_running(self):
  50. if self.fExternalUI is None:
  51. return False
  52. return self.fExternalUI.isRunning()
  53. def set_engine_about_to_close(self):
  54. return True
  55. # ------------------------------------------------------------------------------------------------------------
  56. # Main Window
  57. class CarlaMiniW(ExternalUI, HostWindow):
  58. def __init__(self, host, isPatchbay, parent=None):
  59. ExternalUI.__init__(self)
  60. HostWindow.__init__(self, host, isPatchbay, parent)
  61. self.host = host
  62. if False:
  63. # kdevelop likes this :)
  64. host = PluginHost()
  65. self.host = host
  66. host.setExternalUI(self)
  67. self.fFirstInit = True
  68. self.setWindowTitle(self.fUiName)
  69. self.ready()
  70. # Override this as it can be called from several places.
  71. # We really need to close all UIs as events are driven by host idle which is only available when UI is visible
  72. def closeExternalUI(self):
  73. for i in reversed(range(self.fPluginCount)):
  74. self.host.show_custom_ui(i, False)
  75. ExternalUI.closeExternalUI(self)
  76. # -------------------------------------------------------------------
  77. # ExternalUI Callbacks
  78. def uiShow(self):
  79. if self.parent() is not None:
  80. return
  81. self.show()
  82. def uiFocus(self):
  83. if self.parent() is not None:
  84. return
  85. self.setWindowState((self.windowState() & ~Qt.WindowMinimized) | Qt.WindowActive)
  86. self.show()
  87. self.raise_()
  88. self.activateWindow()
  89. def uiHide(self):
  90. if self.parent() is not None:
  91. return
  92. self.hide()
  93. def uiQuit(self):
  94. self.closeExternalUI()
  95. self.close()
  96. if self != gui:
  97. gui.close()
  98. # there might be other qt windows open which will block carla-plugin from quitting
  99. app.quit()
  100. def uiTitleChanged(self, uiTitle):
  101. self.setWindowTitle(uiTitle)
  102. # -------------------------------------------------------------------
  103. # Qt events
  104. def closeEvent(self, event):
  105. self.closeExternalUI()
  106. HostWindow.closeEvent(self, event)
  107. # there might be other qt windows open which will block carla-plugin from quitting
  108. app.quit()
  109. # -------------------------------------------------------------------
  110. # Custom callback
  111. def msgCallback(self, msg):
  112. try:
  113. self.msgCallback2(msg)
  114. except:
  115. print("msgCallback error, skipped for", msg)
  116. def msgCallback2(self, msg):
  117. msg = charPtrToString(msg)
  118. #if not msg:
  119. #return
  120. if msg == "runtime-info":
  121. values = self.readlineblock().split(":")
  122. load = float(values[0])
  123. xruns = int(values[1])
  124. self.host._set_runtime_info(load, xruns)
  125. elif msg == "transport":
  126. playing = bool(self.readlineblock() == "true")
  127. frame, bar, beat, tick = [int(i) for i in self.readlineblock().split(":")]
  128. bpm = float(self.readlineblock())
  129. self.host._set_transport(playing, frame, bar, beat, tick, bpm)
  130. elif msg.startswith("PEAKS_"):
  131. pluginId = int(msg.replace("PEAKS_", ""))
  132. in1, in2, out1, out2 = [float(i) for i in self.readlineblock().split(":")]
  133. self.host._set_peaks(pluginId, in1, in2, out1, out2)
  134. elif msg.startswith("PARAMVAL_"):
  135. pluginId, paramId = [int(i) for i in msg.replace("PARAMVAL_", "").split(":")]
  136. paramValue = float(self.readlineblock())
  137. if paramId < 0:
  138. self.host._set_internalValue(pluginId, paramId, paramValue)
  139. else:
  140. self.host._set_parameterValue(pluginId, paramId, paramValue)
  141. elif msg.startswith("ENGINE_CALLBACK_"):
  142. action = int(msg.replace("ENGINE_CALLBACK_", ""))
  143. pluginId = int(self.readlineblock())
  144. value1 = int(self.readlineblock())
  145. value2 = int(self.readlineblock())
  146. value3 = int(self.readlineblock())
  147. valuef = float(self.readlineblock())
  148. valueStr = self.readlineblock().replace("\r", "\n")
  149. self.host._setViaCallback(action, pluginId, value1, value2, value3, valuef, valueStr)
  150. engineCallback(self.host, action, pluginId, value1, value2, value3, valuef, valueStr)
  151. elif msg.startswith("ENGINE_OPTION_"):
  152. option = int(msg.replace("ENGINE_OPTION_", ""))
  153. forced = bool(self.readlineblock() == "true")
  154. value = self.readlineblock()
  155. if self.fFirstInit and not forced:
  156. return
  157. if option == ENGINE_OPTION_PROCESS_MODE:
  158. self.host.processMode = int(value)
  159. elif option == ENGINE_OPTION_TRANSPORT_MODE:
  160. self.host.transportMode = int(value)
  161. elif option == ENGINE_OPTION_FORCE_STEREO:
  162. self.host.forceStereo = bool(value == "true")
  163. elif option == ENGINE_OPTION_PREFER_PLUGIN_BRIDGES:
  164. self.host.preferPluginBridges = bool(value == "true")
  165. elif option == ENGINE_OPTION_PREFER_UI_BRIDGES:
  166. self.host.preferUIBridges = bool(value == "true")
  167. elif option == ENGINE_OPTION_UIS_ALWAYS_ON_TOP:
  168. self.host.uisAlwaysOnTop = bool(value == "true")
  169. elif option == ENGINE_OPTION_MAX_PARAMETERS:
  170. self.host.maxParameters = int(value)
  171. elif option == ENGINE_OPTION_UI_BRIDGES_TIMEOUT:
  172. self.host.uiBridgesTimeout = int(value)
  173. elif option == ENGINE_OPTION_PATH_BINARIES:
  174. self.host.pathBinaries = value
  175. elif option == ENGINE_OPTION_PATH_RESOURCES:
  176. self.host.pathResources = value
  177. elif msg.startswith("PLUGIN_INFO_"):
  178. pluginId = int(msg.replace("PLUGIN_INFO_", ""))
  179. self.host._add(pluginId)
  180. type_, category, hints, uniqueId, optsAvail, optsEnabled = [int(i) for i in self.readlineblock().split(":")]
  181. filename = self.readlineblock().replace("\r", "\n")
  182. name = self.readlineblock().replace("\r", "\n")
  183. iconName = self.readlineblock().replace("\r", "\n")
  184. realName = self.readlineblock().replace("\r", "\n")
  185. label = self.readlineblock().replace("\r", "\n")
  186. maker = self.readlineblock().replace("\r", "\n")
  187. copyright = self.readlineblock().replace("\r", "\n")
  188. pinfo = {
  189. 'type': type_,
  190. 'category': category,
  191. 'hints': hints,
  192. 'optionsAvailable': optsAvail,
  193. 'optionsEnabled': optsEnabled,
  194. 'filename': filename,
  195. 'name': name,
  196. 'label': label,
  197. 'maker': maker,
  198. 'copyright': copyright,
  199. 'iconName': iconName,
  200. 'patchbayClientId': 0,
  201. 'uniqueId': uniqueId
  202. }
  203. self.host._set_pluginInfo(pluginId, pinfo)
  204. self.host._set_pluginRealName(pluginId, realName)
  205. elif msg.startswith("AUDIO_COUNT_"):
  206. pluginId, ins, outs = [int(i) for i in msg.replace("AUDIO_COUNT_", "").split(":")]
  207. self.host._set_audioCountInfo(pluginId, {'ins': ins, 'outs': outs})
  208. elif msg.startswith("MIDI_COUNT_"):
  209. pluginId, ins, outs = [int(i) for i in msg.replace("MIDI_COUNT_", "").split(":")]
  210. self.host._set_midiCountInfo(pluginId, {'ins': ins, 'outs': outs})
  211. elif msg.startswith("PARAMETER_COUNT_"):
  212. pluginId, ins, outs, count = [int(i) for i in msg.replace("PARAMETER_COUNT_", "").split(":")]
  213. self.host._set_parameterCountInfo(pluginId, count, {'ins': ins, 'outs': outs})
  214. elif msg.startswith("PARAMETER_DATA_"):
  215. pluginId, paramId = [int(i) for i in msg.replace("PARAMETER_DATA_", "").split(":")]
  216. paramType, paramHints, midiChannel, midiCC = [int(i) for i in self.readlineblock().split(":")]
  217. paramName = self.readlineblock().replace("\r", "\n")
  218. paramUnit = self.readlineblock().replace("\r", "\n")
  219. paramInfo = {
  220. 'name': paramName,
  221. 'symbol': "",
  222. 'unit': paramUnit,
  223. 'scalePointCount': 0,
  224. }
  225. self.host._set_parameterInfo(pluginId, paramId, paramInfo)
  226. paramData = {
  227. 'type': paramType,
  228. 'hints': paramHints,
  229. 'index': paramId,
  230. 'rindex': -1,
  231. 'midiCC': midiCC,
  232. 'midiChannel': midiChannel
  233. }
  234. self.host._set_parameterData(pluginId, paramId, paramData)
  235. elif msg.startswith("PARAMETER_RANGES_"):
  236. pluginId, paramId = [int(i) for i in msg.replace("PARAMETER_RANGES_", "").split(":")]
  237. def_, min_, max_, step, stepSmall, stepLarge = [float(i) for i in self.readlineblock().split(":")]
  238. paramRanges = {
  239. 'def': def_,
  240. 'min': min_,
  241. 'max': max_,
  242. 'step': step,
  243. 'stepSmall': stepSmall,
  244. 'stepLarge': stepLarge
  245. }
  246. self.host._set_parameterRanges(pluginId, paramId, paramRanges)
  247. elif msg.startswith("PROGRAM_COUNT_"):
  248. pluginId, count, current = [int(i) for i in msg.replace("PROGRAM_COUNT_", "").split(":")]
  249. self.host._set_programCount(pluginId, count)
  250. self.host._set_currentProgram(pluginId, current)
  251. elif msg.startswith("PROGRAM_NAME_"):
  252. pluginId, progId = [int(i) for i in msg.replace("PROGRAM_NAME_", "").split(":")]
  253. progName = self.readlineblock().replace("\r", "\n")
  254. self.host._set_programName(pluginId, progId, progName)
  255. elif msg.startswith("MIDI_PROGRAM_COUNT_"):
  256. pluginId, count, current = [int(i) for i in msg.replace("MIDI_PROGRAM_COUNT_", "").split(":")]
  257. self.host._set_midiProgramCount(pluginId, count)
  258. self.host._set_currentMidiProgram(pluginId, current)
  259. elif msg.startswith("MIDI_PROGRAM_DATA_"):
  260. pluginId, midiProgId = [int(i) for i in msg.replace("MIDI_PROGRAM_DATA_", "").split(":")]
  261. bank, program = [int(i) for i in self.readlineblock().split(":")]
  262. name = self.readlineblock().replace("\r", "\n")
  263. self.host._set_midiProgramData(pluginId, midiProgId, {'bank': bank, 'program': program, 'name': name})
  264. elif msg.startswith("CUSTOM_DATA_COUNT_"):
  265. pluginId, count = [int(i) for i in msg.replace("CUSTOM_DATA_COUNT_", "").split(":")]
  266. self.host._set_customDataCount(pluginId, count)
  267. elif msg.startswith("CUSTOM_DATA_"):
  268. pluginId, customDataId = [int(i) for i in msg.replace("CUSTOM_DATA_", "").split(":")]
  269. type_ = self.readlineblock().replace("\r", "\n")
  270. key = self.readlineblock().replace("\r", "\n")
  271. value = self.readlineblock().replace("\r", "\n")
  272. self.host._set_customData(pluginId, customDataId, {'type': type_, 'key': key, 'value': value})
  273. elif msg == "osc-urls":
  274. tcp = self.readlineblock().replace("\r", "\n")
  275. udp = self.readlineblock().replace("\r", "\n")
  276. self.host.fOscTCP = tcp
  277. self.host.fOscUDP = udp
  278. elif msg == "max-plugin-number":
  279. maxnum = int(self.readlineblock())
  280. self.host.fMaxPluginNumber = maxnum
  281. elif msg == "buffer-size":
  282. bufsize = int(self.readlineblock())
  283. self.host.fBufferSize = bufsize
  284. elif msg == "sample-rate":
  285. srate = float(self.readlineblock())
  286. self.host.fSampleRate = srate
  287. elif msg == "error":
  288. error = self.readlineblock().replace("\r", "\n")
  289. engineCallback(self.host, ENGINE_CALLBACK_ERROR, 0, 0, 0, 0, 0.0, error)
  290. elif msg == "show":
  291. self.fFirstInit = False
  292. self.uiShow()
  293. elif msg == "focus":
  294. self.uiFocus()
  295. elif msg == "hide":
  296. self.uiHide()
  297. elif msg == "quit":
  298. self.fQuitReceived = True
  299. self.uiQuit()
  300. elif msg == "uiTitle":
  301. uiTitle = self.readlineblock().replace("\r", "\n")
  302. self.uiTitleChanged(uiTitle)
  303. else:
  304. print("unknown message: \"" + msg + "\"")
  305. # ------------------------------------------------------------------------------------------------------------
  306. # Main
  307. if __name__ == '__main__':
  308. # -------------------------------------------------------------
  309. # App initialization
  310. app = CarlaApplication("Carla2-Plugin")
  311. # -------------------------------------------------------------
  312. # Set-up custom signal handling
  313. setUpSignals()
  314. # -------------------------------------------------------------
  315. # Init host backend
  316. isPatchbay = sys.argv[0].rsplit(os.path.sep)[-1].lower().replace(".exe","") == "carla-plugin-patchbay"
  317. host = initHost("Carla-Plugin", None, False, True, True, PluginHost)
  318. host.processMode = ENGINE_PROCESS_MODE_PATCHBAY if isPatchbay else ENGINE_PROCESS_MODE_CONTINUOUS_RACK
  319. host.processModeForced = True
  320. host.nextProcessMode = host.processMode
  321. loadHostSettings(host)
  322. # -------------------------------------------------------------
  323. # Create GUI
  324. gui = CarlaMiniW(host, isPatchbay)
  325. # -------------------------------------------------------------
  326. # App-Loop
  327. app.exit_exec()