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 = 200; |
36 |
const int maxEgCount = 30; // Maximum number of v2 envelope generators |
37 |
const int maxLfoCount = 30; // 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(::sfz::Array<float>& pCC); |
70 |
void SetCCs(ArrayList< ::sfz::CC>& cc); |
71 |
|
72 |
virtual void AddSmoothCC(uint8_t Controller, float Influence, short int Curve, float Smooth, float Step); |
73 |
|
74 |
int GetCurveCount(); |
75 |
::sfz::Curve* GetCurve(int idx); |
76 |
|
77 |
double GetSampleRate(); |
78 |
}; |
79 |
|
80 |
class CurveCCUnit: public CCUnit { |
81 |
public: |
82 |
CurveCCUnit(SfzSignalUnitRack* rack, Listener* l = NULL): CCUnit(rack, l) { } |
83 |
|
84 |
virtual float Normalize(uint8_t val, short int curve = -1) { |
85 |
if (curve == -1) return val / 127.0f; |
86 |
return GetCurve(curve)->v[val]; |
87 |
} |
88 |
}; |
89 |
|
90 |
|
91 |
|
92 |
class SmoothCCUnit: public CurveCCUnit { |
93 |
protected: |
94 |
RTList<Smoother>* pSmoothers; |
95 |
public: |
96 |
SmoothCCUnit(SfzSignalUnitRack* rack, Listener* l = NULL): CurveCCUnit(rack, l), pSmoothers(NULL) { } |
97 |
virtual ~SmoothCCUnit(); |
98 |
|
99 |
virtual void AddSmoothCC(uint8_t Controller, float Influence, short int Curve, float Smooth, float Step); |
100 |
virtual void RemoveAllCCs() { CurveCCUnit::RemoveAllCCs(); pSmoothers->clear(); } |
101 |
virtual void InitCCList(Pool<CC>* pCCPool, Pool<Smoother>* pSmootherPool); |
102 |
|
103 |
void InitSmoothers(Pool<Smoother>* pSmootherPool); |
104 |
}; |
105 |
|
106 |
|
107 |
class XFInCCUnit: public CCUnit { |
108 |
public: |
109 |
XFInCCUnit(SfzSignalUnitRack* rack, Listener* l = NULL): CCUnit(rack, l) { } |
110 |
|
111 |
virtual bool Active() { return !pCtrls->isEmpty(); } |
112 |
virtual void Calculate(); |
113 |
virtual void SetCrossFadeCCs(::sfz::Array<int>& loCCs, ::sfz::Array<int>& hiCCs); |
114 |
}; |
115 |
|
116 |
|
117 |
class XFOutCCUnit: public XFInCCUnit { |
118 |
public: |
119 |
XFOutCCUnit(SfzSignalUnitRack* rack, Listener* l = NULL): XFInCCUnit(rack, l) { } |
120 |
|
121 |
virtual void Calculate(); |
122 |
}; |
123 |
|
124 |
|
125 |
template <class T> |
126 |
class EGUnit: public SfzSignalUnit { |
127 |
public: |
128 |
::sfz::EG* pEGInfo; |
129 |
T EG; |
130 |
|
131 |
EGUnit(SfzSignalUnitRack* rack): SfzSignalUnit(rack), pEGInfo(NULL) { } |
132 |
EGUnit(const EGUnit& Unit): SfzSignalUnit(Unit) { Copy(Unit); } |
133 |
void operator=(const EGUnit& Unit) { Copy(Unit); } |
134 |
|
135 |
void Copy(const EGUnit& Unit) { |
136 |
pEGInfo = Unit.pEGInfo; |
137 |
|
138 |
SfzSignalUnit::Copy(Unit); |
139 |
} |
140 |
|
141 |
virtual bool Active() { return EG.active(); } |
142 |
virtual float GetLevel() { return DelayStage() ? 0 : EG.getLevel(); } |
143 |
|
144 |
virtual void EnterReleaseStage() { EG.update(EG::event_release, GetSampleRate()); } |
145 |
virtual void CancelRelease() { EG.update(EG::event_cancel_release, GetSampleRate()); } |
146 |
|
147 |
virtual void Increment() { |
148 |
if (DelayStage()) return; |
149 |
|
150 |
SfzSignalUnit::Increment(); |
151 |
if (!EG.active()) return; |
152 |
|
153 |
switch (EG.getSegmentType()) { |
154 |
case EG::segment_lin: |
155 |
EG.processLin(); |
156 |
break; |
157 |
case EG::segment_exp: |
158 |
EG.processExp(); |
159 |
break; |
160 |
case EG::segment_end: |
161 |
EG.getLevel(); |
162 |
break; // noop |
163 |
case EG::segment_pow: |
164 |
EG.processPow(); |
165 |
break; |
166 |
} |
167 |
|
168 |
if (EG.active()) { |
169 |
EG.increment(1); |
170 |
if (!EG.toStageEndLeft()) EG.update(EG::event_stage_end, GetSampleRate()); |
171 |
} |
172 |
} |
173 |
}; |
174 |
|
175 |
class EGv1Unit: public EGUnit<EGADSR> { |
176 |
public: |
177 |
int depth; |
178 |
EGv1Unit(SfzSignalUnitRack* rack): EGUnit<EGADSR>(rack), depth(0) { } |
179 |
}; |
180 |
|
181 |
class EGv2Unit: public EGUnit< ::LinuxSampler::sfz::EG> { |
182 |
protected: |
183 |
::sfz::EG egInfo; |
184 |
public: |
185 |
CCUnit suAmpOnCC; |
186 |
CCUnit suVolOnCC; |
187 |
CCUnit suPitchOnCC; |
188 |
CCUnit suCutoffOnCC; |
189 |
CCUnit suResOnCC; |
190 |
CurveCCUnit suPanOnCC; |
191 |
|
192 |
EGv2Unit(SfzSignalUnitRack* rack); |
193 |
virtual void Trigger(); |
194 |
}; |
195 |
|
196 |
class PitchEGUnit: public EGv1Unit { |
197 |
public: |
198 |
PitchEGUnit(SfzSignalUnitRack* rack): EGv1Unit(rack) { } |
199 |
virtual void Trigger(); |
200 |
}; |
201 |
|
202 |
class FilEGUnit: public EGv1Unit { |
203 |
public: |
204 |
FilEGUnit(SfzSignalUnitRack* rack): EGv1Unit(rack) { } |
205 |
virtual void Trigger(); |
206 |
}; |
207 |
|
208 |
class AmpEGUnit: public EGv1Unit { |
209 |
public: |
210 |
AmpEGUnit(SfzSignalUnitRack* rack): EGv1Unit(rack) { } |
211 |
virtual void Trigger(); |
212 |
}; |
213 |
|
214 |
class AbstractLfo { |
215 |
public: |
216 |
virtual float Render() = 0; |
217 |
virtual void Update(const uint16_t& ExtControlValue) = 0; |
218 |
virtual void Trigger(float Frequency, start_level_t StartLevel, uint16_t InternalDepth, uint16_t ExtControlDepth, bool FlipPhase, unsigned int SampleRate) = 0; |
219 |
virtual void SetPhase(float phase) = 0; |
220 |
virtual void SetFrequency(float Frequency, unsigned int SampleRate) = 0; |
221 |
}; |
222 |
|
223 |
template <class T> |
224 |
class LfoBase: public AbstractLfo, public T { |
225 |
public: |
226 |
LfoBase(float Max): T(Max) { } |
227 |
virtual float Render() { return T::render(); } |
228 |
|
229 |
virtual void Update(const uint16_t& ExtControlValue) { T::update(ExtControlValue); } |
230 |
|
231 |
virtual void Trigger ( |
232 |
float Frequency, start_level_t StartLevel, uint16_t InternalDepth, |
233 |
uint16_t ExtControlDepth, bool FlipPhase, unsigned int SampleRate |
234 |
) { |
235 |
T::trigger(Frequency, StartLevel, InternalDepth, ExtControlDepth, FlipPhase, SampleRate); |
236 |
} |
237 |
|
238 |
virtual void SetPhase(float phase) { T::setPhase(phase); } |
239 |
|
240 |
virtual void SetFrequency(float Frequency, unsigned int SampleRate) { |
241 |
T::setFrequency(Frequency, SampleRate); |
242 |
} |
243 |
}; |
244 |
|
245 |
class LFOUnit; |
246 |
|
247 |
class FadeEGUnit: public EGUnit<EGADSR> { |
248 |
public: |
249 |
FadeEGUnit(SfzSignalUnitRack* rack): EGUnit<EGADSR>(rack) { } |
250 |
virtual void Trigger() { } |
251 |
virtual void EnterReleaseStage() { } |
252 |
virtual void CancelRelease() { } |
253 |
|
254 |
friend class LFOUnit; |
255 |
}; |
256 |
|
257 |
class LFOUnit: public SfzSignalUnit, public CCSignalUnit::Listener { |
258 |
public: |
259 |
::sfz::LFO* pLfoInfo; |
260 |
AbstractLfo* pLFO; |
261 |
FadeEGUnit suFadeEG; |
262 |
SmoothCCUnit suDepthOnCC; |
263 |
SmoothCCUnit suFreqOnCC; |
264 |
|
265 |
LFOUnit(SfzSignalUnitRack* rack); |
266 |
LFOUnit(const LFOUnit& Unit); |
267 |
void operator=(const LFOUnit& Unit) { Copy(Unit); } |
268 |
|
269 |
void Copy(const LFOUnit& Unit) { |
270 |
pLfoInfo = Unit.pLfoInfo; |
271 |
suFadeEG = Unit.suFadeEG; |
272 |
|
273 |
SfzSignalUnit::Copy(Unit); |
274 |
} |
275 |
|
276 |
virtual void Trigger(); |
277 |
virtual void Increment(); |
278 |
virtual float GetLevel() { return Level; } |
279 |
|
280 |
// CCSignalUnit::Listener interface implementation |
281 |
virtual void ValueChanged(CCSignalUnit* pUnit); |
282 |
}; |
283 |
|
284 |
class LFOv1Unit: public LFOUnit { |
285 |
public: |
286 |
::sfz::LFO lfoInfo; |
287 |
LfoBase<LFOSigned> lfo; |
288 |
|
289 |
LFOv1Unit(SfzSignalUnitRack* rack): LFOUnit(rack), lfo(1200.0f) { |
290 |
pLfoInfo = &lfoInfo; pLFO = &lfo; |
291 |
} |
292 |
|
293 |
virtual void Trigger(); |
294 |
}; |
295 |
|
296 |
class LFOv2Unit: public LFOUnit { |
297 |
protected: |
298 |
FixedArray<AbstractLfo*> lfos; |
299 |
LfoBase<LFOSigned> lfo0; // triangle |
300 |
LfoBase<SineLFO<range_signed> > lfo1; // sine |
301 |
LfoBase<PulseLFO<range_unsigned, 750> > lfo2; // pulse 75% |
302 |
LfoBase<SquareLFO<range_signed> > lfo3; // square |
303 |
LfoBase<PulseLFO<range_unsigned, 250> > lfo4; // pulse 25% |
304 |
LfoBase<PulseLFO<range_unsigned, 125> > lfo5; // pulse 12,5% |
305 |
LfoBase<SawLFO<range_unsigned, true> > lfo6; // saw up |
306 |
LfoBase<SawLFO<range_unsigned, false> > lfo7; // saw down |
307 |
|
308 |
|
309 |
public: |
310 |
SmoothCCUnit suVolOnCC; |
311 |
SmoothCCUnit suPitchOnCC; |
312 |
SmoothCCUnit suPanOnCC; |
313 |
SmoothCCUnit suCutoffOnCC; |
314 |
SmoothCCUnit suResOnCC; |
315 |
|
316 |
LFOv2Unit(SfzSignalUnitRack* rack); |
317 |
|
318 |
virtual void Trigger(); |
319 |
virtual bool Active() { return true; } |
320 |
}; |
321 |
|
322 |
class AmpLFOUnit: public LFOv1Unit { |
323 |
public: |
324 |
AmpLFOUnit(SfzSignalUnitRack* rack): LFOv1Unit(rack) { } |
325 |
|
326 |
virtual void Trigger(); |
327 |
}; |
328 |
|
329 |
class PitchLFOUnit: public LFOv1Unit { |
330 |
public: |
331 |
PitchLFOUnit(SfzSignalUnitRack* rack): LFOv1Unit(rack) { } |
332 |
|
333 |
virtual void Trigger(); |
334 |
}; |
335 |
|
336 |
class FilLFOUnit: public LFOv1Unit { |
337 |
public: |
338 |
FilLFOUnit(SfzSignalUnitRack* rack): LFOv1Unit(rack) { } |
339 |
|
340 |
virtual void Trigger(); |
341 |
}; |
342 |
|
343 |
|
344 |
class EndpointUnit: public EndpointSignalUnit { |
345 |
private: |
346 |
float xfCoeff; // crossfade coefficient |
347 |
float pitchVeltrackRatio; |
348 |
|
349 |
public: |
350 |
Voice* pVoice; |
351 |
XFInCCUnit suXFInCC; |
352 |
XFOutCCUnit suXFOutCC; |
353 |
SmoothCCUnit suPanOnCC; |
354 |
|
355 |
EndpointUnit(SfzSignalUnitRack* rack); |
356 |
|
357 |
virtual void Trigger(); |
358 |
|
359 |
/** The endpoint should be active until the volume EG is active. */ |
360 |
virtual bool Active(); |
361 |
|
362 |
virtual float GetVolume(); |
363 |
virtual float GetFilterCutoff(); |
364 |
virtual float GetPitch(); |
365 |
virtual float GetResonance(); |
366 |
virtual float GetPan(); |
367 |
|
368 |
SfzSignalUnitRack* const GetRack(); |
369 |
|
370 |
virtual float CalculateResonance(float res) { |
371 |
return GetResonance() + res; |
372 |
} |
373 |
|
374 |
virtual float CalculateFilterCutoff(float cutoff); |
375 |
}; |
376 |
|
377 |
|
378 |
class SfzSignalUnitRack : public SignalUnitRack { |
379 |
private: |
380 |
EndpointUnit suEndpoint; |
381 |
AmpEGUnit suVolEG; |
382 |
FilEGUnit suFilEG; |
383 |
PitchEGUnit suPitchEG; |
384 |
|
385 |
AmpLFOUnit suAmpLFO; |
386 |
PitchLFOUnit suPitchLFO; |
387 |
FilLFOUnit suFilLFO; |
388 |
|
389 |
CCUnit suEq1GainOnCC; |
390 |
CCUnit suEq2GainOnCC; |
391 |
CCUnit suEq3GainOnCC; |
392 |
|
393 |
CCUnit suEq1FreqOnCC; |
394 |
CCUnit suEq2FreqOnCC; |
395 |
CCUnit suEq3FreqOnCC; |
396 |
|
397 |
CCUnit suEq1BwOnCC; |
398 |
CCUnit suEq2BwOnCC; |
399 |
CCUnit suEq3BwOnCC; |
400 |
|
401 |
// SFZ v2 |
402 |
|
403 |
SmoothCCUnit suVolOnCC; |
404 |
SmoothCCUnit suPitchOnCC; |
405 |
SmoothCCUnit suCutoffOnCC; |
406 |
SmoothCCUnit suResOnCC; |
407 |
|
408 |
FixedArray<EGv2Unit*> EGs; |
409 |
|
410 |
// used for optimization - contains only the ones that are modulating volume |
411 |
FixedArray<EGv2Unit*> volEGs; |
412 |
|
413 |
// used for optimization - contains only the ones that are modulating pitch |
414 |
FixedArray<EGv2Unit*> pitchEGs; |
415 |
|
416 |
// used for optimization - contains only the ones that are modulating filter cutoff |
417 |
FixedArray<EGv2Unit*> filEGs; |
418 |
|
419 |
// used for optimization - contains only the ones that are modulating resonance |
420 |
FixedArray<EGv2Unit*> resEGs; |
421 |
|
422 |
// used for optimization - contains only the ones that are modulating pitch |
423 |
FixedArray<EGv2Unit*> panEGs; |
424 |
|
425 |
|
426 |
FixedArray<LFOv2Unit*> LFOs; |
427 |
|
428 |
// used for optimization - contains only the ones that are modulating volume |
429 |
FixedArray<LFOv2Unit*> volLFOs; |
430 |
|
431 |
// used for optimization - contains only the ones that are modulating pitch |
432 |
FixedArray<LFOv2Unit*> pitchLFOs; |
433 |
|
434 |
// used for optimization - contains only the ones that are modulating filter cutoff |
435 |
FixedArray<LFOv2Unit*> filLFOs; |
436 |
|
437 |
// used for optimization - contains only the ones that are modulating resonance |
438 |
FixedArray<LFOv2Unit*> resLFOs; |
439 |
|
440 |
// used for optimization - contains only the ones that are modulating pan |
441 |
FixedArray<LFOv2Unit*> panLFOs; |
442 |
|
443 |
|
444 |
public: |
445 |
Voice* const pVoice; |
446 |
|
447 |
/** |
448 |
* @param Voice The voice to which this rack belongs. |
449 |
*/ |
450 |
SfzSignalUnitRack(Voice* voice); |
451 |
~SfzSignalUnitRack(); |
452 |
|
453 |
virtual EndpointSignalUnit* GetEndpointUnit(); |
454 |
|
455 |
virtual void Trigger(); |
456 |
virtual void EnterFadeOutStage(); |
457 |
|
458 |
/** Called when the engine is set and the engine's pools are ready to use. */ |
459 |
void InitRTLists(); |
460 |
|
461 |
/** Invoked when the voice gone inactive. */ |
462 |
void Reset(); |
463 |
|
464 |
virtual void UpdateEqSettings(EqSupport* pEqSupport); |
465 |
|
466 |
friend class EndpointUnit; |
467 |
}; |
468 |
}} // namespace LinuxSampler::sfz |
469 |
|
470 |
#endif /* __LS_SFZSIGNALUNITRACK_H__ */ |
471 |
|