/[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 2225 - (show annotations) (download) (as text)
Tue Aug 2 13:44:57 2011 UTC (12 years, 7 months ago) by iliev
File MIME type: text/x-c++hdr
File size: 11852 byte(s)
* sfz engine: implemented opcodes lfoN_phase, lfoN_phase_onccX,
  lfoN_pitch, lfoN_pitch_onccX

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

  ViewVC Help
Powered by ViewVC