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.

385 lines
9.7KB

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