Debian Codegraph @

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

raw
1 /***************************************************/
2 /*! \class Modal
3 \brief STK resonance model instrument.
4
5 This class contains an excitation wavetable,
6 an envelope, an oscillator, and N resonances
7 (non-sweeping BiQuad filters), where N is set
8 during instantiation.
9
10 by Perry R. Cook and Gary P. Scavone, 1995 - 2005.
11 */
12 /***************************************************/
13
14 #include "Modal.h"
15 #include <stdlib.h>
16
17 using namespace Nyq;
18
19 Modal :: Modal(unsigned int modes)
20 : nModes_(modes)
21 {
22 if ( nModes_ == 0 ) {
23 errorString_ << "Modal: 'modes' argument to constructor is zero!";
24 handleError( StkError::FUNCTION_ARGUMENT );
25 }
26
27 // We don't make the excitation wave here yet, because we don't know
28 // what it's going to be.
29
30 ratios_.resize( nModes_ );
31 radii_.resize( nModes_ );
32 filters_ = (BiQuad **) calloc( nModes_, sizeof(BiQuad *) );
33 for (unsigned int i=0; i<nModes_; i++ ) {
34 filters_[i] = new BiQuad;
35 filters_[i]->setEqualGainZeroes();
36 }
37
38 // Set some default values.
39 vibrato_.setFrequency( 6.0 );
40 vibratoGain_ = 0.0;
41 directGain_ = 0.0;
42 masterGain_ = 1.0;
43 baseFrequency_ = 440.0;
44
45 this->clear();
46
47 stickHardness_ = 0.5;
48 strikePosition_ = 0.561;
49 }
50
51 Modal :: ~Modal()
52 {
53 for (unsigned int i=0; i<nModes_; i++ ) {
54 delete filters_[i];
55 }
56 free(filters_);
57 }
58
59 void Modal :: clear()
60 {
61 onepole_.clear();
62 for (unsigned int i=0; i<nModes_; i++ )
63 filters_[i]->clear();
64 }
65
66 void Modal :: setFrequency(StkFloat frequency)
67 {
68 baseFrequency_ = frequency;
69 for (unsigned int i=0; i<nModes_; i++ )
70 this->setRatioAndRadius( i, ratios_[i], radii_[i] );
71 }
72
73 void Modal :: setRatioAndRadius(unsigned int modeIndex, StkFloat ratio, StkFloat radius)
74 {
75 if ( modeIndex >= nModes_ ) {
76 errorString_ << "Modal::setRatioAndRadius: modeIndex parameter is greater than number of modes!";
77 handleError( StkError::WARNING );
78 return;
79 }
80
81 StkFloat nyquist = Stk::sampleRate() / 2.0;
82 StkFloat temp;
83
84 if ( ratio * baseFrequency_ < nyquist ) {
85 ratios_[modeIndex] = ratio;
86 }
87 else {
88 temp = ratio;
89 while (temp * baseFrequency_ > nyquist) temp *= 0.5;
90 ratios_[modeIndex] = temp;
91 #if defined(_STK_DEBUG_)
92 errorString_ << "Modal::setRatioAndRadius: aliasing would occur here ... correcting.";
93 handleError( StkError::DEBUG_WARNING );
94 #endif
95 }
96 radii_[modeIndex] = radius;
97 if (ratio < 0)
98 temp = -ratio;
99 else
100 temp = ratio * baseFrequency_;
101
102 filters_[modeIndex]->setResonance(temp, radius);
103 }
104
105 void Modal :: setMasterGain(StkFloat aGain)
106 {
107 masterGain_ = aGain;
108 }
109
110 void Modal :: setDirectGain(StkFloat aGain)
111 {
112 directGain_ = aGain;
113 }
114
115 void Modal :: setModeGain(unsigned int modeIndex, StkFloat gain)
116 {
117 if ( modeIndex >= nModes_ ) {
118 errorString_ << "Modal::setModeGain: modeIndex parameter is greater than number of modes!";
119 handleError( StkError::WARNING );
120 return;
121 }
122
123 filters_[modeIndex]->setGain(gain);
124 }
125
126 void Modal :: strike(StkFloat amplitude)
127 {
128 StkFloat gain = amplitude;
129 if ( amplitude < 0.0 ) {
130 errorString_ << "Modal::strike: amplitude is less than zero ... setting to zero!";
131 handleError( StkError::WARNING );
132 gain = 0.0;
133 }
134 else if ( amplitude > 1.0 ) {
135 errorString_ << "Modal::strike: amplitude is greater than one ... setting to 1.0!";
136 handleError( StkError::WARNING );
137 gain = 1.0;
138 }
139
140 envelope_.setRate( 1.0 );
141 envelope_.setTarget( gain );
142 onepole_.setPole( 1.0 - gain );
143 envelope_.tick();
144 wave_->reset();
145
146 StkFloat temp;
147 for (unsigned int i=0; i<nModes_; i++) {
148 if (ratios_[i] < 0)
149 temp = -ratios_[i];
150 else
151 temp = ratios_[i] * baseFrequency_;
152 filters_[i]->setResonance(temp, radii_[i]);
153 }
154 }
155
156 void Modal :: noteOn(StkFloat frequency, StkFloat amplitude)
157 {
158 this->strike(amplitude);
159 this->setFrequency(frequency);
160
161 #if defined(_STK_DEBUG_)
162 errorString_ << "Modal::NoteOn: frequency = " << frequency << ", amplitude = " << amplitude << '.';
163 handleError( StkError::DEBUG_WARNING );
164 #endif
165 }
166
167 void Modal :: noteOff(StkFloat amplitude)
168 {
169 // This calls damp, but inverts the meaning of amplitude (high
170 // amplitude means fast damping).
171 this->damp( 1.0 - (amplitude * 0.03) );
172
173 #if defined(_STK_DEBUG_)
174 errorString_ << "Modal::NoteOff: amplitude = " << amplitude << '.';
175 handleError( StkError::DEBUG_WARNING );
176 #endif
177 }
178
179 void Modal :: damp(StkFloat amplitude)
180 {
181 StkFloat temp;
182 for (unsigned int i=0; i<nModes_; i++) {
183 if (ratios_[i] < 0)
184 temp = -ratios_[i];
185 else
186 temp = ratios_[i] * baseFrequency_;
187 filters_[i]->setResonance(temp, radii_[i]*amplitude);
188 }
189 }
190
191 StkFloat Modal :: computeSample()
192 {
193 StkFloat temp = masterGain_ * onepole_.tick( wave_->tick() * envelope_.tick() );
194
195 StkFloat temp2 = 0.0;
196 for (unsigned int i=0; i<nModes_; i++)
197 temp2 += filters_[i]->tick(temp);
198
199 temp2 -= temp2 * directGain_;
200 temp2 += directGain_ * temp;
201
202 if (vibratoGain_ != 0.0) {
203 // Calculate AM and apply to master out
204 temp = 1.0 + (vibrato_.tick() * vibratoGain_);
205 temp2 = temp * temp2;
206 }
207
208 lastOutput_ = temp2;
209 return lastOutput_;
210 }