1 |
/*************************************************************************** |
2 |
* * |
3 |
* LinuxSampler - modular, streaming capable sampler * |
4 |
* * |
5 |
* Copyright (C) 2011 Grigor Iliev * |
6 |
* * |
7 |
* This program is free software; you can redistribute it and/or modify * |
8 |
* it under the terms of the GNU General Public License as published by * |
9 |
* the Free Software Foundation; either version 2 of the License, or * |
10 |
* (at your option) any later version. * |
11 |
* * |
12 |
* This program is distributed in the hope that it will be useful, * |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
15 |
* GNU General Public License for more details. * |
16 |
* * |
17 |
* You should have received a copy of the GNU General Public License * |
18 |
* along with this program; if not, write to the Free Software * |
19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * |
20 |
* MA 02111-1307 USA * |
21 |
***************************************************************************/ |
22 |
|
23 |
#ifndef __LS_SFZSIGNALUNITRACK_H__ |
24 |
#define __LS_SFZSIGNALUNITRACK_H__ |
25 |
|
26 |
#include "../common/SignalUnitRack.h" |
27 |
#include "EG.h" |
28 |
#include "EGADSR.h" |
29 |
#include "../common/AbstractVoice.h" |
30 |
#include "../common/PulseLFO.h" |
31 |
#include "../common/SawLFO.h" |
32 |
#include "../common/SineLFO.h" |
33 |
|
34 |
namespace LinuxSampler { namespace sfz { |
35 |
const int MaxUnitCount = 1000; |
36 |
const int maxEgCount = 100; // Maximum number of v2 envelope generators |
37 |
const int maxLfoCount = 100; // Maximum number of v2 LFOs |
38 |
|
39 |
class Voice; |
40 |
class SfzSignalUnitRack; |
41 |
|
42 |
class SfzSignalUnit: public SignalUnit { |
43 |
public: |
44 |
Voice* pVoice; |
45 |
|
46 |
SfzSignalUnit(SfzSignalUnitRack* rack); |
47 |
SfzSignalUnit(const SfzSignalUnit& Unit): SignalUnit(Unit.pRack) { Copy(Unit); } |
48 |
|
49 |
void Copy(const SfzSignalUnit& Unit) { |
50 |
pVoice = Unit.pVoice; |
51 |
|
52 |
SignalUnit::Copy(Unit); |
53 |
} |
54 |
|
55 |
double GetSampleRate(); |
56 |
float GetInfluence(ArrayList< ::sfz::CC>& cc); |
57 |
}; |
58 |
|
59 |
|
60 |
class CCUnit: public CCSignalUnit { |
61 |
public: |
62 |
Voice* pVoice; |
63 |
|
64 |
CCUnit(SfzSignalUnitRack* rack, Listener* l = NULL); |
65 |
|
66 |
virtual void Trigger(); |
67 |
|
68 |
void SetCCs(::sfz::Array<int>& pCC); |
69 |
void SetCCs(ArrayList< ::sfz::CC>& cc); |
70 |
}; |
71 |
|
72 |
|
73 |
template <class T> |
74 |
class EGUnit: public SfzSignalUnit { |
75 |
public: |
76 |
::sfz::EG* pEGInfo; |
77 |
T EG; |
78 |
|
79 |
EGUnit(SfzSignalUnitRack* rack): SfzSignalUnit(rack), pEGInfo(NULL) { } |
80 |
EGUnit(const EGUnit& Unit): SfzSignalUnit(Unit) { Copy(Unit); } |
81 |
void operator=(const EGUnit& Unit) { Copy(Unit); } |
82 |
|
83 |
void Copy(const EGUnit& Unit) { |
84 |
pEGInfo = Unit.pEGInfo; |
85 |
|
86 |
SfzSignalUnit::Copy(Unit); |
87 |
} |
88 |
|
89 |
virtual bool Active() { return EG.active(); } |
90 |
virtual float GetLevel() { return DelayStage() ? 0 : EG.getLevel(); } |
91 |
|
92 |
virtual void EnterReleaseStage() { EG.update(EG::event_release, GetSampleRate()); } |
93 |
virtual void CancelRelease() { EG.update(EG::event_cancel_release, GetSampleRate()); } |
94 |
|
95 |
virtual void Increment() { |
96 |
if (DelayStage()) return; |
97 |
|
98 |
SfzSignalUnit::Increment(); |
99 |
if (!EG.active()) return; |
100 |
|
101 |
switch (EG.getSegmentType()) { |
102 |
case EG::segment_lin: |
103 |
EG.processLin(); |
104 |
break; |
105 |
case EG::segment_exp: |
106 |
EG.processExp(); |
107 |
break; |
108 |
case EG::segment_end: |
109 |
EG.getLevel(); |
110 |
break; // noop |
111 |
case EG::segment_pow: |
112 |
EG.processPow(); |
113 |
break; |
114 |
} |
115 |
|
116 |
if (EG.active()) { |
117 |
EG.increment(1); |
118 |
if (!EG.toStageEndLeft()) EG.update(EG::event_stage_end, GetSampleRate()); |
119 |
} |
120 |
} |
121 |
}; |
122 |
|
123 |
class EGv1Unit: public EGUnit<EGADSR> { |
124 |
public: |
125 |
int depth; |
126 |
EGv1Unit(SfzSignalUnitRack* rack): EGUnit<EGADSR>(rack), depth(0) { } |
127 |
}; |
128 |
|
129 |
class EGv2Unit: public EGUnit< ::LinuxSampler::sfz::EG> { |
130 |
protected: |
131 |
::sfz::EG egInfo; |
132 |
public: |
133 |
EGv2Unit(SfzSignalUnitRack* rack): EGUnit< ::LinuxSampler::sfz::EG>(rack) { } |
134 |
virtual void Trigger(); |
135 |
}; |
136 |
|
137 |
class PitchEGUnit: public EGv1Unit { |
138 |
public: |
139 |
PitchEGUnit(SfzSignalUnitRack* rack): EGv1Unit(rack) { } |
140 |
virtual void Trigger(); |
141 |
}; |
142 |
|
143 |
class FilEGUnit: public EGv1Unit { |
144 |
public: |
145 |
FilEGUnit(SfzSignalUnitRack* rack): EGv1Unit(rack) { } |
146 |
virtual void Trigger(); |
147 |
}; |
148 |
|
149 |
class AmpEGUnit: public EGv1Unit { |
150 |
public: |
151 |
AmpEGUnit(SfzSignalUnitRack* rack): EGv1Unit(rack) { } |
152 |
virtual void Trigger(); |
153 |
}; |
154 |
|
155 |
class AbstractLfo { |
156 |
public: |
157 |
virtual float Render() = 0; |
158 |
virtual void Update(const uint16_t& ExtControlValue) = 0; |
159 |
virtual void Trigger(float Frequency, start_level_t StartLevel, uint16_t InternalDepth, uint16_t ExtControlDepth, bool FlipPhase, unsigned int SampleRate) = 0; |
160 |
virtual void SetPhase(float phase) = 0; |
161 |
virtual void SetFrequency(float Frequency, unsigned int SampleRate) = 0; |
162 |
}; |
163 |
|
164 |
template <class T> |
165 |
class LfoBase: public AbstractLfo, public T { |
166 |
public: |
167 |
LfoBase(float Max): T(Max) { } |
168 |
virtual float Render() { return T::render(); } |
169 |
|
170 |
virtual void Update(const uint16_t& ExtControlValue) { T::update(ExtControlValue); } |
171 |
|
172 |
virtual void Trigger ( |
173 |
float Frequency, start_level_t StartLevel, uint16_t InternalDepth, |
174 |
uint16_t ExtControlDepth, bool FlipPhase, unsigned int SampleRate |
175 |
) { |
176 |
T::trigger(Frequency, StartLevel, InternalDepth, ExtControlDepth, FlipPhase, SampleRate); |
177 |
} |
178 |
|
179 |
virtual void SetPhase(float phase) { T::setPhase(phase); } |
180 |
|
181 |
virtual void SetFrequency(float Frequency, unsigned int SampleRate) { |
182 |
T::setFrequency(Frequency, SampleRate); |
183 |
} |
184 |
}; |
185 |
|
186 |
class LFOUnit; |
187 |
|
188 |
class FadeEGUnit: public EGUnit<EGADSR> { |
189 |
public: |
190 |
FadeEGUnit(SfzSignalUnitRack* rack): EGUnit<EGADSR>(rack) { } |
191 |
virtual void Trigger() { } |
192 |
virtual void EnterReleaseStage() { } |
193 |
virtual void CancelRelease() { } |
194 |
|
195 |
friend class LFOUnit; |
196 |
}; |
197 |
|
198 |
class LFOUnit: public SfzSignalUnit, public CCSignalUnit::Listener { |
199 |
public: |
200 |
::sfz::LFO* pLfoInfo; |
201 |
AbstractLfo* pLFO; |
202 |
FadeEGUnit suFadeEG; |
203 |
CCUnit suFreqOnCC; |
204 |
|
205 |
LFOUnit(SfzSignalUnitRack* rack); |
206 |
LFOUnit(const LFOUnit& Unit); |
207 |
void operator=(const LFOUnit& Unit) { Copy(Unit); } |
208 |
|
209 |
void Copy(const LFOUnit& Unit) { |
210 |
pLfoInfo = Unit.pLfoInfo; |
211 |
suFadeEG = Unit.suFadeEG; |
212 |
|
213 |
SfzSignalUnit::Copy(Unit); |
214 |
} |
215 |
|
216 |
virtual bool Active() { return pLfoInfo->freq > 0; } |
217 |
virtual void Trigger(); |
218 |
virtual void Increment(); |
219 |
virtual float GetLevel() { return Level; } |
220 |
virtual void ValueChanged(CCSignalUnit* pUnit); |
221 |
}; |
222 |
|
223 |
class LFOv1Unit: public LFOUnit { |
224 |
public: |
225 |
::sfz::LFO lfoInfo; |
226 |
LfoBase<LFOSigned> lfo; |
227 |
|
228 |
LFOv1Unit(SfzSignalUnitRack* rack): LFOUnit(rack), lfo(1200.0f) { |
229 |
pLfoInfo = &lfoInfo; pLFO = &lfo; |
230 |
} |
231 |
|
232 |
virtual void Trigger(); |
233 |
}; |
234 |
|
235 |
class LFOv2Unit: public LFOUnit { |
236 |
protected: |
237 |
FixedArray<AbstractLfo*> lfos; |
238 |
LfoBase<LFOSigned> lfo0; // triangle |
239 |
LfoBase<SineLFO<range_signed> > lfo1; // sine |
240 |
LfoBase<PulseLFO<range_unsigned, 750> > lfo2; // pulse 75% |
241 |
LfoBase<SquareLFO<range_signed> > lfo3; // square |
242 |
LfoBase<PulseLFO<range_unsigned, 250> > lfo4; // pulse 25% |
243 |
LfoBase<PulseLFO<range_unsigned, 125> > lfo5; // pulse 12,5% |
244 |
LfoBase<SawLFO<range_unsigned, true> > lfo6; // saw up |
245 |
LfoBase<SawLFO<range_unsigned, false> > lfo7; // saw down |
246 |
|
247 |
|
248 |
public: |
249 |
CCUnit suPitchOnCC; |
250 |
|
251 |
LFOv2Unit(SfzSignalUnitRack* rack); |
252 |
|
253 |
virtual void Trigger(); |
254 |
}; |
255 |
|
256 |
class AmpLFOUnit: public LFOv1Unit { |
257 |
public: |
258 |
AmpLFOUnit(SfzSignalUnitRack* rack): LFOv1Unit(rack) { } |
259 |
|
260 |
virtual void Trigger(); |
261 |
}; |
262 |
|
263 |
class PitchLFOUnit: public LFOv1Unit { |
264 |
public: |
265 |
CCUnit suDepthCC; |
266 |
|
267 |
PitchLFOUnit(SfzSignalUnitRack* rack): LFOv1Unit(rack), suDepthCC(rack) { } |
268 |
|
269 |
virtual void Trigger(); |
270 |
}; |
271 |
|
272 |
class FilLFOUnit: public LFOv1Unit { |
273 |
public: |
274 |
FilLFOUnit(SfzSignalUnitRack* rack): LFOv1Unit(rack) { } |
275 |
|
276 |
virtual void Trigger(); |
277 |
}; |
278 |
|
279 |
|
280 |
|
281 |
class EndpointUnit: public EndpointSignalUnit { |
282 |
public: |
283 |
Voice* pVoice; |
284 |
|
285 |
EndpointUnit(SfzSignalUnitRack* rack); |
286 |
|
287 |
virtual void Trigger(); |
288 |
|
289 |
/** The endpoint should be active until the volume EG is active. */ |
290 |
virtual bool Active(); |
291 |
|
292 |
virtual float GetVolume(); |
293 |
virtual float GetFilterCutoff(); |
294 |
virtual float GetPitch(); |
295 |
virtual float GetResonance(); |
296 |
virtual float GetPan(); |
297 |
|
298 |
SfzSignalUnitRack* const GetRack(); |
299 |
|
300 |
virtual float CalculateResonance(float res) { |
301 |
return GetResonance() + res; |
302 |
} |
303 |
}; |
304 |
|
305 |
|
306 |
class SfzSignalUnitRack : public SignalUnitRack { |
307 |
private: |
308 |
EndpointUnit suEndpoint; |
309 |
AmpEGUnit suVolEG; |
310 |
FilEGUnit suFilEG; |
311 |
PitchEGUnit suPitchEG; |
312 |
|
313 |
AmpLFOUnit suAmpLFO; |
314 |
PitchLFOUnit suPitchLFO; |
315 |
FilLFOUnit suFilLFO; |
316 |
|
317 |
FixedArray<EGv2Unit*> EGs; |
318 |
|
319 |
// used for optimization - contains only the ones that are modulating volume |
320 |
FixedArray<EGv2Unit*> volEGs; |
321 |
|
322 |
// used for optimization - contains only the ones that are modulating pitch |
323 |
FixedArray<EGv2Unit*> pitchEGs; |
324 |
|
325 |
|
326 |
FixedArray<LFOv2Unit*> LFOs; |
327 |
|
328 |
// used for optimization - contains only the ones that are modulating pitch |
329 |
FixedArray<LFOv2Unit*> pitchLFOs; |
330 |
|
331 |
// used for optimization - contains only the ones that are modulating filter cutoff |
332 |
FixedArray<LFOv2Unit*> filLFOs; |
333 |
|
334 |
// used for optimization - contains only the ones that are modulating resonance |
335 |
FixedArray<LFOv2Unit*> resLFOs; |
336 |
|
337 |
// used for optimization - contains only the ones that are modulating pan |
338 |
FixedArray<LFOv2Unit*> panLFOs; |
339 |
|
340 |
|
341 |
public: |
342 |
Voice* const pVoice; |
343 |
|
344 |
/** |
345 |
* @param Voice The voice to which this rack belongs. |
346 |
*/ |
347 |
SfzSignalUnitRack(Voice* voice); |
348 |
~SfzSignalUnitRack(); |
349 |
|
350 |
virtual EndpointSignalUnit* GetEndpointUnit(); |
351 |
|
352 |
virtual void Trigger(); |
353 |
virtual void EnterFadeOutStage(); |
354 |
|
355 |
friend class EndpointUnit; |
356 |
}; |
357 |
}} // namespace LinuxSampler::sfz |
358 |
|
359 |
#endif /* __LS_SFZSIGNALUNITRACK_H__ */ |
360 |
|