Nsound  0.9.4
Functions | Variables
Sine_UnitTest.cc File Reference
#include <Nsound/Buffer.h>
#include <Nsound/Plotter.h>
#include <Nsound/Sine.h>
#include <Nsound/Wavefile.h>
#include "UnitTest.h"
#include <cmath>
#include <stdlib.h>
#include <iostream>
Include dependency graph for Sine_UnitTest.cc:

Go to the source code of this file.

Functions

void Sine_UnitTest ()
 

Variables

static const char * THIS_FILE = "Sine_UnitTest.cc"
 
static const float64 GAMMA = 1.5e-14
 

Function Documentation

void Sine_UnitTest ( )

Definition at line 51 of file Sine_UnitTest.cc.

References Nsound::Buffer::abs(), Nsound::Generator::drawLine(), GAMMA, Nsound::Generator::generate(), Nsound::Generator::generate2(), Nsound::Buffer::getMax(), Nsound::Buffer::plot(), Nsound::Generator::setChorus(), Nsound::Wavefile::setDefaultSampleSize(), Nsound::Wavefile::setIEEEFloat(), Nsound::Generator::setSeed(), Nsound::Plotter::show(), SUCCESS, TEST_ERROR_HEADER, TEST_HEADER, and THIS_FILE.

Referenced by main().

52 {
53  cout << endl << THIS_FILE;
54 
55  Wavefile::setDefaultSampleSize(64);
56  Wavefile::setIEEEFloat(true);
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 }
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
static const char * THIS_FILE
#define TEST_ERROR_HEADER
Definition: Test.h:49
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
DOXME.
Definition: Sine.h:43

Variable Documentation

const char* THIS_FILE = "Sine_UnitTest.cc"
static

Definition at line 47 of file Sine_UnitTest.cc.

Referenced by Sine_UnitTest().

const float64 GAMMA = 1.5e-14
static

Definition at line 49 of file Sine_UnitTest.cc.

Referenced by Sine_UnitTest().