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 12KB

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