| 1 |
/***************************************************/ |
| 2 |
/*! \class PluckTwo |
| 3 |
\brief STK enhanced plucked string model class. |
| 4 |
|
| 5 |
This class implements an enhanced two-string, |
| 6 |
plucked physical model, a la Jaffe-Smith, |
| 7 |
Smith, and others. |
| 8 |
|
| 9 |
PluckTwo is an abstract class, with no excitation |
| 10 |
specified. Therefore, it can't be directly |
| 11 |
instantiated. |
| 12 |
|
| 13 |
This is a digital waveguide model, making its |
| 14 |
use possibly subject to patents held by |
| 15 |
Stanford University, Yamaha, and others. |
| 16 |
|
| 17 |
by Perry R. Cook and Gary P. Scavone, 1995 - 2005. |
| 18 |
*/ |
| 19 |
/***************************************************/ |
| 20 |
|
| 21 |
#include "PluckTwo.h" |
| 22 |
|
| 23 |
using namespace Nyq; |
| 24 |
|
| 25 |
PluckTwo :: PluckTwo(StkFloat lowestFrequency) |
| 26 |
{ |
| 27 |
length_ = (unsigned long) (Stk::sampleRate() / lowestFrequency + 1); |
| 28 |
lastLength_ = length_ * 0.5; |
| 29 |
delayLine_.setMaximumDelay( length_ ); |
| 30 |
delayLine_.setDelay( lastLength_ ); |
| 31 |
delayLine2_.setMaximumDelay( length_ ); |
| 32 |
delayLine2_.setDelay( lastLength_ ); |
| 33 |
combDelay_.setMaximumDelay( length_ ); |
| 34 |
combDelay_.setDelay( lastLength_ ); |
| 35 |
|
| 36 |
baseLoopGain_ = 0.995; |
| 37 |
loopGain_ = 0.999; |
| 38 |
pluckAmplitude_ = 0.3; |
| 39 |
pluckPosition_ = 0.4; |
| 40 |
detuning_ = 0.995; |
| 41 |
lastFrequency_ = lowestFrequency * 2.0; |
| 42 |
|
| 43 |
} |
| 44 |
|
| 45 |
PluckTwo :: ~PluckTwo() |
| 46 |
{ |
| 47 |
} |
| 48 |
|
| 49 |
void PluckTwo :: clear() |
| 50 |
{ |
| 51 |
delayLine_.clear(); |
| 52 |
delayLine2_.clear(); |
| 53 |
combDelay_.clear(); |
| 54 |
filter_.clear(); |
| 55 |
filter2_.clear(); |
| 56 |
} |
| 57 |
|
| 58 |
void PluckTwo :: setFrequency(StkFloat frequency) |
| 59 |
{ |
| 60 |
lastFrequency_ = frequency; |
| 61 |
if ( lastFrequency_ <= 0.0 ) { |
| 62 |
errorString_ << "Clarinet::setFrequency: parameter is less than or equal to zero!"; |
| 63 |
handleError( StkError::WARNING ); |
| 64 |
lastFrequency_ = 220.0; |
| 65 |
} |
| 66 |
|
| 67 |
// Delay = length - approximate filter delay. |
| 68 |
lastLength_ = Stk::sampleRate() / lastFrequency_; |
| 69 |
StkFloat delay = (lastLength_ / detuning_) - 0.5; |
| 70 |
if ( delay <= 0.0 ) delay = 0.3; |
| 71 |
else if ( delay > length_ ) delay = length_; |
| 72 |
delayLine_.setDelay( delay ); |
| 73 |
|
| 74 |
delay = (lastLength_ * detuning_) - 0.5; |
| 75 |
if ( delay <= 0.0 ) delay = 0.3; |
| 76 |
else if ( delay > length_ ) delay = length_; |
| 77 |
delayLine2_.setDelay( delay ); |
| 78 |
|
| 79 |
loopGain_ = baseLoopGain_ + (frequency * 0.000005); |
| 80 |
if ( loopGain_ > 1.0 ) loopGain_ = 0.99999; |
| 81 |
} |
| 82 |
|
| 83 |
void PluckTwo :: setDetune(StkFloat detune) |
| 84 |
{ |
| 85 |
detuning_ = detune; |
| 86 |
if ( detuning_ <= 0.0 ) { |
| 87 |
errorString_ << "Clarinet::setDeturn: parameter is less than or equal to zero!"; |
| 88 |
handleError( StkError::WARNING ); |
| 89 |
detuning_ = 0.1; |
| 90 |
} |
| 91 |
delayLine_.setDelay(( lastLength_ / detuning_) - 0.5); |
| 92 |
delayLine2_.setDelay( (lastLength_ * detuning_) - 0.5); |
| 93 |
} |
| 94 |
|
| 95 |
void PluckTwo :: setFreqAndDetune(StkFloat frequency, StkFloat detune) |
| 96 |
{ |
| 97 |
detuning_ = detune; |
| 98 |
this->setFrequency( frequency ); |
| 99 |
} |
| 100 |
|
| 101 |
void PluckTwo :: setPluckPosition(StkFloat position) |
| 102 |
{ |
| 103 |
pluckPosition_ = position; |
| 104 |
if ( position < 0.0 ) { |
| 105 |
errorString_ << "PluckTwo::setPluckPosition: parameter is less than zero ... setting to 0.0!"; |
| 106 |
handleError( StkError::WARNING ); |
| 107 |
pluckPosition_ = 0.0; |
| 108 |
} |
| 109 |
else if ( position > 1.0 ) { |
| 110 |
errorString_ << "PluckTwo::setPluckPosition: parameter is greater than one ... setting to 1.0!"; |
| 111 |
handleError( StkError::WARNING ); |
| 112 |
pluckPosition_ = 1.0; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
void PluckTwo :: setBaseLoopGain(StkFloat aGain) |
| 117 |
{ |
| 118 |
baseLoopGain_ = aGain; |
| 119 |
loopGain_ = baseLoopGain_ + (lastFrequency_ * 0.000005); |
| 120 |
if ( loopGain_ > 0.99999 ) loopGain_ = 0.99999; |
| 121 |
} |
| 122 |
|
| 123 |
void PluckTwo :: noteOff(StkFloat amplitude) |
| 124 |
{ |
| 125 |
loopGain_ = (1.0 - amplitude) * 0.5; |
| 126 |
|
| 127 |
#if defined(_STK_DEBUG_) |
| 128 |
errorString_ << "PluckTwo::NoteOff: amplitude = " << amplitude << "."; |
| 129 |
handleError( StkError::DEBUG_WARNING ); |
| 130 |
#endif |
| 131 |
} |
| 132 |
|