-
Notifications
You must be signed in to change notification settings - Fork 185
/
FM.cpp
179 lines (151 loc) · 4.28 KB
/
FM.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
170
171
172
173
174
175
176
177
178
179
/***************************************************/
/*! \class FM
\brief STK abstract FM synthesis base class.
This class controls an arbitrary number of
waves and envelopes, determined via a
constructor argument.
Control Change Numbers:
- Control One = 2
- Control Two = 4
- LFO Speed = 11
- LFO Depth = 1
- ADSR 2 & 4 Target = 128
The basic Chowning/Stanford FM patent expired
in 1995, but there exist follow-on patents,
mostly assigned to Yamaha. If you are of the
type who should worry about this (making
money) worry away.
by Perry R. Cook and Gary P. Scavone, 1995--2023.
*/
/***************************************************/
#include "FM.h"
#include "SKINImsg.h"
namespace stk {
FM :: FM( unsigned int operators )
: nOperators_(operators)
{
if ( nOperators_ == 0 ) {
oStream_ << "FM::FM: Number of operators must be greater than zero!";
handleError( StkError::FUNCTION_ARGUMENT );
}
twozero_.setB2( -1.0 );
twozero_.setGain( 0.0 );
vibrato_.setFrequency( 6.0 );
unsigned int j;
adsr_.resize( nOperators_ );
waves_.resize( nOperators_ );
for (j=0; j<nOperators_; j++ ) {
ratios_.push_back( 1.0 );
gains_.push_back( 1.0 );
adsr_[j] = new ADSR();
}
modDepth_ = 0.0;
control1_ = 1.0;
control2_ = 1.0;
baseFrequency_ = 440.0;
int i;
StkFloat temp = 1.0;
for (i=99; i>=0; i--) {
fmGains_[i] = temp;
temp *= 0.933033;
}
temp = 1.0;
for (i=15; i>=0; i--) {
fmSusLevels_[i] = temp;
temp *= 0.707101;
}
temp = 8.498186;
for (i=0; i<32; i++) {
fmAttTimes_[i] = temp;
temp *= 0.707101;
}
}
FM :: ~FM( void )
{
for (unsigned int i=0; i<nOperators_; i++ ) {
delete waves_[i];
delete adsr_[i];
}
}
void FM :: loadWaves( const char **filenames )
{
for (unsigned int i=0; i<nOperators_; i++ )
waves_[i] = new FileLoop( filenames[i], true );
}
void FM :: setFrequency( StkFloat frequency )
{
#if defined(_STK_DEBUG_)
if ( frequency <= 0.0 ) {
oStream_ << "FM::setFrequency: argument is less than or equal to zero!";
handleError( StkError::WARNING ); return;
}
#endif
baseFrequency_ = frequency;
for ( unsigned int i=0; i<nOperators_; i++ )
waves_[i]->setFrequency( baseFrequency_ * ratios_[i] );
}
void FM :: setRatio( unsigned int waveIndex, StkFloat ratio )
{
if ( waveIndex >= nOperators_ ) {
oStream_ << "FM:setRatio: waveIndex parameter is greater than the number of operators!";
handleError( StkError::WARNING ); return;
}
ratios_[waveIndex] = ratio;
if (ratio > 0.0)
waves_[waveIndex]->setFrequency( baseFrequency_ * ratio );
else
waves_[waveIndex]->setFrequency( ratio );
}
void FM :: setGain( unsigned int waveIndex, StkFloat gain )
{
if ( waveIndex >= nOperators_ ) {
oStream_ << "FM::setGain: waveIndex parameter is greater than the number of operators!";
handleError( StkError::WARNING ); return;
}
gains_[waveIndex] = gain;
}
void FM :: keyOn( void )
{
for ( unsigned int i=0; i<nOperators_; i++ )
adsr_[i]->keyOn();
}
void FM :: keyOff( void )
{
for ( unsigned int i=0; i<nOperators_; i++ )
adsr_[i]->keyOff();
}
void FM :: noteOff( StkFloat amplitude )
{
this->keyOff();
}
void FM :: controlChange( int number, StkFloat value )
{
#if defined(_STK_DEBUG_)
if ( Stk::inRange( value, 0.0, 128.0 ) == false ) {
oStream_ << "FM::controlChange: value (" << value << ") is out of range!";
handleError( StkError::WARNING ); return;
}
#endif
StkFloat normalizedValue = value * ONE_OVER_128;
if (number == __SK_Breath_) // 2
this->setControl1( normalizedValue );
else if (number == __SK_FootControl_) // 4
this->setControl2( normalizedValue );
else if (number == __SK_ModFrequency_) // 11
this->setModulationSpeed( normalizedValue * 12.0);
else if (number == __SK_ModWheel_) // 1
this->setModulationDepth( normalizedValue );
else if (number == __SK_AfterTouch_Cont_) { // 128
//adsr_[0]->setTarget( normalizedValue );
adsr_[1]->setTarget( normalizedValue );
//adsr_[2]->setTarget( normalizedValue );
adsr_[3]->setTarget( normalizedValue );
}
#if defined(_STK_DEBUG_)
else {
oStream_ << "FM::controlChange: undefined control number (" << number << ")!";
handleError( StkError::WARNING );
}
#endif
}
} // stk namespace