forked from thestk/stk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVoicForm.cpp
More file actions
189 lines (164 loc) · 5.6 KB
/
VoicForm.cpp
File metadata and controls
189 lines (164 loc) · 5.6 KB
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
180
181
182
183
184
185
186
187
188
189
/***************************************************/
/*! \class VoicForm
\brief Four formant synthesis instrument.
This instrument contains an excitation singing
wavetable (looping wave with random and
periodic vibrato, smoothing on frequency,
etc.), excitation noise, and four sweepable
complex resonances.
Measured formant data is included, and enough
data is there to support either parallel or
cascade synthesis. In the floating point case
cascade synthesis is the most natural so
that's what you'll find here.
Control Change Numbers:
- Voiced/Unvoiced Mix = 2
- Vowel/Phoneme Selection = 4
- Vibrato Frequency = 11
- Vibrato Gain = 1
- Loudness (Spectral Tilt) = 128
by Perry R. Cook and Gary P. Scavone, 1995--2021.
*/
/***************************************************/
#include "VoicForm.h"
#include "Phonemes.h"
#include "SKINImsg.h"
#include <cstring>
#include <cmath>
namespace stk {
VoicForm :: VoicForm( void ) : Instrmnt()
{
// Concatenate the STK rawwave path to the rawwave file
voiced_ = new SingWave( (Stk::rawwavePath() + "impuls20.raw").c_str(), true );
voiced_->setGainRate( 0.001 );
voiced_->setGainTarget( 0.0 );
for ( int i=0; i<4; i++ )
filters_[i].setSweepRate( 0.001 );
onezero_.setZero( -0.9 );
onepole_.setPole( 0.9 );
noiseEnv_.setRate( 0.001 );
noiseEnv_.setTarget( 0.0 );
this->setPhoneme( "eee" );
this->clear();
}
VoicForm :: ~VoicForm( void )
{
delete voiced_;
}
void VoicForm :: clear( void )
{
onezero_.clear();
onepole_.clear();
for ( int i=0; i<4; i++ ) {
filters_[i].clear();
}
}
void VoicForm :: setFrequency( StkFloat frequency )
{
#if defined(_STK_DEBUG_)
if ( frequency <= 0.0 ) {
oStream_ << "VoicForm::setFrequency: parameter is less than or equal to zero!";
handleError( StkError::WARNING ); return;
}
#endif
voiced_->setFrequency( frequency );
}
bool VoicForm :: setPhoneme( const char *phoneme )
{
bool found = false;
unsigned int i = 0;
while( i < 32 && !found ) {
if ( !strcmp( Phonemes::name(i), phoneme ) ) {
found = true;
filters_[0].setTargets( Phonemes::formantFrequency(i, 0), Phonemes::formantRadius(i, 0), pow(10.0, Phonemes::formantGain(i, 0 ) / 20.0) );
filters_[1].setTargets( Phonemes::formantFrequency(i, 1), Phonemes::formantRadius(i, 1), pow(10.0, Phonemes::formantGain(i, 1 ) / 20.0) );
filters_[2].setTargets( Phonemes::formantFrequency(i, 2), Phonemes::formantRadius(i, 2), pow(10.0, Phonemes::formantGain(i, 2 ) / 20.0) );
filters_[3].setTargets( Phonemes::formantFrequency(i, 3), Phonemes::formantRadius(i, 3), pow(10.0, Phonemes::formantGain(i, 3 ) / 20.0) );
this->setVoiced( Phonemes::voiceGain( i ) );
this->setUnVoiced( Phonemes::noiseGain( i ) );
}
i++;
}
if ( !found ) {
oStream_ << "VoicForm::setPhoneme: phoneme " << phoneme << " not found!";
handleError( StkError::WARNING );
}
return found;
}
void VoicForm :: setFilterSweepRate( unsigned int whichOne, StkFloat rate )
{
if ( whichOne > 3 ) {
oStream_ << "VoicForm::setFilterSweepRate: filter select argument outside range 0-3!";
handleError( StkError::WARNING ); return;
}
filters_[whichOne].setSweepRate(rate);
}
void VoicForm :: quiet( void )
{
voiced_->noteOff();
noiseEnv_.setTarget( 0.0 );
}
void VoicForm :: noteOn( StkFloat frequency, StkFloat amplitude )
{
this->setFrequency( frequency );
voiced_->setGainTarget( amplitude );
onepole_.setPole( 0.97 - (amplitude * 0.2) );
}
void VoicForm :: controlChange( int number, StkFloat value )
{
#if defined(_STK_DEBUG_)
if ( Stk::inRange( value, 0.0, 128.0 ) == false ) {
oStream_ << "VoicForm::controlChange: value (" << value << ") is out of range!";
handleError( StkError::WARNING ); return;
}
#endif
StkFloat normalizedValue = value * ONE_OVER_128;
if (number == __SK_Breath_) { // 2
this->setVoiced( 1.0 - normalizedValue );
this->setUnVoiced( 0.01 * normalizedValue );
}
else if (number == __SK_FootControl_) { // 4
StkFloat temp = 0.0;
unsigned int i = (int) value;
if (i < 32) {
temp = 0.9;
}
else if (i < 64) {
i -= 32;
temp = 1.0;
}
else if (i < 96) {
i -= 64;
temp = 1.1;
}
else if (i < 128) {
i -= 96;
temp = 1.2;
}
else if (i == 128) {
i = 0;
temp = 1.4;
}
filters_[0].setTargets( temp * Phonemes::formantFrequency(i, 0), Phonemes::formantRadius(i, 0), pow(10.0, Phonemes::formantGain(i, 0 ) / 20.0) );
filters_[1].setTargets( temp * Phonemes::formantFrequency(i, 1), Phonemes::formantRadius(i, 1), pow(10.0, Phonemes::formantGain(i, 1 ) / 20.0) );
filters_[2].setTargets( temp * Phonemes::formantFrequency(i, 2), Phonemes::formantRadius(i, 2), pow(10.0, Phonemes::formantGain(i, 2 ) / 20.0) );
filters_[3].setTargets( temp * Phonemes::formantFrequency(i, 3), Phonemes::formantRadius(i, 3), pow(10.0, Phonemes::formantGain(i, 3 ) / 20.0) );
this->setVoiced( Phonemes::voiceGain( i ) );
this->setUnVoiced( Phonemes::noiseGain( i ) );
}
else if (number == __SK_ModFrequency_) // 11
voiced_->setVibratoRate( normalizedValue * 12.0); // 0 to 12 Hz
else if (number == __SK_ModWheel_) // 1
voiced_->setVibratoGain( normalizedValue * 0.2);
else if (number == __SK_AfterTouch_Cont_) { // 128
this->setVoiced( normalizedValue );
onepole_.setPole( 0.97 - ( normalizedValue * 0.2) );
}
#if defined(_STK_DEBUG_)
else {
oStream_ << "VoicForm::controlChange: undefined control number (" << number << ")!";
handleError( StkError::WARNING );
}
#endif
}
} // stk namespace