lynx_test_mpe.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /* A minimal CLAP instrument that writes down every note, note expression and MIDI event the host sends it (issue #108,
  2. * MPE stage 1): LYNX's wrapper "MPE" switch must reach a CLAP plugin that takes CLAP notes as one channel per note
  3. * with tuning / brightness / pressure note expressions. Silent; the log goes to the file named by the environment
  4. * variable LYNX_TEST_MPE_REPORT when the plugin is destroyed.
  5. *
  6. * zig cc -shared -O2 -target x86_64-windows-gnu -Ithird_party/clap/include tools/testplugin/lynx_test_mpe.c \
  7. * -o "LYNX Test MPE.clap"
  8. * LYNX.exe --mpetest "LYNX Test MPE.clap" report.txt
  9. *
  10. * Lines: "on <channel> <key> <velocity*1000>", "off <channel> <key>", "expr <expression id> <channel> <key>
  11. * <value*1000>", "midi <status> <data1> <data2>" (channels 0-based, as CLAP's).
  12. * Round 190ks (GitHub #198): with LYNX_TEST_PADS_AUTO = "call:key:velocity:length,..." it also has a note OUTPUT port and
  13. * sends those hits (CLAP notes) at its Nth process() call with frames, as if pads were clicked in its window; with
  14. * LYNX_TEST_PADS_ECHO = 1 every note on / off it is sent goes back out (the same variables as lynx_test_pads_vst3.cpp:
  15. * LYNX --padrectest "LYNX Test MPE.clap" report.txt). Without them nothing changes. */
  16. #include <clap/clap.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. static const char* const features[] = { CLAP_PLUGIN_FEATURE_INSTRUMENT, CLAP_PLUGIN_FEATURE_SYNTHESIZER, NULL };
  21. static const clap_plugin_descriptor_t descriptor = {
  22. CLAP_VERSION_INIT, "org.lynx.test-mpe", "LYNX Test MPE", "LYNX", "https://github.com/Ka1-S/LYNX-SHARE", "", "",
  23. "1.0.0", "Logs the notes, note expressions and MIDI it receives (MPE host test)", features
  24. };
  25. enum { logSize = 1 << 16 };
  26. typedef struct
  27. {
  28. int call, key, velocity, length;
  29. } AutoHit;
  30. typedef struct
  31. {
  32. clap_plugin_t plugin;
  33. char* log;
  34. int used;
  35. AutoHit hits[32];
  36. int hitCount, echo;
  37. long long calls;
  38. } Logger;
  39. static int sends (void)
  40. {
  41. const char* a = getenv ("LYNX_TEST_PADS_AUTO");
  42. const char* e = getenv ("LYNX_TEST_PADS_ECHO");
  43. return (a != NULL && *a != 0) || (e != NULL && atoi (e) != 0);
  44. }
  45. static Logger* self (const clap_plugin_t* p) { return (Logger*) p->plugin_data; }
  46. static void put (Logger* l, const char* line)
  47. {
  48. const int n = (int) strlen (line);
  49. if (l->log != NULL && l->used + n < logSize)
  50. {
  51. memcpy (l->log + l->used, line, (size_t) n);
  52. l->used += n;
  53. }
  54. }
  55. static uint32_t audio_count (const clap_plugin_t* p, bool in) { (void) p; return in ? 0 : 1; }
  56. static bool audio_get (const clap_plugin_t* p, uint32_t index, bool in, clap_audio_port_info_t* info)
  57. {
  58. (void) p;
  59. if (in || index != 0) return false;
  60. memset (info, 0, sizeof (*info));
  61. info->id = 1;
  62. strcpy (info->name, "Out");
  63. info->flags = CLAP_AUDIO_PORT_IS_MAIN;
  64. info->channel_count = 2;
  65. info->port_type = CLAP_PORT_STEREO;
  66. info->in_place_pair = CLAP_INVALID_ID;
  67. return true;
  68. }
  69. static const clap_plugin_audio_ports_t audio_ports = { audio_count, audio_get };
  70. static uint32_t note_count (const clap_plugin_t* p, bool in) { (void) p; return in ? 1 : (sends() ? 1 : 0); }
  71. static bool note_get (const clap_plugin_t* p, uint32_t index, bool in, clap_note_port_info_t* info)
  72. {
  73. (void) p;
  74. if (index != 0 || (! in && ! sends())) return false;
  75. memset (info, 0, sizeof (*info));
  76. info->id = in ? 2 : 3;
  77. info->supported_dialects = CLAP_NOTE_DIALECT_CLAP | CLAP_NOTE_DIALECT_MIDI;
  78. info->preferred_dialect = CLAP_NOTE_DIALECT_CLAP;
  79. strcpy (info->name, "Notes");
  80. return true;
  81. }
  82. static const clap_plugin_note_ports_t note_ports = { note_count, note_get };
  83. static bool p_init (const clap_plugin_t* p)
  84. {
  85. Logger* l = self (p);
  86. l->log = (char*) malloc (logSize); /* (before any processing: the audio thread only copies into it) */
  87. l->used = 0;
  88. {
  89. const char* a = getenv ("LYNX_TEST_PADS_AUTO");
  90. const char* e = getenv ("LYNX_TEST_PADS_ECHO");
  91. l->echo = e != NULL && atoi (e) != 0;
  92. while (a != NULL && *a != 0 && l->hitCount < 32)
  93. {
  94. AutoHit h;
  95. int used = 0;
  96. if (sscanf (a, "%d:%d:%d:%d%n", &h.call, &h.key, &h.velocity, &h.length, &used) != 4)
  97. break;
  98. if (h.length < 1) h.length = 1;
  99. l->hits[l->hitCount++] = h;
  100. a += used;
  101. while (*a == ',' || *a == ' ') ++a;
  102. }
  103. }
  104. return l->log != NULL;
  105. }
  106. static void p_destroy (const clap_plugin_t* p)
  107. {
  108. Logger* l = self (p);
  109. const char* path = getenv ("LYNX_TEST_MPE_REPORT");
  110. if (path != NULL && l->log != NULL)
  111. {
  112. FILE* f = fopen (path, "wb");
  113. if (f != NULL)
  114. {
  115. fwrite (l->log, 1, (size_t) l->used, f);
  116. fclose (f);
  117. }
  118. }
  119. free (l->log);
  120. free (l);
  121. }
  122. static bool p_activate (const clap_plugin_t* p, double rate, uint32_t minFrames, uint32_t maxFrames)
  123. {
  124. (void) p; (void) rate; (void) minFrames; (void) maxFrames;
  125. return true;
  126. }
  127. static void p_deactivate (const clap_plugin_t* p) { (void) p; }
  128. static bool p_start (const clap_plugin_t* p) { (void) p; return true; }
  129. static void p_stop (const clap_plugin_t* p) { (void) p; }
  130. static void p_reset (const clap_plugin_t* p) { (void) p; }
  131. static void sendNote (const clap_process_t* proc, bool on, int channel, int key, double velocity)
  132. {
  133. clap_event_note_t e;
  134. memset (&e, 0, sizeof (e));
  135. e.header.size = sizeof (e);
  136. e.header.time = 0;
  137. e.header.space_id = CLAP_CORE_EVENT_SPACE_ID;
  138. e.header.type = on ? CLAP_EVENT_NOTE_ON : CLAP_EVENT_NOTE_OFF;
  139. e.note_id = -1;
  140. e.port_index = 0;
  141. e.channel = (int16_t) channel;
  142. e.key = (int16_t) key;
  143. e.velocity = velocity;
  144. if (proc->out_events != NULL)
  145. proc->out_events->try_push (proc->out_events, &e.header);
  146. }
  147. static clap_process_status p_process (const clap_plugin_t* p, const clap_process_t* proc)
  148. {
  149. Logger* l = self (p);
  150. char line[96];
  151. const uint32_t n = proc->in_events != NULL ? proc->in_events->size (proc->in_events) : 0;
  152. for (uint32_t i = 0; i < n; ++i)
  153. {
  154. const clap_event_header_t* h = proc->in_events->get (proc->in_events, i);
  155. if (h == NULL || h->space_id != CLAP_CORE_EVENT_SPACE_ID)
  156. continue;
  157. if (h->type == CLAP_EVENT_NOTE_ON || h->type == CLAP_EVENT_NOTE_OFF)
  158. {
  159. const clap_event_note_t* e = (const clap_event_note_t*) h;
  160. if (h->type == CLAP_EVENT_NOTE_ON)
  161. snprintf (line, sizeof line, "on %d %d %d\n", (int) e->channel, (int) e->key, (int) (e->velocity * 1000.0 + 0.5));
  162. else
  163. snprintf (line, sizeof line, "off %d %d\n", (int) e->channel, (int) e->key);
  164. put (l, line);
  165. if (l->echo)
  166. sendNote (proc, h->type == CLAP_EVENT_NOTE_ON, e->channel < 0 ? 0 : e->channel, e->key, e->velocity);
  167. }
  168. else if (h->type == CLAP_EVENT_NOTE_EXPRESSION)
  169. {
  170. const clap_event_note_expression_t* e = (const clap_event_note_expression_t*) h;
  171. const double v = e->value * 1000.0;
  172. snprintf (line, sizeof line, "expr %d %d %d %d\n", (int) e->expression_id, (int) e->channel, (int) e->key,
  173. (int) (v >= 0.0 ? v + 0.5 : v - 0.5));
  174. put (l, line);
  175. }
  176. else if (h->type == CLAP_EVENT_MIDI)
  177. {
  178. const clap_event_midi_t* e = (const clap_event_midi_t*) h;
  179. snprintf (line, sizeof line, "midi %d %d %d\n", (int) e->data[0], (int) e->data[1], (int) e->data[2]);
  180. put (l, line);
  181. }
  182. }
  183. if (l->hitCount > 0 && proc->frames_count > 0) /* (round 190ks: the "clicked" pads) */
  184. {
  185. ++l->calls;
  186. for (int i = 0; i < l->hitCount; ++i)
  187. {
  188. if (l->hits[i].call == l->calls)
  189. sendNote (proc, true, 9, l->hits[i].key, l->hits[i].velocity / 127.0);
  190. if (l->hits[i].call + l->hits[i].length == l->calls)
  191. sendNote (proc, false, 9, l->hits[i].key, 0.0);
  192. }
  193. }
  194. if (proc->audio_outputs_count >= 1)
  195. for (uint32_t c = 0; c < proc->audio_outputs[0].channel_count; ++c)
  196. if (proc->audio_outputs[0].data32 != NULL && proc->audio_outputs[0].data32[c] != NULL)
  197. memset (proc->audio_outputs[0].data32[c], 0, sizeof (float) * proc->frames_count);
  198. return CLAP_PROCESS_CONTINUE;
  199. }
  200. static const void* p_extension (const clap_plugin_t* p, const char* id)
  201. {
  202. (void) p;
  203. if (strcmp (id, CLAP_EXT_AUDIO_PORTS) == 0) return &audio_ports;
  204. if (strcmp (id, CLAP_EXT_NOTE_PORTS) == 0) return &note_ports;
  205. return NULL;
  206. }
  207. static void p_main_thread (const clap_plugin_t* p) { (void) p; }
  208. static uint32_t f_count (const clap_plugin_factory_t* f) { (void) f; return 1; }
  209. static const clap_plugin_descriptor_t* f_descriptor (const clap_plugin_factory_t* f, uint32_t i) { (void) f; return i == 0 ? &descriptor : NULL; }
  210. static const clap_plugin_t* f_create (const clap_plugin_factory_t* f, const clap_host_t* host, const char* id)
  211. {
  212. (void) f; (void) host;
  213. if (strcmp (id, descriptor.id) != 0) return NULL;
  214. Logger* l = (Logger*) calloc (1, sizeof (Logger));
  215. clap_plugin_t* p = &l->plugin;
  216. p->desc = &descriptor;
  217. p->plugin_data = l;
  218. p->init = p_init; p->destroy = p_destroy; p->activate = p_activate; p->deactivate = p_deactivate;
  219. p->start_processing = p_start; p->stop_processing = p_stop; p->reset = p_reset; p->process = p_process;
  220. p->get_extension = p_extension; p->on_main_thread = p_main_thread;
  221. return p;
  222. }
  223. static const clap_plugin_factory_t factory = { f_count, f_descriptor, f_create };
  224. static bool e_init (const char* path) { (void) path; return true; }
  225. static void e_deinit (void) {}
  226. static const void* e_factory (const char* id) { return strcmp (id, CLAP_PLUGIN_FACTORY_ID) == 0 ? &factory : NULL; }
  227. CLAP_EXPORT const clap_plugin_entry_t clap_entry = { CLAP_VERSION_INIT, e_init, e_deinit, e_factory };