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

Contents of /linuxsampler/trunk/src/engines/common/PulseLFO.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2223 - (show annotations) (download) (as text)
Fri Jul 29 13:39:58 2011 UTC (12 years, 8 months ago) by iliev
File MIME type: text/x-c++hdr
File size: 5195 byte(s)
* implemented sine LFO, pulse LFO and saw LFO
* sfz engine: implemented opcode lfoN_wave

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 protected:
100 unsigned int uiLevel;
101 unsigned int width;
102 int c;
103 float normalizer;
104 };
105
106 template<range_type_t RANGE>
107 class SquareLFO : public PulseLFO<RANGE, 500> {
108 public:
109 SquareLFO(float Max) : PulseLFO<RANGE, 500>::PulseLFO(Max) { }
110 };
111
112 } // namespace LinuxSampler
113
114 #endif // __LS_PULSELFO_H__

  ViewVC Help
Powered by ViewVC