File: signal-tester.cpp

package info (click to toggle)
faust 2.79.3%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 397,496 kB
  • sloc: cpp: 278,433; ansic: 116,164; javascript: 18,529; vhdl: 14,052; sh: 13,884; java: 5,900; objc: 3,852; python: 3,222; makefile: 2,655; cs: 1,672; lisp: 1,146; ruby: 954; yacc: 586; xml: 471; lex: 247; awk: 110; tcl: 26
file content (951 lines) | stat: -rw-r--r-- 25,161 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
/************************************************************************
 FAUST Architecture File
 Copyright (C) 2021 GRAME, Centre National de Creation Musicale
 ---------------------------------------------------------------------
 This Architecture section is free software; you can redistribute it
 and/or modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 3 of
 the License, or (at your option) any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program; If not, see <http://www.gnu.org/licenses/>.
 
 EXCEPTION : As a special exception, you may create a larger work
 that contains this FAUST architecture section and distribute
 that work under terms of your choice, so long as this FAUST
 architecture section is not modified.
 
 ************************************************************************/

#include <libgen.h>
#include <iostream>
#include <string>
#include <vector>

#include "faust/dsp/libfaust-signal.h"
#include "faust/dsp/llvm-dsp.h"
#include "faust/dsp/interpreter-dsp.h"
#include "faust/dsp/poly-dsp.h"
#include "faust/audio/jack-dsp.h"
#include "faust/gui/GTKUI.h"
#include "faust/gui/MidiUI.h"
#include "faust/misc.h"

using namespace std;

// Run a DSP with a GUI
static void runAudio(dsp* dsp, const char* name, int argc, char* argv[])
{
    // Allocate audio driver
    jackaudio audio;
    audio.init("Test", dsp);
    
    // Create GUI
    GTKUI gtk_ui = GTKUI((char*)name, &argc, &argv);
    dsp->buildUserInterface(&gtk_ui);
    
    // Start real-time processing
    audio.start();
    
    // Start GUI
    gtk_ui.run();
    
    // Cleanup
    audio.stop();
}

/**
 * Return the current runtime sample rate.
 *
 * Reproduce the 'SR' definition in platform.lib: SR = min(192000.0, max(1.0, fconstant(int fSamplingFreq, <math.h>)));
 *
 * @return the current runtime sample rate.
 */
inline Signal SR()
{
    return sigMin(sigReal(192000.0), sigMax(sigReal(1.0), sigFConst(SType::kSInt, "fSamplingFreq", "<math.h>")));
}

/**
 * Return the current runtime buffer size.
 *
 * Reproduce the 'BS' definition in platform.lib: BS = fvariable(int count, <math.h>);
 *
 * @return the current runtime buffer size.
 */
inline Signal BS()
{
    return sigFVar(SType::kSInt, "count", "<math.h>");
}

/**
* Creates a foreign function*
*/
inline Box SINH()
{
    tvec signals;
    signals.push_back(sigInput(0));
    return sigFFun(SType::kSReal, {"sinhf", "sinh", "sinhl", "sinhfx"}, { SType::kSReal }, "<FOO.h>",  "", signals);
}

#define COMPILER(exp)    \
{                        \
    createLibContext();  \
    exp                  \
    destroyLibContext(); \
}                        \
    
static void compile(const string& name_app, tvec signals, int argc = 0, const char* argv[] = nullptr)
{
    string error_msg, source = createSourceFromSignals(name_app, signals, "cpp", argc, argv, error_msg);
    if (source != "") {
        cout << source;
    } else {
        cerr << error_msg;
    }
}

// process = ffunction(float sinhf|sinh|sinhl|sinhfx(float), <math.h>, "");

static void test0()
{
    COMPILER
    (
        tvec signals;
        signals.push_back(SINH());
     
        compile("test0", signals);
    )
}

// process = 0.5;

static void test1()
{
    COMPILER
    (
        tvec signals;
        signals.push_back(sigReal(0.5));
     
        compile("test1", signals);
    )
}

