| 1 |
/***************************************************/ |
| 2 |
/*! \class Saxofony |
| 3 |
\brief STK faux conical bore reed instrument class. |
| 4 |
|
| 5 |
This class implements a "hybrid" digital |
| 6 |
waveguide instrument that can generate a |
| 7 |
variety of wind-like sounds. It has also been |
| 8 |
referred to as the "blowed string" model. The |
| 9 |
waveguide section is essentially that of a |
| 10 |
string, with one rigid and one lossy |
| 11 |
termination. The non-linear function is a |
| 12 |
reed table. The string can be "blown" at any |
| 13 |
point between the terminations, though just as |
| 14 |
with strings, it is impossible to excite the |
| 15 |
system at either end. If the excitation is |
| 16 |
placed at the string mid-point, the sound is |
| 17 |
that of a clarinet. At points closer to the |
| 18 |
"bridge", the sound is closer to that of a |
| 19 |
saxophone. See Scavone (2002) for more details. |
| 20 |
|
| 21 |
This is a digital waveguide model, making its |
| 22 |
use possibly subject to patents held by Stanford |
| 23 |
University, Yamaha, and others. |
| 24 |
|
| 25 |
Control Change Numbers: |
| 26 |
- Reed Stiffness = 2 |
| 27 |
- Reed Aperture = 26 |
| 28 |
- Noise Gain = 4 |
| 29 |
- Blow Position = 11 |
| 30 |
- Vibrato Frequency = 29 |
| 31 |
- Vibrato Gain = 1 |
| 32 |
- Breath Pressure = 128 |
| 33 |
|
| 34 |
by Perry R. Cook and Gary P. Scavone, 1995 - 2005. |
| 35 |
*/ |
| 36 |
/***************************************************/ |
| 37 |
|
| 38 |
#include "Saxofony.h" |
| 39 |
#include "SKINI.msg" |
| 40 |
|
| 41 |
using namespace Nyq; |
| 42 |
|
| 43 |
Saxofony :: Saxofony(StkFloat lowestFrequency) |
| 44 |
{ |
| 45 |
length_ = (unsigned long) (Stk::sampleRate() / lowestFrequency + 1); |
| 46 |
// Initialize blowing position to 0.2 of length / 2. |
| 47 |
position_ = 0.2; |
| 48 |
delays_[0].setMaximumDelay( length_ ); |
| 49 |
delays_[0].setDelay( (1.0-position_) * (length_ >> 1) ); |
| 50 |
delays_[1].setMaximumDelay( length_ ); |
| 51 |
delays_[1].setDelay( (1.0-position_) * (length_ >> 1) ); |
| 52 |
|
| 53 |
reedTable_.setOffset( 0.7 ); |
| 54 |
reedTable_.setSlope( 0.3 ); |
| 55 |
|
| 56 |
vibrato_.setFrequency((StkFloat) 5.735); |
| 57 |
|
| 58 |
outputGain_ = 0.3; |
| 59 |
noiseGain_ = 0.2; |
| 60 |
vibratoGain_ = 0.1; |
| 61 |
} |
| 62 |
|
| 63 |
Saxofony :: ~Saxofony() |
| 64 |
{ |
| 65 |
} |
| 66 |
|
| 67 |
void Saxofony :: clear() |
| 68 |
{ |
| 69 |
delays_[0].clear(); |
| 70 |
delays_[1].clear(); |
| 71 |
filter_.clear(); |
| 72 |
} |
| 73 |
|
| 74 |
void Saxofony :: setFrequency(StkFloat frequency) |
| 75 |
{ |
| 76 |
StkFloat freakency = frequency; |
| 77 |
if ( frequency <= 0.0 ) { |
| 78 |
errorString_ << "Saxofony::setFrequency: parameter is less than or equal to zero!"; |
| 79 |
handleError( StkError::WARNING ); |
| 80 |
freakency = 220.0; |
| 81 |
} |
| 82 |
|
| 83 |
StkFloat delay = (Stk::sampleRate() / freakency) - (StkFloat) 3.0; |
| 84 |
if (delay <= 0.0) delay = 0.3; |
| 85 |
else if (delay > length_) delay = length_; |
| 86 |
|
| 87 |
delays_[0].setDelay( (1.0-position_) * delay ); |
| 88 |
delays_[1].setDelay( position_ * delay ); |
| 89 |
} |
| 90 |
|
| 91 |
void Saxofony :: setBlowPosition(StkFloat position) |
| 92 |
{ |
| 93 |
if ( position_ == position ) return; |
| 94 |
|
| 95 |
if ( position < 0.0 ) position_ = 0.0; |
| 96 |
else if ( position > 1.0 ) position_ = 1.0; |
| 97 |
else position_ = position; |
| 98 |
|
| 99 |
StkFloat totalDelay = delays_[0].getDelay(); |
| 100 |
totalDelay += delays_[1].getDelay(); |
| 101 |
|
| 102 |
delays_[0].setDelay( (1.0-position_) * totalDelay ); |
| 103 |
delays_[1].setDelay( position_ * totalDelay ); |
| 104 |
} |
| 105 |
|
| 106 |
void Saxofony :: startBlowing(StkFloat amplitude, StkFloat rate) |
| 107 |
{ |
| 108 |
envelope_.setRate( rate ); |
| 109 |
envelope_.setTarget( amplitude ); |
| 110 |
} |
| 111 |
|
| 112 |
void Saxofony :: stopBlowing(StkFloat rate) |
| 113 |
{ |
| 114 |
envelope_.setRate( rate ); |
| 115 |
envelope_.setTarget( 0.0 ); |
| 116 |
} |
| 117 |
|
| 118 |
void Saxofony :: noteOn(StkFloat frequency, StkFloat amplitude) |
| 119 |
{ |
| 120 |
this->setFrequency( frequency ); |
| 121 |
this->startBlowing( 0.55 + (amplitude * 0.30), amplitude * 0.005 ); |
| 122 |
outputGain_ = amplitude + 0.001; |
| 123 |
|
| 124 |
#if defined(_STK_DEBUG_) |
| 125 |
errorString_ << "Saxofony::NoteOn: frequency = " << frequency << ", amplitude = " << amplitude << "."; |
| 126 |
handleError( StkError::DEBUG_WARNING ); |
| 127 |
#endif |
| 128 |
} |
| 129 |
|
| 130 |
void Saxofony :: noteOff(StkFloat amplitude) |
| 131 |
{ |
| 132 |
this->stopBlowing( amplitude * 0.01 ); |
| 133 |
|
| 134 |
#if defined(_STK_DEBUG_) |
| 135 |
errorString_ << "Saxofony::NoteOff: amplitude = " << amplitude << "."; |
| 136 |
handleError( StkError::DEBUG_WARNING ); |
| 137 |
#endif |
| 138 |
} |
| 139 |
|
| 140 |
StkFloat Saxofony :: computeSample() |
| 141 |
{ |
| 142 |
StkFloat pressureDiff; |
| 143 |
StkFloat breathPressure; |
| 144 |
StkFloat temp; |
| 145 |
|
| 146 |
// Calculate the breath pressure (envelope + noise + vibrato) |
| 147 |
breathPressure = envelope_.tick(); |
| 148 |
breathPressure += breathPressure * noiseGain_ * noise_.tick(); |
| 149 |
breathPressure += breathPressure * vibratoGain_ * vibrato_.tick(); |
| 150 |
|
| 151 |
temp = -0.95 * filter_.tick( delays_[0].lastOut() ); |
| 152 |
lastOutput_ = temp - delays_[1].lastOut(); |
| 153 |
pressureDiff = breathPressure - lastOutput_; |
| 154 |
delays_[1].tick( temp ); |
| 155 |
delays_[0].tick( breathPressure - (pressureDiff * reedTable_.tick(pressureDiff)) - temp ); |
| 156 |
|
| 157 |
lastOutput_ *= outputGain_; |
| 158 |
return lastOutput_; |
| 159 |
} |
| 160 |
|
| 161 |
void Saxofony :: controlChange(int number, StkFloat value) |
| 162 |
{ |
| 163 |
StkFloat norm = value * ONE_OVER_128; |
| 164 |
if ( norm < 0 ) { |
| 165 |
norm = 0.0; |
| 166 |
errorString_ << "Saxofony::controlChange: control value less than zero ... setting to zero!"; |
| 167 |
handleError( StkError::WARNING ); |
| 168 |
} |
| 169 |
else if ( norm > 1.0 ) { |
| 170 |
norm = 1.0; |
| 171 |
errorString_ << "Saxofony::controlChange: control value greater than 128.0 ... setting to 128.0!"; |
| 172 |
handleError( StkError::WARNING ); |
| 173 |
} |
| 174 |
|
| 175 |
if (number == __SK_ReedStiffness_) // 2 |
| 176 |
reedTable_.setSlope( 0.1 + (0.4 * norm) ); |
| 177 |
else if (number == __SK_NoiseLevel_) // 4 |
| 178 |
noiseGain_ = ( norm * 0.4 ); |
| 179 |
else if (number == 29) // 29 |
| 180 |
vibrato_.setFrequency( norm * 12.0 ); |
| 181 |
else if (number == __SK_ModWheel_) // 1 |
| 182 |
vibratoGain_ = ( norm * 0.5 ); |
| 183 |
else if (number == __SK_AfterTouch_Cont_) // 128 |
| 184 |
envelope_.setValue( norm ); |
| 185 |
else if (number == 11) // 11 |
| 186 |
this->setBlowPosition( norm ); |
| 187 |
else if (number == 26) // reed table offset |
| 188 |
reedTable_.setOffset(0.4 + ( norm * 0.6)); |
| 189 |
else { |
| 190 |
errorString_ << "Saxofony::controlChange: undefined control number (" << number << ")!"; |
| 191 |
handleError( StkError::WARNING ); |
| 192 |
} |
| 193 |
|
| 194 |
#if defined(_STK_DEBUG_) |
| 195 |
errorString_ << "Saxofony::controlChange: number = " << number << ", value = " << value << "."; |
| 196 |
handleError( StkError::DEBUG_WARNING ); |
| 197 |
#endif |
| 198 |
} |