Nsound  0.9.4
Wavefile_UnitTest.cc
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------
2 //
3 // $Id: Wavefile_UnitTest.cc 874 2014-09-08 02:21:29Z weegreenblobbie $
4 //
5 // Copyright (c) 2006 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/Nsound.h>
30 
31 #include <Nsound/AudioStream.h>
32 #include <Nsound/Buffer.h>
33 #include <Nsound/Plotter.h>
34 #include <Nsound/Sine.h>
35 #include <Nsound/Wavefile.h>
36 
37 #include "UnitTest.h"
38 
39 #include <stdlib.h>
40 #include <iostream>
41 
42 using namespace Nsound;
43 
44 using std::cout;
45 using std::cerr;
46 using std::endl;
47 using std::flush;
48 
49 static const char * THIS_FILE = "Wavefile_UnitTest.cc";
50 
51 static const float64 GAMMA = 1.5e-14;
52 
53 void
55 {
56  cout << endl << THIS_FILE;
57 
59 
60  Sine sin(100);
61 
62  AudioStream data1(100, 3);
63 
64  data1 << sin.generate(1.0, 13.0);
65 
66  data1[1] *= 0.666666666666666;
67  data1[2] *= 0.333333333333333;
68 
69  // Write out 10 channel 13 Hz wave.
70  cout << TEST_HEADER << "Testing Wavefile::write(48-bit), read(48-bit) ..." << flush;
71 
72  data1 >> "test_wavefile.wav";
73 
74  AudioStream data(100, 3);
75 
76  data << "test_wavefile.wav";
77 
78  AudioStream gold(100, 3);
79 
80  // Create gold file
81 //~ data >> "gold/Wavefile_out1.wav";
82 
83  gold << "gold/Wavefile_out1.wav";
84 
85  AudioStream diff = data - gold;
86 
87  AudioStream abs_diff(diff);
88  abs_diff.abs();
89 
90  for(int32 i = 0; i < 3; ++i)
91  {
92  if(abs_diff[i].getMax() > GAMMA)
93  {
94  cerr << TEST_ERROR_HEADER
95  << "Output did not match gold file!"
96  << endl;
97 
98  diff[i].plot("data - gold");
99  data[i].plot("data");
100  gold[i].plot("gold");
101 
102  Plotter::show();
103 
104  exit(1);
105  }
106  }
107 
108  cout << SUCCESS;
109 
110  // Write out 10 channel 13 Hz wave again but with 24 bits per sample
111  cout << TEST_HEADER << "Testing Wavefile::write(24-bit), read(24-bit) ..." << flush;
112 
114 
115  data1 >> "test_wavefile2.wav";
116 
117  data = AudioStream(100, 3);
118  data << "test_wavefile2.wav";
119 
120  // Create gold file
121 //~ data >> "gold/Wavefile_out2.wav";
122 
123  gold = AudioStream(100, 3);
124  gold << "gold/Wavefile_out2.wav";
125 
126  diff = data - gold;
127 
128  abs_diff = diff;
129  abs_diff.abs();
130 
131  for(int32 i = 0; i < 1; ++i)
132  {
133  if(abs_diff[i].getMax() > 0.5 * GAMMA)
134  {
135  cerr << TEST_ERROR_HEADER
136  << "Output did not match gold file!"
137  << endl;
138 
139  diff[i].plot("data - gold");
140  data[i].plot("data");
141  gold[i].plot("gold");
142 
143  Plotter::show();
144 
145  exit(1);
146  }
147  }
148 
149  cout << SUCCESS << endl;
150 }
static void show()
Acutally draw the plots to the screen.
Definition: Plotter.cc:252
static const float64 GAMMA
#define TEST_HEADER
Definition: Test.h:45
double float64
Definition: Nsound.h:146
void Wavefile_UnitTest()
#define TEST_ERROR_HEADER
Definition: Test.h:49
void plot(const std::string &title="AudioStream") const
Definition: AudioStream.cc:661
virtual float64 generate(const float64 &frequency)
This is a real-time method for the wavetable oscillator.
Definition: Generator.cc:972
#define SUCCESS
Definition: UnitTest.h:42
static const char * THIS_FILE
signed int int32
Definition: Nsound.h:142
static void setDefaultSampleSize(uint32 size)
Definition: Wavefile.cc:79
void abs()
This method calls abs on all buffers held in the stream.
Definition: AudioStream.cc:114
DOXME.
Definition: Sine.h:43