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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2382 - (show annotations) (download)
Sun Dec 2 16:30:42 2012 UTC (11 years, 3 months ago) by persson
File size: 21974 byte(s)
* all engines: add pan CC value to instrument pan parameter before
  applying panning, instead of using two separate pan functions in
  series (#182)
* sfz parser: allow -200 to 200 for pan_oncc opcode (#182)
* gig engine: handle special case when pan parameter in gig file has
  max or min value
* CoreMIDI: fixed memory deallocation error

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

  ViewVC Help
Powered by ViewVC