Nsound  0.9.4
Sine_UnitTest.cc
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------
2 //
3 // $Id: Sine_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/Sine.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 = "Sine_UnitTest.cc";
48 
49 static const float64 GAMMA = 1.5e-14;
50 
52 {
53  cout << endl << THIS_FILE;
54 
57 
58  Sine sine(600);
59 
60  Buffer freqs;
61  Buffer phase;
62 
63  freqs << sine.drawLine(1.0, 0.0, 10.0);
64  phase << sine.drawLine(1.0, 0.0, 1.0);
65 
66  cout << TEST_HEADER << "Testing Sine::generate(1.0, 3.0) ...";
67 
68  Buffer data = sine.generate(1.0, 3.0);
69 
70  // Create the gold file
71 //~ data >> "gold/Sine_1.wav";
72 
73  Buffer gold;
74 
75  gold << "gold/Sine_1.wav";
76 
77  Buffer diff = data - gold;
78 
79  Buffer abs_diff(diff);
80  abs_diff.abs();
81 
82  if(abs_diff.getMax() > GAMMA)
83  {
84  cerr << TEST_ERROR_HEADER
85  << "Output did not match gold file!"
86  << endl;
87 
88  diff.plot("data - gold");
89  data.plot("data");
90  gold.plot("gold");
91 
92  Plotter::show();
93 
94  exit(1);
95  }
96 
97  cout << SUCCESS;
98 
99  // Test 3.5 Hz wave
100  cout << TEST_HEADER << "Testing Sine::generate(1.0, freqs) ...";
101 
102  data = sine.generate(1.0, freqs);
103 
104  // Create the gold file
105 //~ data >> "gold/Sine_2.wav";
106 
107  gold = Buffer();
108 
109  gold << "gold/Sine_2.wav";
110 
111  diff = data - gold;
112 
113  abs_diff = diff;
114  abs_diff.abs();
115 
116  if(abs_diff.getMax() > GAMMA)
117  {
118  cerr << TEST_ERROR_HEADER
119  << "Output did not match gold file!"
120  << endl;
121 
122  diff.plot("data - gold");
123  data.plot("data");
124  gold.plot("gold");
125 
126  Plotter::show();
127 
128  exit(1);
129  }
130 
131  cout << SUCCESS;
132 
133  // Test dynamic
134  cout << TEST_HEADER << "Testing Sine::generate2(1.0, 3.0, 0.5) ...";
135 
136  data = sine.generate2(1.0, 3.0, 0.5);
137 
138  // Create the gold file
139 //~ data >> "gold/Sine_3.wav";
140 
141  gold = Buffer();
142 
143  gold << "gold/Sine_3.wav";
144 
145  diff = data - gold;
146 
147  abs_diff = diff;
148  abs_diff.abs();
149 
150  if(abs_diff.getMax() > GAMMA)
151  {
152  cerr << TEST_ERROR_HEADER
153  << "Output did not match gold file!"
154  << endl;
155 
156  diff.plot("data - gold");
157  data.plot("data");
158  gold.plot("gold");
159 
160  Plotter::show();
161 
162  exit(1);
163  }
164 
165  cout << SUCCESS;
166 
167  // Test dynamic
168  cout << TEST_HEADER << "Testing Sine::generate2(1.0, 3.0, phase) ...";
169 
170  data = sine.generate2(1.0, 3.0, phase);
171 
172  // Create the gold file
173 //~ data >> "gold/Sine_4.wav";
174 
175  gold = Buffer();
176 
177  gold << "gold/Sine_4.wav";
178 
179  diff = data - gold;
180 
181  abs_diff = diff;
182  abs_diff.abs();
183 
184  if(abs_diff.getMax() > GAMMA)
185  {
186  cerr << TEST_ERROR_HEADER
187  << "Output did not match gold file!"
188  << endl;
189 
190  diff.plot("data - gold");
191  data.plot("data");
192  gold.plot("gold");
193 
194  Plotter::show();
195 
196  exit(1);
197  }
198 
199  cout << SUCCESS;
200 
201  // Test dynamic
202  cout << TEST_HEADER << "Testing Sine::generate2(1.0, freqs, 0.5) ...";
203 
204  data = sine.generate2(1.0, freqs, 0.5);
205 
206  // Create the gold file
207 //~ data >> "gold/Sine_5.wav";
208 
209  gold = Buffer();
210 
211  gold << "gold/Sine_5.wav";
212 
213  diff = data - gold;
214 
215  abs_diff = diff;
216  abs_diff.abs();
217 
218  if(abs_diff.getMax() > GAMMA)
219  {
220  cerr << TEST_ERROR_HEADER
221  << "Output did not match gold file!"
222  << endl;
223 
224  diff.plot("data - gold");
225  data.plot("data");
226  gold.plot("gold");
227 
228  Plotter::show();
229 
230  exit(1);
231  }
232 
233  cout << SUCCESS;
234 
235  // Test dynamic
236  cout << TEST_HEADER << "Testing Sine::generate2(1.0, freqs, phase) ...";
237 
238  data = sine.generate2(1.0, freqs, phase);
239 
240  // Create the gold file
241 //~ data >> "gold/Sine_6.wav";
242 
243  gold = Buffer();
244 
245  gold << "gold/Sine_6.wav";
246 
247  diff = data - gold;
248 
249  abs_diff = diff;
250  abs_diff.abs();
251 
252  if(abs_diff.getMax() > GAMMA)
253  {
254  cerr << TEST_ERROR_HEADER
255  << "Output did not match gold file!"
256  << endl;
257 
258  diff.plot("data - gold");
259  data.plot("data");
260  gold.plot("gold");
261 
262  Plotter::show();
263 
264  exit(1);
265  }
266 
267  cout << SUCCESS;
268 
269  // Test dynamic
270  cout << TEST_HEADER << "Testing Sine::setChorus(5,0.10) ...";
271 
272  sine.setSeed(6846513); // Some random seed I typed in.
273 
274  sine.setChorus(5, 0.10);
275 
276  data = sine.generate2(1.0, 2*freqs, phase);
277 
278  // Create the gold file
279 //~ data >> "gold/Sine_7.wav";
280 
281  gold = Buffer();
282 
283  gold << "gold/Sine_7.wav";
284 
285  diff = data - gold;
286 
287  abs_diff = diff;
288  abs_diff.abs();
289 
290  if(abs_diff.getMax() > GAMMA)
291  {
292  cerr << TEST_ERROR_HEADER
293  << "Output did not match gold file!"
294  << endl;
295 
296  diff.plot("data - gold");
297  data.plot("data");
298  gold.plot("gold");
299 
300  Plotter::show();
301 
302  exit(1);
303  }
304 
305  cout << SUCCESS << endl;
306 }
307 
308 
static void show()
Acutally draw the plots to the screen.
Definition: Plotter.cc:252
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
void Sine_UnitTest()
double float64
Definition: Nsound.h:146
static const char * THIS_FILE
#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
void setChorus(const uint32 n_voices, const float64 &sigma=0.02)
Chorus or Unison.
Definition: Generator.cc:361
static void setIEEEFloat(boolean flag)
Definition: Wavefile.cc:98
static const float64 GAMMA
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
void setSeed(const uint32 seed)
Sets the seed for the Generator's random number generator (rng).
Definition: Generator.cc:1303
DOXME.
Definition: Sine.h:43
virtual float64 generate2(const float64 &frequency, const float64 &phase)
This is a real-time method for the wavetable oscillator.
Definition: Generator.cc:979