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