/[svn]/linuxsampler/trunk/src/engines/sfz/SfzSignalUnitRack.h
ViewVC logotype

Contents of /linuxsampler/trunk/src/engines/sfz/SfzSignalUnitRack.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2236 - (show annotations) (download) (as text)
Thu Aug 11 18:25:45 2011 UTC (12 years, 8 months ago) by iliev
File MIME type: text/x-c++hdr
File size: 15439 byte(s)
* sfz engine: implemented opcodes xfin_lokey, xfin_hikey,
  xfout_lokey, xfout_hikey, xf_keycurve, xfin_lovel, xfin_hivel,
  xfout_lovel, xfout_hivel, xf_velcurve, xfin_loccN, xfin_hiccN,
  xfout_loccN, xfout_hiccN, xf_cccurve

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

  ViewVC Help
Powered by ViewVC