Nsound  0.9.4
BufferResample_UnitTest.cc
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------
2 //
3 // $Id: BufferResample_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/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 
39 #include <iostream>
40 #include <sstream>
41 
42 using namespace Nsound;
43 
44 using std::cerr;
45 using std::cout;
46 using std::endl;
47 
48 // The __FILE__ macro includes the path, I don't want the whole path.
49 #define THIS_FILE "BufferResample_UnitTest.cc"
50 #define GAMMA 1.5e-14
51 
53 {
54  cout << endl << THIS_FILE;
55 
58 
59  static const uint32 LM[4] = { 2,3,5,7 };
60 
61  Sine sin(150);
62 
63  Buffer input = sin.generate(1.0, 3.0);
64 
65  cout << TEST_HEADER << "Testing Buffer:getResample(L,M) ...";
66 
67  Buffer data;
68  Buffer gold;
69  Buffer diff;
70 
71  std::stringstream ss;
72 
73  for(uint32 i = 1; i <= 4; ++i)
74  {
75 
76  std::string gold_filename;
77 
78  ss.str("");
79  ss << "gold/BufferResample_out_" << i << "_" << LM[i-1] << ".wav";
80 
81  data = input.getResample(i, LM[i-1]);
82 
83  // Create gold file
84 //~ data >> ss.str().c_str();
85 
86  gold = Buffer(ss.str().c_str());
87 
88  diff = data - gold;
89 
90  if(gold.getLength() != data.getLength() ||
91  diff.getAbs().getMax() > GAMMA)
92  {
93  cerr << TEST_ERROR_HEADER
94  << "Output did not match gold file!"
95  << endl;
96 
97  diff.plot("data - gold");
98  data.plot("data");
99  gold.plot("gold");
100 
101  Plotter::show();
102 
103  exit(1);
104  }
105 
106  // Reverse L & M
107  ss.str("");
108  ss << "gold/BufferResample_out_" << LM[i-1] << "_" << i << ".wav";
109 
110  data = input.getResample(LM[i-1], i);
111 
112  // Create gold file
113 //~ data >> ss.str().c_str();
114 
115  gold = Buffer(ss.str().c_str());
116 
117  diff = data - gold;
118 
119  if(gold.getLength() != data.getLength() ||
120  diff.getAbs().getMax() > GAMMA)
121  {
122  cerr << TEST_ERROR_HEADER
123  << "Output did not match gold file!"
124  << endl;
125 
126  diff.plot("data - gold");
127  data.plot("data");
128  gold.plot("gold");
129 
130  Plotter::show();
131 
132  exit(1);
133  }
134 
135  }
136 
137  cout << SUCCESS << endl;
138 }
139 
140 
unsigned int uint32
Definition: Nsound.h:153
#define GAMMA
static void show()
Acutally draw the plots to the screen.
Definition: Plotter.cc:252
#define THIS_FILE
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
Buffer getAbs() const
Modifies a copy of the Buffer by making any negative value positive.
Definition: Buffer.h:174
uint32 getLength() const
Returns the number of samples in the Buffer.
Definition: Buffer.h:587
#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 void setIEEEFloat(boolean flag)
Definition: Wavefile.cc:98
#define SUCCESS
Definition: UnitTest.h:42
A Buffer for storing audio samples.
Definition: Buffer.h:60
Buffer getResample(float64 factor, const uint32 N=10, float64 beta=5.0) const
Resamples a copy of this Buffer using discrete-time resampling.
Definition: Buffer.cc:1607
static void setDefaultSampleSize(uint32 size)
Definition: Wavefile.cc:79
float64 getMax() const
Returns the maximum sample value in the Buffer.
Definition: Buffer.cc:951
void BufferResample_UnitTest()
DOXME.
Definition: Sine.h:43