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