lynx_test_vst2.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. * LYNX_TEST_VST2_TIMELOG=<file> the song position audioMasterGetTime gives in the first 64 playing processReplacing calls, one
  18. * line each: "S <ppqPos> <playing>" ("S none <playing>" without ppqPos) (round 190te)
  19. */
  20. #ifdef _WIN32
  21. #include <windows.h>
  22. #endif
  23. #include <math.h>
  24. #include <stdint.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. typedef struct Effect Effect;
  29. typedef intptr_t (*HostCallback) (Effect*, int32_t, int32_t, intptr_t, void*, float);
  30. struct Effect
  31. {
  32. int32_t magic;
  33. intptr_t (*dispatcher) (Effect*, int32_t, int32_t, intptr_t, void*, float);
  34. void (*process) (Effect*, float**, float**, int32_t);
  35. void (*setParameter) (Effect*, int32_t, float);
  36. float (*getParameter) (Effect*, int32_t);
  37. int32_t numPrograms, numParams, numInputs, numOutputs, flags;
  38. intptr_t resvd1, resvd2;
  39. int32_t initialDelay, realQualities, offQualities;
  40. float ioRatio;
  41. void* object;
  42. void* user;
  43. int32_t uniqueId, version;
  44. void (*processReplacing) (Effect*, float**, float**, int32_t);
  45. void* processDoubleReplacing;
  46. char future[56];
  47. };
  48. typedef struct
  49. {
  50. int32_t type, byteSize, deltaFrames, flags, noteLength, noteOffset;
  51. unsigned char data[4];
  52. char detune, noteOffVelocity, r1, r2;
  53. } MidiEvent;
  54. typedef struct
  55. {
  56. int32_t numEvents;
  57. intptr_t reserved;
  58. MidiEvent* events[2];
  59. } Events;
  60. typedef struct
  61. {
  62. double samplePos, sampleRate, nanoSeconds, ppqPos, tempo, barStartPos, cycleStartPos, cycleEndPos;
  63. int32_t num, den, smpteOffset, smpteFrameRate, samplesToNextClock, flags;
  64. } TimeInfo;
  65. typedef struct
  66. {
  67. Effect effect;
  68. HostCallback host;
  69. float gain, tempo, rate;
  70. int note;
  71. float amplitude;
  72. double phase;
  73. unsigned char chunk[12];
  74. int blocks, crash;
  75. void* window;
  76. int timed; /* round 190te: process calls written to LYNX_TEST_VST2_TIMELOG */
  77. } Synth;
  78. #ifdef _WIN32
  79. /* LYNX_TEST_VST2_KEYS=eat|pass: the view takes the keyboard focus when clicked and either consumes every key or passes it
  80. back to its parent, as plugins do (issue #96). LYNX_TEST_VST2_KEYLOG: a file getting one line per key it saw. */
  81. static LRESULT CALLBACK keysViewProc (HWND h, UINT m, WPARAM w, LPARAM l)
  82. {
  83. if (m == WM_LBUTTONDOWN)
  84. {
  85. SetFocus (h);
  86. return 0;
  87. }
  88. if (m == WM_KEYDOWN || m == WM_KEYUP)
  89. {
  90. const char* path = getenv ("LYNX_TEST_VST2_KEYLOG");
  91. const char* mode = getenv ("LYNX_TEST_VST2_KEYS");
  92. FILE* f = path != 0 ? fopen (path, "a") : 0;
  93. if (f != 0)
  94. {
  95. fprintf (f, "%s %d %s\n", m == WM_KEYDOWN ? "down" : "up", (int) w, mode != 0 ? mode : "");
  96. fclose (f);
  97. }
  98. if (mode != 0 && strcmp (mode, "pass") == 0)
  99. return SendMessageA (GetParent (h), m, w, l);
  100. return 0;
  101. }
  102. return DefWindowProcA (h, m, w, l);
  103. }
  104. static HWND createKeysView (HWND parent)
  105. {
  106. static int registered = 0;
  107. if (! registered)
  108. {
  109. WNDCLASSA c;
  110. memset (&c, 0, sizeof (c));
  111. c.lpfnWndProc = keysViewProc;
  112. c.hInstance = GetModuleHandleA (0);
  113. c.hbrBackground = (HBRUSH) (COLOR_BTNFACE + 1);
  114. c.lpszClassName = "LYNXTestKeysView";
  115. RegisterClassA (&c);
  116. registered = 1;
  117. }
  118. return CreateWindowExA (0, "LYNXTestKeysView", "LYNX Test Synth", WS_CHILD | WS_VISIBLE, 0, 0, 240, 120, parent, 0, GetModuleHandleA (0), 0);
  119. }
  120. #endif
  121. static intptr_t dispatch (Effect* e, int32_t op, int32_t index, intptr_t value, void* ptr, float opt)
  122. {
  123. Synth* s = (Synth*) e->object;
  124. (void) index;
  125. switch (op)
  126. {
  127. case 1: free (s); return 0; /* close */
  128. case 10: s->rate = opt; return 0; /* setSampleRate */
  129. case 8: strcpy ((char*) ptr, index == 0 ? "Gain" : index == 1 ? "Tempo" : "Bits"); return 0; /* getParamName */
  130. #ifdef _WIN32
  131. case 13: /* editGetRect */
  132. {
  133. static short rect[4] = { 0, 0, 120, 240 }; /* top, left, bottom, right */
  134. *(short**) ptr = rect;
  135. return 1;
  136. }
  137. case 14: /* editOpen: a child of the host's window */
  138. if (getenv ("LYNX_TEST_VST2_KEYS") != 0)
  139. {
  140. s->window = createKeysView ((HWND) ptr);
  141. return s->window != 0;
  142. }
  143. s->window = CreateWindowExA (0, "STATIC", "LYNX Test Synth", WS_CHILD | WS_VISIBLE | SS_CENTER, 0, 0, 240, 120,
  144. (HWND) ptr, 0, GetModuleHandleA (0), 0);
  145. return s->window != 0;
  146. case 15: /* editClose */
  147. if (s->window != 0) DestroyWindow ((HWND) s->window);
  148. s->window = 0;
  149. return 0;
  150. #endif
  151. case 23: /* getChunk */
  152. memcpy (s->chunk, "LXV2", 4);
  153. memcpy (s->chunk + 4, &s->gain, 4);
  154. memcpy (s->chunk + 8, &s->tempo, 4); /* what the host's time info said (for the host test) */
  155. *(void**) ptr = s->chunk;
  156. return 12;
  157. case 24: /* setChunk */
  158. if (value >= 8 && memcmp (ptr, "LXV2", 4) == 0)
  159. memcpy (&s->gain, (char*) ptr + 4, 4);
  160. return 1;
  161. case 25: /* processEvents */
  162. {
  163. Events* ev = (Events*) ptr;
  164. for (int i = 0; i < ev->numEvents; ++i)
  165. {
  166. const unsigned char* d = ev->events[i]->data;
  167. { /* LYNX_TEST_VST2_MIDILOG = a file: every MIDI event as the host sent it (--wrappermiditest) */
  168. const char* path = getenv ("LYNX_TEST_VST2_MIDILOG");
  169. FILE* log = path != 0 ? fopen (path, "a") : 0;
  170. if (log != 0)
  171. {
  172. fprintf (log, "%p %d %d %d\n", (void*) s, d[0], d[1], d[2]);
  173. fclose (log);
  174. }
  175. }
  176. if ((d[0] & 0xF0) == 0x90 && d[2] > 0) { s->note = d[1]; s->amplitude = d[2] / 127.0f; }
  177. else if ((d[0] & 0xF0) == 0x80 || ((d[0] & 0xF0) == 0x90 && d[2] == 0)) { if (d[1] == s->note) s->amplitude = 0.0f; }
  178. }
  179. return 1;
  180. }
  181. case 35: return 2; /* getPlugCategory: synth */
  182. case 45: strcpy ((char*) ptr, "LYNX Test Synth"); return 1; /* getEffectName */
  183. case 47: strcpy ((char*) ptr, "LYNX"); return 1; /* getVendorString */
  184. case 58: return 2400; /* getVstVersion */
  185. default: return 0;
  186. }
  187. }
  188. static void setParam (Effect* e, int32_t i, float v)
  189. {
  190. Synth* s = (Synth*) e->object;
  191. if (i == 0) s->gain = v;
  192. }
  193. static float getParam (Effect* e, int32_t i)
  194. {
  195. Synth* s = (Synth*) e->object;
  196. return i == 0 ? s->gain : i == 1 ? s->tempo : (float) (sizeof (void*) * 8) / 64.0f;
  197. }
  198. static void processReplacing (Effect* e, float** in, float** out, int32_t n)
  199. {
  200. Synth* s = (Synth*) e->object;
  201. (void) in;
  202. if (s->crash && ++s->blocks == 20)
  203. *(volatile int*) 0 = 1; /* LYNX_TEST_VST2_CRASH=process */
  204. const TimeInfo* t = (const TimeInfo*) s->host (e, 7, 0, 0, 0, 0.0f);
  205. if (t != 0 && (t->flags & (1 << 10)))
  206. s->tempo = (float) (t->tempo / 1000.0);
  207. /* round 190te (issue #356): the position of the first 64 playing calls (a host's warm-up runs stopped),
  208. kPpqPosValid = 1 << 9, kVstTransportPlaying = 1 << 1 */
  209. if (s->timed < 64 && t != 0 && (t->flags & (1 << 1)))
  210. {
  211. #ifdef _WIN32
  212. /* (the process's environment as it is now: the C runtime's copy is the one of when the DLL was loaded) */
  213. char text[1024] = { 0 };
  214. const char* path = GetEnvironmentVariableA ("LYNX_TEST_VST2_TIMELOG", text, sizeof (text)) > 0 ? text : 0;
  215. #else
  216. const char* path = getenv ("LYNX_TEST_VST2_TIMELOG");
  217. #endif
  218. FILE* f = path != 0 ? fopen (path, "a") : 0;
  219. if (f != 0)
  220. {
  221. if (t != 0 && (t->flags & (1 << 9)))
  222. fprintf (f, "S %.9f %d\n", t->ppqPos, (t->flags & (1 << 1)) ? 1 : 0);
  223. else
  224. fprintf (f, "S none %d\n", t != 0 && (t->flags & (1 << 1)) ? 1 : 0);
  225. fclose (f);
  226. }
  227. ++s->timed;
  228. }
  229. const double step = 440.0 * pow (2.0, (s->note - 69) / 12.0) / (s->rate > 0 ? s->rate : 44100.0);
  230. for (int32_t i = 0; i < n; ++i)
  231. {
  232. const float v = s->gain * s->amplitude * (float) sin (2.0 * 3.14159265358979 * s->phase);
  233. s->phase += step;
  234. if (s->phase >= 1.0) s->phase -= 1.0;
  235. out[0][i] = v;
  236. out[1][i] = v;
  237. }
  238. }
  239. __declspec (dllexport) Effect* VSTPluginMain (HostCallback host)
  240. {
  241. Synth* s = (Synth*) calloc (1, sizeof (Synth));
  242. if (s == 0 || host (0, 1, 0, 0, 0, 0.0f) == 0) /* audioMasterVersion */
  243. return 0;
  244. s->host = host;
  245. s->gain = 0.5f;
  246. s->rate = 44100.0f;
  247. s->note = 69;
  248. s->effect.magic = 0x56737450;
  249. s->effect.dispatcher = dispatch;
  250. s->effect.setParameter = setParam;
  251. s->effect.getParameter = getParam;
  252. s->effect.processReplacing = processReplacing;
  253. s->effect.numPrograms = 1;
  254. s->effect.numParams = 3;
  255. s->effect.numInputs = 0;
  256. s->effect.numOutputs = 2;
  257. s->effect.flags = (1 << 4) | (1 << 5) | (1 << 8); /* replacing, program chunks, synth */
  258. #ifdef _WIN32
  259. s->effect.flags |= 1; /* has editor */
  260. #endif
  261. {
  262. const char* crash = getenv ("LYNX_TEST_VST2_CRASH");
  263. const char* latency = getenv ("LYNX_TEST_VST2_LATENCY");
  264. s->crash = crash != 0 && strcmp (crash, "process") == 0;
  265. s->effect.initialDelay = latency != 0 ? atoi (latency) : 0;
  266. }
  267. s->effect.object = s;
  268. s->effect.uniqueId = 0x4C585632; /* 'LXV2' */
  269. s->effect.version = 1;
  270. return &s->effect;
  271. }