/[svn]/linuxsampler/trunk/src/engines/sfz/Voice.cpp
ViewVC logotype

Contents of /linuxsampler/trunk/src/engines/sfz/Voice.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2061 - (show annotations) (download)
Tue Feb 23 18:32:31 2010 UTC (14 years, 2 months ago) by persson
File size: 21830 byte(s)
* sfz engine: added support for trigger=release and rt_decay

1 /***************************************************************************
2 * *
3 * LinuxSampler - modular, streaming capable sampler *
4 * *
5 * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck *
6 * Copyright (C) 2005 - 2008 Christian Schoenebeck *
7 * Copyright (C) 2009 - 2010 Christian Schoenebeck and Grigor Iliev *
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 * This program is distributed in the hope that it will be useful, *
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
17 * GNU General Public License for more details. *
18 * *
19 * You should have received a copy of the GNU General Public License *
20 * along with this program; if not, write to the Free Software *
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
22 * MA 02111-1307 USA *
23 ***************************************************************************/
24
25 #include "Voice.h"
26
27 #include "Engine.h"
28 #include "EngineChannel.h"
29
30 namespace LinuxSampler { namespace sfz {
31
32 Voice::Voice() {
33 pEngine = NULL;
34 }
35
36 Voice::~Voice() {
37
38 }
39
40 EngineChannel* Voice::GetSfzEngineChannel() {
41 return static_cast<EngineChannel*>(pEngineChannel);
42 }
43
44 void Voice::SetEngine(LinuxSampler::Engine* pEngine) {
45 Engine* engine = static_cast<Engine*>(pEngine);
46 this->pEngine = engine;
47 this->pDiskThread = engine->pDiskThread;
48 dmsg(6,("Voice::SetEngine()\n"));
49 }
50
51 Voice::SampleInfo Voice::GetSampleInfo() {
52 SampleInfo si;
53 si.SampleRate = pSample->GetSampleRate();
54 si.ChannelCount = pSample->GetChannelCount();
55 si.FrameSize = pSample->GetFrameSize();
56 si.BitDepth = (pSample->GetFrameSize() / pSample->GetChannelCount()) * 8;
57 si.TotalFrameCount = pSample->GetTotalFrameCount();
58
59 si.HasLoops = pRegion->HasLoop();
60 si.LoopStart = pRegion->GetLoopStart();
61 si.LoopLength = pRegion->GetLoopEnd() - pRegion->GetLoopStart();
62 si.LoopPlayCount = pRegion->GetLoopCount();
63 si.Unpitched = pRegion->pitch_keytrack == 0;
64 return si;
65 }
66
67 Voice::RegionInfo Voice::GetRegionInfo() {
68 RegionInfo ri;
69 ri.UnityNote = pRegion->pitch_keycenter;
70 ri.FineTune = pRegion->tune;
71 ri.Pan = int(pRegion->pan * 0.63); // convert from -100..100 to -64..63
72 ri.SampleStartOffset = 0; // TODO:
73
74 ri.EG1PreAttack = pRegion->ampeg_start * 10;
75 ri.EG1Attack = pRegion->ampeg_attack;
76 ri.EG1Hold = pRegion->ampeg_hold;
77 ri.EG1Decay1 = pRegion->ampeg_decay;
78 ri.EG1Decay2 = pRegion->ampeg_decay;
79 ri.EG1Sustain = pRegion->ampeg_sustain * 10;
80 ri.EG1InfiniteSustain = true;
81 ri.EG1Release = pRegion->ampeg_release;
82
83 ri.EG2PreAttack = pRegion->fileg_start * 10;
84 ri.EG2Attack = pRegion->fileg_attack;
85 //ri.EG2Hold = pRegion->fileg_hold; // TODO:
86 ri.EG2Decay1 = pRegion->fileg_decay;
87 ri.EG2Decay2 = pRegion->fileg_decay;
88 ri.EG2Sustain = pRegion->fileg_sustain * 10;
89 ri.EG2InfiniteSustain = true;
90 ri.EG2Release = pRegion->fileg_release;
91
92 ri.EG3Attack = pRegion->pitcheg_attack;
93 ri.EG3Depth = 0; // TODO:
94 ri.VCFEnabled = false; // TODO:
95 ri.VCFType = ::gig::vcf_type_lowpass; // TODO:
96 ri.VCFResonance = 0; // TODO:
97
98 // rt_decay is in dB. Precalculate a suitable value for exp in
99 // GetReleaseTriggerAttenuation: -ln(10) / 20 * rt_decay
100 ri.ReleaseTriggerDecay = -0.115129254649702 * pRegion->rt_decay;
101
102 return ri;
103 }
104
105 Voice::InstrumentInfo Voice::GetInstrumentInfo() {
106 InstrumentInfo ii;
107 ii.FineTune = 0; // TODO:
108 ii.PitchbendRange = 2; // TODO:
109
110 return ii;
111 }
112
113 double Voice::GetSampleAttenuation() {
114 return 1.0; // TODO:
115 }
116
117 double Voice::GetVelocityAttenuation(uint8_t MIDIKeyVelocity) {
118 return double(MIDIKeyVelocity) / 127.0f; // TODO:
119 }
120
121 double Voice::GetVelocityRelease(uint8_t MIDIKeyVelocity) {
122 return 0.9; // TODO:
123 }
124
125 void Voice::ProcessCCEvent(RTList<Event>::Iterator& itEvent) {
126 /*if (itEvent->Type == Event::type_control_change && itEvent->Param.CC.Controller) { // if (valid) MIDI control change event
127 if (pRegion->AttenuationController.type == ::gig::attenuation_ctrl_t::type_controlchange &&
128 itEvent->Param.CC.Controller == pRegion->AttenuationController.controller_number) {
129 CrossfadeSmoother.update(AbstractEngine::CrossfadeCurve[CrossfadeAttenuation(itEvent->Param.CC.Value)]);
130 }
131 }*/ // TODO: ^^^
132 }
133
134 void Voice::ProcessCutoffEvent(RTList<Event>::Iterator& itEvent) {
135 /*int ccvalue = itEvent->Param.CC.Value;
136 if (VCFCutoffCtrl.value == ccvalue) return;
137 VCFCutoffCtrl.value == ccvalue;
138 if (pRegion->VCFCutoffControllerInvert) ccvalue = 127 - ccvalue;
139 if (ccvalue < pRegion->VCFVelocityScale) ccvalue = pRegion->VCFVelocityScale;
140 float cutoff = CutoffBase * float(ccvalue);
141 if (cutoff > 127.0f) cutoff = 127.0f;
142
143 VCFCutoffCtrl.fvalue = cutoff; // needed for initialization of fFinalCutoff next time
144 fFinalCutoff = cutoff;*/ // TODO: ^^^
145 }
146
147 double Voice::CalculateCrossfadeVolume(uint8_t MIDIKeyVelocity) {
148 /*float crossfadeVolume;
149 switch (pRegion->AttenuationController.type) {
150 case ::gig::attenuation_ctrl_t::type_channelaftertouch:
151 crossfadeVolume = Engine::CrossfadeCurve[CrossfadeAttenuation(GetSfzEngineChannel()->ControllerTable[128])];
152 break;
153 case ::gig::attenuation_ctrl_t::type_velocity:
154 crossfadeVolume = Engine::CrossfadeCurve[CrossfadeAttenuation(MIDIKeyVelocity)];
155 break;
156 case ::gig::attenuation_ctrl_t::type_controlchange: //FIXME: currently not sample accurate
157 crossfadeVolume = Engine::CrossfadeCurve[CrossfadeAttenuation(GetSfzEngineChannel()->ControllerTable[pRegion->AttenuationController.controller_number])];
158 break;
159 case ::gig::attenuation_ctrl_t::type_none: // no crossfade defined
160 default:
161 crossfadeVolume = 1.0f;
162 }
163
164 return crossfadeVolume;*/ // TODO: ^^^
165 return 1.0f;
166 }
167
168 double Voice::GetEG1ControllerValue(uint8_t MIDIKeyVelocity) {
169 /*double eg1controllervalue = 0;
170 switch (pRegion->EG1Controller.type) {
171 case ::gig::eg1_ctrl_t::type_none: // no controller defined
172 eg1controllervalue = 0;
173 break;
174 case ::gig::eg1_ctrl_t::type_channelaftertouch:
175 eg1controllervalue = GetSfzEngineChannel()->ControllerTable[128];
176 break;
177 case ::gig::eg1_ctrl_t::type_velocity:
178 eg1controllervalue = MIDIKeyVelocity;
179 break;
180 case ::gig::eg1_ctrl_t::type_controlchange: // MIDI control change controller
181 eg1controllervalue = GetSfzEngineChannel()->ControllerTable[pRegion->EG1Controller.controller_number];
182 break;
183 }
184 if (pRegion->EG1ControllerInvert) eg1controllervalue = 127 - eg1controllervalue;
185
186 return eg1controllervalue;*/ // TODO: ^^^
187 return 0;
188 }
189
190 Voice::EGInfo Voice::CalculateEG1ControllerInfluence(double eg1ControllerValue) {
191 /*EGInfo eg;
192 // (eg1attack is different from the others)
193 eg.Attack = (pRegion->EG1ControllerAttackInfluence) ?
194 1 + 0.031 * (double) (pRegion->EG1ControllerAttackInfluence == 1 ?
195 1 : 1 << pRegion->EG1ControllerAttackInfluence) * eg1ControllerValue : 1.0;
196 eg.Decay = (pRegion->EG1ControllerDecayInfluence) ? 1 + 0.00775 * (double) (1 << pRegion->EG1ControllerDecayInfluence) * eg1ControllerValue : 1.0;
197 eg.Release = (pRegion->EG1ControllerReleaseInfluence) ? 1 + 0.00775 * (double) (1 << pRegion->EG1ControllerReleaseInfluence) * eg1ControllerValue : 1.0;
198
199 return eg;*/ // TODO: ^^^
200 EGInfo eg;
201 eg.Attack = 1.0;
202 eg.Decay = 1.0;
203 eg.Release = 1.0;
204 return eg;
205 }
206
207 void Voice::TriggerEG1(const EGInfo& egInfo, double velrelease, double velocityAttenuation, uint sampleRate, uint8_t velocity) {
208
209 // TODO: controller modulation
210
211 // first check if there is a v2 EG for amplitude
212 for (int i = 0 ; i < pRegion->eg.size() ; i++) {
213 if (pRegion->eg[i].amplitude > 0) {
214 // TODO: actually use the value of the amplitude parameter
215 pEG1 = &EG1;
216 EG1.trigger(pRegion->eg[i], sampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE, velocity);
217 return;
218 }
219 }
220
221 // otherwise use the v1 EGADSR
222 pEG1 = &EGADSR1;
223 EGADSR1.trigger(uint(RgnInfo.EG1PreAttack),
224 RgnInfo.EG1Attack,
225 RgnInfo.EG1Hold,
226 RgnInfo.EG1Decay1,
227 uint(RgnInfo.EG1Sustain),
228 RgnInfo.EG1Release,
229 sampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE);
230 }
231
232 double Voice::GetEG2ControllerValue(uint8_t MIDIKeyVelocity) {
233 /*double eg2controllervalue = 0;
234 switch (pRegion->EG2Controller.type) {
235 case ::gig::eg2_ctrl_t::type_none: // no controller defined
236 eg2controllervalue = 0;
237 break;
238 case ::gig::eg2_ctrl_t::type_channelaftertouch:
239 eg2controllervalue = GetSfzEngineChannel()->ControllerTable[128];
240 break;
241 case ::gig::eg2_ctrl_t::type_velocity:
242 eg2controllervalue = MIDIKeyVelocity;
243 break;
244 case ::gig::eg2_ctrl_t::type_controlchange: // MIDI control change controller
245 eg2controllervalue = GetSfzEngineChannel()->ControllerTable[pRegion->EG2Controller.controller_number];
246 break;
247 }
248 if (pRegion->EG2ControllerInvert) eg2controllervalue = 127 - eg2controllervalue;
249
250 return eg2controllervalue;*/ // TODO: ^^^
251 return 0;
252 }
253
254 Voice::EGInfo Voice::CalculateEG2ControllerInfluence(double eg2ControllerValue) {
255 /*EGInfo eg;
256 eg.Attack = (pRegion->EG2ControllerAttackInfluence) ? 1 + 0.00775 * (double) (1 << pRegion->EG2ControllerAttackInfluence) * eg2ControllerValue : 1.0;
257 eg.Decay = (pRegion->EG2ControllerDecayInfluence) ? 1 + 0.00775 * (double) (1 << pRegion->EG2ControllerDecayInfluence) * eg2ControllerValue : 1.0;
258 eg.Release = (pRegion->EG2ControllerReleaseInfluence) ? 1 + 0.00775 * (double) (1 << pRegion->EG2ControllerReleaseInfluence) * eg2ControllerValue : 1.0;
259
260 return eg;*/ // TODO: ^^^
261 EGInfo eg;
262 eg.Attack = 1.0;
263 eg.Decay = 1.0;
264 eg.Release = 1.0;
265 return eg;
266 }
267
268 void Voice::InitLFO1() {
269 /*uint16_t lfo1_internal_depth;
270 switch (pRegion->LFO1Controller) {
271 case ::gig::lfo1_ctrl_internal:
272 lfo1_internal_depth = pRegion->LFO1InternalDepth;
273 pLFO1->ExtController = 0; // no external controller
274 bLFO1Enabled = (lfo1_internal_depth > 0);
275 break;
276 case ::gig::lfo1_ctrl_modwheel:
277 lfo1_internal_depth = 0;
278 pLFO1->ExtController = 1; // MIDI controller 1
279 bLFO1Enabled = (pRegion->LFO1ControlDepth > 0);
280 break;
281 case ::gig::lfo1_ctrl_breath:
282 lfo1_internal_depth = 0;
283 pLFO1->ExtController = 2; // MIDI controller 2
284 bLFO1Enabled = (pRegion->LFO1ControlDepth > 0);
285 break;
286 case ::gig::lfo1_ctrl_internal_modwheel:
287 lfo1_internal_depth = pRegion->LFO1InternalDepth;
288 pLFO1->ExtController = 1; // MIDI controller 1
289 bLFO1Enabled = (lfo1_internal_depth > 0 || pRegion->LFO1ControlDepth > 0);
290 break;
291 case ::gig::lfo1_ctrl_internal_breath:
292 lfo1_internal_depth = pRegion->LFO1InternalDepth;
293 pLFO1->ExtController = 2; // MIDI controller 2
294 bLFO1Enabled = (lfo1_internal_depth > 0 || pRegion->LFO1ControlDepth > 0);
295 break;
296 default:
297 lfo1_internal_depth = 0;
298 pLFO1->ExtController = 0; // no external controller
299 bLFO1Enabled = false;
300 }
301 if (bLFO1Enabled) {
302 pLFO1->trigger(pRegion->LFO1Frequency,
303 start_level_min,
304 lfo1_internal_depth,
305 pRegion->LFO1ControlDepth,
306 pRegion->LFO1FlipPhase,
307 pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE);
308 pLFO1->update(pLFO1->ExtController ? GetSfzEngineChannel()->ControllerTable[pLFO1->ExtController] : 0);
309 }*/ // TODO: ^^^
310 bLFO1Enabled = false;
311 }
312
313 void Voice::InitLFO2() {
314 /*uint16_t lfo2_internal_depth;
315 switch (pRegion->LFO2Controller) {
316 case ::gig::lfo2_ctrl_internal:
317 lfo2_internal_depth = pRegion->LFO2InternalDepth;
318 pLFO2->ExtController = 0; // no external controller
319 bLFO2Enabled = (lfo2_internal_depth > 0);
320 break;
321 case ::gig::lfo2_ctrl_modwheel:
322 lfo2_internal_depth = 0;
323 pLFO2->ExtController = 1; // MIDI controller 1
324 bLFO2Enabled = (pRegion->LFO2ControlDepth > 0);
325 break;
326 case ::gig::lfo2_ctrl_foot:
327 lfo2_internal_depth = 0;
328 pLFO2->ExtController = 4; // MIDI controller 4
329 bLFO2Enabled = (pRegion->LFO2ControlDepth > 0);
330 break;
331 case ::gig::lfo2_ctrl_internal_modwheel:
332 lfo2_internal_depth = pRegion->LFO2InternalDepth;
333 pLFO2->ExtController = 1; // MIDI controller 1
334 bLFO2Enabled = (lfo2_internal_depth > 0 || pRegion->LFO2ControlDepth > 0);
335 break;
336 case ::gig::lfo2_ctrl_internal_foot:
337 lfo2_internal_depth = pRegion->LFO2InternalDepth;
338 pLFO2->ExtController = 4; // MIDI controller 4
339 bLFO2Enabled = (lfo2_internal_depth > 0 || pRegion->LFO2ControlDepth > 0);
340 break;
341 default:
342 lfo2_internal_depth = 0;
343 pLFO2->ExtController = 0; // no external controller
344 bLFO2Enabled = false;
345 }
346 if (bLFO2Enabled) {
347 pLFO2->trigger(pRegion->LFO2Frequency,
348 start_level_max,
349 lfo2_internal_depth,
350 pRegion->LFO2ControlDepth,
351 pRegion->LFO2FlipPhase,
352 pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE);
353 pLFO2->update(pLFO2->ExtController ? GetSfzEngineChannel()->ControllerTable[pLFO2->ExtController] : 0);
354 }*/ // TODO: ^^^
355 bLFO2Enabled = false;
356 }
357
358 void Voice::InitLFO3() {
359 /*uint16_t lfo3_internal_depth;
360 switch (pRegion->LFO3Controller) {
361 case ::gig::lfo3_ctrl_internal:
362 lfo3_internal_depth = pRegion->LFO3InternalDepth;
363 pLFO3->ExtController = 0; // no external controller
364 bLFO3Enabled = (lfo3_internal_depth > 0);
365 break;
366 case ::gig::lfo3_ctrl_modwheel:
367 lfo3_internal_depth = 0;
368 pLFO3->ExtController = 1; // MIDI controller 1
369 bLFO3Enabled = (pRegion->LFO3ControlDepth > 0);
370 break;
371 case ::gig::lfo3_ctrl_aftertouch:
372 lfo3_internal_depth = 0;
373 pLFO3->ExtController = 128;
374 bLFO3Enabled = true;
375 break;
376 case ::gig::lfo3_ctrl_internal_modwheel:
377 lfo3_internal_depth = pRegion->LFO3InternalDepth;
378 pLFO3->ExtController = 1; // MIDI controller 1
379 bLFO3Enabled = (lfo3_internal_depth > 0 || pRegion->LFO3ControlDepth > 0);
380 break;
381 case ::gig::lfo3_ctrl_internal_aftertouch:
382 lfo3_internal_depth = pRegion->LFO3InternalDepth;
383 pLFO1->ExtController = 128;
384 bLFO3Enabled = (lfo3_internal_depth > 0 || pRegion->LFO3ControlDepth > 0);
385 break;
386 default:
387 lfo3_internal_depth = 0;
388 pLFO3->ExtController = 0; // no external controller
389 bLFO3Enabled = false;
390 }
391 if (bLFO3Enabled) {
392 pLFO3->trigger(pRegion->LFO3Frequency,
393 start_level_mid,
394 lfo3_internal_depth,
395 pRegion->LFO3ControlDepth,
396 false,
397 pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE);
398 pLFO3->update(pLFO3->ExtController ? GetSfzEngineChannel()->ControllerTable[pLFO3->ExtController] : 0);
399 }*/ // TODO: ^^^
400 bLFO3Enabled = false;
401 }
402
403 float Voice::CalculateCutoffBase(uint8_t MIDIKeyVelocity) {
404 /*float cutoff = pRegion->GetVelocityCutoff(MIDIKeyVelocity);
405 if (pRegion->VCFKeyboardTracking) {
406 cutoff *= exp((MIDIKeyVelocity - pRegion->VCFKeyboardTrackingBreakpoint) * 0.057762265f); // (ln(2) / 12)
407 }
408 return cutoff;*/ // TODO: ^^^
409 return 1.0f;
410 }
411
412 float Voice::CalculateFinalCutoff(float cutoffBase) {
413 /*int cvalue;
414 if (VCFCutoffCtrl.controller) {
415 cvalue = GetSfzEngineChannel()->ControllerTable[VCFCutoffCtrl.controller];
416 if (pRegion->VCFCutoffControllerInvert) cvalue = 127 - cvalue;
417 // VCFVelocityScale in this case means Minimum cutoff
418 if (cvalue < pRegion->VCFVelocityScale) cvalue = pRegion->VCFVelocityScale;
419 }
420 else {
421 cvalue = pRegion->VCFCutoff;
422 }
423 float fco = cutoffBase * float(cvalue);
424 if (fco > 127.0f) fco = 127.0f;
425
426 return fco;*/ // TODO: ^^^
427 return 127.0f;
428 }
429
430 uint8_t Voice::GetVCFCutoffCtrl() {
431 /*uint8_t ctrl;
432 switch (pRegion->VCFCutoffController) {
433 case ::gig::vcf_cutoff_ctrl_modwheel:
434 ctrl = 1;
435 break;
436 case ::gig::vcf_cutoff_ctrl_effect1:
437 ctrl = 12;
438 break;
439 case ::gig::vcf_cutoff_ctrl_effect2:
440 ctrl = 13;
441 break;
442 case ::gig::vcf_cutoff_ctrl_breath:
443 ctrl = 2;
444 break;
445 case ::gig::vcf_cutoff_ctrl_foot:
446 ctrl = 4;
447 break;
448 case ::gig::vcf_cutoff_ctrl_sustainpedal:
449 ctrl = 64;
450 break;
451 case ::gig::vcf_cutoff_ctrl_softpedal:
452 ctrl = 67;
453 break;
454 case ::gig::vcf_cutoff_ctrl_genpurpose7:
455 ctrl = 82;
456 break;
457 case ::gig::vcf_cutoff_ctrl_genpurpose8:
458 ctrl = 83;
459 break;
460 case ::gig::vcf_cutoff_ctrl_aftertouch:
461 ctrl = 128;
462 break;
463 case ::gig::vcf_cutoff_ctrl_none:
464 default:
465 ctrl = 0;
466 break;
467 }
468
469 return ctrl;*/ // TODO: ^^^
470 return 0;
471 }
472
473 uint8_t Voice::GetVCFResonanceCtrl() {
474 /*uint8_t ctrl;
475 switch (pRegion->VCFResonanceController) {
476 case ::gig::vcf_res_ctrl_genpurpose3:
477 ctrl = 18;
478 break;
479 case ::gig::vcf_res_ctrl_genpurpose4:
480 ctrl = 19;
481 break;
482 case ::gig::vcf_res_ctrl_genpurpose5:
483 ctrl = 80;
484 break;
485 case ::gig::vcf_res_ctrl_genpurpose6:
486 ctrl = 81;
487 break;
488 case ::gig::vcf_res_ctrl_none:
489 default:
490 ctrl = 0;
491 }
492
493 return ctrl;*/ // TODO: ^^^
494 return 0;
495 }
496
497 float Voice::GetReleaseTriggerAttenuation(float noteLength) {
498 // pow(10, -rt_decay * noteLength / 20):
499 return expf(RgnInfo.ReleaseTriggerDecay * noteLength);
500 }
501
502 }} // namespace LinuxSampler::sfz

  ViewVC Help
Powered by ViewVC