lynx_test_drums_vst2.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /* A minimal multi-output VST2 instrument (round 188d, issue #114): tests the wrapper's output mapping (FL's Processing ›
  2. * Connections) in LYNX and in FL Studio without third-party plugins. Same binary interface as lynx_test_vst2.c:
  3. * zig cc -shared -O2 -target x86_64-windows-gnu tools/testplugin/lynx_test_drums_vst2.c -o "LYNX Test Drums.dll"
  4. * zig cc -shared -O2 -fPIC tools/testplugin/lynx_test_drums_vst2.c -o lynx_test_drums.so (Linux)
  5. * 8 stereo outputs (16 channels) named Kick, Snare, Clap, Hat, Tom 1, Tom 2, Ride, Crash (effGetOutputProperties).
  6. * Key 36 + k plays a decaying sine at 110 × (k + 1) Hz on output k only, with k = (key − 36) mod 8 (C plays output 1,
  7. * C# output 2… in every octave).
  8. * No parameters, no editor, no state. Build options: -DPLUGIN_NAME="..." -DPLUGIN_ID=0x... (a second copy beside the
  9. * first), -DLAST_PAIR_INACTIVE (the Crash pins without kVstPinIsActive: which outputs a host starts active).
  10. * MIDI output (round 190ks, GitHub #198; the same variables as lynx_test_pads_vst3.cpp): with LYNX_TEST_PADS_AUTO =
  11. * "call:key:velocity:length,..." it says canDo "sendVstMidiEvent" and sends those hits (audioMasterProcessEvents) at its
  12. * Nth processReplacing call, as if its pads were clicked; with LYNX_TEST_PADS_ECHO = 1 it also sends back every note it
  13. * is sent. Without them it sends nothing, as before.
  14. */
  15. #include <math.h>
  16. #include <stdio.h>
  17. #include <stdint.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #ifdef _WIN32
  21. #define EXPORT __declspec (dllexport)
  22. #else
  23. #define EXPORT __attribute__ ((visibility ("default")))
  24. #endif
  25. typedef struct Effect Effect;
  26. typedef intptr_t (*HostCallback) (Effect*, int32_t, int32_t, intptr_t, void*, float);
  27. struct Effect
  28. {
  29. int32_t magic;
  30. intptr_t (*dispatcher) (Effect*, int32_t, int32_t, intptr_t, void*, float);
  31. void (*process) (Effect*, float**, float**, int32_t);
  32. void (*setParameter) (Effect*, int32_t, float);
  33. float (*getParameter) (Effect*, int32_t);
  34. int32_t numPrograms, numParams, numInputs, numOutputs, flags;
  35. intptr_t resvd1, resvd2;
  36. int32_t initialDelay, realQualities, offQualities;
  37. float ioRatio;
  38. void* object;
  39. void* user;
  40. int32_t uniqueId, version;
  41. void (*processReplacing) (Effect*, float**, float**, int32_t);
  42. void* processDoubleReplacing;
  43. char future[56];
  44. };
  45. typedef struct
  46. {
  47. int32_t type, byteSize, deltaFrames, flags, noteLength, noteOffset;
  48. unsigned char data[4];
  49. char detune, noteOffVelocity, r1, r2;
  50. } MidiEvent;
  51. typedef struct
  52. {
  53. int32_t numEvents;
  54. intptr_t reserved;
  55. MidiEvent* events[2];
  56. } Events;
  57. typedef struct
  58. {
  59. char label[64];
  60. int32_t flags, arrangementType;
  61. char shortLabel[8];
  62. char future[48];
  63. } PinProperties;
  64. #ifndef PLUGIN_NAME
  65. #define PLUGIN_NAME "LYNX Test Drums"
  66. #endif
  67. #ifndef PLUGIN_ID
  68. #define PLUGIN_ID 0x4C584432 /* 'LXD2' */
  69. #endif
  70. enum { outputPairs = 8 };
  71. static const char* const names[outputPairs] = { "Kick", "Snare", "Clap", "Hat", "Tom 1", "Tom 2", "Ride", "Crash" };
  72. typedef struct
  73. {
  74. int call, key, velocity, length;
  75. } AutoHit;
  76. typedef struct
  77. {
  78. Effect effect;
  79. float rate;
  80. float amplitude[outputPairs];
  81. double phase[outputPairs];
  82. HostCallback host;
  83. AutoHit hits[32];
  84. int hitCount, sends, echo;
  85. long long calls;
  86. unsigned char echoed[64][3];
  87. int echoCount;
  88. } Drums;
  89. static void sendNote (Drums* s, unsigned char status, int key, int velocity)
  90. {
  91. MidiEvent m;
  92. Events ev;
  93. memset (&m, 0, sizeof (m));
  94. m.type = 1;
  95. m.byteSize = sizeof (MidiEvent);
  96. m.data[0] = status;
  97. m.data[1] = (unsigned char) key;
  98. m.data[2] = (unsigned char) velocity;
  99. memset (&ev, 0, sizeof (ev));
  100. ev.numEvents = 1;
  101. ev.events[0] = &m;
  102. s->host (&s->effect, 8, 0, 0, &ev, 0.0f); /* audioMasterProcessEvents */
  103. }
  104. static intptr_t dispatch (Effect* e, int32_t op, int32_t index, intptr_t value, void* ptr, float opt)
  105. {
  106. Drums* s = (Drums*) e->object;
  107. (void) value;
  108. switch (op)
  109. {
  110. case 1: free (s); return 0; /* close */
  111. case 10: s->rate = opt; return 0; /* setSampleRate */
  112. case 25: /* processEvents */
  113. {
  114. Events* ev = (Events*) ptr;
  115. for (int i = 0; i < ev->numEvents; ++i)
  116. {
  117. const unsigned char* d = ev->events[i]->data;
  118. if ((d[0] & 0xF0) == 0x90 && d[2] > 0)
  119. {
  120. const int k = ((d[1] - 36) % outputPairs + outputPairs) % outputPairs;
  121. s->amplitude[k] = d[2] / 127.0f;
  122. s->phase[k] = 0.0;
  123. }
  124. if (s->echo && ((d[0] & 0xF0) == 0x90 || (d[0] & 0xF0) == 0x80) && s->echoCount < 64)
  125. {
  126. s->echoed[s->echoCount][0] = d[0];
  127. s->echoed[s->echoCount][1] = d[1];
  128. s->echoed[s->echoCount][2] = d[2];
  129. ++s->echoCount;
  130. }
  131. }
  132. return 1;
  133. }
  134. case 33: /* getInputProperties: none */
  135. return 0;
  136. case 34: /* getOutputProperties */
  137. {
  138. if (index < 0 || index >= 2 * outputPairs || ptr == 0)
  139. return 0;
  140. PinProperties* p = (PinProperties*) ptr;
  141. memset (p, 0, sizeof (*p));
  142. strcpy (p->label, names[index / 2]);
  143. strncpy (p->shortLabel, names[index / 2], 7);
  144. p->flags = 1 | ((index & 1) == 0 ? 2 : 0); /* active; the left pin of a stereo pair */
  145. #ifdef LAST_PAIR_INACTIVE
  146. if (index / 2 == outputPairs - 1)
  147. p->flags &= ~1; /* the Crash pair not flagged active */
  148. #endif
  149. p->arrangementType = 1; /* stereo */
  150. return 1;
  151. }
  152. case 35: return 2; /* getPlugCategory: synth */
  153. case 45: strcpy ((char*) ptr, PLUGIN_NAME); return 1; /* getEffectName */
  154. case 47: strcpy ((char*) ptr, "LYNX"); return 1; /* getVendorString */
  155. case 48: strcpy ((char*) ptr, PLUGIN_NAME); return 1; /* getProductString */
  156. case 51: /* canDo */
  157. if (ptr != 0 && s->sends && (strcmp ((const char*) ptr, "sendVstEvents") == 0 || strcmp ((const char*) ptr, "sendVstMidiEvent") == 0))
  158. return 1;
  159. return ptr != 0 && (strcmp ((const char*) ptr, "receiveVstEvents") == 0 || strcmp ((const char*) ptr, "receiveVstMidiEvent") == 0) ? 1 : 0;
  160. case 58: return 2400; /* getVstVersion */
  161. default: return 0;
  162. }
  163. }
  164. static void setParam (Effect* e, int32_t i, float v) { (void) e, (void) i, (void) v; }
  165. static float getParam (Effect* e, int32_t i) { (void) e, (void) i; return 0.0f; }
  166. static void processReplacing (Effect* e, float** in, float** out, int32_t n)
  167. {
  168. Drums* s = (Drums*) e->object;
  169. const float rate = s->rate > 0 ? s->rate : 44100.0f;
  170. const float decay = expf (-1.0f / (0.25f * rate)); /* ~250 ms */
  171. (void) in;
  172. if (s->sends && n > 0)
  173. {
  174. ++s->calls;
  175. for (int i = 0; i < s->echoCount; ++i)
  176. sendNote (s, s->echoed[i][0], s->echoed[i][1], s->echoed[i][2]);
  177. s->echoCount = 0;
  178. for (int i = 0; i < s->hitCount; ++i)
  179. {
  180. const AutoHit* h = &s->hits[i];
  181. const int k = ((h->key - 36) % outputPairs + outputPairs) % outputPairs;
  182. if (h->call == s->calls)
  183. {
  184. s->amplitude[k] = h->velocity / 127.0f;
  185. s->phase[k] = 0.0;
  186. sendNote (s, 0x99, h->key, h->velocity);
  187. }
  188. if (h->call + h->length == s->calls)
  189. sendNote (s, 0x89, h->key, 0);
  190. }
  191. }
  192. for (int k = 0; k < outputPairs; ++k)
  193. {
  194. const double step = 110.0 * (k + 1) / rate;
  195. for (int32_t i = 0; i < n; ++i)
  196. {
  197. const float v = s->amplitude[k] * 0.5f * (float) sin (2.0 * 3.14159265358979 * s->phase[k]);
  198. s->phase[k] += step;
  199. if (s->phase[k] >= 1.0) s->phase[k] -= 1.0;
  200. s->amplitude[k] *= decay;
  201. out[2 * k][i] = v;
  202. out[2 * k + 1][i] = v;
  203. }
  204. }
  205. }
  206. EXPORT Effect* VSTPluginMain (HostCallback host)
  207. {
  208. Drums* s = (Drums*) calloc (1, sizeof (Drums));
  209. if (s == 0 || host (0, 1, 0, 0, 0, 0.0f) == 0) /* audioMasterVersion */
  210. return 0;
  211. s->rate = 44100.0f;
  212. s->host = host;
  213. {
  214. const char* a = getenv ("LYNX_TEST_PADS_AUTO");
  215. const char* echo = getenv ("LYNX_TEST_PADS_ECHO");
  216. s->echo = echo != 0 && atoi (echo) != 0;
  217. while (a != 0 && *a != 0 && s->hitCount < 32)
  218. {
  219. AutoHit h;
  220. int used = 0;
  221. if (sscanf (a, "%d:%d:%d:%d%n", &h.call, &h.key, &h.velocity, &h.length, &used) != 4)
  222. break;
  223. if (h.length < 1) h.length = 1;
  224. s->hits[s->hitCount++] = h;
  225. a += used;
  226. while (*a == ',' || *a == ' ') ++a;
  227. }
  228. s->sends = s->hitCount > 0 || s->echo;
  229. }
  230. s->effect.magic = 0x56737450;
  231. s->effect.dispatcher = dispatch;
  232. s->effect.setParameter = setParam;
  233. s->effect.getParameter = getParam;
  234. s->effect.processReplacing = processReplacing;
  235. s->effect.numPrograms = 1;
  236. s->effect.numParams = 0;
  237. s->effect.numInputs = 0;
  238. s->effect.numOutputs = 2 * outputPairs;
  239. s->effect.flags = (1 << 4) | (1 << 8); /* replacing, synth */
  240. s->effect.object = s;
  241. s->effect.uniqueId = PLUGIN_ID;
  242. s->effect.version = 1;
  243. return &s->effect;
  244. }