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, Listener* l = NULL); |
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 |
virtual void SetFrequency(float Frequency, unsigned int SampleRate) = 0; |
154 |
}; |
155 |
|
156 |
template <class T> |
157 |
class LfoBase: public AbstractLfo, public T { |
158 |
public: |
159 |
LfoBase(float Max): T(Max) { } |
160 |
virtual float Render() { return T::render(); } |
161 |
|
162 |
virtual void Update(const uint16_t& ExtControlValue) { T::update(ExtControlValue); } |
163 |
|
164 |
virtual void Trigger ( |
165 |
float Frequency, start_level_t StartLevel, uint16_t InternalDepth, |
166 |
uint16_t ExtControlDepth, bool FlipPhase, unsigned int SampleRate |
167 |
) { |
168 |
T::trigger(Frequency, StartLevel, InternalDepth, ExtControlDepth, FlipPhase, SampleRate); |
169 |
} |
170 |
|
171 |
virtual void SetPhase(float phase) { T::setPhase(phase); } |
172 |
|
173 |
virtual void SetFrequency(float Frequency, unsigned int SampleRate) { |
174 |
T::setFrequency(Frequency, SampleRate); |
175 |
} |
176 |
}; |
177 |
|
178 |
class LFOUnit; |
179 |
|
180 |
class FadeEGUnit: public EGUnit<EGADSR> { |
181 |
public: |
182 |
FadeEGUnit(SfzSignalUnitRack* rack): EGUnit<EGADSR>(rack) { } |
183 |
virtual void Trigger() { } |
184 |
virtual void EnterReleaseStage() { } |
185 |
virtual void CancelRelease() { } |
186 |
|
187 |
friend class LFOUnit; |
188 |
}; |
189 |
|
190 |
class LFOUnit: public SfzSignalUnit, public CCSignalUnit::Listener { |
191 |
public: |
192 |
::sfz::LFO* pLfoInfo; |
193 |
AbstractLfo* pLFO; |
194 |
FadeEGUnit suFadeEG; |
195 |
CCUnit suFreqOnCC; |
196 |
|
197 |
LFOUnit(SfzSignalUnitRack* rack); |
198 |
LFOUnit(const LFOUnit& Unit); |
199 |
void operator=(const LFOUnit& Unit) { Copy(Unit); } |
200 |
|
201 |
void Copy(const LFOUnit& Unit) { |
202 |
pLfoInfo = Unit.pLfoInfo; |
203 |
suFadeEG = Unit.suFadeEG; |
204 |
|
205 |
SfzSignalUnit::Copy(Unit); |
206 |
} |
207 |
|
208 |
virtual bool Active() { return true; } |
209 |
virtual void Trigger(); |
210 |
virtual void Increment(); |
211 |
virtual float GetLevel() { return Level; } |
212 |
virtual void ValueChanged(CCSignalUnit* pUnit); |
213 |
}; |
214 |
|
215 |
class LFOv1Unit: public LFOUnit { |
216 |
public: |
217 |
::sfz::LFO lfoInfo; |
218 |
LfoBase<LFOSigned> lfo; |
219 |
|
220 |
LFOv1Unit(SfzSignalUnitRack* rack): LFOUnit(rack), lfo(1200.0f) { |
221 |
pLfoInfo = &lfoInfo; pLFO = &lfo; |
222 |
} |
223 |
|
224 |
virtual void Trigger(); |
225 |
}; |
226 |
|
227 |
class LFOv2Unit: public LFOUnit { |
228 |
protected: |
229 |
FixedArray<AbstractLfo*> lfos; |
230 |
LfoBase<LFOSigned> lfo0; // triangle |
231 |
LfoBase<SineLFO<range_signed> > lfo1; // sine |
232 |
LfoBase<PulseLFO<range_unsigned, 750> > lfo2; // pulse 75% |
233 |
LfoBase<SquareLFO<range_signed> > lfo3; // square |
234 |
LfoBase<PulseLFO<range_unsigned, 250> > lfo4; // pulse 25% |
235 |
LfoBase<PulseLFO<range_unsigned, 125> > lfo5; // pulse 12,5% |
236 |
LfoBase<SawLFO<range_unsigned, true> > lfo6; // saw up |
237 |
LfoBase<SawLFO<range_unsigned, false> > lfo7; // saw down |
238 |
|
239 |
|
240 |
public: |
241 |
CCUnit suPitchOnCC; |
242 |
|
243 |
LFOv2Unit(SfzSignalUnitRack* rack); |
244 |
|
245 |
virtual void Trigger(); |
246 |
}; |
247 |
|
248 |
class AmpLFOUnit: public LFOv1Unit { |
249 |
public: |
250 |
AmpLFOUnit(SfzSignalUnitRack* rack): LFOv1Unit(rack) { } |
251 |
|
252 |
virtual void Trigger(); |
253 |
}; |
254 |
|
255 |
class PitchLFOUnit: public LFOv1Unit { |
256 |
public: |
257 |
CCUnit suDepthCC; |
258 |
|
259 |
PitchLFOUnit(SfzSignalUnitRack* rack): LFOv1Unit(rack), suDepthCC(rack) { } |
260 |
|
261 |
virtual void Trigger(); |
262 |
}; |
263 |
|
264 |
class FilLFOUnit: public LFOv1Unit { |
265 |
public: |
266 |
FilLFOUnit(SfzSignalUnitRack* rack): LFOv1Unit(rack) { } |
267 |
|
268 |
virtual void Trigger(); |
269 |
}; |
270 |
|
271 |
|
272 |
|
273 |
class EndpointUnit: public EndpointSignalUnit { |
274 |
public: |
275 |
Voice* pVoice; |
276 |
|
277 |
EndpointUnit(SfzSignalUnitRack* rack); |
278 |
|
279 |
virtual void Trigger(); |
280 |
|
281 |
/** The endpoint should be active until the volume EG is active. */ |
282 |
virtual bool Active(); |
283 |
|
284 |
virtual float GetVolume(); |
285 |
virtual float GetFilterCutoff(); |
286 |
virtual float GetPitch(); |
287 |
virtual float GetResonance(); |
288 |
virtual float GetPan(); |
289 |
|
290 |
SfzSignalUnitRack* const GetRack(); |
291 |
|
292 |
virtual float CalculateResonance(float res) { |
293 |
return GetResonance() + res; |
294 |
} |
295 |
}; |
296 |
|
297 |
|
298 |
class SfzSignalUnitRack : public SignalUnitRack { |
299 |
private: |
300 |
EndpointUnit suEndpoint; |
301 |
EGv1Unit suVolEG; |
302 |
FilEGUnit suFilEG; |
303 |
PitchEGUnit suPitchEG; |
304 |
|
305 |
AmpLFOUnit suAmpLFO; |
306 |
PitchLFOUnit suPitchLFO; |
307 |
FilLFOUnit suFilLFO; |
308 |
|
309 |
FixedArray<EGv2Unit*> EGs; |
310 |
|
311 |
// used for optimization - contains only the ones that are modulating volume |
312 |
FixedArray<EGv2Unit*> volEGs; |
313 |
|
314 |
// used for optimization - contains only the ones that are modulating pitch |
315 |
FixedArray<EGv2Unit*> pitchEGs; |
316 |
|
317 |
|
318 |
FixedArray<LFOv2Unit*> LFOs; |
319 |
|
320 |
// used for optimization - contains only the ones that are modulating pitch |
321 |
FixedArray<LFOv2Unit*> pitchLFOs; |
322 |
|
323 |
// used for optimization - contains only the ones that are modulating filter cutoff |
324 |
FixedArray<LFOv2Unit*> filLFOs; |
325 |
|
326 |
// used for optimization - contains only the ones that are modulating resonance |
327 |
FixedArray<LFOv2Unit*> resLFOs; |
328 |
|
329 |
// used for optimization - contains only the ones that are modulating pan |
330 |
FixedArray<LFOv2Unit*> panLFOs; |
331 |
|
332 |
|
333 |
public: |
334 |
Voice* const pVoice; |
335 |
|
336 |
/** |
337 |
* @param Voice The voice to which this rack belongs. |
338 |
*/ |
339 |
SfzSignalUnitRack(Voice* voice); |
340 |
~SfzSignalUnitRack(); |
341 |
|
342 |
virtual EndpointSignalUnit* GetEndpointUnit(); |
343 |
|
344 |
virtual void Trigger(); |
345 |
virtual void EnterFadeOutStage(); |
346 |
|
347 |
friend class EndpointUnit; |
348 |
}; |
349 |
}} // namespace LinuxSampler::sfz |
350 |
|
351 |
#endif /* __LS_SFZSIGNALUNITRACK_H__ */ |
352 |
|