/[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 2230 - (show annotations) (download) (as text)
Fri Aug 5 17:59:10 2011 UTC (12 years, 8 months ago) by iliev
File MIME type: text/x-c++hdr
File size: 13696 byte(s)
* sfz engine: implemented curves
* sfz engine: implemented opcodes volume_onccN, volume_curveccN

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

  ViewVC Help
Powered by ViewVC