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.

332 lines
13KB

  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. self.fIsRunning = True
  27. self.fSampleRate = float(sys.argv[1]) if len(sys.argv) > 1 else 44100.0
  28. # -------------------------------------------------------------------
  29. def sendMsg(self, lines):
  30. # FIXME
  31. gCarla.gui.send(lines)
  32. return True
  33. # -------------------------------------------------------------------
  34. def engine_init(self, driverName, clientName):
  35. return True
  36. def engine_close(self):
  37. return True
  38. def engine_idle(self):
  39. if gCarla.gui.idleExternalUI():
  40. return
  41. self.fIsRunning = False
  42. gCarla.gui.d_uiQuit()
  43. def is_engine_running(self):
  44. return self.fIsRunning
  45. def set_engine_about_to_close(self):
  46. return
  47. # ------------------------------------------------------------------------------------------------------------
  48. # Main Window
  49. class CarlaMiniW(ExternalUI, HostWindow):
  50. def __init__(self, host):
  51. # need to init this first
  52. gCarla.gui = self
  53. # now the regular stuff
  54. ExternalUI.__init__(self)
  55. HostWindow.__init__(self, host)
  56. self.host = host
  57. if False:
  58. # kdevelop likes this :)
  59. host = CarlaHostPlugin()
  60. self.host = host
  61. if sys.argv[0].lower().endswith("/carla-plugin-patchbay"):
  62. from carla_patchbay import CarlaPatchbayW
  63. self.fContainer = CarlaPatchbayW(self, host)
  64. self.setupContainer(True, self.fContainer.themeData)
  65. else:
  66. from carla_rack import CarlaRackW
  67. self.fContainer = CarlaRackW(self, host)
  68. self.setupContainer(False)
  69. self.setWindowTitle(self.fUiName)
  70. self.showUiIfTesting()
  71. # -------------------------------------------------------------------
  72. # ExternalUI Callbacks
  73. def d_uiShow(self):
  74. self.show()
  75. def d_uiHide(self):
  76. self.hide()
  77. def d_uiQuit(self):
  78. self.close()
  79. app.quit()
  80. def d_uiTitleChanged(self, uiTitle):
  81. self.setWindowTitle(uiTitle)
  82. # -------------------------------------------------------------------
  83. # Qt events
  84. def closeEvent(self, event):
  85. self.closeExternalUI()
  86. HostWindow.closeEvent(self, event)
  87. # -------------------------------------------------------------------
  88. # Custom idler
  89. def idleExternalUI(self):
  90. while True:
  91. if self.fPipeRecv is None:
  92. return True
  93. try:
  94. msg = self.fPipeRecv.readline().strip()
  95. except IOError:
  96. return False
  97. if not msg:
  98. return True
  99. elif msg.startswith("PEAKS_"):
  100. pluginId = int(msg.replace("PEAKS_", ""))
  101. in1, in2, out1, out2 = [float(i) for i in self.readlineblock().split(":")]
  102. self.host._set_peaks(pluginId, in1, in2, out1, out2)
  103. elif msg.startswith("PARAMVAL_"):
  104. pluginId, paramId = [int(i) for i in msg.replace("PARAMVAL_", "").split(":")]
  105. paramValue = float(self.readlineblock())
  106. self.host._set_parameterValue(pluginId, paramId, paramValue)
  107. elif msg.startswith("ENGINE_CALLBACK_"):
  108. action = int(msg.replace("ENGINE_CALLBACK_", ""))
  109. pluginId = int(self.readlineblock())
  110. value1 = int(self.readlineblock())
  111. value2 = int(self.readlineblock())
  112. value3 = float(self.readlineblock())
  113. valueStr = self.readlineblock().replace("\r", "\n")
  114. if action == ENGINE_CALLBACK_PLUGIN_RENAMED:
  115. self.host._set_pluginName(pluginId, valueStr)
  116. elif action == ENGINE_CALLBACK_PARAMETER_VALUE_CHANGED:
  117. if value1 < 0:
  118. self.host._set_internalValue(pluginId, value1, value3)
  119. else:
  120. self.host._set_parameterValue(pluginId, value1, value3)
  121. elif action == ENGINE_CALLBACK_PARAMETER_DEFAULT_CHANGED:
  122. self.host._set_parameterDefault(pluginId, value1, value3)
  123. elif action == ENGINE_CALLBACK_PARAMETER_MIDI_CC_CHANGED:
  124. self.host._set_parameterMidiCC(pluginId, value1, value2)
  125. elif action == ENGINE_CALLBACK_PARAMETER_MIDI_CHANNEL_CHANGED:
  126. self.host._set_parameterMidiChannel(pluginId, value1, value2)
  127. elif action == ENGINE_CALLBACK_PROGRAM_CHANGED:
  128. self.host._set_currentProgram(pluginId, value1)
  129. elif action == ENGINE_CALLBACK_MIDI_PROGRAM_CHANGED:
  130. self.host._set_currentMidiProgram(pluginId, value1)
  131. engineCallback(None, action, pluginId, value1, value2, value3, valueStr)
  132. elif msg.startswith("PLUGIN_INFO_"):
  133. pluginId = int(msg.replace("PLUGIN_INFO_", ""))
  134. self.host._add(pluginId)
  135. type_, category, hints, uniqueId, optsAvail, optsEnabled = [int(i) for i in self.readlineblock().split(":")]
  136. filename = self.readlineblock().replace("\r", "\n")
  137. name = self.readlineblock().replace("\r", "\n")
  138. iconName = self.readlineblock().replace("\r", "\n")
  139. realName = self.readlineblock().replace("\r", "\n")
  140. label = self.readlineblock().replace("\r", "\n")
  141. maker = self.readlineblock().replace("\r", "\n")
  142. copyright = self.readlineblock().replace("\r", "\n")
  143. pinfo = {
  144. 'type': type_,
  145. 'category': category,
  146. 'hints': hints,
  147. 'optionsAvailable': optsAvail,
  148. 'optionsEnabled': optsEnabled,
  149. 'filename': filename,
  150. 'name': name,
  151. 'label': label,
  152. 'maker': maker,
  153. 'copyright': copyright,
  154. 'iconName': iconName,
  155. 'patchbayClientId': 0,
  156. 'uniqueId': uniqueId
  157. }
  158. self.host._set_pluginInfo(pluginId, pinfo)
  159. self.host._set_pluginRealName(pluginId, realName)
  160. elif msg.startswith("AUDIO_COUNT_"):
  161. pluginId, ins, outs = [int(i) for i in msg.replace("AUDIO_COUNT_", "").split(":")]
  162. self.host._set_audioCountInfo(pluginId, {'ins': ins, 'outs': outs})
  163. elif msg.startswith("MIDI_COUNT_"):
  164. pluginId, ins, outs = [int(i) for i in msg.replace("MIDI_COUNT_", "").split(":")]
  165. self.host._set_midiCountInfo(pluginId, {'ins': ins, 'outs': outs})
  166. elif msg.startswith("PARAMETER_COUNT_"):
  167. pluginId, ins, outs, count = [int(i) for i in msg.replace("PARAMETER_COUNT_", "").split(":")]
  168. self.host._set_parameterCountInfo(pluginId, count, {'ins': ins, 'outs': outs})
  169. elif msg.startswith("PARAMETER_DATA_"):
  170. pluginId, paramId = [int(i) for i in msg.replace("PARAMETER_DATA_", "").split(":")]
  171. paramType, paramHints, midiChannel, midiCC = [int(i) for i in self.readlineblock().split(":")]
  172. paramName = self.readlineblock().replace("\r", "\n")
  173. paramUnit = self.readlineblock().replace("\r", "\n")
  174. paramInfo = {
  175. 'name': paramName,
  176. 'symbol': "",
  177. 'unit': paramUnit,
  178. 'scalePointCount': 0,
  179. }
  180. self.host._set_parameterInfo(pluginId, paramId, paramInfo)
  181. paramData = {
  182. 'type': paramType,
  183. 'hints': paramHints,
  184. 'index': paramId,
  185. 'rindex': -1,
  186. 'midiCC': midiCC,
  187. 'midiChannel': midiChannel
  188. }
  189. self.host._set_parameterData(pluginId, paramId, paramData)
  190. elif msg.startswith("PARAMETER_RANGES_"):
  191. pluginId, paramId = [int(i) for i in msg.replace("PARAMETER_RANGES_", "").split(":")]
  192. def_, min_, max_, step, stepSmall, stepLarge = [float(i) for i in self.readlineblock().split(":")]
  193. paramRanges = {
  194. 'def': def_,
  195. 'min': min_,
  196. 'max': max_,
  197. 'step': step,
  198. 'stepSmall': stepSmall,
  199. 'stepLarge': stepLarge
  200. }
  201. self.host._set_parameterRanges(pluginId, paramId, paramRanges)
  202. elif msg.startswith("PROGRAM_COUNT_"):
  203. pluginId, count, current = [int(i) for i in msg.replace("PROGRAM_COUNT_", "").split(":")]
  204. self.host._set_programCount(pluginId, count)
  205. self.host._set_currentProgram(pluginId, current)
  206. elif msg.startswith("PROGRAM_NAME_"):
  207. pluginId, progId = [int(i) for i in msg.replace("PROGRAM_NAME_", "").split(":")]
  208. progName = self.readlineblock().replace("\r", "\n")
  209. self.host._set_programName(pluginId, progId, progName)
  210. elif msg.startswith("MIDI_PROGRAM_COUNT_"):
  211. pluginId, count, current = [int(i) for i in msg.replace("MIDI_PROGRAM_COUNT_", "").split(":")]
  212. self.host._set_midiProgramCount(pluginId, count)
  213. self.host._set_currentMidiProgram(pluginId, current)
  214. elif msg.startswith("MIDI_PROGRAM_DATA_"):
  215. pluginId, midiProgId = [int(i) for i in msg.replace("MIDI_PROGRAM_DATA_", "").split(":")]
  216. bank, program = [int(i) for i in self.readlineblock().split(":")]
  217. name = self.readlineblock().replace("\r", "\n")
  218. self.host._set_midiProgramData(pluginId, midiProgId, {'bank': bank, 'program': program, 'name': name})
  219. elif msg == "error":
  220. error = self.readlineblock().replace("\r", "\n")
  221. engineCallback(None, ENGINE_CALLBACK_ERROR, 0, 0, 0, 0.0, error)
  222. elif msg == "show":
  223. self.d_uiShow()
  224. elif msg == "hide":
  225. self.d_uiHide()
  226. elif msg == "quit":
  227. self.fQuitReceived = True
  228. self.d_uiQuit()
  229. elif msg == "uiTitle":
  230. uiTitle = self.readlineblock().replace("\r", "\n")
  231. self.d_uiTitleChanged(uiTitle)
  232. else:
  233. print("unknown message: \"" + msg + "\"")
  234. return True
  235. # ------------------------------------------------------------------------------------------------------------
  236. # Main
  237. if __name__ == '__main__':
  238. # -------------------------------------------------------------
  239. # App initialization
  240. app = CarlaApplication("Carla2-Plugin")
  241. # -------------------------------------------------------------
  242. # Set-up custom signal handling
  243. setUpSignals()
  244. # -------------------------------------------------------------
  245. # Init host backend
  246. host = initHost("Carla-Plugin", PluginHost, False, True, True)
  247. host.processMode = ENGINE_PROCESS_MODE_PATCHBAY if sys.argv[0].lower().endswith("/carla-plugin-patchbay") else ENGINE_PROCESS_MODE_CONTINUOUS_RACK
  248. # FIXME
  249. loadHostSettings(host)
  250. # -------------------------------------------------------------
  251. # Create GUI
  252. gCarla.gui = CarlaMiniW(host)
  253. # -------------------------------------------------------------
  254. # simulate an engire started callback FIXME
  255. engineCallback(host, ENGINE_CALLBACK_ENGINE_STARTED, 0, host.processMode, ENGINE_TRANSPORT_MODE_PLUGIN, 0.0, "Plugin")
  256. # -------------------------------------------------------------
  257. # App-Loop
  258. app.exit_exec()