// process = _ <: +(0.5), *(1.5);

static void test2()
{
    COMPILER
    (
        tvec signals;
        Signal in1 = sigInput(0);
        signals.push_back(sigAdd(in1, sigReal(0.5)));
        signals.push_back(sigMul(in1, sigReal(1.5)));
        
        compile("test2", signals);
     )
}

// process = _ <: @(+(0.5), 500), @(*(1.5), 3000);

static void test3()
{
    COMPILER
    (
        tvec signals;
        Signal in1 = sigInput(0);
        signals.push_back(sigDelay(sigAdd(in1, sigReal(0.5)), sigInt(500)));
        signals.push_back(sigDelay(sigMul(in1, sigReal(1.5)), sigInt(3000)));
         
        compile("test3", signals);
    )
}

// process = _ <: @(500) + 0.5, @(3000) * 1.5;

static void test4()
{
    COMPILER
    (
        tvec signals;
        Signal in1 = sigInput(0);
        signals.push_back(sigAdd(sigDelay(in1, sigInt(500)), sigReal(0.5)));
        signals.push_back(sigMul(sigDelay(in1, sigInt(3000)), sigReal(1.5)));

        compile("test4", signals);
    )
}

// process = _ <: @(+(0.5), 500), sin(@(@(+(0.5), 500), 600));

static void test5()
{
    COMPILER
    (
        tvec signals;
        Signal in1 = sigInput(0);
        signals.push_back(sigDelay(sigAdd(in1, sigReal(0.5)), sigInt(500)));
        signals.push_back(sigSin(sigDelay(sigDelay(sigAdd(in1, sigReal(0.5)), sigInt(500)), sigInt(600))));
        
        compile("test5", signals);
    )
}

// process = _ <: @(+(0.5), 500), @(*(1.5), 3000);

static void test6()
{
    createLibContext();
    
    tvec signals;
    Signal in1 = sigInput(0);
    signals.push_back(sigDelay(sigAdd(in1, sigReal(0.5)), sigInt(500)));
    signals.push_back(sigDelay(sigMul(in1, sigReal(1.5)), sigInt(3000)));
    
    // Vector compilation
    const char* argv[] = { "-vec", "-lv", "1" , "-double" };
    compile("test6", signals, 4, argv);

    destroyLibContext();
}

// process = _ <: @(+(0.5), 500), atan2(@(*(1.5), 3000), 0.5);

static void test7()
{
    COMPILER
    (
        tvec signals;
        Signal in1 = sigInput(0);
        signals.push_back(sigDelay(sigAdd(in1, sigReal(0.5)), sigInt(500)));
        signals.push_back(sigAtan2(sigDelay(sigMul(in1, sigReal(1.5)), sigInt(3000)), sigReal(0.5)));

        compile("test7", signals);
    )
}

// Equivalent signal expressions

static void equivalent1()
{
    COMPILER
    (
         tvec signals;
         Signal s1 = sigAdd(sigDelay(sigInput(0), sigInt(500)), sigReal(0.5));
         signals.push_back(s1);
         signals.push_back(s1);
         
         compile("equivalent1", signals);
     
         // Print the signals
         cout << "\nPrint the signals\n";
         for (size_t i = 0; i < signals.size(); i++) {
             cout << printSignal(signals[i], false, INT_MAX);
         }
     )
}

static void equivalent2()
{
    COMPILER
    (
         tvec signals;
         signals.push_back(sigAdd(sigDelay(sigInput(0), sigInt(500)), sigReal(0.5)));
         signals.push_back(sigAdd(sigDelay(sigInput(0), sigInt(500)), sigReal(0.5)));
     
         compile("equivalent2", signals);
     
         // Print the signals
         cout << "\nPrint the signals\n";
         for (size_t i = 0; i < signals.size(); i++) {
             cout << printSignal(signals[i], false, INT_MAX);
         }
    )
}

// Signals in normal form

