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_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 update(const uint16_t& ExtControlValue) { |
67 |
|
|
const unsigned int intLimit = (unsigned int) -1; // all 0xFFFF... |
68 |
|
|
const float max = this->InternalDepth + ExtControlValue * this->ExtControlDepthCoeff; |
69 |
|
|
if (RANGE == range_unsigned) { |
70 |
|
|
normalizer = max / (float) intLimit; |
71 |
|
|
} else { // signed range |
72 |
|
|
normalizer = max / (float) intLimit * 2.0f; |
73 |
|
|
offset = -max; |
74 |
|
|
} |
75 |
|
|
} |
76 |
|
|
|
77 |
|
|
/** |
78 |
|
|
* Will be called by the voice when the key / voice was triggered. |
79 |
|
|
* |
80 |
|
|
* @param Frequency - frequency of the oscillator in Hz |
81 |
|
|
* @param StartLevel - not implemented |
82 |
|
|
* @param InternalDepth - firm, internal oscillator amplitude |
83 |
|
|
* @param ExtControlDepth - defines how strong the external MIDI |
84 |
|
|
* controller has influence on the |
85 |
|
|
* oscillator amplitude |
86 |
|
|
* @param FlipPhase - not implemented |
87 |
|
|
* @param SampleRate - current sample rate of the engines |
88 |
|
|
* audio output signal |
89 |
|
|
*/ |
90 |
|
|
virtual void trigger(float Frequency, start_level_t StartLevel, uint16_t InternalDepth, uint16_t ExtControlDepth, bool FlipPhase, unsigned int SampleRate) { |
91 |
|
|
this->InternalDepth = (InternalDepth / 1200.0f) * this->Max; |
92 |
|
|
this->ExtControlDepthCoeff = (((float) ExtControlDepth / 1200.0f) / 127.0f) * this->Max; |
93 |
|
|
|
94 |
|
|
const unsigned int intLimit = (unsigned int) -1; // all 0xFFFF... |
95 |
|
|
const float r = Frequency / (float) SampleRate; // frequency alteration quotient |
96 |
|
|
c = (int) (intLimit * r); |
97 |
|
|
|
98 |
|
|
uiLevel = 0; |
99 |
|
|
} |
100 |
iliev |
2225 |
|
101 |
|
|
/** |
102 |
|
|
* Should be invoked after the LFO is triggered. |
103 |
|
|
* @param phase From 0 to 360 degrees. |
104 |
|
|
*/ |
105 |
|
|
void setPhase(float phase) { |
106 |
|
|
if (phase < 0) phase = 0; |
107 |
|
|
if (phase > 360) phase = 360; |
108 |
|
|
phase /= 360.0f; |
109 |
|
|
const unsigned int intLimit = (unsigned int) -1; // all 0xFFFF... |
110 |
|
|
uiLevel = intLimit * phase; |
111 |
|
|
} |
112 |
iliev |
2223 |
|
113 |
|
|
protected: |
114 |
|
|
unsigned int uiLevel; |
115 |
|
|
int c; |
116 |
|
|
float offset; ///< only needed for signed range |
117 |
|
|
float normalizer; |
118 |
|
|
}; |
119 |
|
|
|
120 |
|
|
|
121 |
|
|
template<range_type_t RANGE> |
122 |
|
|
class SawUpLFO : public SawLFO<RANGE, true> { |
123 |
|
|
public: |
124 |
|
|
SawUpLFO(float Max) : SawLFO<RANGE, true>::SawLFO(Max) { } |
125 |
|
|
}; |
126 |
|
|
|
127 |
|
|
template<range_type_t RANGE> |
128 |
|
|
class SawDownLFO : public SawLFO<RANGE, false> { |
129 |
|
|
public: |
130 |
|
|
SawDownLFO(float Max) : SawLFO<RANGE, false>::SawLFO(Max) { } |
131 |
|
|
}; |
132 |
|
|
|
133 |
|
|
} // namespace LinuxSampler |
134 |
|
|
|
135 |
|
|
#endif // __LS_SAWLFO_H__ |