1 |
/*************************************************************************** |
2 |
* * |
3 |
* Copyright (C) 2005 Christian Schoenebeck * |
4 |
* Copyright (C) 2011 Christian Schoenebeck and Grigor Iliev * |
5 |
* * |
6 |
* This library is free software; you can redistribute it and/or modify * |
7 |
* it under the terms of the GNU General Public License as published by * |
8 |
* the Free Software Foundation; either version 2 of the License, or * |
9 |
* (at your option) any later version. * |
10 |
* * |
11 |
* This library is distributed in the hope that it will be useful, * |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
14 |
* GNU General Public License for more details. * |
15 |
* * |
16 |
* You should have received a copy of the GNU General Public License * |
17 |
* along with this library; if not, write to the Free Software * |
18 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * |
19 |
* MA 02111-1307 USA * |
20 |
***************************************************************************/ |
21 |
|
22 |
#ifndef __LS_SINELFO_H__ |
23 |
#define __LS_SINELFO_H__ |
24 |
|
25 |
#include "LFOBase.h" |
26 |
|
27 |
namespace LinuxSampler { |
28 |
|
29 |
/** @brief sine LFO |
30 |
*/ |
31 |
template<range_type_t RANGE> |
32 |
class SineLFO : public LFOBase<RANGE> { |
33 |
public: |
34 |
|
35 |
/** |
36 |
* Constructor |
37 |
* |
38 |
* @param Max - maximum value of the output levels |
39 |
*/ |
40 |
SineLFO(float Max) : LFOBase<RANGE>::LFOBase(Max) { |
41 |
} |
42 |
|
43 |
/** |
44 |
* Calculates exactly one sample point of the LFO wave. |
45 |
* |
46 |
* @returns next LFO level |
47 |
*/ |
48 |
inline float render() { |
49 |
uiLevel += c; |
50 |
if (RANGE == range_unsigned) |
51 |
return normalizer * (sin(c2 * (float)uiLevel) + 1.0f); |
52 |
else /* signed range */ |
53 |
return normalizer * sin(c2 * (float)uiLevel); |
54 |
} |
55 |
|
56 |
/** |
57 |
* Update LFO depth with a new external controller value. |
58 |
* |
59 |
* @param ExtControlValue - new external controller value |
60 |
*/ |
61 |
inline void updateByMIDICtrlValue(const uint16_t& ExtControlValue) { |
62 |
this->ExtControlValue = ExtControlValue; |
63 |
|
64 |
//const unsigned int intLimit = (unsigned int) -1; // all 0xFFFF... |
65 |
const float max = (this->InternalDepth + ExtControlValue * this->ExtControlDepthCoeff) * this->ScriptDepthFactor; |
66 |
if (RANGE == range_unsigned) { |
67 |
normalizer = max / 2.0f; |
68 |
} else { // signed range |
69 |
normalizer = max; |
70 |
} |
71 |
} |
72 |
|
73 |
/** |
74 |
* Will be called by the voice when the key / voice was triggered. |
75 |
* |
76 |
* @param Frequency - frequency of the oscillator in Hz |
77 |
* @param StartLevel - not implemented |
78 |
* @param InternalDepth - firm, internal oscillator amplitude |
79 |
* @param ExtControlDepth - defines how strong the external MIDI |
80 |
* controller has influence on the |
81 |
* oscillator amplitude |
82 |
* @param FlipPhase - not implemented |
83 |
* @param SampleRate - current sample rate of the engines |
84 |
* audio output signal |
85 |
*/ |
86 |
virtual void trigger(float Frequency, start_level_t StartLevel, uint16_t InternalDepth, uint16_t ExtControlDepth, bool FlipPhase, unsigned int SampleRate) { |
87 |
this->Frequency = Frequency; |
88 |
this->InternalDepth = (InternalDepth / 1200.0f) * this->Max; |
89 |
this->ExtControlDepthCoeff = (((float) ExtControlDepth / 1200.0f) / 127.0f) * this->Max; |
90 |
this->ScriptFrequencyFactor = this->ScriptDepthFactor = 1.f; // reset for new voice |
91 |
this->pFinalDepth = NULL; |
92 |
this->pFinalFrequency = NULL; |
93 |
|
94 |
const unsigned int intLimit = (unsigned int) -1; // all 0xFFFF... |
95 |
const float freq = Frequency * this->ScriptFrequencyFactor; |
96 |
const float r = freq / (float) SampleRate; // frequency alteration quotient |
97 |
c = (int) (intLimit * r); |
98 |
c2 = (2.0f * M_PI) / (float) intLimit; |
99 |
|
100 |
uiLevel = 0; |
101 |
} |
102 |
|
103 |
/** |
104 |
* @param phase 0 to 360 degrees |
105 |
*/ |
106 |
void setPhase(float phase) { |
107 |
if (phase < 0) phase = 0; |
108 |
if (phase > 360) phase = 360; |
109 |
phase /= 360.0f; |
110 |
const unsigned int intLimit = (unsigned int) -1; // all 0xFFFF... |
111 |
uiLevel = intLimit * phase; |
112 |
} |
113 |
|
114 |
void setFrequency(float Frequency, unsigned int SampleRate) { |
115 |
this->Frequency = Frequency; |
116 |
const float freq = Frequency * this->ScriptFrequencyFactor; |
117 |
const unsigned int intLimit = (unsigned int) -1; // all 0xFFFF... |
118 |
float r = freq / (float) SampleRate; // frequency alteration quotient |
119 |
c = (int) (intLimit * r); |
120 |
} |
121 |
|
122 |
void setScriptDepthFactor(float factor, bool isFinal) { |
123 |
this->ScriptDepthFactor = factor; |
124 |
// set or reset this script depth parameter to be the sole |
125 |
// source for the LFO depth |
126 |
if (isFinal && !this->pFinalDepth) |
127 |
this->pFinalDepth = &this->ScriptDepthFactor; |
128 |
else if (!isFinal && this->pFinalDepth == &this->ScriptDepthFactor) |
129 |
this->pFinalDepth = NULL; |
130 |
// recalculate upon new depth |
131 |
updateByMIDICtrlValue(this->ExtControlValue); |
132 |
} |
133 |
|
134 |
void setScriptFrequencyFactor(float factor, unsigned int SampleRate) { |
135 |
this->ScriptFrequencyFactor = factor; |
136 |
// in case script frequency was set as "final" value before, |
137 |
// reset it so that all sources are processed from now on |
138 |
if (this->pFinalFrequency == &this->ScriptFrequencyFactor) |
139 |
this->pFinalFrequency = NULL; |
140 |
// recalculate upon new frequency |
141 |
setFrequency(this->Frequency, SampleRate); |
142 |
} |
143 |
|
144 |
void setScriptFrequencyFinal(float hz, unsigned int SampleRate) { |
145 |
this->ScriptFrequencyFactor = hz; |
146 |
// assign script's given frequency as sole source for the LFO |
147 |
// frequency, thus ignore all other sources |
148 |
if (!this->pFinalFrequency) |
149 |
this->pFinalFrequency = &this->ScriptFrequencyFactor; |
150 |
// recalculate upon new frequency |
151 |
setFrequency(this->Frequency, SampleRate); |
152 |
} |
153 |
|
154 |
protected: |
155 |
unsigned int uiLevel; |
156 |
int c; |
157 |
float c2; |
158 |
float normalizer; |
159 |
}; |
160 |
|
161 |
} // namespace LinuxSampler |
162 |
|
163 |
#endif // __LS_SINELFO_H__ |