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.

379 lines
13KB

  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Carla Backend utils
  4. # Copyright (C) 2011-2018 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 (Custom)
  23. from carla_backend import *
  24. # ------------------------------------------------------------------------------------------------------------
  25. def getPluginTypeAsString(ptype):
  26. if ptype == PLUGIN_NONE:
  27. return "NONE"
  28. if ptype == PLUGIN_INTERNAL:
  29. return "Internal"
  30. if ptype == PLUGIN_LADSPA:
  31. return "LADSPA"
  32. if ptype == PLUGIN_DSSI:
  33. return "DSSI"
  34. if ptype == PLUGIN_LV2:
  35. return "LV2"
  36. if ptype == PLUGIN_VST2:
  37. return "VST2"
  38. if ptype == PLUGIN_VST3:
  39. return "VST3"
  40. if ptype == PLUGIN_AU:
  41. return "AU"
  42. if ptype == PLUGIN_SF2:
  43. return "SF2"
  44. if ptype == PLUGIN_SFZ:
  45. return "SFZ"
  46. if ptype == PLUGIN_JACK:
  47. return "JACK"
  48. print("getPluginTypeAsString(%i) - invalid type" % ptype);
  49. return "Unknown"
  50. def getPluginTypeFromString(stype):
  51. if not stype:
  52. return PLUGIN_NONE
  53. stype = stype.lower()
  54. if stype == "none":
  55. return PLUGIN_NONE
  56. if stype in ("internal", "native"):
  57. return PLUGIN_INTERNAL
  58. if stype == "ladspa":
  59. return PLUGIN_LADSPA
  60. if stype == "dssi":
  61. return PLUGIN_DSSI
  62. if stype == "lv2":
  63. return PLUGIN_LV2
  64. if stype in ("vst2", "vst"):
  65. return PLUGIN_VST2
  66. if stype == "vst3":
  67. return PLUGIN_VST3
  68. if stype in ("au", "audiounit"):
  69. return PLUGIN_AU
  70. if stype == "sf2":
  71. return PLUGIN_SF2
  72. if stype == "sfz":
  73. return PLUGIN_SFZ
  74. if stype == "jack":
  75. return PLUGIN_JACK
  76. print("getPluginTypeFromString(\"%s\") - invalid string type" % stype)
  77. return PLUGIN_NONE
  78. # ------------------------------------------------------------------------------------------------------------
  79. # Carla Utils API (C stuff)
  80. CarlaPipeClientHandle = c_void_p
  81. CarlaPipeCallbackFunc = CFUNCTYPE(None, c_void_p, c_char_p)
  82. # Information about an internal Carla plugin.
  83. # @see carla_get_cached_plugin_info()
  84. class CarlaCachedPluginInfo(Structure):
  85. _fields_ = [
  86. # Wherever the data in this struct is valid.
  87. # For performance reasons, plugins are only checked on request,
  88. # and as such, the count vs number of really valid plugins might not match.
  89. # Use this field to skip on plugins which cannot be loaded in Carla.
  90. ("valid", c_bool),
  91. # Plugin category.
  92. ("category", c_enum),
  93. # Plugin hints.
  94. # @see PluginHints
  95. ("hints", c_uint),
  96. # Number of audio inputs.
  97. ("audioIns", c_uint32),
  98. # Number of audio outputs.
  99. ("audioOuts", c_uint32),
  100. # Number of MIDI inputs.
  101. ("midiIns", c_uint32),
  102. # Number of MIDI outputs.
  103. ("midiOuts", c_uint32),
  104. # Number of input parameters.
  105. ("parameterIns", c_uint32),
  106. # Number of output parameters.
  107. ("parameterOuts", c_uint32),
  108. # Plugin name.
  109. ("name", c_char_p),
  110. # Plugin label.
  111. ("label", c_char_p),
  112. # Plugin author/maker.
  113. ("maker", c_char_p),
  114. # Plugin copyright/license.
  115. ("copyright", c_char_p)
  116. ]
  117. # ------------------------------------------------------------------------------------------------------------
  118. # Carla Utils API (Python compatible stuff)
  119. # @see CarlaCachedPluginInfo
  120. PyCarlaCachedPluginInfo = {
  121. 'valid': False,
  122. 'category': PLUGIN_CATEGORY_NONE,
  123. 'hints': 0x0,
  124. 'audioIns': 0,
  125. 'audioOuts': 0,
  126. 'midiIns': 0,
  127. 'midiOuts': 0,
  128. 'parameterIns': 0,
  129. 'parameterOuts': 0,
  130. 'name': "",
  131. 'label': "",
  132. 'maker': "",
  133. 'copyright': ""
  134. }
  135. # ------------------------------------------------------------------------------------------------------------
  136. # Carla Utils object using a DLL
  137. class CarlaUtils(object):
  138. def __init__(self, filename):
  139. object.__init__(self)
  140. self.lib = cdll.LoadLibrary(filename)
  141. #self.lib = CDLL(filename, RTLD_GLOBAL)
  142. self.lib.carla_get_complete_license_text.argtypes = None
  143. self.lib.carla_get_complete_license_text.restype = c_char_p
  144. self.lib.carla_get_juce_version.argtypes = None
  145. self.lib.carla_get_juce_version.restype = c_char_p
  146. self.lib.carla_get_supported_file_extensions.argtypes = None
  147. self.lib.carla_get_supported_file_extensions.restype = POINTER(c_char_p)
  148. self.lib.carla_get_supported_features.argtypes = None
  149. self.lib.carla_get_supported_features.restype = POINTER(c_char_p)
  150. self.lib.carla_get_cached_plugin_count.argtypes = [c_enum, c_char_p]
  151. self.lib.carla_get_cached_plugin_count.restype = c_uint
  152. self.lib.carla_get_cached_plugin_info.argtypes = [c_enum, c_uint]
  153. self.lib.carla_get_cached_plugin_info.restype = POINTER(CarlaCachedPluginInfo)
  154. self.lib.carla_fflush.argtypes = [c_bool]
  155. self.lib.carla_fflush.restype = None
  156. self.lib.carla_fputs.argtypes = [c_bool, c_char_p]
  157. self.lib.carla_fputs.restype = None
  158. self.lib.carla_set_process_name.argtypes = [c_char_p]
  159. self.lib.carla_set_process_name.restype = None
  160. self.lib.carla_pipe_client_new.argtypes = [POINTER(c_char_p), CarlaPipeCallbackFunc, c_void_p]
  161. self.lib.carla_pipe_client_new.restype = CarlaPipeClientHandle
  162. self.lib.carla_pipe_client_idle.argtypes = [CarlaPipeClientHandle]
  163. self.lib.carla_pipe_client_idle.restype = c_char_p
  164. self.lib.carla_pipe_client_is_running.argtypes = [CarlaPipeClientHandle]
  165. self.lib.carla_pipe_client_is_running.restype = c_bool
  166. self.lib.carla_pipe_client_lock.argtypes = [CarlaPipeClientHandle]
  167. self.lib.carla_pipe_client_lock.restype = None
  168. self.lib.carla_pipe_client_unlock.argtypes = [CarlaPipeClientHandle]
  169. self.lib.carla_pipe_client_unlock.restype = None
  170. self.lib.carla_pipe_client_readlineblock.argtypes = [CarlaPipeClientHandle, c_uint]
  171. self.lib.carla_pipe_client_readlineblock.restype = c_char_p
  172. self.lib.carla_pipe_client_write_msg.argtypes = [CarlaPipeClientHandle, c_char_p]
  173. self.lib.carla_pipe_client_write_msg.restype = c_bool
  174. self.lib.carla_pipe_client_write_and_fix_msg.argtypes = [CarlaPipeClientHandle, c_char_p]
  175. self.lib.carla_pipe_client_write_and_fix_msg.restype = c_bool
  176. self.lib.carla_pipe_client_flush.argtypes = [CarlaPipeClientHandle]
  177. self.lib.carla_pipe_client_flush.restype = c_bool
  178. self.lib.carla_pipe_client_flush_and_unlock.argtypes = [CarlaPipeClientHandle]
  179. self.lib.carla_pipe_client_flush_and_unlock.restype = c_bool
  180. self.lib.carla_pipe_client_destroy.argtypes = [CarlaPipeClientHandle]
  181. self.lib.carla_pipe_client_destroy.restype = None
  182. self.lib.carla_cocoa_get_window.argtypes = [c_uintptr]
  183. self.lib.carla_cocoa_get_window.restype = c_int
  184. self.lib.carla_x11_reparent_window.argtypes = [c_uintptr, c_uintptr]
  185. self.lib.carla_x11_reparent_window.restype = None
  186. self.lib.carla_x11_move_window.argtypes = [c_uintptr, c_int, c_int]
  187. self.lib.carla_x11_move_window.restype = None
  188. self.lib.carla_x11_get_window_pos.argtypes = [c_uintptr]
  189. self.lib.carla_x11_get_window_pos.restype = POINTER(c_int)
  190. # use _putenv on windows
  191. if not WINDOWS:
  192. self.msvcrt = None
  193. return
  194. self.msvcrt = cdll.msvcrt
  195. self.msvcrt._putenv.argtypes = [c_char_p]
  196. self.msvcrt._putenv.restype = None
  197. # --------------------------------------------------------------------------------------------------------
  198. # set environment variable
  199. def setenv(self, key, value):
  200. environ[key] = value
  201. if WINDOWS:
  202. keyvalue = "%s=%s" % (key, value)
  203. self.msvcrt._putenv(keyvalue.encode("utf-8"))
  204. # unset environment variable
  205. def unsetenv(self, key):
  206. if environ.get(key) is not None:
  207. environ.pop(key)
  208. if WINDOWS:
  209. keyrm = "%s=" % key
  210. self.msvcrt._putenv(keyrm.encode("utf-8"))
  211. # --------------------------------------------------------------------------------------------------------
  212. # Get the complete license text of used third-party code and features.
  213. # Returned string is in basic html format.
  214. def get_complete_license_text(self):
  215. return charPtrToString(self.lib.carla_get_complete_license_text())
  216. # Get the juce version used in the current Carla build.
  217. def get_juce_version(self):
  218. return charPtrToString(self.lib.carla_get_juce_version())
  219. # Get the list of supported file extensions in carla_load_file().
  220. def get_supported_file_extensions(self):
  221. return charPtrPtrToStringList(self.lib.carla_get_supported_file_extensions())
  222. # Get the list of supported features in the current Carla build.
  223. def get_supported_features(self):
  224. return charPtrPtrToStringList(self.lib.carla_get_supported_features())
  225. # Get how many internal plugins are available.
  226. # Internal and LV2 plugin formats are cached and need to be discovered via this function.
  227. # Do not call this for any other plugin formats.
  228. def get_cached_plugin_count(self, ptype, pluginPath):
  229. return int(self.lib.carla_get_cached_plugin_count(ptype, pluginPath.encode("utf-8")))
  230. # Get information about a cached plugin.
  231. def get_cached_plugin_info(self, ptype, index):
  232. return structToDict(self.lib.carla_get_cached_plugin_info(ptype, index).contents)
  233. def fflush(self, err):
  234. self.lib.carla_fflush(err)
  235. def fputs(self, err, string):
  236. self.lib.carla_fputs(err, string.encode("utf-8"))
  237. def set_process_name(self, name):
  238. self.lib.carla_set_process_name(name.encode("utf-8"))
  239. def pipe_client_new(self, func):
  240. argc = len(argv)
  241. cagrvtype = c_char_p * argc
  242. cargv = cagrvtype()
  243. for i in range(argc):
  244. cargv[i] = c_char_p(argv[i].encode("utf-8"))
  245. self._pipeClientFunc = func
  246. self._pipeClientCallback = CarlaPipeCallbackFunc(func)
  247. return self.lib.carla_pipe_client_new(cargv, self._pipeClientCallback, None)
  248. def pipe_client_idle(self, handle):
  249. while True:
  250. try:
  251. msg = self.lib.carla_pipe_client_idle(handle)
  252. except OSError as e:
  253. print("pipe_client_idle", e)
  254. return
  255. if not msg:
  256. break
  257. self._pipeClientFunc(None, msg.decode("utf-8", errors="ignore"))
  258. def pipe_client_is_running(self, handle):
  259. return bool(self.lib.carla_pipe_client_is_running(handle))
  260. def pipe_client_lock(self, handle):
  261. self.lib.carla_pipe_client_lock(handle)
  262. def pipe_client_unlock(self, handle):
  263. self.lib.carla_pipe_client_unlock(handle)
  264. def pipe_client_readlineblock(self, handle, timeout):
  265. return charPtrToString(self.lib.carla_pipe_client_readlineblock(handle, timeout))
  266. def pipe_client_write_msg(self, handle, msg):
  267. return bool(self.lib.carla_pipe_client_write_msg(handle, msg.encode("utf-8")))
  268. def pipe_client_write_and_fix_msg(self, handle, msg):
  269. return bool(self.lib.carla_pipe_client_write_and_fix_msg(handle, msg.encode("utf-8")))
  270. def pipe_client_flush(self, handle):
  271. return bool(self.lib.carla_pipe_client_flush(handle))
  272. def pipe_client_flush_and_unlock(self, handle):
  273. return bool(self.lib.carla_pipe_client_flush_and_unlock(handle))
  274. def pipe_client_destroy(self, handle):
  275. self.lib.carla_pipe_client_destroy(handle)
  276. def cocoa_get_window(self, winId):
  277. return self.lib.carla_cocoa_get_window(winId)
  278. def x11_reparent_window(self, winId1, winId2):
  279. self.lib.carla_x11_reparent_window(winId1, winId2)
  280. def x11_move_window(self, winId, x, y):
  281. self.lib.carla_x11_move_window(winId, x, y)
  282. def x11_get_window_pos(self, winId):
  283. data = self.lib.carla_x11_get_window_pos(winId)
  284. return tuple(int(data[i]) for i in range(4))
  285. # ------------------------------------------------------------------------------------------------------------