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