Debian Codegraph @

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

raw
1 /***************************************************/
2 /*! \class Mandolin
3 \brief STK mandolin instrument model class.
4
5 This class inherits from PluckTwo and uses
6 "commuted synthesis" techniques to model a
7 mandolin instrument.
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 Commuted Synthesis, in particular, is covered
13 by patents, granted, pending, and/or
14 applied-for. All are assigned to the Board of
15 Trustees, Stanford University. For
16 information, contact the Office of Technology
17 Licensing, Stanford University.
18
19 Control Change Numbers:
20 - Body Size = 2
21 - Pluck Position = 4
22 - String Sustain = 11
23 - String Detuning = 1
24 - Microphone Position = 128
25
26 by Perry R. Cook and Gary P. Scavone, 1995 - 2005.
27 */
28 /***************************************************/
29
30 #include "Mandolin.h"
31 #include "SKINI.msg"
32
33 using namespace Nyq;
34
35 Mandolin :: Mandolin(StkFloat lowestFrequency)
36 : PluckTwo(lowestFrequency)
37 {
38 // Concatenate the STK rawwave path to the rawwave files
39 soundfile_[0] = new FileWvIn( (Stk::rawwavePath() + "mand1.raw").c_str(), true );
40 soundfile_[1] = new FileWvIn( (Stk::rawwavePath() + "mand2.raw").c_str(), true );
41 soundfile_[2] = new FileWvIn( (Stk::rawwavePath() + "mand3.raw").c_str(), true );
42 soundfile_[3] = new FileWvIn( (Stk::rawwavePath() + "mand4.raw").c_str(), true );
43 soundfile_[4] = new FileWvIn( (Stk::rawwavePath() + "mand5.raw").c_str(), true );
44 soundfile_[5] = new FileWvIn( (Stk::rawwavePath() + "mand6.raw").c_str(), true );
45 soundfile_[6] = new FileWvIn( (Stk::rawwavePath() + "mand7.raw").c_str(), true );
46 soundfile_[7] = new FileWvIn( (Stk::rawwavePath() + "mand8.raw").c_str(), true );
47 soundfile_[8] = new FileWvIn( (Stk::rawwavePath() + "mand9.raw").c_str(), true );
48 soundfile_[9] = new FileWvIn( (Stk::rawwavePath() + "mand10.raw").c_str(), true );
49 soundfile_[10] = new FileWvIn( (Stk::rawwavePath() + "mand11.raw").c_str(), true );
50 soundfile_[11] = new FileWvIn( (Stk::rawwavePath() + "mand12.raw").c_str(), true );
51
52 mic_ = 0;
53 dampTime_ = 0;
54 waveDone_ = soundfile_[mic_]->isFinished();
55 }
56
57 Mandolin :: ~Mandolin()
58 {
59 for ( int i=0; i<12; i++ )
60 delete soundfile_[i];
61 }
62
63 void Mandolin :: pluck(StkFloat amplitude)
64 {
65 // This function gets interesting, because pluck
66 // may be longer than string length, so we just
67 // reset the soundfile and add in the pluck in
68 // the tick method.
69 soundfile_[mic_]->reset();
70 waveDone_ = false;
71 pluckAmplitude_ = amplitude;
72 if ( amplitude < 0.0 ) {
73 errorString_ << "Mandolin::pluck: amplitude parameter less than zero ... setting to 0.0!";
74 handleError( StkError::WARNING );
75 pluckAmplitude_ = 0.0;
76 }
77 else if ( amplitude > 1.0 ) {
78 errorString_ << "Mandolin::pluck: amplitude parameter greater than one ... setting to 1.0!";
79 handleError( StkError::WARNING );
80 pluckAmplitude_ = 1.0;
81 }
82
83 // Set the pick position, which puts zeroes at position * length.
84 combDelay_.setDelay( 0.5 * pluckPosition_ * lastLength_ );
85 dampTime_ = (long) lastLength_; // See tick method below.
86 }
87
88 void Mandolin :: pluck(StkFloat amplitude, StkFloat position)
89 {
90 // Pluck position puts zeroes at position * length.
91 pluckPosition_ = position;
92 if ( position < 0.0 ) {
93 std::cerr << "Mandolin::pluck: position parameter less than zero ... setting to 0.0!";
94 handleError( StkError::WARNING );
95 pluckPosition_ = 0.0;
96 }
97 else if ( position > 1.0 ) {
98 errorString_ << "Mandolin::pluck: amplitude parameter greater than one ... setting to 1.0!";
99 handleError( StkError::WARNING );
100 pluckPosition_ = 1.0;
101 }
102
103 this->pluck( amplitude );
104 }
105
106 void Mandolin :: noteOn(StkFloat frequency, StkFloat amplitude)
107 {
108 this->setFrequency( frequency );
109 this->pluck( amplitude );
110
111 #if defined(_STK_DEBUG_)
112 errorString_ << "Mandolin::NoteOn: frequency = " << frequency << ", amplitude = " << amplitude << ".";
113 handleError( StkError::DEBUG_WARNING );
114 #endif
115 }
116
117 void Mandolin :: setBodySize(StkFloat size)
118 {
119 // Scale the commuted body response by its sample rate (22050).
120 StkFloat rate = size * 22050.0 / Stk::sampleRate();
121 for ( int i=0; i<12; i++ )
122 soundfile_[i]->setRate( rate );
123 }
124
125 StkFloat Mandolin :: computeSample()
126 {
127 StkFloat temp = 0.0;
128 if ( !waveDone_ ) {
129 // Scale the pluck excitation with comb
130 // filtering for the duration of the file.
131 temp = soundfile_[mic_]->tick() * pluckAmplitude_;
132 temp = temp - combDelay_.tick(temp);
133 waveDone_ = soundfile_[mic_]->isFinished();
134 }
135
136 // Damping hack to help avoid overflow on re-plucking.
137 if ( dampTime_ >=0 ) {
138 dampTime_ -= 1;
139 // Calculate 1st delay filtered reflection plus pluck excitation.
140 lastOutput_ = delayLine_.tick( filter_.tick( temp + (delayLine_.lastOut() * 0.7) ) );
141 // Calculate 2nd delay just like the 1st.
142 lastOutput_ += delayLine2_.tick( filter2_.tick( temp + (delayLine2_.lastOut() * 0.7) ) );
143 }
144 else { // No damping hack after 1 period.
145 // Calculate 1st delay filtered reflection plus pluck excitation.
146 lastOutput_ = delayLine_.tick( filter_.tick( temp + (delayLine_.lastOut() * loopGain_) ) );
147 // Calculate 2nd delay just like the 1st.
148 lastOutput_ += delayLine2_.tick( filter2_.tick( temp + (delayLine2_.lastOut() * loopGain_) ) );
149 }
150
151 lastOutput_ *= 0.3;
152 return lastOutput_;
153 }
154
155 void Mandolin :: controlChange(int number, StkFloat value)
156 {
157 StkFloat norm = value * ONE_OVER_128;
158 if ( norm < 0 ) {
159 norm = 0.0;
160 errorString_ << "Mandolin::controlChange: control value less than zero ... setting to zero!";
161 handleError( StkError::WARNING );
162 }
163 else if ( norm > 1.0 ) {
164 norm = 1.0;
165 errorString_ << "Mandolin::controlChange: control value greater than 128.0 ... setting to 128.0!";
166 handleError( StkError::WARNING );
167 }
168
169 if (number == __SK_BodySize_) // 2
170 this->setBodySize( norm * 2.0 );
171 else if (number == __SK_PickPosition_) // 4
172 this->setPluckPosition( norm );
173 else if (number == __SK_StringDamping_) // 11
174 this->setBaseLoopGain( 0.97 + (norm * 0.03));
175 else if (number == __SK_StringDetune_) // 1
176 this->setDetune( 1.0 - (norm * 0.1) );
177 else if (number == __SK_AfterTouch_Cont_) // 128
178 mic_ = (int) (norm * 11.0);
179 else {
180 errorString_ << "Mandolin::controlChange: undefined control number (" << number << ")!";
181 handleError( StkError::WARNING );
182 }
183
184 #if defined(_STK_DEBUG_)
185 errorString_ << "Mandolin::controlChange: number = " << number << ", value = " << value << ".";
186 handleError( StkError::DEBUG_WARNING );
187 #endif
188 }