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.

567 lines
19KB

  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Common Carla code
  4. # Copyright (C) 2011-2013 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 doc/GPL.txt file.
  17. # ------------------------------------------------------------------------------------------------------------
  18. # Imports (Global)
  19. import os
  20. import sys
  21. try:
  22. from PyQt5.QtCore import pyqtSignal, pyqtSlot, qCritical, qFatal, qWarning
  23. from PyQt5.QtGui import QIcon
  24. from PyQt5.QtWidgets import QFileDialog, QMessageBox
  25. except:
  26. from PyQt4.QtCore import pyqtSignal, pyqtSlot, qCritical, qFatal, qWarning
  27. from PyQt4.QtGui import QIcon
  28. from PyQt4.QtGui import QFileDialog, QMessageBox
  29. # ------------------------------------------------------------------------------------------------------------
  30. # Import Signal
  31. from signal import signal, SIGINT, SIGTERM
  32. try:
  33. from signal import SIGUSR1
  34. haveSIGUSR1 = True
  35. except:
  36. haveSIGUSR1 = False
  37. # ------------------------------------------------------------------------------------------------------------
  38. # Imports (Custom)
  39. from carla_backend import *
  40. # ------------------------------------------------------------------------------------------------------------
  41. # Platform specific stuff
  42. if MACOS:
  43. from PyQt5.QtGui import qt_mac_set_menubar_icons
  44. qt_mac_set_menubar_icons(False)
  45. elif WINDOWS:
  46. WINDIR = os.getenv("WINDIR")
  47. # ------------------------------------------------------------------------------------------------------------
  48. # Set Version
  49. VERSION = "1.9.0"
  50. # ------------------------------------------------------------------------------------------------------------
  51. # Set TMP
  52. TMP = os.getenv("TMP")
  53. if TMP is None:
  54. if WINDOWS:
  55. qWarning("TMP variable not set")
  56. TMP = os.path.join(WINDIR, "temp")
  57. else:
  58. TMP = "/tmp"
  59. elif not os.path.exists(TMP):
  60. qWarning("TMP does not exist")
  61. TMP = "/tmp"
  62. # ------------------------------------------------------------------------------------------------------------
  63. # Set HOME
  64. HOME = os.getenv("HOME")
  65. if HOME is None:
  66. HOME = os.path.expanduser("~")
  67. if LINUX or MACOS:
  68. qWarning("HOME variable not set")
  69. if not os.path.exists(HOME):
  70. qWarning("HOME does not exist")
  71. HOME = TMP
  72. # ------------------------------------------------------------------------------------------------------------
  73. # Set PATH
  74. PATH = os.getenv("PATH")
  75. if PATH is None:
  76. qWarning("PATH variable not set")
  77. if MACOS:
  78. PATH = ("/opt/local/bin", "/usr/local/bin", "/usr/bin", "/bin")
  79. elif WINDOWS:
  80. PATH = (os.path.join(WINDIR, "system32"), WINDIR)
  81. else:
  82. PATH = ("/usr/local/bin", "/usr/bin", "/bin")
  83. else:
  84. PATH = PATH.split(os.pathsep)
  85. # ------------------------------------------------------------------------------------------------------------
  86. # Static MIDI CC list
  87. MIDI_CC_LIST = (
  88. "0x01 Modulation",
  89. "0x02 Breath",
  90. "0x03 (Undefined)",
  91. "0x04 Foot",
  92. "0x05 Portamento",
  93. "0x07 Volume",
  94. "0x08 Balance",
  95. "0x09 (Undefined)",
  96. "0x0A Pan",
  97. "0x0B Expression",
  98. "0x0C FX Control 1",
  99. "0x0D FX Control 2",
  100. "0x0E (Undefined)",
  101. "0x0F (Undefined)",
  102. "0x10 General Purpose 1",
  103. "0x11 General Purpose 2",
  104. "0x12 General Purpose 3",
  105. "0x13 General Purpose 4",
  106. "0x14 (Undefined)",
  107. "0x15 (Undefined)",
  108. "0x16 (Undefined)",
  109. "0x17 (Undefined)",
  110. "0x18 (Undefined)",
  111. "0x19 (Undefined)",
  112. "0x1A (Undefined)",
  113. "0x1B (Undefined)",
  114. "0x1C (Undefined)",
  115. "0x1D (Undefined)",
  116. "0x1E (Undefined)",
  117. "0x1F (Undefined)",
  118. "0x46 Control 1 [Variation]",
  119. "0x47 Control 2 [Timbre]",
  120. "0x48 Control 3 [Release]",
  121. "0x49 Control 4 [Attack]",
  122. "0x4A Control 5 [Brightness]",
  123. "0x4B Control 6 [Decay]",
  124. "0x4C Control 7 [Vib Rate]",
  125. "0x4D Control 8 [Vib Depth]",
  126. "0x4E Control 9 [Vib Delay]",
  127. "0x4F Control 10 [Undefined]",
  128. "0x50 General Purpose 5",
  129. "0x51 General Purpose 6",
  130. "0x52 General Purpose 7",
  131. "0x53 General Purpose 8",
  132. "0x54 Portamento Control",
  133. "0x5B FX 1 Depth [Reverb]",
  134. "0x5C FX 2 Depth [Tremolo]",
  135. "0x5D FX 3 Depth [Chorus]",
  136. "0x5E FX 4 Depth [Detune]",
  137. "0x5F FX 5 Depth [Phaser]"
  138. )
  139. # ------------------------------------------------------------------------------------------------------------
  140. # Default Plugin Folders (get)
  141. if WINDOWS:
  142. splitter = ";"
  143. APPDATA = os.getenv("APPDATA")
  144. PROGRAMFILES = os.getenv("PROGRAMFILES")
  145. PROGRAMFILESx86 = os.getenv("PROGRAMFILES(x86)")
  146. COMMONPROGRAMFILES = os.getenv("COMMONPROGRAMFILES")
  147. # Small integrity tests
  148. if not APPDATA:
  149. qFatal("APPDATA variable not set, cannot continue")
  150. sys.exit(1)
  151. if not PROGRAMFILES:
  152. qFatal("PROGRAMFILES variable not set, cannot continue")
  153. sys.exit(1)
  154. if not COMMONPROGRAMFILES:
  155. qFatal("COMMONPROGRAMFILES variable not set, cannot continue")
  156. sys.exit(1)
  157. DEFAULT_LADSPA_PATH = ";".join((os.path.join(APPDATA, "LADSPA"),
  158. os.path.join(PROGRAMFILES, "LADSPA")))
  159. DEFAULT_DSSI_PATH = ";".join((os.path.join(APPDATA, "DSSI"),
  160. os.path.join(PROGRAMFILES, "DSSI")))
  161. DEFAULT_LV2_PATH = ";".join((os.path.join(APPDATA, "LV2"),
  162. os.path.join(COMMONPROGRAMFILES, "LV2")))
  163. DEFAULT_VST_PATH = ";".join((os.path.join(PROGRAMFILES, "VstPlugins"),
  164. os.path.join(PROGRAMFILES, "Steinberg", "VstPlugins")))
  165. DEFAULT_AU_PATH = ""
  166. # TODO
  167. DEFAULT_CSOUND_PATH = ""
  168. DEFAULT_GIG_PATH = ";".join((os.path.join(APPDATA, "GIG"),))
  169. DEFAULT_SF2_PATH = ";".join((os.path.join(APPDATA, "SF2"),))
  170. DEFAULT_SFZ_PATH = ";".join((os.path.join(APPDATA, "SFZ"),))
  171. if PROGRAMFILESx86:
  172. DEFAULT_LADSPA_PATH += ";"+os.path.join(PROGRAMFILESx86, "LADSPA")
  173. DEFAULT_DSSI_PATH += ";"+os.path.join(PROGRAMFILESx86, "DSSI")
  174. DEFAULT_VST_PATH += ";"+os.path.join(PROGRAMFILESx86, "VstPlugins")
  175. DEFAULT_VST_PATH += ";"+os.path.join(PROGRAMFILESx86, "Steinberg", "VstPlugins")
  176. elif HAIKU:
  177. splitter = ":"
  178. DEFAULT_LADSPA_PATH = ":".join((os.path.join(HOME, ".ladspa"),
  179. os.path.join("/", "boot", "common", "add-ons", "ladspa")))
  180. DEFAULT_DSSI_PATH = ":".join((os.path.join(HOME, ".dssi"),
  181. os.path.join("/", "boot", "common", "add-ons", "dssi")))
  182. DEFAULT_LV2_PATH = ":".join((os.path.join(HOME, ".lv2"),
  183. os.path.join("/", "boot", "common", "add-ons", "lv2")))
  184. DEFAULT_VST_PATH = ":".join((os.path.join(HOME, ".vst"),
  185. os.path.join("/", "boot", "common", "add-ons", "vst")))
  186. DEFAULT_AU_PATH = ""
  187. # TODO
  188. DEFAULT_CSOUND_PATH = ""
  189. # TODO
  190. DEFAULT_GIG_PATH = ""
  191. DEFAULT_SF2_PATH = ""
  192. DEFAULT_SFZ_PATH = ""
  193. elif MACOS:
  194. splitter = ":"
  195. DEFAULT_LADSPA_PATH = ":".join((os.path.join(HOME, "Library", "Audio", "Plug-Ins", "LADSPA"),
  196. os.path.join("/", "Library", "Audio", "Plug-Ins", "LADSPA")))
  197. DEFAULT_DSSI_PATH = ":".join((os.path.join(HOME, "Library", "Audio", "Plug-Ins", "DSSI"),
  198. os.path.join("/", "Library", "Audio", "Plug-Ins", "DSSI")))
  199. DEFAULT_LV2_PATH = ":".join((os.path.join(HOME, "Library", "Audio", "Plug-Ins", "LV2"),
  200. os.path.join("/", "Library", "Audio", "Plug-Ins", "LV2")))
  201. DEFAULT_VST_PATH = ":".join((os.path.join(HOME, "Library", "Audio", "Plug-Ins", "VST"),
  202. os.path.join("/", "Library", "Audio", "Plug-Ins", "VST")))
  203. DEFAULT_AU_PATH = ":".join((os.path.join(HOME, "Library", "Audio", "Plug-Ins", "Components"),
  204. os.path.join("/", "Library", "Audio", "Plug-Ins", "Components")))
  205. # TODO
  206. DEFAULT_CSOUND_PATH = ""
  207. # TODO
  208. DEFAULT_GIG_PATH = ""
  209. DEFAULT_SF2_PATH = ""
  210. DEFAULT_SFZ_PATH = ""
  211. else:
  212. splitter = ":"
  213. DEFAULT_LADSPA_PATH = ":".join((os.path.join(HOME, ".ladspa"),
  214. os.path.join("/", "usr", "lib", "ladspa"),
  215. os.path.join("/", "usr", "local", "lib", "ladspa")))
  216. DEFAULT_DSSI_PATH = ":".join((os.path.join(HOME, ".dssi"),
  217. os.path.join("/", "usr", "lib", "dssi"),
  218. os.path.join("/", "usr", "local", "lib", "dssi")))
  219. DEFAULT_LV2_PATH = ":".join((os.path.join(HOME, ".lv2"),
  220. os.path.join("/", "usr", "lib", "lv2"),
  221. os.path.join("/", "usr", "local", "lib", "lv2")))
  222. DEFAULT_VST_PATH = ":".join((os.path.join(HOME, ".vst"),
  223. os.path.join("/", "usr", "lib", "vst"),
  224. os.path.join("/", "usr", "local", "lib", "vst")))
  225. DEFAULT_AU_PATH = ""
  226. # TODO
  227. DEFAULT_CSOUND_PATH = ""
  228. DEFAULT_GIG_PATH = ":".join((os.path.join(HOME, ".sounds", "gig"),
  229. os.path.join("/", "usr", "share", "sounds", "gig")))
  230. DEFAULT_SF2_PATH = ":".join((os.path.join(HOME, ".sounds", "sf2"),
  231. os.path.join("/", "usr", "share", "sounds", "sf2")))
  232. DEFAULT_SFZ_PATH = ":".join((os.path.join(HOME, ".sounds", "sfz"),
  233. os.path.join("/", "usr", "share", "sounds", "sfz")))
  234. # ------------------------------------------------------------------------------------------------------------
  235. # Global Carla object
  236. class CarlaObject(object):
  237. __slots__ = [
  238. 'host',
  239. 'gui',
  240. 'isControl',
  241. 'isLocal',
  242. 'processMode',
  243. 'maxParameters',
  244. 'DEFAULT_LADSPA_PATH',
  245. 'DEFAULT_DSSI_PATH',
  246. 'DEFAULT_LV2_PATH',
  247. 'DEFAULT_VST_PATH',
  248. 'DEFAULT_AU_PATH',
  249. 'DEFAULT_CSOUND_PATH',
  250. 'DEFAULT_GIG_PATH',
  251. 'DEFAULT_SF2_PATH',
  252. 'DEFAULT_SFZ_PATH'
  253. ]
  254. Carla = CarlaObject()
  255. Carla.host = None
  256. Carla.gui = None
  257. Carla.isControl = False
  258. Carla.isLocal = False
  259. Carla.processMode = PROCESS_MODE_MULTIPLE_CLIENTS if LINUX else PROCESS_MODE_CONTINUOUS_RACK
  260. Carla.maxParameters = MAX_DEFAULT_PARAMETERS
  261. # ------------------------------------------------------------------------------------------------------------
  262. # Default Plugin Folders (set)
  263. readEnvVars = True
  264. if WINDOWS:
  265. # Check if running Wine. If yes, ignore env vars
  266. from winreg import ConnectRegistry, OpenKey, CloseKey, HKEY_CURRENT_USER
  267. reg = ConnectRegistry(None, HKEY_CURRENT_USER)
  268. try:
  269. key = OpenKey(reg, r"SOFTWARE\Wine")
  270. CloseKey(key)
  271. readEnvVars = False
  272. except:
  273. pass
  274. CloseKey(reg)
  275. del reg
  276. if readEnvVars:
  277. Carla.DEFAULT_LADSPA_PATH = os.getenv("LADSPA_PATH", DEFAULT_LADSPA_PATH).split(splitter)
  278. Carla.DEFAULT_DSSI_PATH = os.getenv("DSSI_PATH", DEFAULT_DSSI_PATH).split(splitter)
  279. Carla.DEFAULT_LV2_PATH = os.getenv("LV2_PATH", DEFAULT_LV2_PATH).split(splitter)
  280. Carla.DEFAULT_VST_PATH = os.getenv("VST_PATH", DEFAULT_VST_PATH).split(splitter)
  281. Carla.DEFAULT_AU_PATH = os.getenv("AU_PATH", DEFAULT_AU_PATH).split(splitter)
  282. Carla.DEFAULT_CSOUND_PATH = os.getenv("CSOUND_PATH", DEFAULT_CSOUND_PATH).split(splitter)
  283. Carla.DEFAULT_GIG_PATH = os.getenv("GIG_PATH", DEFAULT_GIG_PATH).split(splitter)
  284. Carla.DEFAULT_SF2_PATH = os.getenv("SF2_PATH", DEFAULT_SF2_PATH).split(splitter)
  285. Carla.DEFAULT_SFZ_PATH = os.getenv("SFZ_PATH", DEFAULT_SFZ_PATH).split(splitter)
  286. else:
  287. Carla.DEFAULT_LADSPA_PATH = DEFAULT_LADSPA_PATH.split(splitter)
  288. Carla.DEFAULT_DSSI_PATH = DEFAULT_DSSI_PATH.split(splitter)
  289. Carla.DEFAULT_LV2_PATH = DEFAULT_LV2_PATH.split(splitter)
  290. Carla.DEFAULT_VST_PATH = DEFAULT_VST_PATH.split(splitter)
  291. Carla.DEFAULT_AU_PATH = DEFAULT_AU_PATH.split(splitter)
  292. Carla.DEFAULT_CSOUND_PATH = DEFAULT_CSOUND_PATH.split(splitter)
  293. Carla.DEFAULT_GIG_PATH = DEFAULT_GIG_PATH.split(splitter)
  294. Carla.DEFAULT_SF2_PATH = DEFAULT_SF2_PATH.split(splitter)
  295. Carla.DEFAULT_SFZ_PATH = DEFAULT_SFZ_PATH.split(splitter)
  296. # ------------------------------------------------------------------------------------------------------------
  297. # Search for Carla library and tools
  298. carla_library_filename = ""
  299. carla_discovery_native = ""
  300. carla_discovery_posix32 = ""
  301. carla_discovery_posix64 = ""
  302. carla_discovery_win32 = ""
  303. carla_discovery_win64 = ""
  304. carla_bridge_native = ""
  305. carla_bridge_posix32 = ""
  306. carla_bridge_posix64 = ""
  307. carla_bridge_win32 = ""
  308. carla_bridge_win64 = ""
  309. carla_bridge_lv2_external = ""
  310. carla_bridge_lv2_gtk2 = ""
  311. carla_bridge_lv2_gtk3 = ""
  312. carla_bridge_lv2_qt4 = ""
  313. carla_bridge_lv2_qt5 = ""
  314. carla_bridge_lv2_cocoa = ""
  315. carla_bridge_lv2_windows = ""
  316. carla_bridge_lv2_x11 = ""
  317. carla_bridge_vst_mac = ""
  318. carla_bridge_vst_hwnd = ""
  319. carla_bridge_vst_x11 = ""
  320. carla_libname = "libcarla_%s" % "control" if Carla.isControl else "standalone"
  321. if WINDOWS:
  322. carla_libname += ".dll"
  323. elif MACOS:
  324. carla_libname += ".dylib"
  325. else:
  326. carla_libname += ".so"
  327. CWD = sys.path[0]
  328. # make it work with cxfreeze
  329. if CWD.endswith("/carla"):
  330. CWD = CWD.rsplit("/carla", 1)[0]
  331. elif CWD.endswith("\\carla.exe"):
  332. CWD = CWD.rsplit("\\carla.exe", 1)[0]
  333. # find carla_library_filename
  334. if os.path.exists(os.path.join(CWD, "backend", carla_libname)):
  335. carla_library_filename = os.path.join(CWD, "backend", carla_libname)
  336. else:
  337. if WINDOWS:
  338. CARLA_PATH = (os.path.join(PROGRAMFILES, "Carla"),)
  339. elif MACOS:
  340. CARLA_PATH = ("/opt/local/lib", "/usr/local/lib/", "/usr/lib")
  341. else:
  342. CARLA_PATH = ("/usr/local/lib/", "/usr/lib")
  343. for path in CARLA_PATH:
  344. if os.path.exists(os.path.join(path, "carla", carla_libname)):
  345. carla_library_filename = os.path.join(path, "carla", carla_libname)
  346. break
  347. del CARLA_PATH
  348. # find tool
  349. def findTool(toolDir, toolName):
  350. if os.path.exists(os.path.join(CWD, toolDir, toolName)):
  351. return os.path.join(CWD, toolDir, toolName)
  352. for p in PATH:
  353. if os.path.exists(os.path.join(p, toolName)):
  354. return os.path.join(p, toolName)
  355. return ""
  356. # find windows tools
  357. carla_discovery_win32 = findTool("discovery", "carla-discovery-win32.exe")
  358. carla_discovery_win64 = findTool("discovery", "carla-discovery-win64.exe")
  359. carla_bridge_win32 = findTool("bridges", "carla-bridge-win32.exe")
  360. carla_bridge_win64 = findTool("bridges", "carla-bridge-win64.exe")
  361. # find native and posix tools
  362. if not WINDOWS:
  363. carla_discovery_native = findTool("discovery", "carla-discovery-native")
  364. carla_discovery_posix32 = findTool("discovery", "carla-discovery-posix32")
  365. carla_discovery_posix64 = findTool("discovery", "carla-discovery-posix64")
  366. carla_bridge_native = findTool("bridges", "carla-bridge-native")
  367. carla_bridge_posix32 = findTool("bridges", "carla-bridge-posix32")
  368. carla_bridge_posix64 = findTool("bridges", "carla-bridge-posix64")
  369. # find generic tools
  370. carla_bridge_lv2_external = findTool("bridges", "carla-bridge-lv2-external")
  371. # find windows only tools
  372. if WINDOWS:
  373. carla_bridge_lv2_windows = findTool("bridges", "carla-bridge-lv2-windows.exe")
  374. carla_bridge_vst_hwnd = findTool("bridges", "carla-bridge-vst-hwnd.exe")
  375. # find mac os only tools
  376. elif MACOS:
  377. carla_bridge_lv2_cocoa = findTool("bridges", "carla-bridge-lv2-cocoa")
  378. carla_bridge_vst_mac = findTool("bridges", "carla-bridge-vst-mac")
  379. # find other tools
  380. else:
  381. carla_bridge_lv2_gtk2 = findTool("bridges", "carla-bridge-lv2-gtk2")
  382. carla_bridge_lv2_gtk3 = findTool("bridges", "carla-bridge-lv2-gtk3")
  383. carla_bridge_lv2_qt4 = findTool("bridges", "carla-bridge-lv2-qt4")
  384. carla_bridge_lv2_qt5 = findTool("bridges", "carla-bridge-lv2-qt5")
  385. carla_bridge_lv2_x11 = findTool("bridges", "carla-bridge-lv2-x11")
  386. carla_bridge_vst_x11 = findTool("bridges", "carla-bridge-vst-x11")
  387. # ------------------------------------------------------------------------------------------------------------
  388. # Convert a ctypes c_char_p into a python string
  389. def cString(value):
  390. if not value:
  391. return ""
  392. if isinstance(value, str):
  393. return value
  394. return value.decode("utf-8", errors="ignore")
  395. # ------------------------------------------------------------------------------------------------------------
  396. # Check if a value is a number (float support)
  397. def isNumber(value):
  398. try:
  399. float(value)
  400. return True
  401. except:
  402. return False
  403. # ------------------------------------------------------------------------------------------------------------
  404. # Convert a value to a list
  405. def toList(value):
  406. if value is None:
  407. return []
  408. elif not isinstance(value, list):
  409. return [value]
  410. else:
  411. return value
  412. # ------------------------------------------------------------------------------------------------------------
  413. # Get Icon from user theme, using our own as backup (Oxygen)
  414. def getIcon(icon, size=16):
  415. return QIcon.fromTheme(icon, QIcon(":/%ix%i/%s.png" % (size, size, icon)))
  416. # ------------------------------------------------------------------------------------------------------------
  417. # Signal handler
  418. def signalHandler(sig, frame):
  419. if Carla.gui is None:
  420. return
  421. if sig in (SIGINT, SIGTERM):
  422. Carla.gui.SIGTERM.emit()
  423. elif haveSIGUSR1 and sig == SIGUSR1:
  424. Carla.gui.SIGUSR1.emit()
  425. def setUpSignals():
  426. signal(SIGINT, signalHandler)
  427. signal(SIGTERM, signalHandler)
  428. if not haveSIGUSR1:
  429. return
  430. signal(SIGUSR1, signalHandler)
  431. # ------------------------------------------------------------------------------------------------------------
  432. # QLineEdit and QPushButton combo
  433. def getAndSetPath(self_, currentPath, lineEdit):
  434. newPath = QFileDialog.getExistingDirectory(self_, self_.tr("Set Path"), currentPath, QFileDialog.ShowDirsOnly)
  435. if newPath:
  436. lineEdit.setText(newPath)
  437. return newPath
  438. # ------------------------------------------------------------------------------------------------------------
  439. # Custom MessageBox
  440. def CustomMessageBox(self_, icon, title, text, extraText="", buttons=QMessageBox.Yes|QMessageBox.No, defButton=QMessageBox.No):
  441. msgBox = QMessageBox(self_)
  442. msgBox.setIcon(icon)
  443. msgBox.setWindowTitle(title)
  444. msgBox.setText(text)
  445. msgBox.setInformativeText(extraText)
  446. msgBox.setStandardButtons(buttons)
  447. msgBox.setDefaultButton(defButton)
  448. return msgBox.exec_()