-
Notifications
You must be signed in to change notification settings - Fork 185
/
Modal.cpp
169 lines (142 loc) · 4.08 KB
/
Modal.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/***************************************************/
/*! \class Modal
\brief STK resonance model abstract base class.
This class contains an excitation wavetable,
an envelope, an oscillator, and N resonances
(non-sweeping BiQuad filters), where N is set
during instantiation.
by Perry R. Cook and Gary P. Scavone, 1995--2023.
*/
/***************************************************/
#include "Modal.h"
#include <cstdlib>
namespace stk {
Modal :: Modal( unsigned int modes )
: nModes_(modes)
{
if ( nModes_ == 0 ) {
oStream_ << "Modal: 'modes' argument to constructor is zero!";
handleError( StkError::FUNCTION_ARGUMENT );
}
// We don't make the excitation wave here yet, because we don't know
// what it's going to be.
ratios_.resize( nModes_ );
radii_.resize( nModes_ );
filters_ = (BiQuad **) calloc( nModes_, sizeof(BiQuad *) );
for (unsigned int i=0; i<nModes_; i++ ) {
filters_[i] = new BiQuad;
filters_[i]->setEqualGainZeroes();
}
// Set some default values.
vibrato_.setFrequency( 6.0 );
vibratoGain_ = 0.0;
directGain_ = 0.0;
masterGain_ = 1.0;
baseFrequency_ = 440.0;
this->clear();
stickHardness_ = 0.5;
strikePosition_ = 0.561;
}
Modal :: ~Modal( void )
{
for ( unsigned int i=0; i<nModes_; i++ ) {
delete filters_[i];
}
free( filters_ );
}
void Modal :: clear( void )
{
onepole_.clear();
for ( unsigned int i=0; i<nModes_; i++ )
filters_[i]->clear();
}
void Modal :: setFrequency( StkFloat frequency )
{
#if defined(_STK_DEBUG_)
if ( frequency <= 0.0 ) {
oStream_ << "Modal::setFrequency: argument is less than or equal to zero!";
handleError( StkError::WARNING ); return;
}
#endif
baseFrequency_ = frequency;
for ( unsigned int i=0; i<nModes_; i++ )
this->setRatioAndRadius( i, ratios_[i], radii_[i] );
}
void Modal :: setRatioAndRadius( unsigned int modeIndex, StkFloat ratio, StkFloat radius )
{
if ( modeIndex >= nModes_ ) {
oStream_ << "Modal::setRatioAndRadius: modeIndex parameter is greater than number of modes!";
handleError( StkError::WARNING ); return;
}
StkFloat nyquist = Stk::sampleRate() / 2.0;
StkFloat temp;
if ( ratio * baseFrequency_ < nyquist ) {
ratios_[modeIndex] = ratio;
}
else {
temp = ratio;
while (temp * baseFrequency_ > nyquist) temp *= 0.5;
ratios_[modeIndex] = temp;
#if defined(_STK_DEBUG_)
oStream_ << "Modal::setRatioAndRadius: aliasing would occur here ... correcting.";
handleError( StkError::DEBUG_PRINT );
#endif
}
radii_[modeIndex] = radius;
if (ratio < 0)
temp = -ratio;
else
temp = ratio * baseFrequency_;
filters_[modeIndex]->setResonance(temp, radius);
}
void Modal :: setModeGain( unsigned int modeIndex, StkFloat gain )
{
if ( modeIndex >= nModes_ ) {
oStream_ << "Modal::setModeGain: modeIndex parameter is greater than number of modes!";
handleError( StkError::WARNING ); return;
}
filters_[modeIndex]->setGain( gain );
}
void Modal :: strike( StkFloat amplitude )
{
if ( amplitude < 0.0 || amplitude > 1.0 ) {
oStream_ << "Modal::strike: amplitude is out of range!";
handleError( StkError::WARNING );
}
envelope_.setRate( 1.0 );
envelope_.setTarget( amplitude );
onepole_.setPole( 1.0 - amplitude );
envelope_.tick();
wave_->reset();
StkFloat temp;
for ( unsigned int i=0; i<nModes_; i++ ) {
if (ratios_[i] < 0)
temp = -ratios_[i];
else
temp = ratios_[i] * baseFrequency_;
filters_[i]->setResonance(temp, radii_[i]);
}
}
void Modal :: noteOn( StkFloat frequency, StkFloat amplitude )
{
this->strike( amplitude );
this->setFrequency( frequency );
}
void Modal :: noteOff( StkFloat amplitude )
{
// This calls damp, but inverts the meaning of amplitude (high
// amplitude means fast damping).
this->damp( 1.0 - (amplitude * 0.03) );
}
void Modal :: damp( StkFloat amplitude )
{
StkFloat temp;
for ( unsigned int i=0; i<nModes_; i++ ) {
if ( ratios_[i] < 0 )
temp = -ratios_[i];
else
temp = ratios_[i] * baseFrequency_;
filters_[i]->setResonance( temp, radii_[i]*amplitude );
}
}
} // stk namespace