Debian Codegraph @

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

raw
1 /***************************************************/
2 /*! \class BandedWG
3 \brief Banded waveguide modeling class.
4
5 This class uses banded waveguide techniques to
6 model a variety of sounds, including bowed
7 bars, glasses, and bowls. For more
8 information, see Essl, G. and Cook, P. "Banded
9 Waveguides: Towards Physical Modelling of Bar
10 Percussion Instruments", Proceedings of the
11 1999 International Computer Music Conference.
12
13 Control Change Numbers:
14 - Bow Pressure = 2
15 - Bow Motion = 4
16 - Strike Position = 8 (not implemented)
17 - Vibrato Frequency = 11
18 - Gain = 1
19 - Bow Velocity = 128
20 - Set Striking = 64
21 - Instrument Presets = 16
22 - Uniform Bar = 0
23 - Tuned Bar = 1
24 - Glass Harmonica = 2
25 - Tibetan Bowl = 3
26
27 by Georg Essl, 1999 - 2004.
28 Modified for Stk 4.0 by Gary Scavone.
29 */
30 /***************************************************/
31
32 #include "BandedWG.h"
33 #include "SKINI.msg"
34 #include <cmath>
35
36 using namespace Nyq;
37
38 BandedWG :: BandedWG()
39 {
40 doPluck_ = true;
41
42 bowTable_.setSlope( 3.0 );
43 adsr_.setAllTimes( 0.02, 0.005, 0.9, 0.01);
44
45 frequency_ = 220.0;
46 this->setPreset(0);
47
48 bowPosition_ = 0;
49 baseGain_ = (StkFloat) 0.999;
50
51 integrationConstant_ = 0.0;
52 trackVelocity_ = false;
53
54 bowVelocity_ = 0.0;
55 bowTarget_ = 0.0;
56
57 strikeAmp_ = 0.0;
58 }
59
60 BandedWG :: ~BandedWG()
61 {
62 }
63
64 void BandedWG :: clear()
65 {
66 for (int i=0; i<nModes_; i++) {
67 delay_[i].clear();
68 bandpass_[i].clear();
69 }
70 }
71
72 void BandedWG :: setPreset(int preset)
73 {
74 int i;
75 switch (preset){
76
77 case 1: // Tuned Bar
78 presetModes_ = 4;
79 modes_[0] = (StkFloat) 1.0;
80 modes_[1] = (StkFloat) 4.0198391420;
81 modes_[2] = (StkFloat) 10.7184986595;
82 modes_[3] = (StkFloat) 18.0697050938;
83
84 for (i=0; i<presetModes_; i++) {
85 basegains_[i] = (StkFloat) pow(0.999,(double) i+1);
86 excitation_[i] = 1.0;
87 }
88
89 break;
90
91 case 2: // Glass Harmonica
92 presetModes_ = 5;
93 modes_[0] = (StkFloat) 1.0;
94 modes_[1] = (StkFloat) 2.32;
95 modes_[2] = (StkFloat) 4.25;
96 modes_[3] = (StkFloat) 6.63;
97 modes_[4] = (StkFloat) 9.38;
98 // modes_[5] = (StkFloat) 12.22;
99
100 for (i=0; i<presetModes_; i++) {
101 basegains_[i] = (StkFloat) pow(0.999,(double) i+1);
102 excitation_[i] = 1.0;
103 }
104 /*
105 baseGain_ = (StkFloat) 0.99999;
106 for (i=0; i<presetModes_; i++)
107 gains_[i]= (StkFloat) pow(baseGain_, delay_[i].getDelay()+i);
108 */
109
110 break;
111
112 case 3: // Tibetan Prayer Bowl (ICMC'02)
113 presetModes_ = 12;
114 modes_[0]=0.996108344;
115 basegains_[0]=0.999925960128219;
116 excitation_[0]=11.900357/10.0;
117 modes_[1]=1.0038916562;
118 basegains_[1]=0.999925960128219;
119 excitation_[1]=11.900357/10.;
120 modes_[2]=2.979178;
121 basegains_[2]=0.999982774366897;
122 excitation_[2]=10.914886/10.;
123 modes_[3]=2.99329767;
124 basegains_[3]=0.999982774366897;
125 excitation_[3]=10.914886/10.;
126 modes_[4]=5.704452;
127 basegains_[4]=1.0; //0.999999999999999999987356406352;
128 excitation_[4]=42.995041/10.;
129 modes_[5]=5.704452;
130 basegains_[5]=1.0; //0.999999999999999999987356406352;
131 excitation_[5]=42.995041/10.;
132 modes_[6]=8.9982;
133 basegains_[6]=1.0; //0.999999999999999999996995497558225;
134 excitation_[6]=40.063034/10.;
135 modes_[7]=9.01549726;
136 basegains_[7]=1.0; //0.999999999999999999996995497558225;
137 excitation_[7]=40.063034/10.;
138 modes_[8]=12.83303;
139 basegains_[8]=0.999965497558225;
140 excitation_[8]=7.063034/10.;
141 modes_[9]=12.807382;
142 basegains_[9]=0.999965497558225;
143 excitation_[9]=7.063034/10.;
144 modes_[10]=17.2808219;
145 basegains_[10]=0.9999999999999999999965497558225;
146 excitation_[10]=57.063034/10.;
147 modes_[11]=21.97602739726;
148 basegains_[11]=0.999999999999999965497558225;
149 excitation_[11]=57.063034/10.;
150
151 break;
152
153 default: // Uniform Bar
154 presetModes_ = 4;
155 modes_[0] = (StkFloat) 1.0;
156 modes_[1] = (StkFloat) 2.756;
157 modes_[2] = (StkFloat) 5.404;
158 modes_[3] = (StkFloat) 8.933;
159
160 for (i=0; i<presetModes_; i++) {
161 basegains_[i] = (StkFloat) pow(0.9,(double) i+1);
162 excitation_[i] = 1.0;
163 }
164
165 break;
166 }
167
168 nModes_ = presetModes_;
169 setFrequency( frequency_ );
170 }
171
172 void BandedWG :: setFrequency(StkFloat frequency)
173 {
174 frequency_ = frequency;
175 if ( frequency <= 0.0 ) {
176 errorString_ << "BandedWG::setFrequency: parameter is less than or equal to zero!";
177 handleError( StkError::WARNING );
178 frequency_ = 220.0;
179 }
180 if (frequency_ > 1568.0) frequency_ = 1568.0;
181
182 StkFloat radius;
183 StkFloat base = Stk::sampleRate() / frequency_;
184 StkFloat length;
185 for (int i=0; i<presetModes_; i++) {
186 // Calculate the delay line lengths for each mode.
187 length = (int)(base / modes_[i]);
188 if ( length > 2.0) {
189 delay_[i].setDelay( length );
190 gains_[i]=basegains_[i];
191 // gains_[i]=(StkFloat) pow(basegains_[i], 1/((StkFloat)delay_[i].getDelay()));
192 // std::cerr << gains_[i];
193 }
194 else {
195 nModes_ = i;
196 break;
197 }
198 // std::cerr << std::endl;
199
200 // Set the bandpass filter resonances
201 radius = 1.0 - PI * 32 / Stk::sampleRate(); //frequency_ * modes_[i] / Stk::sampleRate()/32;
202 if ( radius < 0.0 ) radius = 0.0;
203 bandpass_[i].setResonance(frequency_ * modes_[i], radius, true);
204
205 delay_[i].clear();
206 bandpass_[i].clear();
207 }
208
209 //int olen = (int)(delay_[0].getDelay());
210 //strikePosition_ = (int)(strikePosition_*(length/modes_[0])/olen);
211 }
212
213 void BandedWG :: setStrikePosition(StkFloat position)
214 {
215 strikePosition_ = (int)(delay_[0].getDelay() * position / 2.0);
216 }
217
218 void BandedWG :: startBowing(StkFloat amplitude, StkFloat rate)
219 {
220 adsr_.setRate(rate);
221 adsr_.keyOn();
222 maxVelocity_ = 0.03 + (0.1 * amplitude);
223 }
224
225 void BandedWG :: stopBowing(StkFloat rate)
226 {
227 adsr_.setRate(rate);
228 adsr_.keyOff();
229 }
230
231 void BandedWG :: pluck(StkFloat amplitude)
232 {
233 int j;
234 StkFloat min_len = delay_[nModes_-1].getDelay();
235 for (int i=0; i<nModes_; i++)
236 for(j=0; j<(int)(delay_[i].getDelay()/min_len); j++)
237 delay_[i].tick( excitation_[i]*amplitude / nModes_ );
238
239 // strikeAmp_ += amplitude;
240 }
241
242 void BandedWG :: noteOn(StkFloat frequency, StkFloat amplitude)
243 {
244 this->setFrequency(frequency);
245
246 if ( doPluck_ )
247 this->pluck(amplitude);
248 else
249 this->startBowing(amplitude, amplitude * 0.001);
250
251 #if defined(_STK_DEBUG_)
252 errorString_ << "BandedWG::NoteOn: frequency = " << frequency << ", amplitude = " << amplitude << ".";
253 handleError( StkError::DEBUG_WARNING );
254 #endif
255 }
256
257 void BandedWG :: noteOff(StkFloat amplitude)
258 {
259 if ( !doPluck_ )
260 this->stopBowing((1.0 - amplitude) * 0.005);
261
262 #if defined(_STK_DEBUG_)
263 errorString_ << "BandedWG::NoteOff: amplitude = " << amplitude << ".";
264 handleError( StkError::DEBUG_WARNING );
265 #endif
266 }
267
268 StkFloat BandedWG :: computeSample()
269 {
270 int k;
271
272 StkFloat input = 0.0;
273 if ( doPluck_ ) {
274 input = 0.0;
275 // input = strikeAmp_/nModes_;
276 // strikeAmp_ = 0.0;
277 }
278 else {
279 if (integrationConstant_ == 0.0)
280 velocityInput_ = 0.0;
281 else
282 velocityInput_ = integrationConstant_ * velocityInput_;
283
284 for (k=0; k<nModes_; k++)
285 velocityInput_ += baseGain_ * delay_[k].lastOut();
286
287 if ( trackVelocity_ ) {
288 bowVelocity_ *= 0.9995;
289 bowVelocity_ += bowTarget_;
290 bowTarget_ *= 0.995;
291 }
292 else
293 bowVelocity_ = adsr_.tick() * maxVelocity_;
294
295 input = bowVelocity_ - velocityInput_;
296 input = input * bowTable_.tick(input);
297 input = input/(StkFloat)nModes_;
298 }
299
300 StkFloat data = 0.0;
301 for (k=0; k<nModes_; k++) {
302 bandpass_[k].tick(input + gains_[k] * delay_[k].lastOut());
303 delay_[k].tick(bandpass_[k].lastOut());
304 data += bandpass_[k].lastOut();
305 }
306
307 //lastOutput = data * nModes_;
308 lastOutput_ = data * 4;
309 return lastOutput_;
310 }
311
312 void BandedWG :: controlChange(int number, StkFloat value)
313 {
314 StkFloat norm = value * ONE_OVER_128;
315 if ( norm < 0 ) {
316 norm = 0.0;
317 errorString_ << "BandedWG::controlChange: control value less than zero ... setting to zero!";
318 handleError( StkError::WARNING );
319 }
320 else if ( norm > 1.0 ) {
321 norm = 1.0;
322 errorString_ << "BandedWG::controlChange: control value greater than 128.0 ... setting to 128.0!";
323 handleError( StkError::WARNING );
324 }
325
326 if (number == __SK_BowPressure_) { // 2
327 if ( norm == 0.0 )
328 doPluck_ = true;
329 else {
330 doPluck_ = false;
331 bowTable_.setSlope( 10.0 - (9.0 * norm));
332 }
333 }
334 else if (number == 4) { // 4
335 if ( !trackVelocity_ ) trackVelocity_ = true;
336 bowTarget_ += 0.005 * (norm - bowPosition_);
337 bowPosition_ = norm;
338 //adsr_.setTarget(bowPosition_);
339 }
340 else if (number == 8) // 8
341 this->setStrikePosition( norm );
342 else if (number == __SK_AfterTouch_Cont_) { // 128
343 //bowTarget_ += 0.02 * (norm - bowPosition_);
344 //bowPosition_ = norm;
345 if ( trackVelocity_ ) trackVelocity_ = false;
346 maxVelocity_ = 0.13 * norm;
347 adsr_.setTarget(norm);
348 }
349 else if (number == __SK_ModWheel_) { // 1
350 // baseGain_ = 0.9989999999 + (0.001 * norm );
351 baseGain_ = 0.8999999999999999 + (0.1 * norm);
352 // std::cerr << "Yuck!" << std::endl;
353 for (int i=0; i<nModes_; i++)
354 gains_[i]=(StkFloat) basegains_[i]*baseGain_;
355 // gains_[i]=(StkFloat) pow(baseGain_, (int)((StkFloat)delay_[i].getDelay()+i));
356 }
357 else if (number == __SK_ModFrequency_) // 11
358 integrationConstant_ = norm;
359 else if (number == __SK_Sustain_) { // 64
360 if (value < 65) doPluck_ = true;
361 else doPluck_ = false;
362 }
363 else if (number == __SK_Portamento_) { // 65
364 if (value < 65) trackVelocity_ = false;
365 else trackVelocity_ = true;
366 }
367 else if (number == __SK_ProphesyRibbon_) // 16
368 this->setPreset((int) value);
369 else {
370 errorString_ << "BandedWG::controlChange: undefined control number (" << number << ")!";
371 handleError( StkError::WARNING );
372 }
373
374 #if defined(_STK_DEBUG_)
375 errorString_ << "BandedWG::controlChange: number = " << number << ", value = " << value << ".";
376 handleError( StkError::DEBUG_WARNING );
377 #endif
378 }
379
380