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.

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