Debian Codegraph @

lib-src/libnyquist/nyquist/nyqstk/src/Sitar.cpp

raw
1 /***************************************************/
2 /*! \class Sitar
3 \brief STK sitar string model class.
4
5 This class implements a sitar plucked string
6 physical model based on the Karplus-Strong
7 algorithm.
8
9 This is a digital waveguide model, making its
10 use possibly subject to patents held by
11 Stanford University, Yamaha, and others.
12 There exist at least two patents, assigned to
13 Stanford, bearing the names of Karplus and/or
14 Strong.
15
16 by Perry R. Cook and Gary P. Scavone, 1995 - 2005.
17 */
18 /***************************************************/
19
20 #include "Sitar.h"
21 #include <math.h>
22
23 using namespace Nyq;
24
25 Sitar :: Sitar(StkFloat lowestFrequency)
26 {
27 unsigned long length = (unsigned long) (Stk::sampleRate() / lowestFrequency + 1);
28 delayLine_.setMaximumDelay( length );
29 delay_ = 0.5 * length;
30 delayLine_.setDelay( delay_ );
31 targetDelay_ = delay_;
32
33 loopFilter_.setZero(0.01);
34 loopGain_ = 0.999;
35
36 envelope_.setAllTimes(0.001, 0.04, 0.0, 0.5);
37 this->clear();
38 }
39
40 Sitar :: ~Sitar()
41 {
42 }
43
44 void Sitar :: clear()
45 {
46 delayLine_.clear();
47 loopFilter_.clear();
48 }
49
50 void Sitar :: setFrequency(StkFloat frequency)
51 {
52 StkFloat freakency = frequency;
53 if ( frequency <= 0.0 ) {
54 errorString_ << "Sitar::setFrequency: parameter is less than or equal to zero!";
55 handleError( StkError::WARNING );
56 freakency = 220.0;
57 }
58
59 targetDelay_ = (Stk::sampleRate() / freakency);
60 delay_ = targetDelay_ * (1.0 + (0.05 * noise_.tick()));
61 delayLine_.setDelay( delay_ );
62 loopGain_ = 0.995 + (freakency * 0.0000005);
63 if ( loopGain_ > 0.9995 ) loopGain_ = 0.9995;
64 }
65
66 void Sitar :: pluck(StkFloat amplitude)
67 {
68 envelope_.keyOn();
69 }
70
71 void Sitar :: noteOn(StkFloat frequency, StkFloat amplitude)
72 {
73 this->setFrequency( frequency );
74 this->pluck( amplitude );
75 amGain_ = 0.1 * amplitude;
76
77 #if defined(_STK_DEBUG_)
78 errorString_ << "Sitar::NoteOn: frequency = " << frequency << ", amplitude = " << amplitude << ".";
79 handleError( StkError::DEBUG_WARNING );
80 #endif
81 }
82
83 void Sitar :: noteOff(StkFloat amplitude)
84 {
85 loopGain_ = (StkFloat) 1.0 - amplitude;
86 if ( loopGain_ < 0.0 ) {
87 errorString_ << "Sitar::noteOff: amplitude is greater than 1.0 ... setting to 1.0!";
88 handleError( StkError::WARNING );
89 loopGain_ = 0.0;
90 }
91 else if ( loopGain_ > 1.0 ) {
92 errorString_ << "Sitar::noteOff: amplitude is < 0.0 ... setting to 0.0!";
93 handleError( StkError::WARNING );
94 loopGain_ = 0.99999;
95 }
96
97 #if defined(_STK_DEBUG_)
98 errorString_ << "Sitar::NoteOff: amplitude = " << amplitude << ".";
99 handleError( StkError::DEBUG_WARNING );
100 #endif
101 }
102
103 StkFloat Sitar :: computeSample()
104 {
105 if ( fabs(targetDelay_ - delay_) > 0.001 ) {
106 if ( targetDelay_ < delay_ )
107 delay_ *= 0.99999;
108 else
109 delay_ *= 1.00001;
110 delayLine_.setDelay( delay_ );
111 }
112
113 lastOutput_ = delayLine_.tick( loopFilter_.tick( delayLine_.lastOut() * loopGain_ ) +
114 (amGain_ * envelope_.tick() * noise_.tick()));
115
116 return lastOutput_;
117 }