1 |
/*************************************************************************** |
2 |
* * |
3 |
* LinuxSampler - modular, streaming capable sampler * |
4 |
* * |
5 |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
6 |
* Copyright (C) 2005, 2006 Christian Schoenebeck * |
7 |
* * |
8 |
* This library is free software; you can redistribute it and/or modify * |
9 |
* it under the terms of the GNU General Public License as published by * |
10 |
* the Free Software Foundation; either version 2 of the License, or * |
11 |
* (at your option) any later version. * |
12 |
* * |
13 |
* This library is distributed in the hope that it will be useful, * |
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
16 |
* GNU General Public License for more details. * |
17 |
* * |
18 |
* You should have received a copy of the GNU General Public License * |
19 |
* along with this library; if not, write to the Free Software * |
20 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * |
21 |
* MA 02111-1307 USA * |
22 |
***************************************************************************/ |
23 |
|
24 |
#ifndef __LS_GIG_EGDECAY_H__ |
25 |
#define __LS_GIG_EGDECAY_H__ |
26 |
|
27 |
#include "../../common/global.h" |
28 |
#include "../../common/RTMath.h" |
29 |
|
30 |
namespace LinuxSampler { namespace gig { |
31 |
|
32 |
/** @brief Decay Envelope Generator (linear) |
33 |
* |
34 |
* Simple Envelope Generator with only one stage: 'Decay' which is a |
35 |
* linear segment. The initial envelope level (given by 'Depth') will |
36 |
* raise / drop in 'DecayTime' seconds to a level of exactly 1.0. If |
37 |
* the initial level ('Depth') is already 1.0, nothing happens. |
38 |
*/ |
39 |
class EGDecay { |
40 |
public: |
41 |
EGDecay(); |
42 |
|
43 |
/** |
44 |
* Will be called by the voice when the key / voice was |
45 |
* triggered and initialize the envelope generator. |
46 |
* |
47 |
* @param Depth - initial level of the envelope |
48 |
* @param DecayTime - decay time of the envelope (0.000 - 10.000s) |
49 |
* @param SampleRate - sample rate of used audio driver |
50 |
*/ |
51 |
void trigger(float Depth, float DecayTime, unsigned int SampleRate); //FIXME: we should better use 'float' for SampleRate |
52 |
|
53 |
/** |
54 |
* Returns true if envelope is still in stage 'Decay', returns |
55 |
* false if end of envelope is reached. |
56 |
*/ |
57 |
inline bool active() { |
58 |
return (bool) Coeff; |
59 |
} |
60 |
|
61 |
/** |
62 |
* Advance envelope by \a SamplePoints steps. |
63 |
*/ |
64 |
inline void increment(const int SamplePoints) { |
65 |
StepsLeft = RTMath::Max(0, StepsLeft - SamplePoints); |
66 |
} |
67 |
|
68 |
/** |
69 |
* Returns amount of steps until end of envelope is reached. |
70 |
*/ |
71 |
inline int toEndLeft() { |
72 |
return StepsLeft; |
73 |
} |
74 |
|
75 |
/** |
76 |
* Should be called once the end of the envelope is reached. It |
77 |
* will neutralize the envelope coefficient to not alter the |
78 |
* envelope anymore and will set the output level to final level |
79 |
* of exactly 1.0f. So after this call, render() can still |
80 |
* safely be called from the sampler's main synthesis loop. |
81 |
*/ |
82 |
inline void update() { |
83 |
Level = 1.0f; |
84 |
Coeff = 0.0f; |
85 |
} |
86 |
|
87 |
/** |
88 |
* Calculates exactly one level of the envelope. |
89 |
* |
90 |
* @returns next envelope level |
91 |
*/ |
92 |
inline float render() { |
93 |
return (Level += Coeff); |
94 |
} |
95 |
|
96 |
/** |
97 |
* Returns the level which this envelope will have in |
98 |
* \a SamplePoints steps. It will not alter anything. |
99 |
*/ |
100 |
inline float level(const int SamplePoints) const { |
101 |
return Level + RTMath::Min(SamplePoints, StepsLeft) * Coeff; |
102 |
} |
103 |
|
104 |
private: |
105 |
float Level; ///< current EG output level |
106 |
float Coeff; ///< linear coefficient for changing the output level in time |
107 |
int StepsLeft; ///< how many steps left until end is reached |
108 |
}; |
109 |
|
110 |
}} // namespace LinuxSampler::gig |
111 |
|
112 |
#endif // __LS_GIG_EGDECAY_H__ |