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.

337 lines
11KB

  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Carla Backend utils
  4. # Copyright (C) 2011-2014 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_GIG:
  43. return "GIG"
  44. if ptype == PLUGIN_SF2:
  45. return "SF2"
  46. if ptype == PLUGIN_SFZ:
  47. return "SFZ"
  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 == "gig":
  71. return PLUGIN_GIG
  72. if stype == "sf2":
  73. return PLUGIN_SF2
  74. if stype == "sfz":
  75. return PLUGIN_SFZ
  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. # Plugin category.
  87. ("category", c_enum),
  88. # Plugin hints.
  89. # @see PluginHints
  90. ("hints", c_uint),
  91. # Number of audio inputs.
  92. ("audioIns", c_uint32),
  93. # Number of audio outputs.
  94. ("audioOuts", c_uint32),
  95. # Number of MIDI inputs.
  96. ("midiIns", c_uint32),
  97. # Number of MIDI outputs.
  98. ("midiOuts", c_uint32),
  99. # Number of input parameters.
  100. ("parameterIns", c_uint32),
  101. # Number of output parameters.
  102. ("parameterOuts", c_uint32),
  103. # Plugin name.
  104. ("name", c_char_p),
  105. # Plugin label.
  106. ("label", c_char_p),
  107. # Plugin author/maker.
  108. ("maker", c_char_p),
  109. # Plugin copyright/license.
  110. ("copyright", c_char_p)
  111. ]
  112. # ------------------------------------------------------------------------------------------------------------
  113. # Carla Utils API (Python compatible stuff)
  114. # @see CarlaCachedPluginInfo
  115. PyCarlaCachedPluginInfo = {
  116. 'category': PLUGIN_CATEGORY_NONE,
  117. 'hints': 0x0,
  118. 'audioIns': 0,
  119. 'audioOuts': 0,
  120. 'midiIns': 0,
  121. 'midiOuts': 0,
  122. 'parameterIns': 0,
  123. 'parameterOuts': 0,
  124. 'name': "",
  125. 'label': "",
  126. 'maker': "",
  127. 'copyright': ""
  128. }
  129. # ------------------------------------------------------------------------------------------------------------
  130. # Carla Utils object using a DLL
  131. class CarlaUtils(object):
  132. def __init__(self, filename):
  133. object.__init__(self)
  134. self.lib = cdll.LoadLibrary(filename)
  135. self.lib.carla_get_complete_license_text.argtypes = None
  136. self.lib.carla_get_complete_license_text.restype = c_char_p
  137. self.lib.carla_get_juce_version.argtypes = None
  138. self.lib.carla_get_juce_version.restype = c_char_p
  139. self.lib.carla_get_supported_file_extensions.argtypes = None
  140. self.lib.carla_get_supported_file_extensions.restype = c_char_p
  141. self.lib.carla_get_cached_plugin_count.argtypes = [c_enum, c_char_p]
  142. self.lib.carla_get_cached_plugin_count.restype = c_uint
  143. self.lib.carla_get_cached_plugin_info.argtypes = [c_enum, c_uint]
  144. self.lib.carla_get_cached_plugin_info.restype = POINTER(CarlaCachedPluginInfo)
  145. self.lib.carla_set_process_name.argtypes = [c_char_p]
  146. self.lib.carla_set_process_name.restype = None
  147. self.lib.carla_pipe_client_new.argtypes = [POINTER(c_char_p), CarlaPipeCallbackFunc, c_void_p]
  148. self.lib.carla_pipe_client_new.restype = CarlaPipeClientHandle
  149. self.lib.carla_pipe_client_idle.argtypes = [CarlaPipeClientHandle]
  150. self.lib.carla_pipe_client_idle.restype = None
  151. self.lib.carla_pipe_client_is_running.argtypes = [CarlaPipeClientHandle]
  152. self.lib.carla_pipe_client_is_running.restype = c_bool
  153. self.lib.carla_pipe_client_lock.argtypes = [CarlaPipeClientHandle]
  154. self.lib.carla_pipe_client_lock.restype = None
  155. self.lib.carla_pipe_client_unlock.argtypes = [CarlaPipeClientHandle]
  156. self.lib.carla_pipe_client_unlock.restype = None
  157. self.lib.carla_pipe_client_readlineblock.argtypes = [CarlaPipeClientHandle, c_uint]
  158. self.lib.carla_pipe_client_readlineblock.restype = c_char_p
  159. self.lib.carla_pipe_client_write_msg.argtypes = [CarlaPipeClientHandle, c_char_p]
  160. self.lib.carla_pipe_client_write_msg.restype = c_bool
  161. self.lib.carla_pipe_client_write_and_fix_msg.argtypes = [CarlaPipeClientHandle, c_char_p]
  162. self.lib.carla_pipe_client_write_and_fix_msg.restype = c_bool
  163. self.lib.carla_pipe_client_flush.argtypes = [CarlaPipeClientHandle]
  164. self.lib.carla_pipe_client_flush.restype = c_bool
  165. self.lib.carla_pipe_client_flush_and_unlock.argtypes = [CarlaPipeClientHandle]
  166. self.lib.carla_pipe_client_flush_and_unlock.restype = c_bool
  167. self.lib.carla_pipe_client_destroy.argtypes = [CarlaPipeClientHandle]
  168. self.lib.carla_pipe_client_destroy.restype = None
  169. self.lib.carla_x11_reparent_window.argtypes = [c_uintptr, c_uintptr]
  170. self.lib.carla_x11_reparent_window.restype = None
  171. self.lib.carla_x11_move_window.argtypes = [c_uintptr, c_int, c_int]
  172. self.lib.carla_x11_move_window.restype = None
  173. self.lib.carla_x11_get_window_pos.argtypes = [c_uintptr]
  174. self.lib.carla_x11_get_window_pos.restype = POINTER(c_int)
  175. # use _putenv on windows
  176. if not WINDOWS:
  177. self.msvcrt = None
  178. return
  179. self.msvcrt = cdll.msvcrt
  180. self.msvcrt._putenv.argtypes = [c_char_p]
  181. self.msvcrt._putenv.restype = None
  182. # --------------------------------------------------------------------------------------------------------
  183. # set environment variable
  184. def setenv(self, key, value):
  185. environ[key] = value
  186. if WINDOWS:
  187. keyvalue = "%s=%s" % (key, value)
  188. self.msvcrt._putenv(keyvalue.encode("utf-8"))
  189. # unset environment variable
  190. def unsetenv(self, key):
  191. if environ.get(key) is not None:
  192. environ.pop(key)
  193. if WINDOWS:
  194. keyrm = "%s=" % key
  195. self.msvcrt._putenv(keyrm.encode("utf-8"))
  196. # --------------------------------------------------------------------------------------------------------
  197. # Get the complete license text of used third-party code and features.
  198. # Returned string is in basic html format.
  199. def get_complete_license_text(self):
  200. return charPtrToString(self.lib.carla_get_complete_license_text())
  201. # Get the juce version used in the current Carla build.
  202. def get_juce_version(self):
  203. return charPtrToString(self.lib.carla_get_juce_version())
  204. # Get all the supported file extensions in carla_load_file().
  205. # Returned string uses this syntax:
  206. # @code
  207. # "*.ext1;*.ext2;*.ext3"
  208. # @endcode
  209. def get_supported_file_extensions(self):
  210. return charPtrToString(self.lib.carla_get_supported_file_extensions())
  211. # Get how many internal plugins are available.
  212. def get_cached_plugin_count(self, ptype, pluginPath):
  213. return int(self.lib.carla_get_cached_plugin_count(ptype, pluginPath.encode("utf-8")))
  214. # Get information about a cached plugin.
  215. def get_cached_plugin_info(self, ptype, index):
  216. return structToDict(self.lib.carla_get_cached_plugin_info(ptype, index).contents)
  217. def set_process_name(self, name):
  218. self.lib.carla_set_process_name(name.encode("utf-8"))
  219. def pipe_client_new(self, func):
  220. argc = len(argv)
  221. cagrvtype = c_char_p * argc
  222. cargv = cagrvtype()
  223. for i in range(argc):
  224. cargv[i] = c_char_p(argv[i].encode("utf-8"))
  225. self._pipeClientCallback = CarlaPipeCallbackFunc(func)
  226. return self.lib.carla_pipe_client_new(cargv, self._pipeClientCallback, None)
  227. def pipe_client_idle(self, handle):
  228. self.lib.carla_pipe_client_idle(handle)
  229. def pipe_client_is_running(self, handle):
  230. return bool(self.lib.carla_pipe_client_is_running(handle))
  231. def pipe_client_lock(self, handle):
  232. self.lib.carla_pipe_client_lock(handle)
  233. def pipe_client_unlock(self, handle):
  234. self.lib.carla_pipe_client_unlock(handle)
  235. def pipe_client_readlineblock(self, handle, timeout):
  236. return charPtrToString(self.lib.carla_pipe_client_readlineblock(handle, timeout))
  237. def pipe_client_write_msg(self, handle, msg):
  238. return bool(self.lib.carla_pipe_client_write_msg(handle, msg.encode("utf-8")))
  239. def pipe_client_write_and_fix_msg(self, handle, msg):
  240. return bool(self.lib.carla_pipe_client_write_and_fix_msg(handle, msg.encode("utf-8")))
  241. def pipe_client_flush(self, handle):
  242. return bool(self.lib.carla_pipe_client_flush(handle))
  243. def pipe_client_flush_and_unlock(self, handle):
  244. return bool(self.lib.carla_pipe_client_flush_and_unlock(handle))
  245. def pipe_client_destroy(self, handle):
  246. self.lib.carla_pipe_client_destroy(handle)
  247. def x11_reparent_window(self, winId1, winId2):
  248. self.lib.carla_x11_reparent_window(winId1, winId2)
  249. def x11_move_window(self, winId, x, y):
  250. self.lib.carla_x11_move_window(winId, x, y)
  251. def x11_get_window_pos(self, winId):
  252. data = self.lib.carla_x11_get_window_pos(winId)
  253. return (int(data[0]), int(data[1]))
  254. # ------------------------------------------------------------------------------------------------------------