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.

carla_utils.py 16KB

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