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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3118 - (show annotations) (download) (as text)
Fri Apr 21 13:33:03 2017 UTC (7 years ago) by schoenebeck
File MIME type: text/x-c++hdr
File size: 7057 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 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_SAWLFO_H__
23 #define __LS_SAWLFO_H__
24
25 #include "LFOBase.h"
26
27 namespace LinuxSampler {
28
29 /** @brief Saw LFO (int math implementation)
30 *
31 * This is a saw Low Frequency Oscillator which uses pure integer
32 * math (without branches) to synthesize the saw wave.
33 */
34 template<range_type_t RANGE, bool SAWUP>
35 class SawLFO : public LFOBase<RANGE> {
36 public:
37
38 /**
39 * Constructor
40 *
41 * @param Max - maximum value of the output levels
42 */
43 SawLFO(float Max) : LFOBase<RANGE>::LFOBase(Max) {
44 }
45
46 /**
47 * Calculates exactly one sample point of the LFO wave.
48 *
49 * @returns next LFO level
50 */
51 inline float render() {
52 const unsigned int intLimit = (unsigned int) -1; // all 0xFFFF...
53
54 uiLevel += c;
55 if (RANGE == range_unsigned)
56 return normalizer * (float) (SAWUP ? uiLevel : intLimit - uiLevel);
57 else /* signed range */
58 return normalizer * (float) (SAWUP ? uiLevel : intLimit - uiLevel) + offset;
59 }
60
61 /**
62 * Update LFO depth with a new external controller value.
63 *
64 * @param ExtControlValue - new external controller value
65 */
66 inline void updateByMIDICtrlValue(const uint16_t& ExtControlValue) {
67 this->ExtControlValue = ExtControlValue;
68
69 const unsigned int intLimit = (unsigned int) -1; // all 0xFFFF...
70 const float max = (this->InternalDepth + ExtControlValue * this->ExtControlDepthCoeff) * this->ScriptDepthFactor;
71 if (RANGE == range_unsigned) {
72 normalizer = max / (float) intLimit;
73 } else { // signed range
74 normalizer = max / (float) intLimit * 2.0f;
75 offset = -max;
76 }
77 }
78
79 /**
80 * Will be called by the voice when the key / voice was triggered.
81 *
82 * @param Frequency - frequency of the oscillator in Hz
83 * @param StartLevel - not implemented
84 * @param InternalDepth - firm, internal oscillator amplitude
85 * @param ExtControlDepth - defines how strong the external MIDI
86 * controller has influence on the
87 * oscillator amplitude
88 * @param FlipPhase - not implemented
89 * @param SampleRate - current sample rate of the engines
90 * audio output signal
91 */
92 virtual void trigger(float Frequency, start_level_t StartLevel, uint16_t InternalDepth, uint16_t ExtControlDepth, bool FlipPhase, unsigned int SampleRate) {
93 this->Frequency = Frequency;
94 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
98 const unsigned int intLimit = (unsigned int) -1; // all 0xFFFF...
99 const float freq = Frequency * this->ScriptFrequencyFactor;
100 const float r = freq / (float) SampleRate; // frequency alteration quotient
101 c = (int) (intLimit * r);
102
103 uiLevel = 0;
104 }
105
106 /**
107 * Should be invoked after the LFO is triggered.
108 * @param phase From 0 to 360 degrees.
109 */
110 void setPhase(float phase) {
111 if (phase < 0) phase = 0;
112 if (phase > 360) phase = 360;
113 phase /= 360.0f;
114 const unsigned int intLimit = (unsigned int) -1; // all 0xFFFF...
115 uiLevel = intLimit * phase;
116 }
117
118 void setFrequency(float Frequency, unsigned int SampleRate) {
119 this->Frequency = Frequency;
120 const float freq = Frequency * this->ScriptFrequencyFactor;
121 const unsigned int intLimit = (unsigned int) -1; // all 0xFFFF...
122 float r = freq / (float) SampleRate; // frequency alteration quotient
123 c = (int) (intLimit * r);
124 }
125
126 void setScriptDepthFactor(float factor) {
127 this->ScriptDepthFactor = factor;
128 updateByMIDICtrlValue(this->ExtControlValue);
129 }
130
131 void setScriptFrequencyFactor(float factor, unsigned int SampleRate) {
132 this->ScriptFrequencyFactor = factor;
133 setFrequency(this->Frequency, SampleRate);
134 }
135
136 protected:
137 unsigned int uiLevel;
138 int c;
139 float offset; ///< only needed for signed range
140 float normalizer;
141 };
142
143
144 template<range_type_t RANGE>
145 class SawUpLFO : public SawLFO<RANGE, true> {
146 public:
147 SawUpLFO(float Max) : SawLFO<RANGE, true>::SawLFO(Max) { }
148 };
149
150 template<range_type_t RANGE>
151 class SawDownLFO : public SawLFO<RANGE, false> {
152 public:
153 SawDownLFO(float Max) : SawLFO<RANGE, false>::SawLFO(Max) { }
154 };
155
156 } // namespace LinuxSampler
157
158 #endif // __LS_SAWLFO_H__

  ViewVC Help
Powered by ViewVC