Nsound  0.9.4
Pluck.cc
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------
2 //
3 // $Id: Pluck.cc 874 2014-09-08 02:21:29Z weegreenblobbie $
4 //
5 // Copyright (c) 2004-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/AudioStream.h>
30 #include <Nsound/Buffer.h>
31 #include <Nsound/DelayLine.h>
32 #include <Nsound/FilterDC.h>
33 #include <Nsound/FilterTone.h>
34 #include <Nsound/Pluck.h>
35 
36 #include <cmath>
37 
38 using namespace Nsound;
39 
40 //-----------------------------------------------------------------------------
42 Pluck::
43 Pluck(const float64 & sample_rate, uint32 n_smooth_samples)
44  :
45  Generator(sample_rate),
46  n_smooth_samples_(n_smooth_samples)
47 {
48 }
49 
50 //-----------------------------------------------------------------------------
52 Pluck::
54 {
55 }
56 
57 Buffer
58 Pluck::
59 generate(const float64 & duration, const float64 & frequency)
60 {
61  DelayLine delay(sample_rate_, 1.0 / frequency);
62  FilterDC dc_filter(0.99);
63  FilterTone tone(sample_rate_, 14 * frequency * duration);
64 
65  Buffer noise_env = drawLine(1.0 / frequency, 1.0, 1.0)
66  << drawLine(duration - (1.0 / frequency), 0.0, 0.0)
67  << 0.0;
68 
69  Buffer x = whiteNoise(duration) * noise_env * 2.0;
70 
72  x.normalize();
73 
74  uint32 n_samples = static_cast<int32>(duration * sample_rate_);
75 
76  Buffer y;
77 
78  delay.write(x[0]);
79  for(uint32 n = 0; n < n_samples; ++n)
80  {
81 
82  y << dc_filter.filter(tone.filter(delay.read()));
83 
84  delay.write(x[n] + y[n]);
85  }
86  y.normalize();
87 
88  return y;
89 }
90 
float64 sample_rate_
Used to determine when to create a sync sample.
Definition: Generator.h:616
unsigned int uint32
Definition: Nsound.h:153
uint32 n_smooth_samples_
Definition: Pluck.h:84
Pluck(const float64 &sample_rate, uint32 n_smooth_samples)
Definition: Pluck.cc:43
double float64
Definition: Nsound.h:146
void smooth(uint32 n_passes, uint32 n_samples_per_average)
Applies a moving average filter to smooth this Buffer.
Definition: Buffer.cc:1981
float64 read()
Definition: DelayLine.cc:97
void normalize()
Multiplies the Buffer by a constant gain so the peak sample has magnitude 1.0.
Definition: Buffer.cc:1064
A Buffer for storing audio samples.
Definition: Buffer.h:60
Buffer whiteNoise(const float64 &duration) const
This method generates noise from a uniform distribution.
Definition: Generator.cc:1325
signed int int32
Definition: Nsound.h:142
AudioStream filter(const AudioStream &x)
Definition: FilterTone.cc:63
Buffer generate(const float64 &duration, const float64 &frequency)
Implements simple Karplus-Strong plucked string.
Definition: Pluck.cc:59
Buffer drawLine(const float64 &duration, const float64 &amplitude_start, const float64 &amplitude_finish) const
This method draws a linear line beteween 2 points.
Definition: Generator.cc:464
AudioStream filter(const AudioStream &x)
Definition: FilterDC.cc:47
A class the provides draw utilities and a wavetable oscillator.
Definition: Generator.h:50
void write(float64 x)
Definition: DelayLine.cc:133
virtual ~Pluck()
Definition: Pluck.cc:53