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.

test_api.py 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. # Copyright 2016 David Robillard <d@drobilla.net>
  2. # Copyright 2013 Kaspar Emanuel <kaspar.emanuel@gmail.com>
  3. #
  4. # Permission to use, copy, modify, and/or distribute this software for any
  5. # purpose with or without fee is hereby granted, provided that the above
  6. # copyright notice and this permission notice appear in all copies.
  7. #
  8. # THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. import lilv
  16. import unittest
  17. import os
  18. location = "file://" + os.getcwd() + "/bindings/bindings_test_plugin.lv2/"
  19. class NodeTests(unittest.TestCase):
  20. def setUp(self):
  21. self.world = lilv.World()
  22. def testNodes(self):
  23. aint = self.world.new_int(1)
  24. aint2 = self.world.new_int(1)
  25. aint3 = self.world.new_int(3)
  26. afloat = self.world.new_float(2.0)
  27. atrue = self.world.new_bool(True)
  28. afalse = self.world.new_bool(False)
  29. auri = self.world.new_uri("http://example.org")
  30. afile = self.world.new_file_uri(None, "/foo/bar")
  31. astring = self.world.new_string("hello")
  32. self.assertEqual(auri.get_turtle_token(), '<http://example.org>')
  33. self.assertTrue(aint.is_int())
  34. self.assertTrue(afloat.is_float())
  35. self.assertTrue(auri.is_uri())
  36. self.assertTrue(astring.is_string())
  37. self.assertTrue(astring.is_literal())
  38. self.assertFalse(auri.is_blank())
  39. self.assertTrue(int(aint) == 1)
  40. self.assertTrue(float(afloat) == 2.0)
  41. self.assertTrue(bool(atrue))
  42. self.assertFalse(bool(afalse))
  43. self.assertEqual(afile.get_path(), "/foo/bar")
  44. self.assertTrue(aint == aint2)
  45. self.assertTrue(aint != aint3)
  46. self.assertTrue(aint != afloat)
  47. with self.assertRaises(ValueError):
  48. int(atrue)
  49. with self.assertRaises(ValueError):
  50. float(aint)
  51. with self.assertRaises(ValueError):
  52. bool(astring)
  53. class UriTests(unittest.TestCase):
  54. def setUp(self):
  55. self.world = lilv.World()
  56. self.world.load_all();
  57. def testInvalidURI(self):
  58. self.plugin_uri = self.world.new_uri("invalid_uri")
  59. self.assertIsNone(self.plugin_uri)
  60. def testNonExistentURI(self):
  61. self.plugin_uri = self.world.new_uri("exist:does_not")
  62. self.plugin = self.world.get_all_plugins().get_by_uri(self.plugin_uri)
  63. self.assertEqual(self.plugin, None)
  64. def testPortTypes(self):
  65. self.assertIsNotNone(self.world.new_uri(lilv.LILV_URI_INPUT_PORT))
  66. def testPortTypes2(self):
  67. self.assertIsNotNone(self.world.new_uri(lilv.LILV_URI_OUTPUT_PORT))
  68. def testPortTypes3(self):
  69. self.assertIsNotNone(self.world.new_uri(lilv.LILV_URI_AUDIO_PORT))
  70. def testPortTypes4(self):
  71. self.assertIsNotNone(self.world.new_uri(lilv.LILV_URI_CONTROL_PORT))
  72. class PluginClassTests(unittest.TestCase):
  73. def setUp(self):
  74. self.world = lilv.World()
  75. def testPluginClasses(self):
  76. pclass = self.world.get_plugin_class()
  77. self.assertIsNotNone(pclass)
  78. self.assertIsNone(pclass.get_parent_uri())
  79. self.assertIsNotNone(pclass.get_uri())
  80. self.assertIsNotNone(pclass.get_label())
  81. self.assertEqual(str(pclass.get_uri()), str(pclass))
  82. for i in pclass.get_children():
  83. self.assertIsNotNone(i)
  84. self.assertIsNotNone(i.get_uri())
  85. self.assertIsNotNone(i.get_label())
  86. class PluginClassesTests(unittest.TestCase):
  87. def setUp(self):
  88. self.world = lilv.World()
  89. self.world.load_all()
  90. def testPluginClasses(self):
  91. classes = self.world.get_plugin_classes()
  92. pclass = self.world.get_plugin_class()
  93. self.assertIsNotNone(classes)
  94. self.assertIsNotNone(pclass)
  95. self.assertTrue(pclass in classes)
  96. self.assertTrue(pclass.get_uri() in classes)
  97. self.assertGreater(len(classes), 1)
  98. self.assertIsNotNone(classes[0])
  99. self.assertIsNotNone(classes[pclass.get_uri()])
  100. class LoadTests(unittest.TestCase):
  101. def setUp(self):
  102. self.world = lilv.World()
  103. self.bundle_uri = self.world.new_uri(location)
  104. self.world.load_specifications()
  105. self.world.load_plugin_classes()
  106. def tearDown(self):
  107. del self.world
  108. def testLoadUnload(self):
  109. self.world.load_bundle(self.bundle_uri)
  110. plugins = self.world.get_all_plugins()
  111. plugin = plugins.get(plugins.begin())
  112. self.world.load_resource(plugin)
  113. self.world.unload_resource(plugin)
  114. self.world.unload_bundle(self.bundle_uri)
  115. class PluginTests(unittest.TestCase):
  116. def setUp(self):
  117. self.world = lilv.World()
  118. self.world.set_option(lilv.OPTION_FILTER_LANG, self.world.new_bool(True))
  119. self.bundle_uri = self.world.new_uri(location)
  120. self.assertIsNotNone(self.bundle_uri, "Invalid URI: '" + location + "'")
  121. self.world.load_bundle(self.bundle_uri)
  122. self.plugins = self.world.get_all_plugins()
  123. self.plugin = self.plugins.get(self.plugins.begin())
  124. self.assertTrue(self.plugin.verify())
  125. self.assertTrue(self.plugin in self.plugins)
  126. self.assertTrue(self.plugin.get_uri() in self.plugins)
  127. self.assertEqual(self.plugins[self.plugin.get_uri()], self.plugin)
  128. self.assertIsNotNone(self.plugin, msg="Test plugin not found at location: '" + location + "'")
  129. self.assertEqual(location, str(self.plugin.get_bundle_uri()))
  130. self.plugin_uri = self.plugin.get_uri()
  131. self.assertEqual(self.plugin.get_uri(), self.plugin_uri, "URI equality broken")
  132. self.instance = lilv.Instance(self.plugin, 48000, None)
  133. self.assertIsNotNone(self.instance)
  134. self.lv2_InputPort = self.world.new_uri(lilv.LILV_URI_INPUT_PORT)
  135. self.lv2_OutputPort = self.world.new_uri(lilv.LILV_URI_OUTPUT_PORT)
  136. self.lv2_AudioPort = self.world.new_uri(lilv.LILV_URI_AUDIO_PORT)
  137. self.lv2_ControlPort = self.world.new_uri(lilv.LILV_URI_CONTROL_PORT)
  138. def testGetters(self):
  139. self.assertIsNotNone(self.plugin.get_bundle_uri())
  140. self.assertGreater(len(self.plugin.get_data_uris()), 0)
  141. self.assertIsNotNone(self.plugin.get_library_uri())
  142. self.assertTrue(self.plugin.get_name().is_string())
  143. self.assertTrue(self.plugin.get_class().get_uri().is_uri())
  144. self.assertEqual(len(self.plugin.get_value(self.world.ns.doap.license)), 1)
  145. licenses = self.plugin.get_value(self.world.ns.doap.license)
  146. features = self.plugin.get_value(self.world.ns.lv2.optionalFeature)
  147. self.assertEqual(len(licenses), 1)
  148. self.assertTrue(licenses[0] in licenses)
  149. with self.assertRaises(IndexError):
  150. self.assertIsNone(licenses[len(licenses)])
  151. self.assertEqual(len(licenses) + len(features),
  152. len(licenses.merge(features)))
  153. self.assertEqual(licenses.get(licenses.begin()), self.world.new_uri('http://opensource.org/licenses/isc'))
  154. self.assertEqual(licenses[0], licenses.get(licenses.begin()))
  155. self.assertTrue(self.plugin.has_feature(self.world.ns.lv2.hardRTCapable))
  156. self.assertEqual(len(self.plugin.get_supported_features()), 1)
  157. self.assertEqual(len(self.plugin.get_optional_features()), 1)
  158. self.assertEqual(len(self.plugin.get_required_features()), 0)
  159. self.assertFalse(self.plugin.has_extension_data(self.world.new_uri('http://example.org/nope')))
  160. self.assertEqual(len(self.plugin.get_extension_data()), 0)
  161. self.assertEqual(len(self.plugin.get_extension_data()), 0)
  162. self.assertFalse(self.plugin.has_latency())
  163. self.assertIsNone(self.plugin.get_latency_port_index())
  164. def testPorts(self):
  165. self.assertEqual(self.plugin.get_num_ports(), 4)
  166. self.assertIsNotNone(self.plugin.get_port(0))
  167. self.assertIsNotNone(self.plugin.get_port(1))
  168. self.assertIsNotNone(self.plugin.get_port(2))
  169. self.assertIsNotNone(self.plugin.get_port(3))
  170. self.assertIsNone(self.plugin.get_port_by_index(4))
  171. self.assertIsNotNone(self.plugin.get_port("input"))
  172. self.assertIsNotNone(self.plugin.get_port("output"))
  173. self.assertIsNotNone(self.plugin.get_port("audio_input"))
  174. self.assertIsNotNone(self.plugin.get_port("audio_output"))
  175. self.assertIsNone(self.plugin.get_port_by_symbol("nonexistent"))
  176. self.assertIsNone(self.plugin.get_port_by_designation(self.world.ns.lv2.InputPort, self.world.ns.lv2.control))
  177. self.assertIsNone(self.plugin.get_project())
  178. self.assertIsNone(self.plugin.get_author_name())
  179. self.assertIsNone(self.plugin.get_author_email())
  180. self.assertIsNone(self.plugin.get_author_homepage())
  181. self.assertFalse(self.plugin.is_replaced())
  182. self.assertEqual(0, len(self.plugin.get_related(self.world.new_uri("http://example.org/Type"))))
  183. self.assertEqual(1, self.plugin.get_num_ports_of_class(self.lv2_InputPort, self.lv2_AudioPort))
  184. port = self.plugin.get_port("input")
  185. self.assertTrue(port.get_node().is_blank())
  186. self.assertEqual(0, port.get(self.world.ns.lv2.index))
  187. self.assertEqual(1, len(port.get_value(self.world.ns.lv2.symbol)))
  188. self.assertEqual(port.get_value(self.world.ns.lv2.symbol)[0], "input")
  189. self.assertFalse(port.has_property(self.world.ns.lv2.latency))
  190. self.assertFalse(port.supports_event(self.world.ns.midi.MidiEvent))
  191. self.assertEqual(0, port.get_index())
  192. self.assertEqual("input", port.get_symbol())
  193. self.assertEqual("Input", port.get_name())
  194. self.assertEqual([self.world.ns.lv2.ControlPort, self.world.ns.lv2.InputPort],
  195. list(port.get_classes()))
  196. self.assertTrue(port.is_a(self.world.ns.lv2.ControlPort))
  197. self.assertFalse(port.is_a(self.world.ns.lv2.AudioPort))
  198. self.assertEquals((0.5, 0.0, 1.0), port.get_range())
  199. self.assertEquals(0, len(port.get_properties()))
  200. def testScalePoints(self):
  201. port = self.plugin.get_port("input")
  202. points = port.get_scale_points()
  203. self.assertEqual(points[0].get_label(), "off")
  204. self.assertEqual(points[0].get_value(), 0.0)
  205. self.assertEqual(points[1].get_label(), "on")
  206. self.assertEqual(points[1].get_value(), 1.0)
  207. def testPortCount(self):
  208. self.assertEqual(1, self.plugin.get_num_ports_of_class(self.lv2_OutputPort, self.lv2_AudioPort))
  209. self.assertEqual(1, self.plugin.get_num_ports_of_class(self.lv2_OutputPort, self.lv2_ControlPort))
  210. self.assertEqual(1, self.plugin.get_num_ports_of_class(self.lv2_InputPort, self.lv2_AudioPort))
  211. self.assertEqual(1, self.plugin.get_num_ports_of_class(self.lv2_InputPort, self.lv2_ControlPort))
  212. class QueryTests(unittest.TestCase):
  213. def setUp(self):
  214. self.world = lilv.World()
  215. self.world.load_all()
  216. self.bundle_uri = self.world.new_uri(location)
  217. self.world.load_bundle(self.bundle_uri)
  218. self.plugins = self.world.get_all_plugins()
  219. self.plugin = self.plugins.get(self.plugins.begin())
  220. def testNamespaces(self):
  221. self.assertEqual(self.world.ns.lv2, "http://lv2plug.in/ns/lv2core#")
  222. self.assertEqual(self.world.ns.lv2.Plugin, "http://lv2plug.in/ns/lv2core#Plugin")
  223. def testQuery(self):
  224. self.assertTrue(self.world.ask(None,
  225. self.world.ns.rdf.type,
  226. self.world.ns.lv2.Plugin))
  227. self.assertLess(0, len(self.world.find_nodes(None,
  228. self.world.ns.rdf.type,
  229. self.world.ns.lv2.Plugin)))
  230. self.assertEqual(self.plugin.get_uri(), self.world.get(None,
  231. self.world.ns.rdf.type,
  232. self.world.ns.lv2.Plugin))
  233. class InstanceTests(unittest.TestCase):
  234. def setUp(self):
  235. self.world = lilv.World()
  236. self.bundle_uri = self.world.new_uri(location)
  237. self.world.load_bundle(self.bundle_uri)
  238. self.plugins = self.world.get_all_plugins()
  239. self.plugin = self.plugins[0]
  240. self.instance = lilv.Instance(self.plugin, 48000)
  241. self.assertEqual(self.plugin.get_uri(), self.instance.get_uri())
  242. self.assertIsNone(self.instance.get_extension_data(self.world.new_uri("http://example.org/ext")))
  243. self.assertIsNone(self.instance.get_extension_data("http://example.org/ext"))
  244. def testRun(self):
  245. import numpy
  246. n_samples = 100
  247. buf = numpy.zeros(n_samples)
  248. with self.assertRaises(Exception):
  249. self.instance.connect_port(0, "hello")
  250. self.instance.connect_port(0, None)
  251. self.instance.connect_port(0, None)
  252. self.instance.connect_port(2, buf)
  253. self.instance.connect_port(3, buf)
  254. self.instance.activate()
  255. self.instance.run(n_samples)
  256. self.instance.deactivate()
  257. class UITests(unittest.TestCase):
  258. def setUp(self):
  259. self.world = lilv.World()
  260. self.bundle_uri = self.world.new_uri(location)
  261. self.world.load_bundle(self.bundle_uri)
  262. self.plugins = self.world.get_all_plugins()
  263. self.plugin = self.plugins[0]
  264. def testUI(self):
  265. uis = self.plugin.get_uis()
  266. ui_uri = self.world.new_uri('http://example.org/lilv-bindings-test-plugin-ui')
  267. self.assertEqual(1, len(uis))
  268. self.assertEqual(str(uis[0]), str(ui_uri))
  269. self.assertEqual(uis[0], str(ui_uri))
  270. self.assertEqual(uis[0].get_uri(), ui_uri)
  271. self.assertEqual(uis[0].get_bundle_uri(), self.bundle_uri)
  272. self.assertEqual(uis[0].get_binary_uri(), str(self.bundle_uri) + "TODO")
  273. self.assertEqual(uis[uis[0].get_uri()], uis[0])
  274. self.assertTrue(uis[0].is_a(self.world.ns.ui.GtkUI))
  275. self.assertTrue(uis[0] in uis)
  276. self.assertTrue(uis[0].get_uri() in uis)
  277. self.assertEqual([self.world.ns.ui.GtkUI], list(uis[0].get_classes()))
  278. for ui in uis:
  279. print(ui)