/[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 2225 - (show annotations) (download) (as text)
Tue Aug 2 13:44:57 2011 UTC (12 years, 7 months ago) by iliev
File MIME type: text/x-c++hdr
File size: 5654 byte(s)
* sfz engine: implemented opcodes lfoN_phase, lfoN_phase_onccX,
  lfoN_pitch, lfoN_pitch_onccX

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 protected:
112 unsigned int uiLevel;
113 unsigned int width;
114 int c;
115 float normalizer;
116 };
117
118 template<range_type_t RANGE>
119 class SquareLFO : public PulseLFO<RANGE, 500> {
120 public:
121 SquareLFO(float Max) : PulseLFO<RANGE, 500>::PulseLFO(Max) { }
122 };
123
124 } // namespace LinuxSampler
125
126 #endif // __LS_PULSELFO_H__

  ViewVC Help
Powered by ViewVC