1 |
/*************************************************************************** |
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_PULSELFO_H__ |
23 |
#define __LS_PULSELFO_H__ |
24 |
|
25 |
#include "LFOBase.h" |
26 |
|
27 |
namespace LinuxSampler { |
28 |
|
29 |
/** @brief Pulse LFO (int math implementation) |
30 |
* |
31 |
* This is a pulse Low Frequency Oscillator which uses pure integer |
32 |
* math (without branches) to synthesize the pulse wave. |
33 |
*/ |
34 |
template<range_type_t RANGE, int WIDTH /* in permilles */> |
35 |
class PulseLFO : public LFOBase<RANGE> { |
36 |
public: |
37 |
|
38 |
/** |
39 |
* Constructor |
40 |
* |
41 |
* @param Max - maximum value of the output levels |
42 |
*/ |
43 |
PulseLFO(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 |
uiLevel += c; |
53 |
if (RANGE == range_unsigned) |
54 |
return uiLevel <= width ? normalizer : 0; |
55 |
else /* signed range */ |
56 |
return uiLevel <= width ? normalizer : -normalizer; |
57 |
} |
58 |
|
59 |
/** |
60 |
* Update LFO depth with a new external controller value. |
61 |
* |
62 |
* @param ExtControlValue - new external controller value |
63 |
*/ |
64 |
inline void update(const uint16_t& ExtControlValue) { |
65 |
const float max = this->InternalDepth + ExtControlValue * this->ExtControlDepthCoeff; |
66 |
normalizer = max; |
67 |
} |
68 |
|
69 |
/** |
70 |
* Will be called by the voice when the key / voice was triggered. |
71 |
* |
72 |
* @param Frequency - frequency of the oscillator in Hz |
73 |
* @param InternalDepth - firm, internal oscillator amplitude |
74 |
* @param ExtControlDepth - defines how strong the external MIDI |
75 |
* controller has influence on the |
76 |
* oscillator amplitude |
77 |
* @param FlipPhase - inverts the oscillator wave against |
78 |
* a horizontal axis |
79 |
* @param SampleRate - current sample rate of the engines |
80 |
* audio output signal |
81 |
* @param PulseWidth - the pulse width in percents |
82 |
*/ |
83 |
void trigger(float Frequency, uint16_t InternalDepth, uint16_t ExtControlDepth, float PulseWidth, unsigned int SampleRate) { |
84 |
this->InternalDepth = (InternalDepth / 1200.0f) * this->Max; |
85 |
this->ExtControlDepthCoeff = (((float) ExtControlDepth / 1200.0f) / 127.0f) * this->Max; |
86 |
|
87 |
const unsigned int intLimit = (unsigned int) -1; // all 0xFFFF... |
88 |
const float r = Frequency / (float) SampleRate; // frequency alteration quotient |
89 |
c = (int) (intLimit * r); |
90 |
width = (PulseWidth / 100.0) * intLimit; |
91 |
|
92 |
uiLevel = 0; |
93 |
} |
94 |
|
95 |
virtual void trigger(float Frequency, start_level_t StartLevel, uint16_t InternalDepth, uint16_t ExtControlDepth, bool FlipPhase, unsigned int SampleRate) { |
96 |
trigger(Frequency, InternalDepth, ExtControlDepth, WIDTH / 10.0f, SampleRate); |
97 |
} |
98 |
|
99 |
/** |
100 |
* Should be invoked after the LFO is triggered. |
101 |
* @param phase From 0 to 360 degrees. |
102 |
*/ |
103 |
void setPhase(float phase) { |
104 |
if (phase < 0) phase = 0; |
105 |
if (phase > 360) phase = 360; |
106 |
phase /= 360.0f; |
107 |
const unsigned int intLimit = (unsigned int) -1; // all 0xFFFF... |
108 |
uiLevel = intLimit * phase; |
109 |
} |
110 |
|
111 |
void setFrequency(float Frequency, unsigned int SampleRate) { |
112 |
const unsigned int intLimit = (unsigned int) -1; // all 0xFFFF... |
113 |
float r = Frequency / (float) SampleRate; // frequency alteration quotient |
114 |
c = (int) (intLimit * r); |
115 |
} |
116 |
|
117 |
protected: |
118 |
unsigned int uiLevel; |
119 |
unsigned int width; |
120 |
int c; |
121 |
float normalizer; |
122 |
}; |
123 |
|
124 |
template<range_type_t RANGE> |
125 |
class SquareLFO : public PulseLFO<RANGE, 500> { |
126 |
public: |
127 |
SquareLFO(float Max) : PulseLFO<RANGE, 500>::PulseLFO(Max) { } |
128 |
}; |
129 |
|
130 |
} // namespace LinuxSampler |
131 |
|
132 |
#endif // __LS_PULSELFO_H__ |