4
0

lynx_test_ext.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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. static uint32_t ports_count (const clap_plugin_t* p, bool in) { (void) p; (void) in; return 1; }
  84. static bool ports_get (const clap_plugin_t* p, uint32_t index, bool in, clap_audio_port_info_t* info)
  85. {
  86. (void) p;
  87. if (index != 0) return false;
  88. memset (info, 0, sizeof (*info));
  89. info->id = in ? 0 : 1;
  90. strcpy (info->name, in ? "In" : "Out");
  91. info->flags = CLAP_AUDIO_PORT_IS_MAIN;
  92. info->channel_count = 2;
  93. info->port_type = CLAP_PORT_STEREO;
  94. info->in_place_pair = CLAP_INVALID_ID;
  95. return true;
  96. }
  97. static const clap_plugin_audio_ports_t audio_ports = { ports_count, ports_get };
  98. /* params */
  99. static uint32_t params_count (const clap_plugin_t* p) { (void) p; return 2; }
  100. static bool params_info (const clap_plugin_t* p, uint32_t index, clap_param_info_t* info)
  101. {
  102. (void) p;
  103. if (index > 1) return false;
  104. memset (info, 0, sizeof (*info));
  105. info->id = index == 0 ? LEVEL_ID : WIDTH_ID;
  106. info->flags = CLAP_PARAM_IS_AUTOMATABLE;
  107. strcpy (info->name, index == 0 ? "Level" : "Width");
  108. info->min_value = 0.0;
  109. info->max_value = 1.0;
  110. info->default_value = 0.5;
  111. return true;
  112. }
  113. static bool params_value (const clap_plugin_t* p, clap_id id, double* value)
  114. {
  115. Ext* e = self_of (p);
  116. if (id == LEVEL_ID) { *value = e->level; return true; }
  117. if (id == WIDTH_ID) { *value = e->width; return true; }
  118. return false;
  119. }
  120. static bool params_to_text (const clap_plugin_t* p, clap_id id, double v, char* out, uint32_t size)
  121. {
  122. (void) p; (void) id;
  123. snprintf (out, size, "%.3f", v);
  124. return true;
  125. }
  126. static bool params_from_text (const clap_plugin_t* p, clap_id id, const char* text, double* v)
  127. {
  128. (void) p; (void) id;
  129. *v = atof (text);
  130. return true;
  131. }
  132. static void apply_event (Ext* e, const clap_event_header_t* h, uint32_t frames)
  133. {
  134. if (h->space_id != CLAP_CORE_EVENT_SPACE_ID || h->type != CLAP_EVENT_PARAM_VALUE) return;
  135. const clap_event_param_value_t* v = (const clap_event_param_value_t*) h;
  136. if (v->param_id == LEVEL_ID)
  137. {
  138. e->level = v->value;
  139. if (e->logged < 400)
  140. {
  141. char line[96];
  142. snprintf (line, sizeof (line), "P %u %.6f n=%u\n", (unsigned) h->time, v->value, (unsigned) frames);
  143. say (line);
  144. ++e->logged;
  145. }
  146. }
  147. else if (v->param_id == WIDTH_ID)
  148. e->width = v->value;
  149. }
  150. static void params_flush (const clap_plugin_t* p, const clap_input_events_t* in, const clap_output_events_t* out)
  151. {
  152. (void) out;
  153. for (uint32_t i = 0; i < in->size (in); ++i)
  154. apply_event (self_of (p), in->get (in, i), 0);
  155. }
  156. static const clap_plugin_params_t params = { params_count, params_info, params_value, params_to_text, params_from_text, params_flush };
  157. /* state + state-context */
  158. static bool write_state (Ext* e, const clap_ostream_t* s, uint32_t context)
  159. {
  160. unsigned char data[12];
  161. float level = (float) e->level, width = (float) e->width;
  162. memcpy (data, "LXE", 3);
  163. data[3] = (unsigned char) context;
  164. memcpy (data + 4, &level, 4);
  165. memcpy (data + 8, &width, 4);
  166. return s->write (s, data, sizeof (data)) == (int64_t) sizeof (data);
  167. }
  168. static bool read_state (Ext* e, const clap_istream_t* s, uint32_t* context)
  169. {
  170. unsigned char data[12];
  171. if (s->read (s, data, sizeof (data)) != (int64_t) sizeof (data) || memcmp (data, "LXE", 3) != 0) return false;
  172. float level, width;
  173. memcpy (&level, data + 4, 4);
  174. memcpy (&width, data + 8, 4);
  175. e->level = level;
  176. e->width = width;
  177. *context = data[3];
  178. return true;
  179. }
  180. 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); }
  181. static bool state_load (const clap_plugin_t* p, const clap_istream_t* s)
  182. {
  183. uint32_t saved = 0;
  184. say ("load context=0\n");
  185. return read_state (self_of (p), s, &saved);
  186. }
  187. static const clap_plugin_state_t state = { state_save, state_load };
  188. static bool context_save (const clap_plugin_t* p, const clap_ostream_t* s, uint32_t context)
  189. {
  190. char line[48];
  191. snprintf (line, sizeof (line), "save context=%u\n", (unsigned) context);
  192. say (line);
  193. return write_state (self_of (p), s, context);
  194. }
  195. static bool context_load (const clap_plugin_t* p, const clap_istream_t* s, uint32_t context)
  196. {
  197. uint32_t saved = 0;
  198. char line[64];
  199. const bool ok = read_state (self_of (p), s, &saved);
  200. snprintf (line, sizeof (line), "load context=%u saved_with=%u\n", (unsigned) context, (unsigned) saved);
  201. say (line);
  202. return ok;
  203. }
  204. static const clap_plugin_state_context_t state_context = { context_save, context_load };
  205. /* latency 0, tail, render, voice info */
  206. static uint32_t latency_get (const clap_plugin_t* p) { (void) p; return 0; }
  207. static const clap_plugin_latency_t latency = { latency_get };
  208. static uint32_t tail_get (const clap_plugin_t* p) { (void) p; return 4800; }
  209. static const clap_plugin_tail_t tail = { tail_get };
  210. static bool render_hard (const clap_plugin_t* p) { (void) p; return false; }
  211. static bool render_set (const clap_plugin_t* p, clap_plugin_render_mode mode)
  212. {
  213. (void) p;
  214. say (mode == CLAP_RENDER_OFFLINE ? "render offline\n" : "render realtime\n");
  215. return true;
  216. }
  217. static const clap_plugin_render_t render = { render_hard, render_set };
  218. static bool voice_get (const clap_plugin_t* p, clap_voice_info_t* info)
  219. {
  220. (void) p;
  221. info->voice_count = 3;
  222. info->voice_capacity = 16;
  223. info->flags = CLAP_VOICE_INFO_SUPPORTS_OVERLAPPING_NOTES;
  224. return true;
  225. }
  226. static const clap_plugin_voice_info_t voice_info = { voice_get };
  227. /* thread pool */
  228. static void pool_exec (const clap_plugin_t* p, uint32_t task)
  229. {
  230. Ext* e = self_of (p);
  231. atomic_fetch_add (&e->tasks, 1);
  232. if (task < 8) atomic_store (&e->threadIds[task], thread_id());
  233. volatile double x = 0.0; /* a little work so the tasks overlap */
  234. for (int i = 0; i < 20000; ++i) x += (double) i * 1e-9;
  235. }
  236. static const clap_plugin_thread_pool_t thread_pool = { pool_exec };
  237. /* remote controls */
  238. static uint32_t remote_count (const clap_plugin_t* p) { (void) p; return 1; }
  239. static bool remote_get (const clap_plugin_t* p, uint32_t index, clap_remote_controls_page_t* page)
  240. {
  241. (void) p;
  242. if (index != 0) return false;
  243. memset (page, 0, sizeof (*page));
  244. strcpy (page->section_name, "Test");
  245. strcpy (page->page_name, "Main");
  246. page->page_id = 1;
  247. for (int k = 0; k < CLAP_REMOTE_CONTROLS_COUNT; ++k) page->param_ids[k] = CLAP_INVALID_ID;
  248. page->param_ids[0] = WIDTH_ID; /* (the page's own order: Width first, then Level) */
  249. page->param_ids[1] = LEVEL_ID;
  250. return true;
  251. }
  252. static const clap_plugin_remote_controls_t remote_controls = { remote_count, remote_get };
  253. /* preset load */
  254. static bool preset_load (const clap_plugin_t* p, uint32_t kind, const char* location, const char* key)
  255. {
  256. Ext* e = self_of (p);
  257. (void) location;
  258. if (kind != CLAP_PRESET_DISCOVERY_LOCATION_PLUGIN || key == NULL) return false;
  259. double v = strcmp (key, "low") == 0 ? 0.25 : strcmp (key, "mid") == 0 ? 0.5 : strcmp (key, "high") == 0 ? 0.75 : -1.0;
  260. if (v < 0.0) return false;
  261. e->level = v;
  262. char line[64];
  263. snprintf (line, sizeof (line), "preset %s level=%.2f\n", key, v);
  264. say (line);
  265. const clap_host_preset_load_t* host = (const clap_host_preset_load_t*) e->host->get_extension (e->host, CLAP_EXT_PRESET_LOAD);
  266. if (host != NULL) host->loaded (e->host, kind, location, key);
  267. return true;
  268. }
  269. static const clap_plugin_preset_load_t preset_load_ext = { preset_load };
  270. /* plugin */
  271. static bool p_init (const clap_plugin_t* p) { (void) p; return true; }
  272. static void p_destroy (const clap_plugin_t* p) { free (self_of (p)); }
  273. 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; }
  274. static void p_deactivate (const clap_plugin_t* p) { (void) p; }
  275. static bool p_start (const clap_plugin_t* p) { (void) p; return true; }
  276. static void p_stop (const clap_plugin_t* p) { (void) p; }
  277. static void p_reset (const clap_plugin_t* p) { (void) p; }
  278. static clap_process_status p_process (const clap_plugin_t* p, const clap_process_t* proc)
  279. {
  280. Ext* e = self_of (p);
  281. const uint32_t n = proc->frames_count;
  282. const uint32_t events = proc->in_events->size (proc->in_events);
  283. uint32_t next = 0;
  284. float* l = proc->audio_outputs_count > 0 && proc->audio_outputs[0].channel_count > 0 ? proc->audio_outputs[0].data32[0] : NULL;
  285. float* r = proc->audio_outputs_count > 0 && proc->audio_outputs[0].channel_count > 1 ? proc->audio_outputs[0].data32[1] : NULL;
  286. for (uint32_t i = 0; i < n; ++i)
  287. {
  288. while (next < events)
  289. {
  290. const clap_event_header_t* h = proc->in_events->get (proc->in_events, next);
  291. if (h->time > i) break;
  292. apply_event (e, h, n);
  293. ++next;
  294. }
  295. if (l != NULL) l[i] = (float) e->level;
  296. if (r != NULL) r[i] = (float) e->level;
  297. }
  298. for (; next < events; ++next)
  299. apply_event (e, proc->in_events->get (proc->in_events, next), n);
  300. if (++e->calls == 10)
  301. {
  302. const clap_host_thread_pool_t* pool = (const clap_host_thread_pool_t*) e->host->get_extension (e->host, CLAP_EXT_THREAD_POOL);
  303. atomic_store (&e->tasks, 0);
  304. for (int k = 0; k < 8; ++k) atomic_store (&e->threadIds[k], 0ULL);
  305. const bool ok = pool != NULL && pool->request_exec (e->host, 8);
  306. int threads = 0;
  307. for (int a = 0; a < 8; ++a)
  308. {
  309. const unsigned long long id = atomic_load (&e->threadIds[a]);
  310. int seen = 0;
  311. for (int b = 0; b < a; ++b) seen = seen || atomic_load (&e->threadIds[b]) == id;
  312. if (id != 0 && ! seen) ++threads;
  313. }
  314. char line[96];
  315. snprintf (line, sizeof (line), "pool ok=%d tasks=%d threads=%d\n", ok ? 1 : 0, atomic_load (&e->tasks), threads);
  316. say (line);
  317. }
  318. return CLAP_PROCESS_CONTINUE;
  319. }
  320. static const void* p_extension (const clap_plugin_t* p, const char* id)
  321. {
  322. (void) p;
  323. if (strcmp (id, CLAP_EXT_AUDIO_PORTS) == 0) return &audio_ports;
  324. if (strcmp (id, CLAP_EXT_PARAMS) == 0) return &params;
  325. if (strcmp (id, CLAP_EXT_STATE) == 0) return &state;
  326. if (strcmp (id, CLAP_EXT_STATE_CONTEXT) == 0) return &state_context;
  327. if (strcmp (id, CLAP_EXT_LATENCY) == 0) return &latency;
  328. if (strcmp (id, CLAP_EXT_TAIL) == 0) return &tail;
  329. if (strcmp (id, CLAP_EXT_RENDER) == 0) return &render;
  330. if (strcmp (id, CLAP_EXT_VOICE_INFO) == 0) return &voice_info;
  331. if (strcmp (id, CLAP_EXT_THREAD_POOL) == 0) return &thread_pool;
  332. if (strcmp (id, CLAP_EXT_REMOTE_CONTROLS) == 0) return &remote_controls;
  333. if (strcmp (id, CLAP_EXT_PRESET_LOAD) == 0) return &preset_load_ext;
  334. return NULL;
  335. }
  336. static void p_main_thread (const clap_plugin_t* p) { (void) p; }
  337. static uint32_t f_count (const clap_plugin_factory_t* f) { (void) f; return 1; }
  338. static const clap_plugin_descriptor_t* f_descriptor (const clap_plugin_factory_t* f, uint32_t i) { (void) f; return i == 0 ? &descriptor : NULL; }
  339. static const clap_plugin_t* f_create (const clap_plugin_factory_t* f, const clap_host_t* host, const char* id)
  340. {
  341. (void) f;
  342. if (strcmp (id, descriptor.id) != 0) return NULL;
  343. Ext* e = (Ext*) calloc (1, sizeof (Ext));
  344. e->host = host;
  345. e->level = 0.5;
  346. e->width = 0.5;
  347. clap_plugin_t* p = &e->plugin;
  348. p->desc = &descriptor;
  349. p->plugin_data = e;
  350. p->init = p_init; p->destroy = p_destroy; p->activate = p_activate; p->deactivate = p_deactivate;
  351. p->start_processing = p_start; p->stop_processing = p_stop; p->reset = p_reset; p->process = p_process;
  352. p->get_extension = p_extension; p->on_main_thread = p_main_thread;
  353. return p;
  354. }
  355. static const clap_plugin_factory_t factory = { f_count, f_descriptor, f_create };
  356. /* preset discovery: one provider with three presets inside the plugin */
  357. static const clap_preset_discovery_provider_descriptor_t provider_descriptor = { CLAP_VERSION_INIT, "org.lynx.test-ext.presets", "LYNX Test Ext presets", "LYNX" };
  358. static bool prov_init (const clap_preset_discovery_provider_t* p)
  359. {
  360. const clap_preset_discovery_indexer_t* indexer = (const clap_preset_discovery_indexer_t*) p->provider_data;
  361. const clap_preset_discovery_location_t location = { CLAP_PRESET_DISCOVERY_IS_FACTORY_CONTENT, "Factory", CLAP_PRESET_DISCOVERY_LOCATION_PLUGIN, NULL };
  362. return indexer->declare_location (indexer, &location);
  363. }
  364. static void prov_destroy (const clap_preset_discovery_provider_t* p) { free ((void*) p); }
  365. 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)
  366. {
  367. (void) p; (void) location;
  368. if (kind != CLAP_PRESET_DISCOVERY_LOCATION_PLUGIN) return false;
  369. static const char* const names[3] = { "Low", "Mid", "High" };
  370. static const char* const keys[3] = { "low", "mid", "high" };
  371. const clap_universal_plugin_id_t id = { "clap", "org.lynx.test-ext" };
  372. for (int i = 0; i < 3; ++i)
  373. if (r->begin_preset (r, names[i], keys[i]))
  374. r->add_plugin_id (r, &id);
  375. return true;
  376. }
  377. static const void* prov_extension (const clap_preset_discovery_provider_t* p, const char* id) { (void) p; (void) id; return NULL; }
  378. static uint32_t d_count (const clap_preset_discovery_factory_t* f) { (void) f; return 1; }
  379. static const clap_preset_discovery_provider_descriptor_t* d_descriptor (const clap_preset_discovery_factory_t* f, uint32_t i)
  380. {
  381. (void) f;
  382. return i == 0 ? &provider_descriptor : NULL;
  383. }
  384. 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)
  385. {
  386. (void) f;
  387. if (strcmp (id, provider_descriptor.id) != 0) return NULL;
  388. clap_preset_discovery_provider_t* p = (clap_preset_discovery_provider_t*) calloc (1, sizeof (*p));
  389. p->desc = &provider_descriptor;
  390. p->provider_data = (void*) indexer;
  391. p->init = prov_init;
  392. p->destroy = prov_destroy;
  393. p->get_metadata = prov_metadata;
  394. p->get_extension = prov_extension;
  395. return p;
  396. }
  397. static const clap_preset_discovery_factory_t discovery = { d_count, d_descriptor, d_create };
  398. static bool e_init (const char* path) { (void) path; return true; }
  399. static void e_deinit (void) {}
  400. static const void* e_factory (const char* id)
  401. {
  402. if (strcmp (id, CLAP_PLUGIN_FACTORY_ID) == 0) return &factory;
  403. if (strcmp (id, CLAP_PRESET_DISCOVERY_FACTORY_ID) == 0) return &discovery;
  404. return NULL;
  405. }
  406. CLAP_EXPORT const clap_plugin_entry_t clap_entry = { CLAP_VERSION_INIT, e_init, e_deinit, e_factory };