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.c 9.9KB

9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. Lilv Test Plugin
  3. Copyright 2011-2015 David Robillard <d@drobilla.net>
  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. THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  10. ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  12. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  13. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. */
  15. #include <assert.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include "lv2/lv2plug.in/ns/ext/atom/atom.h"
  20. #include "lv2/lv2plug.in/ns/ext/state/state.h"
  21. #include "lv2/lv2plug.in/ns/ext/urid/urid.h"
  22. #include "lv2/lv2plug.in/ns/lv2core/lv2.h"
  23. #define TEST_URI "http://example.org/lilv-test-plugin"
  24. enum {
  25. TEST_INPUT = 0,
  26. TEST_OUTPUT = 1,
  27. TEST_CONTROL = 2
  28. };
  29. typedef struct {
  30. LV2_URID_Map* map;
  31. struct {
  32. LV2_URID atom_Float;
  33. } uris;
  34. char* tmp_file_path;
  35. char* rec_file_path;
  36. FILE* rec_file;
  37. float* input;
  38. float* output;
  39. unsigned num_runs;
  40. } Test;
  41. static void
  42. cleanup(LV2_Handle instance)
  43. {
  44. Test* test = (Test*)instance;
  45. if (test->rec_file) {
  46. fclose(test->rec_file);
  47. }
  48. free(test->tmp_file_path);
  49. free(test->rec_file_path);
  50. free(instance);
  51. }
  52. static void
  53. connect_port(LV2_Handle instance,
  54. uint32_t port,
  55. void* data)
  56. {
  57. Test* test = (Test*)instance;
  58. switch (port) {
  59. case TEST_INPUT:
  60. test->input = (float*)data;
  61. break;
  62. case TEST_OUTPUT:
  63. test->output = (float*)data;
  64. break;
  65. case TEST_CONTROL:
  66. test->output = (float*)data;
  67. break;
  68. default:
  69. break;
  70. }
  71. }
  72. static LV2_Handle
  73. instantiate(const LV2_Descriptor* descriptor,
  74. double rate,
  75. const char* path,
  76. const LV2_Feature* const* features)
  77. {
  78. Test* test = (Test*)malloc(sizeof(Test));
  79. if (!test) {
  80. return NULL;
  81. }
  82. test->map = NULL;
  83. test->input = NULL;
  84. test->output = NULL;
  85. test->num_runs = 0;
  86. test->tmp_file_path = (char*)malloc(L_tmpnam);
  87. test->rec_file_path = NULL;
  88. test->rec_file = NULL;
  89. tmpnam(test->tmp_file_path);
  90. LV2_State_Make_Path* make_path = NULL;
  91. for (int i = 0; features[i]; ++i) {
  92. if (!strcmp(features[i]->URI, LV2_URID_URI "#map")) {
  93. test->map = (LV2_URID_Map*)features[i]->data;
  94. test->uris.atom_Float = test->map->map(
  95. test->map->handle, LV2_ATOM__Float);
  96. } else if (!strcmp(features[i]->URI, LV2_STATE__makePath)) {
  97. make_path = (LV2_State_Make_Path*)features[i]->data;
  98. }
  99. }
  100. if (!test->map) {
  101. fprintf(stderr, "Host does not support urid:map\n");
  102. free(test);
  103. return NULL;
  104. }
  105. if (make_path) {
  106. test->rec_file_path = make_path->path(make_path->handle, "recfile");
  107. if (!(test->rec_file = fopen(test->rec_file_path, "w"))) {
  108. fprintf(stderr, "ERROR: Failed to open rec file\n");
  109. }
  110. fprintf(test->rec_file, "instantiate\n");
  111. }
  112. return (LV2_Handle)test;
  113. }
  114. static void
  115. run(LV2_Handle instance,
  116. uint32_t sample_count)
  117. {
  118. Test* test = (Test*)instance;
  119. *test->output = *test->input;
  120. if (sample_count == 1) {
  121. ++test->num_runs;
  122. } else if (sample_count == 2 && test->rec_file) {
  123. // Append to rec file (changes size)
  124. fprintf(test->rec_file, "run\n");
  125. } else if (sample_count == 3 && test->rec_file) {
  126. // Change the first byte of rec file (doesn't change size)
  127. fseek(test->rec_file, 0, SEEK_SET);
  128. fprintf(test->rec_file, "X");
  129. fseek(test->rec_file, 0, SEEK_END);
  130. }
  131. }
  132. static uint32_t
  133. map_uri(Test* plugin, const char* uri)
  134. {
  135. return plugin->map->map(plugin->map->handle, uri);
  136. }
  137. static LV2_State_Status
  138. save(LV2_Handle instance,
  139. LV2_State_Store_Function store,
  140. void* callback_data,
  141. uint32_t flags,
  142. const LV2_Feature* const* features)
  143. {
  144. Test* plugin = (Test*)instance;
  145. LV2_State_Map_Path* map_path = NULL;
  146. LV2_State_Make_Path* make_path = NULL;
  147. for (int i = 0; features && features[i]; ++i) {
  148. if (!strcmp(features[i]->URI, LV2_STATE__mapPath)) {
  149. map_path = (LV2_State_Map_Path*)features[i]->data;
  150. } else if (!strcmp(features[i]->URI, LV2_STATE__makePath)) {
  151. make_path = (LV2_State_Make_Path*)features[i]->data;
  152. }
  153. }
  154. store(callback_data,
  155. map_uri(plugin, "http://example.org/greeting"),
  156. "hello",
  157. strlen("hello") + 1,
  158. map_uri(plugin, LV2_ATOM__String),
  159. LV2_STATE_IS_POD | LV2_STATE_IS_PORTABLE);
  160. const uint32_t urid = map_uri(plugin, "http://example.org/urivalue");
  161. store(callback_data,
  162. map_uri(plugin, "http://example.org/uri"),
  163. &urid,
  164. sizeof(uint32_t),
  165. map_uri(plugin, LV2_ATOM__URID),
  166. LV2_STATE_IS_POD | LV2_STATE_IS_PORTABLE);
  167. store(callback_data,
  168. map_uri(plugin, "http://example.org/num-runs"),
  169. &plugin->num_runs,
  170. sizeof(plugin->num_runs),
  171. map_uri(plugin, LV2_ATOM__Int),
  172. LV2_STATE_IS_POD | LV2_STATE_IS_PORTABLE);
  173. const float two = 2.0f;
  174. store(callback_data,
  175. map_uri(plugin, "http://example.org/two"),
  176. &two,
  177. sizeof(two),
  178. map_uri(plugin, LV2_ATOM__Float),
  179. LV2_STATE_IS_POD | LV2_STATE_IS_PORTABLE);
  180. const uint32_t affirmative = 1;
  181. store(callback_data,
  182. map_uri(plugin, "http://example.org/true"),
  183. &affirmative,
  184. sizeof(affirmative),
  185. map_uri(plugin, LV2_ATOM__Bool),
  186. LV2_STATE_IS_POD | LV2_STATE_IS_PORTABLE);
  187. const uint32_t negative = 0;
  188. store(callback_data,
  189. map_uri(plugin, "http://example.org/false"),
  190. &negative,
  191. sizeof(negative),
  192. map_uri(plugin, LV2_ATOM__Bool),
  193. LV2_STATE_IS_POD | LV2_STATE_IS_PORTABLE);
  194. const uint8_t blob[] = "I am a blob of arbitrary data.";
  195. store(callback_data,
  196. map_uri(plugin, "http://example.org/blob"),
  197. blob,
  198. sizeof(blob),
  199. map_uri(plugin, "http://example.org/SomeUnknownType"),
  200. LV2_STATE_IS_POD | LV2_STATE_IS_PORTABLE);
  201. if (map_path) {
  202. FILE* file = fopen(plugin->tmp_file_path, "w");
  203. fprintf(file, "Hello\n");
  204. fclose(file);
  205. char* apath = map_path->abstract_path(map_path->handle,
  206. plugin->tmp_file_path);
  207. char* apath2 = map_path->abstract_path(map_path->handle,
  208. plugin->tmp_file_path);
  209. if (strcmp(apath, apath2)) {
  210. fprintf(stderr, "ERROR: Path %s != %s\n", apath, apath2);
  211. }
  212. store(callback_data,
  213. map_uri(plugin, "http://example.org/extfile"),
  214. apath,
  215. strlen(apath) + 1,
  216. map_uri(plugin, LV2_ATOM__Path),
  217. LV2_STATE_IS_POD);
  218. free(apath);
  219. free(apath2);
  220. if (plugin->rec_file) {
  221. fflush(plugin->rec_file);
  222. apath = map_path->abstract_path(map_path->handle,
  223. plugin->rec_file_path);
  224. store(callback_data,
  225. map_uri(plugin, "http://example.org/recfile"),
  226. apath,
  227. strlen(apath) + 1,
  228. map_uri(plugin, LV2_ATOM__Path),
  229. LV2_STATE_IS_POD);
  230. free(apath);
  231. }
  232. if (make_path) {
  233. char* spath = make_path->path(make_path->handle, "save");
  234. FILE* sfile = fopen(spath, "w");
  235. fprintf(sfile, "save");
  236. fclose(sfile);
  237. apath = map_path->abstract_path(map_path->handle, spath);
  238. store(callback_data,
  239. map_uri(plugin, "http://example.org/save-file"),
  240. apath,
  241. strlen(apath) + 1,
  242. map_uri(plugin, LV2_ATOM__Path),
  243. LV2_STATE_IS_POD);
  244. free(apath);
  245. free(spath);
  246. }
  247. }
  248. return LV2_STATE_SUCCESS;
  249. }
  250. static LV2_State_Status
  251. restore(LV2_Handle instance,
  252. LV2_State_Retrieve_Function retrieve,
  253. void* callback_data,
  254. uint32_t flags,
  255. const LV2_Feature* const* features)
  256. {
  257. Test* plugin = (Test*)instance;
  258. LV2_State_Map_Path* map_path = NULL;
  259. for (int i = 0; features && features[i]; ++i) {
  260. if (!strcmp(features[i]->URI, LV2_STATE__mapPath)) {
  261. map_path = (LV2_State_Map_Path*)features[i]->data;
  262. }
  263. }
  264. size_t size;
  265. uint32_t type;
  266. uint32_t valflags;
  267. plugin->num_runs = *(int32_t*)retrieve(
  268. callback_data,
  269. map_uri(plugin, "http://example.org/num-runs"),
  270. &size, &type, &valflags);
  271. if (!map_path) {
  272. return LV2_STATE_ERR_NO_FEATURE;
  273. }
  274. char* apath = (char*)retrieve(
  275. callback_data,
  276. map_uri(plugin, "http://example.org/extfile"),
  277. &size, &type, &valflags);
  278. if (valflags != LV2_STATE_IS_POD) {
  279. fprintf(stderr, "error: Restored bad file flags\n");
  280. return LV2_STATE_ERR_BAD_FLAGS;
  281. }
  282. if (apath) {
  283. char* path = map_path->absolute_path(map_path->handle, apath);
  284. FILE* f = fopen(path, "r");
  285. char str[8];
  286. size_t n_read = fread(str, 1, sizeof(str), f);
  287. fclose(f);
  288. if (strncmp(str, "Hello\n", n_read)) {
  289. fprintf(stderr, "error: Restored bad file contents `%s' != `Hello'\n",
  290. str);
  291. }
  292. free(path);
  293. }
  294. apath = (char*)retrieve(
  295. callback_data,
  296. map_uri(plugin, "http://example.org/save-file"),
  297. &size, &type, &valflags);
  298. if (apath) {
  299. char* spath = map_path->absolute_path(map_path->handle, apath);
  300. FILE* sfile = fopen(spath, "r");
  301. if (!sfile) {
  302. fprintf(stderr, "error: Failed to open save file %s\n", spath);
  303. } else {
  304. fclose(sfile);
  305. }
  306. free(spath);
  307. } else {
  308. fprintf(stderr, "error: Failed to restore save file.\n");
  309. }
  310. return LV2_STATE_SUCCESS;
  311. }
  312. static const void*
  313. extension_data(const char* uri)
  314. {
  315. static const LV2_State_Interface state = { save, restore };
  316. if (!strcmp(uri, LV2_STATE__interface)) {
  317. return &state;
  318. }
  319. return NULL;
  320. }
  321. static const LV2_Descriptor descriptor = {
  322. TEST_URI,
  323. instantiate,
  324. connect_port,
  325. NULL, // activate,
  326. run,
  327. NULL, // deactivate,
  328. cleanup,
  329. extension_data
  330. };
  331. LV2_SYMBOL_EXPORT
  332. const LV2_Descriptor* lv2_descriptor(uint32_t index)
  333. {
  334. switch (index) {
  335. case 0:
  336. return &descriptor;
  337. default:
  338. return NULL;
  339. }
  340. }