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.

357 lines
12KB

  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. if ptype == PLUGIN_JACK:
  49. return "JACK"
  50. print("getPluginTypeAsString(%i) - invalid type" % ptype);
  51. return "Unknown"
  52. def getPluginTypeFromString(stype):
  53. if not stype:
  54. return PLUGIN_NONE
  55. stype = stype.lower()
  56. if stype == "none":
  57. return PLUGIN_NONE
  58. if stype in ("internal", "native"):
  59. return PLUGIN_INTERNAL
  60. if stype == "ladspa":
  61. return PLUGIN_LADSPA
  62. if stype == "dssi":
  63. return PLUGIN_DSSI
  64. if stype == "lv2":
  65. return PLUGIN_LV2
  66. if stype in ("vst2", "vst"):
  67. return PLUGIN_VST2
  68. if stype == "vst3":
  69. return PLUGIN_VST3
  70. if stype in ("au", "audiounit"):
  71. return PLUGIN_AU
  72. if stype == "gig":
  73. return PLUGIN_GIG
  74. if stype == "sf2":
  75. return PLUGIN_SF2
  76. if stype == "sfz":
  77. return PLUGIN_SFZ
  78. if stype == "jack":
  79. return PLUGIN_JACK
  80. print("getPluginTypeFromString(\"%s\") - invalid string type" % stype)
  81. return PLUGIN_NONE
  82. # ------------------------------------------------------------------------------------------------------------
  83. # Carla Utils API (C stuff)
  84. CarlaPipeClientHandle = c_void_p
  85. CarlaPipeCallbackFunc = CFUNCTYPE(None, c_void_p, c_char_p)
  86. # Information about an internal Carla plugin.
  87. # @see carla_get_cached_plugin_info()
  88. class CarlaCachedPluginInfo(Structure):
  89. _fields_ = [
  90. # Plugin category.
  91. ("category", c_enum),
  92. # Plugin hints.
  93. # @see PluginHints
  94. ("hints", c_uint),
  95. # Number of audio inputs.
  96. ("audioIns", c_uint32),
  97. # Number of audio outputs.
  98. ("audioOuts", c_uint32),
  99. # Number of MIDI inputs.
  100. ("midiIns", c_uint32),
  101. # Number of MIDI outputs.
  102. ("midiOuts", c_uint32),
  103. # Number of input parameters.
  104. ("parameterIns", c_uint32),
  105. # Number of output parameters.
  106. ("parameterOuts", c_uint32),
  107. # Plugin name.
  108. ("name", c_char_p),
  109. # Plugin label.
  110. ("label", c_char_p),
  111. # Plugin author/maker.
  112. ("maker", c_char_p),
  113. # Plugin copyright/license.
  114. ("copyright", c_char_p)
  115. ]
  116. # ------------------------------------------------------------------------------------------------------------
  117. # Carla Utils API (Python compatible stuff)
  118. # @see CarlaCachedPluginInfo
  119. PyCarlaCachedPluginInfo = {
  120. 'category': PLUGIN_CATEGORY_NONE,
  121. 'hints': 0x0,
  122. 'audioIns': 0,
  123. 'audioOuts': 0,
  124. 'midiIns': 0,
  125. 'midiOuts': 0,
  126. 'parameterIns': 0,
  127. 'parameterOuts': 0,
  128. 'name': "",
  129. 'label': "",
  130. 'maker': "",
  131. 'copyright': ""
  132. }
  133. # ------------------------------------------------------------------------------------------------------------
  134. # Carla Utils object using a DLL
  135. class CarlaUtils(object):
  136. def __init__(self, filename):
  137. object.__init__(self)
  138. self.lib = cdll.LoadLibrary(filename)
  139. #self.lib = CDLL(filename, RTLD_GLOBAL)
  140. self.lib.carla_get_complete_license_text.argtypes = None
  141. self.lib.carla_get_complete_license_text.restype = c_char_p
  142. self.lib.carla_get_juce_version.argtypes = None
  143. self.lib.carla_get_juce_version.restype = c_char_p
  144. self.lib.carla_get_supported_file_extensions.argtypes = None
  145. self.lib.carla_get_supported_file_extensions.restype = c_char_p
  146. self.lib.carla_get_cached_plugin_count.argtypes = [c_enum, c_char_p]
  147. self.lib.carla_get_cached_plugin_count.restype = c_uint
  148. self.lib.carla_get_cached_plugin_info.argtypes = [c_enum, c_uint]
  149. self.lib.carla_get_cached_plugin_info.restype = POINTER(CarlaCachedPluginInfo)
  150. self.lib.carla_fflush.argtypes = [c_bool]
  151. self.lib.carla_fflush.restype = None
  152. self.lib.carla_fputs.argtypes = [c_bool, c_char_p]
  153. self.lib.carla_fputs.restype = None
  154. self.lib.carla_set_process_name.argtypes = [c_char_p]
  155. self.lib.carla_set_process_name.restype = None
  156. self.lib.carla_pipe_client_new.argtypes = [POINTER(c_char_p), CarlaPipeCallbackFunc, c_void_p]
  157. self.lib.carla_pipe_client_new.restype = CarlaPipeClientHandle
  158. self.lib.carla_pipe_client_idle.argtypes = [CarlaPipeClientHandle]
  159. self.lib.carla_pipe_client_idle.restype = None
  160. self.lib.carla_pipe_client_is_running.argtypes = [CarlaPipeClientHandle]
  161. self.lib.carla_pipe_client_is_running.restype = c_bool
  162. self.lib.carla_pipe_client_lock.argtypes = [CarlaPipeClientHandle]
  163. self.lib.carla_pipe_client_lock.restype = None
  164. self.lib.carla_pipe_client_unlock.argtypes = [CarlaPipeClientHandle]
  165. self.lib.carla_pipe_client_unlock.restype = None
  166. self.lib.carla_pipe_client_readlineblock.argtypes = [CarlaPipeClientHandle, c_uint]
  167. self.lib.carla_pipe_client_readlineblock.restype = c_char_p
  168. self.lib.carla_pipe_client_write_msg.argtypes = [CarlaPipeClientHandle, c_char_p]
  169. self.lib.carla_pipe_client_write_msg.restype = c_bool
  170. self.lib.carla_pipe_client_write_and_fix_msg.argtypes = [CarlaPipeClientHandle, c_char_p]
  171. self.lib.carla_pipe_client_write_and_fix_msg.restype = c_bool
  172. self.lib.carla_pipe_client_flush.argtypes = [CarlaPipeClientHandle]
  173. self.lib.carla_pipe_client_flush.restype = c_bool
  174. self.lib.carla_pipe_client_flush_and_unlock.argtypes = [CarlaPipeClientHandle]
  175. self.lib.carla_pipe_client_flush_and_unlock.restype = c_bool
  176. self.lib.carla_pipe_client_destroy.argtypes = [CarlaPipeClientHandle]
  177. self.lib.carla_pipe_client_destroy.restype = None
  178. self.lib.carla_x11_reparent_window.argtypes = [c_uintptr, c_uintptr]
  179. self.lib.carla_x11_reparent_window.restype = None
  180. self.lib.carla_x11_move_window.argtypes = [c_uintptr, c_int, c_int]
  181. self.lib.carla_x11_move_window.restype = None
  182. self.lib.carla_x11_get_window_pos.argtypes = [c_uintptr]
  183. self.lib.carla_x11_get_window_pos.restype = POINTER(c_int)
  184. # use _putenv on windows
  185. if not WINDOWS:
  186. self.msvcrt = None
  187. return
  188. self.msvcrt = cdll.msvcrt
  189. self.msvcrt._putenv.argtypes = [c_char_p]
  190. self.msvcrt._putenv.restype = None
  191. # --------------------------------------------------------------------------------------------------------
  192. # set environment variable
  193. def setenv(self, key, value):
  194. environ[key] = value
  195. if WINDOWS:
  196. keyvalue = "%s=%s" % (key, value)
  197. self.msvcrt._putenv(keyvalue.encode("utf-8"))
  198. # unset environment variable
  199. def unsetenv(self, key):
  200. if environ.get(key) is not None:
  201. environ.pop(key)
  202. if WINDOWS:
  203. keyrm = "%s=" % key
  204. self.msvcrt._putenv(keyrm.encode("utf-8"))
  205. # --------------------------------------------------------------------------------------------------------
  206. # Get the complete license text of used third-party code and features.
  207. # Returned string is in basic html format.
  208. def get_complete_license_text(self):
  209. return charPtrToString(self.lib.carla_get_complete_license_text())
  210. # Get the juce version used in the current Carla build.
  211. def get_juce_version(self):
  212. return charPtrToString(self.lib.carla_get_juce_version())
  213. # Get all the supported file extensions in carla_load_file().
  214. # Returned string uses this syntax:
  215. # @code
  216. # "*.ext1;*.ext2;*.ext3"
  217. # @endcode
  218. def get_supported_file_extensions(self):
  219. return charPtrToString(self.lib.carla_get_supported_file_extensions())
  220. # Get how many internal plugins are available.
  221. def get_cached_plugin_count(self, ptype, pluginPath):
  222. return int(self.lib.carla_get_cached_plugin_count(ptype, pluginPath.encode("utf-8")))
  223. # Get information about a cached plugin.
  224. def get_cached_plugin_info(self, ptype, index):
  225. return structToDict(self.lib.carla_get_cached_plugin_info(ptype, index).contents)
  226. def fflush(self, err):
  227. self.lib.carla_fflush(err)
  228. def fputs(self, err, string):
  229. self.lib.carla_fputs(err, string.encode("utf-8"))
  230. def set_process_name(self, name):
  231. self.lib.carla_set_process_name(name.encode("utf-8"))
  232. def pipe_client_new(self, func):
  233. argc = len(argv)
  234. cagrvtype = c_char_p * argc
  235. cargv = cagrvtype()
  236. for i in range(argc):
  237. cargv[i] = c_char_p(argv[i].encode("utf-8"))
  238. self._pipeClientCallback = CarlaPipeCallbackFunc(func)
  239. return self.lib.carla_pipe_client_new(cargv, self._pipeClientCallback, None)
  240. def pipe_client_idle(self, handle):
  241. try:
  242. self.lib.carla_pipe_client_idle(handle)
  243. except OSError as e:
  244. print("pipe_client_idle", e)
  245. def pipe_client_is_running(self, handle):
  246. return bool(self.lib.carla_pipe_client_is_running(handle))
  247. def pipe_client_lock(self, handle):
  248. self.lib.carla_pipe_client_lock(handle)
  249. def pipe_client_unlock(self, handle):
  250. self.lib.carla_pipe_client_unlock(handle)
  251. def pipe_client_readlineblock(self, handle, timeout):
  252. return charPtrToString(self.lib.carla_pipe_client_readlineblock(handle, timeout))
  253. def pipe_client_write_msg(self, handle, msg):
  254. return bool(self.lib.carla_pipe_client_write_msg(handle, msg.encode("utf-8")))
  255. def pipe_client_write_and_fix_msg(self, handle, msg):
  256. return bool(self.lib.carla_pipe_client_write_and_fix_msg(handle, msg.encode("utf-8")))
  257. def pipe_client_flush(self, handle):
  258. return bool(self.lib.carla_pipe_client_flush(handle))
  259. def pipe_client_flush_and_unlock(self, handle):
  260. return bool(self.lib.carla_pipe_client_flush_and_unlock(handle))
  261. def pipe_client_destroy(self, handle):
  262. self.lib.carla_pipe_client_destroy(handle)
  263. def x11_reparent_window(self, winId1, winId2):
  264. self.lib.carla_x11_reparent_window(winId1, winId2)
  265. def x11_move_window(self, winId, x, y):
  266. self.lib.carla_x11_move_window(winId, x, y)
  267. def x11_get_window_pos(self, winId):
  268. data = self.lib.carla_x11_get_window_pos(winId)
  269. return (int(data[0]), int(data[1]))
  270. # ------------------------------------------------------------------------------------------------------------