/[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 2027 - (show annotations) (download)
Tue Nov 3 19:27:42 2009 UTC (14 years, 5 months ago) by iliev
File size: 21054 byte(s)
* sfz engine: support for exclusive groups
* sf2 engine: support for exclusive groups
* sf2 engine: manage presets only
* sf2 engine: preset regions are now taken into account

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

  ViewVC Help
Powered by ViewVC