static void normalform()
{
    COMPILER
    (
        tvec signals;
        signals.push_back(sigAdd(sigAdd(sigDelay(sigDelay(sigInput(0), sigReal(500)), sigReal(200)), sigReal(0.5)), sigReal(3)));
        signals.push_back(sigMul(sigMul(sigDelay(sigInput(0), sigInt(500)), sigReal(0.5)), sigReal(4)));

        compile("normalform", signals);

        // Print the signals
        cout << "\nPrint the signals\n";
        for (size_t i = 0; i < signals.size(); i++) {
            cout << printSignal(signals[i], false, INT_MAX);
        }
     
        cout << "\nPrint the signals in short form\n";
        for (size_t i = 0; i < signals.size(); i++) {
             cout << printSignal(signals[i], false, 128);
        }

        cout << "\nPrint the signals in shared form\n";
        for (size_t i = 0; i < signals.size(); i++) {
            cout << printSignal(signals[i], true, INT_MAX);
        }

        // Compute normal form
        tvec nf = simplifyToNormalForm2(signals);
     
        cout << "\nPrint the signals in normal form\n";
        for (size_t i = 0; i < nf.size(); i++) {
            cout << printSignal(nf[i], false, INT_MAX);
        }
     
        cout << "\nPrint the signals in short form\n";
        for (size_t i = 0; i < nf.size(); i++) {
            cout << printSignal(nf[i], false, 128);
        }

        cout << "\nPrint the signals in normal form in shared mode\n";
        for (size_t i = 0; i < nf.size(); i++) {
            cout << printSignal(nf[i], true, INT_MAX);
        }
     )
}

static void intervals()
{
    COMPILER
    (
        tvec signals;
     
        Signal s1 = sigAdd(sigAdd(sigDelay(sigDelay(sigInput(0), sigReal(500)), sigReal(200)), sigReal(0.5)), sigReal(3));
        Signal s2 = sigMul(sigMul(sigDelay(sigInput(0), sigInt(500)), sigReal(0.5)), sigReal(4));
        signals.push_back(s1);
        signals.push_back(s2);
         
        compile("intervals1", signals);
     
        Interval i1 = getSigInterval(s1);
        Interval i2 = getSigInterval(s2);
     
        cout << i1 << endl;
        cout << i2 << endl;
     
        Interval i3(48);
        Interval i4(-10, 10, 48);
        setSigInterval(s1, i3);
        setSigInterval(s2, i4);
     
        // Compute normal form
        tvec nf = simplifyToNormalForm2(signals);
         
        cout << "\nPrint the signals in normal form\n";
        for (size_t i = 0; i < nf.size(); i++) {
            cout << printSignal(nf[i], false, INT_MAX);
        }
         
        cout << "\nPrint the signals in short form\n";
        for (size_t i = 0; i < nf.size(); i++) {
            cout << printSignal(nf[i], false, 128);
        }
         
        cout << "\nPrint the signals in normal form in shared mode\n";
        for (size_t i = 0; i < nf.size(); i++) {
            cout << printSignal(nf[i], true, INT_MAX);
        }
     
        compile("intervals2", signals);
     )
}

// process = @(+(0.5), 500) * vslider("Vol", 0.5, 0, 1, 0.01);

static void test8()
{
    COMPILER
    (
        tvec signals;
        Signal in1 = sigInput(0);
        Signal slider = sigVSlider("Vol", sigReal(0.5), sigReal(0.0), sigReal(1.0), sigReal(0.01));
        signals.push_back(sigMul(slider, sigDelay(sigAdd(in1, sigReal(0.5)), sigInt(500))));
        
        compile("test8", signals);
    )
}

/*
import("stdfaust.lib");
 
freq = vslider("h:Oscillator/freq", 440, 50, 1000, 0.1);
gain = vslider("h:Oscillator/gain", 0, 0, 1, 0.01);
 
process = freq * gain;
*/

static void test9()
{
    COMPILER
    (
        tvec signals;
        Signal freq = sigVSlider("h:Oscillator/freq", sigReal(440), sigReal(50), sigReal(1000), sigReal(0.1));
        Signal gain = sigVSlider("h:Oscillator/gain", sigReal(0), sigReal(0), sigReal(1), sigReal(0.011));
        signals.push_back(sigMul(freq, sigMul(gain, sigInput(0))));
     
        compile("test9", signals);
     )
}

