MiddlewareTest.cpp (5342B)
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 MiddleWareTest 37 { 38 public: 39 struct FFTCleaner { ~FFTCleaner() { FFT_cleanup(); } } cleaner; 40 Config config; 41 void setUp() { 42 synth = new SYNTH_T; 43 synth->buffersize = 256; 44 synth->samplerate = 48000; 45 //synth->alias(); 46 47 outL = new float[1024]; 48 for(int i = 0; i < synth->buffersize; ++i) 49 outL[i] = 0.0f; 50 outR = new float[1024]; 51 for(int i = 0; i < synth->buffersize; ++i) 52 outR[i] = 0.0f; 53 54 delete synth; 55 synth = NULL; 56 for(int i = 0; i < NUM_MIDDLEWARE; ++i) { 57 synth = new SYNTH_T; 58 synth->buffersize = 256; 59 synth->samplerate = 48000; 60 //synth->alias(); 61 middleware[i] = new MiddleWare(std::move(*synth), &config); 62 master[i] = middleware[i]->spawnMaster(); 63 //printf("Octave size = %d\n", master[i]->microtonal.getoctavesize()); 64 if (i != NUM_MIDDLEWARE-1) { 65 delete synth; 66 } else { 67 // "synth" is kept to be directly accessed by the tests 68 } 69 } 70 } 71 72 void tearDown() { 73 for(int i = 0; i < NUM_MIDDLEWARE; ++i) 74 delete middleware[i]; 75 delete synth; 76 77 delete[] outL; 78 delete[] outR; 79 } 80 81 82 void testInit() { 83 84 for(int x=0; x<100; ++x) { 85 for(int i=0; i<NUM_MIDDLEWARE; ++i) { 86 middleware[i]->tick(); 87 master[i]->GetAudioOutSamples(rand()%1025, 88 synth->samplerate, outL, outR); 89 } 90 } 91 } 92 93 void testPanic() 94 { 95 master[0]->setController(0, 0x64, 0); 96 master[0]->noteOn(0,64,64); 97 master[0]->AudioOut(outL, outR); 98 99 float sum = 0.0f; 100 for(int i = 0; i < synth->buffersize; ++i) 101 sum += fabsf(outL[i]); 102 103 TS_ASSERT(0.1f < sum); 104 } 105 106 string loadfile(string fname) const 107 { 108 std::ifstream t(fname.c_str()); 109 std::string str((std::istreambuf_iterator<char>(t)), 110 std::istreambuf_iterator<char>()); 111 return str; 112 } 113 114 void testLoad(void) 115 { 116 for(int i=0; i<NUM_MIDDLEWARE; ++i) { 117 middleware[i]->transmitMsg("/load-part", "is", 0, (string(SOURCE_DIR) + "/../../instruments/banks/Organ/0037-Church Organ 1.xiz").c_str()); 118 middleware[i]->tick(); 119 master[i]->GetAudioOutSamples(synth->buffersize, synth->samplerate, outL, outR); 120 middleware[i]->tick(); 121 master[i]->GetAudioOutSamples(synth->buffersize, synth->samplerate, outL, outR); 122 middleware[i]->tick(); 123 master[i]->GetAudioOutSamples(synth->buffersize, synth->samplerate, outL, outR); 124 middleware[i]->tick(); 125 master[i]->GetAudioOutSamples(synth->buffersize, synth->samplerate, outL, outR); 126 middleware[i]->tick(); 127 } 128 //const string fname = string(SOURCE_DIR) + "/../../instruments/banks/Organ/0037-Church Organ 1.xiz"; 129 //const string fdata = loadfile(fname); 130 } 131 132 void testChangeToOutOfRangeProgram() 133 { 134 middleware[0]->transmitMsg("/bank/msb", "i", 0); 135 middleware[0]->tick(); 136 middleware[0]->transmitMsg("/bank/lsb", "i", 1); 137 middleware[0]->tick(); 138 middleware[0]->pendingSetProgram(0, 32); 139 middleware[0]->tick(); 140 master[0]->GetAudioOutSamples(synth->buffersize, synth->samplerate, outL, outR); 141 // We should ideally be checking to verify that the part change 142 // didn't happen, but it's not clear how to do that. We're 143 // currently relying on the assert(filename) in loadPart() failing 144 // if this logic gets broken. 145 } 146 147 private: 148 SYNTH_T *synth; 149 float *outR, *outL; 150 MiddleWare *middleware[NUM_MIDDLEWARE]; 151 Master *master[NUM_MIDDLEWARE]; 152 }; 153 154 int main() 155 { 156 MiddleWareTest test; 157 RUN_TEST(testInit); 158 RUN_TEST(testPanic); 159 RUN_TEST(testLoad); 160 RUN_TEST(testChangeToOutOfRangeProgram); 161 return test_summary(); 162 }