/[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 2055 - (show annotations) (download)
Sat Jan 30 10:30:02 2010 UTC (14 years, 2 months ago) by persson
File size: 21472 byte(s)
* sfz engine: added support for v2 multiple stage envelope generators
* sfz engine: added a fine-tuned v1 envelope generator instead of
  using the one from the gig engine

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 ri.ReleaseTriggerDecay = 0;
99
100 return ri;
101 }
102
103 Voice::InstrumentInfo Voice::GetInstrumentInfo() {
104 InstrumentInfo ii;
105 ii.FineTune = 0; // TODO:
106 ii.PitchbendRange = 2; // TODO:
107
108 return ii;
109 }
110
111 double Voice::GetSampleAttenuation() {
112 return 1.0; // TODO:
113 }
114
115 double Voice::GetVelocityAttenuation(uint8_t MIDIKeyVelocity) {
116 return double(MIDIKeyVelocity) / 127.0f; // TODO:
117 }
118
119 double Voice::GetVelocityRelease(uint8_t MIDIKeyVelocity) {
120 return 0.9; // TODO:
121 }
122
123 void Voice::ProcessCCEvent(RTList<Event>::Iterator& itEvent) {
124 /*if (itEvent->Type == Event::type_control_change && itEvent->Param.CC.Controller) { // if (valid) MIDI control change event
125 if (pRegion->AttenuationController.type == ::gig::attenuation_ctrl_t::type_controlchange &&
126 itEvent->Param.CC.Controller == pRegion->AttenuationController.controller_number) {
127 CrossfadeSmoother.update(AbstractEngine::CrossfadeCurve[CrossfadeAttenuation(itEvent->Param.CC.Value)]);
128 }
129 }*/ // TODO: ^^^
130 }
131
132 void Voice::ProcessCutoffEvent(RTList<Event>::Iterator& itEvent) {
133 /*int ccvalue = itEvent->Param.CC.Value;
134 if (VCFCutoffCtrl.value == ccvalue) return;
135 VCFCutoffCtrl.value == ccvalue;
136 if (pRegion->VCFCutoffControllerInvert) ccvalue = 127 - ccvalue;
137 if (ccvalue < pRegion->VCFVelocityScale) ccvalue = pRegion->VCFVelocityScale;
138 float cutoff = CutoffBase * float(ccvalue);
139 if (cutoff > 127.0f) cutoff = 127.0f;
140
141 VCFCutoffCtrl.fvalue = cutoff; // needed for initialization of fFinalCutoff next time
142 fFinalCutoff = cutoff;*/ // TODO: ^^^
143 }
144
145 double Voice::CalculateCrossfadeVolume(uint8_t MIDIKeyVelocity) {
146 /*float crossfadeVolume;
147 switch (pRegion->AttenuationController.type) {
148 case ::gig::attenuation_ctrl_t::type_channelaftertouch:
149 crossfadeVolume = Engine::CrossfadeCurve[CrossfadeAttenuation(GetSfzEngineChannel()->ControllerTable[128])];
150 break;
151 case ::gig::attenuation_ctrl_t::type_velocity:
152 crossfadeVolume = Engine::CrossfadeCurve[CrossfadeAttenuation(MIDIKeyVelocity)];
153 break;
154 case ::gig::attenuation_ctrl_t::type_controlchange: //FIXME: currently not sample accurate
155 crossfadeVolume = Engine::CrossfadeCurve[CrossfadeAttenuation(GetSfzEngineChannel()->ControllerTable[pRegion->AttenuationController.controller_number])];
156 break;
157 case ::gig::attenuation_ctrl_t::type_none: // no crossfade defined
158 default:
159 crossfadeVolume = 1.0f;
160 }
161
162 return crossfadeVolume;*/ // TODO: ^^^
163 return 1.0f;
164 }
165
166 double Voice::GetEG1ControllerValue(uint8_t MIDIKeyVelocity) {
167 /*double eg1controllervalue = 0;
168 switch (pRegion->EG1Controller.type) {
169 case ::gig::eg1_ctrl_t::type_none: // no controller defined
170 eg1controllervalue = 0;
171 break;
172 case ::gig::eg1_ctrl_t::type_channelaftertouch:
173 eg1controllervalue = GetSfzEngineChannel()->ControllerTable[128];
174 break;
175 case ::gig::eg1_ctrl_t::type_velocity:
176 eg1controllervalue = MIDIKeyVelocity;
177 break;
178 case ::gig::eg1_ctrl_t::type_controlchange: // MIDI control change controller
179 eg1controllervalue = GetSfzEngineChannel()->ControllerTable[pRegion->EG1Controller.controller_number];
180 break;
181 }
182 if (pRegion->EG1ControllerInvert) eg1controllervalue = 127 - eg1controllervalue;
183
184 return eg1controllervalue;*/ // TODO: ^^^
185 return 0;
186 }
187
188 Voice::EGInfo Voice::CalculateEG1ControllerInfluence(double eg1ControllerValue) {
189 /*EGInfo eg;
190 // (eg1attack is different from the others)
191 eg.Attack = (pRegion->EG1ControllerAttackInfluence) ?
192 1 + 0.031 * (double) (pRegion->EG1ControllerAttackInfluence == 1 ?
193 1 : 1 << pRegion->EG1ControllerAttackInfluence) * eg1ControllerValue : 1.0;
194 eg.Decay = (pRegion->EG1ControllerDecayInfluence) ? 1 + 0.00775 * (double) (1 << pRegion->EG1ControllerDecayInfluence) * eg1ControllerValue : 1.0;
195 eg.Release = (pRegion->EG1ControllerReleaseInfluence) ? 1 + 0.00775 * (double) (1 << pRegion->EG1ControllerReleaseInfluence) * eg1ControllerValue : 1.0;
196
197 return eg;*/ // TODO: ^^^
198 EGInfo eg;
199 eg.Attack = 1.0;
200 eg.Decay = 1.0;
201 eg.Release = 1.0;
202 return eg;
203 }
204
205 void Voice::TriggerEG1(const EGInfo& egInfo, double velrelease, double velocityAttenuation, uint sampleRate, uint8_t velocity) {
206
207 // TODO: controller modulation
208
209 // first check if there is a v2 EG for amplitude
210 for (int i = 0 ; i < pRegion->eg.size() ; i++) {
211 if (pRegion->eg[i].amplitude > 0) {
212 // TODO: actually use the value of the amplitude parameter
213 pEG1 = &EG1;
214 EG1.trigger(pRegion->eg[i], sampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE, velocity);
215 return;
216 }
217 }
218
219 // otherwise use the v1 EGADSR
220 pEG1 = &EGADSR1;
221 EGADSR1.trigger(uint(RgnInfo.EG1PreAttack),
222 RgnInfo.EG1Attack,
223 RgnInfo.EG1Hold,
224 RgnInfo.EG1Decay1,
225 uint(RgnInfo.EG1Sustain),
226 RgnInfo.EG1Release,
227 sampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE);
228 }
229
230 double Voice::GetEG2ControllerValue(uint8_t MIDIKeyVelocity) {
231 /*double eg2controllervalue = 0;
232 switch (pRegion->EG2Controller.type) {
233 case ::gig::eg2_ctrl_t::type_none: // no controller defined
234 eg2controllervalue = 0;
235 break;
236 case ::gig::eg2_ctrl_t::type_channelaftertouch:
237 eg2controllervalue = GetSfzEngineChannel()->ControllerTable[128];
238 break;
239 case ::gig::eg2_ctrl_t::type_velocity:
240 eg2controllervalue = MIDIKeyVelocity;
241 break;
242 case ::gig::eg2_ctrl_t::type_controlchange: // MIDI control change controller
243 eg2controllervalue = GetSfzEngineChannel()->ControllerTable[pRegion->EG2Controller.controller_number];
244 break;
245 }
246 if (pRegion->EG2ControllerInvert) eg2controllervalue = 127 - eg2controllervalue;
247
248 return eg2controllervalue;*/ // TODO: ^^^
249 return 0;
250 }
251
252 Voice::EGInfo Voice::CalculateEG2ControllerInfluence(double eg2ControllerValue) {
253 /*EGInfo eg;
254 eg.Attack = (pRegion->EG2ControllerAttackInfluence) ? 1 + 0.00775 * (double) (1 << pRegion->EG2ControllerAttackInfluence) * eg2ControllerValue : 1.0;
255 eg.Decay = (pRegion->EG2ControllerDecayInfluence) ? 1 + 0.00775 * (double) (1 << pRegion->EG2ControllerDecayInfluence) * eg2ControllerValue : 1.0;
256 eg.Release = (pRegion->EG2ControllerReleaseInfluence) ? 1 + 0.00775 * (double) (1 << pRegion->EG2ControllerReleaseInfluence) * eg2ControllerValue : 1.0;
257
258 return eg;*/ // TODO: ^^^
259 EGInfo eg;
260 eg.Attack = 1.0;
261 eg.Decay = 1.0;
262 eg.Release = 1.0;
263 return eg;
264 }
265
266 void Voice::InitLFO1() {
267 /*uint16_t lfo1_internal_depth;
268 switch (pRegion->LFO1Controller) {
269 case ::gig::lfo1_ctrl_internal:
270 lfo1_internal_depth = pRegion->LFO1InternalDepth;
271 pLFO1->ExtController = 0; // no external controller
272 bLFO1Enabled = (lfo1_internal_depth > 0);
273 break;
274 case ::gig::lfo1_ctrl_modwheel:
275 lfo1_internal_depth = 0;
276 pLFO1->ExtController = 1; // MIDI controller 1
277 bLFO1Enabled = (pRegion->LFO1ControlDepth > 0);
278 break;
279 case ::gig::lfo1_ctrl_breath:
280 lfo1_internal_depth = 0;
281 pLFO1->ExtController = 2; // MIDI controller 2
282 bLFO1Enabled = (pRegion->LFO1ControlDepth > 0);
283 break;
284 case ::gig::lfo1_ctrl_internal_modwheel:
285 lfo1_internal_depth = pRegion->LFO1InternalDepth;
286 pLFO1->ExtController = 1; // MIDI controller 1
287 bLFO1Enabled = (lfo1_internal_depth > 0 || pRegion->LFO1ControlDepth > 0);
288 break;
289 case ::gig::lfo1_ctrl_internal_breath:
290 lfo1_internal_depth = pRegion->LFO1InternalDepth;
291 pLFO1->ExtController = 2; // MIDI controller 2
292 bLFO1Enabled = (lfo1_internal_depth > 0 || pRegion->LFO1ControlDepth > 0);
293 break;
294 default:
295 lfo1_internal_depth = 0;
296 pLFO1->ExtController = 0; // no external controller
297 bLFO1Enabled = false;
298 }
299 if (bLFO1Enabled) {
300 pLFO1->trigger(pRegion->LFO1Frequency,
301 start_level_min,
302 lfo1_internal_depth,
303 pRegion->LFO1ControlDepth,
304 pRegion->LFO1FlipPhase,
305 pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE);
306 pLFO1->update(pLFO1->ExtController ? GetSfzEngineChannel()->ControllerTable[pLFO1->ExtController] : 0);
307 }*/ // TODO: ^^^
308 bLFO1Enabled = false;
309 }
310
311 void Voice::InitLFO2() {
312 /*uint16_t lfo2_internal_depth;
313 switch (pRegion->LFO2Controller) {
314 case ::gig::lfo2_ctrl_internal:
315 lfo2_internal_depth = pRegion->LFO2InternalDepth;
316 pLFO2->ExtController = 0; // no external controller
317 bLFO2Enabled = (lfo2_internal_depth > 0);
318 break;
319 case ::gig::lfo2_ctrl_modwheel:
320 lfo2_internal_depth = 0;
321 pLFO2->ExtController = 1; // MIDI controller 1
322 bLFO2Enabled = (pRegion->LFO2ControlDepth > 0);
323 break;
324 case ::gig::lfo2_ctrl_foot:
325 lfo2_internal_depth = 0;
326 pLFO2->ExtController = 4; // MIDI controller 4
327 bLFO2Enabled = (pRegion->LFO2ControlDepth > 0);
328 break;
329 case ::gig::lfo2_ctrl_internal_modwheel:
330 lfo2_internal_depth = pRegion->LFO2InternalDepth;
331 pLFO2->ExtController = 1; // MIDI controller 1
332 bLFO2Enabled = (lfo2_internal_depth > 0 || pRegion->LFO2ControlDepth > 0);
333 break;
334 case ::gig::lfo2_ctrl_internal_foot:
335 lfo2_internal_depth = pRegion->LFO2InternalDepth;
336 pLFO2->ExtController = 4; // MIDI controller 4
337 bLFO2Enabled = (lfo2_internal_depth > 0 || pRegion->LFO2ControlDepth > 0);
338 break;
339 default:
340 lfo2_internal_depth = 0;
341 pLFO2->ExtController = 0; // no external controller
342 bLFO2Enabled = false;
343 }
344 if (bLFO2Enabled) {
345 pLFO2->trigger(pRegion->LFO2Frequency,
346 start_level_max,
347 lfo2_internal_depth,
348 pRegion->LFO2ControlDepth,
349 pRegion->LFO2FlipPhase,
350 pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE);
351 pLFO2->update(pLFO2->ExtController ? GetSfzEngineChannel()->ControllerTable[pLFO2->ExtController] : 0);
352 }*/ // TODO: ^^^
353 bLFO2Enabled = false;
354 }
355
356 void Voice::InitLFO3() {
357 /*uint16_t lfo3_internal_depth;
358 switch (pRegion->LFO3Controller) {
359 case ::gig::lfo3_ctrl_internal:
360 lfo3_internal_depth = pRegion->LFO3InternalDepth;
361 pLFO3->ExtController = 0; // no external controller
362 bLFO3Enabled = (lfo3_internal_depth > 0);
363 break;
364 case ::gig::lfo3_ctrl_modwheel:
365 lfo3_internal_depth = 0;
366 pLFO3->ExtController = 1; // MIDI controller 1
367 bLFO3Enabled = (pRegion->LFO3ControlDepth > 0);
368 break;
369 case ::gig::lfo3_ctrl_aftertouch:
370 lfo3_internal_depth = 0;
371 pLFO3->ExtController = 128;
372 bLFO3Enabled = true;
373 break;
374 case ::gig::lfo3_ctrl_internal_modwheel:
375 lfo3_internal_depth = pRegion->LFO3InternalDepth;
376 pLFO3->ExtController = 1; // MIDI controller 1
377 bLFO3Enabled = (lfo3_internal_depth > 0 || pRegion->LFO3ControlDepth > 0);
378 break;
379 case ::gig::lfo3_ctrl_internal_aftertouch:
380 lfo3_internal_depth = pRegion->LFO3InternalDepth;
381 pLFO1->ExtController = 128;
382 bLFO3Enabled = (lfo3_internal_depth > 0 || pRegion->LFO3ControlDepth > 0);
383 break;
384 default:
385 lfo3_internal_depth = 0;
386 pLFO3->ExtController = 0; // no external controller
387 bLFO3Enabled = false;
388 }
389 if (bLFO3Enabled) {
390 pLFO3->trigger(pRegion->LFO3Frequency,
391 start_level_mid,
392 lfo3_internal_depth,
393 pRegion->LFO3ControlDepth,
394 false,
395 pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE);
396 pLFO3->update(pLFO3->ExtController ? GetSfzEngineChannel()->ControllerTable[pLFO3->ExtController] : 0);
397 }*/ // TODO: ^^^
398 bLFO3Enabled = false;
399 }
400
401 float Voice::CalculateCutoffBase(uint8_t MIDIKeyVelocity) {
402 /*float cutoff = pRegion->GetVelocityCutoff(MIDIKeyVelocity);
403 if (pRegion->VCFKeyboardTracking) {
404 cutoff *= exp((MIDIKeyVelocity - pRegion->VCFKeyboardTrackingBreakpoint) * 0.057762265f); // (ln(2) / 12)
405 }
406 return cutoff;*/ // TODO: ^^^
407 return 1.0f;
408 }
409
410 float Voice::CalculateFinalCutoff(float cutoffBase) {
411 /*int cvalue;
412 if (VCFCutoffCtrl.controller) {
413 cvalue = GetSfzEngineChannel()->ControllerTable[VCFCutoffCtrl.controller];
414 if (pRegion->VCFCutoffControllerInvert) cvalue = 127 - cvalue;
415 // VCFVelocityScale in this case means Minimum cutoff
416 if (cvalue < pRegion->VCFVelocityScale) cvalue = pRegion->VCFVelocityScale;
417 }
418 else {
419 cvalue = pRegion->VCFCutoff;
420 }
421 float fco = cutoffBase * float(cvalue);
422 if (fco > 127.0f) fco = 127.0f;
423
424 return fco;*/ // TODO: ^^^
425 return 127.0f;
426 }
427
428 uint8_t Voice::GetVCFCutoffCtrl() {
429 /*uint8_t ctrl;
430 switch (pRegion->VCFCutoffController) {
431 case ::gig::vcf_cutoff_ctrl_modwheel:
432 ctrl = 1;
433 break;
434 case ::gig::vcf_cutoff_ctrl_effect1:
435 ctrl = 12;
436 break;
437 case ::gig::vcf_cutoff_ctrl_effect2:
438 ctrl = 13;
439 break;
440 case ::gig::vcf_cutoff_ctrl_breath:
441 ctrl = 2;
442 break;
443 case ::gig::vcf_cutoff_ctrl_foot:
444 ctrl = 4;
445 break;
446 case ::gig::vcf_cutoff_ctrl_sustainpedal:
447 ctrl = 64;
448 break;
449 case ::gig::vcf_cutoff_ctrl_softpedal:
450 ctrl = 67;
451 break;
452 case ::gig::vcf_cutoff_ctrl_genpurpose7:
453 ctrl = 82;
454 break;
455 case ::gig::vcf_cutoff_ctrl_genpurpose8:
456 ctrl = 83;
457 break;
458 case ::gig::vcf_cutoff_ctrl_aftertouch:
459 ctrl = 128;
460 break;
461 case ::gig::vcf_cutoff_ctrl_none:
462 default:
463 ctrl = 0;
464 break;
465 }
466
467 return ctrl;*/ // TODO: ^^^
468 return 0;
469 }
470
471 uint8_t Voice::GetVCFResonanceCtrl() {
472 /*uint8_t ctrl;
473 switch (pRegion->VCFResonanceController) {
474 case ::gig::vcf_res_ctrl_genpurpose3:
475 ctrl = 18;
476 break;
477 case ::gig::vcf_res_ctrl_genpurpose4:
478 ctrl = 19;
479 break;
480 case ::gig::vcf_res_ctrl_genpurpose5:
481 ctrl = 80;
482 break;
483 case ::gig::vcf_res_ctrl_genpurpose6:
484 ctrl = 81;
485 break;
486 case ::gig::vcf_res_ctrl_none:
487 default:
488 ctrl = 0;
489 }
490
491 return ctrl;*/ // TODO: ^^^
492 return 0;
493 }
494
495 }} // namespace LinuxSampler::sfz

  ViewVC Help
Powered by ViewVC