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