lynx_test_vst2.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /* A minimal VST2 instrument for testing LYNX's own VST2 host (src/engine/Vst2Format.cpp) without third-party
  2. * plugins. Written against the VST 2.4 binary interface as LYNX declares it (no Steinberg headers):
  3. * zig cc -shared -O2 -target x86_64-windows-gnu tools/testplugin/lynx_test_vst2.c -o "LYNX Test Synth.dll"
  4. * LYNX.exe --vst2test "LYNX Test Synth.dll" report.txt
  5. * 32-bit build (round 176a, the 32-bit bridge LYNX-bridge32.exe):
  6. * zig cc -shared -O2 -target x86-windows-gnu tools/testplugin/lynx_test_vst2.c -luser32 -o "LYNX Test Synth 32.dll"
  7. * LYNX.exe --bridge32test "LYNX Test Synth 32.dll" report.txt
  8. * The wrapper's MIDI options on a running plugin (round 181c):
  9. * LYNX_TEST_VST2_MIDILOG=midi.txt LYNX.exe --wrappermiditest "LYNX Test Synth.dll" report.txt
  10. * Sound: a sine at the played note, amplitude = parameter 0 ("Gain") × velocity.
  11. * Parameter 1 ("Tempo") reports the host tempo / 1000 it last read through audioMasterGetTime.
  12. * State: a program chunk ('LXV2' + gain + last tempo/1000), so a host must restore it with effSetChunk.
  13. * Parameter 2 ("Bits"): the plugin's pointer size / 64 (0.5 in a 32-bit build): which process it runs in.
  14. * Editor (Windows): a plain 240x120 child window. Environment (inherited by a bridge process):
  15. * LYNX_TEST_VST2_CRASH=process an access violation in the 20th processReplacing call (crash isolation tests)
  16. * LYNX_TEST_VST2_LATENCY=<n> reports n samples of latency (initialDelay)
  17. */
  18. #ifdef _WIN32
  19. #include <windows.h>
  20. #endif
  21. #include <math.h>
  22. #include <stdint.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. typedef struct Effect Effect;
  27. typedef intptr_t (*HostCallback) (Effect*, int32_t, int32_t, intptr_t, void*, float);
  28. struct Effect
  29. {
  30. int32_t magic;
  31. intptr_t (*dispatcher) (Effect*, int32_t, int32_t, intptr_t, void*, float);
  32. void (*process) (Effect*, float**, float**, int32_t);
  33. void (*setParameter) (Effect*, int32_t, float);
  34. float (*getParameter) (Effect*, int32_t);
  35. int32_t numPrograms, numParams, numInputs, numOutputs, flags;
  36. intptr_t resvd1, resvd2;
  37. int32_t initialDelay, realQualities, offQualities;
  38. float ioRatio;
  39. void* object;
  40. void* user;
  41. int32_t uniqueId, version;
  42. void (*processReplacing) (Effect*, float**, float**, int32_t);
  43. void* processDoubleReplacing;
  44. char future[56];
  45. };
  46. typedef struct
  47. {
  48. int32_t type, byteSize, deltaFrames, flags, noteLength, noteOffset;
  49. unsigned char data[4];
  50. char detune, noteOffVelocity, r1, r2;
  51. } MidiEvent;
  52. typedef struct
  53. {
  54. int32_t numEvents;
  55. intptr_t reserved;
  56. MidiEvent* events[2];
  57. } Events;
  58. typedef struct
  59. {
  60. double samplePos, sampleRate, nanoSeconds, ppqPos, tempo, barStartPos, cycleStartPos, cycleEndPos;
  61. int32_t num, den, smpteOffset, smpteFrameRate, samplesToNextClock, flags;
  62. } TimeInfo;
  63. typedef struct
  64. {
  65. Effect effect;
  66. HostCallback host;
  67. float gain, tempo, rate;
  68. int note;
  69. float amplitude;
  70. double phase;
  71. unsigned char chunk[12];
  72. int blocks, crash;
  73. void* window;
  74. } Synth;
  75. #ifdef _WIN32
  76. /* LYNX_TEST_VST2_KEYS=eat|pass: the view takes the keyboard focus when clicked and either consumes every key or passes it
  77. back to its parent, as plugins do (issue #96). LYNX_TEST_VST2_KEYLOG: a file getting one line per key it saw. */
  78. static LRESULT CALLBACK keysViewProc (HWND h, UINT m, WPARAM w, LPARAM l)
  79. {
  80. if (m == WM_LBUTTONDOWN)
  81. {
  82. SetFocus (h);
  83. return 0;
  84. }
  85. if (m == WM_KEYDOWN || m == WM_KEYUP)
  86. {
  87. const char* path = getenv ("LYNX_TEST_VST2_KEYLOG");
  88. const char* mode = getenv ("LYNX_TEST_VST2_KEYS");
  89. FILE* f = path != 0 ? fopen (path, "a") : 0;
  90. if (f != 0)
  91. {
  92. fprintf (f, "%s %d %s\n", m == WM_KEYDOWN ? "down" : "up", (int) w, mode != 0 ? mode : "");
  93. fclose (f);
  94. }
  95. if (mode != 0 && strcmp (mode, "pass") == 0)
  96. return SendMessageA (GetParent (h), m, w, l);
  97. return 0;
  98. }
  99. return DefWindowProcA (h, m, w, l);
  100. }
  101. static HWND createKeysView (HWND parent)
  102. {
  103. static int registered = 0;
  104. if (! registered)
  105. {
  106. WNDCLASSA c;
  107. memset (&c, 0, sizeof (c));
  108. c.lpfnWndProc = keysViewProc;
  109. c.hInstance = GetModuleHandleA (0);
  110. c.hbrBackground = (HBRUSH) (COLOR_BTNFACE + 1);
  111. c.lpszClassName = "LYNXTestKeysView";
  112. RegisterClassA (&c);
  113. registered = 1;
  114. }
  115. return CreateWindowExA (0, "LYNXTestKeysView", "LYNX Test Synth", WS_CHILD | WS_VISIBLE, 0, 0, 240, 120, parent, 0, GetModuleHandleA (0), 0);
  116. }
  117. #endif
  118. static intptr_t dispatch (Effect* e, int32_t op, int32_t index, intptr_t value, void* ptr, float opt)
  119. {
  120. Synth* s = (Synth*) e->object;
  121. (void) index;
  122. switch (op)
  123. {
  124. case 1: free (s); return 0; /* close */
  125. case 10: s->rate = opt; return 0; /* setSampleRate */
  126. case 8: strcpy ((char*) ptr, index == 0 ? "Gain" : index == 1 ? "Tempo" : "Bits"); return 0; /* getParamName */
  127. #ifdef _WIN32
  128. case 13: /* editGetRect */
  129. {
  130. static short rect[4] = { 0, 0, 120, 240 }; /* top, left, bottom, right */
  131. *(short**) ptr = rect;
  132. return 1;
  133. }
  134. case 14: /* editOpen: a child of the host's window */
  135. if (getenv ("LYNX_TEST_VST2_KEYS") != 0)
  136. {
  137. s->window = createKeysView ((HWND) ptr);
  138. return s->window != 0;
  139. }
  140. s->window = CreateWindowExA (0, "STATIC", "LYNX Test Synth", WS_CHILD | WS_VISIBLE | SS_CENTER, 0, 0, 240, 120,
  141. (HWND) ptr, 0, GetModuleHandleA (0), 0);
  142. return s->window != 0;
  143. case 15: /* editClose */
  144. if (s->window != 0) DestroyWindow ((HWND) s->window);
  145. s->window = 0;
  146. return 0;
  147. #endif
  148. case 23: /* getChunk */
  149. memcpy (s->chunk, "LXV2", 4);
  150. memcpy (s->chunk + 4, &s->gain, 4);
  151. memcpy (s->chunk + 8, &s->tempo, 4); /* what the host's time info said (for the host test) */
  152. *(void**) ptr = s->chunk;
  153. return 12;
  154. case 24: /* setChunk */
  155. if (value >= 8 && memcmp (ptr, "LXV2", 4) == 0)
  156. memcpy (&s->gain, (char*) ptr + 4, 4);
  157. return 1;
  158. case 25: /* processEvents */
  159. {
  160. Events* ev = (Events*) ptr;
  161. for (int i = 0; i < ev->numEvents; ++i)
  162. {
  163. const unsigned char* d = ev->events[i]->data;
  164. { /* LYNX_TEST_VST2_MIDILOG = a file: every MIDI event as the host sent it (--wrappermiditest) */
  165. const char* path = getenv ("LYNX_TEST_VST2_MIDILOG");
  166. FILE* log = path != 0 ? fopen (path, "a") : 0;
  167. if (log != 0)
  168. {
  169. fprintf (log, "%p %d %d %d\n", (void*) s, d[0], d[1], d[2]);
  170. fclose (log);
  171. }
  172. }
  173. if ((d[0] & 0xF0) == 0x90 && d[2] > 0) { s->note = d[1]; s->amplitude = d[2] / 127.0f; }
  174. else if ((d[0] & 0xF0) == 0x80 || ((d[0] & 0xF0) == 0x90 && d[2] == 0)) { if (d[1] == s->note) s->amplitude = 0.0f; }
  175. }
  176. return 1;
  177. }
  178. case 35: return 2; /* getPlugCategory: synth */
  179. case 45: strcpy ((char*) ptr, "LYNX Test Synth"); return 1; /* getEffectName */
  180. case 47: strcpy ((char*) ptr, "LYNX"); return 1; /* getVendorString */
  181. case 58: return 2400; /* getVstVersion */
  182. default: return 0;
  183. }
  184. }
  185. static void setParam (Effect* e, int32_t i, float v)
  186. {
  187. Synth* s = (Synth*) e->object;
  188. if (i == 0) s->gain = v;
  189. }
  190. static float getParam (Effect* e, int32_t i)
  191. {
  192. Synth* s = (Synth*) e->object;
  193. return i == 0 ? s->gain : i == 1 ? s->tempo : (float) (sizeof (void*) * 8) / 64.0f;
  194. }
  195. static void processReplacing (Effect* e, float** in, float** out, int32_t n)
  196. {
  197. Synth* s = (Synth*) e->object;
  198. (void) in;
  199. if (s->crash && ++s->blocks == 20)
  200. *(volatile int*) 0 = 1; /* LYNX_TEST_VST2_CRASH=process */
  201. const TimeInfo* t = (const TimeInfo*) s->host (e, 7, 0, 0, 0, 0.0f);
  202. if (t != 0 && (t->flags & (1 << 10)))
  203. s->tempo = (float) (t->tempo / 1000.0);
  204. const double step = 440.0 * pow (2.0, (s->note - 69) / 12.0) / (s->rate > 0 ? s->rate : 44100.0);
  205. for (int32_t i = 0; i < n; ++i)
  206. {
  207. const float v = s->gain * s->amplitude * (float) sin (2.0 * 3.14159265358979 * s->phase);
  208. s->phase += step;
  209. if (s->phase >= 1.0) s->phase -= 1.0;
  210. out[0][i] = v;
  211. out[1][i] = v;
  212. }
  213. }
  214. __declspec (dllexport) Effect* VSTPluginMain (HostCallback host)
  215. {
  216. Synth* s = (Synth*) calloc (1, sizeof (Synth));
  217. if (s == 0 || host (0, 1, 0, 0, 0, 0.0f) == 0) /* audioMasterVersion */
  218. return 0;
  219. s->host = host;
  220. s->gain = 0.5f;
  221. s->rate = 44100.0f;
  222. s->note = 69;
  223. s->effect.magic = 0x56737450;
  224. s->effect.dispatcher = dispatch;
  225. s->effect.setParameter = setParam;
  226. s->effect.getParameter = getParam;
  227. s->effect.processReplacing = processReplacing;
  228. s->effect.numPrograms = 1;
  229. s->effect.numParams = 3;
  230. s->effect.numInputs = 0;
  231. s->effect.numOutputs = 2;
  232. s->effect.flags = (1 << 4) | (1 << 5) | (1 << 8); /* replacing, program chunks, synth */
  233. #ifdef _WIN32
  234. s->effect.flags |= 1; /* has editor */
  235. #endif
  236. {
  237. const char* crash = getenv ("LYNX_TEST_VST2_CRASH");
  238. const char* latency = getenv ("LYNX_TEST_VST2_LATENCY");
  239. s->crash = crash != 0 && strcmp (crash, "process") == 0;
  240. s->effect.initialDelay = latency != 0 ? atoi (latency) : 0;
  241. }
  242. s->effect.object = s;
  243. s->effect.uniqueId = 0x4C585632; /* 'LXV2' */
  244. s->effect.version = 1;
  245. return &s->effect;
  246. }