// process = + ~ _;

static void test10()
{
    COMPILER
    (
        tvec signals;
        Signal in1 = sigInput(0);
        signals.push_back(sigRecursion(sigAdd(sigSelf(), in1)));
        
        compile("test10", signals);
    )
}

// Alternate way of writing
static void test10bis()
{
    COMPILER
    (
        Signal in1 = sigInput(0);
        tvec ins;
        ins.push_back(sigAdd(sigSelfN(0), in1));
        tvec outs = sigRecursionN(ins);
     
        compile("test10bis", outs);
    )
}

// mutual recursion

/*
process = (cross : (+,+)) ~ (*(0.9),*(0.5))
with {
    // Route a,b, (recursive) and c,d (inputs) signals
    cross(a,b,c,d) = d,b,c,a;
};
*/

static void test10ter()
{
    COMPILER
    (
        Signal in0 = sigInput(0);
        Signal in1 = sigInput(1);
        tvec ins;
        ins.push_back(sigAdd(sigMul(sigSelfN(1), sigReal(0.5)), in0));
        ins.push_back(sigAdd(sigMul(sigSelfN(0), sigReal(0.9)), in1));
        tvec outs = sigRecursionN(ins);
        
        compile("test10ter", outs);
     )
}


// import("stdfaust.lib");
// process = ma.SR, ma.BS;

static void test11()
{
    COMPILER
    (
        tvec signals;
        signals.push_back(SR());
        signals.push_back(BS());
        
        compile("test11", signals);
    )
}

// process = waveform { 0, 100, 200, 300, 400 };

static void test12()
{
    COMPILER
    (
        tvec waveform;
        // Fill the waveform content vector
        for (int i = 0; i < 5; i++) {
            waveform.push_back(sigReal(100*i));
        }
        tvec signals;
        signals.push_back(sigInt(waveform.size())); // the waveform size
        signals.push_back(sigWaveform(waveform));   // the waveform content
        
        compile("test12", signals);
     )
}

// process = waveform { 100+0, 100+100, 100+200, 100+300, 100+400 }; ==> failure

static void test13()
{
    COMPILER
    (
        tvec waveform;
        for (int i = 0; i < 5; i++) {
            waveform.push_back(sigAdd(sigReal(100), sigReal(100*i)));
        }
        tvec signals;
        signals.push_back(sigInt(waveform.size())); // the waveform size
        signals.push_back(sigWaveform(waveform));   // the waveform content
        
        compile("test13", signals);
     )
}

// process = _ <: +;

static void test14()
{
    COMPILER
    (
        tvec signals;
        Signal in1 = sigInput(0);
        signals.push_back(sigAdd(in1, in1));
     
        compile("test14", signals);
    )
}

// process = _,_ <: !,_,_,! :> _,_;

static void test15()
{
    COMPILER
    (
        tvec signals;
        Signal in1 = sigInput(0);
        Signal in2 = sigInput(1);
        signals.push_back(in2);
        signals.push_back(in1);
         
        compile("test15", signals);
    )
}

// process = _,_,_,_ : _,!,!,_;

static void test16()
{
    COMPILER
    (
        tvec signals;
        Signal in1 = sigInput(0);
        Signal in3 = sigInput(3);
        signals.push_back(in1);
        signals.push_back(in3);
         
        compile("test16", signals);
    )
}

/*
 import("stdfaust.lib");
 process = phasor(440)
 with {
     decimalpart(x) = x-int(x);
     phasor(f) = f/ma.SR : (+ : decimalpart) ~ _;
 };
 */

static Signal decimalpart(Signal x)
{
    return sigSub(x, sigIntCast(x));
}

static Signal phasor(Signal f)
{
    return sigRecursion(decimalpart(sigAdd(sigSelf(), sigDiv(f, SR()))));
}

