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.

435 lines
16KB

  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.startswith("PEAKS_"):
  121. pluginId = int(msg.replace("PEAKS_", ""))
  122. in1, in2, out1, out2 = [float(i) for i in self.readlineblock().split(":")]
  123. self.host._set_peaks(pluginId, in1, in2, out1, out2)
  124. elif msg.startswith("PARAMVAL_"):
  125. pluginId, paramId = [int(i) for i in msg.replace("PARAMVAL_", "").split(":")]
  126. paramValue = float(self.readlineblock())
  127. if paramId < 0:
  128. self.host._set_internalValue(pluginId, paramId, paramValue)
  129. else:
  130. self.host._set_parameterValue(pluginId, paramId, paramValue)
  131. elif msg.startswith("ENGINE_CALLBACK_"):
  132. action = int(msg.replace("ENGINE_CALLBACK_", ""))
  133. pluginId = int(self.readlineblock())
  134. value1 = int(self.readlineblock())
  135. value2 = int(self.readlineblock())
  136. value3 = int(self.readlineblock())
  137. valuef = float(self.readlineblock())
  138. valueStr = self.readlineblock().replace("\r", "\n")
  139. if action == ENGINE_CALLBACK_PLUGIN_RENAMED:
  140. self.host._set_pluginName(pluginId, valueStr)
  141. elif action == ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED:
  142. if value1 < 0:
  143. self.host._set_internalValue(pluginId, value1, valuef)
  144. else:
  145. self.host._set_parameterValue(pluginId, value1, valuef)
  146. elif action == ENGINE_CALLBACK_PARAMETER_DEFAULT_CHANGED:
  147. self.host._set_parameterDefault(pluginId, value1, valuef)
  148. elif action == ENGINE_CALLBACK_PARAMETER_MIDI_CC_CHANGED:
  149. self.host._set_parameterMidiCC(pluginId, value1, value2)
  150. elif action == ENGINE_CALLBACK_PARAMETER_MIDI_CHANNEL_CHANGED:
  151. self.host._set_parameterMidiChannel(pluginId, value1, value2)
  152. elif action == ENGINE_CALLBACK_PROGRAM_CHANGED:
  153. self.host._set_currentProgram(pluginId, value1)
  154. elif action == ENGINE_CALLBACK_MIDI_PROGRAM_CHANGED:
  155. self.host._set_currentMidiProgram(pluginId, value1)
  156. engineCallback(self.host, action, pluginId, value1, value2, value3, valuef, valueStr)
  157. elif msg.startswith("ENGINE_OPTION_"):
  158. option = int(msg.replace("ENGINE_OPTION_", ""))
  159. forced = bool(self.readlineblock() == "true")
  160. value = self.readlineblock()
  161. if self.fFirstInit and not forced:
  162. return
  163. if option == ENGINE_OPTION_PROCESS_MODE:
  164. self.host.processMode = int(value)
  165. elif option == ENGINE_OPTION_TRANSPORT_MODE:
  166. self.host.transportMode = int(value)
  167. elif option == ENGINE_OPTION_FORCE_STEREO:
  168. self.host.forceStereo = bool(value == "true")
  169. elif option == ENGINE_OPTION_PREFER_PLUGIN_BRIDGES:
  170. self.host.preferPluginBridges = bool(value == "true")
  171. elif option == ENGINE_OPTION_PREFER_UI_BRIDGES:
  172. self.host.preferUIBridges = bool(value == "true")
  173. elif option == ENGINE_OPTION_UIS_ALWAYS_ON_TOP:
  174. self.host.uisAlwaysOnTop = bool(value == "true")
  175. elif option == ENGINE_OPTION_MAX_PARAMETERS:
  176. self.host.maxParameters = int(value)
  177. elif option == ENGINE_OPTION_UI_BRIDGES_TIMEOUT:
  178. self.host.uiBridgesTimeout = int(value)
  179. elif option == ENGINE_OPTION_PATH_BINARIES:
  180. self.host.pathBinaries = value
  181. elif option == ENGINE_OPTION_PATH_RESOURCES:
  182. self.host.pathResources = value
  183. elif msg.startswith("PLUGIN_INFO_"):
  184. pluginId = int(msg.replace("PLUGIN_INFO_", ""))
  185. self.host._add(pluginId)
  186. type_, category, hints, uniqueId, optsAvail, optsEnabled = [int(i) for i in self.readlineblock().split(":")]
  187. filename = self.readlineblock().replace("\r", "\n")
  188. name = self.readlineblock().replace("\r", "\n")
  189. iconName = self.readlineblock().replace("\r", "\n")
  190. realName = self.readlineblock().replace("\r", "\n")
  191. label = self.readlineblock().replace("\r", "\n")
  192. maker = self.readlineblock().replace("\r", "\n")
  193. copyright = self.readlineblock().replace("\r", "\n")
  194. pinfo = {
  195. 'type': type_,
  196. 'category': category,
  197. 'hints': hints,
  198. 'optionsAvailable': optsAvail,
  199. 'optionsEnabled': optsEnabled,
  200. 'filename': filename,
  201. 'name': name,
  202. 'label': label,
  203. 'maker': maker,
  204. 'copyright': copyright,
  205. 'iconName': iconName,
  206. 'patchbayClientId': 0,
  207. 'uniqueId': uniqueId
  208. }
  209. self.host._set_pluginInfo(pluginId, pinfo)
  210. self.host._set_pluginRealName(pluginId, realName)
  211. elif msg.startswith("AUDIO_COUNT_"):
  212. pluginId, ins, outs = [int(i) for i in msg.replace("AUDIO_COUNT_", "").split(":")]
  213. self.host._set_audioCountInfo(pluginId, {'ins': ins, 'outs': outs})
  214. elif msg.startswith("MIDI_COUNT_"):
  215. pluginId, ins, outs = [int(i) for i in msg.replace("MIDI_COUNT_", "").split(":")]
  216. self.host._set_midiCountInfo(pluginId, {'ins': ins, 'outs': outs})
  217. elif msg.startswith("PARAMETER_COUNT_"):
  218. pluginId, ins, outs, count = [int(i) for i in msg.replace("PARAMETER_COUNT_", "").split(":")]
  219. self.host._set_parameterCountInfo(pluginId, count, {'ins': ins, 'outs': outs})
  220. elif msg.startswith("PARAMETER_DATA_"):
  221. pluginId, paramId = [int(i) for i in msg.replace("PARAMETER_DATA_", "").split(":")]
  222. paramType, paramHints, midiChannel, midiCC = [int(i) for i in self.readlineblock().split(":")]
  223. paramName = self.readlineblock().replace("\r", "\n")
  224. paramUnit = self.readlineblock().replace("\r", "\n")
  225. paramInfo = {
  226. 'name': paramName,
  227. 'symbol': "",
  228. 'unit': paramUnit,
  229. 'scalePointCount': 0,
  230. }
  231. self.host._set_parameterInfo(pluginId, paramId, paramInfo)
  232. paramData = {
  233. 'type': paramType,
  234. 'hints': paramHints,
  235. 'index': paramId,
  236. 'rindex': -1,
  237. 'midiCC': midiCC,
  238. 'midiChannel': midiChannel
  239. }
  240. self.host._set_parameterData(pluginId, paramId, paramData)
  241. elif msg.startswith("PARAMETER_RANGES_"):
  242. pluginId, paramId = [int(i) for i in msg.replace("PARAMETER_RANGES_", "").split(":")]
  243. def_, min_, max_, step, stepSmall, stepLarge = [float(i) for i in self.readlineblock().split(":")]
  244. paramRanges = {
  245. 'def': def_,
  246. 'min': min_,
  247. 'max': max_,
  248. 'step': step,
  249. 'stepSmall': stepSmall,
  250. 'stepLarge': stepLarge
  251. }
  252. self.host._set_parameterRanges(pluginId, paramId, paramRanges)
  253. elif msg.startswith("PROGRAM_COUNT_"):
  254. pluginId, count, current = [int(i) for i in msg.replace("PROGRAM_COUNT_", "").split(":")]
  255. self.host._set_programCount(pluginId, count)
  256. self.host._set_currentProgram(pluginId, current)
  257. elif msg.startswith("PROGRAM_NAME_"):
  258. pluginId, progId = [int(i) for i in msg.replace("PROGRAM_NAME_", "").split(":")]
  259. progName = self.readlineblock().replace("\r", "\n")
  260. self.host._set_programName(pluginId, progId, progName)
  261. elif msg.startswith("MIDI_PROGRAM_COUNT_"):
  262. pluginId, count, current = [int(i) for i in msg.replace("MIDI_PROGRAM_COUNT_", "").split(":")]
  263. self.host._set_midiProgramCount(pluginId, count)
  264. self.host._set_currentMidiProgram(pluginId, current)
  265. elif msg.startswith("MIDI_PROGRAM_DATA_"):
  266. pluginId, midiProgId = [int(i) for i in msg.replace("MIDI_PROGRAM_DATA_", "").split(":")]
  267. bank, program = [int(i) for i in self.readlineblock().split(":")]
  268. name = self.readlineblock().replace("\r", "\n")
  269. self.host._set_midiProgramData(pluginId, midiProgId, {'bank': bank, 'program': program, 'name': name})
  270. elif msg.startswith("CUSTOM_DATA_COUNT_"):
  271. pluginId, count = [int(i) for i in msg.replace("CUSTOM_DATA_COUNT_", "").split(":")]
  272. self.host._set_customDataCount(pluginId, count)
  273. elif msg.startswith("CUSTOM_DATA_"):
  274. pluginId, customDataId = [int(i) for i in msg.replace("CUSTOM_DATA_", "").split(":")]
  275. type_ = self.readlineblock().replace("\r", "\n")
  276. key = self.readlineblock().replace("\r", "\n")
  277. value = self.readlineblock().replace("\r", "\n")
  278. self.host._set_customData(pluginId, customDataId, {'type': type_, 'key': key, 'value': value})
  279. elif msg == "osc-urls":
  280. tcp = self.readlineblock().replace("\r", "\n")
  281. udp = self.readlineblock().replace("\r", "\n")
  282. self.host.fOscTCP = tcp
  283. self.host.fOscUDP = udp
  284. elif msg == "max-plugin-number":
  285. maxnum = int(self.readlineblock())
  286. self.host.fMaxPluginNumber = maxnum
  287. elif msg == "buffer-size":
  288. bufsize = int(self.readlineblock())
  289. self.host.fBufferSize = bufsize
  290. elif msg == "sample-rate":
  291. srate = float(self.readlineblock())
  292. self.host.fSampleRate = srate
  293. elif msg == "transport":
  294. playing = bool(self.readlineblock() == "true")
  295. frame, bar, beat, tick = [int(i) for i in self.readlineblock().split(":")]
  296. bpm = float(self.readlineblock())
  297. self.host._set_transport(playing, frame, bar, beat, tick, bpm)
  298. elif msg == "error":
  299. error = self.readlineblock().replace("\r", "\n")
  300. engineCallback(self.host, ENGINE_CALLBACK_ERROR, 0, 0, 0, 0, 0.0, error)
  301. elif msg == "show":
  302. self.fFirstInit = False
  303. self.uiShow()
  304. elif msg == "focus":
  305. self.uiFocus()
  306. elif msg == "hide":
  307. self.uiHide()
  308. elif msg == "quit":
  309. self.fQuitReceived = True
  310. self.uiQuit()
  311. elif msg == "uiTitle":
  312. uiTitle = self.readlineblock().replace("\r", "\n")
  313. self.uiTitleChanged(uiTitle)
  314. else:
  315. print("unknown message: \"" + msg + "\"")
  316. # ------------------------------------------------------------------------------------------------------------
  317. # Main
  318. if __name__ == '__main__':
  319. # -------------------------------------------------------------
  320. # App initialization
  321. app = CarlaApplication("Carla2-Plugin")
  322. # -------------------------------------------------------------
  323. # Set-up custom signal handling
  324. setUpSignals()
  325. # -------------------------------------------------------------
  326. # Init host backend
  327. isPatchbay = sys.argv[0].rsplit(os.path.sep)[-1].lower().replace(".exe","") == "carla-plugin-patchbay"
  328. host = initHost("Carla-Plugin", None, False, True, True, PluginHost)
  329. host.processMode = ENGINE_PROCESS_MODE_PATCHBAY if isPatchbay else ENGINE_PROCESS_MODE_CONTINUOUS_RACK
  330. host.processModeForced = True
  331. host.nextProcessMode = host.processMode
  332. loadHostSettings(host)
  333. # -------------------------------------------------------------
  334. # Create GUI
  335. gui = CarlaMiniW(host, isPatchbay)
  336. # -------------------------------------------------------------
  337. # App-Loop
  338. app.exit_exec()