Nsound  0.9.4
GuitarBass.cc
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------
2 //
3 // $Id: GuitarBass.cc 874 2014-09-08 02:21:29Z weegreenblobbie $
4 //
5 // Copyright (c) 2004-Present 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>
33 #include <Nsound/FilterTone.h>
34 #include <Nsound/GuitarBass.h>
35 #include <Nsound/Plotter.h>
36 #include <Nsound/Triangle.h>
37 
38 using namespace Nsound;
39 
40 //-----------------------------------------------------------------------------
42 GuitarBass(const float64 & sample_rate)
43  :
44  Instrument(sample_rate)
45 {
46 }
47 
48 //-----------------------------------------------------------------------------
51 {
52 }
53 
57 {
59 
60  // duration, freq
61  y << play(0.450, 61.734)
62  << 0.90 * play(0.300, 123.467)
63  << 0.95 * play(0.150, 92.491)
64  << 0.80 * play(0.450, 61.734)
65  << 0.80 * play(0.450, 46.245)
66  << 0.80 * play(0.450, 55.000)
67  << 0.80 * play(0.450, 61.734)
68  << play(0.450, 73.414)
69  << play(0.650, 61.734);
70 
71  return y;
72 }
73 
76 play(const float64 & duration, const float64 & frequency)
77 {
79 
80  Buffer outenv = tri.drawLine(0.01, 0.0, 1.0)
81  << tri.drawLine(duration - 0.11, 1.0, 1.0)
82  << tri.drawLine(0.11, 1.0, 0.0);
83 
84  // draw a tirangle envlope
85  float64 triangle_duration = 1.0 / (2.0 * frequency);
86  Buffer triangle = -1.0 * tri.generate(triangle_duration, 2.0 * frequency)
87  << tri.drawLine(duration - triangle_duration, 0.0,0.0);
88 
89  // this envlope is for the body resonance
90  Buffer envres = tri.drawLine(0.1, 0.0, 1.0)
91  << tri.drawLine(duration - 0.1, 1.0, 1.0);
92 
93  FilterTone tone(sample_rate_, frequency * frequency / 10.0);
94 
95  DelayLine delay(sample_rate_, 1.0 / frequency);
96 
97  uint32 n_samples = static_cast<uint32>(duration * sample_rate_);
98 
99  float64 filter_out = 0.0;
100 
102 
103  Buffer::circular_iterator tri_iter = triangle.cbegin();
104 
105  filter_out = tone.filter(0);
106  for(uint32 n = 0; n < n_samples; ++n, ++tri_iter)
107  {
108  delay.write(filter_out + *tri_iter);
109  filter_out = tone.filter(delay.read());
110  y << filter_out;
111  }
112 
113  // Add some resonance from the body
114 
115  FilterBandPassIIR body1(sample_rate_, 2, 120.0, 200.0, 0.01);
116  FilterBandPassIIR body2(sample_rate_, 2, 60.0, 100.0, 0.01);
117 
118  y += 0.0001 * body1.filter(y)
119  + 0.00004 * body2.filter(y);
120 
121  y *= outenv;
122 
123  return y;
124 }
125 
126 
127 // :mode=c++: jEdit modeline
Triangle generator.
Definition: Triangle.h:48
unsigned int uint32
Definition: Nsound.h:153
~GuitarBass()
Destructor.
Definition: GuitarBass.cc:50
A circulator iterator for class Buffer.
GuitarBass(const float64 &sample_rate)
Creates a Bass Kick Drum.
Definition: GuitarBass.cc:42
double float64
Definition: Nsound.h:146
circular_iterator cbegin()
Retruns the itreator at the start of the Buffer.
Definition: Buffer.h:318
virtual float64 generate(const float64 &frequency)
This is a real-time method for the wavetable oscillator.
Definition: Generator.cc:972
float64 read()
Definition: DelayLine.cc:97
AudioStream play()
Plays a demo for this instrument.
Definition: GuitarBass.cc:56
The Nsound Instrument baseclass. All Nsound instruments extend this class.
Definition: Instrument.h:50
float64 sample_rate_
Definition: Instrument.h:76
A Buffer for storing audio samples.
Definition: Buffer.h:60
AudioStream filter(const AudioStream &x)
Definition: FilterTone.cc:63
AudioStream filter(const AudioStream &x)
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
void write(float64 x)
Definition: DelayLine.cc:133