Nsound  0.9.4
test_generators.cc
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------
2 //
3 // $Id: test_generators.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/NsoundAll.h>
30 
31 #include <cmath>
32 #include <iostream>
33 #include <sstream>
34 
35 using namespace Nsound;
36 
37 using std::cerr;
38 using std::cout;
39 using std::endl;
40 
41 void testBuffer();
42 
43 int main(int argc, char ** argv)
44 {
45 //~ Generator generator(44100);
46 //~ Sine sine(44100);
47 //~
48 //~ Buffer exp = sine.drawExponential(1.0);
49 //~
50 //~ exp.plot("exp");
51 //~
52 //~ Buffer decay = sine.drawDecay(1.0);
53 //~
54 //~ decay.plot("decay");
55 //~
56 //~ Buffer g2 = sine.drawFatGaussian(2.0, 0.3);
57 //~
58 //~ g2.plot("Gaussian 30% pass band time");
59 //~
60 //~ Buffer gaussian = sine.drawGaussian(1.0);
61 //~
62 //~ gaussian.plot("Gaussian");
63 //~
64 //~ Triangle triangle2(
65 //~ 44100,
66 //~ 1.0*0.1428,
67 //~ 2.0*0.1428, 1.0,
68 //~ 3.0*0.1428,
69 //~ 4.0*0.1428,
70 //~ 5.0*0.1428, -1.0,
71 //~ 6.0*0.1428);
72 //~
73 //~ Buffer p = generator.drawParabola(1.0,1.0,0.5,0.75,1.0);
74 //~ p << generator.drawParabola(1.0, 1.0, 0.5, 0.50, 1.0);
75 //~ p << generator.drawParabola(1.0, 1.0, 0.5, 0.25, 1.0);
76 //~ p.plot("parabola");
77 //~
78 //~ Buffer triangle_buffer = triangle2.generate(1.0,1.0);
79 //~ triangle_buffer.plot("triangle2(100,1*0.1666,2*0.1666, 1.0,3*0.1666,4*0.1666,5*0.1666, -1.0,6*0.1666)");
80 //~
81 //~ Triangle triangle1(44100);
82 //~
83 //~ triangle_buffer = triangle1.generate(1.0,1.0);
84 //~ triangle_buffer.plot("triangle");
85 //~
86 //~ Square square1(
87 //~ 44100,
88 //~ 0.2, 1.0,
89 //~ 0.4,
90 //~ 0.6, -1.0,
91 //~ 0.8);
92 //~ Buffer square_buffer = 0.5 * square1.generate(1.0,1.0);
93 //~ square_buffer.plot("square2 (44100, 0.2, 1.0,0.4, 0.6, -1.0, 0.8) (1/2 amplitude)");
94 //~
95 //~ Square square2(44100);
96 //~ square_buffer = 0.5 * square2.generate(1.0,1.0);
97 //~ square_buffer.plot("square (1/2 amplitude)");
98 //~
99 //~ Buffer sin = sine.generate(1.0,1.0);
100 //~ sin.plot("sine");
101 //~
102 //~ Buffer line;
103 //~ line = generator.drawLine(1.0,1.0,-1.0);
104 //~ line.plot("line (0,1) -> (1,-1)");
105 //~
106 //~ line = generator.drawLine(1.0,0.0,-1.0);
107 //~ line.plot("line (0,0) -> (1,-1)");
108 //~
109 //~ line = generator.drawLine(1.0,-1.0,1.0);
110 //~ line.plot("line (0,-1) -> (1,1)");
111 //~
112 //~ line = generator.drawLine(1.0,0.0,1.0);
113 //~ line.plot("line (0,0) -> (1,1)");
114 
115 
116 
117  float64 sr = 44100.0;
118 
119 //~ FFTransform ftr(sr);
120 //~
121 //~ ftr.setWindow(HANNING);
122 //~
123 //~//~ // Six Track
124 //~
125 //~ // Reference
126 //~ Buffer six_trak("../examples/six trak patch.wav");
127 //~
128 //~ FFTChunkVector vec = ftr.fft(six_trak, 4096*2*2*2);
129 //~
130 //~ Buffer six_mag = vec[0].getMagnitude().getdB();
131 //~ Buffer six_x = vec[0].getFrequencyAxis();
132 //~
133 //~ Plotter pylab;
134 //~
135 //~ pylab.plot(six_x, six_mag, "r");
136 //~ pylab.title("six trak");
137 //~
138 //~ pylab.hold();
139 //~
140 //~//~ // Imatation
141 //~
142 //~ float64 pw = 0.0044;
143 //~
144 //~ Pulse p(sr, pw);
145 //~
146 //~ p.setChorus(5, 0.010);
147 //~
148 //~ Buffer b = p.generate(4.0, 133.750); // + std::pow(1.05946, 4.0));
149 //~
150 //~ b -= 0.5;
151 //~
152 //~ FilterLowPassIIR lpf(sr, 2, 3000.0, 0.01);
153 //~
154 //~ b = lpf.filter(b);
155 //~
156 //~ b.normalize();
157 //~
158 //~ b *= 0.66;
159 //~
160 //~ vec = ftr.fft(b.subbuffer(10000), 4096*2*2*2);
161 //~
162 //~ pylab.plot(six_x, vec[0].getMagnitude().getdB(), "b");
163 //~
164 //~ pylab.axisX(0, 6000);
165 
166 //~ b >> "imitation.wav";
167 
168 
169 //~ ////////////////////////////////////////////////////////////////////
170 //~ // Bass Synth
171 //~
172 //~ // Reference
173 //~ Buffer six_trak("../examples/bass.wav");
174 //~
175 //~ FFTChunkVector vec = ftr.fft(six_trak, 4096*2);
176 //~
177 //~ Buffer six_mag = vec[0].getMagnitude().getdB();
178 //~ Buffer six_x = vec[0].getFrequencyAxis();
179 //~
180 //~ Plotter pylab;
181 //~
182 //~ pylab.plot(six_x, six_mag, "r");
183 //~ pylab.title("six trak");
184 //~
185 //~ pylab.axisX(-10, 4000);
186 //~
187 //~ pylab.hold();
188 //~
189 //~//~ // Imatation
190 //~
191 //~ Sawtooth saw1(sr);
192 //~ Sawtooth saw2(sr);
193 //~
194 //~ saw1.setChorus(6, 0.01);
195 //~ saw2.setChorus(6, 0.01);
196 //~
197 //~ saw1.generate(1.0, 110.5);
198 //~ saw2.generate(1.0, 221.0);
199 //~
200 //~ Buffer b = saw1.generate(0.24, 110.5, false)
201 //~ + saw2.generate(0.24, 221.0, false);
202 //~
203 //~ FilterLowPassIIR lpf2(sr, 2, 4000.0, 0.01);
204 //~
205 //~ b = lpf2.filter(b);
206 //~
207 //~ b.normalize();
208 //~
209 //~ // Apply envelope.
210 //~ AdsrEnvelope env(sr, 0.00001, 1.25, 0.25, 0.75, 0.50, 0.0001);
211 //~
212 //~ b = env.shape(b);
213 //~
214 //~ b *= 0.66;
215 //~
216 //~ vec = ftr.fft(b, 4096*2);
217 //~
218 //~ pylab.plot(six_x, vec[0].getMagnitude().getdB(), "b");
219 //~
220 //~ pylab.axisX(-10, 4000);
221 //~
222 //~ b >> "imitation2.wav";
223 
224 
225 //~ Wavefile::setDefaultSampleRate(48000);
226 //~
227 //~ FilterFlanger flanger(48000, 2.0, 0.002);
228 //~ FilterPhaser phaser(48000, 2, 2.5, 0.002, 0.0025);
229 
230 //~ Buffer c("../examples/california.wav");
231 //~ Buffer w("../examples/walle.wav");
232 //~
233 //~ Buffer b;
234 //~ b << c << w;
235 //~
236 //~ Buffer out = flanger.filter(b);
237 //~
238 //~ out >> "flanger.wav";
239 //~
240 //~ out = phaser.filter(b);
241 //~
242 //~ out >> "phaser.wav";
243 
244 
245 //~ Buffer c("../examples/california.wav");
246 //~ Buffer walle("../examples/walle.wav");
247 //~
248 //~ BufferWindowSearch search(walle, 24030);
249 //~
250 //~ Plotter pylab;
251 //~
252 //~ walle.plot("walle.wav");
253 //~
254 //~ uint32 pos = 0;
255 //~
256 //~ std::vector<Buffer> windows;
257 //~
258 //~ while(search.getSamplesLeft() > 0)
259 //~ {
260 //~ pylab.axvline((float64)pos, "color='red'");
261 //~
262 //~ Buffer w = search.nextWindow();
263 //~
264 //~ pos += w.getLength();
265 //~
266 //~ windows.push_back(w);
267 //~ }
268 //~
269 //~ pylab.figure();
270 //~ pylab.hold(true);
271 //~
272 //~ pos = 0;
273 //~
274 //~ Buffer y;
275 //~
276 //~ for(uint32 i = 0; i < windows.size(); ++i)
277 //~ {
278 //~ pylab.axvline((float64)pos, "color='red'");
279 //~
280 //~ y << windows[i];
281 //~
282 //~ pos += windows[i].getLength();
283 //~ }
284 //~
285 //~ y.plot("reconstructed");
286 //~
287 //~ Buffer diff = walle - y;
288 //~
289 //~ cout << "len(walle) = " << walle.getLength() << endl;
290 //~ cout << "len(y) = " << y.getLength() << endl;
291 //~
292 //~ cout << diff.getAbs().getMax() << endl;
293 
297 
298  Buffer input = Buffer("../examples/california.wav")
299  << Buffer("../examples/walle.wav");
300 
301  input >> "input.wav";
302 
303  Buffer data(input);
304 
305  data.resample(1.013);
306 
307 //~ // Create "gold file"
308 //~ data >> "gold_1.013.wav";
309 
310  data >> "data_1.013.wav";
311 
312  Buffer gold("gold_1.013.wav");
313 
314  Buffer diff = gold - data;
315 
316  cout << "len(orig) = " << input.getLength() << endl
317  << "len(data) = " << data.getLength() << endl
318  << "len(gold) = " << gold.getLength() << endl
319  << "Ratio = " <<
320  ( static_cast<float64>(data.getLength())
321  / static_cast<float64>(input.getLength())) << endl
322 
323  << "Max error = " << diff.getAbs().getMax() << endl;
324 
325  diff.plot("gold - data");
326  gold.plot("gold");
327  input.plot("data");
328 
329  Plotter::show();
330 
331  return 0;
332 }
333 
334 
static void show()
Acutally draw the plots to the screen.
Definition: Plotter.cc:252
void testBuffer()
void plot(const std::string &title="Buffer") const
Requires matplotlib. Creates a plot of this Buffer.
Definition: Buffer.cc:1551
void resample(float64 factor)
Resamples this Buffer using discrete-time resampling.
Definition: Buffer.h:1446
Buffer getAbs() const
Modifies a copy of the Buffer by making any negative value positive.
Definition: Buffer.h:174
double float64
Definition: Nsound.h:146
uint32 getLength() const
Returns the number of samples in the Buffer.
Definition: Buffer.h:587
int main(int argc, char **argv)
static void setIEEEFloat(boolean flag)
Definition: Wavefile.cc:98
A Buffer for storing audio samples.
Definition: Buffer.h:60
static void setDefaultSampleRate(const int32 rate)
Definition: Wavefile.cc:70
static void setDefaultSampleSize(uint32 size)
Definition: Wavefile.cc:79
float64 getMax() const
Returns the maximum sample value in the Buffer.
Definition: Buffer.cc:951
float64 sr
Definition: example3.cc:24