1 /*
2   ZynAddSubFX - a software synthesizer
3 
4   PluginTest.h - CxxTest for embedding zyn
5   Copyright (C) 2013-2013 Mark McCurry
6   Authors: Mark McCurry
7 
8   This program is free software; you can redistribute it and/or
9   modify it under the terms of the GNU General Public License
10   as published by the Free Software Foundation; either version 2
11   of the License, or (at your option) any later version.
12 */
13 #include "test-suite.h"
14 #include <cmath>
15 #include <cstdlib>
16 #include <iostream>
17 #include <fstream>
18 #include <string>
19 #include "../Misc/MiddleWare.h"
20 #include "../Misc/Master.h"
21 #include "../Misc/PresetExtractor.h"
22 #include "../Misc/PresetExtractor.cpp"
23 #include "../Misc/Util.h"
24 #include "../globals.h"
25 #include "../UI/NSM.H"
26 using namespace std;
27 using namespace zyn;
28 
29 NSM_Client *nsm = 0;
30 MiddleWare *middleware = 0;
31 
32 char *instance_name=(char*)"";
33 
34 #define NUM_MIDDLEWARE 3
35 
36 class PluginTest
37 {
38     public:
39         Config config;
setUp()40         void setUp() {
41             synth = new SYNTH_T;
42             synth->buffersize = 256;
43             synth->samplerate = 48000;
44             //synth->alias();
45 
46             outL  = new float[1024];
47             for(int i = 0; i < synth->buffersize; ++i)
48                 outL[i] = 0.0f;
49             outR = new float[1024];
50             for(int i = 0; i < synth->buffersize; ++i)
51                 outR[i] = 0.0f;
52 
53             delete synth;
54             synth = NULL;
55             for(int i = 0; i < NUM_MIDDLEWARE; ++i) {
56                 synth = new SYNTH_T;
57                 synth->buffersize = 256;
58                 synth->samplerate = 48000;
59                 //synth->alias();
60                 middleware[i] = new MiddleWare(std::move(*synth), &config);
61                 master[i] = middleware[i]->spawnMaster();
62                 //printf("Octave size = %d\n", master[i]->microtonal.getoctavesize());
63             }
64         }
65 
tearDown()66         void tearDown() {
67             for(int i = 0; i < NUM_MIDDLEWARE; ++i)
68                 delete middleware[i];
69 
70             delete[] outL;
71             delete[] outR;
72             delete synth;
73         }
74 
75 
testInit()76         void testInit() {
77 
78             for(int x=0; x<100; ++x) {
79                 for(int i=0; i<NUM_MIDDLEWARE; ++i) {
80                     middleware[i]->tick();
81                     master[i]->GetAudioOutSamples(rand()%1025,
82                             synth->samplerate, outL, outR);
83                 }
84             }
85         }
86 
testPanic()87         void testPanic()
88         {
89             master[0]->setController(0, 0x64, 0);
90             master[0]->noteOn(0,64,64);
91             master[0]->AudioOut(outL, outR);
92 
93             float sum = 0.0f;
94             for(int i = 0; i < synth->buffersize; ++i)
95                 sum += fabsf(outL[i]);
96 
97             TS_ASSERT(0.1f < sum);
98         }
99 
loadfile(string fname) const100         string loadfile(string fname) const
101         {
102             std::ifstream t(fname.c_str());
103             std::string str((std::istreambuf_iterator<char>(t)),
104                                      std::istreambuf_iterator<char>());
105             return str;
106         }
107 
testLoad(void)108         void testLoad(void)
109         {
110             for(int i=0; i<NUM_MIDDLEWARE; ++i) {
111                 middleware[i]->transmitMsg("/load-part", "is", 0, (string(SOURCE_DIR) + "/../../instruments/banks/Organ/0037-Church Organ 1.xiz").c_str());
112                 middleware[i]->tick();
113                 master[i]->GetAudioOutSamples(synth->buffersize, synth->samplerate, outL, outR);
114                 middleware[i]->tick();
115                 master[i]->GetAudioOutSamples(synth->buffersize, synth->samplerate, outL, outR);
116                 middleware[i]->tick();
117                 master[i]->GetAudioOutSamples(synth->buffersize, synth->samplerate, outL, outR);
118                 middleware[i]->tick();
119                 master[i]->GetAudioOutSamples(synth->buffersize, synth->samplerate, outL, outR);
120                 middleware[i]->tick();
121             }
122             //const string fname = string(SOURCE_DIR) + "/../../instruments/banks/Organ/0037-Church Organ 1.xiz";
123             //const string fdata = loadfile(fname);
124         }
125 
testChangeToOutOfRangeProgram()126         void testChangeToOutOfRangeProgram()
127         {
128             middleware[0]->transmitMsg("/bank/msb", "i", 0);
129             middleware[0]->tick();
130             middleware[0]->transmitMsg("/bank/lsb", "i", 1);
131             middleware[0]->tick();
132             middleware[0]->pendingSetProgram(0, 32);
133             middleware[0]->tick();
134             master[0]->GetAudioOutSamples(synth->buffersize, synth->samplerate, outL, outR);
135             // We should ideally be checking to verify that the part change
136             // didn't happen, but it's not clear how to do that.  We're
137             // currently relying on the assert(filename) in loadPart() failing
138             // if this logic gets broken.
139         }
140 
141     private:
142         SYNTH_T *synth;
143         float *outR, *outL;
144         MiddleWare *middleware[NUM_MIDDLEWARE];
145         Master *master[NUM_MIDDLEWARE];
146 };
147 
main()148 int main()
149 {
150     PluginTest test;
151     RUN_TEST(testInit);
152     RUN_TEST(testPanic);
153     RUN_TEST(testLoad);
154     RUN_TEST(testChangeToOutOfRangeProgram);
155     return test_summary();
156 }
157