1 /***************************************************/
2 /*! \class Flute
3     \brief STK flute physical model class.
4 
5     This class implements a simple flute
6     physical model, as discussed by Karjalainen,
7     Smith, Waryznyk, etc.  The jet model uses
8     a polynomial, a la Cook.
9 
10     This is a digital waveguide model, making its
11     use possibly subject to patents held by Stanford
12     University, Yamaha, and others.
13 
14     Control Change Numbers:
15        - Jet Delay = 2
16        - Noise Gain = 4
17        - Vibrato Frequency = 11
18        - Vibrato Gain = 1
19        - Breath Pressure = 128
20 
21     by Perry R. Cook and Gary P. Scavone, 1995 - 2005.
22 */
23 /***************************************************/
24 
25 #include "Flute.h"
26 #include "SKINI.msg"
27 
28 using namespace Nyq;
29 
Flute(StkFloat lowestFrequency)30 Flute :: Flute(StkFloat lowestFrequency)
31 {
32   length_ = (unsigned long) (Stk::sampleRate() / lowestFrequency + 1);
33   boreDelay_.setMaximumDelay( length_ );
34   boreDelay_.setDelay( 100.0 );
35 
36   length_ >>= 1;
37   jetDelay_.setMaximumDelay( length_ );
38   jetDelay_.setDelay( 49.0 );
39 
40   vibrato_.setFrequency( 5.925 );
41 
42   this->clear();
43 
44   filter_.setPole( 0.7 - ((StkFloat) 0.1 * 22050.0 / Stk::sampleRate() ) );
45   filter_.setGain( -1.0 );
46 
47   dcBlock_.setBlockZero();
48 
49   adsr_.setAllTimes( 0.005, 0.01, 0.8, 0.010);
50   endReflection_ = 0.5;
51   jetReflection_ = 0.5;
52   noiseGain_     = 0.15;    // Breath pressure random component.
53   vibratoGain_   = 0.05;    // Breath periodic vibrato component.
54   jetRatio_      = 0.32;
55 
56 	maxPressure_ = 0.0;
57   lastFrequency_ = 220.0;
58 }
59 
~Flute()60 Flute :: ~Flute()
61 {
62 }
63 
clear()64 void Flute :: clear()
65 {
66   jetDelay_.clear();
67   boreDelay_.clear();
68   filter_.clear();
69   dcBlock_.clear();
70 }
71 
setFrequency(StkFloat frequency)72 void Flute :: setFrequency(StkFloat frequency)
73 {
74   lastFrequency_ = frequency;
75   if ( frequency <= 0.0 ) {
76     errorString_ << "Flute::setFrequency: parameter is less than or equal to zero!";
77     handleError( StkError::WARNING );
78     lastFrequency_ = 220.0;
79   }
80 
81   // We're overblowing here.
82   lastFrequency_ *= 0.66666;
83 
84   // delay = length - approximate filter delay.
85   StkFloat delay = Stk::sampleRate() / lastFrequency_ - (StkFloat) 2.0;
86   if ( delay <= 0.0 ) delay = 0.3;
87   else if ( delay > length_ ) delay = length_;
88 
89   boreDelay_.setDelay(delay);
90   jetDelay_.setDelay(delay * jetRatio_);
91 }
92 
startBlowing(StkFloat amplitude,StkFloat rate)93 void Flute :: startBlowing(StkFloat amplitude, StkFloat rate)
94 {
95   adsr_.setAttackRate( rate );
96   maxPressure_ = amplitude / (StkFloat) 0.8;
97   adsr_.keyOn();
98 }
99 
stopBlowing(StkFloat rate)100 void Flute :: stopBlowing(StkFloat rate)
101 {
102   adsr_.setReleaseRate( rate );
103   adsr_.keyOff();
104 }
105 
noteOn(StkFloat frequency,StkFloat amplitude)106 void Flute :: noteOn(StkFloat frequency, StkFloat amplitude)
107 {
108   this->setFrequency( frequency );
109   this->startBlowing( 1.1 + (amplitude * 0.20), amplitude * 0.02 );
110   outputGain_ = amplitude + 0.001;
111 
112 #if defined(_STK_DEBUG_)
113   errorString_ << "Flute::NoteOn: frequency = " << frequency << ", amplitude = " << amplitude << ".";
114   handleError( StkError::DEBUG_WARNING );
115 #endif
116 }
117 
noteOff(StkFloat amplitude)118 void Flute :: noteOff(StkFloat amplitude)
119 {
120   this->stopBlowing( amplitude * 0.02 );
121 
122 #if defined(_STK_DEBUG_)
123   errorString_ << "Flute::NoteOff: amplitude = " << amplitude << ".";
124   handleError( StkError::DEBUG_WARNING );
125 #endif
126 }
127 
setJetReflection(StkFloat coefficient)128 void Flute :: setJetReflection(StkFloat coefficient)
129 {
130   jetReflection_ = coefficient;
131 }
132 
setEndReflection(StkFloat coefficient)133 void Flute :: setEndReflection(StkFloat coefficient)
134 {
135   endReflection_ = coefficient;
136 }
137 
setJetDelay(StkFloat aRatio)138 void Flute :: setJetDelay(StkFloat aRatio)
139 {
140   // Delay = length - approximate filter delay.
141   StkFloat temp = Stk::sampleRate() / lastFrequency_ - (StkFloat) 2.0;
142   jetRatio_ = aRatio;
143   jetDelay_.setDelay(temp * aRatio); // Scaled by ratio.
144 }
145 
computeSample()146 StkFloat Flute :: computeSample()
147 {
148   StkFloat pressureDiff;
149   StkFloat breathPressure;
150 
151   // Calculate the breath pressure (envelope + noise + vibrato)
152   breathPressure = maxPressure_ * adsr_.tick();
153   breathPressure += breathPressure * ( noiseGain_ * noise_.tick() + vibratoGain_ * vibrato_.tick() );
154   //breathPressure += breathPressure * vibratoGain_ * vibrato_.tick();
155 
156   StkFloat temp = filter_.tick( boreDelay_.lastOut() );
157   temp = dcBlock_.tick( temp ); // Block DC on reflection.
158 
159   pressureDiff = breathPressure - (jetReflection_ * temp);
160   pressureDiff = jetDelay_.tick( pressureDiff );
161   pressureDiff = jetTable_.tick( pressureDiff ) + (endReflection_ * temp);
162   lastOutput_ = (StkFloat) 0.3 * boreDelay_.tick( pressureDiff );
163 
164   lastOutput_ *= outputGain_;
165   return lastOutput_;
166 }
167 
controlChange(int number,StkFloat value)168 void Flute :: controlChange(int number, StkFloat value)
169 {
170   StkFloat norm = value * ONE_OVER_128;
171   if ( norm < 0 ) {
172     norm = 0.0;
173     errorString_ << "Flute::controlChange: control value less than zero ... setting to zero!";
174     handleError( StkError::WARNING );
175   }
176   else if ( norm > 1.0 ) {
177     norm = 1.0;
178     errorString_ << "Flute::controlChange: control value greater than 128.0 ... setting to 128.0!";
179     handleError( StkError::WARNING );
180   }
181 
182   if (number == __SK_JetDelay_) // 2
183     this->setJetDelay( (StkFloat) (0.08 + (0.48 * norm)) );
184   else if (number == __SK_NoiseLevel_) // 4
185     noiseGain_ = ( norm * 0.4);
186   else if (number == __SK_ModFrequency_) // 11
187     vibrato_.setFrequency( norm * 12.0);
188   else if (number == __SK_ModWheel_) // 1
189     vibratoGain_ = ( norm * 0.4 );
190   else if (number == __SK_AfterTouch_Cont_) // 128
191     adsr_.setTarget( norm );
192   else {
193     errorString_ << "Flute::controlChange: undefined control number (" << number << ")!";
194     handleError( StkError::WARNING );
195   }
196 
197 #if defined(_STK_DEBUG_)
198     errorString_ << "Flute::controlChange: number = " << number << ", value = " << value << ".";
199     handleError( StkError::DEBUG_WARNING );
200 #endif
201 }
202