1 |
/*************************************************************************** |
/*************************************************************************** |
2 |
* * |
* * |
3 |
* Copyright (C) 2005 Christian Schoenebeck * |
* Copyright (C) 2005 - 2019 Christian Schoenebeck * |
4 |
* * |
* * |
5 |
* This library is free software; you can redistribute it and/or modify * |
* This library is free software; you can redistribute it and/or modify * |
6 |
* it under the terms of the GNU General Public License as published by * |
* it under the terms of the GNU General Public License as published by * |
62 |
* |
* |
63 |
* @param ExtControlValue - new external controller value |
* @param ExtControlValue - new external controller value |
64 |
*/ |
*/ |
65 |
inline void update(const uint16_t& ExtControlValue) { |
inline void updateByMIDICtrlValue(const uint16_t& ExtControlValue) { |
66 |
|
this->ExtControlValue = ExtControlValue; |
67 |
|
|
68 |
const unsigned int intLimit = (unsigned int) -1; // all 0xFFFF... |
const unsigned int intLimit = (unsigned int) -1; // all 0xFFFF... |
69 |
const float max = InternalDepth + ExtControlValue * ExtControlDepthCoeff; |
const float max = (this->InternalDepth + ExtControlValue * this->ExtControlDepthCoeff) * this->ScriptDepthFactor; |
70 |
if (RANGE == range_unsigned) { |
if (RANGE == range_unsigned) { |
71 |
normalizer = max / (float) intLimit; |
normalizer = max / (float) intLimit; |
72 |
} else { // signed range |
} else { // signed range |
90 |
* audio output signal |
* audio output signal |
91 |
*/ |
*/ |
92 |
void trigger(float Frequency, start_level_t StartLevel, uint16_t InternalDepth, uint16_t ExtControlDepth, bool FlipPhase, unsigned int SampleRate) { |
void trigger(float Frequency, start_level_t StartLevel, uint16_t InternalDepth, uint16_t ExtControlDepth, bool FlipPhase, unsigned int SampleRate) { |
93 |
this->InternalDepth = (InternalDepth / 1200.0f) * Max; |
this->Frequency = Frequency; |
94 |
this->ExtControlDepthCoeff = (((float) ExtControlDepth / 1200.0f) / 127.0f) * Max; |
this->InternalDepth = (InternalDepth / 1200.0f) * this->Max; |
95 |
|
this->ExtControlDepthCoeff = (((float) ExtControlDepth / 1200.0f) / 127.0f) * this->Max; |
96 |
|
this->ScriptFrequencyFactor = this->ScriptDepthFactor = 1.f; // reset for new voice |
97 |
if (RANGE == range_unsigned) { |
if (RANGE == range_unsigned) { |
98 |
this->InternalDepth *= 2.0f; |
this->InternalDepth *= 2.0f; |
99 |
this->ExtControlDepthCoeff *= 2.0f; |
this->ExtControlDepthCoeff *= 2.0f; |
100 |
} |
} |
101 |
|
this->pFinalDepth = NULL; |
102 |
|
this->pFinalFrequency = NULL; |
103 |
|
|
104 |
const unsigned int intLimit = (unsigned int) -1; // all 0xFFFF... |
const unsigned int intLimit = (unsigned int) -1; // all 0xFFFF... |
105 |
const float r = Frequency / (float) SampleRate; // frequency alteration quotient |
const float freq = Frequency * this->ScriptFrequencyFactor; |
106 |
|
const float r = freq / (float) SampleRate; // frequency alteration quotient |
107 |
c = (int) (intLimit * r); |
c = (int) (intLimit * r); |
108 |
|
|
109 |
switch (StartLevel) { |
switch (StartLevel) { |
111 |
iLevel = (FlipPhase) ? 0 : intLimit >> 1; |
iLevel = (FlipPhase) ? 0 : intLimit >> 1; |
112 |
break; |
break; |
113 |
case start_level_mid: |
case start_level_mid: |
114 |
if (FlipPhase) c = -c; // wave should go down |
iLevel = (FlipPhase) ? intLimit / 4 * 3 : intLimit >> 2; |
|
iLevel = intLimit >> 2; |
|
115 |
break; |
break; |
116 |
case start_level_min: |
case start_level_min: |
117 |
iLevel = (FlipPhase) ? intLimit >> 1 : 0; |
iLevel = (FlipPhase) ? intLimit >> 1 : 0; |
118 |
break; |
break; |
119 |
} |
} |
120 |
} |
} |
121 |
|
|
122 |
|
/** |
123 |
|
* Should be invoked after the LFO is triggered. |
124 |
|
* @param phase From 0 to 360 degrees. |
125 |
|
*/ |
126 |
|
void setPhase(float phase) { |
127 |
|
if (phase < 0) phase = 0; |
128 |
|
if (phase > 360) phase = 360; |
129 |
|
phase /= 360.0f; |
130 |
|
const unsigned int intLimit = (unsigned int) -1; // all 0xFFFF... |
131 |
|
unsigned int uiPhase = intLimit * phase + iLevel; |
132 |
|
if (uiPhase > intLimit / 2) iLevel = uiPhase - intLimit; |
133 |
|
else iLevel = uiPhase; |
134 |
|
} |
135 |
|
|
136 |
|
void setFrequency(float Frequency, unsigned int SampleRate) { |
137 |
|
this->Frequency = Frequency; |
138 |
|
const float freq = Frequency * this->ScriptFrequencyFactor; |
139 |
|
const unsigned int intLimit = (unsigned int) -1; // all 0xFFFF... |
140 |
|
float r = freq / (float) SampleRate; // frequency alteration quotient |
141 |
|
c = (int) (intLimit * r); |
142 |
|
} |
143 |
|
|
144 |
|
void setScriptDepthFactor(float factor, bool isFinal) { |
145 |
|
this->ScriptDepthFactor = factor; |
146 |
|
// set or reset this script depth parameter to be the sole |
147 |
|
// source for the LFO depth |
148 |
|
if (isFinal && !this->pFinalDepth) |
149 |
|
this->pFinalDepth = &this->ScriptDepthFactor; |
150 |
|
else if (!isFinal && this->pFinalDepth == &this->ScriptDepthFactor) |
151 |
|
this->pFinalDepth = NULL; |
152 |
|
// recalculate upon new depth |
153 |
|
updateByMIDICtrlValue(this->ExtControlValue); |
154 |
|
} |
155 |
|
|
156 |
|
void setScriptFrequencyFactor(float factor, unsigned int SampleRate) { |
157 |
|
this->ScriptFrequencyFactor = factor; |
158 |
|
// in case script frequency was set as "final" value before, |
159 |
|
// reset it so that all sources are processed from now on |
160 |
|
if (this->pFinalFrequency == &this->ScriptFrequencyFactor) |
161 |
|
this->pFinalFrequency = NULL; |
162 |
|
// recalculate upon new frequency |
163 |
|
setFrequency(this->Frequency, SampleRate); |
164 |
|
} |
165 |
|
|
166 |
|
void setScriptFrequencyFinal(float hz, unsigned int SampleRate) { |
167 |
|
this->ScriptFrequencyFactor = hz; |
168 |
|
// assign script's given frequency as sole source for the LFO |
169 |
|
// frequency, thus ignore all other sources |
170 |
|
if (!this->pFinalFrequency) |
171 |
|
this->pFinalFrequency = &this->ScriptFrequencyFactor; |
172 |
|
// recalculate upon new frequency |
173 |
|
setFrequency(this->Frequency, SampleRate); |
174 |
|
} |
175 |
|
|
176 |
private: |
protected: |
177 |
int iLevel; |
int iLevel; |
178 |
int c; |
int c; |
179 |
float offset; ///< only needed for signed range |
float offset; ///< only needed for signed range |