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.

264 lines
7.1KB

  1. /*
  2. Copyright 2007-2014 David Robillard <http://drobilla.net>
  3. Permission to use, copy, modify, and/or distribute this software for any
  4. purpose with or without fee is hereby granted, provided that the above
  5. copyright notice and this permission notice appear in all copies.
  6. THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  7. WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  8. MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  9. ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  10. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  11. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  12. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  13. */
  14. #include <assert.h>
  15. #include <limits.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include "lv2/atom.h"
  20. #include "lv2/event.h"
  21. #include "lilv_internal.h"
  22. LilvPort*
  23. lilv_port_new(LilvWorld* world,
  24. const SordNode* node,
  25. uint32_t index,
  26. const char* symbol)
  27. {
  28. LilvPort* port = (LilvPort*)malloc(sizeof(LilvPort));
  29. port->node = lilv_node_new_from_node(world, node);
  30. port->index = index;
  31. port->symbol = lilv_node_new(world, LILV_VALUE_STRING, symbol);
  32. port->classes = lilv_nodes_new();
  33. return port;
  34. }
  35. void
  36. lilv_port_free(const LilvPlugin* plugin, LilvPort* port)
  37. {
  38. if (port) {
  39. lilv_node_free(port->node);
  40. lilv_nodes_free(port->classes);
  41. lilv_node_free(port->symbol);
  42. free(port);
  43. }
  44. }
  45. LILV_API bool
  46. lilv_port_is_a(const LilvPlugin* plugin,
  47. const LilvPort* port,
  48. const LilvNode* port_class)
  49. {
  50. LILV_FOREACH(nodes, i, port->classes)
  51. if (lilv_node_equals(lilv_nodes_get(port->classes, i), port_class))
  52. return true;
  53. return false;
  54. }
  55. LILV_API bool
  56. lilv_port_has_property(const LilvPlugin* p,
  57. const LilvPort* port,
  58. const LilvNode* property)
  59. {
  60. return lilv_world_ask_internal(p->world,
  61. port->node->node,
  62. p->world->uris.lv2_portProperty,
  63. property->node);
  64. }
  65. LILV_API bool
  66. lilv_port_supports_event(const LilvPlugin* p,
  67. const LilvPort* port,
  68. const LilvNode* event)
  69. {
  70. const uint8_t* predicates[] = { (const uint8_t*)LV2_EVENT__supportsEvent,
  71. (const uint8_t*)LV2_ATOM__supports,
  72. NULL };
  73. for (const uint8_t** pred = predicates; *pred; ++pred) {
  74. if (lilv_world_ask_internal(p->world,
  75. port->node->node,
  76. sord_new_uri(p->world->world, *pred),
  77. event->node)) {
  78. return true;
  79. }
  80. }
  81. return false;
  82. }
  83. static LilvNodes*
  84. lilv_port_get_value_by_node(const LilvPlugin* p,
  85. const LilvPort* port,
  86. const SordNode* predicate)
  87. {
  88. return lilv_world_find_nodes_internal(p->world,
  89. port->node->node,
  90. predicate,
  91. NULL);
  92. }
  93. LILV_API const LilvNode*
  94. lilv_port_get_node(const LilvPlugin* plugin,
  95. const LilvPort* port)
  96. {
  97. return port->node;
  98. }
  99. LILV_API LilvNodes*
  100. lilv_port_get_value(const LilvPlugin* p,
  101. const LilvPort* port,
  102. const LilvNode* predicate)
  103. {
  104. if (!lilv_node_is_uri(predicate)) {
  105. LILV_ERRORF("Predicate `%s' is not a URI\n",
  106. sord_node_get_string(predicate->node));
  107. return NULL;
  108. }
  109. return lilv_port_get_value_by_node(p, port, predicate->node);
  110. }
  111. LILV_API LilvNode*
  112. lilv_port_get(const LilvPlugin* p,
  113. const LilvPort* port,
  114. const LilvNode* predicate)
  115. {
  116. LilvNodes* values = lilv_port_get_value(p, port, predicate);
  117. LilvNode* value = lilv_node_duplicate(
  118. values ? lilv_nodes_get_first(values) : NULL);
  119. lilv_nodes_free(values);
  120. return value;
  121. }
  122. LILV_API uint32_t
  123. lilv_port_get_index(const LilvPlugin* p,
  124. const LilvPort* port)
  125. {
  126. return port->index;
  127. }
  128. LILV_API const LilvNode*
  129. lilv_port_get_symbol(const LilvPlugin* p,
  130. const LilvPort* port)
  131. {
  132. return port->symbol;
  133. }
  134. LILV_API LilvNode*
  135. lilv_port_get_name(const LilvPlugin* p,
  136. const LilvPort* port)
  137. {
  138. LilvNodes* results = lilv_port_get_value_by_node(
  139. p, port, p->world->uris.lv2_name);
  140. LilvNode* ret = NULL;
  141. if (results) {
  142. LilvNode* val = lilv_nodes_get_first(results);
  143. if (lilv_node_is_string(val))
  144. ret = lilv_node_duplicate(val);
  145. lilv_nodes_free(results);
  146. }
  147. if (!ret)
  148. LILV_WARNF("Plugin <%s> port has no (mandatory) doap:name\n",
  149. lilv_node_as_string(lilv_plugin_get_uri(p)));
  150. return ret;
  151. }
  152. LILV_API const LilvNodes*
  153. lilv_port_get_classes(const LilvPlugin* p,
  154. const LilvPort* port)
  155. {
  156. return port->classes;
  157. }
  158. LILV_API void
  159. lilv_port_get_range(const LilvPlugin* p,
  160. const LilvPort* port,
  161. LilvNode** def,
  162. LilvNode** min,
  163. LilvNode** max)
  164. {
  165. if (def) {
  166. LilvNodes* defaults = lilv_port_get_value_by_node(
  167. p, port, p->world->uris.lv2_default);
  168. *def = defaults
  169. ? lilv_node_duplicate(lilv_nodes_get_first(defaults))
  170. : NULL;
  171. lilv_nodes_free(defaults);
  172. }
  173. if (min) {
  174. LilvNodes* minimums = lilv_port_get_value_by_node(
  175. p, port, p->world->uris.lv2_minimum);
  176. *min = minimums
  177. ? lilv_node_duplicate(lilv_nodes_get_first(minimums))
  178. : NULL;
  179. lilv_nodes_free(minimums);
  180. }
  181. if (max) {
  182. LilvNodes* maximums = lilv_port_get_value_by_node(
  183. p, port, p->world->uris.lv2_maximum);
  184. *max = maximums
  185. ? lilv_node_duplicate(lilv_nodes_get_first(maximums))
  186. : NULL;
  187. lilv_nodes_free(maximums);
  188. }
  189. }
  190. LILV_API LilvScalePoints*
  191. lilv_port_get_scale_points(const LilvPlugin* p,
  192. const LilvPort* port)
  193. {
  194. SordIter* points = lilv_world_query_internal(
  195. p->world,
  196. port->node->node,
  197. sord_new_uri(p->world->world, (const uint8_t*)LV2_CORE__scalePoint),
  198. NULL);
  199. LilvScalePoints* ret = NULL;
  200. if (!sord_iter_end(points))
  201. ret = lilv_scale_points_new();
  202. FOREACH_MATCH(points) {
  203. const SordNode* point = sord_iter_get_node(points, SORD_OBJECT);
  204. LilvNode* value = lilv_plugin_get_unique(p,
  205. point,
  206. p->world->uris.rdf_value);
  207. LilvNode* label = lilv_plugin_get_unique(p,
  208. point,
  209. p->world->uris.rdfs_label);
  210. if (value && label) {
  211. zix_tree_insert(
  212. (ZixTree*)ret, lilv_scale_point_new(value, label), NULL);
  213. }
  214. }
  215. sord_iter_free(points);
  216. assert(!ret || lilv_nodes_size(ret) > 0);
  217. return ret;
  218. }
  219. LILV_API LilvNodes*
  220. lilv_port_get_properties(const LilvPlugin* p,
  221. const LilvPort* port)
  222. {
  223. LilvNode* pred = lilv_node_new_from_node(
  224. p->world, p->world->uris.lv2_portProperty);
  225. LilvNodes* ret = lilv_port_get_value(p, port, pred);
  226. lilv_node_free(pred);
  227. return ret;
  228. }