4 |
* * |
* * |
5 |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
6 |
* Copyright (C) 2005 - 2008 Christian Schoenebeck * |
* Copyright (C) 2005 - 2008 Christian Schoenebeck * |
7 |
* Copyright (C) 2009 - 2012 Christian Schoenebeck and Grigor Iliev * |
* Copyright (C) 2009 Christian Schoenebeck and Grigor Iliev * |
8 |
|
* Copyright (C) 2010 - 2017 Christian Schoenebeck and Andreas Persson * |
9 |
* * |
* * |
10 |
* This program is free software; you can redistribute it and/or modify * |
* This program is free software; you can redistribute it and/or modify * |
11 |
* it under the terms of the GNU General Public License as published by * |
* it under the terms of the GNU General Public License as published by * |
33 |
|
|
34 |
namespace LinuxSampler { namespace gig { |
namespace LinuxSampler { namespace gig { |
35 |
|
|
36 |
|
// converts ::gig::lfo_wave_t (libgig) -> LFO::wave_t (LinuxSampler) |
37 |
|
inline LFO::wave_t fromGigLfoWave(::gig::lfo_wave_t wave) { |
38 |
|
// simply assuming equally mapped enums on both sides |
39 |
|
return static_cast<LFO::wave_t>(wave); |
40 |
|
} |
41 |
|
|
42 |
Voice::Voice() { |
Voice::Voice() { |
43 |
pEngine = NULL; |
pEngine = NULL; |
44 |
pEG1 = &EG1; |
pEG1 = &EG1; |
65 |
si.ChannelCount = pSample->Channels; |
si.ChannelCount = pSample->Channels; |
66 |
si.FrameSize = pSample->FrameSize; |
si.FrameSize = pSample->FrameSize; |
67 |
si.BitDepth = pSample->BitDepth; |
si.BitDepth = pSample->BitDepth; |
68 |
si.TotalFrameCount = pSample->SamplesTotal; |
si.TotalFrameCount = (uint)pSample->SamplesTotal; |
69 |
|
|
70 |
si.HasLoops = pRegion->SampleLoops; |
si.HasLoops = pRegion->SampleLoops; |
71 |
si.LoopStart = (si.HasLoops) ? pRegion->pSampleLoops[0].LoopStart : 0; |
si.LoopStart = (si.HasLoops) ? pRegion->pSampleLoops[0].LoopStart : 0; |
131 |
} |
} |
132 |
} |
} |
133 |
|
|
134 |
|
void Voice::ProcessChannelPressureEvent(RTList<Event>::Iterator& itEvent) { |
135 |
|
if (itEvent->Type == Event::type_channel_pressure) { // if (valid) MIDI channel pressure (aftertouch) event |
136 |
|
if (pRegion->AttenuationController.type == ::gig::attenuation_ctrl_t::type_channelaftertouch) { |
137 |
|
CrossfadeSmoother.update(AbstractEngine::CrossfadeCurve[CrossfadeAttenuation(itEvent->Param.ChannelPressure.Value)]); |
138 |
|
} |
139 |
|
} |
140 |
|
} |
141 |
|
|
142 |
|
void Voice::ProcessPolyphonicKeyPressureEvent(RTList<Event>::Iterator& itEvent) { |
143 |
|
// Not used so far |
144 |
|
} |
145 |
|
|
146 |
void Voice::ProcessCutoffEvent(RTList<Event>::Iterator& itEvent) { |
void Voice::ProcessCutoffEvent(RTList<Event>::Iterator& itEvent) { |
147 |
int ccvalue = itEvent->Param.CC.Value; |
int ccvalue = itEvent->Param.CC.Value; |
148 |
if (VCFCutoffCtrl.value == ccvalue) return; |
if (VCFCutoffCtrl.value == ccvalue) return; |
200 |
Voice::EGInfo Voice::CalculateEG1ControllerInfluence(double eg1ControllerValue) { |
Voice::EGInfo Voice::CalculateEG1ControllerInfluence(double eg1ControllerValue) { |
201 |
EGInfo eg; |
EGInfo eg; |
202 |
// (eg1attack is different from the others) |
// (eg1attack is different from the others) |
203 |
eg.Attack = (pRegion->EG1ControllerAttackInfluence) ? |
if (pRegion->EG1Attack < 1e-8 && // attack in gig == 0 |
204 |
1 + 0.031 * (double) (pRegion->EG1ControllerAttackInfluence == 1 ? |
(pRegion->EG1ControllerAttackInfluence == 0 || |
205 |
1 : 1 << pRegion->EG1ControllerAttackInfluence) * eg1ControllerValue : 1.0; |
eg1ControllerValue <= 10)) { // strange GSt special case |
206 |
|
eg.Attack = 0; // this will force the attack to be 0 in the call to EG1.trigger |
207 |
|
} else { |
208 |
|
eg.Attack = (pRegion->EG1ControllerAttackInfluence) ? |
209 |
|
1 + 0.031 * (double) (pRegion->EG1ControllerAttackInfluence == 1 ? |
210 |
|
1 : 1 << pRegion->EG1ControllerAttackInfluence) * eg1ControllerValue : 1.0; |
211 |
|
} |
212 |
eg.Decay = (pRegion->EG1ControllerDecayInfluence) ? 1 + 0.00775 * (double) (1 << pRegion->EG1ControllerDecayInfluence) * eg1ControllerValue : 1.0; |
eg.Decay = (pRegion->EG1ControllerDecayInfluence) ? 1 + 0.00775 * (double) (1 << pRegion->EG1ControllerDecayInfluence) * eg1ControllerValue : 1.0; |
213 |
eg.Release = (pRegion->EG1ControllerReleaseInfluence) ? 1 + 0.00775 * (double) (1 << pRegion->EG1ControllerReleaseInfluence) * eg1ControllerValue : 1.0; |
eg.Release = (pRegion->EG1ControllerReleaseInfluence) ? 1 + 0.00775 * (double) (1 << pRegion->EG1ControllerReleaseInfluence) * eg1ControllerValue : 1.0; |
214 |
|
|
279 |
bLFO1Enabled = false; |
bLFO1Enabled = false; |
280 |
} |
} |
281 |
if (bLFO1Enabled) { |
if (bLFO1Enabled) { |
282 |
pLFO1->trigger(pRegion->LFO1Frequency, |
pLFO1->trigger(fromGigLfoWave(pRegion->LFO1WaveForm), |
283 |
start_level_min, |
pRegion->LFO1Frequency, |
284 |
|
pRegion->LFO1Phase, |
285 |
|
LFO::start_level_mid, // see https://sourceforge.net/p/linuxsampler/mailman/linuxsampler-devel/thread/2189307.cNP0Xbctxq%40silver/#msg36774029 |
286 |
lfo1_internal_depth, |
lfo1_internal_depth, |
287 |
pRegion->LFO1ControlDepth, |
pRegion->LFO1ControlDepth, |
288 |
pRegion->LFO1FlipPhase, |
pRegion->LFO1FlipPhase, |
289 |
pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); |
pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); |
290 |
pLFO1->update(pLFO1->ExtController ? GetGigEngineChannel()->ControllerTable[pLFO1->ExtController] : 0); |
pLFO1->updateByMIDICtrlValue(pLFO1->ExtController ? GetGigEngineChannel()->ControllerTable[pLFO1->ExtController] : 0); |
291 |
|
pLFO1->setScriptDepthFactor( |
292 |
|
pNote->Override.AmpLFODepth.Value, |
293 |
|
pNote->Override.AmpLFODepth.Final |
294 |
|
); |
295 |
|
if (pNote->Override.AmpLFOFreq.isFinal()) |
296 |
|
pLFO1->setScriptFrequencyFinal( |
297 |
|
pNote->Override.AmpLFOFreq.Value, pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE |
298 |
|
); |
299 |
|
else |
300 |
|
pLFO1->setScriptFrequencyFactor( |
301 |
|
pNote->Override.AmpLFOFreq.Value, pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE |
302 |
|
); |
303 |
} |
} |
304 |
} |
} |
305 |
|
|
337 |
bLFO2Enabled = false; |
bLFO2Enabled = false; |
338 |
} |
} |
339 |
if (bLFO2Enabled) { |
if (bLFO2Enabled) { |
340 |
pLFO2->trigger(pRegion->LFO2Frequency, |
pLFO2->trigger(fromGigLfoWave(pRegion->LFO2WaveForm), |
341 |
start_level_max, |
pRegion->LFO2Frequency, |
342 |
|
pRegion->LFO2Phase, |
343 |
|
LFO::start_level_mid, // see https://sourceforge.net/p/linuxsampler/mailman/linuxsampler-devel/thread/2189307.cNP0Xbctxq%40silver/#msg36774029 |
344 |
lfo2_internal_depth, |
lfo2_internal_depth, |
345 |
pRegion->LFO2ControlDepth, |
pRegion->LFO2ControlDepth, |
346 |
pRegion->LFO2FlipPhase, |
pRegion->LFO2FlipPhase, |
347 |
pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); |
pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); |
348 |
pLFO2->update(pLFO2->ExtController ? GetGigEngineChannel()->ControllerTable[pLFO2->ExtController] : 0); |
pLFO2->updateByMIDICtrlValue(pLFO2->ExtController ? GetGigEngineChannel()->ControllerTable[pLFO2->ExtController] : 0); |
349 |
|
pLFO2->setScriptDepthFactor( |
350 |
|
pNote->Override.CutoffLFODepth.Value, |
351 |
|
pNote->Override.CutoffLFODepth.Final |
352 |
|
); |
353 |
|
if (pNote->Override.CutoffLFOFreq.isFinal()) |
354 |
|
pLFO2->setScriptFrequencyFinal(pNote->Override.CutoffLFOFreq.Value, pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); |
355 |
|
else |
356 |
|
pLFO2->setScriptFrequencyFactor(pNote->Override.CutoffLFOFreq.Value, pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); |
357 |
} |
} |
358 |
} |
} |
359 |
|
|
372 |
break; |
break; |
373 |
case ::gig::lfo3_ctrl_aftertouch: |
case ::gig::lfo3_ctrl_aftertouch: |
374 |
lfo3_internal_depth = 0; |
lfo3_internal_depth = 0; |
375 |
pLFO3->ExtController = 128; |
pLFO3->ExtController = CTRL_TABLE_IDX_AFTERTOUCH; |
376 |
bLFO3Enabled = true; |
bLFO3Enabled = true; |
377 |
break; |
break; |
378 |
case ::gig::lfo3_ctrl_internal_modwheel: |
case ::gig::lfo3_ctrl_internal_modwheel: |
382 |
break; |
break; |
383 |
case ::gig::lfo3_ctrl_internal_aftertouch: |
case ::gig::lfo3_ctrl_internal_aftertouch: |
384 |
lfo3_internal_depth = pRegion->LFO3InternalDepth; |
lfo3_internal_depth = pRegion->LFO3InternalDepth; |
385 |
pLFO3->ExtController = 128; |
pLFO3->ExtController = CTRL_TABLE_IDX_AFTERTOUCH; |
386 |
bLFO3Enabled = (lfo3_internal_depth > 0 || pRegion->LFO3ControlDepth > 0); |
bLFO3Enabled = (lfo3_internal_depth > 0 || pRegion->LFO3ControlDepth > 0); |
387 |
break; |
break; |
388 |
default: |
default: |
391 |
bLFO3Enabled = false; |
bLFO3Enabled = false; |
392 |
} |
} |
393 |
if (bLFO3Enabled) { |
if (bLFO3Enabled) { |
394 |
pLFO3->trigger(pRegion->LFO3Frequency, |
pLFO3->trigger(fromGigLfoWave(pRegion->LFO3WaveForm), |
395 |
start_level_mid, |
pRegion->LFO3Frequency, |
396 |
|
pRegion->LFO3Phase, |
397 |
|
LFO::start_level_max, // see https://sourceforge.net/p/linuxsampler/mailman/linuxsampler-devel/thread/2189307.cNP0Xbctxq%40silver/#msg36774029 |
398 |
lfo3_internal_depth, |
lfo3_internal_depth, |
399 |
pRegion->LFO3ControlDepth, |
pRegion->LFO3ControlDepth, |
400 |
false, |
pRegion->LFO3FlipPhase, |
401 |
pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); |
pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); |
402 |
pLFO3->update(pLFO3->ExtController ? GetGigEngineChannel()->ControllerTable[pLFO3->ExtController] : 0); |
pLFO3->updateByMIDICtrlValue(pLFO3->ExtController ? GetGigEngineChannel()->ControllerTable[pLFO3->ExtController] : 0); |
403 |
|
pLFO3->setScriptDepthFactor( |
404 |
|
pNote->Override.PitchLFODepth.Value, |
405 |
|
pNote->Override.PitchLFODepth.Final |
406 |
|
); |
407 |
|
if (pNote->Override.PitchLFOFreq.isFinal()) |
408 |
|
pLFO3->setScriptFrequencyFinal(pNote->Override.PitchLFOFreq.Value, pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); |
409 |
|
else |
410 |
|
pLFO3->setScriptFrequencyFactor(pNote->Override.PitchLFOFreq.Value, pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); |
411 |
} |
} |
412 |
} |
} |
413 |
|
|
414 |
float Voice::CalculateCutoffBase(uint8_t MIDIKeyVelocity) { |
float Voice::CalculateCutoffBase(uint8_t MIDIKeyVelocity) { |
415 |
float cutoff = pRegion->GetVelocityCutoff(MIDIKeyVelocity); |
float cutoff = pRegion->GetVelocityCutoff(MIDIKeyVelocity); |
416 |
if (pRegion->VCFKeyboardTracking) { |
if (pRegion->VCFKeyboardTracking) { |
417 |
cutoff *= RTMath::CentsToFreqRatioUnlimited((MIDIKey - pRegion->VCFKeyboardTrackingBreakpoint) * 100); |
cutoff *= RTMath::CentsToFreqRatioUnlimited((MIDIKey() - pRegion->VCFKeyboardTrackingBreakpoint) * 100); |
418 |
} |
} |
419 |
return cutoff; |
return cutoff; |
420 |
} |
} |
467 |
ctrl = 83; |
ctrl = 83; |
468 |
break; |
break; |
469 |
case ::gig::vcf_cutoff_ctrl_aftertouch: |
case ::gig::vcf_cutoff_ctrl_aftertouch: |
470 |
ctrl = 128; |
ctrl = CTRL_TABLE_IDX_AFTERTOUCH; |
471 |
break; |
break; |
472 |
case ::gig::vcf_cutoff_ctrl_none: |
case ::gig::vcf_cutoff_ctrl_none: |
473 |
default: |
default: |
502 |
} |
} |
503 |
|
|
504 |
void Voice::TriggerEG1(const EGInfo& egInfo, double velrelease, double velocityAttenuation, uint sampleRate, uint8_t velocity) { |
void Voice::TriggerEG1(const EGInfo& egInfo, double velrelease, double velocityAttenuation, uint sampleRate, uint8_t velocity) { |
505 |
|
EG1.setStateOptions( |
506 |
|
pRegion->EG1Options.AttackCancel, |
507 |
|
pRegion->EG1Options.AttackHoldCancel, |
508 |
|
pRegion->EG1Options.Decay1Cancel, |
509 |
|
pRegion->EG1Options.Decay2Cancel, |
510 |
|
pRegion->EG1Options.ReleaseCancel |
511 |
|
); |
512 |
EG1.trigger(pRegion->EG1PreAttack, |
EG1.trigger(pRegion->EG1PreAttack, |
513 |
pRegion->EG1Attack * egInfo.Attack, |
(pNote && pNote->Override.Attack.isFinal()) ? |
514 |
|
pNote->Override.Attack.Value : |
515 |
|
RTMath::Max(pRegion->EG1Attack, 0.0316) * egInfo.Attack, |
516 |
pRegion->EG1Hold, |
pRegion->EG1Hold, |
517 |
pRegion->EG1Decay1 * egInfo.Decay * velrelease, |
(pNote && pNote->Override.Decay.isFinal()) ? |
518 |
pRegion->EG1Decay2 * egInfo.Decay * velrelease, |
pNote->Override.Decay.Value : |
519 |
|
pRegion->EG1Decay1 * egInfo.Decay * velrelease, |
520 |
|
(pNote && pNote->Override.Decay.isFinal()) ? |
521 |
|
pNote->Override.Decay.Value : |
522 |
|
pRegion->EG1Decay2 * egInfo.Decay * velrelease, |
523 |
pRegion->EG1InfiniteSustain, |
pRegion->EG1InfiniteSustain, |
524 |
pRegion->EG1Sustain, |
(pNote && pNote->Override.Sustain.Final) ? |
525 |
pRegion->EG1Release * egInfo.Release * velrelease, |
uint(pNote->Override.Sustain.Value * 1000.f) : |
526 |
|
pRegion->EG1Sustain * (pNote ? pNote->Override.Sustain.Value : 1.f), |
527 |
|
(pNote && pNote->Override.Release.isFinal()) ? |
528 |
|
pNote->Override.Release.Value : |
529 |
|
RTMath::Max(pRegion->EG1Release * velrelease, 0.014) * egInfo.Release, |
530 |
velocityAttenuation, |
velocityAttenuation, |
531 |
sampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); |
sampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); |
532 |
} |
} |
533 |
|
|
534 |
void Voice::TriggerEG2(const EGInfo& egInfo, double velrelease, double velocityAttenuation, uint sampleRate, uint8_t velocity) { |
void Voice::TriggerEG2(const EGInfo& egInfo, double velrelease, double velocityAttenuation, uint sampleRate, uint8_t velocity) { |
535 |
|
EG2.setStateOptions( |
536 |
|
pRegion->EG2Options.AttackCancel, |
537 |
|
pRegion->EG2Options.AttackHoldCancel, |
538 |
|
pRegion->EG2Options.Decay1Cancel, |
539 |
|
pRegion->EG2Options.Decay2Cancel, |
540 |
|
pRegion->EG2Options.ReleaseCancel |
541 |
|
); |
542 |
EG2.trigger(uint(RgnInfo.EG2PreAttack), |
EG2.trigger(uint(RgnInfo.EG2PreAttack), |
543 |
RgnInfo.EG2Attack * egInfo.Attack, |
(pNote && pNote->Override.CutoffAttack.isFinal()) ? |
544 |
|
pNote->Override.CutoffAttack.Value : |
545 |
|
RgnInfo.EG2Attack * egInfo.Attack, |
546 |
false, |
false, |
547 |
RgnInfo.EG2Decay1 * egInfo.Decay * velrelease, |
(pNote && pNote->Override.CutoffDecay.isFinal()) ? |
548 |
RgnInfo.EG2Decay2 * egInfo.Decay * velrelease, |
pNote->Override.CutoffDecay.Value : |
549 |
|
RgnInfo.EG2Decay1 * egInfo.Decay * velrelease, |
550 |
|
(pNote && pNote->Override.CutoffDecay.isFinal()) ? |
551 |
|
pNote->Override.CutoffDecay.Value : |
552 |
|
RgnInfo.EG2Decay2 * egInfo.Decay * velrelease, |
553 |
RgnInfo.EG2InfiniteSustain, |
RgnInfo.EG2InfiniteSustain, |
554 |
uint(RgnInfo.EG2Sustain), |
(pNote && pNote->Override.CutoffSustain.Final) ? |
555 |
RgnInfo.EG2Release * egInfo.Release * velrelease, |
uint(pNote->Override.CutoffSustain.Value * 1000.f) : |
556 |
|
uint(RgnInfo.EG2Sustain), |
557 |
|
(pNote && pNote->Override.CutoffRelease.isFinal()) ? |
558 |
|
pNote->Override.CutoffRelease.Value : |
559 |
|
RgnInfo.EG2Release * egInfo.Release * velrelease, |
560 |
velocityAttenuation, |
velocityAttenuation, |
561 |
sampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); |
sampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); |
562 |
} |
} |
563 |
|
|
564 |
void Voice::ProcessGroupEvent(RTList<Event>::Iterator& itEvent) { |
void Voice::ProcessGroupEvent(RTList<Event>::Iterator& itEvent) { |
565 |
dmsg(4,("Voice %x processGroupEvents event type=%d", this, itEvent->Type)); |
dmsg(4,("Voice %p processGroupEvents event type=%d", (void*)this, itEvent->Type)); |
566 |
|
|
567 |
// TODO: The SustainPedal condition could be wrong, maybe the |
// TODO: The SustainPedal condition could be wrong, maybe the |
568 |
// check should be if this Voice is in release stage or is a |
// check should be if this Voice is in release stage or is a |
569 |
// release sample instead. Need to test this in GSt. |
// release sample instead. Need to test this in GSt. |
570 |
if (itEvent->Param.Note.Key != MIDIKey || |
// -- Andreas |
571 |
!GetGigEngineChannel()->SustainPedal) { |
// |
572 |
dmsg(4,("Voice %x - kill", this)); |
// Commented sustain pedal check out. I don't think voices of the same |
573 |
|
// note should be stopped at all, because it doesn't sound naturally |
574 |
|
// with a drumkit. |
575 |
|
// -- Christian, 2013-01-08 |
576 |
|
if (itEvent->Param.Note.Key != HostKey() /*|| |
577 |
|
!GetGigEngineChannel()->SustainPedal*/) { |
578 |
|
dmsg(4,("Voice %p - kill", (void*)this)); |
579 |
|
|
580 |
// kill the voice fast |
// kill the voice fast |
581 |
pEG1->enterFadeOutStage(); |
pEG1->enterFadeOutStage(); |
586 |
EG1.CalculateFadeOutCoeff(FadeOutTime, SampleRate); |
EG1.CalculateFadeOutCoeff(FadeOutTime, SampleRate); |
587 |
} |
} |
588 |
|
|
589 |
|
int Voice::CalculatePan(uint8_t pan) { |
590 |
|
int p; |
591 |
|
// Gst behaviour: -64 and 63 are special cases |
592 |
|
if (RgnInfo.Pan == -64) p = pan * 2 - 127; |
593 |
|
else if (RgnInfo.Pan == 63) p = pan * 2; |
594 |
|
else p = pan + RgnInfo.Pan; |
595 |
|
|
596 |
|
if (p < 0) return 0; |
597 |
|
if (p > 127) return 127; |
598 |
|
return p; |
599 |
|
} |
600 |
|
|
601 |
|
release_trigger_t Voice::GetReleaseTriggerFlags() { |
602 |
|
release_trigger_t flags = |
603 |
|
(pRegion->NoNoteOffReleaseTrigger) ? |
604 |
|
release_trigger_none : release_trigger_noteoff; //HACK: currently this method is actually only called by EngineBase if it already knows that this voice requires release trigger, so I took the short way instead of checking (again) the existence of a ::gig::dimension_releasetrigger |
605 |
|
switch (pRegion->SustainReleaseTrigger) { |
606 |
|
case ::gig::sust_rel_trg_none: |
607 |
|
break; |
608 |
|
case ::gig::sust_rel_trg_maxvelocity: |
609 |
|
flags |= release_trigger_sustain_maxvelocity; |
610 |
|
break; |
611 |
|
case ::gig::sust_rel_trg_keyvelocity: |
612 |
|
flags |= release_trigger_sustain_keyvelocity; |
613 |
|
break; |
614 |
|
} |
615 |
|
return flags; |
616 |
|
} |
617 |
|
|
618 |
}} // namespace LinuxSampler::gig |
}} // namespace LinuxSampler::gig |