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.

382 lines
14KB

  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.ReloadAllCallback.connect(self.fRack.slot_handleReloadAllCallback)
  84. host.PatchbayClientAddedCallback.connect(self.fPatchbay.slot_handlePatchbayClientAddedCallback)
  85. host.PatchbayClientRemovedCallback.connect(self.fPatchbay.slot_handlePatchbayClientRemovedCallback)
  86. host.PatchbayClientRenamedCallback.connect(self.fPatchbay.slot_handlePatchbayClientRenamedCallback)
  87. host.PatchbayClientDataChangedCallback.connect(self.fPatchbay.slot_handlePatchbayClientDataChangedCallback)
  88. host.PatchbayPortAddedCallback.connect(self.fPatchbay.slot_handlePatchbayPortAddedCallback)
  89. host.PatchbayPortRemovedCallback.connect(self.fPatchbay.slot_handlePatchbayPortRemovedCallback)
  90. host.PatchbayPortRenamedCallback.connect(self.fPatchbay.slot_handlePatchbayPortRenamedCallback)
  91. host.PatchbayConnectionAddedCallback.connect(self.fPatchbay.slot_handlePatchbayConnectionAddedCallback)
  92. host.PatchbayConnectionRemovedCallback.connect(self.fPatchbay.slot_handlePatchbayConnectionRemovedCallback)
  93. # -----------------------------------------------------------------
  94. def fixCanvasPreviewSize(self):
  95. self.fPatchbay.resize(self.fRack.size())
  96. self.fPatchbay.slot_miniCanvasCheckSize()
  97. def setUseCustomPaint(self, useCustomPaint):
  98. if self.fUseCustomPaint != useCustomPaint:
  99. self.fUseCustomPaint = useCustomPaint
  100. self.update()
  101. # -----------------------------------------------------------------
  102. def removeAllPlugins(self):
  103. self.fRack.removeAllPlugins()
  104. self.fPatchbay.removeAllPlugins()
  105. # -----------------------------------------------------------------
  106. def engineStarted(self):
  107. self.fRack.engineStarted()
  108. self.fPatchbay.engineStarted()
  109. self.fParent.engineStarted()
  110. def engineStopped(self):
  111. self.fRack.engineStopped()
  112. self.fPatchbay.engineStopped()
  113. self.fParent.engineStopped()
  114. # -----------------------------------------------------------------
  115. def idleFast(self):
  116. self.fRack.idleFast()
  117. self.fPatchbay.idleFast()
  118. def idleSlow(self):
  119. self.fRack.idleSlow()
  120. self.fPatchbay.idleSlow()
  121. # -----------------------------------------------------------------
  122. def projectLoadingStarted(self):
  123. self.fRack.projectLoadingStarted()
  124. self.fPatchbay.projectLoadingStarted()
  125. def projectLoadingFinished(self):
  126. self.fRack.projectLoadingFinished()
  127. self.fPatchbay.projectLoadingFinished()
  128. # -----------------------------------------------------------------
  129. def saveSettings(self, settings):
  130. self.fRack.saveSettings(settings)
  131. self.fPatchbay.saveSettings(settings)
  132. def showEditDialog(self, pluginId):
  133. self.fRack.showEditDialog(pluginId)
  134. # -----------------------------------------------------------------
  135. def paintEvent(self, event):
  136. QTabWidget.paintEvent(self, event)
  137. if MACOS or not self.fUseCustomPaint:
  138. return
  139. painter = QPainter(self)
  140. painter.setBrush(QColor(36, 36, 36))
  141. painter.setPen(QColor(62, 62, 62))
  142. painter.drawRect(1, self.height()/2, self.width()-3, self.height()-self.height()/2-1)
  143. def resizeEvent(self, event):
  144. QTabWidget.resizeEvent(self, event)
  145. if self.currentIndex() == 0:
  146. self.fixCanvasPreviewSize()
  147. # ------------------------------------------------------------------------------------------------------------
  148. # Main Window
  149. class CarlaHostW(HostWindow):
  150. def __init__(self, host):
  151. HostWindow.__init__(self, host)
  152. # -------------------------------------------------------------
  153. # Set-up container
  154. self.fContainer = CarlaMultiW(self, host)
  155. self.setupContainer(True, self.fContainer.fPatchbay.themeData)
  156. self.fContainer.setUseCustomPaint(self.fSavedSettings[CARLA_KEY_CUSTOM_PAINTING])
  157. # -------------------------------------------------------------
  158. # Set-up GUI stuff
  159. self.fInfoText = ""
  160. self.fInfoLabel = QLabel(self)
  161. self.fInfoLabel.setAlignment(Qt.AlignRight|Qt.AlignVCenter)
  162. self.fInfoLabel.setText("Engine stopped")
  163. self.fDockLocation = Qt.LeftDockWidgetArea
  164. self.fDockFloating = 0
  165. if MACOS and False: # TODO: check if NOT using pro theme
  166. self.fInfoLabel.hide()
  167. self.setUnifiedTitleAndToolBarOnMac(True)
  168. # -------------------------------------------------------------
  169. self.ui.act_settings_show_toolbar.triggered.connect(self.slot_toolbarShown)
  170. self.ui.dockWidget.dockLocationChanged.connect(self.slot_dockLocationChanged)
  171. self.ui.dockWidget.topLevelChanged.connect(self.slot_dockTopLevelChanged)
  172. self.ui.dockWidget.installEventFilter(self)
  173. QTimer.singleShot(0, self.slot_initWidgets)
  174. # -----------------------------------------------------------------
  175. def engineChanged(self):
  176. self.fInfoText = "Engine running | SampleRate: %g | BufferSize: %i" % (self.host.get_sample_rate(), self.host.get_buffer_size())
  177. self.fInfoLabel.setText("%s | %s" % (self.fInfoText, self.fTextTransport))
  178. def engineStarted(self):
  179. self.engineChanged()
  180. def engineStopped(self):
  181. self.fInfoText = ""
  182. self.fInfoLabel.setText("Engine stopped")
  183. # -----------------------------------------------------------------
  184. def updateInfoLabelXandSize(self):
  185. tabBar = self.fContainer.tabBar()
  186. x = tabBar.width() + 20
  187. width = self.fContainer.width() - tabBar.width() - 20
  188. if self.fDockLocation == Qt.LeftDockWidgetArea and self.fDockFloating <= 1:
  189. x += self.ui.dockWidget.width()
  190. self.fInfoLabel.move(x, self.fInfoLabel.y())
  191. self.fInfoLabel.resize(width, self.fInfoLabel.height())
  192. if self.fDockFloating == 1:
  193. self.fDockFloating = 2
  194. def updateInfoLabelY(self):
  195. tabBar = self.fContainer.tabBar()
  196. y = tabBar.mapFromParent(self.ui.centralwidget.pos()).y()
  197. if not self.ui.toolBar.isVisible():
  198. y -= self.ui.toolBar.height()
  199. self.fInfoLabel.move(self.fInfoLabel.x(), y)
  200. # -----------------------------------------------------------------
  201. @pyqtSlot()
  202. def slot_initWidgets(self):
  203. tabBar = self.fContainer.tabBar()
  204. x = tabBar.width() + 20
  205. y = tabBar.mapFromParent(self.ui.centralwidget.pos()).y()
  206. if self.fDockLocation == Qt.LeftDockWidgetArea and self.fDockFloating <= 1:
  207. x += self.ui.tabUtils.width()
  208. self.fInfoLabel.move(x, y)
  209. self.fInfoLabel.resize(self.fContainer.width()-tabBar.width()-20, tabBar.height())
  210. # FIXME: Qt4 needs this so it properly creates & resizes the canvas
  211. self.fContainer.setCurrentIndex(1)
  212. self.fContainer.setCurrentIndex(0)
  213. self.fContainer.fixCanvasPreviewSize()
  214. @pyqtSlot(bool)
  215. def slot_dockTopLevelChanged(self, top):
  216. self.fDockFloating = 1 if top else 0
  217. self.updateInfoLabelXandSize()
  218. @pyqtSlot(Qt.DockWidgetArea)
  219. def slot_dockLocationChanged(self, area):
  220. self.fDockLocation = area
  221. self.updateInfoLabelXandSize()
  222. @pyqtSlot()
  223. def slot_toolbarShown(self):
  224. self.updateInfoLabelY()
  225. # -----------------------------------------------------------------
  226. def eventFilter(self, obj, event):
  227. if obj == self.ui.dockWidget and event.type() == QEvent.Resize:
  228. self.updateInfoLabelXandSize()
  229. return HostWindow.eventFilter(self, obj, event)
  230. def resizeEvent(self, event):
  231. HostWindow.resizeEvent(self, event)
  232. self.updateInfoLabelXandSize()
  233. def timerEvent(self, event):
  234. HostWindow.timerEvent(self, event)
  235. if event.timerId() == self.fIdleTimerFast:
  236. self.fInfoLabel.setText("%s | %s" % (self.fInfoText, self.fTextTransport))
  237. # ------------------------------------------------------------------------------------------------------------
  238. # Main
  239. if __name__ == '__main__':
  240. # -------------------------------------------------------------
  241. # Read CLI args
  242. initName = os.path.basename(__file__) if ("__file__" in dir() and os.path.dirname(__file__) in PATH) else sys.argv[0]
  243. libPrefix = None
  244. for arg in sys.argv:
  245. if arg.startswith("--with-appname="):
  246. initName = os.path.basename(arg.replace("--with-initname=", ""))
  247. elif arg.startswith("--with-libprefix="):
  248. libPrefix = arg.replace("--with-libprefix=", "")
  249. # -------------------------------------------------------------
  250. # App initialization
  251. app = CarlaApplication("Carla2", libPrefix)
  252. # -------------------------------------------------------------
  253. # Set-up custom signal handling
  254. setUpSignals()
  255. # -------------------------------------------------------------
  256. # Init host backend
  257. host = initHost(initName, libPrefix, False, False, True)
  258. loadHostSettings(host)
  259. # -------------------------------------------------------------
  260. # Create GUI
  261. gCarla.gui = CarlaHostW(host)
  262. # -------------------------------------------------------------
  263. # Load project file if set
  264. args = app.arguments()
  265. if len(args) > 1:
  266. arg = args[-1]
  267. if arg.startswith("--with-appname=") or arg.startswith("--with-libprefix="):
  268. pass
  269. elif os.path.exists(arg):
  270. gCarla.gui.loadProjectLater(arg)
  271. # -------------------------------------------------------------
  272. # Show GUI
  273. gCarla.gui.show()
  274. # -------------------------------------------------------------
  275. # App-Loop
  276. app.exit_exec()