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