static void test17()
{
    COMPILER
    (
        tvec signals;
        signals.push_back(phasor(sigReal(440.0)));

        compile("test17", signals);
    )
}

/*
 import("stdfaust.lib");
 process = osc(440), osc(440)
 with {
    decimalpart(x) = x-int(x);
    phasor(f) = f/ma.SR : (+ : decimalpart) ~ _;
    osc(f) = sin(2 * ma.PI * phasor(f));
 };
 */

static Signal osc(Signal f)
{
    return sigSin(sigMul(phasor(f), sigMul(sigReal(2.0), sigReal(3.141592653))));
}

static void test18()
{
    COMPILER
    (
        tvec signals;
        signals.push_back(osc(sigReal(440.0)));
        signals.push_back(osc(sigReal(440.0)));

        compile("test18", signals);
    )
}

// process = 0,0 : soundfile("sound[url:{'tango.wav'}]", 2);

static void test19()
{
    COMPILER
    (
         tvec signals;
         // Soundfile definition
         Signal sf = sigSoundfile("sound[url:{'tango.wav'}]");
         // Simple read index of 0
         Signal rdx = sigInt(0);
         // Part 0
         Signal part = sigInt(0);
         // Wrapped index to avoid reading outside the buffer
         Signal wridx = sigIntCast(sigMax(sigInt(0), sigMin(rdx, sigSub(sigSoundfileLength(sf, sigInt(0)), sigInt(1)))));
         // Accessing part 0 length
         signals.push_back(sigSoundfileLength(sf, part));
         // Accessing part 0 SR
         signals.push_back(sigSoundfileRate(sf, part));
         // Accessing chan 0 and part 0, with a wrapped read index
         signals.push_back(sigSoundfileBuffer(sf, sigInt(0), part, wridx));
         // Accessing chan 1 and part 0, with a wrapped read index
         signals.push_back(sigSoundfileBuffer(sf, sigInt(1), part, wridx));
     
         compile("test19", signals);
     )
}

// process = 10,1,int(_) : rdtable;

static void test20()
{
    COMPILER
    (
        tvec signals;
        signals.push_back(sigReadOnlyTable(sigInt(10), sigInt(1), sigIntCast(sigInput(0))));

        compile("test20", signals);
    )
}

// process = 10,1,int(_),int(_),int(_) : rwtable;

static void test21()
{
    COMPILER
    (
         tvec signals;
         signals.push_back(sigWriteReadTable(sigInt(10), sigInt(1), sigIntCast(sigInput(0)), sigIntCast(sigInput(1)), sigIntCast(sigInput(2))));
     
         compile("test21", signals);
     )
}

/*
 import("stdfaust.lib");
 process = osc(f1), osc(f2)
 with {
    decimalpart(x) = x-int(x);
    phasor(f) = f/ma.SR : (+ : decimalpart) ~ _;
    osc(f) = sin(2 * ma.PI * phasor(f));
    f1 = vslider("Freq1", 300, 100, 2000, 0.01);
    f2 = vslider("Freq2", 500, 100, 2000, 0.01);
 };
 */

// Using the LLVM backend.
static void test22(int argc, char* argv[])
{
    cout << "test22\n";
    string error_msg;
    llvm_dsp_factory* factory = nullptr;
    
    createLibContext();
    {
        tvec signals;
        signals.push_back(osc(sigVSlider("h:Oscillator/Freq1", sigReal(300), sigReal(100), sigReal(2000), sigReal(0.01))));
        signals.push_back(osc(sigVSlider("h:Oscillator/Freq2", sigReal(500), sigReal(100), sigReal(2000), sigReal(0.01))));
     
        factory = createDSPFactoryFromSignals("FaustDSP", signals, 0, nullptr, "", error_msg);
    }
    destroyLibContext();
    
    // The factory can be used outside of the createLibContext/destroyLibContext scope
    if (factory) {
        dsp* dsp = factory->createDSPInstance();
        assert(dsp);
        runAudio(dsp, "Organ", argc, argv);
        delete dsp;
        deleteDSPFactory(factory);
    } else {
        cerr << "Cannot create factory" << error_msg << endl;
    }
}

