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.

401 lines
16KB

  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Carla plugin host
  4. # Copyright (C) 2011-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 (Config)
  19. from carla_config import *
  20. # ------------------------------------------------------------------------------------------------------------
  21. # Imports (Global)
  22. if config_UseQt5:
  23. from PyQt5.QtCore import QEvent
  24. from PyQt5.QtWidgets import QLabel, QTabWidget
  25. else:
  26. from PyQt4.QtCore import QEvent
  27. from PyQt4.QtGui import QLabel, QTabWidget
  28. # ------------------------------------------------------------------------------------------------------------
  29. # Imports (Custom Stuff)
  30. from carla_host import *
  31. from carla_patchbay import CarlaPatchbayW
  32. from carla_rack import CarlaRackW
  33. # ------------------------------------------------------------------------------------------------------------
  34. # Tab widget (rack + patchbay)
  35. class CarlaMultiW(QTabWidget):
  36. def __init__(self, parent, host):
  37. QTabWidget.__init__(self, parent)
  38. if False:
  39. # kdevelop likes this :)
  40. host = CarlaHostMeta()
  41. self.host = host
  42. # -------------------------------------------------------------
  43. self.fRack = CarlaRackW(parent, host, False)
  44. self.fPatchbay = CarlaPatchbayW(parent, host, False, False)
  45. self.fParent = parent
  46. self.fUseCustomPaint = False
  47. self.addTab(self.fRack, "Plugins")
  48. self.addTab(self.fPatchbay, "Patchbay")
  49. #self.fPatchbay.hide()
  50. #self.removeTab(1)
  51. #self.fPatchbay.setParent(None)
  52. #self.fPatchbay.show()
  53. self.scene = self.fPatchbay.scene
  54. parent.ui.act_plugins_enable.triggered.connect(self.fRack.slot_pluginsEnable)
  55. parent.ui.act_plugins_disable.triggered.connect(self.fRack.slot_pluginsDisable)
  56. parent.ui.act_plugins_volume100.triggered.connect(self.fRack.slot_pluginsVolume100)
  57. parent.ui.act_plugins_mute.triggered.connect(self.fRack.slot_pluginsMute)
  58. parent.ui.act_plugins_wet100.triggered.connect(self.fRack.slot_pluginsWet100)
  59. parent.ui.act_plugins_bypass.triggered.connect(self.fRack.slot_pluginsBypass)
  60. parent.ui.act_plugins_center.triggered.connect(self.fRack.slot_pluginsCenter)
  61. parent.ui.act_plugins_panic.triggered.connect(self.fRack.slot_pluginsDisable)
  62. parent.ui.act_canvas_show_internal.triggered.connect(self.fPatchbay.slot_canvasShowInternal)
  63. parent.ui.act_canvas_show_external.triggered.connect(self.fPatchbay.slot_canvasShowExternal)
  64. parent.ui.act_canvas_arrange.triggered.connect(self.fPatchbay.slot_canvasArrange)
  65. parent.ui.act_canvas_refresh.triggered.connect(self.fPatchbay.slot_canvasRefresh)
  66. parent.ui.act_canvas_zoom_fit.triggered.connect(self.fPatchbay.slot_canvasZoomFit)
  67. parent.ui.act_canvas_zoom_in.triggered.connect(self.fPatchbay.slot_canvasZoomIn)
  68. parent.ui.act_canvas_zoom_out.triggered.connect(self.fPatchbay.slot_canvasZoomOut)
  69. parent.ui.act_canvas_zoom_100.triggered.connect(self.fPatchbay.slot_canvasZoomReset)
  70. parent.ui.act_canvas_print.triggered.connect(self.fPatchbay.slot_canvasPrint)
  71. parent.ui.act_canvas_save_image.triggered.connect(self.fPatchbay.slot_canvasSaveImage)
  72. # TODO, later
  73. parent.ui.act_canvas_arrange.setEnabled(False)
  74. parent.ui.act_settings_configure.triggered.connect(self.fPatchbay.slot_configureCarla)
  75. host.PluginAddedCallback.connect(self.fRack.slot_handlePluginAddedCallback)
  76. host.PluginAddedCallback.connect(self.fPatchbay.slot_handlePluginAddedCallback)
  77. host.PluginRemovedCallback.connect(self.fRack.slot_handlePluginRemovedCallback)
  78. host.PluginRemovedCallback.connect(self.fPatchbay.slot_handlePluginRemovedCallback)
  79. host.PluginRenamedCallback.connect(self.fRack.slot_handlePluginRenamedCallback)
  80. host.PluginRenamedCallback.connect(self.fPatchbay.slot_handlePluginRenamedCallback)
  81. host.PluginUnavailableCallback.connect(self.fRack.slot_handlePluginUnavailableCallback)
  82. host.PluginUnavailableCallback.connect(self.fPatchbay.slot_handlePluginUnavailableCallback)
  83. host.ParameterValueChangedCallback.connect(self.fRack.slot_handleParameterValueChangedCallback)
  84. host.ParameterValueChangedCallback.connect(self.fPatchbay.slot_handleParameterValueChangedCallback)
  85. host.ParameterDefaultChangedCallback.connect(self.fRack.slot_handleParameterDefaultChangedCallback)
  86. host.ParameterMidiChannelChangedCallback.connect(self.fRack.slot_handleParameterMidiChannelChangedCallback)
  87. host.ParameterMidiCcChangedCallback.connect(self.fRack.slot_handleParameterMidiCcChangedCallback)
  88. host.ProgramChangedCallback.connect(self.fRack.slot_handleProgramChangedCallback)
  89. host.MidiProgramChangedCallback.connect(self.fRack.slot_handleMidiProgramChangedCallback)
  90. host.OptionChangedCallback.connect(self.fRack.slot_handleOptionChangedCallback)
  91. host.UiStateChangedCallback.connect(self.fRack.slot_handleUiStateChangedCallback)
  92. host.NoteOnCallback.connect(self.fRack.slot_handleNoteOnCallback)
  93. host.NoteOnCallback.connect(self.fPatchbay.slot_handleNoteOnCallback)
  94. host.NoteOffCallback.connect(self.fRack.slot_handleNoteOffCallback)
  95. host.NoteOffCallback.connect(self.fPatchbay.slot_handleNoteOffCallback)
  96. host.UpdateCallback.connect(self.fRack.slot_handleUpdateCallback)
  97. host.ReloadInfoCallback.connect(self.fRack.slot_handleReloadInfoCallback)
  98. host.ReloadParametersCallback.connect(self.fRack.slot_handleReloadParametersCallback)
  99. host.ReloadParametersCallback.connect(self.fPatchbay.slot_handleReloadParametersCallback)
  100. host.ReloadProgramsCallback.connect(self.fRack.slot_handleReloadProgramsCallback)
  101. host.ReloadAllCallback.connect(self.fRack.slot_handleReloadAllCallback)
  102. host.ReloadAllCallback.connect(self.fPatchbay.slot_handleReloadAllCallback)
  103. host.PatchbayClientAddedCallback.connect(self.fPatchbay.slot_handlePatchbayClientAddedCallback)
  104. host.PatchbayClientRemovedCallback.connect(self.fPatchbay.slot_handlePatchbayClientRemovedCallback)
  105. host.PatchbayClientRenamedCallback.connect(self.fPatchbay.slot_handlePatchbayClientRenamedCallback)
  106. host.PatchbayClientDataChangedCallback.connect(self.fPatchbay.slot_handlePatchbayClientDataChangedCallback)
  107. host.PatchbayPortAddedCallback.connect(self.fPatchbay.slot_handlePatchbayPortAddedCallback)
  108. host.PatchbayPortRemovedCallback.connect(self.fPatchbay.slot_handlePatchbayPortRemovedCallback)
  109. host.PatchbayPortRenamedCallback.connect(self.fPatchbay.slot_handlePatchbayPortRenamedCallback)
  110. host.PatchbayConnectionAddedCallback.connect(self.fPatchbay.slot_handlePatchbayConnectionAddedCallback)
  111. host.PatchbayConnectionRemovedCallback.connect(self.fPatchbay.slot_handlePatchbayConnectionRemovedCallback)
  112. # -----------------------------------------------------------------
  113. def fixCanvasPreviewSize(self):
  114. self.fPatchbay.resize(self.fRack.size())
  115. self.fPatchbay.slot_miniCanvasCheckSize()
  116. def setUseCustomPaint(self, useCustomPaint):
  117. if self.fUseCustomPaint != useCustomPaint:
  118. self.fUseCustomPaint = useCustomPaint
  119. self.update()
  120. # -----------------------------------------------------------------
  121. def removeAllPlugins(self):
  122. self.fRack.removeAllPlugins()
  123. self.fPatchbay.removeAllPlugins()
  124. # -----------------------------------------------------------------
  125. def engineStarted(self):
  126. self.fRack.engineStarted()
  127. self.fPatchbay.engineStarted()
  128. self.fParent.engineStarted()
  129. def engineStopped(self):
  130. self.fRack.engineStopped()
  131. self.fPatchbay.engineStopped()
  132. self.fParent.engineStopped()
  133. # -----------------------------------------------------------------
  134. def idleFast(self):
  135. self.fRack.idleFast()
  136. self.fPatchbay.idleFast()
  137. def idleSlow(self):
  138. self.fRack.idleSlow()
  139. self.fPatchbay.idleSlow()
  140. # -----------------------------------------------------------------
  141. def projectLoadingStarted(self):
  142. self.fRack.projectLoadingStarted()
  143. self.fPatchbay.projectLoadingStarted()
  144. def projectLoadingFinished(self):
  145. self.fRack.projectLoadingFinished()
  146. self.fPatchbay.projectLoadingFinished()
  147. # -----------------------------------------------------------------
  148. def saveSettings(self, settings):
  149. self.fRack.saveSettings(settings)
  150. self.fPatchbay.saveSettings(settings)
  151. def showEditDialog(self, pluginId):
  152. self.fRack.showEditDialog(pluginId)
  153. # -----------------------------------------------------------------
  154. def paintEvent(self, event):
  155. QTabWidget.paintEvent(self, event)
  156. if MACOS or not self.fUseCustomPaint:
  157. return
  158. painter = QPainter(self)
  159. painter.setBrush(QColor(36, 36, 36))
  160. painter.setPen(QColor(62, 62, 62))
  161. painter.drawRect(1, self.height()/2, self.width()-3, self.height()-self.height()/2-1)
  162. def resizeEvent(self, event):
  163. QTabWidget.resizeEvent(self, event)
  164. if self.currentIndex() == 0:
  165. self.fixCanvasPreviewSize()
  166. # ------------------------------------------------------------------------------------------------------------
  167. # Main Window
  168. class CarlaHostW(HostWindow):
  169. def __init__(self, host):
  170. HostWindow.__init__(self, host)
  171. # -------------------------------------------------------------
  172. # Set-up container
  173. self.fContainer = CarlaMultiW(self, host)
  174. self.setupContainer(True, self.fContainer.fPatchbay.themeData)
  175. self.fContainer.setUseCustomPaint(self.fSavedSettings[CARLA_KEY_CUSTOM_PAINTING])
  176. # -------------------------------------------------------------
  177. # Set-up GUI stuff
  178. self.fInfoText = ""
  179. self.fInfoLabel = QLabel(self)
  180. self.fInfoLabel.setAlignment(Qt.AlignRight|Qt.AlignVCenter)
  181. self.fInfoLabel.setText("Engine stopped")
  182. self.fDockLocation = Qt.LeftDockWidgetArea
  183. self.fDockFloating = 0
  184. if MACOS and False: # TODO: check if NOT using pro theme
  185. self.fInfoLabel.hide()
  186. self.setUnifiedTitleAndToolBarOnMac(True)
  187. # -------------------------------------------------------------
  188. self.ui.act_settings_show_toolbar.triggered.connect(self.slot_toolbarShown)
  189. self.ui.dockWidget.dockLocationChanged.connect(self.slot_dockLocationChanged)
  190. self.ui.dockWidget.topLevelChanged.connect(self.slot_dockTopLevelChanged)
  191. self.ui.dockWidget.installEventFilter(self)
  192. QTimer.singleShot(0, self.slot_initWidgets)
  193. # -----------------------------------------------------------------
  194. def engineChanged(self):
  195. self.fInfoText = "Engine running | SampleRate: %g | BufferSize: %i" % (self.host.get_sample_rate(), self.host.get_buffer_size())
  196. self.fInfoLabel.setText("%s | %s" % (self.fInfoText, self.fTextTransport))
  197. def engineStarted(self):
  198. self.engineChanged()
  199. def engineStopped(self):
  200. self.fInfoText = ""
  201. self.fInfoLabel.setText("Engine stopped")
  202. # -----------------------------------------------------------------
  203. def updateInfoLabelXandSize(self):
  204. tabBar = self.fContainer.tabBar()
  205. x = tabBar.width() + 20
  206. width = self.fContainer.width() - tabBar.width() - 20
  207. if self.fDockLocation == Qt.LeftDockWidgetArea and self.fDockFloating <= 1:
  208. x += self.ui.dockWidget.width()
  209. self.fInfoLabel.move(x, self.fInfoLabel.y())
  210. self.fInfoLabel.resize(width, self.fInfoLabel.height())
  211. if self.fDockFloating == 1:
  212. self.fDockFloating = 2
  213. def updateInfoLabelY(self):
  214. tabBar = self.fContainer.tabBar()
  215. y = tabBar.mapFromParent(self.ui.centralwidget.pos()).y()
  216. if not self.ui.toolBar.isVisible():
  217. y -= self.ui.toolBar.height()
  218. self.fInfoLabel.move(self.fInfoLabel.x(), y)
  219. # -----------------------------------------------------------------
  220. @pyqtSlot()
  221. def slot_initWidgets(self):
  222. tabBar = self.fContainer.tabBar()
  223. x = tabBar.width() + 20
  224. y = tabBar.mapFromParent(self.ui.centralwidget.pos()).y()
  225. if self.fDockLocation == Qt.LeftDockWidgetArea and self.fDockFloating <= 1:
  226. x += self.ui.tabUtils.width()
  227. self.fInfoLabel.move(x, y)
  228. self.fInfoLabel.resize(self.fContainer.width()-tabBar.width()-20, tabBar.height())
  229. # FIXME: Qt4 needs this so it properly creates & resizes the canvas
  230. self.fContainer.setCurrentIndex(1)
  231. self.fContainer.setCurrentIndex(0)
  232. self.fContainer.fixCanvasPreviewSize()
  233. @pyqtSlot(bool)
  234. def slot_dockTopLevelChanged(self, top):
  235. self.fDockFloating = 1 if top else 0
  236. self.updateInfoLabelXandSize()
  237. @pyqtSlot(Qt.DockWidgetArea)
  238. def slot_dockLocationChanged(self, area):
  239. self.fDockLocation = area
  240. self.updateInfoLabelXandSize()
  241. @pyqtSlot()
  242. def slot_toolbarShown(self):
  243. self.updateInfoLabelY()
  244. # -----------------------------------------------------------------
  245. def eventFilter(self, obj, event):
  246. if obj == self.ui.dockWidget and event.type() == QEvent.Resize:
  247. self.updateInfoLabelXandSize()
  248. return HostWindow.eventFilter(self, obj, event)
  249. def resizeEvent(self, event):
  250. HostWindow.resizeEvent(self, event)
  251. self.updateInfoLabelXandSize()
  252. def timerEvent(self, event):
  253. HostWindow.timerEvent(self, event)
  254. if event.timerId() == self.fIdleTimerFast:
  255. self.fInfoLabel.setText("%s | %s" % (self.fInfoText, self.fTextTransport))
  256. # ------------------------------------------------------------------------------------------------------------
  257. # Main
  258. if __name__ == '__main__':
  259. # -------------------------------------------------------------
  260. # Read CLI args
  261. initName = os.path.basename(__file__) if ("__file__" in dir() and os.path.dirname(__file__) in PATH) else sys.argv[0]
  262. libPrefix = None
  263. for arg in sys.argv:
  264. if arg.startswith("--with-appname="):
  265. initName = os.path.basename(arg.replace("--with-initname=", ""))
  266. elif arg.startswith("--with-libprefix="):
  267. libPrefix = arg.replace("--with-libprefix=", "")
  268. # -------------------------------------------------------------
  269. # App initialization
  270. app = CarlaApplication("Carla2", libPrefix)
  271. # -------------------------------------------------------------
  272. # Set-up custom signal handling
  273. setUpSignals()
  274. # -------------------------------------------------------------
  275. # Init host backend
  276. host = initHost(initName, libPrefix, False, False, True)
  277. loadHostSettings(host)
  278. # -------------------------------------------------------------
  279. # Create GUI
  280. gCarla.gui = CarlaHostW(host)
  281. # -------------------------------------------------------------
  282. # Load project file if set
  283. args = app.arguments()
  284. if len(args) > 1:
  285. arg = args[-1]
  286. if arg.startswith("--with-appname=") or arg.startswith("--with-libprefix="):
  287. pass
  288. elif os.path.exists(arg):
  289. gCarla.gui.loadProjectLater(arg)
  290. # -------------------------------------------------------------
  291. # Show GUI
  292. gCarla.gui.show()
  293. # -------------------------------------------------------------
  294. # App-Loop
  295. app.exit_exec()