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