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