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.

1776 lines
66KB

  1. """Lilv Python interface"""
  2. __author__ = "David Robillard"
  3. __copyright__ = "Copyright 2016 David Robillard"
  4. __license__ = "ISC"
  5. __version__ = "0.22.1"
  6. __maintainer__ = "David Robillard"
  7. __email__ = "d@drobilla.net"
  8. __status__ = "Production"
  9. import ctypes
  10. import os
  11. import sys
  12. from ctypes import Structure, CDLL, POINTER, CFUNCTYPE
  13. from ctypes import c_bool, c_double, c_float, c_int, c_size_t, c_uint, c_uint32
  14. from ctypes import c_char, c_char_p, c_void_p
  15. from ctypes import byref
  16. # Load lilv library
  17. _lib = CDLL("liblilv-0.so")
  18. # Set namespaced aliases for all lilv functions
  19. class String(str):
  20. # Wrapper for string parameters to pass as raw C UTF-8 strings
  21. def from_param(cls, obj):
  22. return obj.encode('utf-8')
  23. from_param = classmethod(from_param)
  24. def _as_uri(obj):
  25. if type(obj) in [Plugin, PluginClass, UI]:
  26. return obj.get_uri()
  27. else:
  28. return obj
  29. free = _lib.lilv_free
  30. # uri_to_path = _lib.lilv_uri_to_path
  31. file_uri_parse = _lib.lilv_file_uri_parse
  32. new_uri = _lib.lilv_new_uri
  33. new_file_uri = _lib.lilv_new_file_uri
  34. new_string = _lib.lilv_new_string
  35. new_int = _lib.lilv_new_int
  36. new_float = _lib.lilv_new_float
  37. new_bool = _lib.lilv_new_bool
  38. node_free = _lib.lilv_node_free
  39. node_duplicate = _lib.lilv_node_duplicate
  40. node_equals = _lib.lilv_node_equals
  41. node_get_turtle_token = _lib.lilv_node_get_turtle_token
  42. node_is_uri = _lib.lilv_node_is_uri
  43. node_as_uri = _lib.lilv_node_as_uri
  44. node_is_blank = _lib.lilv_node_is_blank
  45. node_as_blank = _lib.lilv_node_as_blank
  46. node_is_literal = _lib.lilv_node_is_literal
  47. node_is_string = _lib.lilv_node_is_string
  48. node_as_string = _lib.lilv_node_as_string
  49. node_get_path = _lib.lilv_node_get_path
  50. node_is_float = _lib.lilv_node_is_float
  51. node_as_float = _lib.lilv_node_as_float
  52. node_is_int = _lib.lilv_node_is_int
  53. node_as_int = _lib.lilv_node_as_int
  54. node_is_bool = _lib.lilv_node_is_bool
  55. node_as_bool = _lib.lilv_node_as_bool
  56. plugin_classes_free = _lib.lilv_plugin_classes_free
  57. plugin_classes_size = _lib.lilv_plugin_classes_size
  58. plugin_classes_begin = _lib.lilv_plugin_classes_begin
  59. plugin_classes_get = _lib.lilv_plugin_classes_get
  60. plugin_classes_next = _lib.lilv_plugin_classes_next
  61. plugin_classes_is_end = _lib.lilv_plugin_classes_is_end
  62. plugin_classes_get_by_uri = _lib.lilv_plugin_classes_get_by_uri
  63. scale_points_free = _lib.lilv_scale_points_free
  64. scale_points_size = _lib.lilv_scale_points_size
  65. scale_points_begin = _lib.lilv_scale_points_begin
  66. scale_points_get = _lib.lilv_scale_points_get
  67. scale_points_next = _lib.lilv_scale_points_next
  68. scale_points_is_end = _lib.lilv_scale_points_is_end
  69. uis_free = _lib.lilv_uis_free
  70. uis_size = _lib.lilv_uis_size
  71. uis_begin = _lib.lilv_uis_begin
  72. uis_get = _lib.lilv_uis_get
  73. uis_next = _lib.lilv_uis_next
  74. uis_is_end = _lib.lilv_uis_is_end
  75. uis_get_by_uri = _lib.lilv_uis_get_by_uri
  76. nodes_free = _lib.lilv_nodes_free
  77. nodes_size = _lib.lilv_nodes_size
  78. nodes_begin = _lib.lilv_nodes_begin
  79. nodes_get = _lib.lilv_nodes_get
  80. nodes_next = _lib.lilv_nodes_next
  81. nodes_is_end = _lib.lilv_nodes_is_end
  82. nodes_get_first = _lib.lilv_nodes_get_first
  83. nodes_contains = _lib.lilv_nodes_contains
  84. nodes_merge = _lib.lilv_nodes_merge
  85. plugins_size = _lib.lilv_plugins_size
  86. plugins_begin = _lib.lilv_plugins_begin
  87. plugins_get = _lib.lilv_plugins_get
  88. plugins_next = _lib.lilv_plugins_next
  89. plugins_is_end = _lib.lilv_plugins_is_end
  90. plugins_get_by_uri = _lib.lilv_plugins_get_by_uri
  91. world_new = _lib.lilv_world_new
  92. world_set_option = _lib.lilv_world_set_option
  93. world_free = _lib.lilv_world_free
  94. world_load_all = _lib.lilv_world_load_all
  95. world_load_bundle = _lib.lilv_world_load_bundle
  96. world_load_specifications = _lib.lilv_world_load_specifications
  97. world_load_plugin_classes = _lib.lilv_world_load_plugin_classes
  98. world_unload_bundle = _lib.lilv_world_unload_bundle
  99. world_load_resource = _lib.lilv_world_load_resource
  100. world_unload_resource = _lib.lilv_world_unload_resource
  101. world_get_plugin_class = _lib.lilv_world_get_plugin_class
  102. world_get_plugin_classes = _lib.lilv_world_get_plugin_classes
  103. world_get_all_plugins = _lib.lilv_world_get_all_plugins
  104. world_find_nodes = _lib.lilv_world_find_nodes
  105. world_get = _lib.lilv_world_get
  106. world_ask = _lib.lilv_world_ask
  107. plugin_verify = _lib.lilv_plugin_verify
  108. plugin_get_uri = _lib.lilv_plugin_get_uri
  109. plugin_get_bundle_uri = _lib.lilv_plugin_get_bundle_uri
  110. plugin_get_data_uris = _lib.lilv_plugin_get_data_uris
  111. plugin_get_library_uri = _lib.lilv_plugin_get_library_uri
  112. plugin_get_name = _lib.lilv_plugin_get_name
  113. plugin_get_class = _lib.lilv_plugin_get_class
  114. plugin_get_value = _lib.lilv_plugin_get_value
  115. plugin_has_feature = _lib.lilv_plugin_has_feature
  116. plugin_get_supported_features = _lib.lilv_plugin_get_supported_features
  117. plugin_get_required_features = _lib.lilv_plugin_get_required_features
  118. plugin_get_optional_features = _lib.lilv_plugin_get_optional_features
  119. plugin_has_extension_data = _lib.lilv_plugin_has_extension_data
  120. plugin_get_extension_data = _lib.lilv_plugin_get_extension_data
  121. plugin_get_num_ports = _lib.lilv_plugin_get_num_ports
  122. plugin_get_port_ranges_float = _lib.lilv_plugin_get_port_ranges_float
  123. plugin_has_latency = _lib.lilv_plugin_has_latency
  124. plugin_get_latency_port_index = _lib.lilv_plugin_get_latency_port_index
  125. plugin_get_port_by_index = _lib.lilv_plugin_get_port_by_index
  126. plugin_get_port_by_symbol = _lib.lilv_plugin_get_port_by_symbol
  127. plugin_get_port_by_designation = _lib.lilv_plugin_get_port_by_designation
  128. plugin_get_project = _lib.lilv_plugin_get_project
  129. plugin_get_author_name = _lib.lilv_plugin_get_author_name
  130. plugin_get_author_email = _lib.lilv_plugin_get_author_email
  131. plugin_get_author_homepage = _lib.lilv_plugin_get_author_homepage
  132. plugin_is_replaced = _lib.lilv_plugin_is_replaced
  133. plugin_get_related = _lib.lilv_plugin_get_related
  134. port_get_node = _lib.lilv_port_get_node
  135. port_get_value = _lib.lilv_port_get_value
  136. port_get = _lib.lilv_port_get
  137. port_get_properties = _lib.lilv_port_get_properties
  138. port_has_property = _lib.lilv_port_has_property
  139. port_supports_event = _lib.lilv_port_supports_event
  140. port_get_index = _lib.lilv_port_get_index
  141. port_get_symbol = _lib.lilv_port_get_symbol
  142. port_get_name = _lib.lilv_port_get_name
  143. port_get_classes = _lib.lilv_port_get_classes
  144. port_is_a = _lib.lilv_port_is_a
  145. port_get_range = _lib.lilv_port_get_range
  146. port_get_scale_points = _lib.lilv_port_get_scale_points
  147. state_new_from_world = _lib.lilv_state_new_from_world
  148. state_new_from_file = _lib.lilv_state_new_from_file
  149. state_new_from_string = _lib.lilv_state_new_from_string
  150. state_new_from_instance = _lib.lilv_state_new_from_instance
  151. state_free = _lib.lilv_state_free
  152. state_equals = _lib.lilv_state_equals
  153. state_get_num_properties = _lib.lilv_state_get_num_properties
  154. state_get_plugin_uri = _lib.lilv_state_get_plugin_uri
  155. state_get_uri = _lib.lilv_state_get_uri
  156. state_get_label = _lib.lilv_state_get_label
  157. state_set_label = _lib.lilv_state_set_label
  158. state_set_metadata = _lib.lilv_state_set_metadata
  159. state_emit_port_values = _lib.lilv_state_emit_port_values
  160. state_restore = _lib.lilv_state_restore
  161. state_save = _lib.lilv_state_save
  162. state_to_string = _lib.lilv_state_to_string
  163. state_delete = _lib.lilv_state_delete
  164. scale_point_get_label = _lib.lilv_scale_point_get_label
  165. scale_point_get_value = _lib.lilv_scale_point_get_value
  166. plugin_class_get_parent_uri = _lib.lilv_plugin_class_get_parent_uri
  167. plugin_class_get_uri = _lib.lilv_plugin_class_get_uri
  168. plugin_class_get_label = _lib.lilv_plugin_class_get_label
  169. plugin_class_get_children = _lib.lilv_plugin_class_get_children
  170. plugin_instantiate = _lib.lilv_plugin_instantiate
  171. instance_free = _lib.lilv_instance_free
  172. plugin_get_uis = _lib.lilv_plugin_get_uis
  173. ui_get_uri = _lib.lilv_ui_get_uri
  174. ui_get_classes = _lib.lilv_ui_get_classes
  175. ui_is_a = _lib.lilv_ui_is_a
  176. ui_is_supported = _lib.lilv_ui_is_supported
  177. ui_get_bundle_uri = _lib.lilv_ui_get_bundle_uri
  178. ui_get_binary_uri = _lib.lilv_ui_get_binary_uri
  179. ## LV2 types
  180. LV2_Handle = POINTER(None)
  181. LV2_URID_Map_Handle = POINTER(None)
  182. LV2_URID_Unmap_Handle = POINTER(None)
  183. LV2_URID = c_uint32
  184. class LV2_Feature(Structure):
  185. __slots__ = [ 'URI', 'data' ]
  186. _fields_ = [('URI', c_char_p),
  187. ('data', POINTER(None))]
  188. class LV2_Descriptor(Structure):
  189. __slots__ = [ 'URI',
  190. 'instantiate',
  191. 'connect_port',
  192. 'activate',
  193. 'run',
  194. 'deactivate',
  195. 'cleanup',
  196. 'extension_data' ]
  197. LV2_Descriptor._fields_ = [
  198. ('URI', c_char_p),
  199. ('instantiate', CFUNCTYPE(LV2_Handle, POINTER(LV2_Descriptor),
  200. c_double, c_char_p, POINTER(POINTER(LV2_Feature)))),
  201. ('connect_port', CFUNCTYPE(None, LV2_Handle, c_uint32, POINTER(None))),
  202. ('activate', CFUNCTYPE(None, LV2_Handle)),
  203. ('run', CFUNCTYPE(None, LV2_Handle, c_uint32)),
  204. ('deactivate', CFUNCTYPE(None, LV2_Handle)),
  205. ('cleanup', CFUNCTYPE(None, LV2_Handle)),
  206. ('extension_data', CFUNCTYPE(c_void_p, c_char_p)),
  207. ]
  208. class LV2_URID_Map(Structure):
  209. __slots__ = [ 'handle', 'map' ]
  210. _fields_ = [
  211. ('handle', LV2_URID_Map_Handle),
  212. ('map', CFUNCTYPE(LV2_URID, LV2_URID_Map_Handle, c_char_p)),
  213. ]
  214. class LV2_URID_Unmap(Structure):
  215. __slots__ = [ 'handle', 'unmap' ]
  216. _fields_ = [
  217. ('handle', LV2_URID_Unmap_Handle),
  218. ('unmap', CFUNCTYPE(c_char_p, LV2_URID_Unmap_Handle, LV2_URID)),
  219. ]
  220. # Lilv types
  221. class Plugin(Structure):
  222. """LV2 Plugin."""
  223. def __init__(self, world, plugin):
  224. self.world = world
  225. self.plugin = plugin
  226. def __eq__(self, other):
  227. return self.get_uri() == other.get_uri()
  228. def verify(self):
  229. """Check if `plugin` is valid.
  230. This is not a rigorous validator, but can be used to reject some malformed
  231. plugins that could cause bugs (e.g. plugins with missing required fields).
  232. Note that normal hosts do NOT need to use this - lilv does not
  233. load invalid plugins into plugin lists. This is included for plugin
  234. testing utilities, etc.
  235. """
  236. return plugin_verify(self.plugin)
  237. def get_uri(self):
  238. """Get the URI of `plugin`.
  239. Any serialization that refers to plugins should refer to them by this.
  240. Hosts SHOULD NOT save any filesystem paths, plugin indexes, etc. in saved
  241. files pass save only the URI.
  242. The URI is a globally unique identifier for one specific plugin. Two
  243. plugins with the same URI are compatible in port signature, and should
  244. be guaranteed to work in a compatible and consistent way. If a plugin
  245. is upgraded in an incompatible way (eg if it has different ports), it
  246. MUST have a different URI than it's predecessor.
  247. """
  248. return Node.wrap(node_duplicate(plugin_get_uri(self.plugin)))
  249. def get_bundle_uri(self):
  250. """Get the (resolvable) URI of the plugin's "main" bundle.
  251. This returns the URI of the bundle where the plugin itself was found. Note
  252. that the data for a plugin may be spread over many bundles, that is,
  253. get_data_uris() may return URIs which are not within this bundle.
  254. Typical hosts should not need to use this function.
  255. Note this always returns a fully qualified URI. If you want a local
  256. filesystem path, use lilv.file_uri_parse().
  257. """
  258. return Node.wrap(node_duplicate(plugin_get_bundle_uri(self.plugin)))
  259. def get_data_uris(self):
  260. """Get the (resolvable) URIs of the RDF data files that define a plugin.
  261. Typical hosts should not need to use this function.
  262. Note this always returns fully qualified URIs. If you want local
  263. filesystem paths, use lilv.file_uri_parse().
  264. """
  265. return Nodes(plugin_get_data_uris(self.plugin))
  266. def get_library_uri(self):
  267. """Get the (resolvable) URI of the shared library for `plugin`.
  268. Note this always returns a fully qualified URI. If you want a local
  269. filesystem path, use lilv.file_uri_parse().
  270. """
  271. return Node.wrap(node_duplicate(plugin_get_library_uri(self.plugin)))
  272. def get_name(self):
  273. """Get the name of `plugin`.
  274. This returns the name (doap:name) of the plugin. The name may be
  275. translated according to the current locale, this value MUST NOT be used
  276. as a plugin identifier (use the URI for that).
  277. """
  278. return Node.wrap(plugin_get_name(self.plugin))
  279. def get_class(self):
  280. """Get the class this plugin belongs to (e.g. Filters)."""
  281. return PluginClass(plugin_get_class(self.plugin))
  282. def get_value(self, predicate):
  283. """Get a value associated with the plugin in a plugin's data files.
  284. `predicate` must be either a URI or a QName.
  285. Returns the ?object of all triples found of the form:
  286. plugin-uri predicate ?object
  287. May return None if the property was not found, or if object(s) is not
  288. sensibly represented as a LilvNodes (e.g. blank nodes).
  289. """
  290. return Nodes(plugin_get_value(self.plugin, predicate.node))
  291. def has_feature(self, feature_uri):
  292. """Return whether a feature is supported by a plugin.
  293. This will return true if the feature is an optional or required feature
  294. of the plugin.
  295. """
  296. return plugin_has_feature(self.plugin, feature_uri.node)
  297. def get_supported_features(self):
  298. """Get the LV2 Features supported (required or optionally) by a plugin.
  299. A feature is "supported" by a plugin if it is required OR optional.
  300. Since required features have special rules the host must obey, this function
  301. probably shouldn't be used by normal hosts. Using get_optional_features()
  302. and get_required_features() separately is best in most cases.
  303. """
  304. return Nodes(plugin_get_supported_features(self.plugin))
  305. def get_required_features(self):
  306. """Get the LV2 Features required by a plugin.
  307. If a feature is required by a plugin, hosts MUST NOT use the plugin if they do not
  308. understand (or are unable to support) that feature.
  309. All values returned here MUST be return plugin_(self.plugin)ed to the plugin's instantiate method
  310. (along with data, if necessary, as defined by the feature specification)
  311. or plugin instantiation will fail.
  312. """
  313. return Nodes(plugin_get_required_features(self.plugin))
  314. def get_optional_features(self):
  315. """Get the LV2 Features optionally supported by a plugin.
  316. Hosts MAY ignore optional plugin features for whatever reasons. Plugins
  317. MUST operate (at least somewhat) if they are instantiated without being
  318. passed optional features.
  319. """
  320. return Nodes(plugin_get_optional_features(self.plugin))
  321. def has_extension_data(self, uri):
  322. """Return whether or not a plugin provides a specific extension data."""
  323. return plugin_has_extension_data(self.plugin, uri.node)
  324. def get_extension_data(self):
  325. """Get a sequence of all extension data provided by a plugin.
  326. This can be used to find which URIs get_extension_data()
  327. will return a value for without instantiating the plugin.
  328. """
  329. return Nodes(plugin_get_extension_data(self.plugin))
  330. def get_num_ports(self):
  331. """Get the number of ports on this plugin."""
  332. return plugin_get_num_ports(self.plugin)
  333. # def get_port_ranges_float(self, min_values, max_values, def_values):
  334. # """Get the port ranges (minimum, maximum and default values) for all ports.
  335. # `min_values`, `max_values` and `def_values` must either point to an array
  336. # of N floats, where N is the value returned by get_num_ports()
  337. # for this plugin, or None. The elements of the array will be set to the
  338. # the minimum, maximum and default values of the ports on this plugin,
  339. # with array index corresponding to port index. If a port doesn't have a
  340. # minimum, maximum or default value, or the port's type is not float, the
  341. # corresponding array element will be set to NAN.
  342. # This is a convenience method for the common case of getting the range of
  343. # all float ports on a plugin, and may be significantly faster than
  344. # repeated calls to Port.get_range().
  345. # """
  346. # plugin_get_port_ranges_float(self.plugin, min_values, max_values, def_values)
  347. def get_num_ports_of_class(self, *args):
  348. """Get the number of ports on this plugin that are members of some class(es)."""
  349. args = list(map(lambda x: x.node, args))
  350. args += (None,)
  351. return plugin_get_num_ports_of_class(self.plugin, *args)
  352. def has_latency(self):
  353. """Return whether or not the plugin introduces (and reports) latency.
  354. The index of the latency port can be found with
  355. get_latency_port() ONLY if this function returns true.
  356. """
  357. return plugin_has_latency(self.plugin)
  358. def get_latency_port_index(self):
  359. """Return the index of the plugin's latency port.
  360. Returns None if the plugin has no latency port.
  361. Any plugin that introduces unwanted latency that should be compensated for
  362. (by hosts with the ability/need) MUST provide this port, which is a control
  363. rate output port that reports the latency for each cycle in frames.
  364. """
  365. return plugin_get_latency_port_index(self.plugin) if self.has_latency() else None
  366. def get_port(self, key):
  367. """Get a port on `plugin` by index or symbol."""
  368. if type(key) == int:
  369. return self.get_port_by_index(key)
  370. else:
  371. return self.get_port_by_symbol(key)
  372. def get_port_by_index(self, index):
  373. """Get a port on `plugin` by `index`."""
  374. return Port.wrap(self, plugin_get_port_by_index(self.plugin, index))
  375. def get_port_by_symbol(self, symbol):
  376. """Get a port on `plugin` by `symbol`.
  377. Note this function is slower than get_port_by_index(),
  378. especially on plugins with a very large number of ports.
  379. """
  380. if type(symbol) == str:
  381. symbol = self.world.new_string(symbol)
  382. return Port.wrap(self, plugin_get_port_by_symbol(self.plugin, symbol.node))
  383. def get_port_by_designation(self, port_class, designation):
  384. """Get a port on `plugin` by its lv2:designation.
  385. The designation of a port describes the meaning, assignment, allocation or
  386. role of the port, e.g. "left channel" or "gain". If found, the port with
  387. matching `port_class` and `designation` is be returned, otherwise None is
  388. returned. The `port_class` can be used to distinguish the input and output
  389. ports for a particular designation. If `port_class` is None, any port with
  390. the given designation will be returned.
  391. """
  392. return Port.wrap(self,
  393. plugin_get_port_by_designation(self.plugin,
  394. port_class.node,
  395. designation.node))
  396. def get_project(self):
  397. """Get the project the plugin is a part of.
  398. More information about the project can be read via find_nodes(),
  399. typically using properties from DOAP (e.g. doap:name).
  400. """
  401. return Node.wrap(plugin_get_project(self.plugin))
  402. def get_author_name(self):
  403. """Get the full name of the plugin's author.
  404. Returns None if author name is not present.
  405. """
  406. return Node.wrap(plugin_get_author_name(self.plugin))
  407. def get_author_email(self):
  408. """Get the email address of the plugin's author.
  409. Returns None if author email address is not present.
  410. """
  411. return Node.wrap(plugin_get_author_email(self.plugin))
  412. def get_author_homepage(self):
  413. """Get the address of the plugin author's home page.
  414. Returns None if author homepage is not present.
  415. """
  416. return Node.wrap(plugin_get_author_homepage(self.plugin))
  417. def is_replaced(self):
  418. """Return true iff `plugin` has been replaced by another plugin.
  419. The plugin will still be usable, but hosts should hide them from their
  420. user interfaces to prevent users from using deprecated plugins.
  421. """
  422. return plugin_is_replaced(self.plugin)
  423. def get_related(self, resource_type):
  424. """Get the resources related to `plugin` with lv2:appliesTo.
  425. Some plugin-related resources are not linked directly to the plugin with
  426. rdfs:seeAlso and thus will not be automatically loaded along with the plugin
  427. data (usually for performance reasons). All such resources of the given @c
  428. type related to `plugin` can be accessed with this function.
  429. If `resource_type` is None, all such resources will be returned, regardless of type.
  430. To actually load the data for each returned resource, use world.load_resource().
  431. """
  432. return Nodes(plugin_get_related(self.plugin, resource_type))
  433. def get_uis(self):
  434. """Get all UIs for `plugin`."""
  435. return UIs(plugin_get_uis(self.plugin))
  436. class PluginClass(Structure):
  437. """Plugin Class (type/category)."""
  438. def __init__(self, plugin_class):
  439. self.plugin_class = plugin_class
  440. def __str__(self):
  441. return self.get_uri().__str__()
  442. def get_parent_uri(self):
  443. """Get the URI of this class' superclass.
  444. May return None if class has no parent.
  445. """
  446. return Node.wrap(node_duplicate(plugin_class_get_parent_uri(self.plugin_class)))
  447. def get_uri(self):
  448. """Get the URI of this plugin class."""
  449. return Node.wrap(node_duplicate(plugin_class_get_uri(self.plugin_class)))
  450. def get_label(self):
  451. """Get the label of this plugin class, ie "Oscillators"."""
  452. return Node.wrap(node_duplicate(plugin_class_get_label(self.plugin_class)))
  453. def get_children(self):
  454. """Get the subclasses of this plugin class."""
  455. return PluginClasses(plugin_class_get_children(self.plugin_class))
  456. class Port(Structure):
  457. """Port on a Plugin."""
  458. @classmethod
  459. def wrap(cls, plugin, port):
  460. return Port(plugin, port) if plugin and port else None
  461. def __init__(self, plugin, port):
  462. self.plugin = plugin
  463. self.port = port
  464. def get_node(self):
  465. """Get the RDF node of `port`.
  466. Ports nodes may be may be URIs or blank nodes.
  467. """
  468. return Node.wrap(node_duplicate(port_get_node(self.plugin, self.port)))
  469. def get_value(self, predicate):
  470. """Port analog of Plugin.get_value()."""
  471. return Nodes(port_get_value(self.plugin.plugin, self.port, predicate.node))
  472. def get(self, predicate):
  473. """Get a single property value of a port.
  474. This is equivalent to lilv_nodes_get_first(lilv_port_get_value(...)) but is
  475. simpler to use in the common case of only caring about one value. The
  476. caller is responsible for freeing the returned node.
  477. """
  478. return Node.wrap(port_get(self.plugin.plugin, self.port, predicate.node))
  479. def get_properties(self):
  480. """Return the LV2 port properties of a port."""
  481. return Nodes(port_get_properties(self.plugin.plugin, self.port))
  482. def has_property(self, property_uri):
  483. """Return whether a port has a certain property."""
  484. return port_has_property(self.plugin.plugin, self.port, property_uri.node)
  485. def supports_event(self, event_type):
  486. """Return whether a port supports a certain event type.
  487. More precisely, this returns true iff the port has an atom:supports or an
  488. ev:supportsEvent property with `event_type` as the value.
  489. """
  490. return port_supports_event(self.plugin.plugin, self.port, event_type.node)
  491. def get_index(self):
  492. """Get the index of a port.
  493. The index is only valid for the life of the plugin and may change between
  494. versions. For a stable identifier, use the symbol.
  495. """
  496. return port_get_index(self.plugin.plugin, self.port)
  497. def get_symbol(self):
  498. """Get the symbol of a port.
  499. The 'symbol' is a short string, a valid C identifier.
  500. """
  501. return Node.wrap(node_duplicate(port_get_symbol(self.plugin.plugin, self.port)))
  502. def get_name(self):
  503. """Get the name of a port.
  504. This is guaranteed to return the untranslated name (the doap:name in the
  505. data file without a language tag).
  506. """
  507. return Node.wrap(port_get_name(self.plugin.plugin, self.port))
  508. def get_classes(self):
  509. """Get all the classes of a port.
  510. This can be used to determine if a port is an input, output, audio,
  511. control, midi, etc, etc, though it's simpler to use is_a().
  512. The returned list does not include lv2:Port, which is implied.
  513. Returned value is shared and must not be destroyed by caller.
  514. """
  515. return Nodes(port_get_classes(self.plugin.plugin, self.port))
  516. def is_a(self, port_class):
  517. """Determine if a port is of a given class (input, output, audio, etc).
  518. For convenience/performance/extensibility reasons, hosts are expected to
  519. create a LilvNode for each port class they "care about". Well-known type
  520. URI strings are defined (e.g. LILV_URI_INPUT_PORT) for convenience, but
  521. this function is designed so that Lilv is usable with any port types
  522. without requiring explicit support in Lilv.
  523. """
  524. return port_is_a(self.plugin.plugin, self.port, port_class.node)
  525. def get_range(self):
  526. """Return the default, minimum, and maximum values of a port as a tuple."""
  527. pdef = POINTER(Node)()
  528. pmin = POINTER(Node)()
  529. pmax = POINTER(Node)()
  530. port_get_range(self.plugin.plugin, self.port, byref(pdef), byref(pmin), byref(pmax))
  531. return (Node(pdef.contents) if pdef else None,
  532. Node(pmin.contents) if pmin else None,
  533. Node(pmax.contents) if pmax else None)
  534. def get_scale_points(self):
  535. """Get the scale points (enumeration values) of a port.
  536. This returns a collection of 'interesting' named values of a port
  537. (e.g. appropriate entries for a UI selector associated with this port).
  538. Returned value may be None if `port` has no scale points.
  539. """
  540. return ScalePoints(port_get_scale_points(self.plugin.plugin, self.port))
  541. class ScalePoint(Structure):
  542. """Scale point (detent)."""
  543. def __init__(self, point):
  544. self.point = point
  545. def get_label(self):
  546. """Get the label of this scale point (enumeration value)."""
  547. return Node.wrap(scale_point_get_label(self.point))
  548. def get_value(self):
  549. """Get the value of this scale point (enumeration value)."""
  550. return Node.wrap(scale_point_get_value(self.point))
  551. class UI(Structure):
  552. """Plugin UI."""
  553. def __init__(self, ui):
  554. self.ui = ui
  555. def __str__(self):
  556. return str(self.get_uri())
  557. def __eq__(self, other):
  558. return self.get_uri() == _as_uri(other)
  559. def get_uri(self):
  560. """Get the URI of a Plugin UI."""
  561. return Node.wrap(node_duplicate(ui_get_uri(self.ui)))
  562. def get_classes(self):
  563. """Get the types (URIs of RDF classes) of a Plugin UI.
  564. Note that in most cases is_supported() should be used, which avoids
  565. the need to use this function (and type specific logic).
  566. """
  567. return Nodes(ui_get_classes(self.ui))
  568. def is_a(self, class_uri):
  569. """Check whether a plugin UI has a given type."""
  570. return ui_is_a(self.ui, class_uri.node)
  571. def get_bundle_uri(self):
  572. """Get the URI of the UI's bundle."""
  573. return Node.wrap(node_duplicate(ui_get_bundle_uri(self.ui)))
  574. def get_binary_uri(self):
  575. """Get the URI for the UI's shared library."""
  576. return Node.wrap(node_duplicate(ui_get_binary_uri(self.ui)))
  577. class Node(Structure):
  578. """Data node (URI, string, integer, etc.).
  579. A Node can be converted to the corresponding Python datatype, and all nodes
  580. can be converted to strings, for example::
  581. >>> world = lilv.World()
  582. >>> i = world.new_int(42)
  583. >>> print(i)
  584. 42
  585. >>> int(i) * 2
  586. 84
  587. """
  588. @classmethod
  589. def wrap(cls, node):
  590. return Node(node) if node else None
  591. def __init__(self, node):
  592. self.node = node
  593. def __del__(self):
  594. if hasattr(self, 'node'):
  595. node_free(self.node)
  596. def __eq__(self, other):
  597. otype = type(other)
  598. if otype in [str, int, float]:
  599. return otype(self) == other
  600. return node_equals(self.node, other.node)
  601. def __ne__(self, other):
  602. return not node_equals(self.node, other.node)
  603. def __str__(self):
  604. return node_as_string(self.node).decode('utf-8')
  605. def __int__(self):
  606. if not self.is_int():
  607. raise ValueError('node %s is not an integer' % str(self))
  608. return node_as_int(self.node)
  609. def __float__(self):
  610. if not self.is_float():
  611. raise ValueError('node %s is not a float' % str(self))
  612. return node_as_float(self.node)
  613. def __bool__(self):
  614. if not self.is_bool():
  615. raise ValueError('node %s is not a bool' % str(self))
  616. return node_as_bool(self.node)
  617. __nonzero__ = __bool__
  618. def get_turtle_token(self):
  619. """Return this value as a Turtle/SPARQL token."""
  620. return node_get_turtle_token(self.node).decode('utf-8')
  621. def is_uri(self):
  622. """Return whether the value is a URI (resource)."""
  623. return node_is_uri(self.node)
  624. def is_blank(self):
  625. """Return whether the value is a blank node (resource with no URI)."""
  626. return node_is_blank(self.node)
  627. def is_literal(self):
  628. """Return whether this value is a literal (i.e. not a URI)."""
  629. return node_is_literal(self.node)
  630. def is_string(self):
  631. """Return whether this value is a string literal.
  632. Returns true if value is a string value (and not numeric).
  633. """
  634. return node_is_string(self.node)
  635. def get_path(self, hostname=None):
  636. """Return the path of a file URI node.
  637. Returns None if value is not a file URI."""
  638. return node_get_path(self.node, hostname).decode('utf-8')
  639. def is_float(self):
  640. """Return whether this value is a decimal literal."""
  641. return node_is_float(self.node)
  642. def is_int(self):
  643. """Return whether this value is an integer literal."""
  644. return node_is_int(self.node)
  645. def is_bool(self):
  646. """Return whether this value is a boolean."""
  647. return node_is_bool(self.node)
  648. class Iter(Structure):
  649. """Collection iterator."""
  650. def __init__(self, collection, iterator, constructor, iter_get, iter_next, iter_is_end):
  651. self.collection = collection
  652. self.iterator = iterator
  653. self.constructor = constructor
  654. self.iter_get = iter_get
  655. self.iter_next = iter_next
  656. self.iter_is_end = iter_is_end
  657. def get(self):
  658. """Get the current item."""
  659. return self.constructor(self.iter_get(self.collection, self.iterator))
  660. def next(self):
  661. """Move to and return the next item."""
  662. if self.is_end():
  663. raise StopIteration
  664. elem = self.get()
  665. self.iterator = self.iter_next(self.collection, self.iterator)
  666. return elem
  667. def is_end(self):
  668. """Return true if the end of the collection has been reached."""
  669. return self.iter_is_end(self.collection, self.iterator)
  670. __next__ = next
  671. class Collection(Structure):
  672. # Base class for all lilv collection wrappers.
  673. def __init__(self, collection, iter_begin, constructor, iter_get, iter_next, is_end):
  674. self.collection = collection
  675. self.constructor = constructor
  676. self.iter_begin = iter_begin
  677. self.iter_get = iter_get
  678. self.iter_next = iter_next
  679. self.is_end = is_end
  680. def __iter__(self):
  681. return Iter(self.collection, self.iter_begin(self.collection), self.constructor,
  682. self.iter_get, self.iter_next, self.is_end)
  683. def __getitem__(self, index):
  684. if index >= len(self):
  685. raise IndexError
  686. pos = 0
  687. for i in self:
  688. if pos == index:
  689. return i
  690. pos += 1
  691. def begin(self):
  692. return self.__iter__()
  693. def get(self, iterator):
  694. return iterator.get()
  695. class Plugins(Collection):
  696. """Collection of plugins."""
  697. def __init__(self, world, collection):
  698. def constructor(plugin):
  699. return Plugin(world, plugin)
  700. super(Plugins, self).__init__(collection, plugins_begin, constructor, plugins_get, plugins_next, plugins_is_end)
  701. self.world = world
  702. def __contains__(self, key):
  703. return bool(self.get_by_uri(_as_uri(key)))
  704. def __len__(self):
  705. return plugins_size(self.collection)
  706. def __getitem__(self, key):
  707. if type(key) == int:
  708. return super(Plugins, self).__getitem__(key)
  709. return self.get_by_uri(key)
  710. def get_by_uri(self, uri):
  711. plugin = plugins_get_by_uri(self.collection, uri.node)
  712. return Plugin(self.world, plugin) if plugin else None
  713. class PluginClasses(Collection):
  714. """Collection of plugin classes."""
  715. def __init__(self, collection):
  716. super(PluginClasses, self).__init__(
  717. collection, plugin_classes_begin, PluginClass,
  718. plugin_classes_get, plugin_classes_next, plugin_classes_is_end)
  719. def __contains__(self, key):
  720. return bool(self.get_by_uri(_as_uri(key)))
  721. def __len__(self):
  722. return plugin_classes_size(self.collection)
  723. def __getitem__(self, key):
  724. if type(key) == int:
  725. return super(PluginClasses, self).__getitem__(key)
  726. return self.get_by_uri(key)
  727. def get_by_uri(self, uri):
  728. plugin_class = plugin_classes_get_by_uri(self.collection, uri.node)
  729. return PluginClass(plugin_class) if plugin_class else None
  730. class ScalePoints(Collection):
  731. """Collection of scale points."""
  732. def __init__(self, collection):
  733. super(ScalePoints, self).__init__(
  734. collection, scale_points_begin, ScalePoint,
  735. scale_points_get, scale_points_next, scale_points_is_end)
  736. def __len__(self):
  737. return scale_points_size(self.collection)
  738. class UIs(Collection):
  739. """Collection of plugin UIs."""
  740. def __init__(self, collection):
  741. super(UIs, self).__init__(collection, uis_begin, UI,
  742. uis_get, uis_next, uis_is_end)
  743. def __contains__(self, uri):
  744. return bool(self.get_by_uri(_as_uri(uri)))
  745. def __len__(self):
  746. return uis_size(self.collection)
  747. def __getitem__(self, key):
  748. if type(key) == int:
  749. return super(UIs, self).__getitem__(key)
  750. return self.get_by_uri(key)
  751. def get_by_uri(self, uri):
  752. ui = uis_get_by_uri(self.collection, uri.node)
  753. return UI(ui) if ui else None
  754. class Nodes(Collection):
  755. """Collection of data nodes."""
  756. @classmethod
  757. def constructor(ignore, node):
  758. return Node(node_duplicate(node))
  759. def __init__(self, collection):
  760. super(Nodes, self).__init__(collection, nodes_begin, Nodes.constructor,
  761. nodes_get, nodes_next, nodes_is_end)
  762. def __contains__(self, value):
  763. return nodes_contains(self.collection, value.node)
  764. def __len__(self):
  765. return nodes_size(self.collection)
  766. def merge(self, b):
  767. return Nodes(nodes_merge(self.collection, b.collection))
  768. class Namespace():
  769. """Namespace prefix.
  770. Use attribute syntax to easily create URIs within this namespace, for
  771. example::
  772. >>> world = lilv.World()
  773. >>> ns = Namespace(world, "http://example.org/")
  774. >>> print(ns.foo)
  775. http://example.org/foo
  776. """
  777. def __init__(self, world, prefix):
  778. self.world = world
  779. self.prefix = prefix
  780. def __eq__(self, other):
  781. return str(self) == str(other)
  782. def __str__(self):
  783. return self.prefix
  784. def __getattr__(self, suffix):
  785. return self.world.new_uri(self.prefix + suffix)
  786. class World(Structure):
  787. """Library context.
  788. Includes a set of namespaces as the instance variable `ns`, so URIs can be constructed like::
  789. uri = world.ns.lv2.Plugin
  790. :ivar ns: Common LV2 namespace prefixes: atom, doap, foaf, lilv, lv2, midi, owl, rdf, rdfs, ui, xsd.
  791. """
  792. def __init__(self):
  793. world = self
  794. # Define Namespaces class locally so available prefixes are documented
  795. class Namespaces():
  796. """Set of namespaces.
  797. Use to easily construct uris, like: ns.lv2.InputPort"""
  798. atom = Namespace(world, 'http://lv2plug.in/ns/ext/atom#')
  799. doap = Namespace(world, 'http://usefulinc.com/ns/doap#')
  800. foaf = Namespace(world, 'http://xmlns.com/foaf/0.1/')
  801. lilv = Namespace(world, 'http://drobilla.net/ns/lilv#')
  802. lv2 = Namespace(world, 'http://lv2plug.in/ns/lv2core#')
  803. midi = Namespace(world, 'http://lv2plug.in/ns/ext/midi#')
  804. owl = Namespace(world, 'http://www.w3.org/2002/07/owl#')
  805. rdf = Namespace(world, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#')
  806. rdfs = Namespace(world, 'http://www.w3.org/2000/01/rdf-schema#')
  807. ui = Namespace(world, 'http://lv2plug.in/ns/extensions/ui#')
  808. xsd = Namespace(world, 'http://www.w3.org/2001/XMLSchema#')
  809. self.world = _lib.lilv_world_new()
  810. self.ns = Namespaces()
  811. def __del__(self):
  812. world_free(self.world)
  813. def set_option(self, uri, value):
  814. """Set a world option.
  815. Currently recognized options:
  816. lilv.OPTION_FILTER_LANG
  817. lilv.OPTION_DYN_MANIFEST
  818. """
  819. return world_set_option(self, uri, value.node)
  820. def load_all(self):
  821. """Load all installed LV2 bundles on the system.
  822. This is the recommended way for hosts to load LV2 data. It implements the
  823. established/standard best practice for discovering all LV2 data on the
  824. system. The environment variable LV2_PATH may be used to control where
  825. this function will look for bundles.
  826. Hosts should use this function rather than explicitly load bundles, except
  827. in special circumstances (e.g. development utilities, or hosts that ship
  828. with special plugin bundles which are installed to a known location).
  829. """
  830. world_load_all(self.world)
  831. def load_bundle(self, bundle_uri):
  832. """Load a specific bundle.
  833. `bundle_uri` must be a fully qualified URI to the bundle directory,
  834. with the trailing slash, eg. file:///usr/lib/lv2/foo.lv2/
  835. Normal hosts should not need this function (use load_all()).
  836. Hosts MUST NOT attach any long-term significance to bundle paths
  837. (e.g. in save files), since there are no guarantees they will remain
  838. unchanged between (or even during) program invocations. Plugins (among
  839. other things) MUST be identified by URIs (not paths) in save files.
  840. """
  841. world_load_bundle(self.world, bundle_uri.node)
  842. def load_specifications(self):
  843. """Load all specifications from currently loaded bundles.
  844. This is for hosts that explicitly load specific bundles, its use is not
  845. necessary when using load_all(). This function parses the
  846. specifications and adds them to the model.
  847. """
  848. world_load_specifications(self.world)
  849. def load_plugin_classes(self):
  850. """Load all plugin classes from currently loaded specifications.
  851. Must be called after load_specifications(). This is for hosts
  852. that explicitly load specific bundles, its use is not necessary when using
  853. load_all().
  854. """
  855. world_load_plugin_classes(self.world)
  856. def unload_bundle(self, bundle_uri):
  857. """Unload a specific bundle.
  858. This unloads statements loaded by load_bundle(). Note that this
  859. is not necessarily all information loaded from the bundle. If any resources
  860. have been separately loaded with load_resource(), they must be
  861. separately unloaded with unload_resource().
  862. """
  863. return world_unload_bundle(self.world, bundle_uri.node)
  864. def load_resource(self, resource):
  865. """Load all the data associated with the given `resource`.
  866. The resource must be a subject (i.e. a URI or a blank node).
  867. Returns the number of files parsed, or -1 on error.
  868. All accessible data files linked to `resource` with rdfs:seeAlso will be
  869. loaded into the world model.
  870. """
  871. return world_load_resource(self.world, _as_uri(resource).node)
  872. def unload_resource(self, resource):
  873. """Unload all the data associated with the given `resource`.
  874. The resource must be a subject (i.e. a URI or a blank node).
  875. This unloads all data loaded by a previous call to
  876. load_resource() with the given `resource`.
  877. """
  878. return world_unload_resource(self.world, _as_uri(resource).node)
  879. def get_plugin_class(self):
  880. """Get the parent of all other plugin classes, lv2:Plugin."""
  881. return PluginClass(world_get_plugin_class(self.world))
  882. def get_plugin_classes(self):
  883. """Return a list of all found plugin classes."""
  884. return PluginClasses(world_get_plugin_classes(self.world))
  885. def get_all_plugins(self):
  886. """Return a list of all found plugins.
  887. The returned list contains just enough references to query
  888. or instantiate plugins. The data for a particular plugin will not be
  889. loaded into memory until a call to an lilv_plugin_* function results in
  890. a query (at which time the data is cached with the LilvPlugin so future
  891. queries are very fast).
  892. The returned list and the plugins it contains are owned by `world`
  893. and must not be freed by caller.
  894. """
  895. return Plugins(self, _lib.lilv_world_get_all_plugins(self.world))
  896. def find_nodes(self, subject, predicate, obj):
  897. """Find nodes matching a triple pattern.
  898. Either `subject` or `object` may be None (i.e. a wildcard), but not both.
  899. Returns all matches for the wildcard field, or None.
  900. """
  901. return Nodes(world_find_nodes(self.world,
  902. subject.node if subject is not None else None,
  903. predicate.node if predicate is not None else None,
  904. obj.node if obj is not None else None))
  905. def get(self, subject, predicate, obj):
  906. """Find a single node that matches a pattern.
  907. Exactly one of `subject`, `predicate`, `object` must be None.
  908. Returns the first matching node, or None if no matches are found.
  909. """
  910. return Node.wrap(world_get(self.world,
  911. subject.node if subject is not None else None,
  912. predicate.node if predicate is not None else None,
  913. obj.node if obj is not None else None))
  914. def ask(self, subject, predicate, obj):
  915. """Return true iff a statement matching a certain pattern exists.
  916. This is useful for checking if particular statement exists without having to
  917. bother with collections and memory management.
  918. """
  919. return world_ask(self.world,
  920. subject.node if subject is not None else None,
  921. predicate.node if predicate is not None else None,
  922. obj.node if obj is not None else None)
  923. def new_uri(self, uri):
  924. """Create a new URI node."""
  925. return Node.wrap(_lib.lilv_new_uri(self.world, uri))
  926. def new_file_uri(self, host, path):
  927. """Create a new file URI node. The host may be None."""
  928. return Node.wrap(_lib.lilv_new_file_uri(self.world, host, path))
  929. def new_string(self, string):
  930. """Create a new string node."""
  931. return Node.wrap(_lib.lilv_new_string(self.world, string))
  932. def new_int(self, val):
  933. """Create a new int node."""
  934. return Node.wrap(_lib.lilv_new_int(self.world, val))
  935. def new_float(self, val):
  936. """Create a new float node."""
  937. return Node.wrap(_lib.lilv_new_float(self.world, val))
  938. def new_bool(self, val):
  939. """Create a new bool node."""
  940. return Node.wrap(_lib.lilv_new_bool(self.world, val))
  941. class Instance(Structure):
  942. """Plugin instance."""
  943. __slots__ = [ 'lv2_descriptor', 'lv2_handle', 'pimpl', 'plugin', 'rate', 'instance' ]
  944. _fields_ = [
  945. ('lv2_descriptor', POINTER(LV2_Descriptor)),
  946. ('lv2_handle', LV2_Handle),
  947. ('pimpl', POINTER(None)),
  948. ]
  949. def __init__(self, plugin, rate, features=None):
  950. self.plugin = plugin
  951. self.rate = rate
  952. self.instance = plugin_instantiate(plugin.plugin, rate, features)
  953. def get_uri(self):
  954. """Get the URI of the plugin which `instance` is an instance of.
  955. Returned string is shared and must not be modified or deleted.
  956. """
  957. return self.get_descriptor().URI
  958. def connect_port(self, port_index, data):
  959. """Connect a port to a data location.
  960. This may be called regardless of whether the plugin is activated,
  961. activation and deactivation does not destroy port connections.
  962. """
  963. import numpy
  964. if data is None:
  965. self.get_descriptor().connect_port(
  966. self.get_handle(),
  967. port_index,
  968. data)
  969. elif type(data) == numpy.ndarray:
  970. self.get_descriptor().connect_port(
  971. self.get_handle(),
  972. port_index,
  973. data.ctypes.data_as(POINTER(c_float)))
  974. else:
  975. raise Exception("Unsupported data type")
  976. def activate(self):
  977. """Activate a plugin instance.
  978. This resets all state information in the plugin, except for port data
  979. locations (as set by connect_port()). This MUST be called
  980. before calling run().
  981. """
  982. if self.get_descriptor().activate:
  983. self.get_descriptor().activate(self.get_handle())
  984. def run(self, sample_count):
  985. """Run `instance` for `sample_count` frames.
  986. If the hint lv2:hardRTCapable is set for this plugin, this function is
  987. guaranteed not to block.
  988. """
  989. self.get_descriptor().run(self.get_handle(), sample_count)
  990. def deactivate(self):
  991. """Deactivate a plugin instance.
  992. Note that to run the plugin after this you must activate it, which will
  993. reset all state information (except port connections).
  994. """
  995. if self.get_descriptor().deactivate:
  996. self.get_descriptor().deactivate(self.get_handle())
  997. def get_extension_data(self, uri):
  998. """Get extension data from the plugin instance.
  999. The type and semantics of the data returned is specific to the particular
  1000. extension, though in all cases it is shared and must not be deleted.
  1001. """
  1002. if self.get_descriptor().extension_data:
  1003. return self.get_descriptor().extension_data(str(uri))
  1004. def get_descriptor(self):
  1005. """Get the LV2_Descriptor of the plugin instance.
  1006. Normally hosts should not need to access the LV2_Descriptor directly,
  1007. use the lilv_instance_* functions.
  1008. """
  1009. return self.instance[0].lv2_descriptor[0]
  1010. def get_handle(self):
  1011. """Get the LV2_Handle of the plugin instance.
  1012. Normally hosts should not need to access the LV2_Handle directly,
  1013. use the lilv_instance_* functions.
  1014. """
  1015. return self.instance[0].lv2_handle
  1016. class State(Structure):
  1017. """Plugin state (TODO)."""
  1018. pass
  1019. class VariadicFunction(object):
  1020. # Wrapper for calling C variadic functions
  1021. def __init__(self, function, restype, argtypes):
  1022. self.function = function
  1023. self.function.restype = restype
  1024. self.argtypes = argtypes
  1025. def __call__(self, *args):
  1026. fixed_args = []
  1027. i = 0
  1028. for argtype in self.argtypes:
  1029. fixed_args.append(argtype.from_param(args[i]))
  1030. i += 1
  1031. return self.function(*fixed_args + list(args[i:]))
  1032. # Set return and argument types for lilv C functions
  1033. free.argtypes = [POINTER(None)]
  1034. free.restype = None
  1035. # uri_to_path.argtypes = [String]
  1036. # uri_to_path.restype = c_char_p
  1037. file_uri_parse.argtypes = [String, POINTER(POINTER(c_char))]
  1038. file_uri_parse.restype = c_char_p
  1039. new_uri.argtypes = [POINTER(World), String]
  1040. new_uri.restype = POINTER(Node)
  1041. new_file_uri.argtypes = [POINTER(World), c_char_p, String]
  1042. new_file_uri.restype = POINTER(Node)
  1043. new_string.argtypes = [POINTER(World), String]
  1044. new_string.restype = POINTER(Node)
  1045. new_int.argtypes = [POINTER(World), c_int]
  1046. new_int.restype = POINTER(Node)
  1047. new_float.argtypes = [POINTER(World), c_float]
  1048. new_float.restype = POINTER(Node)
  1049. new_bool.argtypes = [POINTER(World), c_bool]
  1050. new_bool.restype = POINTER(Node)
  1051. node_free.argtypes = [POINTER(Node)]
  1052. node_free.restype = None
  1053. node_duplicate.argtypes = [POINTER(Node)]
  1054. node_duplicate.restype = POINTER(Node)
  1055. node_equals.argtypes = [POINTER(Node), POINTER(Node)]
  1056. node_equals.restype = c_bool
  1057. node_get_turtle_token.argtypes = [POINTER(Node)]
  1058. node_get_turtle_token.restype = c_char_p
  1059. node_is_uri.argtypes = [POINTER(Node)]
  1060. node_is_uri.restype = c_bool
  1061. node_as_uri.argtypes = [POINTER(Node)]
  1062. node_as_uri.restype = c_char_p
  1063. node_is_blank.argtypes = [POINTER(Node)]
  1064. node_is_blank.restype = c_bool
  1065. node_as_blank.argtypes = [POINTER(Node)]
  1066. node_as_blank.restype = c_char_p
  1067. node_is_literal.argtypes = [POINTER(Node)]
  1068. node_is_literal.restype = c_bool
  1069. node_is_string.argtypes = [POINTER(Node)]
  1070. node_is_string.restype = c_bool
  1071. node_as_string.argtypes = [POINTER(Node)]
  1072. node_as_string.restype = c_char_p
  1073. node_get_path.argtypes = [POINTER(Node), POINTER(POINTER(c_char))]
  1074. node_get_path.restype = c_char_p
  1075. node_is_float.argtypes = [POINTER(Node)]
  1076. node_is_float.restype = c_bool
  1077. node_as_float.argtypes = [POINTER(Node)]
  1078. node_as_float.restype = c_float
  1079. node_is_int.argtypes = [POINTER(Node)]
  1080. node_is_int.restype = c_bool
  1081. node_as_int.argtypes = [POINTER(Node)]
  1082. node_as_int.restype = c_int
  1083. node_is_bool.argtypes = [POINTER(Node)]
  1084. node_is_bool.restype = c_bool
  1085. node_as_bool.argtypes = [POINTER(Node)]
  1086. node_as_bool.restype = c_bool
  1087. plugin_classes_free.argtypes = [POINTER(PluginClasses)]
  1088. plugin_classes_free.restype = None
  1089. plugin_classes_size.argtypes = [POINTER(PluginClasses)]
  1090. plugin_classes_size.restype = c_uint
  1091. plugin_classes_begin.argtypes = [POINTER(PluginClasses)]
  1092. plugin_classes_begin.restype = POINTER(Iter)
  1093. plugin_classes_get.argtypes = [POINTER(PluginClasses), POINTER(Iter)]
  1094. plugin_classes_get.restype = POINTER(PluginClass)
  1095. plugin_classes_next.argtypes = [POINTER(PluginClasses), POINTER(Iter)]
  1096. plugin_classes_next.restype = POINTER(Iter)
  1097. plugin_classes_is_end.argtypes = [POINTER(PluginClasses), POINTER(Iter)]
  1098. plugin_classes_is_end.restype = c_bool
  1099. plugin_classes_get_by_uri.argtypes = [POINTER(PluginClasses), POINTER(Node)]
  1100. plugin_classes_get_by_uri.restype = POINTER(PluginClass)
  1101. scale_points_free.argtypes = [POINTER(ScalePoints)]
  1102. scale_points_free.restype = None
  1103. scale_points_size.argtypes = [POINTER(ScalePoints)]
  1104. scale_points_size.restype = c_uint
  1105. scale_points_begin.argtypes = [POINTER(ScalePoints)]
  1106. scale_points_begin.restype = POINTER(Iter)
  1107. scale_points_get.argtypes = [POINTER(ScalePoints), POINTER(Iter)]
  1108. scale_points_get.restype = POINTER(ScalePoint)
  1109. scale_points_next.argtypes = [POINTER(ScalePoints), POINTER(Iter)]
  1110. scale_points_next.restype = POINTER(Iter)
  1111. scale_points_is_end.argtypes = [POINTER(ScalePoints), POINTER(Iter)]
  1112. scale_points_is_end.restype = c_bool
  1113. uis_free.argtypes = [POINTER(UIs)]
  1114. uis_free.restype = None
  1115. uis_size.argtypes = [POINTER(UIs)]
  1116. uis_size.restype = c_uint
  1117. uis_begin.argtypes = [POINTER(UIs)]
  1118. uis_begin.restype = POINTER(Iter)
  1119. uis_get.argtypes = [POINTER(UIs), POINTER(Iter)]
  1120. uis_get.restype = POINTER(UI)
  1121. uis_next.argtypes = [POINTER(UIs), POINTER(Iter)]
  1122. uis_next.restype = POINTER(Iter)
  1123. uis_is_end.argtypes = [POINTER(UIs), POINTER(Iter)]
  1124. uis_is_end.restype = c_bool
  1125. uis_get_by_uri.argtypes = [POINTER(UIs), POINTER(Node)]
  1126. uis_get_by_uri.restype = POINTER(UI)
  1127. nodes_free.argtypes = [POINTER(Nodes)]
  1128. nodes_free.restype = None
  1129. nodes_size.argtypes = [POINTER(Nodes)]
  1130. nodes_size.restype = c_uint
  1131. nodes_begin.argtypes = [POINTER(Nodes)]
  1132. nodes_begin.restype = POINTER(Iter)
  1133. nodes_get.argtypes = [POINTER(Nodes), POINTER(Iter)]
  1134. nodes_get.restype = POINTER(Node)
  1135. nodes_next.argtypes = [POINTER(Nodes), POINTER(Iter)]
  1136. nodes_next.restype = POINTER(Iter)
  1137. nodes_is_end.argtypes = [POINTER(Nodes), POINTER(Iter)]
  1138. nodes_is_end.restype = c_bool
  1139. nodes_get_first.argtypes = [POINTER(Nodes)]
  1140. nodes_get_first.restype = POINTER(Node)
  1141. nodes_contains.argtypes = [POINTER(Nodes), POINTER(Node)]
  1142. nodes_contains.restype = c_bool
  1143. nodes_merge.argtypes = [POINTER(Nodes), POINTER(Nodes)]
  1144. nodes_merge.restype = POINTER(Nodes)
  1145. plugins_size.argtypes = [POINTER(Plugins)]
  1146. plugins_size.restype = c_uint
  1147. plugins_begin.argtypes = [POINTER(Plugins)]
  1148. plugins_begin.restype = POINTER(Iter)
  1149. plugins_get.argtypes = [POINTER(Plugins), POINTER(Iter)]
  1150. plugins_get.restype = POINTER(Plugin)
  1151. plugins_next.argtypes = [POINTER(Plugins), POINTER(Iter)]
  1152. plugins_next.restype = POINTER(Iter)
  1153. plugins_is_end.argtypes = [POINTER(Plugins), POINTER(Iter)]
  1154. plugins_is_end.restype = c_bool
  1155. plugins_get_by_uri.argtypes = [POINTER(Plugins), POINTER(Node)]
  1156. plugins_get_by_uri.restype = POINTER(Plugin)
  1157. world_new.argtypes = []
  1158. world_new.restype = POINTER(World)
  1159. world_set_option.argtypes = [POINTER(World), String, POINTER(Node)]
  1160. world_set_option.restype = None
  1161. world_free.argtypes = [POINTER(World)]
  1162. world_free.restype = None
  1163. world_load_all.argtypes = [POINTER(World)]
  1164. world_load_all.restype = None
  1165. world_load_bundle.argtypes = [POINTER(World), POINTER(Node)]
  1166. world_load_bundle.restype = None
  1167. world_load_specifications.argtypes = [POINTER(World)]
  1168. world_load_specifications.restype = None
  1169. world_load_plugin_classes.argtypes = [POINTER(World)]
  1170. world_load_plugin_classes.restype = None
  1171. world_unload_bundle.argtypes = [POINTER(World), POINTER(Node)]
  1172. world_unload_bundle.restype = c_int
  1173. world_load_resource.argtypes = [POINTER(World), POINTER(Node)]
  1174. world_load_resource.restype = c_int
  1175. world_unload_resource.argtypes = [POINTER(World), POINTER(Node)]
  1176. world_unload_resource.restype = c_int
  1177. world_get_plugin_class.argtypes = [POINTER(World)]
  1178. world_get_plugin_class.restype = POINTER(PluginClass)
  1179. world_get_plugin_classes.argtypes = [POINTER(World)]
  1180. world_get_plugin_classes.restype = POINTER(PluginClasses)
  1181. world_get_all_plugins.argtypes = [POINTER(World)]
  1182. world_get_all_plugins.restype = POINTER(Plugins)
  1183. world_find_nodes.argtypes = [POINTER(World), POINTER(Node), POINTER(Node), POINTER(Node)]
  1184. world_find_nodes.restype = POINTER(Nodes)
  1185. world_get.argtypes = [POINTER(World), POINTER(Node), POINTER(Node), POINTER(Node)]
  1186. world_get.restype = POINTER(Node)
  1187. world_ask.argtypes = [POINTER(World), POINTER(Node), POINTER(Node), POINTER(Node)]
  1188. world_ask.restype = c_bool
  1189. plugin_verify.argtypes = [POINTER(Plugin)]
  1190. plugin_verify.restype = c_bool
  1191. plugin_get_uri.argtypes = [POINTER(Plugin)]
  1192. plugin_get_uri.restype = POINTER(Node)
  1193. plugin_get_bundle_uri.argtypes = [POINTER(Plugin)]
  1194. plugin_get_bundle_uri.restype = POINTER(Node)
  1195. plugin_get_data_uris.argtypes = [POINTER(Plugin)]
  1196. plugin_get_data_uris.restype = POINTER(Nodes)
  1197. plugin_get_library_uri.argtypes = [POINTER(Plugin)]
  1198. plugin_get_library_uri.restype = POINTER(Node)
  1199. plugin_get_name.argtypes = [POINTER(Plugin)]
  1200. plugin_get_name.restype = POINTER(Node)
  1201. plugin_get_class.argtypes = [POINTER(Plugin)]
  1202. plugin_get_class.restype = POINTER(PluginClass)
  1203. plugin_get_value.argtypes = [POINTER(Plugin), POINTER(Node)]
  1204. plugin_get_value.restype = POINTER(Nodes)
  1205. plugin_has_feature.argtypes = [POINTER(Plugin), POINTER(Node)]
  1206. plugin_has_feature.restype = c_bool
  1207. plugin_get_supported_features.argtypes = [POINTER(Plugin)]
  1208. plugin_get_supported_features.restype = POINTER(Nodes)
  1209. plugin_get_required_features.argtypes = [POINTER(Plugin)]
  1210. plugin_get_required_features.restype = POINTER(Nodes)
  1211. plugin_get_optional_features.argtypes = [POINTER(Plugin)]
  1212. plugin_get_optional_features.restype = POINTER(Nodes)
  1213. plugin_has_extension_data.argtypes = [POINTER(Plugin), POINTER(Node)]
  1214. plugin_has_extension_data.restype = c_bool
  1215. plugin_get_extension_data.argtypes = [POINTER(Plugin)]
  1216. plugin_get_extension_data.restype = POINTER(Nodes)
  1217. plugin_get_num_ports.argtypes = [POINTER(Plugin)]
  1218. plugin_get_num_ports.restype = c_uint32
  1219. plugin_get_port_ranges_float.argtypes = [POINTER(Plugin), POINTER(c_float), POINTER(c_float), POINTER(c_float)]
  1220. plugin_get_port_ranges_float.restype = None
  1221. plugin_get_num_ports_of_class = VariadicFunction(_lib.lilv_plugin_get_num_ports_of_class,
  1222. c_uint32,
  1223. [POINTER(Plugin), POINTER(Node)])
  1224. plugin_has_latency.argtypes = [POINTER(Plugin)]
  1225. plugin_has_latency.restype = c_bool
  1226. plugin_get_latency_port_index.argtypes = [POINTER(Plugin)]
  1227. plugin_get_latency_port_index.restype = c_uint32
  1228. plugin_get_port_by_index.argtypes = [POINTER(Plugin), c_uint32]
  1229. plugin_get_port_by_index.restype = POINTER(Port)
  1230. plugin_get_port_by_symbol.argtypes = [POINTER(Plugin), POINTER(Node)]
  1231. plugin_get_port_by_symbol.restype = POINTER(Port)
  1232. plugin_get_port_by_designation.argtypes = [POINTER(Plugin), POINTER(Node), POINTER(Node)]
  1233. plugin_get_port_by_designation.restype = POINTER(Port)
  1234. plugin_get_project.argtypes = [POINTER(Plugin)]
  1235. plugin_get_project.restype = POINTER(Node)
  1236. plugin_get_author_name.argtypes = [POINTER(Plugin)]
  1237. plugin_get_author_name.restype = POINTER(Node)
  1238. plugin_get_author_email.argtypes = [POINTER(Plugin)]
  1239. plugin_get_author_email.restype = POINTER(Node)
  1240. plugin_get_author_homepage.argtypes = [POINTER(Plugin)]
  1241. plugin_get_author_homepage.restype = POINTER(Node)
  1242. plugin_is_replaced.argtypes = [POINTER(Plugin)]
  1243. plugin_is_replaced.restype = c_bool
  1244. plugin_get_related.argtypes = [POINTER(Plugin), POINTER(Node)]
  1245. plugin_get_related.restype = POINTER(Nodes)
  1246. port_get_node.argtypes = [POINTER(Plugin), POINTER(Port)]
  1247. port_get_node.restype = POINTER(Node)
  1248. port_get_value.argtypes = [POINTER(Plugin), POINTER(Port), POINTER(Node)]
  1249. port_get_value.restype = POINTER(Nodes)
  1250. port_get.argtypes = [POINTER(Plugin), POINTER(Port), POINTER(Node)]
  1251. port_get.restype = POINTER(Node)
  1252. port_get_properties.argtypes = [POINTER(Plugin), POINTER(Port)]
  1253. port_get_properties.restype = POINTER(Nodes)
  1254. port_has_property.argtypes = [POINTER(Plugin), POINTER(Port), POINTER(Node)]
  1255. port_has_property.restype = c_bool
  1256. port_supports_event.argtypes = [POINTER(Plugin), POINTER(Port), POINTER(Node)]
  1257. port_supports_event.restype = c_bool
  1258. port_get_index.argtypes = [POINTER(Plugin), POINTER(Port)]
  1259. port_get_index.restype = c_uint32
  1260. port_get_symbol.argtypes = [POINTER(Plugin), POINTER(Port)]
  1261. port_get_symbol.restype = POINTER(Node)
  1262. port_get_name.argtypes = [POINTER(Plugin), POINTER(Port)]
  1263. port_get_name.restype = POINTER(Node)
  1264. port_get_classes.argtypes = [POINTER(Plugin), POINTER(Port)]
  1265. port_get_classes.restype = POINTER(Nodes)
  1266. port_is_a.argtypes = [POINTER(Plugin), POINTER(Port), POINTER(Node)]
  1267. port_is_a.restype = c_bool
  1268. port_get_range.argtypes = [POINTER(Plugin), POINTER(Port), POINTER(POINTER(Node)), POINTER(POINTER(Node)), POINTER(POINTER(Node))]
  1269. port_get_range.restype = None
  1270. port_get_scale_points.argtypes = [POINTER(Plugin), POINTER(Port)]
  1271. port_get_scale_points.restype = POINTER(ScalePoints)
  1272. state_new_from_world.argtypes = [POINTER(World), POINTER(LV2_URID_Map), POINTER(Node)]
  1273. state_new_from_world.restype = POINTER(State)
  1274. state_new_from_file.argtypes = [POINTER(World), POINTER(LV2_URID_Map), POINTER(Node), String]
  1275. state_new_from_file.restype = POINTER(State)
  1276. state_new_from_string.argtypes = [POINTER(World), POINTER(LV2_URID_Map), String]
  1277. state_new_from_string.restype = POINTER(State)
  1278. LilvGetPortValueFunc = CFUNCTYPE(c_void_p, c_char_p, POINTER(None), POINTER(c_uint32), POINTER(c_uint32))
  1279. state_new_from_instance.argtypes = [POINTER(Plugin), POINTER(Instance), POINTER(LV2_URID_Map), c_char_p, c_char_p, c_char_p, String, LilvGetPortValueFunc, POINTER(None), c_uint32, POINTER(POINTER(LV2_Feature))]
  1280. state_new_from_instance.restype = POINTER(State)
  1281. state_free.argtypes = [POINTER(State)]
  1282. state_free.restype = None
  1283. state_equals.argtypes = [POINTER(State), POINTER(State)]
  1284. state_equals.restype = c_bool
  1285. state_get_num_properties.argtypes = [POINTER(State)]
  1286. state_get_num_properties.restype = c_uint
  1287. state_get_plugin_uri.argtypes = [POINTER(State)]
  1288. state_get_plugin_uri.restype = POINTER(Node)
  1289. state_get_uri.argtypes = [POINTER(State)]
  1290. state_get_uri.restype = POINTER(Node)
  1291. state_get_label.argtypes = [POINTER(State)]
  1292. state_get_label.restype = c_char_p
  1293. state_set_label.argtypes = [POINTER(State), String]
  1294. state_set_label.restype = None
  1295. state_set_metadata.argtypes = [POINTER(State), c_uint32, POINTER(None), c_size_t, c_uint32, c_uint32]
  1296. state_set_metadata.restype = c_int
  1297. LilvSetPortValueFunc = CFUNCTYPE(None, c_char_p, POINTER(None), POINTER(None), c_uint32, c_uint32)
  1298. state_emit_port_values.argtypes = [POINTER(State), LilvSetPortValueFunc, POINTER(None)]
  1299. state_emit_port_values.restype = None
  1300. state_restore.argtypes = [POINTER(State), POINTER(Instance), LilvSetPortValueFunc, POINTER(None), c_uint32, POINTER(POINTER(LV2_Feature))]
  1301. state_restore.restype = None
  1302. state_save.argtypes = [POINTER(World), POINTER(LV2_URID_Map), POINTER(LV2_URID_Unmap), POINTER(State), c_char_p, c_char_p, String]
  1303. state_save.restype = c_int
  1304. state_to_string.argtypes = [POINTER(World), POINTER(LV2_URID_Map), POINTER(LV2_URID_Unmap), POINTER(State), c_char_p, String]
  1305. state_to_string.restype = c_char_p
  1306. state_delete.argtypes = [POINTER(World), POINTER(State)]
  1307. state_delete.restype = c_int
  1308. scale_point_get_label.argtypes = [POINTER(ScalePoint)]
  1309. scale_point_get_label.restype = POINTER(Node)
  1310. scale_point_get_value.argtypes = [POINTER(ScalePoint)]
  1311. scale_point_get_value.restype = POINTER(Node)
  1312. plugin_class_get_parent_uri.argtypes = [POINTER(PluginClass)]
  1313. plugin_class_get_parent_uri.restype = POINTER(Node)
  1314. plugin_class_get_uri.argtypes = [POINTER(PluginClass)]
  1315. plugin_class_get_uri.restype = POINTER(Node)
  1316. plugin_class_get_label.argtypes = [POINTER(PluginClass)]
  1317. plugin_class_get_label.restype = POINTER(Node)
  1318. plugin_class_get_children.argtypes = [POINTER(PluginClass)]
  1319. plugin_class_get_children.restype = POINTER(PluginClasses)
  1320. plugin_instantiate.argtypes = [POINTER(Plugin), c_double, POINTER(POINTER(LV2_Feature))]
  1321. plugin_instantiate.restype = POINTER(Instance)
  1322. instance_free.argtypes = [POINTER(Instance)]
  1323. instance_free.restype = None
  1324. plugin_get_uis.argtypes = [POINTER(Plugin)]
  1325. plugin_get_uis.restype = POINTER(UIs)
  1326. ui_get_uri.argtypes = [POINTER(UI)]
  1327. ui_get_uri.restype = POINTER(Node)
  1328. ui_get_classes.argtypes = [POINTER(UI)]
  1329. ui_get_classes.restype = POINTER(Nodes)
  1330. ui_is_a.argtypes = [POINTER(UI), POINTER(Node)]
  1331. ui_is_a.restype = c_bool
  1332. LilvUISupportedFunc = CFUNCTYPE(c_uint, c_char_p, c_char_p)
  1333. ui_is_supported.argtypes = [POINTER(UI), LilvUISupportedFunc, POINTER(Node), POINTER(POINTER(Node))]
  1334. ui_is_supported.restype = c_uint
  1335. ui_get_bundle_uri.argtypes = [POINTER(UI)]
  1336. ui_get_bundle_uri.restype = POINTER(Node)
  1337. ui_get_binary_uri.argtypes = [POINTER(UI)]
  1338. ui_get_binary_uri.restype = POINTER(Node)
  1339. OPTION_FILTER_LANG = 'http://drobilla.net/ns/lilv#filter-lang'
  1340. OPTION_DYN_MANIFEST = 'http://drobilla.net/ns/lilv#dyn-manifest'
  1341. # Define URI constants for compatibility with old Python bindings
  1342. LILV_NS_DOAP = 'http://usefulinc.com/ns/doap#'
  1343. LILV_NS_FOAF = 'http://xmlns.com/foaf/0.1/'
  1344. LILV_NS_LILV = 'http://drobilla.net/ns/lilv#'
  1345. LILV_NS_LV2 = 'http://lv2plug.in/ns/lv2core#'
  1346. LILV_NS_OWL = 'http://www.w3.org/2002/07/owl#'
  1347. LILV_NS_RDF = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'
  1348. LILV_NS_RDFS = 'http://www.w3.org/2000/01/rdf-schema#'
  1349. LILV_NS_XSD = 'http://www.w3.org/2001/XMLSchema#'
  1350. LILV_URI_ATOM_PORT = 'http://lv2plug.in/ns/ext/atom#AtomPort'
  1351. LILV_URI_AUDIO_PORT = 'http://lv2plug.in/ns/lv2core#AudioPort'
  1352. LILV_URI_CONTROL_PORT = 'http://lv2plug.in/ns/lv2core#ControlPort'
  1353. LILV_URI_CV_PORT = 'http://lv2plug.in/ns/lv2core#CVPort'
  1354. LILV_URI_EVENT_PORT = 'http://lv2plug.in/ns/ext/event#EventPort'
  1355. LILV_URI_INPUT_PORT = 'http://lv2plug.in/ns/lv2core#InputPort'
  1356. LILV_URI_MIDI_EVENT = 'http://lv2plug.in/ns/ext/midi#MidiEvent'
  1357. LILV_URI_OUTPUT_PORT = 'http://lv2plug.in/ns/lv2core#OutputPort'
  1358. LILV_URI_PORT = 'http://lv2plug.in/ns/lv2core#Port'
  1359. LILV_OPTION_FILTER_LANG = 'http://drobilla.net/ns/lilv#filter-lang'
  1360. LILV_OPTION_DYN_MANIFEST = 'http://drobilla.net/ns/lilv#dyn-manifest'