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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC