Debian Codegraph @

modules/lsp-runtime-lib/src/test/utest/fmt/sfz/pullparser.cpp

raw
1 /*
2 * Copyright (C) 2023 Linux Studio Plugins Project <https://lsp-plug.in/>
3 * (C) 2023 Vladimir Sadovnikov <[email protected]>
4 *
5 * This file is part of lsp-runtime-lib
6 * Created on: 30 янв. 2023 г.
7 *
8 * lsp-runtime-lib is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * any later version.
12 *
13 * lsp-runtime-lib is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with lsp-runtime-lib. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22 #include <lsp-plug.in/fmt/sfz/PullParser.h>
23 #include <lsp-plug.in/test-fw/utest.h>
24
25 UTEST_BEGIN("runtime.fmt.sfz", pullparser)
26
27 class Verifier
28 {
29 private:
30 sfz::PullParser sParser;
31 test_type_t *pTest;
32 const char *__test_group;
33 const char *__test_name;
34
35 public:
36 Verifier(test_type_t *test)
37 {
38 pTest = test;
39 __test_group = test->__test_group;
40 __test_name = test->__test_name;
41 }
42
43 public:
44 Verifier &open(const char *name)
45 {
46 io::Path path;
47 UTEST_ASSERT(path.fmt("%s/%s", pTest->resources(), name) > 0);
48 UTEST_ASSERT(sParser.open(&path) == STATUS_OK);
49 return *this;
50 }
51
52 Verifier &wrap(const char *text)
53 {
54 UTEST_ASSERT(sParser.wrap(text) == STATUS_OK);
55 return *this;
56 }
57
58 Verifier &close()
59 {
60 UTEST_ASSERT(sParser.close() == STATUS_OK);
61 return *this;
62 }
63
64 Verifier &header(const char *name)
65 {
66 sfz::event_t ev;
67 UTEST_ASSERT(sParser.next(&ev) == STATUS_OK);
68 UTEST_ASSERT(ev.type == sfz::EVENT_HEADER);
69 UTEST_ASSERT(ev.name.equals_ascii(name));
70 UTEST_ASSERT(ev.value.is_empty());
71 UTEST_ASSERT(ev.blob.avail() == -STATUS_NO_DATA);
72 pTest->printf(" header \"%s\"\n", name);
73 return *this;
74 }
75
76 Verifier &opcode(const char *name, const char *value)
77 {
78 sfz::event_t ev;
79 UTEST_ASSERT(sParser.next(&ev) == STATUS_OK);
80 UTEST_ASSERT(ev.type == sfz::EVENT_OPCODE);
81 UTEST_ASSERT(ev.name.equals_ascii(name));
82 UTEST_ASSERT(ev.value.equals_ascii(value));
83 UTEST_ASSERT(ev.blob.avail() == -STATUS_NO_DATA);
84 pTest->printf(" opcode %s=\"%s\"\n", name, value);
85 return *this;
86 }
87
88 Verifier &comment(const char *value)
89 {
90 sfz::event_t ev;
91 UTEST_ASSERT(sParser.next(&ev) == STATUS_OK);
92 UTEST_ASSERT(ev.type == sfz::EVENT_COMMENT);
93 UTEST_ASSERT(ev.name.is_empty());
94 UTEST_ASSERT(ev.value.equals_ascii(value));
95 UTEST_ASSERT(ev.blob.avail() == -STATUS_NO_DATA);
96 pTest->printf(" comment \"%s\"\n", value);
97 return *this;
98 }
99
100 Verifier &include(const char *value)
101 {
102 sfz::event_t ev;
103 UTEST_ASSERT(sParser.next(&ev) == STATUS_OK);
104 UTEST_ASSERT(ev.type == sfz::EVENT_INCLUDE);
105 UTEST_ASSERT(ev.name.equals_ascii(value));
106 UTEST_ASSERT(ev.value.is_empty());
107 pTest->printf(" include \"%s\"\n", value);
108 return *this;
109 }
110
111 Verifier &define(const char *name, const char *value)
112 {
113 sfz::event_t ev;
114 UTEST_ASSERT(sParser.next(&ev) == STATUS_OK);
115 UTEST_ASSERT(ev.type == sfz::EVENT_DEFINE);
116 UTEST_ASSERT(ev.name.equals_ascii(name));
117 UTEST_ASSERT(ev.value.equals_ascii(value));
118 UTEST_ASSERT(ev.blob.avail() == -STATUS_NO_DATA);
119 pTest->printf(" define %s %s\n", name, value);
120 return *this;
121 }
122
123 Verifier &sample(const char *name, const char *data)
124 {
125 sfz::event_t ev;
126 UTEST_ASSERT(sParser.next(&ev) == STATUS_OK);
127 UTEST_ASSERT(ev.type == sfz::EVENT_SAMPLE);
128 UTEST_ASSERT(ev.name.equals_ascii(name));
129 UTEST_ASSERT(ev.value.is_empty());
130 UTEST_ASSERT(ev.blob.avail() == ssize_t(strlen(data)));
131 UTEST_ASSERT(memcmp(ev.blob.data(), data, ev.blob.avail()) == 0);
132
133 pTest->printf(" sample name=\"%s\" data=\"%s\"\n", name, data);
134 return *this;
135 }
136
137 Verifier &status(status_t code)
138 {
139 sfz::event_t ev;
140 UTEST_ASSERT(sParser.next(&ev) == code);
141 return *this;
142 }
143 };
144
145 void check_valid_sfz1()
146 {
147 static const char *text =
148 "<control>\n"
149 "\n"
150 "<global>\n"
151 " \n"
152 " <group>\n"
153 " <region>\n"
154 " sample=\n"
155 " <region>\n"
156 " sample=\n"
157 " <group>\n"
158 " <region>\n"
159 " sample=\n"
160 " <region>\n"
161 " sample=\n";
162
163 printf("Checking valid SFZ (case 1)...\n");
164
165 Verifier v(this);
166 v.wrap(text);
167 v.header("control");
168 v.header("global");
169 v.header("group");
170 v.header("region");
171 v.opcode("sample", "");
172 v.header("region");
173 v.opcode("sample", "");
174 v.header("group");
175 v.header("region");
176 v.opcode("sample", "");
177 v.header("region");
178 v.opcode("sample", "");
179 v.status(STATUS_EOF);
180 v.close();
181 }
182
183 void check_valid_sfz2()
184 {
185 static const char *text =
186 "<group>\r\n"
187 "lovel=64 // enter stuff here if you want to apply it to all regions\r\n"
188 "hivel=127\r\n"
189 "\r\n"
190 "<region>\r\n"
191 "sample=Trumpet_C4_v2.wav\r\n"
192 "key=60\r\n"
193 "\r\n"
194 "<region>\r\n"
195 "sample=Trumpet_C#4_v2.wav\r\n"
196 "key=61\r\n"
197 "\r\n"
198 "<region>\r\n"
199 "sample=Trumpet_D4_v2.wav\r\n"
200 "key=62\r\n";
201
202 printf("Checking valid SFZ (case 2)...\n");
203
204 Verifier v(this);
205 v.wrap(text);
206 v.header("group");
207 v.opcode("lovel", "64");
208 v.comment(" enter stuff here if you want to apply it to all regions");
209 v.opcode("hivel", "127");
210 v.header("region");
211 v.opcode("sample", "Trumpet_C4_v2.wav");
212 v.opcode("key", "60");
213 v.header("region");
214 v.opcode("sample", "Trumpet_C#4_v2.wav");
215 v.opcode("key", "61");
216 v.header("region");
217 v.opcode("sample", "Trumpet_D4_v2.wav");
218 v.opcode("key", "62");
219 v.status(STATUS_EOF);
220 v.close();
221 }
222
223 void check_valid_sfz3()
224 {
225 static const char *text =
226 "<region> sample=piano_D4_vl1.wav lokey=62 hikey=63 pitch_keycenter=62 lovel=1 hivel=50\n"
227 "<region> sample=piano_E4_vl1.wav lokey=64 hikey=65 pitch_keycenter=64 lovel=1 hivel=50\n"
228 "<region> sample=piano_F#4_vl1.wav lokey=66 hikey=67 pitch_keycenter=66 lovel=11 hivel=50\n"
229 "<region> sample=piano_G#4_vl1.wav lokey=68 hikey=69 pitch_keycenter=68 lovel=1 hivel=50";
230
231 printf("Checking valid SFZ (case 3)...\n");
232
233 Verifier v(this);
234 v.wrap(text);
235 v.header("region");
236 v.opcode("sample", "piano_D4_vl1.wav");
237 v.opcode("lokey", "62");
238 v.opcode("hikey", "63");
239 v.opcode("pitch_keycenter", "62");
240 v.opcode("lovel", "1");
241 v.opcode("hivel", "50");
242 v.header("region");
243 v.opcode("sample", "piano_E4_vl1.wav");
244 v.opcode("lokey", "64");
245 v.opcode("hikey", "65");
246 v.opcode("pitch_keycenter", "64");
247 v.opcode("lovel", "1");
248 v.opcode("hivel", "50");
249 v.header("region");
250 v.opcode("sample", "piano_F#4_vl1.wav");
251 v.opcode("lokey", "66");
252 v.opcode("hikey", "67");
253 v.opcode("pitch_keycenter", "66");
254 v.opcode("lovel", "11");
255 v.opcode("hivel", "50");
256 v.header("region");
257 v.opcode("sample", "piano_G#4_vl1.wav");
258 v.opcode("lokey", "68");
259 v.opcode("hikey", "69");
260 v.opcode("pitch_keycenter", "68");
261 v.opcode("lovel", "1");
262 v.opcode("hivel", "50");
263 v.status(STATUS_EOF);
264 v.close();
265 }
266
267 void check_valid_sfz4()
268 {
269 static const char *text =
270 "<control>\n"
271 "#define $KEY1 36 // comment\n"
272 "#define $KEY2 38\n"
273 "<global>\n"
274 "loop_mode=one_shot\n"
275 "ampeg_attack=0.001\n"
276 "ampeg_decay=0.7\n"
277 "ampeg_sustain=100\n"
278 "\n"
279 "<master>\n"
280 "amplitude_cc30=100\n"
281 "offset_cc33=3000\n"
282 "ampeg_sustain_oncc33=-100\n"
283 "<group> key=$KEY1\n"
284 "<region>\n"
285 "sample=../Samples/bobobo/bobobo_bass_vl1_rr1.wav hirand=0.250\n"
286 "<region>\n"
287 "sample=../Samples/bobobo/bobobo_bass_vl1_rr2.wav lorand=0.250 hirand=0.500\n"
288 "<region>\n"
289 "sample=../Samples/bobobo/bobobo_bass_vl1_rr3.wav lorand=0.500 hirand=0.750\n"
290 "<region>\n"
291 "sample=../Samples/bobobo/bobobo_bass_vl1_rr4.wav lorand=0.750\n"
292 "\n"
293 "<master>\n"
294 "amplitude_cc35=100\n"
295 "offset_cc38=1500\n"
296 "ampeg_sustain_oncc38=-100\n"
297 "<group>key=$KEY2\n"
298 "<region>\n"
299 "sample=../Samples/bobobo/bobobo_tenor_l_vl1_rr1.wav hirand=0.250\n"
300 "<region>\n"
301 "sample=../Samples/bobobo/bobobo_tenor_l_vl1_rr2.wav lorand=0.250 hirand=0.500\n"
302 "<region>\n"
303 "sample=../Samples/bobobo/bobobo_tenor_l_vl1_rr3.wav lorand=0.500 hirand=0.750\n"
304 "<region>\n"
305 "sample=../Samples/bobobo/bobobo_tenor_l_vl1_rr4.wav lorand=0.750\n";
306
307 printf("Checking valid SFZ (case 4)...\n");
308
309 Verifier v(this);
310 v.wrap(text);
311 v.header("control");
312 v.define("$KEY1", "36");
313 v.comment(" comment");
314 v.define("$KEY2", "38");
315 v.header("global");
316 v.opcode("loop_mode", "one_shot");
317 v.opcode("ampeg_attack", "0.001");
318 v.opcode("ampeg_decay", "0.7");
319 v.opcode("ampeg_sustain", "100");
320 v.header("master");
321 v.opcode("amplitude_cc30", "100");
322 v.opcode("offset_cc33", "3000");
323 v.opcode("ampeg_sustain_oncc33", "-100");
324 v.header("group");
325 v.opcode("key", "$KEY1");
326 v.header("region");
327 v.opcode("sample", "../Samples/bobobo/bobobo_bass_vl1_rr1.wav");
328 v.opcode("hirand", "0.250");
329 v.header("region");
330 v.opcode("sample", "../Samples/bobobo/bobobo_bass_vl1_rr2.wav");
331 v.opcode("lorand", "0.250");
332 v.opcode("hirand", "0.500");
333 v.header("region");
334 v.opcode("sample", "../Samples/bobobo/bobobo_bass_vl1_rr3.wav");
335 v.opcode("lorand", "0.500");
336 v.opcode("hirand", "0.750");
337 v.header("region");
338 v.opcode("sample", "../Samples/bobobo/bobobo_bass_vl1_rr4.wav");
339 v.opcode("lorand", "0.750");
340 v.header("master");
341 v.opcode("amplitude_cc35", "100");
342 v.opcode("offset_cc38", "1500");
343 v.opcode("ampeg_sustain_oncc38", "-100");
344 v.header("group");
345 v.opcode("key", "$KEY2");
346 v.header("region");
347 v.opcode("sample", "../Samples/bobobo/bobobo_tenor_l_vl1_rr1.wav");
348 v.opcode("hirand", "0.250");
349 v.header("region");
350 v.opcode("sample", "../Samples/bobobo/bobobo_tenor_l_vl1_rr2.wav");
351 v.opcode("lorand", "0.250");
352 v.opcode("hirand", "0.500");
353 v.header("region");
354 v.opcode("sample", "../Samples/bobobo/bobobo_tenor_l_vl1_rr3.wav");
355 v.opcode("lorand", "0.500");
356 v.opcode("hirand", "0.750");
357 v.header("region");
358 v.opcode("sample", "../Samples/bobobo/bobobo_tenor_l_vl1_rr4.wav");
359 v.opcode("lorand", "0.750");
360
361 v.status(STATUS_EOF);
362 v.close();
363 }
364
365 void check_special_cases()
366 {
367 static const char *text =
368 "<region>\n"
369 "sample=path/to//sample/1 opcode=value1\n"
370 "sample=path to sample 2 opcode=value2\n"
371 "#include \"some file.sfz\"\n"
372 "sample=path to sample 3 // opcode=value3\n"
373 "sample=path to sample 4 opcode=value4 #include \"some file 2.sfz\" // comment\n"
374 "//sample=path to sample 5 opcode=value5\r\n";
375
376 printf("Checking special cases...\n");
377
378 Verifier v(this);
379 v.wrap(text);
380 v.header("region");
381 v.opcode("sample", "path/to//sample/1");
382 v.opcode("opcode", "value1");
383 v.opcode("sample", "path to sample 2");
384 v.opcode("opcode", "value2");
385 v.include("some file.sfz");
386 v.opcode("sample", "path to sample 3 //");
387 v.opcode("opcode", "value3");
388 v.opcode("sample", "path to sample 4");
389 v.opcode("opcode", "value4");
390 v.include("some file 2.sfz");
391 v.comment(" comment");
392 v.comment("sample=path to sample 5 opcode=value5");
393 v.status(STATUS_EOF);
394 v.close();
395 }
396
397 void check_blob_sfz()
398 {
399 static const char *text =
400 "<sample> name=foo.wav\n"
401 "some_opcode=value\n"
402 "data=gggJ~\x92\x93\x9dJ\x93\x9dJ\x8bJ\x9e\x8f\x9d\x9eJlvylJggg7444oyp7474$\r\n"
403 "some_opcode2=value2\n"
404 "<region> sample=foo.wav pitch_keycenter=69\r\n";
405
406 static const char *blob =
407 "=== This is a test BLOB ===\r\n"
408 "\n"
409 "\n"
410 "EOF\r\n"
411 "\r\n";
412
413 printf("Checking built-in blob case...\n");
414
415 Verifier v(this);
416 v.wrap(text);
417 v.header("sample");
418 v.opcode("some_opcode", "value");
419 v.opcode("some_opcode2", "value2");
420 v.sample("foo.wav", blob);
421 v.header("region");
422 v.opcode("sample", "foo.wav");
423 v.opcode("pitch_keycenter", "69");
424 v.status(STATUS_EOF);
425 v.close();
426 }
427
428 void check_blob_sfz2()
429 {
430 static const char *text =
431 "<sample> name=foo.wav\n"
432 "some_opcode=value\n"
433 "data=gggJ~\x92\x93\x9dJ\x93\x9dJ\x8bJ\x9e\x8f\x9d\x9eJlvylJggg7444oyp7474$\r\n"
434 "some_opcode2=value2\n"
435 "<sample> name=bar.wav\n"
436 "some_opcode=value2\n"
437 "data=gggJ~\x92\x93\x9dJ\x94\x9eJ\x8bJ\x9e\x8f\x9d\x9eJlvylJggg7444oyp7474$\r\n"
438 "some_opcode2=value3\n"
439 "<region> sample=foo.wav pitch_keycenter=69\r\n"
440 "<region> sample=bar.wav pitch_keycenter=90\r\n";
441
442 static const char *blob1 =
443 "=== This is a test BLOB ===\r\n"
444 "\n"
445 "\n"
446 "EOF\r\n"
447 "\r\n";
448
449 static const char *blob2 =
450 "=== This jt a test BLOB ===\r\n"
451 "\n"
452 "\n"
453 "EOF\r\n"
454 "\r\n";
455
456 printf("Checking built-in blob case...\n");
457
458 Verifier v(this);
459 v.wrap(text);
460 v.header("sample");
461 v.opcode("some_opcode", "value");
462 v.opcode("some_opcode2", "value2");
463 v.sample("foo.wav", blob1);
464 v.header("sample");
465 v.opcode("some_opcode", "value2");
466 v.opcode("some_opcode2", "value3");
467 v.sample("bar.wav", blob2);
468 v.header("region");
469 v.opcode("sample", "foo.wav");
470 v.opcode("pitch_keycenter", "69");
471 v.header("region");
472 v.opcode("sample", "bar.wav");
473 v.opcode("pitch_keycenter", "90");
474 v.status(STATUS_EOF);
475 v.close();
476 }
477
478 void check_parse_file()
479 {
480 printf("Checking file parse...\n");
481
482 Verifier v(this);
483 v.open("fmt/sfz/example.sfz");
484 v.header("global");
485 v.opcode("loop_mode", "one_shot");
486 v.opcode("seq_length", "4");
487
488 v.header("group");
489 v.opcode("key", "36");
490 v.opcode("hivel", "31");
491 v.opcode("amp_velcurve_31", "1");
492
493 v.header("region");
494 v.opcode("seq_position", "1");
495 v.opcode("sample", "kick_vl1_rr1.wav");
496
497 v.header("region");
498 v.opcode("seq_position", "2");
499 v.opcode("sample", "kick_vl1_rr2.wav");
500
501 v.header("region");
502 v.opcode("seq_position", "3");
503 v.opcode("sample", "kick_vl1_rr3.wav");
504
505 v.header("region");
506 v.opcode("seq_position", "4");
507 v.opcode("sample", "kick_vl1_rr4.wav");
508
509 v.header("group");
510 v.opcode("key", "36");
511 v.opcode("lovel", "32");
512 v.opcode("hivel", "63");
513 v.opcode("amp_velcurve_63", "1");
514
515 v.header("region");
516 v.opcode("seq_position", "1");
517 v.opcode("sample", "kick_vl2_rr1.wav");
518
519 v.header("region");
520 v.opcode("seq_position", "2");
521 v.opcode("sample", "kick_vl2_rr2.wav");
522
523 v.header("region");
524 v.opcode("seq_position", "3");
525 v.opcode("sample", "kick_vl2_rr3.wav");
526
527 v.header("region");
528 v.opcode("seq_position", "4");
529 v.opcode("sample", "kick_vl2_rr4.wav");
530
531 v.header("group");
532 v.opcode("key", "36");
533 v.opcode("lovel", "64");
534 v.opcode("hivel", "95");
535 v.opcode("amp_velcurve_95", "1");
536
537 v.header("region");
538 v.opcode("seq_position", "1");
539 v.opcode("sample", "kick_vl3_rr1.wav");
540
541 v.header("region");
542 v.opcode("seq_position", "2");
543 v.opcode("sample", "kick_vl3_rr2.wav");
544
545 v.header("region");
546 v.opcode("seq_position", "3");
547 v.opcode("sample", "kick_vl3_rr3.wav");
548
549 v.header("region");
550 v.opcode("seq_position", "4");
551 v.opcode("sample", "kick_vl3_rr4.wav");
552
553 v.header("group");
554 v.opcode("key", "36");
555 v.opcode("lovel", "96");
556 v.opcode("seq_length", "3");
557
558 v.header("region");
559 v.opcode("seq_position", "1");
560 v.opcode("sample", "kick_vl4_rr1.wav");
561
562 v.header("region");
563 v.opcode("seq_position", "2");
564 v.opcode("sample", "kick_vl4_rr2.wav");
565
566 v.header("region");
567 v.opcode("seq_position", "3");
568 v.opcode("sample", "kick_vl4_rr3.wav");
569
570 v.status(STATUS_EOF);
571 v.close();
572 }
573
574 UTEST_MAIN
575 {
576 check_valid_sfz1();
577 check_valid_sfz2();
578 check_valid_sfz3();
579 check_valid_sfz4();
580 check_special_cases();
581 check_blob_sfz();
582 check_blob_sfz2();
583 check_parse_file();
584 }
585
586 UTEST_END
587
588
589