/[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 2021 - (show annotations) (download)
Fri Oct 30 16:36:20 2009 UTC (14 years, 5 months ago) by iliev
File size: 20401 byte(s)
* sfz engine: loop support
* sf2 engine: 24bit support
* sf2 engine: loop support
* sf2 engine: instrument unloading

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 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 = pRegion->pan;
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;
80 ri.EG1InfiniteSustain = true;
81 ri.EG1Release = pRegion->ampeg_release;
82
83 ri.EG2PreAttack = pRegion->fileg_start;
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;
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 double Voice::GetEG2ControllerValue(uint8_t MIDIKeyVelocity) {
206 /*double eg2controllervalue = 0;
207 switch (pRegion->EG2Controller.type) {
208 case ::gig::eg2_ctrl_t::type_none: // no controller defined
209 eg2controllervalue = 0;
210 break;
211 case ::gig::eg2_ctrl_t::type_channelaftertouch:
212 eg2controllervalue = GetSfzEngineChannel()->ControllerTable[128];
213 break;
214 case ::gig::eg2_ctrl_t::type_velocity:
215 eg2controllervalue = MIDIKeyVelocity;
216 break;
217 case ::gig::eg2_ctrl_t::type_controlchange: // MIDI control change controller
218 eg2controllervalue = GetSfzEngineChannel()->ControllerTable[pRegion->EG2Controller.controller_number];
219 break;
220 }
221 if (pRegion->EG2ControllerInvert) eg2controllervalue = 127 - eg2controllervalue;
222
223 return eg2controllervalue;*/ // TODO: ^^^
224 return 0;
225 }
226
227 Voice::EGInfo Voice::CalculateEG2ControllerInfluence(double eg2ControllerValue) {
228 /*EGInfo eg;
229 eg.Attack = (pRegion->EG2ControllerAttackInfluence) ? 1 + 0.00775 * (double) (1 << pRegion->EG2ControllerAttackInfluence) * eg2ControllerValue : 1.0;
230 eg.Decay = (pRegion->EG2ControllerDecayInfluence) ? 1 + 0.00775 * (double) (1 << pRegion->EG2ControllerDecayInfluence) * eg2ControllerValue : 1.0;
231 eg.Release = (pRegion->EG2ControllerReleaseInfluence) ? 1 + 0.00775 * (double) (1 << pRegion->EG2ControllerReleaseInfluence) * eg2ControllerValue : 1.0;
232
233 return eg;*/ // TODO: ^^^
234 EGInfo eg;
235 eg.Attack = 1.0;
236 eg.Decay = 1.0;
237 eg.Release = 1.0;
238 return eg;
239 }
240
241 void Voice::InitLFO1() {
242 /*uint16_t lfo1_internal_depth;
243 switch (pRegion->LFO1Controller) {
244 case ::gig::lfo1_ctrl_internal:
245 lfo1_internal_depth = pRegion->LFO1InternalDepth;
246 pLFO1->ExtController = 0; // no external controller
247 bLFO1Enabled = (lfo1_internal_depth > 0);
248 break;
249 case ::gig::lfo1_ctrl_modwheel:
250 lfo1_internal_depth = 0;
251 pLFO1->ExtController = 1; // MIDI controller 1
252 bLFO1Enabled = (pRegion->LFO1ControlDepth > 0);
253 break;
254 case ::gig::lfo1_ctrl_breath:
255 lfo1_internal_depth = 0;
256 pLFO1->ExtController = 2; // MIDI controller 2
257 bLFO1Enabled = (pRegion->LFO1ControlDepth > 0);
258 break;
259 case ::gig::lfo1_ctrl_internal_modwheel:
260 lfo1_internal_depth = pRegion->LFO1InternalDepth;
261 pLFO1->ExtController = 1; // MIDI controller 1
262 bLFO1Enabled = (lfo1_internal_depth > 0 || pRegion->LFO1ControlDepth > 0);
263 break;
264 case ::gig::lfo1_ctrl_internal_breath:
265 lfo1_internal_depth = pRegion->LFO1InternalDepth;
266 pLFO1->ExtController = 2; // MIDI controller 2
267 bLFO1Enabled = (lfo1_internal_depth > 0 || pRegion->LFO1ControlDepth > 0);
268 break;
269 default:
270 lfo1_internal_depth = 0;
271 pLFO1->ExtController = 0; // no external controller
272 bLFO1Enabled = false;
273 }
274 if (bLFO1Enabled) {
275 pLFO1->trigger(pRegion->LFO1Frequency,
276 start_level_min,
277 lfo1_internal_depth,
278 pRegion->LFO1ControlDepth,
279 pRegion->LFO1FlipPhase,
280 pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE);
281 pLFO1->update(pLFO1->ExtController ? GetSfzEngineChannel()->ControllerTable[pLFO1->ExtController] : 0);
282 }*/ // TODO: ^^^
283 bLFO1Enabled = false;
284 }
285
286 void Voice::InitLFO2() {
287 /*uint16_t lfo2_internal_depth;
288 switch (pRegion->LFO2Controller) {
289 case ::gig::lfo2_ctrl_internal:
290 lfo2_internal_depth = pRegion->LFO2InternalDepth;
291 pLFO2->ExtController = 0; // no external controller
292 bLFO2Enabled = (lfo2_internal_depth > 0);
293 break;
294 case ::gig::lfo2_ctrl_modwheel:
295 lfo2_internal_depth = 0;
296 pLFO2->ExtController = 1; // MIDI controller 1
297 bLFO2Enabled = (pRegion->LFO2ControlDepth > 0);
298 break;
299 case ::gig::lfo2_ctrl_foot:
300 lfo2_internal_depth = 0;
301 pLFO2->ExtController = 4; // MIDI controller 4
302 bLFO2Enabled = (pRegion->LFO2ControlDepth > 0);
303 break;
304 case ::gig::lfo2_ctrl_internal_modwheel:
305 lfo2_internal_depth = pRegion->LFO2InternalDepth;
306 pLFO2->ExtController = 1; // MIDI controller 1
307 bLFO2Enabled = (lfo2_internal_depth > 0 || pRegion->LFO2ControlDepth > 0);
308 break;
309 case ::gig::lfo2_ctrl_internal_foot:
310 lfo2_internal_depth = pRegion->LFO2InternalDepth;
311 pLFO2->ExtController = 4; // MIDI controller 4
312 bLFO2Enabled = (lfo2_internal_depth > 0 || pRegion->LFO2ControlDepth > 0);
313 break;
314 default:
315 lfo2_internal_depth = 0;
316 pLFO2->ExtController = 0; // no external controller
317 bLFO2Enabled = false;
318 }
319 if (bLFO2Enabled) {
320 pLFO2->trigger(pRegion->LFO2Frequency,
321 start_level_max,
322 lfo2_internal_depth,
323 pRegion->LFO2ControlDepth,
324 pRegion->LFO2FlipPhase,
325 pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE);
326 pLFO2->update(pLFO2->ExtController ? GetSfzEngineChannel()->ControllerTable[pLFO2->ExtController] : 0);
327 }*/ // TODO: ^^^
328 bLFO2Enabled = false;
329 }
330
331 void Voice::InitLFO3() {
332 /*uint16_t lfo3_internal_depth;
333 switch (pRegion->LFO3Controller) {
334 case ::gig::lfo3_ctrl_internal:
335 lfo3_internal_depth = pRegion->LFO3InternalDepth;
336 pLFO3->ExtController = 0; // no external controller
337 bLFO3Enabled = (lfo3_internal_depth > 0);
338 break;
339 case ::gig::lfo3_ctrl_modwheel:
340 lfo3_internal_depth = 0;
341 pLFO3->ExtController = 1; // MIDI controller 1
342 bLFO3Enabled = (pRegion->LFO3ControlDepth > 0);
343 break;
344 case ::gig::lfo3_ctrl_aftertouch:
345 lfo3_internal_depth = 0;
346 pLFO3->ExtController = 128;
347 bLFO3Enabled = true;
348 break;
349 case ::gig::lfo3_ctrl_internal_modwheel:
350 lfo3_internal_depth = pRegion->LFO3InternalDepth;
351 pLFO3->ExtController = 1; // MIDI controller 1
352 bLFO3Enabled = (lfo3_internal_depth > 0 || pRegion->LFO3ControlDepth > 0);
353 break;
354 case ::gig::lfo3_ctrl_internal_aftertouch:
355 lfo3_internal_depth = pRegion->LFO3InternalDepth;
356 pLFO1->ExtController = 128;
357 bLFO3Enabled = (lfo3_internal_depth > 0 || pRegion->LFO3ControlDepth > 0);
358 break;
359 default:
360 lfo3_internal_depth = 0;
361 pLFO3->ExtController = 0; // no external controller
362 bLFO3Enabled = false;
363 }
364 if (bLFO3Enabled) {
365 pLFO3->trigger(pRegion->LFO3Frequency,
366 start_level_mid,
367 lfo3_internal_depth,
368 pRegion->LFO3ControlDepth,
369 false,
370 pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE);
371 pLFO3->update(pLFO3->ExtController ? GetSfzEngineChannel()->ControllerTable[pLFO3->ExtController] : 0);
372 }*/ // TODO: ^^^
373 bLFO3Enabled = false;
374 }
375
376 float Voice::CalculateCutoffBase(uint8_t MIDIKeyVelocity) {
377 /*float cutoff = pRegion->GetVelocityCutoff(MIDIKeyVelocity);
378 if (pRegion->VCFKeyboardTracking) {
379 cutoff *= exp((MIDIKeyVelocity - pRegion->VCFKeyboardTrackingBreakpoint) * 0.057762265f); // (ln(2) / 12)
380 }
381 return cutoff;*/ // TODO: ^^^
382 return 1.0f;
383 }
384
385 float Voice::CalculateFinalCutoff(float cutoffBase) {
386 /*int cvalue;
387 if (VCFCutoffCtrl.controller) {
388 cvalue = GetSfzEngineChannel()->ControllerTable[VCFCutoffCtrl.controller];
389 if (pRegion->VCFCutoffControllerInvert) cvalue = 127 - cvalue;
390 // VCFVelocityScale in this case means Minimum cutoff
391 if (cvalue < pRegion->VCFVelocityScale) cvalue = pRegion->VCFVelocityScale;
392 }
393 else {
394 cvalue = pRegion->VCFCutoff;
395 }
396 float fco = cutoffBase * float(cvalue);
397 if (fco > 127.0f) fco = 127.0f;
398
399 return fco;*/ // TODO: ^^^
400 return 127.0f;
401 }
402
403 uint8_t Voice::GetVCFCutoffCtrl() {
404 /*uint8_t ctrl;
405 switch (pRegion->VCFCutoffController) {
406 case ::gig::vcf_cutoff_ctrl_modwheel:
407 ctrl = 1;
408 break;
409 case ::gig::vcf_cutoff_ctrl_effect1:
410 ctrl = 12;
411 break;
412 case ::gig::vcf_cutoff_ctrl_effect2:
413 ctrl = 13;
414 break;
415 case ::gig::vcf_cutoff_ctrl_breath:
416 ctrl = 2;
417 break;
418 case ::gig::vcf_cutoff_ctrl_foot:
419 ctrl = 4;
420 break;
421 case ::gig::vcf_cutoff_ctrl_sustainpedal:
422 ctrl = 64;
423 break;
424 case ::gig::vcf_cutoff_ctrl_softpedal:
425 ctrl = 67;
426 break;
427 case ::gig::vcf_cutoff_ctrl_genpurpose7:
428 ctrl = 82;
429 break;
430 case ::gig::vcf_cutoff_ctrl_genpurpose8:
431 ctrl = 83;
432 break;
433 case ::gig::vcf_cutoff_ctrl_aftertouch:
434 ctrl = 128;
435 break;
436 case ::gig::vcf_cutoff_ctrl_none:
437 default:
438 ctrl = 0;
439 break;
440 }
441
442 return ctrl;*/ // TODO: ^^^
443 return 0;
444 }
445
446 uint8_t Voice::GetVCFResonanceCtrl() {
447 /*uint8_t ctrl;
448 switch (pRegion->VCFResonanceController) {
449 case ::gig::vcf_res_ctrl_genpurpose3:
450 ctrl = 18;
451 break;
452 case ::gig::vcf_res_ctrl_genpurpose4:
453 ctrl = 19;
454 break;
455 case ::gig::vcf_res_ctrl_genpurpose5:
456 ctrl = 80;
457 break;
458 case ::gig::vcf_res_ctrl_genpurpose6:
459 ctrl = 81;
460 break;
461 case ::gig::vcf_res_ctrl_none:
462 default:
463 ctrl = 0;
464 }
465
466 return ctrl;*/ // TODO: ^^^
467 return 0;
468 }
469
470 }} // namespace LinuxSampler::sfz

  ViewVC Help
Powered by ViewVC