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

Annotation of /linuxsampler/trunk/src/engines/common/LFOSawIntMathOld.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3625 - (hide annotations) (download) (as text)
Thu Oct 3 13:37:25 2019 UTC (4 years, 6 months ago) by schoenebeck
File MIME type: text/x-c++hdr
File size: 8647 byte(s)
* gig format extension: Added support for different LFO wave forms
  (currently either sine [default], triangle, saw or square).

* gig format extension: Added support for LFO phase displacement
  (0°..360°).

* gig format extension: Added support for flipping LFO polarity on LFO 3
  (in the original gig format this was only available for LFO 1 and LFO 2).

* Bumped version (2.1.1.svn22).

1 iliev 2223 /***************************************************************************
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 schoenebeck 3614 #ifndef __LS_SAWLFOINTOLD_H__
23     #define __LS_SAWLFOINTOLD_H__
24 iliev 2223
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 schoenebeck 3612 template<LFO::range_type_t RANGE, bool SAWUP>
35 schoenebeck 3614 class LFOSawIntMathOld : public LFOBase<RANGE> {
36 iliev 2223 public:
37    
38     /**
39     * Constructor
40     *
41     * @param Max - maximum value of the output levels
42     */
43 schoenebeck 3614 LFOSawIntMathOld(float Max) : LFOBase<RANGE>::LFOBase(Max) {
44 schoenebeck 3625 //NOTE: DO NOT add any custom initialization here, since it would break LFOCluster construction !
45 iliev 2223 }
46    
47     /**
48     * Calculates exactly one sample point of the LFO wave.
49     *
50     * @returns next LFO level
51     */
52     inline float render() {
53     const unsigned int intLimit = (unsigned int) -1; // all 0xFFFF...
54    
55     uiLevel += c;
56 schoenebeck 3612 if (RANGE == LFO::range_unsigned)
57 iliev 2223 return normalizer * (float) (SAWUP ? uiLevel : intLimit - uiLevel);
58     else /* signed range */
59     return normalizer * (float) (SAWUP ? uiLevel : intLimit - uiLevel) + offset;
60     }
61    
62     /**
63     * Update LFO depth with a new external controller value.
64     *
65     * @param ExtControlValue - new external controller value
66     */
67 schoenebeck 3118 inline void updateByMIDICtrlValue(const uint16_t& ExtControlValue) {
68     this->ExtControlValue = ExtControlValue;
69    
70 iliev 2223 const unsigned int intLimit = (unsigned int) -1; // all 0xFFFF...
71 schoenebeck 3118 const float max = (this->InternalDepth + ExtControlValue * this->ExtControlDepthCoeff) * this->ScriptDepthFactor;
72 schoenebeck 3612 if (RANGE == LFO::range_unsigned) {
73 iliev 2223 normalizer = max / (float) intLimit;
74     } else { // signed range
75     normalizer = max / (float) intLimit * 2.0f;
76     offset = -max;
77     }
78     }
79    
80     /**
81     * Will be called by the voice when the key / voice was triggered.
82     *
83     * @param Frequency - frequency of the oscillator in Hz
84     * @param StartLevel - not implemented
85     * @param InternalDepth - firm, internal oscillator amplitude
86     * @param ExtControlDepth - defines how strong the external MIDI
87     * controller has influence on the
88     * oscillator amplitude
89     * @param FlipPhase - not implemented
90     * @param SampleRate - current sample rate of the engines
91     * audio output signal
92     */
93 schoenebeck 3612 virtual void trigger(float Frequency, LFO::start_level_t StartLevel, uint16_t InternalDepth, uint16_t ExtControlDepth, bool FlipPhase, unsigned int SampleRate) {
94 schoenebeck 3118 this->Frequency = Frequency;
95 iliev 2223 this->InternalDepth = (InternalDepth / 1200.0f) * this->Max;
96     this->ExtControlDepthCoeff = (((float) ExtControlDepth / 1200.0f) / 127.0f) * this->Max;
97 schoenebeck 3118 this->ScriptFrequencyFactor = this->ScriptDepthFactor = 1.f; // reset for new voice
98 schoenebeck 3561 this->pFinalDepth = NULL;
99     this->pFinalFrequency = NULL;
100 iliev 2223
101     const unsigned int intLimit = (unsigned int) -1; // all 0xFFFF...
102 schoenebeck 3118 const float freq = Frequency * this->ScriptFrequencyFactor;
103     const float r = freq / (float) SampleRate; // frequency alteration quotient
104 iliev 2223 c = (int) (intLimit * r);
105    
106     uiLevel = 0;
107     }
108 iliev 2225
109     /**
110     * Should be invoked after the LFO is triggered.
111     * @param phase From 0 to 360 degrees.
112     */
113     void setPhase(float phase) {
114     if (phase < 0) phase = 0;
115     if (phase > 360) phase = 360;
116     phase /= 360.0f;
117     const unsigned int intLimit = (unsigned int) -1; // all 0xFFFF...
118     uiLevel = intLimit * phase;
119     }
120 iliev 2227
121     void setFrequency(float Frequency, unsigned int SampleRate) {
122 schoenebeck 3118 this->Frequency = Frequency;
123     const float freq = Frequency * this->ScriptFrequencyFactor;
124 iliev 2227 const unsigned int intLimit = (unsigned int) -1; // all 0xFFFF...
125 schoenebeck 3118 float r = freq / (float) SampleRate; // frequency alteration quotient
126 iliev 2227 c = (int) (intLimit * r);
127     }
128 iliev 2223
129 schoenebeck 3561 void setScriptDepthFactor(float factor, bool isFinal) {
130 schoenebeck 3118 this->ScriptDepthFactor = factor;
131 schoenebeck 3561 // set or reset this script depth parameter to be the sole
132     // source for the LFO depth
133     if (isFinal && !this->pFinalDepth)
134     this->pFinalDepth = &this->ScriptDepthFactor;
135     else if (!isFinal && this->pFinalDepth == &this->ScriptDepthFactor)
136     this->pFinalDepth = NULL;
137     // recalculate upon new depth
138 schoenebeck 3118 updateByMIDICtrlValue(this->ExtControlValue);
139     }
140    
141     void setScriptFrequencyFactor(float factor, unsigned int SampleRate) {
142     this->ScriptFrequencyFactor = factor;
143 schoenebeck 3561 // in case script frequency was set as "final" value before,
144     // reset it so that all sources are processed from now on
145     if (this->pFinalFrequency == &this->ScriptFrequencyFactor)
146     this->pFinalFrequency = NULL;
147     // recalculate upon new frequency
148 schoenebeck 3118 setFrequency(this->Frequency, SampleRate);
149     }
150    
151 schoenebeck 3561 void setScriptFrequencyFinal(float hz, unsigned int SampleRate) {
152     this->ScriptFrequencyFactor = hz;
153     // assign script's given frequency as sole source for the LFO
154     // frequency, thus ignore all other sources
155     if (!this->pFinalFrequency)
156     this->pFinalFrequency = &this->ScriptFrequencyFactor;
157     // recalculate upon new frequency
158     setFrequency(this->Frequency, SampleRate);
159     }
160    
161 iliev 2223 protected:
162     unsigned int uiLevel;
163     int c;
164     float offset; ///< only needed for signed range
165     float normalizer;
166     };
167    
168    
169 schoenebeck 3612 template<LFO::range_type_t RANGE>
170 schoenebeck 3614 class SawUpLFO : public LFOSawIntMathOld<RANGE, true> {
171 iliev 2223 public:
172 schoenebeck 3614 SawUpLFO(float Max) : LFOSawIntMathOld<RANGE, true>::LFOSawIntMathOld(Max) { }
173 iliev 2223 };
174    
175 schoenebeck 3612 template<LFO::range_type_t RANGE>
176 schoenebeck 3614 class SawDownLFO : public LFOSawIntMathOld<RANGE, false> {
177 iliev 2223 public:
178 schoenebeck 3614 SawDownLFO(float Max) : LFOSawIntMathOld<RANGE, false>::LFOSawIntMathOld(Max) { }
179 iliev 2223 };
180    
181     } // namespace LinuxSampler
182    
183 schoenebeck 3614 #endif // __LS_SAWLFOINTOLD_H__

  ViewVC Help
Powered by ViewVC