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.

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