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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2203 - (show annotations) (download)
Sat Jul 9 16:44:27 2011 UTC (7 years ago) by persson
File size: 22053 byte(s)
* sf2 engine: fine-tuned amplitude EG (by switching from gig to sfz EG)
* sfz engine: added support for EG hold (ampeg_hold)
* Mac OS X: made it possible to specify plugin installation dir to
  configure

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 - 2011 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 sf2 {
31
32 Voice::Voice() {
33 pEngine = NULL;
34 pEG1 = &EG1;
35 pEG2 = &EG2;
36 }
37
38 Voice::~Voice() {
39
40 }
41
42 EngineChannel* Voice::GetSf2EngineChannel() {
43 return static_cast<EngineChannel*>(pEngineChannel);
44 }
45
46 void Voice::SetEngine(LinuxSampler::Engine* pEngine) {
47 Engine* engine = static_cast<Engine*>(pEngine);
48 this->pEngine = engine;
49 this->pDiskThread = engine->pDiskThread;
50 dmsg(6,("Voice::SetEngine()\n"));
51 }
52
53 Voice::SampleInfo Voice::GetSampleInfo() {
54 SampleInfo si;
55 si.SampleRate = pSample->SampleRate;
56 si.ChannelCount = pSample->GetChannelCount();
57 si.FrameSize = pSample->GetFrameSize();
58 si.BitDepth = (pSample->GetFrameSize() / pSample->GetChannelCount()) * 8;
59 si.TotalFrameCount = pSample->GetTotalFrameCount();
60
61 si.HasLoops = pRegion->HasLoop;
62 si.LoopStart = (si.HasLoops) ? pRegion->LoopStart : 0;
63 si.LoopLength = (si.HasLoops) ? ((pRegion->LoopEnd) - pRegion->LoopStart): 0;
64 si.LoopPlayCount = 0; // TODO:
65 si.Unpitched = pSample->IsUnpitched();
66
67 return si;
68 }
69
70 Voice::RegionInfo Voice::GetRegionInfo() {
71 ::sf2::Region* reg = NULL;
72 ::sf2::Preset* preset = GetSf2EngineChannel()->pInstrument;
73 for (int i = 0; i < preset->GetRegionCount(); i++) { // TODO: some optimization?
74 if (preset->GetRegion(i)->pInstrument == pRegion->GetParentInstrument()) {
75 reg = preset->GetRegion(i); // TODO: Can the instrument belong to more than one preset region?
76 break;
77 }
78 }
79 pPresetRegion = reg;
80
81 RegionInfo ri;
82 ri.UnityNote = pRegion->GetUnityNote();
83 ri.FineTune = pRegion->GetFineTune(reg) + (pRegion->GetCoarseTune(reg) * 100);
84 ri.Pan = pRegion->GetPan(reg);
85 ri.SampleStartOffset = pRegion->startAddrsOffset + pRegion->startAddrsCoarseOffset;
86
87 // filter cutoff frequency
88 ri.EG2PreAttack = 1000;
89 ri.EG2Attack = pRegion->GetEG2Attack(reg);
90 //ri.EG2Hold = pRegion->EG2Hold; // TODO:
91 ri.EG2Decay1 = pRegion->GetEG2Decay(reg);
92 ri.EG2Decay2 = pRegion->GetEG2Decay(reg);
93 ri.EG2Sustain = pRegion->GetEG2Sustain(reg);
94 ri.EG2InfiniteSustain = true;
95 ri.EG2Release = pRegion->GetEG2Release(reg);
96
97 // sample pitch
98 ri.EG3Attack = 0; // TODO:
99 ri.EG3Depth = 0; // TODO:
100 ri.VCFEnabled = false; // TODO:
101 ri.VCFType = Filter::vcf_type_2p_lowpass; // TODO:
102 ri.VCFResonance = 0; // TODO:
103
104 ri.ReleaseTriggerDecay = 0; // TODO:
105
106 return ri;
107 }
108
109 Voice::InstrumentInfo Voice::GetInstrumentInfo() {
110 InstrumentInfo ii;
111 ii.FineTune = 0; // TODO:
112 ii.PitchbendRange = 2; // TODO:
113
114 return ii;
115 }
116
117 double Voice::GetSampleAttenuation() {
118 return 1.0; // TODO:
119 }
120
121 double Voice::GetVelocityAttenuation(uint8_t MIDIKeyVelocity) {
122 return double(MIDIKeyVelocity) / 127.0f; // TODO:
123 }
124
125 double Voice::GetVelocityRelease(uint8_t MIDIKeyVelocity) {
126 return 0.9; // TODO:
127 }
128
129 void Voice::ProcessCCEvent(RTList<Event>::Iterator& itEvent) {
130 /*if (itEvent->Type == Event::type_control_change && itEvent->Param.CC.Controller) { // if (valid) MIDI control change event
131 if (pRegion->AttenuationController.type == ::gig::attenuation_ctrl_t::type_controlchange &&
132 itEvent->Param.CC.Controller == pRegion->AttenuationController.controller_number) {
133 CrossfadeSmoother.update(AbstractEngine::CrossfadeCurve[CrossfadeAttenuation(itEvent->Param.CC.Value)]);
134 }
135 }*/ // TODO: ^^^
136 }
137
138 void Voice::ProcessCutoffEvent(RTList<Event>::Iterator& itEvent) {
139 /*int ccvalue = itEvent->Param.CC.Value;
140 if (VCFCutoffCtrl.value == ccvalue) return;
141 VCFCutoffCtrl.value == ccvalue;
142 if (pRegion->VCFCutoffControllerInvert) ccvalue = 127 - ccvalue;
143 if (ccvalue < pRegion->VCFVelocityScale) ccvalue = pRegion->VCFVelocityScale;
144 float cutoff = CutoffBase * float(ccvalue);
145 if (cutoff > 127.0f) cutoff = 127.0f;
146
147 VCFCutoffCtrl.fvalue = cutoff; // needed for initialization of fFinalCutoff next time
148 fFinalCutoff = cutoff;*/ // TODO: ^^^
149 }
150
151 double Voice::CalculateCrossfadeVolume(uint8_t MIDIKeyVelocity) {
152 /*float crossfadeVolume;
153 switch (pRegion->AttenuationController.type) {
154 case ::gig::attenuation_ctrl_t::type_channelaftertouch:
155 crossfadeVolume = Engine::CrossfadeCurve[CrossfadeAttenuation(GetSf2EngineChannel()->ControllerTable[128])];
156 break;
157 case ::gig::attenuation_ctrl_t::type_velocity:
158 crossfadeVolume = Engine::CrossfadeCurve[CrossfadeAttenuation(MIDIKeyVelocity)];
159 break;
160 case ::gig::attenuation_ctrl_t::type_controlchange: //FIXME: currently not sample accurate
161 crossfadeVolume = Engine::CrossfadeCurve[CrossfadeAttenuation(GetSf2EngineChannel()->ControllerTable[pRegion->AttenuationController.controller_number])];
162 break;
163 case ::gig::attenuation_ctrl_t::type_none: // no crossfade defined
164 default:
165 crossfadeVolume = 1.0f;
166 }
167
168 return crossfadeVolume;*/ // TODO: ^^^
169 return 1.0f;
170 }
171
172 double Voice::GetEG1ControllerValue(uint8_t MIDIKeyVelocity) {
173 /*double eg1controllervalue = 0;
174 switch (pRegion->EG1Controller.type) {
175 case ::gig::eg1_ctrl_t::type_none: // no controller defined
176 eg1controllervalue = 0;
177 break;
178 case ::gig::eg1_ctrl_t::type_channelaftertouch:
179 eg1controllervalue = GetSf2EngineChannel()->ControllerTable[128];
180 break;
181 case ::gig::eg1_ctrl_t::type_velocity:
182 eg1controllervalue = MIDIKeyVelocity;
183 break;
184 case ::gig::eg1_ctrl_t::type_controlchange: // MIDI control change controller
185 eg1controllervalue = GetSf2EngineChannel()->ControllerTable[pRegion->EG1Controller.controller_number];
186 break;
187 }
188 if (pRegion->EG1ControllerInvert) eg1controllervalue = 127 - eg1controllervalue;
189
190 return eg1controllervalue;*/ // TODO: ^^^
191 return 0;
192 }
193
194 Voice::EGInfo Voice::CalculateEG1ControllerInfluence(double eg1ControllerValue) {
195 /*EGInfo eg;
196 // (eg1attack is different from the others)
197 eg.Attack = (pRegion->EG1ControllerAttackInfluence) ?
198 1 + 0.031 * (double) (pRegion->EG1ControllerAttackInfluence == 1 ?
199 1 : 1 << pRegion->EG1ControllerAttackInfluence) * eg1ControllerValue : 1.0;
200 eg.Decay = (pRegion->EG1ControllerDecayInfluence) ? 1 + 0.00775 * (double) (1 << pRegion->EG1ControllerDecayInfluence) * eg1ControllerValue : 1.0;
201 eg.Release = (pRegion->EG1ControllerReleaseInfluence) ? 1 + 0.00775 * (double) (1 << pRegion->EG1ControllerReleaseInfluence) * eg1ControllerValue : 1.0;
202
203 return eg;*/ // TODO: ^^^
204 EGInfo eg;
205 eg.Attack = 1.0;
206 eg.Decay = 1.0;
207 eg.Release = 1.0;
208 return eg;
209 }
210
211 void Voice::TriggerEG1(const EGInfo& egInfo, double velrelease, double velocityAttenuation, uint sampleRate, uint8_t velocity) {
212 EG1.trigger(0,
213 pRegion->GetEG1Attack(pPresetRegion),
214 pRegion->GetEG1Hold(pPresetRegion),
215 pRegion->GetEG1Decay(pPresetRegion),
216 uint(pRegion->GetEG1Sustain(pPresetRegion)),
217 pRegion->GetEG1Release(pPresetRegion),
218 sampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE);
219 }
220
221 double Voice::GetEG2ControllerValue(uint8_t MIDIKeyVelocity) {
222 /*double eg2controllervalue = 0;
223 switch (pRegion->EG2Controller.type) {
224 case ::gig::eg2_ctrl_t::type_none: // no controller defined
225 eg2controllervalue = 0;
226 break;
227 case ::gig::eg2_ctrl_t::type_channelaftertouch:
228 eg2controllervalue = GetSf2EngineChannel()->ControllerTable[128];
229 break;
230 case ::gig::eg2_ctrl_t::type_velocity:
231 eg2controllervalue = MIDIKeyVelocity;
232 break;
233 case ::gig::eg2_ctrl_t::type_controlchange: // MIDI control change controller
234 eg2controllervalue = GetSf2EngineChannel()->ControllerTable[pRegion->EG2Controller.controller_number];
235 break;
236 }
237 if (pRegion->EG2ControllerInvert) eg2controllervalue = 127 - eg2controllervalue;
238
239 return eg2controllervalue;*/ // TODO: ^^^
240 return 0;
241 }
242
243 Voice::EGInfo Voice::CalculateEG2ControllerInfluence(double eg2ControllerValue) {
244 /*EGInfo eg;
245 eg.Attack = (pRegion->EG2ControllerAttackInfluence) ? 1 + 0.00775 * (double) (1 << pRegion->EG2ControllerAttackInfluence) * eg2ControllerValue : 1.0;
246 eg.Decay = (pRegion->EG2ControllerDecayInfluence) ? 1 + 0.00775 * (double) (1 << pRegion->EG2ControllerDecayInfluence) * eg2ControllerValue : 1.0;
247 eg.Release = (pRegion->EG2ControllerReleaseInfluence) ? 1 + 0.00775 * (double) (1 << pRegion->EG2ControllerReleaseInfluence) * eg2ControllerValue : 1.0;
248
249 return eg;*/ // TODO: ^^^
250 EGInfo eg;
251 eg.Attack = 1.0;
252 eg.Decay = 1.0;
253 eg.Release = 1.0;
254 return eg;
255 }
256
257 void Voice::TriggerEG2(const EGInfo& egInfo, double velrelease, double velocityAttenuation, uint sampleRate, uint8_t velocity) {
258 EG2.trigger(uint(RgnInfo.EG2PreAttack),
259 RgnInfo.EG2Attack * egInfo.Attack,
260 false,
261 RgnInfo.EG2Decay1 * egInfo.Decay * velrelease,
262 RgnInfo.EG2Decay2 * egInfo.Decay * velrelease,
263 RgnInfo.EG2InfiniteSustain,
264 uint(RgnInfo.EG2Sustain),
265 RgnInfo.EG2Release * egInfo.Release * velrelease,
266 velocityAttenuation,
267 sampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE);
268 }
269
270 void Voice::InitLFO1() {
271 /*uint16_t lfo1_internal_depth;
272 switch (pRegion->LFO1Controller) {
273 case ::gig::lfo1_ctrl_internal:
274 lfo1_internal_depth = pRegion->LFO1InternalDepth;
275 pLFO1->ExtController = 0; // no external controller
276 bLFO1Enabled = (lfo1_internal_depth > 0);
277 break;
278 case ::gig::lfo1_ctrl_modwheel:
279 lfo1_internal_depth = 0;
280 pLFO1->ExtController = 1; // MIDI controller 1
281 bLFO1Enabled = (pRegion->LFO1ControlDepth > 0);
282 break;
283 case ::gig::lfo1_ctrl_breath:
284 lfo1_internal_depth = 0;
285 pLFO1->ExtController = 2; // MIDI controller 2
286 bLFO1Enabled = (pRegion->LFO1ControlDepth > 0);
287 break;
288 case ::gig::lfo1_ctrl_internal_modwheel:
289 lfo1_internal_depth = pRegion->LFO1InternalDepth;
290 pLFO1->ExtController = 1; // MIDI controller 1
291 bLFO1Enabled = (lfo1_internal_depth > 0 || pRegion->LFO1ControlDepth > 0);
292 break;
293 case ::gig::lfo1_ctrl_internal_breath:
294 lfo1_internal_depth = pRegion->LFO1InternalDepth;
295 pLFO1->ExtController = 2; // MIDI controller 2
296 bLFO1Enabled = (lfo1_internal_depth > 0 || pRegion->LFO1ControlDepth > 0);
297 break;
298 default:
299 lfo1_internal_depth = 0;
300 pLFO1->ExtController = 0; // no external controller
301 bLFO1Enabled = false;
302 }
303 if (bLFO1Enabled) {
304 pLFO1->trigger(pRegion->LFO1Frequency,
305 start_level_min,
306 lfo1_internal_depth,
307 pRegion->LFO1ControlDepth,
308 pRegion->LFO1FlipPhase,
309 pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE);
310 pLFO1->update(pLFO1->ExtController ? GetSf2EngineChannel()->ControllerTable[pLFO1->ExtController] : 0);
311 }*/ // TODO: ^^^
312 bLFO1Enabled = false;
313 }
314
315 void Voice::InitLFO2() {
316 /*uint16_t lfo2_internal_depth;
317 switch (pRegion->LFO2Controller) {
318 case ::gig::lfo2_ctrl_internal:
319 lfo2_internal_depth = pRegion->LFO2InternalDepth;
320 pLFO2->ExtController = 0; // no external controller
321 bLFO2Enabled = (lfo2_internal_depth > 0);
322 break;
323 case ::gig::lfo2_ctrl_modwheel:
324 lfo2_internal_depth = 0;
325 pLFO2->ExtController = 1; // MIDI controller 1
326 bLFO2Enabled = (pRegion->LFO2ControlDepth > 0);
327 break;
328 case ::gig::lfo2_ctrl_foot:
329 lfo2_internal_depth = 0;
330 pLFO2->ExtController = 4; // MIDI controller 4
331 bLFO2Enabled = (pRegion->LFO2ControlDepth > 0);
332 break;
333 case ::gig::lfo2_ctrl_internal_modwheel:
334 lfo2_internal_depth = pRegion->LFO2InternalDepth;
335 pLFO2->ExtController = 1; // MIDI controller 1
336 bLFO2Enabled = (lfo2_internal_depth > 0 || pRegion->LFO2ControlDepth > 0);
337 break;
338 case ::gig::lfo2_ctrl_internal_foot:
339 lfo2_internal_depth = pRegion->LFO2InternalDepth;
340 pLFO2->ExtController = 4; // MIDI controller 4
341 bLFO2Enabled = (lfo2_internal_depth > 0 || pRegion->LFO2ControlDepth > 0);
342 break;
343 default:
344 lfo2_internal_depth = 0;
345 pLFO2->ExtController = 0; // no external controller
346 bLFO2Enabled = false;
347 }
348 if (bLFO2Enabled) {
349 pLFO2->trigger(pRegion->LFO2Frequency,
350 start_level_max,
351 lfo2_internal_depth,
352 pRegion->LFO2ControlDepth,
353 pRegion->LFO2FlipPhase,
354 pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE);
355 pLFO2->update(pLFO2->ExtController ? GetSf2EngineChannel()->ControllerTable[pLFO2->ExtController] : 0);
356 }*/ // TODO: ^^^
357 bLFO2Enabled = false;
358 }
359
360 void Voice::InitLFO3() {
361 /*uint16_t lfo3_internal_depth;
362 switch (pRegion->LFO3Controller) {
363 case ::gig::lfo3_ctrl_internal:
364 lfo3_internal_depth = pRegion->LFO3InternalDepth;
365 pLFO3->ExtController = 0; // no external controller
366 bLFO3Enabled = (lfo3_internal_depth > 0);
367 break;
368 case ::gig::lfo3_ctrl_modwheel:
369 lfo3_internal_depth = 0;
370 pLFO3->ExtController = 1; // MIDI controller 1
371 bLFO3Enabled = (pRegion->LFO3ControlDepth > 0);
372 break;
373 case ::gig::lfo3_ctrl_aftertouch:
374 lfo3_internal_depth = 0;
375 pLFO3->ExtController = 128;
376 bLFO3Enabled = true;
377 break;
378 case ::gig::lfo3_ctrl_internal_modwheel:
379 lfo3_internal_depth = pRegion->LFO3InternalDepth;
380 pLFO3->ExtController = 1; // MIDI controller 1
381 bLFO3Enabled = (lfo3_internal_depth > 0 || pRegion->LFO3ControlDepth > 0);
382 break;
383 case ::gig::lfo3_ctrl_internal_aftertouch:
384 lfo3_internal_depth = pRegion->LFO3InternalDepth;
385 pLFO1->ExtController = 128;
386 bLFO3Enabled = (lfo3_internal_depth > 0 || pRegion->LFO3ControlDepth > 0);
387 break;
388 default:
389 lfo3_internal_depth = 0;
390 pLFO3->ExtController = 0; // no external controller
391 bLFO3Enabled = false;
392 }
393 if (bLFO3Enabled) {
394 pLFO3->trigger(pRegion->LFO3Frequency,
395 start_level_mid,
396 lfo3_internal_depth,
397 pRegion->LFO3ControlDepth,
398 false,
399 pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE);
400 pLFO3->update(pLFO3->ExtController ? GetSf2EngineChannel()->ControllerTable[pLFO3->ExtController] : 0);
401 }*/ // TODO: ^^^
402 bLFO3Enabled = false;
403 }
404
405 float Voice::CalculateCutoffBase(uint8_t MIDIKeyVelocity) {
406 /*float cutoff = pRegion->GetVelocityCutoff(MIDIKeyVelocity);
407 if (pRegion->VCFKeyboardTracking) {
408 cutoff *= RTMath::CentsToFreqRatioUnlimited((MIDIKey - pRegion->VCFKeyboardTrackingBreakpoint) * 100);
409 }
410 return cutoff;*/ // TODO: ^^^
411 return 1.0f;
412 }
413
414 float Voice::CalculateFinalCutoff(float cutoffBase) {
415 /*int cvalue;
416 if (VCFCutoffCtrl.controller) {
417 cvalue = GetSf2EngineChannel()->ControllerTable[VCFCutoffCtrl.controller];
418 if (pRegion->VCFCutoffControllerInvert) cvalue = 127 - cvalue;
419 // VCFVelocityScale in this case means Minimum cutoff
420 if (cvalue < pRegion->VCFVelocityScale) cvalue = pRegion->VCFVelocityScale;
421 }
422 else {
423 cvalue = pRegion->VCFCutoff;
424 }
425 float fco = cutoffBase * float(cvalue);
426 if (fco > 127.0f) fco = 127.0f;
427
428 return fco;*/ // TODO: ^^^
429 return 0.0f;
430 }
431
432 uint8_t Voice::GetVCFCutoffCtrl() {
433 /*uint8_t ctrl;
434 switch (pRegion->VCFCutoffController) {
435 case ::gig::vcf_cutoff_ctrl_modwheel:
436 ctrl = 1;
437 break;
438 case ::gig::vcf_cutoff_ctrl_effect1:
439 ctrl = 12;
440 break;
441 case ::gig::vcf_cutoff_ctrl_effect2:
442 ctrl = 13;
443 break;
444 case ::gig::vcf_cutoff_ctrl_breath:
445 ctrl = 2;
446 break;
447 case ::gig::vcf_cutoff_ctrl_foot:
448 ctrl = 4;
449 break;
450 case ::gig::vcf_cutoff_ctrl_sustainpedal:
451 ctrl = 64;
452 break;
453 case ::gig::vcf_cutoff_ctrl_softpedal:
454 ctrl = 67;
455 break;
456 case ::gig::vcf_cutoff_ctrl_genpurpose7:
457 ctrl = 82;
458 break;
459 case ::gig::vcf_cutoff_ctrl_genpurpose8:
460 ctrl = 83;
461 break;
462 case ::gig::vcf_cutoff_ctrl_aftertouch:
463 ctrl = 128;
464 break;
465 case ::gig::vcf_cutoff_ctrl_none:
466 default:
467 ctrl = 0;
468 break;
469 }
470
471 return ctrl;*/ // TODO: ^^^
472 return 0;
473 }
474
475 uint8_t Voice::GetVCFResonanceCtrl() {
476 /*uint8_t ctrl;
477 switch (pRegion->VCFResonanceController) {
478 case ::gig::vcf_res_ctrl_genpurpose3:
479 ctrl = 18;
480 break;
481 case ::gig::vcf_res_ctrl_genpurpose4:
482 ctrl = 19;
483 break;
484 case ::gig::vcf_res_ctrl_genpurpose5:
485 ctrl = 80;
486 break;
487 case ::gig::vcf_res_ctrl_genpurpose6:
488 ctrl = 81;
489 break;
490 case ::gig::vcf_res_ctrl_none:
491 default:
492 ctrl = 0;
493 }
494
495 return ctrl;*/ // TODO: ^^^
496 return 0;
497 }
498
499 void Voice::ProcessGroupEvent(RTList<Event>::Iterator& itEvent) {
500 if (itEvent->Param.Note.Key != MIDIKey) {
501 // kill the voice fast
502 pEG1->enterFadeOutStage();
503 }
504 }
505
506 }} // namespace LinuxSampler::sf2

  ViewVC Help
Powered by ViewVC