Add python3 to github action test deps
[zynaddsubfx-code.git] / src / Tests / SubNoteTest.h
blob5cde444068b43ca75368c32763d7773b92cf1463
1 /*
2 ZynAddSubFX - a software synthesizer
4 AdNoteTest.h - CxxTest for Synth/SUBnote
5 Copyright (C) 2009-2011 Mark McCurry
6 Author: Mark McCurry
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.
14 //Based Upon AdNoteTest.h
15 #include <cxxtest/TestSuite.h>
16 #include <iostream>
17 #include <fstream>
18 #include <ctime>
19 #include <string>
20 #include "../Misc/Master.h"
21 #include "../Misc/Allocator.h"
22 #include "../Misc/Util.h"
23 #include "../Misc/XMLwrapper.h"
24 #include "../Synth/SUBnote.h"
25 #include "../Params/SUBnoteParameters.h"
26 #include "../Params/Presets.h"
27 #include "../globals.h"
28 #include <rtosc/thread-link.h>
30 using namespace std;
31 using namespace zyn;
33 SYNTH_T *synth;
35 class SubNoteTest:public CxxTest::TestSuite
37 public:
39 SUBnoteParameters *pars;
40 SUBnote *note;
41 Master *master;
42 AbsTime *time;
43 Controller *controller;
44 float test_freq_log2;
45 Alloc memory;
46 rtosc::ThreadLink *tr;
47 WatchManager *w;
50 float *outR, *outL;
52 void setUp() {
53 synth = new SYNTH_T;
54 //First the sensible settings and variables that have to be set:
55 synth->buffersize = 256;
56 time = new AbsTime(*synth);
58 outL = new float[synth->buffersize];
59 for(int i = 0; i < synth->buffersize; ++i)
60 *(outL + i) = 0;
61 outR = new float[synth->buffersize];
62 for(int i = 0; i < synth->buffersize; ++i)
63 *(outR + i) = 0;
65 tr = new rtosc::ThreadLink(1024,3);
66 w = new WatchManager(tr);
68 //prepare the default settings
69 SUBnoteParameters *defaultPreset = new SUBnoteParameters(time);
70 XMLwrapper wrap;
71 wrap.loadXMLfile(string(SOURCE_DIR)
72 + string("/guitar-adnote.xmz"));
73 TS_ASSERT(wrap.enterbranch("MASTER"));
74 TS_ASSERT(wrap.enterbranch("PART", 1));
75 TS_ASSERT(wrap.enterbranch("INSTRUMENT"));
76 TS_ASSERT(wrap.enterbranch("INSTRUMENT_KIT"));
77 TS_ASSERT(wrap.enterbranch("INSTRUMENT_KIT_ITEM", 0));
78 TS_ASSERT(wrap.enterbranch("SUB_SYNTH_PARAMETERS"));
79 defaultPreset->getfromXML(wrap);
81 controller = new Controller(*synth, time);
83 //lets go with.... 50! as a nice note
84 test_freq_log2 = log2f(440.0f) + (50.0 - 69.0f) / 12.0f;
86 SynthParams pars{memory, *controller, *synth, *time, 120, 0, test_freq_log2, false, prng()};
87 note = new SUBnote(defaultPreset, pars, w);
88 this->pars = defaultPreset;
91 void tearDown() {
92 delete controller;
93 delete note;
94 delete [] outL;
95 delete [] outR;
96 delete time;
97 delete synth;
98 delete pars;
101 void testDefaults() {
102 //Note: if these tests fail it is due to the relationship between
103 //global.h::RND and SUBnote.cpp
105 int sampleCount = 0;
107 //#define WRITE_OUTPUT
109 #ifdef WRITE_OUTPUT
110 ofstream file("subnoteout", ios::out);
111 #endif
112 note->noteout(outL, outR);
113 #ifdef WRITE_OUTPUT
114 for(int i = 0; i < synth->buffersize; ++i)
115 file << outL[i] << std::endl;
117 #endif
118 sampleCount += synth->buffersize;
120 TS_ASSERT_DELTA(outL[255], 0.0010f, 0.0001f);
122 note->releasekey();
124 TS_ASSERT(!tr->hasNext());
125 w->add_watch("noteout/filter");
127 note->noteout(outL, outR);
128 sampleCount += synth->buffersize;
129 TS_ASSERT_DELTA(outL[255], 0.0114f, 0.0001f);
130 w->tick();
132 note->noteout(outL, outR);
133 sampleCount += synth->buffersize;
134 TS_ASSERT_DELTA(outL[255], -0.0014f, 0.0001f);
135 w->tick();
137 TS_ASSERT(tr->hasNext());
138 TS_ASSERT_EQUALS(string("noteout/filter"), tr->read());
139 TS_ASSERT(!tr->hasNext());
141 w->add_watch("noteout/amp_int");
142 note->noteout(outL, outR);
143 sampleCount += synth->buffersize;
144 TS_ASSERT_DELTA(outL[255], -0.0031f, 0.0001f);
145 w->tick();
147 note->noteout(outL, outR);
148 sampleCount += synth->buffersize;
149 TS_ASSERT_DELTA(outL[255], -0.0013f, 0.0001f);
150 w->tick();
151 TS_ASSERT(tr->hasNext());
152 TS_ASSERT_EQUALS(string("noteout/amp_int"), tr->read());
153 TS_ASSERT(!tr->hasNext());
155 while(!note->finished()) {
156 note->noteout(outL, outR);
157 #ifdef WRITE_OUTPUT
158 for(int i = 0; i < synth->buffersize; ++i)
159 file << outL[i] << std::endl;
161 #endif
162 sampleCount += synth->buffersize;
164 #ifdef WRITE_OUTPUT
165 file.close();
166 #endif
168 TS_ASSERT_EQUALS(sampleCount, 5888);
171 #define OUTPUT_PROFILE
172 #ifdef OUTPUT_PROFILE
173 void testSpeed() {
174 const int samps = 15000;
176 int t_on = clock(); // timer before calling func
177 for(int i = 0; i < samps; ++i)
178 note->noteout(outL, outR);
179 int t_off = clock(); // timer when func returns
181 printf("SubNoteTest: %f seconds for %d Samples to be generated.\n",
182 (static_cast<float>(t_off - t_on)) / CLOCKS_PER_SEC, samps);
184 #endif