/[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 2232 - (show annotations) (download) (as text)
Mon Aug 8 13:40:04 2011 UTC (12 years, 8 months ago) by iliev
File MIME type: text/x-c++hdr
File size: 13919 byte(s)
* sfz engine: implemented opcode volume_smoothccN

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 CCUnit suFreqOnCC;
232
233 LFOUnit(SfzSignalUnitRack* rack);
234 LFOUnit(const LFOUnit& Unit);
235 void operator=(const LFOUnit& Unit) { Copy(Unit); }
236
237 void Copy(const LFOUnit& Unit) {
238 pLfoInfo = Unit.pLfoInfo;
239 suFadeEG = Unit.suFadeEG;
240
241 SfzSignalUnit::Copy(Unit);
242 }
243
244 virtual bool Active() { return pLfoInfo->freq > 0; }
245 virtual void Trigger();
246 virtual void Increment();
247 virtual float GetLevel() { return Level; }
248 virtual void ValueChanged(CCSignalUnit* pUnit);
249 };
250
251 class LFOv1Unit: public LFOUnit {
252 public:
253 ::sfz::LFO lfoInfo;
254 LfoBase<LFOSigned> lfo;
255
256 LFOv1Unit(SfzSignalUnitRack* rack): LFOUnit(rack), lfo(1200.0f) {
257 pLfoInfo = &lfoInfo; pLFO = &lfo;
258 }
259
260 virtual void Trigger();
261 };
262
263 class LFOv2Unit: public LFOUnit {
264 protected:
265 FixedArray<AbstractLfo*> lfos;
266 LfoBase<LFOSigned> lfo0; // triangle
267 LfoBase<SineLFO<range_signed> > lfo1; // sine
268 LfoBase<PulseLFO<range_unsigned, 750> > lfo2; // pulse 75%
269 LfoBase<SquareLFO<range_signed> > lfo3; // square
270 LfoBase<PulseLFO<range_unsigned, 250> > lfo4; // pulse 25%
271 LfoBase<PulseLFO<range_unsigned, 125> > lfo5; // pulse 12,5%
272 LfoBase<SawLFO<range_unsigned, true> > lfo6; // saw up
273 LfoBase<SawLFO<range_unsigned, false> > lfo7; // saw down
274
275
276 public:
277 CCUnit suPitchOnCC;
278
279 LFOv2Unit(SfzSignalUnitRack* rack);
280
281 virtual void Trigger();
282 };
283
284 class AmpLFOUnit: public LFOv1Unit {
285 public:
286 AmpLFOUnit(SfzSignalUnitRack* rack): LFOv1Unit(rack) { }
287
288 virtual void Trigger();
289 };
290
291 class PitchLFOUnit: public LFOv1Unit {
292 public:
293 CCUnit suDepthCC;
294
295 PitchLFOUnit(SfzSignalUnitRack* rack): LFOv1Unit(rack), suDepthCC(rack) { }
296
297 virtual void Trigger();
298 };
299
300 class FilLFOUnit: public LFOv1Unit {
301 public:
302 FilLFOUnit(SfzSignalUnitRack* rack): LFOv1Unit(rack) { }
303
304 virtual void Trigger();
305 };
306
307
308 class EndpointUnit: public EndpointSignalUnit {
309 public:
310 Voice* pVoice;
311
312 EndpointUnit(SfzSignalUnitRack* rack);
313
314 virtual void Trigger();
315
316 /** The endpoint should be active until the volume EG is active. */
317 virtual bool Active();
318
319 virtual float GetVolume();
320 virtual float GetFilterCutoff();
321 virtual float GetPitch();
322 virtual float GetResonance();
323 virtual float GetPan();
324
325 SfzSignalUnitRack* const GetRack();
326
327 virtual float CalculateResonance(float res) {
328 return GetResonance() + res;
329 }
330 };
331
332
333 class SfzSignalUnitRack : public SignalUnitRack {
334 private:
335 EndpointUnit suEndpoint;
336 AmpEGUnit suVolEG;
337 FilEGUnit suFilEG;
338 PitchEGUnit suPitchEG;
339
340 AmpLFOUnit suAmpLFO;
341 PitchLFOUnit suPitchLFO;
342 FilLFOUnit suFilLFO;
343
344 // SFZ v2
345
346 SmoothCCUnit suVolOnCC;
347
348 FixedArray<EGv2Unit*> EGs;
349
350 // used for optimization - contains only the ones that are modulating volume
351 FixedArray<EGv2Unit*> volEGs;
352
353 // used for optimization - contains only the ones that are modulating pitch
354 FixedArray<EGv2Unit*> pitchEGs;
355
356
357 FixedArray<LFOv2Unit*> LFOs;
358
359 // used for optimization - contains only the ones that are modulating pitch
360 FixedArray<LFOv2Unit*> pitchLFOs;
361
362 // used for optimization - contains only the ones that are modulating filter cutoff
363 FixedArray<LFOv2Unit*> filLFOs;
364
365 // used for optimization - contains only the ones that are modulating resonance
366 FixedArray<LFOv2Unit*> resLFOs;
367
368 // used for optimization - contains only the ones that are modulating pan
369 FixedArray<LFOv2Unit*> panLFOs;
370
371
372 public:
373 Voice* const pVoice;
374
375 /**
376 * @param Voice The voice to which this rack belongs.
377 */
378 SfzSignalUnitRack(Voice* voice);
379 ~SfzSignalUnitRack();
380
381 virtual EndpointSignalUnit* GetEndpointUnit();
382
383 virtual void Trigger();
384 virtual void EnterFadeOutStage();
385
386 friend class EndpointUnit;
387 };
388 }} // namespace LinuxSampler::sfz
389
390 #endif /* __LS_SFZSIGNALUNITRACK_H__ */
391

  ViewVC Help
Powered by ViewVC