mirror of
https://github.com/thestk/stk
synced 2026-01-15 14:01:52 +00:00
Version 4.4.3
This commit is contained in:
committed by
Stephen Sinclair
parent
baca57040b
commit
0aec39260a
145
src/Flute.cpp
145
src/Flute.cpp
@@ -1,5 +1,5 @@
|
||||
/***************************************************/
|
||||
/*! \class Flute
|
||||
/*! \Class Flute
|
||||
\brief STK flute physical model class.
|
||||
|
||||
This class implements a simple flute
|
||||
@@ -18,7 +18,7 @@
|
||||
- Vibrato Gain = 1
|
||||
- Breath Pressure = 128
|
||||
|
||||
by Perry R. Cook and Gary P. Scavone, 1995 - 2010.
|
||||
by Perry R. Cook and Gary P. Scavone, 1995-2011.
|
||||
*/
|
||||
/***************************************************/
|
||||
|
||||
@@ -29,23 +29,22 @@ namespace stk {
|
||||
|
||||
Flute :: Flute( StkFloat lowestFrequency )
|
||||
{
|
||||
length_ = (unsigned long) ( Stk::sampleRate() / lowestFrequency + 1 );
|
||||
boreDelay_.setMaximumDelay( length_ );
|
||||
boreDelay_.setDelay( 100.0 );
|
||||
if ( lowestFrequency <= 0.0 ) {
|
||||
oStream_ << "Flute::Flute: argument is less than or equal to zero!";
|
||||
handleError( StkError::FUNCTION_ARGUMENT );
|
||||
}
|
||||
|
||||
jetDelay_.setMaximumDelay( length_ );
|
||||
unsigned long nDelays = (unsigned long) ( Stk::sampleRate() / lowestFrequency );
|
||||
boreDelay_.setMaximumDelay( nDelays + 1 );
|
||||
|
||||
jetDelay_.setMaximumDelay( nDelays + 1 );
|
||||
jetDelay_.setDelay( 49.0 );
|
||||
|
||||
vibrato_.setFrequency( 5.925 );
|
||||
|
||||
this->clear();
|
||||
|
||||
filter_.setPole( 0.7 - ((StkFloat) 0.1 * 22050.0 / Stk::sampleRate() ) );
|
||||
filter_.setGain( -1.0 );
|
||||
|
||||
filter_.setPole( 0.7 - ( 0.1 * 22050.0 / Stk::sampleRate() ) );
|
||||
dcBlock_.setBlockZero();
|
||||
|
||||
adsr_.setAllTimes( 0.005, 0.01, 0.8, 0.010);
|
||||
adsr_.setAllTimes( 0.005, 0.01, 0.8, 0.010 );
|
||||
endReflection_ = 0.5;
|
||||
jetReflection_ = 0.5;
|
||||
noiseGain_ = 0.15; // Breath pressure random component.
|
||||
@@ -53,7 +52,8 @@ Flute :: Flute( StkFloat lowestFrequency )
|
||||
jetRatio_ = 0.32;
|
||||
|
||||
maxPressure_ = 0.0;
|
||||
lastFrequency_ = 220.0;
|
||||
this->clear();
|
||||
this->setFrequency( 220.0 );
|
||||
}
|
||||
|
||||
Flute :: ~Flute( void )
|
||||
@@ -70,27 +70,39 @@ void Flute :: clear( void )
|
||||
|
||||
void Flute :: setFrequency( StkFloat frequency )
|
||||
{
|
||||
lastFrequency_ = frequency;
|
||||
#if defined(_STK_DEBUG_)
|
||||
if ( frequency <= 0.0 ) {
|
||||
errorString_ << "Flute::setFrequency: parameter is less than or equal to zero!";
|
||||
handleError( StkError::WARNING );
|
||||
lastFrequency_ = 220.0;
|
||||
oStream_ << "Flute::setFrequency: argument is less than or equal to zero!";
|
||||
handleError( StkError::WARNING ); return;
|
||||
}
|
||||
#endif
|
||||
|
||||
// We're overblowing here.
|
||||
lastFrequency_ *= 0.66666;
|
||||
lastFrequency_ = frequency * 0.66666;
|
||||
|
||||
// delay = length - approximate filter delay.
|
||||
StkFloat delay = Stk::sampleRate() / lastFrequency_ - (StkFloat) 2.0;
|
||||
if ( delay <= 0.0 ) delay = 0.3;
|
||||
else if ( delay > length_ ) delay = length_;
|
||||
// Account for filter delay and one sample "lastOut" delay
|
||||
// (previously approximated as 2.0 samples). The tuning is still
|
||||
// not perfect but I'm not sure why. Also, we are not accounting
|
||||
// for the dc blocking filter delay.
|
||||
StkFloat delay = Stk::sampleRate() / lastFrequency_ - filter_.phaseDelay( lastFrequency_ ) - 1.0;
|
||||
|
||||
boreDelay_.setDelay(delay);
|
||||
jetDelay_.setDelay(delay * jetRatio_);
|
||||
boreDelay_.setDelay( delay );
|
||||
jetDelay_.setDelay( delay * jetRatio_ );
|
||||
}
|
||||
|
||||
void Flute :: setJetDelay( StkFloat aRatio )
|
||||
{
|
||||
jetRatio_ = aRatio;
|
||||
jetDelay_.setDelay( boreDelay_.getDelay() * aRatio ); // Scaled by ratio.
|
||||
}
|
||||
|
||||
void Flute :: startBlowing( StkFloat amplitude, StkFloat rate )
|
||||
{
|
||||
if ( amplitude <= 0.0 || rate <= 0.0 ) {
|
||||
oStream_ << "Flute::startBlowing: one or more arguments is less than or equal to zero!";
|
||||
handleError( StkError::WARNING ); return;
|
||||
}
|
||||
|
||||
adsr_.setAttackRate( rate );
|
||||
maxPressure_ = amplitude / (StkFloat) 0.8;
|
||||
adsr_.keyOn();
|
||||
@@ -98,6 +110,11 @@ void Flute :: startBlowing( StkFloat amplitude, StkFloat rate )
|
||||
|
||||
void Flute :: stopBlowing( StkFloat rate )
|
||||
{
|
||||
if ( rate <= 0.0 ) {
|
||||
oStream_ << "Flute::stopBlowing: argument is less than or equal to zero!";
|
||||
handleError( StkError::WARNING ); return;
|
||||
}
|
||||
|
||||
adsr_.setReleaseRate( rate );
|
||||
adsr_.keyOff();
|
||||
}
|
||||
@@ -107,73 +124,39 @@ void Flute :: noteOn( StkFloat frequency, StkFloat amplitude )
|
||||
this->setFrequency( frequency );
|
||||
this->startBlowing( 1.1 + (amplitude * 0.20), amplitude * 0.02 );
|
||||
outputGain_ = amplitude + 0.001;
|
||||
|
||||
#if defined(_STK_DEBUG_)
|
||||
errorString_ << "Flute::NoteOn: frequency = " << frequency << ", amplitude = " << amplitude << ".";
|
||||
handleError( StkError::DEBUG_WARNING );
|
||||
#endif
|
||||
}
|
||||
|
||||
void Flute :: noteOff( StkFloat amplitude )
|
||||
{
|
||||
this->stopBlowing( amplitude * 0.02 );
|
||||
|
||||
#if defined(_STK_DEBUG_)
|
||||
errorString_ << "Flute::NoteOff: amplitude = " << amplitude << ".";
|
||||
handleError( StkError::DEBUG_WARNING );
|
||||
#endif
|
||||
}
|
||||
|
||||
void Flute :: setJetReflection( StkFloat coefficient )
|
||||
{
|
||||
jetReflection_ = coefficient;
|
||||
}
|
||||
|
||||
void Flute :: setEndReflection( StkFloat coefficient )
|
||||
{
|
||||
endReflection_ = coefficient;
|
||||
}
|
||||
|
||||
void Flute :: setJetDelay( StkFloat aRatio )
|
||||
{
|
||||
// Delay = length - approximate filter delay.
|
||||
StkFloat temp = Stk::sampleRate() / lastFrequency_ - (StkFloat) 2.0;
|
||||
jetRatio_ = aRatio;
|
||||
jetDelay_.setDelay(temp * aRatio); // Scaled by ratio.
|
||||
}
|
||||
|
||||
void Flute :: controlChange( int number, StkFloat value )
|
||||
{
|
||||
StkFloat norm = value * ONE_OVER_128;
|
||||
if ( norm < 0 ) {
|
||||
norm = 0.0;
|
||||
errorString_ << "Flute::controlChange: control value less than zero ... setting to zero!";
|
||||
handleError( StkError::WARNING );
|
||||
}
|
||||
else if ( norm > 1.0 ) {
|
||||
norm = 1.0;
|
||||
errorString_ << "Flute::controlChange: control value greater than 128.0 ... setting to 128.0!";
|
||||
handleError( StkError::WARNING );
|
||||
}
|
||||
|
||||
if (number == __SK_JetDelay_) // 2
|
||||
this->setJetDelay( (StkFloat) (0.08 + (0.48 * norm)) );
|
||||
else if (number == __SK_NoiseLevel_) // 4
|
||||
noiseGain_ = ( norm * 0.4);
|
||||
else if (number == __SK_ModFrequency_) // 11
|
||||
vibrato_.setFrequency( norm * 12.0);
|
||||
else if (number == __SK_ModWheel_) // 1
|
||||
vibratoGain_ = ( norm * 0.4 );
|
||||
else if (number == __SK_AfterTouch_Cont_) // 128
|
||||
adsr_.setTarget( norm );
|
||||
else {
|
||||
errorString_ << "Flute::controlChange: undefined control number (" << number << ")!";
|
||||
handleError( StkError::WARNING );
|
||||
}
|
||||
|
||||
#if defined(_STK_DEBUG_)
|
||||
errorString_ << "Flute::controlChange: number = " << number << ", value = " << value << ".";
|
||||
handleError( StkError::DEBUG_WARNING );
|
||||
if ( Stk::inRange( value, 0.0, 128.0 ) == false ) {
|
||||
oStream_ << "Flute::controlChange: value (" << value << ") is out of range!";
|
||||
handleError( StkError::WARNING ); return;
|
||||
}
|
||||
#endif
|
||||
|
||||
StkFloat normalizedValue = value * ONE_OVER_128;
|
||||
if (number == __SK_JetDelay_) // 2
|
||||
this->setJetDelay( (StkFloat) (0.08 + (0.48 * normalizedValue)) );
|
||||
else if (number == __SK_NoiseLevel_) // 4
|
||||
noiseGain_ = ( normalizedValue * 0.4);
|
||||
else if (number == __SK_ModFrequency_) // 11
|
||||
vibrato_.setFrequency( normalizedValue * 12.0);
|
||||
else if (number == __SK_ModWheel_) // 1
|
||||
vibratoGain_ = ( normalizedValue * 0.4 );
|
||||
else if (number == __SK_AfterTouch_Cont_) // 128
|
||||
adsr_.setTarget( normalizedValue );
|
||||
#if defined(_STK_DEBUG_)
|
||||
else {
|
||||
oStream_ << "Flute::controlChange: undefined control number (" << number << ")!";
|
||||
handleError( StkError::WARNING );
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user