Nsound  0.9.4
DrumBD01.cc
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------
2 //
3 // $Id: DrumBD01.cc 874 2014-09-08 02:21:29Z weegreenblobbie $
4 //
5 // Copyright (c) 2009-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/DrumBD01.h>
33 #include <Nsound/FilterTone.h>
34 #include <Nsound/Sine.h>
35 
36 using namespace Nsound;
37 
38 //-----------------------------------------------------------------------------
40 DrumBD01(const float64 & sample_rate)
41  :
42  Instrument(sample_rate){}
43 
44 //-----------------------------------------------------------------------------
47 
51 {
53 
54  y << play(0.5, 50.0, 0.25)
55  << play(0.5, 50.0, 0.50)
56  << play(0.5, 50.0, 0.75)
57  << play(0.5, 50.0, 1.0);
58 
59  return y;
60 }
61 
65  const float64 & duration,
66  const float64 & frequency)
67 {
68  return play(duration, frequency, 1.0);
69 }
70 
71 
75  const float64 & duration,
76  const float64 & frequency,
77  const float64 & velocity)
78 {
79  float64 v = velocity;
80 
81  if(v > 1.0) v = 1.0;
82  if(v < 0.0) v = 0.0;
83 
84  float64 fdec = 0.025 * (v * v + 0.1072);
85 
86  // Create a sine generator
87  Sine sin(sample_rate_);
88 
89  Buffer kcps1 = sin.drawDecay(10.0*fdec, 7.0)
90  << sin.drawLine(duration - fdec, 0.0, 0.0);
91 
92  Buffer kcps2 = sin.drawDecay(10.0*0.32, 7.0)
93  << sin.drawLine(duration - 0.32, 0.0, 0.0);
94 
95  Buffer kcps = (4.3333 * kcps1 + kcps2) * frequency;
96 
97  Buffer klpf = sin.drawDecay(10.0*0.04, 7.0)
98  << sin.drawLine(duration - 0.04, 0.0, 0.0);
99 
100  Buffer klpf2 = 8.0 * sin.drawDecay(10.0*0.0005, 7.0)
101  << sin.drawLine(duration - 0.0005, 0.0, 0.0);
102 
103  Buffer kamp = 1.0
104  - 0.5*sin.drawDecay(0.15, 7.0)
105  << sin.drawLine(duration - 0.15, 1.0, 1.0);
106 
107  Buffer a1 = sin.generate(duration, kcps);
108 
109  a1.limit(-0.25, 0.25);
110 
111  Buffer freq = kcps * (0.5 + klpf2);
112 
113  FilterTone tone(sample_rate_, freq[0]);
114 
115  a1 = tone.filter(a1, freq);
116 
117  Buffer a2 = sin.whiteNoise(duration) * sin.whiteNoise(duration);
118 
119  tone.makeKernel(500.0);
120 
121  a2 -= tone.filter(a2);
122 
123  freq = 4.0 * (klpf + klpf2) * kcps;
124 
125  FilterLowPassIIR lpf(sample_rate_, 2, freq[0], 0.01);
126 
127  a1 = lpf.filter(kamp * a1, freq);
128 
129  a2 = lpf.filter(kamp * a2, kcps);
130  a2 *= 5.0 * klpf.getSqrt();
131 
132  a1 += a2 * 1.0;
133 
134  a1.normalize();
135 
136  Buffer env = sin.drawLine(duration - 0.01, 1.0, 1.0)
137  << sin.drawLine(0.005, 1.0, 0.0)
138  << sin.drawLine(0.005, 0.0, 0.0);
139 
140  AudioStream as(sample_rate_, 1);
141 
142  as << env * a1;
143 
144  return as;
145 }
146 
147 // :mode=c++: jEdit modeline
AudioStream filter(const AudioStream &x)
void limit(float64 min, float64 max)
Limits the Buffer to min and max.
Definition: Buffer.cc:876
AudioStream play()
Plays a demo for this instrument.
Definition: DrumBD01.cc:50
double float64
Definition: Nsound.h:146
DrumBD01(const float64 &sample_rate)
Creates a Drum.
Definition: DrumBD01.cc:40
virtual float64 generate(const float64 &frequency)
This is a real-time method for the wavetable oscillator.
Definition: Generator.cc:972
Buffer drawDecay(const float64 &duration, const float64 &alpha=2.0 *M_PI) const
This method draws an exponential curve that decays from 1.0 to 0.0 over the duration.
Definition: Generator.cc:388
A class for filtering audio in the frequecy domain.
void normalize()
Multiplies the Buffer by a constant gain so the peak sample has magnitude 1.0.
Definition: Buffer.cc:1064
~DrumBD01()
Destructor.
Definition: DrumBD01.cc:46
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
Buffer getSqrt() const
Taks the square root of each sample in a copy of this Buffer.
Definition: Buffer.h:1779
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
DOXME.
Definition: Sine.h:43