The Synthesis ToolKit in C++ (STK)

Home Information Classes Download Usage Mail List Requirements Links FAQ Tutorial

Clarinet.h
1 #ifndef STK_CLARINET_H
2 #define STK_CLARINET_H
3
4 #include "Instrmnt.h"
5 #include "DelayL.h"
6 #include "ReedTable.h"
7 #include "OneZero.h"
8 #include "Envelope.h"
9 #include "Noise.h"
10 #include "SineWave.h"
11
12 namespace stk {
13
14 /***************************************************/
36 /***************************************************/
37
38 class Clarinet : public Instrmnt
39 {
40 public:
42
45 Clarinet( StkFloat lowestFrequency = 8.0 );
46
48 ~Clarinet( void );
49
51 void clear( void );
52
54 void setFrequency( StkFloat frequency );
55
57 void startBlowing( StkFloat amplitude, StkFloat rate );
58
60 void stopBlowing( StkFloat rate );
61
63 void noteOn( StkFloat frequency, StkFloat amplitude );
64
66 void noteOff( StkFloat amplitude );
67
69 void controlChange( int number, StkFloat value );
70
72 StkFloat tick( unsigned int channel = 0 );
73
75
82 StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
83
84 protected:
85
86 DelayL delayLine_;
87 ReedTable reedTable_;
88 OneZero filter_;
89 Envelope envelope_;
90 Noise noise_;
91 SineWave vibrato_;
92
93 StkFloat outputGain_;
94 StkFloat noiseGain_;
95 StkFloat vibratoGain_;
96 };
97
98 inline StkFloat Clarinet :: tick( unsigned int )
99 {
100 StkFloat pressureDiff;
101 StkFloat breathPressure;
102
103 // Calculate the breath pressure (envelope + noise + vibrato)
104 breathPressure = envelope_.tick();
105 breathPressure += breathPressure * noiseGain_ * noise_.tick();
106 breathPressure += breathPressure * vibratoGain_ * vibrato_.tick();
107
108 // Perform commuted loss filtering.
109 pressureDiff = -0.95 * filter_.tick( delayLine_.lastOut() );
110
111 // Calculate pressure difference of reflected and mouthpiece pressures.
112 pressureDiff = pressureDiff - breathPressure;
113
114 // Perform non-linear scattering using pressure difference in reed function.
115 lastFrame_[0] = delayLine_.tick(breathPressure + pressureDiff * reedTable_.tick(pressureDiff));
116
117 // Apply output gain.
118 lastFrame_[0] *= outputGain_;
119
120 return lastFrame_[0];
121 }
122
123 inline StkFrames& Clarinet :: tick( StkFrames& frames, unsigned int channel )
124 {
125 unsigned int nChannels = lastFrame_.channels();
126 #if defined(_STK_DEBUG_)
127 if ( channel > frames.channels() - nChannels ) {
128 oStream_ << "Clarinet::tick(): channel and StkFrames arguments are incompatible!";
129 handleError( StkError::FUNCTION_ARGUMENT );
130 }
131 #endif
132
133 StkFloat *samples = &frames[channel];
134 unsigned int j, hop = frames.channels() - nChannels;
135 if ( nChannels == 1 ) {
136 for ( unsigned int i=0; i<frames.frames(); i++, samples += hop )
137 *samples++ = tick();
138 }
139 else {
140 for ( unsigned int i=0; i<frames.frames(); i++, samples += hop ) {
141 *samples++ = tick();
142 for ( j=1; j<nChannels; j++ )
143 *samples++ = lastFrame_[j];
144 }
145 }
146
147 return frames;
148 }
149
150 } // stk namespace
151
152 #endif

The Synthesis ToolKit in C++ (STK)
©1995--2017 Perry R. Cook and Gary P. Scavone. All Rights Reserved.