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

Contents of /linuxsampler/trunk/src/engines/common/PulseLFO.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 weeks, 3 days ago) by schoenebeck
File MIME type: text/x-c++hdr
File size: 6806 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_PULSELFO_H__
23 #define __LS_PULSELFO_H__
24
25 #include "LFOBase.h"
26
27 namespace LinuxSampler {
28
29 /** @brief Pulse LFO (int math implementation)
30 *
31 * This is a pulse Low Frequency Oscillator which uses pure integer
32 * math (without branches) to synthesize the pulse wave.
33 */
34 template<range_type_t RANGE, int WIDTH /* in permilles */>
35 class PulseLFO : public LFOBase<RANGE> {
36 public:
37
38 /**
39 * Constructor
40 *
41 * @param Max - maximum value of the output levels
42 */
43 PulseLFO(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 uiLevel += c;
53 if (RANGE == range_unsigned)
54 return uiLevel <= width ? normalizer : 0;
55 else /* signed range */
56 return uiLevel <= width ? normalizer : -normalizer;
57 }
58
59 /**
60 * Update LFO depth with a new external controller value.
61 *
62 * @param ExtControlValue - new external controller value
63 */
64 inline void updateByMIDICtrlValue(const uint16_t& ExtControlValue) {
65 this->ExtControlValue = ExtControlValue;
66
67 const float max = (this->InternalDepth + ExtControlValue * this->ExtControlDepthCoeff) * this->ScriptDepthFactor;
68 normalizer = max;
69 }
70
71 /**
72 * Will be called by the voice when the key / voice was triggered.
73 *
74 * @param Frequency - frequency of the oscillator in Hz
75 * @param InternalDepth - firm, internal oscillator amplitude
76 * @param ExtControlDepth - defines how strong the external MIDI
77 * controller has influence on the
78 * oscillator amplitude
79 * @param FlipPhase - inverts the oscillator wave against
80 * a horizontal axis
81 * @param SampleRate - current sample rate of the engines
82 * audio output signal
83 * @param PulseWidth - the pulse width in percents
84 */
85 void trigger(float Frequency, uint16_t InternalDepth, uint16_t ExtControlDepth, float PulseWidth, unsigned int SampleRate) {
86 this->Frequency = Frequency;
87 this->InternalDepth = (InternalDepth / 1200.0f) * this->Max;
88 this->ExtControlDepthCoeff = (((float) ExtControlDepth / 1200.0f) / 127.0f) * this->Max;
89 this->ScriptFrequencyFactor = this->ScriptDepthFactor = 1.f; // reset for new voice
90
91 const unsigned int intLimit = (unsigned int) -1; // all 0xFFFF...
92 const float freq = Frequency * this->ScriptFrequencyFactor;
93 const float r = freq / (float) SampleRate; // frequency alteration quotient
94 c = (int) (intLimit * r);
95 width = (PulseWidth / 100.0) * intLimit;
96
97 uiLevel = 0;
98 }
99
100 virtual void trigger(float Frequency, start_level_t StartLevel, uint16_t InternalDepth, uint16_t ExtControlDepth, bool FlipPhase, unsigned int SampleRate) {
101 trigger(Frequency, InternalDepth, ExtControlDepth, WIDTH / 10.0f, SampleRate);
102 }
103
104 /**
105 * Should be invoked after the LFO is triggered.
106 * @param phase From 0 to 360 degrees.
107 */
108 void setPhase(float phase) {
109 if (phase < 0) phase = 0;
110 if (phase > 360) phase = 360;
111 phase /= 360.0f;
112 const unsigned int intLimit = (unsigned int) -1; // all 0xFFFF...
113 uiLevel = intLimit * phase;
114 }
115
116 void setFrequency(float Frequency, unsigned int SampleRate) {
117 this->Frequency = Frequency;
118 const float freq = Frequency * this->ScriptFrequencyFactor;
119 const unsigned int intLimit = (unsigned int) -1; // all 0xFFFF...
120 float r = freq / (float) SampleRate; // frequency alteration quotient
121 c = (int) (intLimit * r);
122 }
123
124 void setScriptDepthFactor(float factor) {
125 this->ScriptDepthFactor = factor;
126 updateByMIDICtrlValue(this->ExtControlValue);
127 }
128
129 void setScriptFrequencyFactor(float factor, unsigned int SampleRate) {
130 this->ScriptFrequencyFactor = factor;
131 setFrequency(this->Frequency, SampleRate);
132 }
133
134 protected:
135 unsigned int uiLevel;
136 unsigned int width;
137 int c;
138 float normalizer;
139 };
140
141 template<range_type_t RANGE>
142 class SquareLFO : public PulseLFO<RANGE, 500> {
143 public:
144 SquareLFO(float Max) : PulseLFO<RANGE, 500>::PulseLFO(Max) { }
145 };
146
147 } // namespace LinuxSampler
148
149 #endif // __LS_PULSELFO_H__

  ViewVC Help
Powered by ViewVC