/[svn]/linuxsampler/trunk/src/engines/common/LFOTriangleDiHarmonic.h
ViewVC logotype

Contents of /linuxsampler/trunk/src/engines/common/LFOTriangleDiHarmonic.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3118 - (show annotations) (download) (as text)
Fri Apr 21 13:33:03 2017 UTC (4 months ago) by schoenebeck
File MIME type: text/x-c++hdr
File size: 7886 byte(s)
* NKSP: Fixed crash when using built-in script array variable "%ALL_EVENTS".
* NKSP: Added built-in function "change_amp_lfo_depth()".
* NKSP: Added built-in function "change_amp_lfo_freq()".
* NKSP: Added built-in function "change_pitch_lfo_depth()".
* NKSP: Added built-in function "change_pitch_lfo_freq()".
* Bumped version (2.0.0.svn44).

1 /***************************************************************************
2 * *
3 * Copyright (C) 2005 - 2017 Christian Schoenebeck *
4 * *
5 * 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 *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This library is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this library; if not, write to the Free Software *
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
18 * MA 02111-1307 USA *
19 ***************************************************************************/
20
21 #ifndef __LS_LFOTRIANGLEDIHARMONIC_H__
22 #define __LS_LFOTRIANGLEDIHARMONIC_H__
23
24 #include "LFOBase.h"
25
26 // amplitue of 2nd harmonic (to approximate the triangular wave)
27 #define AMP2 -0.11425509f
28
29 namespace LinuxSampler {
30
31 /** @brief Triangle LFO (di-harmonic implementation)
32 *
33 * This is a triangle Low Frequency Oscillator implementation which uses
34 * a di-harmonic solution. This means it sums up two harmonics
35 * (sinusoids) to approximate a triangular wave.
36 */
37 template<range_type_t RANGE>
38 class LFOTriangleDiHarmonic : public LFOBase<RANGE> {
39 public:
40
41 /**
42 * Constructor
43 *
44 * @param Max - maximum value of the output levels
45 */
46 LFOTriangleDiHarmonic(float Max) : LFOBase<RANGE>::LFOBase(Max) {
47 }
48
49 /**
50 * Calculates exactly one sample point of the LFO wave.
51 *
52 * @returns next LFO level
53 */
54 inline float render() {
55 real1 -= c1 * imag1;
56 imag1 += c1 * real1;
57 real2 -= c2 * imag2;
58 imag2 += c2 * real2;
59 if (RANGE == range_unsigned)
60 return (real1 + real2 * AMP2) * normalizer + offset;
61 else /* signed range */
62 return (real1 + real2 * AMP2) * normalizer;
63 }
64
65 /**
66 * Update LFO depth with a new external controller value.
67 *
68 * @param ExtControlValue - new external controller value
69 */
70 inline void updateByMIDICtrlValue(const uint16_t& ExtControlValue) {
71 this->ExtControlValue = ExtControlValue;
72
73 const float max = (this->InternalDepth + ExtControlValue * this->ExtControlDepthCoeff) * this->ScriptDepthFactor;
74 if (RANGE == range_unsigned) {
75 const float harmonicCompensation = 1.0f + fabsf(AMP2); // to compensate the compensation ;) (see trigger())
76 normalizer = max * 0.5f;
77 offset = normalizer * harmonicCompensation;
78 } else { // signed range
79 normalizer = max;
80 }
81 }
82
83 /**
84 * Will be called by the voice when the key / voice was triggered.
85 *
86 * @param Frequency - frequency of the oscillator in Hz
87 * @param StartLevel - on which level the wave should start
88 * @param InternalDepth - firm, internal oscillator amplitude
89 * @param ExtControlDepth - defines how strong the external MIDI
90 * controller has influence on the
91 * oscillator amplitude
92 * @param FlipPhase - inverts the oscillator wave against
93 * a horizontal axis
94 * @param SampleRate - current sample rate of the engines
95 * audio output signal
96 */
97 void trigger(float Frequency, start_level_t StartLevel, uint16_t InternalDepth, uint16_t ExtControlDepth, bool FlipPhase, unsigned int SampleRate) {
98 this->Frequency = Frequency;
99 this->ScriptFrequencyFactor = this->ScriptDepthFactor = 1.f; // reset for new voice
100 const float harmonicCompensation = 1.0f + fabsf(AMP2); // to compensate the 2nd harmonic's amplitude overhead
101 this->InternalDepth = (InternalDepth / 1200.0f) * this->Max / harmonicCompensation;
102 this->ExtControlDepthCoeff = (((float) ExtControlDepth / 1200.0f) / 127.0f) * this->Max / harmonicCompensation;
103
104 const float freq = Frequency * this->ScriptFrequencyFactor;
105 c1 = 2.0f * M_PI * freq / (float) SampleRate;
106 c2 = 2.0f * M_PI * freq / (float) SampleRate * 3.0f;
107
108 double phi; // phase displacement
109 switch (StartLevel) {
110 case start_level_mid:
111 //FIXME: direct jumping to 90� and 270� doesn't work out due to numeric accuracy problems (causes wave deformation)
112 //phi = (FlipPhase) ? 0.5 * M_PI : 1.5 * M_PI; // 90� or 270�
113 //break;
114 case start_level_max:
115 phi = (FlipPhase) ? M_PI : 0.0; // 180� or 0�
116 break;
117 case start_level_min:
118 phi = (FlipPhase) ? 0.0 : M_PI; // 0� or 180�
119 break;
120 }
121 real1 = real2 = cos(phi);
122 imag1 = imag2 = sin(phi);
123 }
124
125 /**
126 * Should be invoked after the LFO is triggered with StartLevel
127 * start_level_min.
128 * @param phase From 0 to 360 degrees.
129 */
130 void setPhase(float phase) {
131 if (phase < 0) phase = 0;
132 if (phase > 360) phase = 360;
133 phase /= 360.0f;
134
135 // FIXME: too heavy?
136 float steps = 1.0f / (c1 / (2.0f * M_PI)); // number of steps for one cycle
137 steps *= phase + 0.25f;
138 for (int i = 0; i < steps; i++) render();
139 }
140
141 void setFrequency(float Frequency, unsigned int SampleRate) {
142 this->Frequency = Frequency;
143 const float freq = Frequency * this->ScriptFrequencyFactor;
144 c1 = 2.0f * M_PI * freq / (float) SampleRate;
145 c2 = 2.0f * M_PI * freq / (float) SampleRate * 3.0f;
146 }
147
148 void setScriptDepthFactor(float factor) {
149 this->ScriptDepthFactor = factor;
150 updateByMIDICtrlValue(this->ExtControlValue);
151 }
152
153 void setScriptFrequencyFactor(float factor, unsigned int SampleRate) {
154 this->ScriptFrequencyFactor = factor;
155 setFrequency(this->Frequency, SampleRate);
156 }
157
158 private:
159 float c1;
160 float c2;
161 float real1;
162 float imag1;
163 float real2;
164 float imag2;
165 float normalizer;
166 float offset;
167 };
168
169 } // namespace LinuxSampler
170
171 #endif // __LS_LFOTRIANGLEDIHARMONIC_H__

  ViewVC Help
Powered by ViewVC