3 |
* LinuxSampler - modular, streaming capable sampler * |
* LinuxSampler - modular, streaming capable sampler * |
4 |
* * |
* * |
5 |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
6 |
|
* Copyright (C) 2005 Christian Schoenebeck * |
7 |
* * |
* * |
8 |
* This program is free software; you can redistribute it and/or modify * |
* This program is free software; you can redistribute it and/or modify * |
9 |
* it under the terms of the GNU General Public License as published by * |
* it under the terms of the GNU General Public License as published by * |
24 |
#ifndef __LS_GIG_EGADSR_H__ |
#ifndef __LS_GIG_EGADSR_H__ |
25 |
#define __LS_GIG_EGADSR_H__ |
#define __LS_GIG_EGADSR_H__ |
26 |
|
|
|
#include <math.h> |
|
|
|
|
27 |
#include "../../common/global.h" |
#include "../../common/global.h" |
28 |
#include "../../common/RTMath.h" |
#include "../../common/RTMath.h" |
|
#include "../../common/RTELMemoryPool.h" |
|
|
#include "../common/Event.h" |
|
|
#include "Manipulator.h" |
|
|
#include "Engine.h" |
|
|
|
|
|
#define EG_ENVELOPE_LIMIT 0.001 |
|
|
#define EG_MIN_RELEASE_TIME 0.0001 |
|
29 |
|
|
30 |
namespace LinuxSampler { namespace gig { |
namespace LinuxSampler { namespace gig { |
31 |
|
|
38 |
*/ |
*/ |
39 |
class EGADSR { |
class EGADSR { |
40 |
public: |
public: |
41 |
|
|
42 |
|
/** |
43 |
|
* Used to define what kind of segment the envelope currently is at. |
44 |
|
*/ |
45 |
|
enum segment_t { |
46 |
|
segment_end = 0, ///< final end state of envelope reached |
47 |
|
segment_lin = 1, ///< envelope is currently at a linear segment |
48 |
|
segment_exp = 2 ///< envelope is currently at a exponental segment |
49 |
|
}; |
50 |
|
|
51 |
|
/** |
52 |
|
* Used to inform the EG about an event. |
53 |
|
*/ |
54 |
|
enum event_t { |
55 |
|
event_stage_end, |
56 |
|
event_release, |
57 |
|
event_cancel_release |
58 |
|
}; |
59 |
|
|
60 |
|
/** |
61 |
|
* Constructor |
62 |
|
*/ |
63 |
|
EGADSR(); |
64 |
|
|
65 |
|
/** |
66 |
|
* Change fade out time. |
67 |
|
*/ |
68 |
|
void CalculateFadeOutCoeff(float FadeOutTime, float SampleRate); |
69 |
|
|
70 |
|
/** |
71 |
|
* Will be called by the voice when the key / voice was triggered. |
72 |
|
* |
73 |
|
* @param PreAttack - Preattack value for the envelope |
74 |
|
* (0 - 1000 permille) |
75 |
|
* @param AttackTime - Attack time for the envelope |
76 |
|
* (0.000 - 60.000s) |
77 |
|
* @param HoldAttack - if true, Decay1 will be postponed until the |
78 |
|
* sample reached the sample loop start. |
79 |
|
* @param LoopStart - sample position where sample loop starts |
80 |
|
* (if any) |
81 |
|
* @param Decay1Time - Decay1 time of the sample amplitude EG |
82 |
|
* (0.000 - 60.000s) |
83 |
|
* @param Decay2Time - only if !InfiniteSustain: 2nd decay stage |
84 |
|
* time of the sample amplitude EG |
85 |
|
* (0.000 - 60.000s) |
86 |
|
* @param InfiniteSustain - if true, instead of going into Decay2 |
87 |
|
* stage, Decay1 level will be hold until note |
88 |
|
* will be released |
89 |
|
* @param SustainLevel - Sustain level of the sample amplitude EG |
90 |
|
* (0 - 1000 permille) |
91 |
|
* @param ReleaseTIme - Release time for the envelope |
92 |
|
* (0.000 - 60.000s) |
93 |
|
* @param Volume - volume the sample will be played at |
94 |
|
* (0.0 - 1.0) - used when calculating the |
95 |
|
* exponential curve parameters. |
96 |
|
* @param SampleRate - sample rate of used audio output driver |
97 |
|
*/ |
98 |
|
void trigger(uint PreAttack, float AttackTime, bool HoldAttack, long LoopStart, float Decay1Time, double Decay2Time, bool InfiniteSustain, uint SustainLevel, float ReleaseTime, float Volume, uint SampleRate); //FIXME: we should better use 'float' for SampleRate |
99 |
|
|
100 |
|
/** |
101 |
|
* Returns true in case envelope hasn't reached its final end state yet. |
102 |
|
*/ |
103 |
|
inline bool active() { |
104 |
|
return (bool) Segment; |
105 |
|
} |
106 |
|
|
107 |
|
/** |
108 |
|
* Returns what kind of segment the envelope currently is at. |
109 |
|
*/ |
110 |
|
inline segment_t getSegmentType() { |
111 |
|
return Segment; |
112 |
|
} |
113 |
|
|
114 |
|
/** |
115 |
|
* Advance envelope by \a SamplePoints steps. |
116 |
|
*/ |
117 |
|
inline void increment(int SamplePoints) { |
118 |
|
StepsLeft = RTMath::Max(0, StepsLeft - SamplePoints); |
119 |
|
} |
120 |
|
|
121 |
|
/** |
122 |
|
* Returns amount of steps until the end of current envelope stage. |
123 |
|
*/ |
124 |
|
inline int toStageEndLeft() { |
125 |
|
return StepsLeft; |
126 |
|
} |
127 |
|
|
128 |
|
/** |
129 |
|
* Should be called to inform the EG about an external event and |
130 |
|
* also whenever an envelope stage is completed. This will handle |
131 |
|
* the envelope's transition to the respective next stage. |
132 |
|
* |
133 |
|
* @param Event - what happened |
134 |
|
* @param SamplePos - current sample playback position |
135 |
|
* @param CurrentPitch - current frequency alternation quotient |
136 |
|
*/ |
137 |
|
void update(event_t Event, double SamplePos, float CurrentPitch, uint SampleRate); |
138 |
|
|
139 |
|
/** |
140 |
|
* Calculates exactly one, the next sample point of EG |
141 |
|
* (linear segment). Call this if envelope is currently in a linear |
142 |
|
* segment. |
143 |
|
* |
144 |
|
* @returns next envelope level |
145 |
|
*/ |
146 |
|
inline float processLin() { |
147 |
|
return (Level += Coeff); |
148 |
|
} |
149 |
|
|
150 |
|
/** |
151 |
|
* Calculates exactly one, the next sample point of EG |
152 |
|
* (exponential segment). Call this if envelope is currently in an |
153 |
|
* exponential segment. |
154 |
|
* |
155 |
|
* @returns next envelope level |
156 |
|
*/ |
157 |
|
inline float processExp() { |
158 |
|
return (Level = Level * Coeff + Offset); |
159 |
|
} |
160 |
|
|
161 |
|
/** |
162 |
|
* Returns current envelope level without modifying anything. This |
163 |
|
* might be needed once the envelope reached its final end state, |
164 |
|
* because calling processLin() or processExp() at this point will |
165 |
|
* result in undesired behavior. |
166 |
|
*/ |
167 |
|
inline float getLevel() { |
168 |
|
return Level; |
169 |
|
} |
170 |
|
|
171 |
|
private: |
172 |
|
|
173 |
enum stage_t { |
enum stage_t { |
174 |
stage_attack, |
stage_attack, |
175 |
stage_attack_hold, |
stage_attack_hold, |
176 |
stage_decay1, |
stage_decay1_part1, |
177 |
|
stage_decay1_part2, |
178 |
stage_decay2, |
stage_decay2, |
179 |
stage_sustain, |
stage_sustain, |
180 |
stage_release, |
stage_release_part1, |
181 |
|
stage_release_part2, |
182 |
stage_fadeout, |
stage_fadeout, |
183 |
stage_end |
stage_end |
184 |
}; |
}; |
185 |
|
|
186 |
EGADSR(gig::Engine* pEngine, Event::destination_t ModulationDestination); |
float Level; |
187 |
void Process(uint TotalSamples, RTList<Event>* pEvents, RTList<Event>::Iterator itTriggerEvent, double SamplePos, double CurrentPitch, RTList<Event>::Iterator itKillEvent = RTList<Event>::Iterator()); |
float Coeff; |
188 |
void Trigger(uint PreAttack, double AttackTime, bool HoldAttack, long LoopStart, double Decay1Time, double Decay2Time, bool InfiniteSustain, uint SustainLevel, double ReleaseTime, uint Delay); |
float Offset; |
189 |
inline EGADSR::stage_t GetStage() { return Stage; } |
int StepsLeft; |
190 |
protected: |
segment_t Segment; |
191 |
gig::Engine* pEngine; |
stage_t Stage; |
192 |
Event::destination_t ModulationDestination; |
event_t PostponedEvent; ///< only used in Attack stage to postpone transition events until attack time is reached |
193 |
uint TriggerDelay; ///< number of sample points triggering should be delayed |
bool HoldAttack; |
194 |
float Level; |
bool InfiniteSustain; |
195 |
stage_t Stage; |
long LoopStart; |
196 |
float AttackCoeff; |
float Decay1Time; |
197 |
long AttackStepsLeft; ///< number of sample points til end of attack stage |
float Decay1Level2; |
198 |
bool HoldAttack; |
float Decay1Slope; |
199 |
long LoopStart; |
float Decay2Time; |
200 |
float Decay1Coeff; |
float SustainLevel; |
201 |
long Decay1StepsLeft; ///< number of sample points in Decay1 stage |
float ReleaseCoeff; |
202 |
float Decay2Coeff; |
float ReleaseCoeff2; |
203 |
bool InfiniteSustain; |
float ReleaseCoeff3; |
204 |
float SustainLevel; |
float ReleaseLevel2; |
205 |
float ReleaseCoeff; |
float ReleaseSlope; |
206 |
long ReleaseStepsLeft; ///< number of sample points til end of release stage |
float invVolume; |
207 |
bool ReleasePostponed; ///< If a "release" event occured in the previous audio fragment, but wasn't processed yet. |
float ExpOffset; |
208 |
const static float FadeOutCoeff; |
float FadeOutCoeff; ///< very fast ramp down for e.g. voice stealing |
209 |
private: |
|
210 |
static float CalculateFadeOutCoeff(); |
void enterAttackStage(const uint PreAttack, const float AttackTime, const uint SampleRate, const double SamplePos, const float CurrentPitch); |
211 |
|
void enterAttackHoldStage(const double SamplePos, const float CurrentPitch); |
212 |
|
void enterDecay1Part1Stage(const uint SampleRate); |
213 |
|
void enterDecay1Part2Stage(const uint SampleRate); |
214 |
|
void enterDecay2Stage(const uint SampleRate); |
215 |
|
void enterSustainStage(); |
216 |
|
void enterReleasePart1Stage(); |
217 |
|
void enterReleasePart2Stage(); |
218 |
|
void enterFadeOutStage(); |
219 |
|
void enterEndStage(); |
220 |
}; |
}; |
221 |
|
|
222 |
}} // namespace LinuxSampler::gig |
}} // namespace LinuxSampler::gig |