Sine_UnitTest.cc

Go to the documentation of this file.
00001 
00002 //
00003 //  $Id: Sine_UnitTest.cc 628 2011-02-23 04:37:00Z weegreenblobbie $
00004 //
00005 //  Copyright (c) 2006-Present Nick Hilton
00006 //
00007 //  weegreenblobbie_yahoo_com (replace '_' with '@' and '.')
00008 //
00010 
00012 //
00013 //  This program is free software; you can redistribute it and/or modify
00014 //  it under the terms of the GNU General Public License as published by
00015 //  the Free Software Foundation; either version 2 of the License, or
00016 //  (at your option) any later version.
00017 //
00018 //  This program is distributed in the hope that it will be useful,
00019 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00020 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00021 //  GNU Library General Public License for more details.
00022 //
00023 //  You should have received a copy of the GNU General Public License
00024 //  along with this program; if not, write to the Free Software
00025 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00026 //
00028 
00029 #include <Nsound/Buffer.h>
00030 #include <Nsound/Plotter.h>
00031 #include <Nsound/Sine.h>
00032 #include <Nsound/Wavefile.h>
00033 
00034 #include "UnitTest.h"
00035 
00036 #include <cmath>
00037 #include <stdlib.h>
00038 #include <iostream>
00039 
00040 using namespace Nsound;
00041 
00042 using std::cerr;
00043 using std::cout;
00044 using std::endl;
00045 
00046 // The __FILE__ macro includes the path, I don't want the whole path.
00047 static const char * THIS_FILE = "Sine_UnitTest.cc";
00048 
00049 static const float64 GAMMA = 1.5e-14;
00050 
00051 void Sine_UnitTest()
00052 {
00053     cout << endl << THIS_FILE;
00054 
00055     Wavefile::setDefaultSampleSize(64);
00056     Wavefile::setIEEEFloat(true);
00057 
00058     Sine sine(600);
00059 
00060     Buffer freqs;
00061     Buffer phase;
00062 
00063     freqs << sine.drawLine(1.0, 0.0, 10.0);
00064     phase << sine.drawLine(1.0, 0.0, 1.0);
00065 
00067     cout << TEST_HEADER << "Testing Sine::generate(1.0, 3.0) ...";
00068 
00069     Buffer data = sine.generate(1.0, 3.0);
00070 
00071     // Create the gold file
00072 //~    data >> "gold/Sine_1.wav";
00073 
00074     Buffer gold;
00075 
00076     gold << "gold/Sine_1.wav";
00077 
00078     Buffer diff = data - gold;
00079 
00080     Buffer abs_diff(diff);
00081     abs_diff.abs();
00082 
00083     if(abs_diff.getMax() > GAMMA)
00084     {
00085         cerr << TEST_ERROR_HEADER
00086              << "Output did not match gold file!"
00087              << endl;
00088 
00089         diff.plot("data - gold");
00090         data.plot("data");
00091         gold.plot("gold");
00092 
00093         Plotter::show();
00094 
00095         exit(1);
00096     }
00097 
00098     cout << SUCCESS;
00099 
00101     // Test 3.5 Hz wave
00102     cout << TEST_HEADER << "Testing Sine::generate(1.0, freqs) ...";
00103 
00104     data = sine.generate(1.0, freqs);
00105 
00106     // Create the gold file
00107 //~    data >> "gold/Sine_2.wav";
00108 
00109     gold = Buffer();
00110 
00111     gold << "gold/Sine_2.wav";
00112 
00113     diff = data - gold;
00114 
00115     abs_diff = diff;
00116     abs_diff.abs();
00117 
00118     if(abs_diff.getMax() > GAMMA)
00119     {
00120         cerr << TEST_ERROR_HEADER
00121              << "Output did not match gold file!"
00122              << endl;
00123 
00124         diff.plot("data - gold");
00125         data.plot("data");
00126         gold.plot("gold");
00127 
00128         Plotter::show();
00129 
00130         exit(1);
00131     }
00132 
00133     cout << SUCCESS;
00134 
00136     // Test dynamic
00137     cout << TEST_HEADER << "Testing Sine::generate2(1.0, 3.0, 0.5) ...";
00138 
00139     data = sine.generate2(1.0, 3.0, 0.5);
00140 
00141     // Create the gold file
00142 //~    data >> "gold/Sine_3.wav";
00143 
00144     gold = Buffer();
00145 
00146     gold << "gold/Sine_3.wav";
00147 
00148     diff = data - gold;
00149 
00150     abs_diff = diff;
00151     abs_diff.abs();
00152 
00153     if(abs_diff.getMax() > GAMMA)
00154     {
00155         cerr << TEST_ERROR_HEADER
00156              << "Output did not match gold file!"
00157              << endl;
00158 
00159         diff.plot("data - gold");
00160         data.plot("data");
00161         gold.plot("gold");
00162 
00163         Plotter::show();
00164 
00165         exit(1);
00166     }
00167 
00168     cout << SUCCESS;
00169 
00171     // Test dynamic
00172     cout << TEST_HEADER << "Testing Sine::generate2(1.0, 3.0, phase) ...";
00173 
00174     data = sine.generate2(1.0, 3.0, phase);
00175 
00176     // Create the gold file
00177 //~    data >> "gold/Sine_4.wav";
00178 
00179     gold = Buffer();
00180 
00181     gold << "gold/Sine_4.wav";
00182 
00183     diff = data - gold;
00184 
00185     abs_diff = diff;
00186     abs_diff.abs();
00187 
00188     if(abs_diff.getMax() > GAMMA)
00189     {
00190         cerr << TEST_ERROR_HEADER
00191              << "Output did not match gold file!"
00192              << endl;
00193 
00194         diff.plot("data - gold");
00195         data.plot("data");
00196         gold.plot("gold");
00197 
00198         Plotter::show();
00199 
00200         exit(1);
00201     }
00202 
00203     cout << SUCCESS;
00204 
00206     // Test dynamic
00207     cout << TEST_HEADER << "Testing Sine::generate2(1.0, freqs, 0.5) ...";
00208 
00209     data = sine.generate2(1.0, freqs, 0.5);
00210 
00211     // Create the gold file
00212 //~    data >> "gold/Sine_5.wav";
00213 
00214     gold = Buffer();
00215 
00216     gold << "gold/Sine_5.wav";
00217 
00218     diff = data - gold;
00219 
00220     abs_diff = diff;
00221     abs_diff.abs();
00222 
00223     if(abs_diff.getMax() > GAMMA)
00224     {
00225         cerr << TEST_ERROR_HEADER
00226              << "Output did not match gold file!"
00227              << endl;
00228 
00229         diff.plot("data - gold");
00230         data.plot("data");
00231         gold.plot("gold");
00232 
00233         Plotter::show();
00234 
00235         exit(1);
00236     }
00237 
00238     cout << SUCCESS;
00239 
00241     // Test dynamic
00242     cout << TEST_HEADER << "Testing Sine::generate2(1.0, freqs, phase) ...";
00243 
00244     data = sine.generate2(1.0, freqs, phase);
00245 
00246     // Create the gold file
00247 //~    data >> "gold/Sine_6.wav";
00248 
00249     gold = Buffer();
00250 
00251     gold << "gold/Sine_6.wav";
00252 
00253     diff = data - gold;
00254 
00255     abs_diff = diff;
00256     abs_diff.abs();
00257 
00258     if(abs_diff.getMax() > GAMMA)
00259     {
00260         cerr << TEST_ERROR_HEADER
00261              << "Output did not match gold file!"
00262              << endl;
00263 
00264         diff.plot("data - gold");
00265         data.plot("data");
00266         gold.plot("gold");
00267 
00268         Plotter::show();
00269 
00270         exit(1);
00271     }
00272 
00273     cout << SUCCESS;
00274 
00276     // Test dynamic
00277     cout << TEST_HEADER << "Testing Sine::setChorus(5,0.10) ...";
00278 
00279     sine.setSeed(6846513); // Some random seed I typed in.
00280 
00281     sine.setChorus(5, 0.10);
00282 
00283     data = sine.generate2(1.0, 2*freqs, phase);
00284 
00285     // Create the gold file
00286 //~    data >> "gold/Sine_7.wav";
00287 
00288     gold = Buffer();
00289 
00290     gold << "gold/Sine_7.wav";
00291 
00292     diff = data - gold;
00293 
00294     abs_diff = diff;
00295     abs_diff.abs();
00296 
00297     if(abs_diff.getMax() > GAMMA)
00298     {
00299         cerr << TEST_ERROR_HEADER
00300              << "Output did not match gold file!"
00301              << endl;
00302 
00303         diff.plot("data - gold");
00304         data.plot("data");
00305         gold.plot("gold");
00306 
00307         Plotter::show();
00308 
00309         exit(1);
00310     }
00311 
00312     cout << SUCCESS << endl;
00313 }
00314 
00315 
Generated on Sun Apr 15 20:10:06 2012 for nsound by  doxygen 1.6.3