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 |
|
|
#include "SfzSignalUnitRack.h" |
24 |
|
|
#include "Voice.h" |
25 |
iliev |
2221 |
#include <SF.h> |
26 |
iliev |
2218 |
|
27 |
|
|
namespace LinuxSampler { namespace sfz { |
28 |
|
|
|
29 |
|
|
SfzSignalUnit::SfzSignalUnit(SfzSignalUnitRack* rack): SignalUnit(rack), pVoice(rack->pVoice) { |
30 |
|
|
|
31 |
|
|
} |
32 |
|
|
|
33 |
|
|
double SfzSignalUnit::GetSampleRate() { |
34 |
|
|
return pVoice->GetSampleRate() / CONFIG_DEFAULT_SUBFRAGMENT_SIZE; |
35 |
|
|
} |
36 |
|
|
|
37 |
|
|
|
38 |
|
|
void EGv1Unit::Trigger() { |
39 |
|
|
::sfz::Region* const pRegion = pVoice->pRegion; |
40 |
iliev |
2220 |
|
41 |
iliev |
2218 |
// the length of the decay and release curves are dependent on the velocity |
42 |
|
|
const double velrelease = 1 / pVoice->GetVelocityRelease(pVoice->MIDIVelocity); |
43 |
|
|
|
44 |
iliev |
2220 |
// set the delay trigger |
45 |
|
|
uiDelayTrigger = (pRegion->ampeg_delay + pRegion->ampeg_vel2delay * velrelease) * GetSampleRate(); |
46 |
|
|
|
47 |
iliev |
2218 |
EG.trigger(uint(pRegion->ampeg_start * 10), |
48 |
|
|
std::max(0.0, pRegion->ampeg_attack + pRegion->ampeg_vel2attack * velrelease), |
49 |
|
|
std::max(0.0, pRegion->ampeg_hold + pRegion->ampeg_vel2hold * velrelease), |
50 |
|
|
std::max(0.0, pRegion->ampeg_decay + pRegion->ampeg_vel2decay * velrelease), |
51 |
|
|
uint(std::min(std::max(0.0, 10 * (pRegion->ampeg_sustain + pRegion->ampeg_vel2sustain * velrelease)), 1000.0)), |
52 |
|
|
std::max(0.0, pRegion->ampeg_release + pRegion->ampeg_vel2release * velrelease), |
53 |
|
|
GetSampleRate()); |
54 |
|
|
} |
55 |
|
|
|
56 |
iliev |
2220 |
|
57 |
iliev |
2218 |
void EGv2Unit::Trigger() { |
58 |
|
|
EG.trigger(*pEGInfo, GetSampleRate(), pVoice->MIDIVelocity); |
59 |
|
|
} |
60 |
iliev |
2219 |
|
61 |
iliev |
2220 |
|
62 |
|
|
void PitchEGUnit::Trigger() { |
63 |
|
|
::sfz::Region* const pRegion = pVoice->pRegion; |
64 |
|
|
depth = pRegion->pitcheg_depth; |
65 |
|
|
|
66 |
|
|
// the length of the decay and release curves are dependent on the velocity |
67 |
|
|
const double velrelease = 1 / pVoice->GetVelocityRelease(pVoice->MIDIVelocity); |
68 |
iliev |
2218 |
|
69 |
iliev |
2220 |
// set the delay trigger |
70 |
|
|
uiDelayTrigger = (pRegion->pitcheg_delay + pRegion->pitcheg_vel2delay * velrelease) * GetSampleRate(); |
71 |
|
|
|
72 |
|
|
EG.trigger(uint(pRegion->pitcheg_start * 10), |
73 |
|
|
std::max(0.0, pRegion->pitcheg_attack + pRegion->pitcheg_vel2attack * velrelease), |
74 |
|
|
std::max(0.0, pRegion->pitcheg_hold + pRegion->pitcheg_vel2hold * velrelease), |
75 |
|
|
std::max(0.0, pRegion->pitcheg_decay + pRegion->pitcheg_vel2decay * velrelease), |
76 |
|
|
uint(std::min(std::max(0.0, 10 * (pRegion->pitcheg_sustain + pRegion->pitcheg_vel2sustain * velrelease)), 1000.0)), |
77 |
|
|
std::max(0.0, pRegion->pitcheg_release + pRegion->pitcheg_vel2release * velrelease), |
78 |
|
|
GetSampleRate()); |
79 |
|
|
} |
80 |
|
|
|
81 |
|
|
|
82 |
iliev |
2219 |
void LFOUnit::Increment() { |
83 |
|
|
if (DelayStage()) return; |
84 |
|
|
|
85 |
|
|
SignalUnit::Increment(); |
86 |
|
|
|
87 |
|
|
Level = lfo.render(); |
88 |
|
|
} |
89 |
|
|
|
90 |
|
|
void LFOUnit::Trigger() { |
91 |
|
|
//reset |
92 |
|
|
Level = 0; |
93 |
|
|
|
94 |
|
|
// set the delay trigger |
95 |
iliev |
2220 |
uiDelayTrigger = pLfoInfo->delay * GetSampleRate(); |
96 |
iliev |
2219 |
} |
97 |
|
|
|
98 |
iliev |
2221 |
void LFOv1Unit::Trigger() { |
99 |
|
|
LFOUnit::Trigger(); |
100 |
|
|
|
101 |
|
|
lfo.trigger ( |
102 |
|
|
pLfoInfo->freq, |
103 |
|
|
start_level_mid, |
104 |
|
|
1, 0, false, GetSampleRate() |
105 |
|
|
); |
106 |
|
|
lfo.update(0); |
107 |
|
|
} |
108 |
|
|
|
109 |
iliev |
2219 |
void LFOv2Unit::Trigger() { |
110 |
|
|
LFOUnit::Trigger(); |
111 |
|
|
|
112 |
|
|
lfo.trigger ( |
113 |
|
|
pLfoInfo->freq, |
114 |
|
|
start_level_mid, |
115 |
|
|
1, 0, false, GetSampleRate() |
116 |
|
|
); |
117 |
|
|
lfo.update(0); |
118 |
|
|
} |
119 |
iliev |
2221 |
|
120 |
|
|
void AmpLFOUnit::Trigger() { |
121 |
|
|
::sfz::Region* const pRegion = pVoice->pRegion; |
122 |
|
|
pLfoInfo->delay = pRegion->amplfo_delay; |
123 |
|
|
pLfoInfo->freq = pRegion->amplfo_freq; |
124 |
|
|
pLfoInfo->volume = pRegion->amplfo_depth; |
125 |
|
|
|
126 |
|
|
LFOv1Unit::Trigger(); |
127 |
|
|
} |
128 |
|
|
|
129 |
|
|
void PitchLFOUnit::Trigger() { |
130 |
|
|
::sfz::Region* const pRegion = pVoice->pRegion; |
131 |
|
|
pLfoInfo->delay = pRegion->pitchlfo_delay; |
132 |
|
|
pLfoInfo->freq = pRegion->pitchlfo_freq; |
133 |
|
|
pLfoInfo->pitch = pRegion->pitchlfo_depth; |
134 |
|
|
|
135 |
|
|
LFOv1Unit::Trigger(); |
136 |
|
|
} |
137 |
|
|
|
138 |
|
|
void FilLFOUnit::Trigger() { |
139 |
|
|
::sfz::Region* const pRegion = pVoice->pRegion; |
140 |
|
|
pLfoInfo->delay = pRegion->fillfo_delay; |
141 |
|
|
pLfoInfo->freq = pRegion->fillfo_freq; |
142 |
|
|
pLfoInfo->cutoff = pRegion->fillfo_depth; |
143 |
|
|
|
144 |
|
|
LFOv1Unit::Trigger(); |
145 |
|
|
} |
146 |
iliev |
2218 |
|
147 |
iliev |
2219 |
|
148 |
iliev |
2218 |
EndpointUnit::EndpointUnit(SfzSignalUnitRack* rack): EndpointSignalUnit(rack) { |
149 |
|
|
|
150 |
|
|
} |
151 |
|
|
|
152 |
|
|
SfzSignalUnitRack* const EndpointUnit::GetRack() { |
153 |
|
|
return static_cast<SfzSignalUnitRack* const>(pRack); |
154 |
|
|
} |
155 |
|
|
|
156 |
|
|
void EndpointUnit::Trigger() { |
157 |
|
|
|
158 |
|
|
} |
159 |
|
|
|
160 |
|
|
bool EndpointUnit::Active() { |
161 |
|
|
if (GetRack()->suVolEG.Active()) return true; |
162 |
|
|
|
163 |
|
|
bool b = false; |
164 |
|
|
for (int i = 0; i < GetRack()->volEGs.size(); i++) { |
165 |
|
|
if (GetRack()->volEGs[i]->Active()) { b = true; break; } |
166 |
|
|
} |
167 |
|
|
|
168 |
|
|
return b; |
169 |
|
|
} |
170 |
|
|
|
171 |
|
|
float EndpointUnit::GetVolume() { |
172 |
|
|
float vol = GetRack()->suVolEG.Active() ? GetRack()->suVolEG.GetLevel() : 0; |
173 |
|
|
|
174 |
|
|
for (int i = 0; i < GetRack()->volEGs.size(); i++) { |
175 |
|
|
EGv2Unit* eg = GetRack()->volEGs[i]; |
176 |
|
|
if (!eg->Active()) continue; |
177 |
|
|
vol += eg->GetLevel() * (eg->pEGInfo->amplitude / 100.0f); |
178 |
|
|
} |
179 |
|
|
|
180 |
iliev |
2221 |
AmpLFOUnit* u = &(GetRack()->suAmpLFO); |
181 |
|
|
vol *= u->Active() ? ::sf2::ToRatio((u->GetLevel() * u->pLfoInfo->volume) * 10.0) : 1; |
182 |
|
|
|
183 |
iliev |
2218 |
return vol; |
184 |
|
|
} |
185 |
|
|
|
186 |
|
|
float EndpointUnit::GetFilterCutoff() { |
187 |
iliev |
2221 |
float val; |
188 |
iliev |
2219 |
|
189 |
iliev |
2221 |
FilLFOUnit* u = &(GetRack()->suFilLFO); |
190 |
|
|
val = u->Active() ? RTMath::CentsToFreqRatioUnlimited(u->GetLevel() * u->pLfoInfo->cutoff) : 1; |
191 |
|
|
|
192 |
iliev |
2219 |
for (int i = 0; i < GetRack()->filLFOs.size(); i++) { |
193 |
|
|
LFOv2Unit* lfo = GetRack()->filLFOs[i]; |
194 |
|
|
if (!lfo->Active()) continue; |
195 |
|
|
|
196 |
|
|
float f = lfo->GetLevel() * lfo->pLfoInfo->cutoff; |
197 |
|
|
val *= RTMath::CentsToFreqRatioUnlimited(f); |
198 |
|
|
} |
199 |
|
|
|
200 |
|
|
return val; |
201 |
iliev |
2218 |
} |
202 |
|
|
|
203 |
|
|
float EndpointUnit::GetPitch() { |
204 |
iliev |
2221 |
double p; |
205 |
iliev |
2220 |
EGv1Unit* u = &(GetRack()->suPitchEG); |
206 |
iliev |
2221 |
p = u->Active() ? RTMath::CentsToFreqRatioUnlimited(u->GetLevel() * u->depth) : 1; |
207 |
|
|
|
208 |
|
|
PitchLFOUnit* u2 = &(GetRack()->suPitchLFO); |
209 |
|
|
p *= u2->Active() ? RTMath::CentsToFreqRatioUnlimited(u2->GetLevel() * u2->pLfoInfo->pitch) : 1; |
210 |
|
|
|
211 |
|
|
return p; |
212 |
iliev |
2218 |
} |
213 |
|
|
|
214 |
|
|
float EndpointUnit::GetResonance() { |
215 |
iliev |
2219 |
float val = 0; |
216 |
|
|
|
217 |
|
|
for (int i = 0; i < GetRack()->resLFOs.size(); i++) { |
218 |
|
|
LFOv2Unit* lfo = GetRack()->resLFOs[i]; |
219 |
|
|
if (!lfo->Active()) continue; |
220 |
|
|
|
221 |
|
|
val += lfo->GetLevel() * lfo->pLfoInfo->resonance; |
222 |
|
|
} |
223 |
|
|
|
224 |
|
|
return val; |
225 |
iliev |
2218 |
} |
226 |
|
|
|
227 |
iliev |
2219 |
float EndpointUnit::GetPan() { |
228 |
|
|
float pan = 0; |
229 |
|
|
|
230 |
|
|
for (int i = 0; i < GetRack()->panLFOs.size(); i++) { |
231 |
|
|
LFOv2Unit* lfo = GetRack()->panLFOs[i]; |
232 |
|
|
if (!lfo->Active()) continue; |
233 |
|
|
|
234 |
|
|
pan += lfo->GetLevel() * lfo->pLfoInfo->pan; |
235 |
|
|
} |
236 |
|
|
|
237 |
|
|
if(pan < -100) return -100; |
238 |
|
|
if(pan > 100) return 100; |
239 |
|
|
|
240 |
|
|
return pan; |
241 |
|
|
} |
242 |
iliev |
2218 |
|
243 |
iliev |
2219 |
|
244 |
iliev |
2218 |
SfzSignalUnitRack::SfzSignalUnitRack(Voice* voice) |
245 |
iliev |
2220 |
: SignalUnitRack(MaxUnitCount), pVoice(voice), suEndpoint(this), suVolEG(this), suPitchEG(this), |
246 |
iliev |
2219 |
EGs(maxEgCount), volEGs(maxEgCount), pitchEGs(maxEgCount), |
247 |
iliev |
2221 |
suAmpLFO(this), suPitchLFO(this), suFilLFO(this), |
248 |
iliev |
2219 |
LFOs(maxLfoCount), filLFOs(maxLfoCount), resLFOs(maxLfoCount), panLFOs(maxLfoCount) |
249 |
iliev |
2218 |
{ |
250 |
iliev |
2220 |
suEndpoint.pVoice = suVolEG.pVoice = suPitchEG.pVoice = voice; |
251 |
iliev |
2221 |
suAmpLFO.pVoice = suPitchLFO.pVoice = suFilLFO.pVoice = voice; |
252 |
iliev |
2218 |
|
253 |
|
|
for (int i = 0; i < EGs.capacity(); i++) { |
254 |
|
|
EGs[i] = new EGv2Unit(this); |
255 |
|
|
EGs[i]->pVoice = voice; |
256 |
|
|
} |
257 |
iliev |
2219 |
|
258 |
|
|
for (int i = 0; i < LFOs.capacity(); i++) { |
259 |
|
|
LFOs[i] = new LFOv2Unit(this); |
260 |
|
|
LFOs[i]->pVoice = voice; |
261 |
|
|
} |
262 |
iliev |
2218 |
} |
263 |
|
|
|
264 |
|
|
SfzSignalUnitRack::~SfzSignalUnitRack() { |
265 |
iliev |
2219 |
for (int i = 0; i < EGs.capacity(); i++) { |
266 |
iliev |
2218 |
delete EGs[i]; EGs[i] = NULL; |
267 |
|
|
} |
268 |
iliev |
2219 |
|
269 |
|
|
for (int i = 0; i < LFOs.capacity(); i++) { |
270 |
|
|
delete LFOs[i]; LFOs[i] = NULL; |
271 |
|
|
} |
272 |
iliev |
2218 |
} |
273 |
|
|
|
274 |
|
|
void SfzSignalUnitRack::Trigger() { |
275 |
|
|
EGs.clear(); |
276 |
|
|
volEGs.clear(); |
277 |
|
|
pitchEGs.clear(); |
278 |
|
|
|
279 |
iliev |
2219 |
LFOs.clear(); |
280 |
|
|
filLFOs.clear(); |
281 |
|
|
resLFOs.clear(); |
282 |
|
|
panLFOs.clear(); |
283 |
|
|
|
284 |
iliev |
2218 |
::sfz::Region* const pRegion = pVoice->pRegion; |
285 |
|
|
|
286 |
iliev |
2219 |
for (int i = 0; i < pRegion->eg.size(); i++) { |
287 |
iliev |
2218 |
if (pRegion->eg[i].node.size() == 0) continue; |
288 |
|
|
|
289 |
|
|
if(EGs.size() < EGs.capacity()) { |
290 |
|
|
EGv2Unit eg(this); |
291 |
|
|
eg.pEGInfo = &(pRegion->eg[i]); |
292 |
|
|
EGs.increment()->Copy(eg); |
293 |
|
|
} else { std::cerr << "Maximum number of EGs reached!" << std::endl; break; } |
294 |
|
|
|
295 |
|
|
if (pRegion->eg[i].amplitude > 0) { |
296 |
|
|
if(volEGs.size() < volEGs.capacity()) volEGs.add(EGs[EGs.size() - 1]); |
297 |
|
|
else std::cerr << "Maximum number of EGs reached!" << std::endl; |
298 |
|
|
} |
299 |
|
|
} |
300 |
|
|
|
301 |
|
|
if (pRegion->ampeg_sustain == -1) { |
302 |
|
|
if (volEGs.size() > 0) pRegion->ampeg_sustain = 0; |
303 |
|
|
else pRegion->ampeg_sustain = 100; |
304 |
|
|
} |
305 |
|
|
|
306 |
iliev |
2219 |
// LFO |
307 |
|
|
for (int i = 0; i < pRegion->lfos.size(); i++) { |
308 |
|
|
if (pRegion->lfos[i].freq == -1) continue; // Not initialized |
309 |
|
|
|
310 |
|
|
if(LFOs.size() < LFOs.capacity()) { |
311 |
|
|
LFOv2Unit lfo(this); |
312 |
|
|
lfo.pLfoInfo = &(pRegion->lfos[i]); |
313 |
|
|
LFOs.increment()->Copy(lfo); |
314 |
|
|
} else { std::cerr << "Maximum number of LFOs reached!" << std::endl; break; } |
315 |
|
|
|
316 |
|
|
if (pRegion->lfos[i].cutoff != 0) { |
317 |
|
|
if(filLFOs.size() < filLFOs.capacity()) filLFOs.add(LFOs[LFOs.size() - 1]); |
318 |
|
|
else std::cerr << "Maximum number of LFOs reached!" << std::endl; |
319 |
|
|
} |
320 |
|
|
|
321 |
|
|
if (pRegion->lfos[i].resonance != 0) { |
322 |
|
|
if(resLFOs.size() < resLFOs.capacity()) resLFOs.add(LFOs[LFOs.size() - 1]); |
323 |
|
|
else std::cerr << "Maximum number of LFOs reached!" << std::endl; |
324 |
|
|
} |
325 |
|
|
|
326 |
|
|
if (pRegion->lfos[i].pan != 0) { |
327 |
|
|
if(panLFOs.size() < panLFOs.capacity()) panLFOs.add(LFOs[LFOs.size() - 1]); |
328 |
|
|
else std::cerr << "Maximum number of LFOs reached!" << std::endl; |
329 |
|
|
} |
330 |
|
|
} |
331 |
|
|
|
332 |
iliev |
2218 |
Units.clear(); |
333 |
|
|
|
334 |
|
|
Units.add(&suVolEG); |
335 |
iliev |
2220 |
Units.add(&suPitchEG); |
336 |
iliev |
2221 |
Units.add(&suPitchLFO); |
337 |
|
|
Units.add(&suAmpLFO); |
338 |
|
|
Units.add(&suFilLFO); |
339 |
iliev |
2218 |
|
340 |
iliev |
2219 |
for (int i = 0; i < EGs.size(); i++) { |
341 |
iliev |
2218 |
Units.add(EGs[i]); |
342 |
|
|
} |
343 |
|
|
|
344 |
iliev |
2219 |
for (int i = 0; i < LFOs.size(); i++) { |
345 |
|
|
Units.add(LFOs[i]); |
346 |
|
|
} |
347 |
|
|
|
348 |
iliev |
2218 |
Units.add(&suEndpoint); |
349 |
|
|
|
350 |
|
|
SignalUnitRack::Trigger(); |
351 |
|
|
} |
352 |
|
|
|
353 |
|
|
EndpointSignalUnit* SfzSignalUnitRack::GetEndpointUnit() { |
354 |
|
|
return &suEndpoint; |
355 |
|
|
} |
356 |
|
|
|
357 |
|
|
void SfzSignalUnitRack::EnterFadeOutStage() { |
358 |
|
|
suVolEG.EG.enterFadeOutStage(); |
359 |
|
|
|
360 |
|
|
for (int i = 0; i < volEGs.size(); i++) { |
361 |
|
|
volEGs[i]->EG.enterFadeOutStage(); |
362 |
|
|
} |
363 |
|
|
} |
364 |
|
|
|
365 |
|
|
}} // namespace LinuxSampler::sfz |