1 |
/*************************************************************************** |
2 |
* * |
3 |
* Copyright (C) 2005 Christian Schoenebeck * |
4 |
* * |
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 |
inline void update(const uint16_t& ExtControlValue) { |
66 |
const unsigned int intLimit = (unsigned int) -1; // all 0xFFFF... |
67 |
const float max = this->InternalDepth + ExtControlValue * this->ExtControlDepthCoeff; |
68 |
if (RANGE == range_unsigned) { |
69 |
normalizer = max / (float) intLimit; |
70 |
} else { // signed range |
71 |
normalizer = max / (float) intLimit * 4.0f; |
72 |
offset = -max; |
73 |
} |
74 |
} |
75 |
|
76 |
/** |
77 |
* Will be called by the voice when the key / voice was triggered. |
78 |
* |
79 |
* @param Frequency - frequency of the oscillator in Hz |
80 |
* @param StartLevel - on which level the wave should start |
81 |
* @param InternalDepth - firm, internal oscillator amplitude |
82 |
* @param ExtControlDepth - defines how strong the external MIDI |
83 |
* controller has influence on the |
84 |
* oscillator amplitude |
85 |
* @param FlipPhase - inverts the oscillator wave against |
86 |
* a horizontal axis |
87 |
* @param SampleRate - current sample rate of the engines |
88 |
* audio output signal |
89 |
*/ |
90 |
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 |
if (RANGE == range_unsigned) { |
94 |
this->InternalDepth *= 2.0f; |
95 |
this->ExtControlDepthCoeff *= 2.0f; |
96 |
} |
97 |
|
98 |
const unsigned int intLimit = (unsigned int) -1; // all 0xFFFF... |
99 |
const float r = Frequency / (float) SampleRate; // frequency alteration quotient |
100 |
c = (int) (intLimit * r); |
101 |
|
102 |
switch (StartLevel) { |
103 |
case start_level_max: |
104 |
iLevel = (FlipPhase) ? 0 : intLimit >> 1; |
105 |
break; |
106 |
case start_level_mid: |
107 |
if (FlipPhase) c = -c; // wave should go down |
108 |
iLevel = intLimit >> 2; |
109 |
break; |
110 |
case start_level_min: |
111 |
iLevel = (FlipPhase) ? intLimit >> 1 : 0; |
112 |
break; |
113 |
} |
114 |
} |
115 |
|
116 |
protected: |
117 |
int iLevel; |
118 |
int c; |
119 |
float offset; ///< only needed for signed range |
120 |
float normalizer; |
121 |
}; |
122 |
|
123 |
} // namespace LinuxSampler |
124 |
|
125 |
#endif // __LS_LFOTRIANGLEINTMATH_H__ |