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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3118 - (hide 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: 6246 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 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     #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 schoenebeck 3118 inline void updateByMIDICtrlValue(const uint16_t& ExtControlValue) {
62     this->ExtControlValue = ExtControlValue;
63    
64 schoenebeck 3034 //const unsigned int intLimit = (unsigned int) -1; // all 0xFFFF...
65 schoenebeck 3118 const float max = (this->InternalDepth + ExtControlValue * this->ExtControlDepthCoeff) * this->ScriptDepthFactor;
66 iliev 2223 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 schoenebeck 3118 this->Frequency = Frequency;
88 iliev 2223 this->InternalDepth = (InternalDepth / 1200.0f) * this->Max;
89     this->ExtControlDepthCoeff = (((float) ExtControlDepth / 1200.0f) / 127.0f) * this->Max;
90 schoenebeck 3118 this->ScriptFrequencyFactor = this->ScriptDepthFactor = 1.f; // reset for new voice
91 iliev 2223
92     const unsigned int intLimit = (unsigned int) -1; // all 0xFFFF...
93 schoenebeck 3118 const float freq = Frequency * this->ScriptFrequencyFactor;
94     const float r = freq / (float) SampleRate; // frequency alteration quotient
95 iliev 2223 c = (int) (intLimit * r);
96     c2 = (2.0f * M_PI) / (float) intLimit;
97    
98     uiLevel = 0;
99     }
100 iliev 2225
101     /**
102     * @param phase 0 to 360 degrees
103     */
104     void setPhase(float phase) {
105     if (phase < 0) phase = 0;
106     if (phase > 360) phase = 360;
107     phase /= 360.0f;
108     const unsigned int intLimit = (unsigned int) -1; // all 0xFFFF...
109     uiLevel = intLimit * phase;
110     }
111 iliev 2227
112     void setFrequency(float Frequency, unsigned int SampleRate) {
113 schoenebeck 3118 this->Frequency = Frequency;
114     const float freq = Frequency * this->ScriptFrequencyFactor;
115 iliev 2227 const unsigned int intLimit = (unsigned int) -1; // all 0xFFFF...
116 schoenebeck 3118 float r = freq / (float) SampleRate; // frequency alteration quotient
117 iliev 2227 c = (int) (intLimit * r);
118     }
119 iliev 2223
120 schoenebeck 3118 void setScriptDepthFactor(float factor) {
121     this->ScriptDepthFactor = factor;
122     updateByMIDICtrlValue(this->ExtControlValue);
123     }
124    
125     void setScriptFrequencyFactor(float factor, unsigned int SampleRate) {
126     this->ScriptFrequencyFactor = factor;
127     setFrequency(this->Frequency, SampleRate);
128     }
129    
130 iliev 2223 protected:
131     unsigned int uiLevel;
132     int c;
133     float c2;
134     float normalizer;
135     };
136    
137     } // namespace LinuxSampler
138    
139     #endif // __LS_SINELFO_H__

  ViewVC Help
Powered by ViewVC