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.

494 lines
16KB

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