/[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 2015 - (show annotations) (download)
Sun Oct 25 22:22:52 2009 UTC (14 years, 5 months ago) by iliev
File size: 20415 byte(s)
* Refactoring: moved the independent code from gig::Voice to base classes
* SoundFont format engine: implemented EG1 & EG2

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

  ViewVC Help
Powered by ViewVC