Nsound  0.9.4
Triangle_UnitTest.cc
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------
2 //
3 // $Id: Triangle_UnitTest.cc 874 2014-09-08 02:21:29Z weegreenblobbie $
4 //
5 // Copyright (c) 2006-Present Nick Hilton
6 //
7 // weegreenblobbie_yahoo_com (replace '_' with '@' and '.')
8 //
9 //-----------------------------------------------------------------------------
10 
11 //-----------------------------------------------------------------------------
12 //
13 // This program is free software; you can redistribute it and/or modify
14 // it under the terms of the GNU General Public License as published by
15 // the Free Software Foundation; either version 2 of the License, or
16 // (at your option) any later version.
17 //
18 // This program is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 // GNU Library General Public License for more details.
22 //
23 // You should have received a copy of the GNU General Public License
24 // along with this program; if not, write to the Free Software
25 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 //
27 //-----------------------------------------------------------------------------
28 
29 #include <Nsound/Buffer.h>
30 #include <Nsound/Plotter.h>
31 #include <Nsound/Triangle.h>
32 #include <Nsound/Wavefile.h>
33 
34 #include "UnitTest.h"
35 
36 #include <cmath>
37 #include <stdlib.h>
38 #include <iostream>
39 
40 using namespace Nsound;
41 
42 using std::cerr;
43 using std::cout;
44 using std::endl;
45 
46 // The __FILE__ macro includes the path, I don't want the whole path.
47 static const char * THIS_FILE = "Triangle_UnitTest.cc";
48 
49 static const float64 GAMMA = 1.5e-14;
50 
52 {
53  cout << endl << THIS_FILE;
54 
57 
58  Triangle tri(100);
59 
60  Buffer data;
61  Buffer gold;
62  Buffer diff;
63 
64  // Test 7 Hz wave
65  cout << TEST_HEADER << "Testing Triangle::generate(1.01, 2.0) ...";
66 
67  data = tri.generate(1.01, 2.0);
68 
69  // Create the gold file
70 //~ data >> "gold/Triangle_2Hz.wav";
71 
72  gold = Buffer("gold/Triangle_2Hz.wav");
73 
74  diff = data - gold;
75 
76  diff.abs();
77 
78  if(diff.getMax() > GAMMA)
79  {
80  cerr << TEST_ERROR_HEADER
81  << "Output did not match gold file!"
82  << endl;
83 
84  diff.plot("data - gold");
85  data.plot("data");
86  gold.plot("gold");
87 
88  Plotter::show();
89 
90  exit(1);
91  }
92 
93  cout << SUCCESS;
94 
95  // Test 3.5 Hz wave
96  cout << TEST_HEADER << "Testing Triangle::generate(1.01, 1.0) ...";
97 
98  data = tri.generate(1.01, 1.0);
99 
100  // Create the gold file
101 //~ data >> "gold/Triangle_1.0Hz.wav";
102 
103  gold = Buffer("gold/Triangle_1.0Hz.wav");
104 
105  diff = data - gold;
106 
107  diff.abs();
108 
109  if(diff.getMax() > GAMMA)
110  {
111  cerr << TEST_ERROR_HEADER
112  << "Output did not match gold file!"
113  << endl;
114 
115  diff.plot("data - gold");
116  data.plot("data");
117  gold.plot("gold");
118 
119  Plotter::show();
120 
121  exit(1);
122  }
123 
124  cout << SUCCESS;
125 
126  // Test dynamic
127  cout << TEST_HEADER << "Testing Triangle::generate(1.01, frequencies) ...";
128 
129  Buffer freqs = tri.drawLine(1.01, 1.0, 5.0);
130 
131  data = tri.generate(1.01, freqs);
132 
133  // Create the gold file
134 //~ data >> "gold/Triangle_1_to_5Hz.wav";
135 
136  gold = Buffer("gold/Triangle_1_to_5Hz.wav");
137 
138  diff = data - gold;
139 
140  diff.abs();
141 
142  if(diff.getMax() > GAMMA)
143  {
144  cerr << TEST_ERROR_HEADER
145  << "Output did not match gold file!"
146  << endl;
147 
148  diff.plot("data - gold");
149  data.plot("data");
150  gold.plot("gold");
151 
152  Plotter::show();
153 
154  exit(1);
155  }
156 
157  Plotter::show();
158 
159  cout << SUCCESS << endl;
160 }
161 
162 
Triangle generator.
Definition: Triangle.h:48
static void show()
Acutally draw the plots to the screen.
Definition: Plotter.cc:252
void Triangle_UnitTest()
void plot(const std::string &title="Buffer") const
Requires matplotlib. Creates a plot of this Buffer.
Definition: Buffer.cc:1551
#define TEST_HEADER
Definition: Test.h:45
double float64
Definition: Nsound.h:146
#define TEST_ERROR_HEADER
Definition: Test.h:49
virtual float64 generate(const float64 &frequency)
This is a real-time method for the wavetable oscillator.
Definition: Generator.cc:972
static const char * THIS_FILE
static void setIEEEFloat(boolean flag)
Definition: Wavefile.cc:98
void abs()
Modifies the Buffer by making any negative value positive.
Definition: Buffer.cc:119
#define SUCCESS
Definition: UnitTest.h:42
A Buffer for storing audio samples.
Definition: Buffer.h:60
static void setDefaultSampleSize(uint32 size)
Definition: Wavefile.cc:79
Buffer drawLine(const float64 &duration, const float64 &amplitude_start, const float64 &amplitude_finish) const
This method draws a linear line beteween 2 points.
Definition: Generator.cc:464
float64 getMax() const
Returns the maximum sample value in the Buffer.
Definition: Buffer.cc:951
static const float64 GAMMA