static void test22bis(int argc, char* argv[])
{
    cout << "test22\n";
    string error_msg;
    llvm_dsp_factory* factory1 = nullptr;
    llvm_dsp_factory* factory2 = nullptr;
    
    createLibContext();
    {
        {
            tvec signals;
            signals.push_back(osc(sigVSlider("h:Oscillator/Freq1", sigReal(300), sigReal(100), sigReal(2000), sigReal(0.01))));
            signals.push_back(osc(sigVSlider("h:Oscillator/Freq2", sigReal(500), sigReal(100), sigReal(2000), sigReal(0.01))));
            factory1 = createDSPFactoryFromSignals("FaustDSP1", signals, 0, nullptr, "", error_msg);
        }
        
        {
            tvec signals;
            signals.push_back(osc(sigVSlider("h:Oscillator/Foo1", sigReal(200), sigReal(100), sigReal(2000), sigReal(0.01))));
            signals.push_back(osc(sigVSlider("h:Oscillator/Foo2", sigReal(700), sigReal(100), sigReal(2000), sigReal(0.01))));
            factory2 = createDSPFactoryFromSignals("FaustDSP2", signals, 0, nullptr, "", error_msg);
        }
    }
    destroyLibContext();
    
    // The factories can be used outside of the createLibContext/destroyLibContext scope
    if (factory1 && factory2) {
        {
            dsp* dsp = factory1->createDSPInstance();
            assert(dsp);
            runAudio(dsp, "Organ1", argc, argv);
            delete dsp;
        }
        {
            dsp* dsp = factory2->createDSPInstance();
            assert(dsp);
            runAudio(dsp, "Organ2", argc, argv);
            delete dsp;
        }
        deleteDSPFactory(factory1);
        deleteDSPFactory(factory2);
    } else {
        cerr << "Cannot create factories" << error_msg << endl;
    }
}

// Using the Interpreter backend.
static void test23(int argc, char* argv[])
{
    cout << "test23\n";
    interpreter_dsp_factory* factory = nullptr;
    string error_msg;
    
    createLibContext();
    {
        tvec signals;
        signals.push_back(osc(sigHSlider("v:Oscillator/Freq1", sigReal(300), sigReal(100), sigReal(2000), sigReal(0.01))));
        signals.push_back(osc(sigHSlider("v:Oscillator/Freq2", sigReal(500), sigReal(100), sigReal(2000), sigReal(0.01))));
    
        factory = createInterpreterDSPFactoryFromSignals("FaustDSP", signals, 0, nullptr, error_msg);
    }
    destroyLibContext();
    
    // The factory can be used outside of the createLibContext/destroyLibContext scope
    if (factory) {
        dsp* dsp = factory->createDSPInstance();
        assert(dsp);
        runAudio(dsp, "Organ", argc, argv);
        delete dsp;
        deleteInterpreterDSPFactory(factory);
    } else {
        cerr << "Cannot create factory" << error_msg << endl;
    }
}

/*
import("stdfaust.lib");
process = organ, organ
with {
    decimalpart(x) = x-int(x);
    phasor(f) = f/ma.SR : (+ : decimalpart) ~ _;
    osc(f) = sin(2 * ma.PI * phasor(f));
    freq = nentry("freq", 100, 100, 3000, 0.01);
    gate = button("gate");
    gain = nentry("gain", 0.5, 0, 1, 0.01);
    organ = gate * (osc(freq) * gain + osc(2 * freq) * gain);
};
*/

