Nsound  0.9.4
test_pluck.cc
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------
2 //
3 // $Id: test_pluck.cc 874 2014-09-08 02:21:29Z weegreenblobbie $
4 //
5 // Copyright (c) 2005-2007 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 "Test.h"
32 
33 #include <math.h>
34 #include <iostream>
35 
36 using namespace Nsound;
37 
38 using std::cerr;
39 using std::cout;
40 using std::endl;
41 using std::flush;
42 
43 #define THIS_FILE "test_pluck.cc"
44 
45 int main(int argc, char ** argv)
46 {
47  Pluck pluck(44100, 1024);
48 
49  AudioStream output(44100, 1);
50 
51  cout << TEST_HEADER
52  << "pluck.generate() ..." << flush;
53 
54  Tic();
55 
56  Buffer temp = pluck.generate(2.0, 246.94);
57 
58  cout << " " << Toc() << " seconds" << endl << flush;
59 
60  output << temp;
61  output << temp.getReverse() << pluck.silence(1.0);
62 
63  cout << TEST_HEADER
64  << "pluck.generate(): plucking 6 strings: guitar chord E ..."
65  << flush;
66 
67  Tic();
68 
69  Buffer string6 = pluck.generate(2.0, 82.41); // E2 C4 = middle C
70  Buffer string5 = pluck.generate(2.0, 123.47); // B2
71  Buffer string4 = pluck.generate(2.0, 164.81); // E3
72  Buffer string3 = pluck.generate(2.0, 207.65); // G3#
73  Buffer string2 = pluck.generate(2.0, 246.94); // B3
74  Buffer string1 = pluck.generate(2.0, 329.63); // E4
75 
76  // add in each string with a little delay
77 
78  uint32 offset = static_cast<uint32>(0.150 * 44100.0);
79 
80  Buffer final(string6);
81 
82  final.add(string5, offset );
83  final.add(string4, offset * 2);
84  final.add(string3, offset * 3);
85  final.add(string2, offset * 4);
86  final.add(string1, offset * 5);
87 
88  final.normalize();
89 
90  cout << " " << Toc() << " seconds" << endl << flush;
91 
92  output << final;
93  output << final.getReverse()
94  << pluck.silence(0.5);
95 
96  output >> "test_pluck.wav";
97 
98  return 0;
99 }
unsigned int uint32
Definition: Nsound.h:153
Implements a simple Karplus-Strong String Synthesis algorithim.
Definition: Pluck.h:42
int main(int argc, char **argv)
Definition: test_pluck.cc:45
#define TEST_HEADER
Definition: Test.h:45
Buffer silence(const float64 &duration) const
This method generates silence.
Definition: Generator.cc:1310
A Buffer for storing audio samples.
Definition: Buffer.h:60
Nsound::float64 Toc()
Definition: TicToc.cc:42
Buffer generate(const float64 &duration, const float64 &frequency)
Implements simple Karplus-Strong plucked string.
Definition: Pluck.cc:59
void Tic()
Definition: TicToc.cc:37
void add(const Buffer &buffer, uint32 offset=0, uint32 n_samples=0)
This method adds buffer to *this.
Definition: Buffer.cc:225
AudioStream getReverse() const
Reverses the samples in the AudioStream.
Definition: AudioStream.h:433