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.

511 lines
17KB

  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Carla Backend utils
  4. # Copyright (C) 2011-2020 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. from os import environ
  20. from sys import argv
  21. # ------------------------------------------------------------------------------------------------------------
  22. # Imports (ctypes)
  23. from ctypes import (
  24. c_bool, c_char_p, c_double, c_int, c_uint, c_uint32, c_void_p,
  25. cdll, Structure,
  26. CFUNCTYPE, POINTER
  27. )
  28. # ------------------------------------------------------------------------------------------------------------
  29. # Imports (Custom)
  30. from carla_backend import (
  31. PLUGIN_NONE,
  32. PLUGIN_INTERNAL,
  33. PLUGIN_LADSPA,
  34. PLUGIN_DSSI,
  35. PLUGIN_LV2,
  36. PLUGIN_VST2,
  37. PLUGIN_VST3,
  38. PLUGIN_AU,
  39. PLUGIN_JSFX,
  40. PLUGIN_DLS,
  41. PLUGIN_GIG,
  42. PLUGIN_SF2,
  43. PLUGIN_SFZ,
  44. PLUGIN_JACK,
  45. PLUGIN_CATEGORY_NONE,
  46. PLUGIN_CATEGORY_SYNTH,
  47. PLUGIN_CATEGORY_DELAY,
  48. PLUGIN_CATEGORY_EQ,
  49. PLUGIN_CATEGORY_FILTER,
  50. PLUGIN_CATEGORY_DISTORTION,
  51. PLUGIN_CATEGORY_DYNAMICS,
  52. PLUGIN_CATEGORY_MODULATOR,
  53. PLUGIN_CATEGORY_UTILITY,
  54. PLUGIN_CATEGORY_OTHER,
  55. WINDOWS,
  56. c_enum, c_uintptr,
  57. charPtrToString,
  58. charPtrPtrToStringList,
  59. structToDict
  60. )
  61. # ------------------------------------------------------------------------------------------------------------
  62. def getPluginTypeAsString(ptype):
  63. if ptype == PLUGIN_NONE:
  64. return "NONE"
  65. if ptype == PLUGIN_INTERNAL:
  66. return "Internal"
  67. if ptype == PLUGIN_LADSPA:
  68. return "LADSPA"
  69. if ptype == PLUGIN_DSSI:
  70. return "DSSI"
  71. if ptype == PLUGIN_LV2:
  72. return "LV2"
  73. if ptype == PLUGIN_VST2:
  74. return "VST2"
  75. if ptype == PLUGIN_VST3:
  76. return "VST3"
  77. if ptype == PLUGIN_AU:
  78. return "AU"
  79. if ptype == PLUGIN_JSFX:
  80. return "JSFX"
  81. if ptype == PLUGIN_DLS:
  82. return "DLS"
  83. if ptype == PLUGIN_GIG:
  84. return "GIG"
  85. if ptype == PLUGIN_SF2:
  86. return "SF2"
  87. if ptype == PLUGIN_SFZ:
  88. return "SFZ"
  89. if ptype == PLUGIN_JACK:
  90. return "JACK"
  91. print("getPluginTypeAsString(%i) - invalid type" % ptype)
  92. return "Unknown"
  93. def getPluginTypeFromString(stype):
  94. if not stype:
  95. return PLUGIN_NONE
  96. stype = stype.lower()
  97. if stype == "none":
  98. return PLUGIN_NONE
  99. if stype in ("internal", "native"):
  100. return PLUGIN_INTERNAL
  101. if stype == "ladspa":
  102. return PLUGIN_LADSPA
  103. if stype == "dssi":
  104. return PLUGIN_DSSI
  105. if stype == "lv2":
  106. return PLUGIN_LV2
  107. if stype in ("vst2", "vst"):
  108. return PLUGIN_VST2
  109. if stype == "vst3":
  110. return PLUGIN_VST3
  111. if stype in ("au", "audiounit"):
  112. return PLUGIN_AU
  113. if stype == "jsfx":
  114. return PLUGIN_JSFX
  115. if stype == "dls":
  116. return PLUGIN_DLS
  117. if stype == "gig":
  118. return PLUGIN_GIG
  119. if stype == "sf2":
  120. return PLUGIN_SF2
  121. if stype == "sfz":
  122. return PLUGIN_SFZ
  123. if stype == "jack":
  124. return PLUGIN_JACK
  125. print("getPluginTypeFromString(\"%s\") - invalid string type" % stype)
  126. return PLUGIN_NONE
  127. def getPluginCategoryAsString(category):
  128. if category == PLUGIN_CATEGORY_NONE:
  129. return "none"
  130. if category == PLUGIN_CATEGORY_SYNTH:
  131. return "synth"
  132. if category == PLUGIN_CATEGORY_DELAY:
  133. return "delay"
  134. if category == PLUGIN_CATEGORY_EQ:
  135. return "eq"
  136. if category == PLUGIN_CATEGORY_FILTER:
  137. return "filter"
  138. if category == PLUGIN_CATEGORY_DISTORTION:
  139. return "distortion"
  140. if category == PLUGIN_CATEGORY_DYNAMICS:
  141. return "dynamics"
  142. if category == PLUGIN_CATEGORY_MODULATOR:
  143. return "modulator"
  144. if category == PLUGIN_CATEGORY_UTILITY:
  145. return "utility"
  146. if category == PLUGIN_CATEGORY_OTHER:
  147. return "other"
  148. print("CarlaBackend::getPluginCategoryAsString(%i) - invalid category" % category)
  149. return "NONE"
  150. # ------------------------------------------------------------------------------------------------------------
  151. # Carla Utils API (C stuff)
  152. CarlaPipeClientHandle = c_void_p
  153. CarlaPipeCallbackFunc = CFUNCTYPE(None, c_void_p, c_char_p)
  154. # Information about an internal Carla plugin.
  155. # @see carla_get_cached_plugin_info()
  156. class CarlaCachedPluginInfo(Structure):
  157. _fields_ = [
  158. # Wherever the data in this struct is valid.
  159. # For performance reasons, plugins are only checked on request,
  160. # and as such, the count vs number of really valid plugins might not match.
  161. # Use this field to skip on plugins which cannot be loaded in Carla.
  162. ("valid", c_bool),
  163. # Plugin category.
  164. ("category", c_enum),
  165. # Plugin hints.
  166. # @see PluginHints
  167. ("hints", c_uint),
  168. # Number of audio inputs.
  169. ("audioIns", c_uint32),
  170. # Number of audio outputs.
  171. ("audioOuts", c_uint32),
  172. # Number of CV inputs.
  173. ("cvIns", c_uint32),
  174. # Number of CV outputs.
  175. ("cvOuts", c_uint32),
  176. # Number of MIDI inputs.
  177. ("midiIns", c_uint32),
  178. # Number of MIDI outputs.
  179. ("midiOuts", c_uint32),
  180. # Number of input parameters.
  181. ("parameterIns", c_uint32),
  182. # Number of output parameters.
  183. ("parameterOuts", c_uint32),
  184. # Plugin name.
  185. ("name", c_char_p),
  186. # Plugin label.
  187. ("label", c_char_p),
  188. # Plugin author/maker.
  189. ("maker", c_char_p),
  190. # Plugin copyright/license.
  191. ("copyright", c_char_p)
  192. ]
  193. # ------------------------------------------------------------------------------------------------------------
  194. # Carla Utils API (Python compatible stuff)
  195. # @see CarlaCachedPluginInfo
  196. PyCarlaCachedPluginInfo = {
  197. 'valid': False,
  198. 'category': PLUGIN_CATEGORY_NONE,
  199. 'hints': 0x0,
  200. 'audioIns': 0,
  201. 'audioOuts': 0,
  202. 'cvIns': 0,
  203. 'cvOuts': 0,
  204. 'midiIns': 0,
  205. 'midiOuts': 0,
  206. 'parameterIns': 0,
  207. 'parameterOuts': 0,
  208. 'name': "",
  209. 'label': "",
  210. 'maker': "",
  211. 'copyright': ""
  212. }
  213. # ------------------------------------------------------------------------------------------------------------
  214. # Carla Utils object using a DLL
  215. class CarlaUtils():
  216. def __init__(self, filename):
  217. self.lib = cdll.LoadLibrary(filename)
  218. #self.lib = CDLL(filename, RTLD_GLOBAL)
  219. self.lib.carla_get_complete_license_text.argtypes = None
  220. self.lib.carla_get_complete_license_text.restype = c_char_p
  221. self.lib.carla_get_juce_version.argtypes = None
  222. self.lib.carla_get_juce_version.restype = c_char_p
  223. self.lib.carla_get_supported_file_extensions.argtypes = None
  224. self.lib.carla_get_supported_file_extensions.restype = POINTER(c_char_p)
  225. self.lib.carla_get_supported_features.argtypes = None
  226. self.lib.carla_get_supported_features.restype = POINTER(c_char_p)
  227. self.lib.carla_get_cached_plugin_count.argtypes = [c_enum, c_char_p]
  228. self.lib.carla_get_cached_plugin_count.restype = c_uint
  229. self.lib.carla_get_cached_plugin_info.argtypes = [c_enum, c_uint]
  230. self.lib.carla_get_cached_plugin_info.restype = POINTER(CarlaCachedPluginInfo)
  231. self.lib.carla_fflush.argtypes = [c_bool]
  232. self.lib.carla_fflush.restype = None
  233. self.lib.carla_fputs.argtypes = [c_bool, c_char_p]
  234. self.lib.carla_fputs.restype = None
  235. self.lib.carla_set_process_name.argtypes = [c_char_p]
  236. self.lib.carla_set_process_name.restype = None
  237. self.lib.carla_pipe_client_new.argtypes = [POINTER(c_char_p), CarlaPipeCallbackFunc, c_void_p]
  238. self.lib.carla_pipe_client_new.restype = CarlaPipeClientHandle
  239. self.lib.carla_pipe_client_idle.argtypes = [CarlaPipeClientHandle]
  240. self.lib.carla_pipe_client_idle.restype = None
  241. self.lib.carla_pipe_client_is_running.argtypes = [CarlaPipeClientHandle]
  242. self.lib.carla_pipe_client_is_running.restype = c_bool
  243. self.lib.carla_pipe_client_lock.argtypes = [CarlaPipeClientHandle]
  244. self.lib.carla_pipe_client_lock.restype = None
  245. self.lib.carla_pipe_client_unlock.argtypes = [CarlaPipeClientHandle]
  246. self.lib.carla_pipe_client_unlock.restype = None
  247. self.lib.carla_pipe_client_readlineblock.argtypes = [CarlaPipeClientHandle, c_uint]
  248. self.lib.carla_pipe_client_readlineblock.restype = c_char_p
  249. self.lib.carla_pipe_client_readlineblock_bool.argtypes = [CarlaPipeClientHandle, c_uint]
  250. self.lib.carla_pipe_client_readlineblock_bool.restype = c_bool
  251. self.lib.carla_pipe_client_readlineblock_int.argtypes = [CarlaPipeClientHandle, c_uint]
  252. self.lib.carla_pipe_client_readlineblock_int.restype = c_int
  253. self.lib.carla_pipe_client_readlineblock_float.argtypes = [CarlaPipeClientHandle, c_uint]
  254. self.lib.carla_pipe_client_readlineblock_float.restype = c_double
  255. self.lib.carla_pipe_client_write_msg.argtypes = [CarlaPipeClientHandle, c_char_p]
  256. self.lib.carla_pipe_client_write_msg.restype = c_bool
  257. self.lib.carla_pipe_client_write_and_fix_msg.argtypes = [CarlaPipeClientHandle, c_char_p]
  258. self.lib.carla_pipe_client_write_and_fix_msg.restype = c_bool
  259. self.lib.carla_pipe_client_flush.argtypes = [CarlaPipeClientHandle]
  260. self.lib.carla_pipe_client_flush.restype = c_bool
  261. self.lib.carla_pipe_client_flush_and_unlock.argtypes = [CarlaPipeClientHandle]
  262. self.lib.carla_pipe_client_flush_and_unlock.restype = c_bool
  263. self.lib.carla_pipe_client_destroy.argtypes = [CarlaPipeClientHandle]
  264. self.lib.carla_pipe_client_destroy.restype = None
  265. self.lib.carla_juce_init.argtypes = None
  266. self.lib.carla_juce_init.restype = None
  267. self.lib.carla_juce_idle.argtypes = None
  268. self.lib.carla_juce_idle.restype = None
  269. self.lib.carla_juce_cleanup.argtypes = None
  270. self.lib.carla_juce_cleanup.restype = None
  271. self.lib.carla_get_desktop_scale_factor.argtypes = None
  272. self.lib.carla_get_desktop_scale_factor.restype = c_double
  273. self.lib.carla_cocoa_get_window.argtypes = [c_uintptr]
  274. self.lib.carla_cocoa_get_window.restype = c_int
  275. self.lib.carla_cocoa_set_transient_window_for.argtypes = [c_uintptr, c_uintptr]
  276. self.lib.carla_cocoa_set_transient_window_for.restype = None
  277. self.lib.carla_x11_reparent_window.argtypes = [c_uintptr, c_uintptr]
  278. self.lib.carla_x11_reparent_window.restype = None
  279. self.lib.carla_x11_move_window.argtypes = [c_uintptr, c_int, c_int]
  280. self.lib.carla_x11_move_window.restype = None
  281. self.lib.carla_x11_get_window_pos.argtypes = [c_uintptr]
  282. self.lib.carla_x11_get_window_pos.restype = POINTER(c_int)
  283. self._pipeClientFunc = None
  284. self._pipeClientCallback = None
  285. # use _putenv on windows
  286. if not WINDOWS:
  287. self.msvcrt = None
  288. return
  289. self.msvcrt = cdll.msvcrt
  290. # pylint: disable=protected-access
  291. self.msvcrt._putenv.argtypes = [c_char_p]
  292. self.msvcrt._putenv.restype = None
  293. # pylint: enable=protected-access
  294. # --------------------------------------------------------------------------------------------------------
  295. # set environment variable
  296. def setenv(self, key, value):
  297. environ[key] = value
  298. if WINDOWS:
  299. keyvalue = "%s=%s" % (key, value)
  300. # pylint: disable=protected-access
  301. self.msvcrt._putenv(keyvalue.encode("utf-8"))
  302. # pylint: enable=protected-access
  303. # unset environment variable
  304. def unsetenv(self, key):
  305. if environ.get(key) is not None:
  306. environ.pop(key)
  307. if WINDOWS:
  308. keyrm = "%s=" % key
  309. # pylint: disable=protected-access
  310. self.msvcrt._putenv(keyrm.encode("utf-8"))
  311. # pylint: enable=protected-access
  312. # --------------------------------------------------------------------------------------------------------
  313. # Get the complete license text of used third-party code and features.
  314. # Returned string is in basic html format.
  315. def get_complete_license_text(self):
  316. return charPtrToString(self.lib.carla_get_complete_license_text())
  317. # Get the juce version used in the current Carla build.
  318. def get_juce_version(self):
  319. return charPtrToString(self.lib.carla_get_juce_version())
  320. # Get the list of supported file extensions in carla_load_file().
  321. def get_supported_file_extensions(self):
  322. return charPtrPtrToStringList(self.lib.carla_get_supported_file_extensions())
  323. # Get the list of supported features in the current Carla build.
  324. def get_supported_features(self):
  325. return charPtrPtrToStringList(self.lib.carla_get_supported_features())
  326. # Get how many internal plugins are available.
  327. # Internal and LV2 plugin formats are cached and need to be discovered via this function.
  328. # Do not call this for any other plugin formats.
  329. def get_cached_plugin_count(self, ptype, pluginPath):
  330. return int(self.lib.carla_get_cached_plugin_count(ptype, pluginPath.encode("utf-8")))
  331. # Get information about a cached plugin.
  332. def get_cached_plugin_info(self, ptype, index):
  333. return structToDict(self.lib.carla_get_cached_plugin_info(ptype, index).contents)
  334. def fflush(self, err):
  335. self.lib.carla_fflush(err)
  336. def fputs(self, err, string):
  337. self.lib.carla_fputs(err, string.encode("utf-8"))
  338. def set_process_name(self, name):
  339. self.lib.carla_set_process_name(name.encode("utf-8"))
  340. def pipe_client_new(self, func):
  341. argc = len(argv)
  342. cagrvtype = c_char_p * argc
  343. cargv = cagrvtype()
  344. for i in range(argc):
  345. cargv[i] = c_char_p(argv[i].encode("utf-8"))
  346. self._pipeClientFunc = func
  347. self._pipeClientCallback = CarlaPipeCallbackFunc(func)
  348. return self.lib.carla_pipe_client_new(cargv, self._pipeClientCallback, None)
  349. def pipe_client_idle(self, handle):
  350. try:
  351. self.lib.carla_pipe_client_idle(handle)
  352. except OSError as e:
  353. print("carla_pipe_client_idle", e)
  354. def pipe_client_is_running(self, handle):
  355. return bool(self.lib.carla_pipe_client_is_running(handle))
  356. def pipe_client_lock(self, handle):
  357. self.lib.carla_pipe_client_lock(handle)
  358. def pipe_client_unlock(self, handle):
  359. self.lib.carla_pipe_client_unlock(handle)
  360. def pipe_client_readlineblock(self, handle, timeout):
  361. return charPtrToString(self.lib.carla_pipe_client_readlineblock(handle, timeout))
  362. def pipe_client_readlineblock_bool(self, handle, timeout):
  363. return bool(self.lib.carla_pipe_client_readlineblock_bool(handle, timeout))
  364. def pipe_client_readlineblock_int(self, handle, timeout):
  365. return int(self.lib.carla_pipe_client_readlineblock_int(handle, timeout))
  366. def pipe_client_readlineblock_float(self, handle, timeout):
  367. return float(self.lib.carla_pipe_client_readlineblock_float(handle, timeout))
  368. def pipe_client_write_msg(self, handle, msg):
  369. return bool(self.lib.carla_pipe_client_write_msg(handle, msg.encode("utf-8")))
  370. def pipe_client_write_and_fix_msg(self, handle, msg):
  371. return bool(self.lib.carla_pipe_client_write_and_fix_msg(handle, msg.encode("utf-8")))
  372. def pipe_client_flush(self, handle):
  373. return bool(self.lib.carla_pipe_client_flush(handle))
  374. def pipe_client_flush_and_unlock(self, handle):
  375. return bool(self.lib.carla_pipe_client_flush_and_unlock(handle))
  376. def pipe_client_destroy(self, handle):
  377. self.lib.carla_pipe_client_destroy(handle)
  378. def juce_init(self):
  379. self.lib.carla_juce_init()
  380. def juce_idle(self):
  381. self.lib.carla_juce_idle()
  382. def juce_cleanup(self):
  383. self.lib.carla_juce_cleanup()
  384. def get_desktop_scale_factor(self):
  385. return float(self.lib.carla_get_desktop_scale_factor())
  386. def cocoa_get_window(self, winId):
  387. return self.lib.carla_cocoa_get_window(winId)
  388. def cocoa_set_transient_window_for(self, childWinId, parentWinId):
  389. self.lib.carla_cocoa_set_transient_window_for(childWinId, parentWinId)
  390. def x11_reparent_window(self, winId1, winId2):
  391. self.lib.carla_x11_reparent_window(winId1, winId2)
  392. def x11_move_window(self, winId, x, y):
  393. self.lib.carla_x11_move_window(winId, x, y)
  394. def x11_get_window_pos(self, winId):
  395. data = self.lib.carla_x11_get_window_pos(winId)
  396. return tuple(int(data[i]) for i in range(4))
  397. # ------------------------------------------------------------------------------------------------------------