lynx_test_ext.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /* A CLAP effect for LYNX's CLAP extension and timing tests (round 190jg, BUILD-LIST 18):
  2. * Windows: zig cc -shared -O2 -target x86_64-windows-gnu -Ithird_party/clap/include tools/testplugin/lynx_test_ext.c \
  3. * -o "LYNX Test Ext.clap"
  4. * Linux / macOS: cc -shared -fPIC -O2 -Ithird_party/clap/include tools/testplugin/lynx_test_ext.c -lpthread -o "LYNX Test Ext.clap"
  5. * then: LYNX --clapext "LYNX Test Ext.clap" report.txt; LYNX --automationtimingtest "LYNX Test Ext.clap" report.txt
  6. * Sound: it ignores its input and outputs its "Level" parameter (id 7, 0..1) as a constant (DC) on both channels,
  7. * taking each CLAP_EVENT_PARAM_VALUE at its own sample (the event's time): a step of the automation shows in the output
  8. * at the sample the host put it.
  9. * It also uses / offers, and writes down in the file named by the environment variable LYNX_TEST_EXT_LOG:
  10. * thread-pool on its 10th process call it asks the host to run 8 tasks ("pool ok=1 tasks=8 threads=<n>")
  11. * tail 4800 samples; voice-info: 3 voices of 16, overlapping notes
  12. * render the mode the host sets ("render offline" / "render realtime")
  13. * state-context a state saved for a preset / a duplicate / the project ("save context=<n>", "load context=<n>")
  14. * remote-controls one page "Test / Main": Level, Width, then empty knobs
  15. * preset-discovery + preset-load: three presets inside the plugin ("Low" 0.25, "Mid" 0.5, "High" 0.75)
  16. * and the parameter events it received ("P <time> <value> n=<frames>", the first 400).
  17. */
  18. #include <clap/clap.h>
  19. #include <stdatomic.h>
  20. #include <stdint.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #if defined(_WIN32)
  25. #include <windows.h>
  26. #else
  27. #include <pthread.h>
  28. #endif
  29. #define LEVEL_ID 7
  30. #define WIDTH_ID 9
  31. static const char* const features[] = { CLAP_PLUGIN_FEATURE_AUDIO_EFFECT, CLAP_PLUGIN_FEATURE_UTILITY, NULL };
  32. static const clap_plugin_descriptor_t descriptor = {
  33. CLAP_VERSION_INIT, "org.lynx.test-ext", "LYNX Test Ext", "LYNX", "https://github.com/Ka1-S/LYNX-SHARE", "", "", "1.0.0",
  34. "CLAP extension and timing tests", features
  35. };
  36. static FILE* log_file (void)
  37. {
  38. static FILE* f = NULL;
  39. static int opened = 0;
  40. if (! opened)
  41. {
  42. opened = 1;
  43. #if defined(_WIN32)
  44. char path[1024] = { 0 };
  45. if (GetEnvironmentVariableA ("LYNX_TEST_EXT_LOG", path, sizeof (path)) > 0)
  46. f = fopen (path, "a");
  47. #else
  48. const char* path = getenv ("LYNX_TEST_EXT_LOG");
  49. if (path != NULL)
  50. f = fopen (path, "a");
  51. #endif
  52. }
  53. return f;
  54. }
  55. static void say (const char* text)
  56. {
  57. FILE* f = log_file();
  58. if (f != NULL)
  59. {
  60. fputs (text, f);
  61. fflush (f);
  62. }
  63. }
  64. typedef struct
  65. {
  66. clap_plugin_t plugin;
  67. const clap_host_t* host;
  68. double level, width;
  69. int calls, logged;
  70. atomic_int tasks;
  71. atomic_ullong threadIds[8];
  72. } Ext;
  73. static Ext* self_of (const clap_plugin_t* p) { return (Ext*) p->plugin_data; }
  74. static unsigned long long thread_id (void)
  75. {
  76. #if defined(_WIN32)
  77. return (unsigned long long) GetCurrentThreadId();
  78. #else
  79. return (unsigned long long) (uintptr_t) pthread_self();
  80. #endif
  81. }
  82. /* audio ports */
  83. /* round 190rk: a second port each way (a "Sidechain" input, an "Aux" output), as FabFilter's effects have: the host
  84. * must give process() one buffer per declared port (it gave the main ones only, and they read past the array) */
  85. static uint32_t ports_count (const clap_plugin_t* p, bool in) { (void) p; (void) in; return 2; }
  86. static bool ports_get (const clap_plugin_t* p, uint32_t index, bool in, clap_audio_port_info_t* info)
  87. {
  88. (void) p;
  89. if (index > 1) return false;
  90. memset (info, 0, sizeof (*info));
  91. info->id = (in ? 0 : 1) + 10 * index;
  92. strcpy (info->name, index == 1 ? (in ? "Sidechain" : "Aux") : (in ? "In" : "Out"));
  93. info->flags = index == 0 ? CLAP_AUDIO_PORT_IS_MAIN : 0;
  94. info->channel_count = 2;
  95. info->port_type = CLAP_PORT_STEREO;
  96. info->in_place_pair = CLAP_INVALID_ID;
  97. return true;
  98. }
  99. static const clap_plugin_audio_ports_t audio_ports = { ports_count, ports_get };
  100. /* params */
  101. static uint32_t params_count (const clap_plugin_t* p) { (void) p; return 2; }
  102. static bool params_info (const clap_plugin_t* p, uint32_t index, clap_param_info_t* info)
  103. {
  104. (void) p;
  105. if (index > 1) return false;
  106. memset (info, 0, sizeof (*info));
  107. info->id = index == 0 ? LEVEL_ID : WIDTH_ID;
  108. info->flags = CLAP_PARAM_IS_AUTOMATABLE;
  109. strcpy (info->name, index == 0 ? "Level" : "Width");
  110. info->min_value = 0.0;
  111. info->max_value = 1.0;
  112. info->default_value = 0.5;
  113. return true;
  114. }
  115. static bool params_value (const clap_plugin_t* p, clap_id id, double* value)
  116. {
  117. Ext* e = self_of (p);
  118. if (id == LEVEL_ID) { *value = e->level; return true; }
  119. if (id == WIDTH_ID) { *value = e->width; return true; }
  120. return false;
  121. }
  122. static bool params_to_text (const clap_plugin_t* p, clap_id id, double v, char* out, uint32_t size)
  123. {
  124. (void) p; (void) id;
  125. snprintf (out, size, "%.3f", v);
  126. return true;
  127. }
  128. static bool params_from_text (const clap_plugin_t* p, clap_id id, const char* text, double* v)
  129. {
  130. (void) p; (void) id;
  131. *v = atof (text);
  132. return true;
  133. }
  134. static void apply_event (Ext* e, const clap_event_header_t* h, uint32_t frames)
  135. {
  136. if (h->space_id != CLAP_CORE_EVENT_SPACE_ID || h->type != CLAP_EVENT_PARAM_VALUE) return;
  137. const clap_event_param_value_t* v = (const clap_event_param_value_t*) h;
  138. if (v->param_id == LEVEL_ID)
  139. {
  140. e->level = v->value;
  141. if (e->logged < 400)
  142. {
  143. char line[96];
  144. snprintf (line, sizeof (line), "P %u %.6f n=%u\n", (unsigned) h->time, v->value, (unsigned) frames);
  145. say (line);
  146. ++e->logged;
  147. }
  148. }
  149. else if (v->param_id == WIDTH_ID)
  150. e->width = v->value;
  151. }
  152. static void params_flush (const clap_plugin_t* p, const clap_input_events_t* in, const clap_output_events_t* out)
  153. {
  154. (void) out;
  155. for (uint32_t i = 0; i < in->size (in); ++i)
  156. apply_event (self_of (p), in->get (in, i), 0);
  157. }
  158. static const clap_plugin_params_t params = { params_count, params_info, params_value, params_to_text, params_from_text, params_flush };
  159. /* state + state-context */
  160. static bool write_state (Ext* e, const clap_ostream_t* s, uint32_t context)
  161. {
  162. unsigned char data[12];
  163. float level = (float) e->level, width = (float) e->width;
  164. memcpy (data, "LXE", 3);
  165. data[3] = (unsigned char) context;
  166. memcpy (data + 4, &level, 4);
  167. memcpy (data + 8, &width, 4);
  168. return s->write (s, data, sizeof (data)) == (int64_t) sizeof (data);
  169. }
  170. static bool read_state (Ext* e, const clap_istream_t* s, uint32_t* context)
  171. {
  172. unsigned char data[12];
  173. if (s->read (s, data, sizeof (data)) != (int64_t) sizeof (data) || memcmp (data, "LXE", 3) != 0) return false;
  174. float level, width;
  175. memcpy (&level, data + 4, 4);
  176. memcpy (&width, data + 8, 4);
  177. e->level = level;
  178. e->width = width;
  179. *context = data[3];
  180. return true;
  181. }
  182. static bool state_save (const clap_plugin_t* p, const clap_ostream_t* s) { say ("save context=0\n"); return write_state (self_of (p), s, 0); }
  183. static bool state_load (const clap_plugin_t* p, const clap_istream_t* s)
  184. {
  185. uint32_t saved = 0;
  186. say ("load context=0\n");
  187. return read_state (self_of (p), s, &saved);
  188. }
  189. static const clap_plugin_state_t state = { state_save, state_load };
  190. static bool context_save (const clap_plugin_t* p, const clap_ostream_t* s, uint32_t context)
  191. {
  192. char line[48];
  193. snprintf (line, sizeof (line), "save context=%u\n", (unsigned) context);
  194. say (line);
  195. return write_state (self_of (p), s, context);
  196. }
  197. static bool context_load (const clap_plugin_t* p, const clap_istream_t* s, uint32_t context)
  198. {
  199. uint32_t saved = 0;
  200. char line[64];
  201. const bool ok = read_state (self_of (p), s, &saved);
  202. snprintf (line, sizeof (line), "load context=%u saved_with=%u\n", (unsigned) context, (unsigned) saved);
  203. say (line);
  204. return ok;
  205. }
  206. static const clap_plugin_state_context_t state_context = { context_save, context_load };
  207. /* latency 0, tail, render, voice info */
  208. static uint32_t latency_get (const clap_plugin_t* p) { (void) p; return 0; }
  209. static const clap_plugin_latency_t latency = { latency_get };
  210. static uint32_t tail_get (const clap_plugin_t* p) { (void) p; return 4800; }
  211. static const clap_plugin_tail_t tail = { tail_get };
  212. static bool render_hard (const clap_plugin_t* p) { (void) p; return false; }
  213. static bool render_set (const clap_plugin_t* p, clap_plugin_render_mode mode)
  214. {
  215. (void) p;
  216. say (mode == CLAP_RENDER_OFFLINE ? "render offline\n" : "render realtime\n");
  217. return true;
  218. }
  219. static const clap_plugin_render_t render = { render_hard, render_set };
  220. static bool voice_get (const clap_plugin_t* p, clap_voice_info_t* info)
  221. {
  222. (void) p;
  223. info->voice_count = 3;
  224. info->voice_capacity = 16;
  225. info->flags = CLAP_VOICE_INFO_SUPPORTS_OVERLAPPING_NOTES;
  226. return true;
  227. }
  228. static const clap_plugin_voice_info_t voice_info = { voice_get };
  229. /* thread pool */
  230. static void pool_exec (const clap_plugin_t* p, uint32_t task)
  231. {
  232. Ext* e = self_of (p);
  233. atomic_fetch_add (&e->tasks, 1);
  234. if (task < 8) atomic_store (&e->threadIds[task], thread_id());
  235. volatile double x = 0.0; /* a little work so the tasks overlap */
  236. for (int i = 0; i < 20000; ++i) x += (double) i * 1e-9;
  237. }
  238. static const clap_plugin_thread_pool_t thread_pool = { pool_exec };
  239. /* remote controls */
  240. static uint32_t remote_count (const clap_plugin_t* p) { (void) p; return 1; }
  241. static bool remote_get (const clap_plugin_t* p, uint32_t index, clap_remote_controls_page_t* page)
  242. {
  243. (void) p;
  244. if (index != 0) return false;
  245. memset (page, 0, sizeof (*page));
  246. strcpy (page->section_name, "Test");
  247. strcpy (page->page_name, "Main");
  248. page->page_id = 1;
  249. for (int k = 0; k < CLAP_REMOTE_CONTROLS_COUNT; ++k) page->param_ids[k] = CLAP_INVALID_ID;
  250. page->param_ids[0] = WIDTH_ID; /* (the page's own order: Width first, then Level) */
  251. page->param_ids[1] = LEVEL_ID;
  252. return true;
  253. }
  254. static const clap_plugin_remote_controls_t remote_controls = { remote_count, remote_get };
  255. /* preset load */
  256. static bool preset_load (const clap_plugin_t* p, uint32_t kind, const char* location, const char* key)
  257. {
  258. Ext* e = self_of (p);
  259. (void) location;
  260. if (kind != CLAP_PRESET_DISCOVERY_LOCATION_PLUGIN || key == NULL) return false;
  261. double v = strcmp (key, "low") == 0 ? 0.25 : strcmp (key, "mid") == 0 ? 0.5 : strcmp (key, "high") == 0 ? 0.75 : -1.0;
  262. if (v < 0.0) return false;
  263. e->level = v;
  264. char line[64];
  265. snprintf (line, sizeof (line), "preset %s level=%.2f\n", key, v);
  266. say (line);
  267. const clap_host_preset_load_t* host = (const clap_host_preset_load_t*) e->host->get_extension (e->host, CLAP_EXT_PRESET_LOAD);
  268. if (host != NULL) host->loaded (e->host, kind, location, key);
  269. return true;
  270. }
  271. static const clap_plugin_preset_load_t preset_load_ext = { preset_load };
  272. /* plugin */
  273. static bool p_init (const clap_plugin_t* p) { (void) p; return true; }
  274. static void p_destroy (const clap_plugin_t* p) { free (self_of (p)); }
  275. static bool p_activate (const clap_plugin_t* p, double r, uint32_t a, uint32_t b) { (void) p; (void) r; (void) a; (void) b; return true; }
  276. static void p_deactivate (const clap_plugin_t* p) { (void) p; }
  277. static bool p_start (const clap_plugin_t* p) { (void) p; return true; }
  278. static void p_stop (const clap_plugin_t* p) { (void) p; }
  279. static void p_reset (const clap_plugin_t* p) { (void) p; }
  280. static clap_process_status p_process (const clap_plugin_t* p, const clap_process_t* proc)
  281. {
  282. Ext* e = self_of (p);
  283. const uint32_t n = proc->frames_count;
  284. const uint32_t events = proc->in_events->size (proc->in_events);
  285. uint32_t next = 0;
  286. float* l = proc->audio_outputs_count > 0 && proc->audio_outputs[0].channel_count > 0 ? proc->audio_outputs[0].data32[0] : NULL;
  287. float* r = proc->audio_outputs_count > 0 && proc->audio_outputs[0].channel_count > 1 ? proc->audio_outputs[0].data32[1] : NULL;
  288. for (uint32_t i = 0; i < n; ++i)
  289. {
  290. while (next < events)
  291. {
  292. const clap_event_header_t* h = proc->in_events->get (proc->in_events, next);
  293. if (h->time > i) break;
  294. apply_event (e, h, n);
  295. ++next;
  296. }
  297. if (l != NULL) l[i] = (float) e->level;
  298. if (r != NULL) r[i] = (float) e->level;
  299. }
  300. for (; next < events; ++next)
  301. apply_event (e, proc->in_events->get (proc->in_events, next), n);
  302. if (e->calls == 4)
  303. {
  304. /* round 190rk: what the host passed (one buffer per port; a transport) */
  305. char line[160];
  306. int writable = proc->audio_outputs_count > 1 && proc->audio_outputs[1].channel_count > 0 && proc->audio_outputs[1].data32 != NULL
  307. && proc->audio_outputs[1].data32[0] != NULL;
  308. if (writable) proc->audio_outputs[1].data32[0][0] = 0.0f;
  309. snprintf (line, sizeof (line), "ports in=%u out=%u side_channels=%u aux_writable=%d transport=%d\n", proc->audio_inputs_count,
  310. proc->audio_outputs_count, proc->audio_inputs_count > 1 ? proc->audio_inputs[1].channel_count : 0u, writable,
  311. proc->transport != NULL ? 1 : 0);
  312. say (line);
  313. }
  314. if (++e->calls == 10)
  315. {
  316. const clap_host_thread_pool_t* pool = (const clap_host_thread_pool_t*) e->host->get_extension (e->host, CLAP_EXT_THREAD_POOL);
  317. atomic_store (&e->tasks, 0);
  318. for (int k = 0; k < 8; ++k) atomic_store (&e->threadIds[k], 0ULL);
  319. const bool ok = pool != NULL && pool->request_exec (e->host, 8);
  320. int threads = 0;
  321. for (int a = 0; a < 8; ++a)
  322. {
  323. const unsigned long long id = atomic_load (&e->threadIds[a]);
  324. int seen = 0;
  325. for (int b = 0; b < a; ++b) seen = seen || atomic_load (&e->threadIds[b]) == id;
  326. if (id != 0 && ! seen) ++threads;
  327. }
  328. char line[96];
  329. snprintf (line, sizeof (line), "pool ok=%d tasks=%d threads=%d\n", ok ? 1 : 0, atomic_load (&e->tasks), threads);
  330. say (line);
  331. }
  332. return CLAP_PROCESS_CONTINUE;
  333. }
  334. static const void* p_extension (const clap_plugin_t* p, const char* id)
  335. {
  336. (void) p;
  337. if (strcmp (id, CLAP_EXT_AUDIO_PORTS) == 0) return &audio_ports;
  338. if (strcmp (id, CLAP_EXT_PARAMS) == 0) return &params;
  339. if (strcmp (id, CLAP_EXT_STATE) == 0) return &state;
  340. if (strcmp (id, CLAP_EXT_STATE_CONTEXT) == 0) return &state_context;
  341. if (strcmp (id, CLAP_EXT_LATENCY) == 0) return &latency;
  342. if (strcmp (id, CLAP_EXT_TAIL) == 0) return &tail;
  343. if (strcmp (id, CLAP_EXT_RENDER) == 0) return &render;
  344. if (strcmp (id, CLAP_EXT_VOICE_INFO) == 0) return &voice_info;
  345. if (strcmp (id, CLAP_EXT_THREAD_POOL) == 0) return &thread_pool;
  346. if (strcmp (id, CLAP_EXT_REMOTE_CONTROLS) == 0) return &remote_controls;
  347. if (strcmp (id, CLAP_EXT_PRESET_LOAD) == 0) return &preset_load_ext;
  348. return NULL;
  349. }
  350. static void p_main_thread (const clap_plugin_t* p) { (void) p; }
  351. static uint32_t f_count (const clap_plugin_factory_t* f) { (void) f; return 1; }
  352. static const clap_plugin_descriptor_t* f_descriptor (const clap_plugin_factory_t* f, uint32_t i) { (void) f; return i == 0 ? &descriptor : NULL; }
  353. static const clap_plugin_t* f_create (const clap_plugin_factory_t* f, const clap_host_t* host, const char* id)
  354. {
  355. (void) f;
  356. if (strcmp (id, descriptor.id) != 0) return NULL;
  357. Ext* e = (Ext*) calloc (1, sizeof (Ext));
  358. e->host = host;
  359. e->level = 0.5;
  360. e->width = 0.5;
  361. clap_plugin_t* p = &e->plugin;
  362. p->desc = &descriptor;
  363. p->plugin_data = e;
  364. p->init = p_init; p->destroy = p_destroy; p->activate = p_activate; p->deactivate = p_deactivate;
  365. p->start_processing = p_start; p->stop_processing = p_stop; p->reset = p_reset; p->process = p_process;
  366. p->get_extension = p_extension; p->on_main_thread = p_main_thread;
  367. return p;
  368. }
  369. static const clap_plugin_factory_t factory = { f_count, f_descriptor, f_create };
  370. /* preset discovery: one provider with three presets inside the plugin */
  371. static const clap_preset_discovery_provider_descriptor_t provider_descriptor = { CLAP_VERSION_INIT, "org.lynx.test-ext.presets", "LYNX Test Ext presets", "LYNX" };
  372. static bool prov_init (const clap_preset_discovery_provider_t* p)
  373. {
  374. const clap_preset_discovery_indexer_t* indexer = (const clap_preset_discovery_indexer_t*) p->provider_data;
  375. const clap_preset_discovery_location_t location = { CLAP_PRESET_DISCOVERY_IS_FACTORY_CONTENT, "Factory", CLAP_PRESET_DISCOVERY_LOCATION_PLUGIN, NULL };
  376. return indexer->declare_location (indexer, &location);
  377. }
  378. static void prov_destroy (const clap_preset_discovery_provider_t* p) { free ((void*) p); }
  379. static bool prov_metadata (const clap_preset_discovery_provider_t* p, uint32_t kind, const char* location, const clap_preset_discovery_metadata_receiver_t* r)
  380. {
  381. (void) p; (void) location;
  382. if (kind != CLAP_PRESET_DISCOVERY_LOCATION_PLUGIN) return false;
  383. static const char* const names[3] = { "Low", "Mid", "High" };
  384. static const char* const keys[3] = { "low", "mid", "high" };
  385. const clap_universal_plugin_id_t id = { "clap", "org.lynx.test-ext" };
  386. for (int i = 0; i < 3; ++i)
  387. if (r->begin_preset (r, names[i], keys[i]))
  388. r->add_plugin_id (r, &id);
  389. return true;
  390. }
  391. static const void* prov_extension (const clap_preset_discovery_provider_t* p, const char* id) { (void) p; (void) id; return NULL; }
  392. static uint32_t d_count (const clap_preset_discovery_factory_t* f) { (void) f; return 1; }
  393. static const clap_preset_discovery_provider_descriptor_t* d_descriptor (const clap_preset_discovery_factory_t* f, uint32_t i)
  394. {
  395. (void) f;
  396. return i == 0 ? &provider_descriptor : NULL;
  397. }
  398. static const clap_preset_discovery_provider_t* d_create (const clap_preset_discovery_factory_t* f, const clap_preset_discovery_indexer_t* indexer, const char* id)
  399. {
  400. (void) f;
  401. if (strcmp (id, provider_descriptor.id) != 0) return NULL;
  402. clap_preset_discovery_provider_t* p = (clap_preset_discovery_provider_t*) calloc (1, sizeof (*p));
  403. p->desc = &provider_descriptor;
  404. p->provider_data = (void*) indexer;
  405. p->init = prov_init;
  406. p->destroy = prov_destroy;
  407. p->get_metadata = prov_metadata;
  408. p->get_extension = prov_extension;
  409. return p;
  410. }
  411. static const clap_preset_discovery_factory_t discovery = { d_count, d_descriptor, d_create };
  412. static bool e_init (const char* path) { (void) path; return true; }
  413. static void e_deinit (void) {}
  414. static const void* e_factory (const char* id)
  415. {
  416. if (strcmp (id, CLAP_PLUGIN_FACTORY_ID) == 0) return &factory;
  417. if (strcmp (id, CLAP_PRESET_DISCOVERY_FACTORY_ID) == 0) return &discovery;
  418. return NULL;
  419. }
  420. CLAP_EXPORT const clap_plugin_entry_t clap_entry = { CLAP_VERSION_INIT, e_init, e_deinit, e_factory };