// Simple polyphonic DSP.
static void test24(int argc, char* argv[])
{
    cout << "test24\n";
    interpreter_dsp_factory* factory = nullptr;
    string error_msg;
    
    createLibContext();
    {
        tvec signals;
    
        // Follow the freq/gate/gain convention, see: https://faustdoc.grame.fr/manual/midi/#standard-polyphony-parameters
        Signal freq = sigNumEntry("freq", sigReal(100), sigReal(100), sigReal(3000), sigReal(0.01));
        Signal gate = sigButton("gate");
        Signal gain = sigNumEntry("gain", sigReal(0.5), sigReal(0), sigReal(1), sigReal(0.01));
        Signal organ = sigMul(gate, sigAdd(sigMul(osc(freq), gain), sigMul(osc(sigMul(freq, sigInt(2))), gain)));
        // Stereo
        signals.push_back(organ);
        signals.push_back(organ);
    
        factory = createInterpreterDSPFactoryFromSignals("FaustDSP", signals, 0, nullptr, error_msg);
    }
    destroyLibContext();
    
    // The factory can be used outside of the createLibContext/destroyLibContext scope
    if (factory) {
        dsp* dsp = factory->createDSPInstance();
        assert(dsp);
        
        // Allocate polyphonic DSP
        dsp = new mydsp_poly(dsp, 8, true, true);
        
        // Allocate MIDI/audio driver
        jackaudio_midi audio;
        audio.init("Organ", dsp);
        
        // Create GUI
        GTKUI gtk_ui = GTKUI((char*)"Organ", &argc, &argv);
        dsp->buildUserInterface(&gtk_ui);
        
        // Create MIDI controller
        MidiUI midi_ui = MidiUI(&audio);
        dsp->buildUserInterface(&midi_ui);
        
        // Start real-time processing
        audio.start();
        
        // Start MIDI
        midi_ui.run();
        
        // Start GUI
        gtk_ui.run();
        
        // Cleanup
        audio.stop();
        delete dsp;
        deleteInterpreterDSPFactory(factory);
    } else {
        cerr << "Cannot create factory" << error_msg << endl;
    }
}

// Compile a complete DSP program to a box expression, then a list of signals, then to a source string
// in several target languages
static void test25()
{
    cout << "test25\n";
    vector<const char*> lang = { "c", "cpp", "cmajor", "codebox", "csharp", "dlang", "fir", "interp", "jax", "jsfx", "julia", "rust", "wast" };
    // Context has to be created/destroyed each time
    for (const auto& it : lang) {
        createLibContext();
        {
            int inputs = 0;
            int outputs = 0;
            string error_msg;
        
            // Create the oscillator
            Box osc = DSPToBoxes("FaustDSP", "import(\"stdfaust.lib\"); process = os.osc(440);", 0, nullptr, &inputs, &outputs, error_msg);
            if (!osc) {
                cerr << error_msg;
                destroyLibContext();
                return;
            }
        
            // Convert to signals
            tvec signals = boxesToSignals(osc, error_msg);
            if (signals.size() == 0) {
                cerr << error_msg;
                destroyLibContext();
                return;
            }
        
            // Compile signals to the target language
            string source = createSourceFromSignals("FaustDSP", signals, it,  0, nullptr, error_msg);
            if (source != "") {
                cout << source;
            } else {
                cerr << error_msg;
            }
        }
        destroyLibContext();
    }
}


list<GUI*> GUI::fGuiList;
ztimedmap GUI::gTimedZoneMap;

int main(int argc, char* argv[])
{
    test0();
    test1();
    test2();
    test3();
    test4();
    test5();
    test6();
    test7();
    equivalent1();
    equivalent2();
    normalform();
    intervals();
    test8();
    test9();
    test10();
    test10bis();
    test10ter();
    test11();
    test12();
    test13();
    test14();
    test15();
    test16();
    test17();
    test18();
    test19();
    test20();
    test21();
    
    // Test with audio, GUI and LLVM backend
    test22(argc, argv);
    
    // Test with audio, GUI and LLVM backend and 2 factories/DSP
    test22bis(argc, argv);
    
    // Test with audio, GUI and Interp backend
    test23(argc, argv);
    
    // Test with audio, GUI, MIDI and Interp backend
    test24(argc, argv);
    
    // Test 'DSPToBoxes/boxesToSignals/createSourceFromSignals' API
    test25();
    
    return 0;
}