Nsound  0.9.4
FluteSlide.cc
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------
2 //
3 // $Id: FluteSlide.cc 874 2014-09-08 02:21:29Z weegreenblobbie $
4 //
5 // Copyright (c) 2004-2008 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/FilterTone.h>
33 #include <Nsound/FluteSlide.h>
34 #include <Nsound/Sine.h>
35 
36 using namespace Nsound;
37 
38 //-----------------------------------------------------------------------------
40 FluteSlide(const float64 & sample_rate)
41  :
42  Instrument(sample_rate)
43 {
44 }
45 
46 //-----------------------------------------------------------------------------
49 {
50 }
51 
55 {
57 
58  // duration, freq
59  y << play(0.600, 261.616)
60  << play(0.600, 293.656)
61  << play(0.600, 329.609)
62  << play(0.600, 349.218)
63  << play(0.600, 391.973)
64  << play(0.600, 445.000);
65 
66  return y;
67 }
68 
71 play(const float64 & duration, const float64 & frequency)
72 {
73  return play(duration, frequency, 1.0, 0.05);
74 }
75 
79  const float64 & duration,
80  const float64 & frequency,
81  const float64 & pressure,
82  const float64 & breath)
83 {
84  Sine sine(sample_rate_);
85 
86  float64 n_delay_seconds = 1.0 / frequency;
87 
88  DelayLine delay1(sample_rate_, n_delay_seconds / 2.0);
89  DelayLine delay2(sample_rate_, n_delay_seconds);
90 
91  FilterTone tone(sample_rate_, 2000.0);
92 
93 
94  Buffer env1 = sine.drawLine(0.1, 0.0, pressure)
95  << sine.drawLine(duration - 0.2, pressure, pressure)
96  << sine.drawLine(0.1, pressure, 0.0);
97 
98  Buffer env2 = sine.drawLine(0.01, 0.0, 1.0)
99  << sine.drawLine(duration - 0.02, 1.0, 1.0)
100  << sine.drawLine(0.01, 1.0, 0.0) << 0.0;
101 
102  Buffer flow = env1 * sine.whiteNoise(duration);
103 
104  Buffer input = breath * flow + env1;
105 
106  float64 flute = 0.0;
107 
108  uint32 n_samples = input.getLength();
109 
111 
112  for(uint32 n = 0; n < n_samples; ++n)
113  {
114  float64 x = input[n] + flute * 0.35;
115 
116  delay1.write(x);
117 
118  x = delay1.read();
119 
120  x -= x * x * x;
121 
122  x += flute * 0.4;
123 
124  x = tone.filter(x);
125 
126  y << x;
127 
128  delay2.write(x);
129 
130  flute = delay2.read();
131  }
132 
133  y *= env2 * duration;
134 
135  y.normalize();
136 
137  return y;
138 }
139 
140 
141 // :mode=c++: jEdit modeline
~FluteSlide()
Destructor.
Definition: FluteSlide.cc:48
unsigned int uint32
Definition: Nsound.h:153
double float64
Definition: Nsound.h:146
uint32 getLength() const
Returns the number of samples in the Buffer.
Definition: Buffer.h:587
void normalize()
Multiplies the AudioStream by a constant gain so the peak sample has magnitude 1.0.
Definition: AudioStream.cc:285
float64 read()
Definition: DelayLine.cc:97
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
Buffer whiteNoise(const float64 &duration) const
This method generates noise from a uniform distribution.
Definition: Generator.cc:1325
AudioStream filter(const AudioStream &x)
Definition: FilterTone.cc:63
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 play()
Plays a demo for this instrument.
Definition: FluteSlide.cc:54
DOXME.
Definition: Sine.h:43
void write(float64 x)
Definition: DelayLine.cc:133
FluteSlide(const float64 &sample_rate)
Creates a Bass Kick Drum.
Definition: FluteSlide.cc:40