--- linuxsampler/trunk/src/engines/gig/Voice.cpp 2011/04/25 08:12:36 2175 +++ linuxsampler/trunk/src/engines/gig/Voice.cpp 2019/12/13 17:14:48 3655 @@ -4,7 +4,8 @@ * * * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * * Copyright (C) 2005 - 2008 Christian Schoenebeck * - * Copyright (C) 2009 - 2011 Christian Schoenebeck and Grigor Iliev * + * Copyright (C) 2009 Christian Schoenebeck and Grigor Iliev * + * Copyright (C) 2010 - 2017 Christian Schoenebeck and Andreas Persson * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * @@ -32,6 +33,33 @@ namespace LinuxSampler { namespace gig { + // sanity checks: fromGigLfoWave() assumes equally mapped enums + static_assert(int64_t(::gig::lfo_wave_sine) == int64_t(LFO::wave_sine), + "enum LFO::wave_t not equally value mapped to libgig's enum ::gig::lfo_wave_t"); + static_assert(int64_t(::gig::lfo_wave_triangle) == int64_t(LFO::wave_triangle), + "enum LFO::wave_t not equally value mapped to libgig's enum ::gig::lfo_wave_t"); + static_assert(int64_t(::gig::lfo_wave_saw) == int64_t(LFO::wave_saw), + "enum LFO::wave_t not equally value mapped to libgig's enum ::gig::lfo_wave_t"); + static_assert(int64_t(::gig::lfo_wave_square) == int64_t(LFO::wave_square), + "enum LFO::wave_t not equally value mapped to libgig's enum ::gig::lfo_wave_t"); + + // converts ::gig::lfo_wave_t (libgig) -> LFO::wave_t (LinuxSampler) + inline LFO::wave_t fromGigLfoWave(::gig::lfo_wave_t wave) { + // simply assuming equally mapped enums on both sides + return static_cast(wave); + } + + // Returns true for GigaStudio's original filter types (which are resembled + // by LS very accurately with same frequency response and patch settings + // behaviour), false for our own LS specific filter implementation types. + constexpr bool isGStFilterType(::gig::vcf_type_t type) { + return type == ::gig::vcf_type_lowpass || + type == ::gig::vcf_type_lowpassturbo || + type == ::gig::vcf_type_bandpass || + type == ::gig::vcf_type_highpass || + type == ::gig::vcf_type_bandreject; + } + Voice::Voice() { pEngine = NULL; pEG1 = &EG1; @@ -58,7 +86,7 @@ si.ChannelCount = pSample->Channels; si.FrameSize = pSample->FrameSize; si.BitDepth = pSample->BitDepth; - si.TotalFrameCount = pSample->SamplesTotal; + si.TotalFrameCount = (uint)pSample->SamplesTotal; si.HasLoops = pRegion->SampleLoops; si.LoopStart = (si.HasLoops) ? pRegion->pSampleLoops[0].LoopStart : 0; @@ -76,15 +104,6 @@ ri.Pan = pRegion->Pan; ri.SampleStartOffset = pRegion->SampleStartOffset; - ri.EG1PreAttack = pRegion->EG1PreAttack; - ri.EG1Attack = pRegion->EG1Attack; - ri.EG1Hold = pRegion->EG1Hold; - ri.EG1Decay1 = pRegion->EG1Decay1; - ri.EG1Decay2 = pRegion->EG1Decay2; - ri.EG1Sustain = pRegion->EG1Sustain; - ri.EG1InfiniteSustain = pRegion->EG1InfiniteSustain; - ri.EG1Release = pRegion->EG1Release; - ri.EG2PreAttack = pRegion->EG2PreAttack; ri.EG2Attack = pRegion->EG2Attack; ri.EG2Decay1 = pRegion->EG2Decay1; @@ -133,17 +152,69 @@ } } + void Voice::ProcessChannelPressureEvent(RTList::Iterator& itEvent) { + if (itEvent->Type == Event::type_channel_pressure) { // if (valid) MIDI channel pressure (aftertouch) event + if (pRegion->AttenuationController.type == ::gig::attenuation_ctrl_t::type_channelaftertouch) { + CrossfadeSmoother.update(AbstractEngine::CrossfadeCurve[CrossfadeAttenuation(itEvent->Param.ChannelPressure.Value)]); + } + } + } + + void Voice::ProcessPolyphonicKeyPressureEvent(RTList::Iterator& itEvent) { + // Not used so far + } + + uint8_t Voice::MinCutoff() const { + // If there's a cutoff controller defined then VCFVelocityScale means + // "minimum cutoff". If there is no MIDI controller defined for cutoff + // then VCFVelocityScale is already taken into account on libgig side + // instead by call to pRegion->GetVelocityCutoff(MIDIKeyVelocity). + return pRegion->VCFVelocityScale; + } + + // This is called on any cutoff controller changes, however not when the + // voice is triggered. So the initial cutoff value is retrieved by a call + // to CalculateFinalCutoff() instead. void Voice::ProcessCutoffEvent(RTList::Iterator& itEvent) { - int ccvalue = itEvent->Param.CC.Value; - if (VCFCutoffCtrl.value == ccvalue) return; - VCFCutoffCtrl.value = ccvalue; - if (pRegion->VCFCutoffControllerInvert) ccvalue = 127 - ccvalue; - if (ccvalue < pRegion->VCFVelocityScale) ccvalue = pRegion->VCFVelocityScale; - float cutoff = CutoffBase * float(ccvalue); + if (VCFCutoffCtrl.value == itEvent->Param.CC.Value) return; + float ccvalue = VCFCutoffCtrl.value = itEvent->Param.CC.Value; + + // if the selected filter type is an official GigaStudio filter type + // then we preserve the original (no matter how odd) historical GSt + // behaviour identically; for our own filter types though we deviate to + // more meaningful behaviours where appropriate + const bool isGStFilter = isGStFilterType(pRegion->VCFType); + + if (pRegion->VCFCutoffControllerInvert) ccvalue = 127 - ccvalue; + if (isGStFilter) { + // VCFVelocityScale in this case means "minimum cutoff" for GSt + if (ccvalue < MinCutoff()) ccvalue = MinCutoff(); + } else { + // for our own filter types we interpret "minimum cutoff" + // differently: GSt handles this as a simple hard limit with the + // consequence that a certain range of the controller is simply + // dead; so for our filter types we rather remap that to + // restrain within the min_cutoff..127 range as well, but + // effectively spanned over the entire controller range (0..127) + // to avoid such a "dead" lower controller zone + ccvalue = MinCutoff() + (ccvalue / 127.f) * float(127 - MinCutoff()); + } + + float cutoff = CutoffBase * ccvalue; if (cutoff > 127.0f) cutoff = 127.0f; - VCFCutoffCtrl.fvalue = cutoff; // needed for initialization of fFinalCutoff next time - fFinalCutoff = cutoff; + // the filter implementations of the original GSt filter types take an + // abstract cutoff parameter range of 0..127, whereas our own filter + // types take a cutoff parameter in Hz, so remap here: + // 0 .. 127 [lin] -> 21 Hz .. 18 kHz [x^4] (center @2.2 kHz) + if (!isGStFilter) { + cutoff = (cutoff + 29.f) / (127.f + 29.f); + cutoff = cutoff * cutoff * cutoff * cutoff * 18000.f; + if (cutoff > 0.49f * pEngine->SampleRate) + cutoff = 0.49f * pEngine->SampleRate; + } + + fFinalCutoff = VCFCutoffCtrl.fvalue = cutoff; } double Voice::CalculateCrossfadeVolume(uint8_t MIDIKeyVelocity) { @@ -190,9 +261,15 @@ Voice::EGInfo Voice::CalculateEG1ControllerInfluence(double eg1ControllerValue) { EGInfo eg; // (eg1attack is different from the others) - eg.Attack = (pRegion->EG1ControllerAttackInfluence) ? - 1 + 0.031 * (double) (pRegion->EG1ControllerAttackInfluence == 1 ? - 1 : 1 << pRegion->EG1ControllerAttackInfluence) * eg1ControllerValue : 1.0; + if (pRegion->EG1Attack < 1e-8 && // attack in gig == 0 + (pRegion->EG1ControllerAttackInfluence == 0 || + eg1ControllerValue <= 10)) { // strange GSt special case + eg.Attack = 0; // this will force the attack to be 0 in the call to EG1.trigger + } else { + eg.Attack = (pRegion->EG1ControllerAttackInfluence) ? + 1 + 0.031 * (double) (pRegion->EG1ControllerAttackInfluence == 1 ? + 1 : 1 << pRegion->EG1ControllerAttackInfluence) * eg1ControllerValue : 1.0; + } eg.Decay = (pRegion->EG1ControllerDecayInfluence) ? 1 + 0.00775 * (double) (1 << pRegion->EG1ControllerDecayInfluence) * eg1ControllerValue : 1.0; eg.Release = (pRegion->EG1ControllerReleaseInfluence) ? 1 + 0.00775 * (double) (1 << pRegion->EG1ControllerReleaseInfluence) * eg1ControllerValue : 1.0; @@ -263,13 +340,27 @@ bLFO1Enabled = false; } if (bLFO1Enabled) { - pLFO1->trigger(pRegion->LFO1Frequency, - start_level_min, + pLFO1->trigger(fromGigLfoWave(pRegion->LFO1WaveForm), + pRegion->LFO1Frequency, + pRegion->LFO1Phase, + LFO::start_level_mid, // see https://sourceforge.net/p/linuxsampler/mailman/linuxsampler-devel/thread/2189307.cNP0Xbctxq%40silver/#msg36774029 lfo1_internal_depth, pRegion->LFO1ControlDepth, pRegion->LFO1FlipPhase, pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); - pLFO1->update(pLFO1->ExtController ? GetGigEngineChannel()->ControllerTable[pLFO1->ExtController] : 0); + pLFO1->updateByMIDICtrlValue(pLFO1->ExtController ? GetGigEngineChannel()->ControllerTable[pLFO1->ExtController] : 0); + pLFO1->setScriptDepthFactor( + pNote->Override.AmpLFODepth.Value, + pNote->Override.AmpLFODepth.Final + ); + if (pNote->Override.AmpLFOFreq.isFinal()) + pLFO1->setScriptFrequencyFinal( + pNote->Override.AmpLFOFreq.Value, pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE + ); + else + pLFO1->setScriptFrequencyFactor( + pNote->Override.AmpLFOFreq.Value, pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE + ); } } @@ -307,13 +398,23 @@ bLFO2Enabled = false; } if (bLFO2Enabled) { - pLFO2->trigger(pRegion->LFO2Frequency, - start_level_max, + pLFO2->trigger(fromGigLfoWave(pRegion->LFO2WaveForm), + pRegion->LFO2Frequency, + pRegion->LFO2Phase, + LFO::start_level_mid, // see https://sourceforge.net/p/linuxsampler/mailman/linuxsampler-devel/thread/2189307.cNP0Xbctxq%40silver/#msg36774029 lfo2_internal_depth, pRegion->LFO2ControlDepth, pRegion->LFO2FlipPhase, pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); - pLFO2->update(pLFO2->ExtController ? GetGigEngineChannel()->ControllerTable[pLFO2->ExtController] : 0); + pLFO2->updateByMIDICtrlValue(pLFO2->ExtController ? GetGigEngineChannel()->ControllerTable[pLFO2->ExtController] : 0); + pLFO2->setScriptDepthFactor( + pNote->Override.CutoffLFODepth.Value, + pNote->Override.CutoffLFODepth.Final + ); + if (pNote->Override.CutoffLFOFreq.isFinal()) + pLFO2->setScriptFrequencyFinal(pNote->Override.CutoffLFOFreq.Value, pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); + else + pLFO2->setScriptFrequencyFactor(pNote->Override.CutoffLFOFreq.Value, pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); } } @@ -332,7 +433,7 @@ break; case ::gig::lfo3_ctrl_aftertouch: lfo3_internal_depth = 0; - pLFO3->ExtController = 128; + pLFO3->ExtController = CTRL_TABLE_IDX_AFTERTOUCH; bLFO3Enabled = true; break; case ::gig::lfo3_ctrl_internal_modwheel: @@ -342,7 +443,7 @@ break; case ::gig::lfo3_ctrl_internal_aftertouch: lfo3_internal_depth = pRegion->LFO3InternalDepth; - pLFO3->ExtController = 128; + pLFO3->ExtController = CTRL_TABLE_IDX_AFTERTOUCH; bLFO3Enabled = (lfo3_internal_depth > 0 || pRegion->LFO3ControlDepth > 0); break; default: @@ -351,38 +452,82 @@ bLFO3Enabled = false; } if (bLFO3Enabled) { - pLFO3->trigger(pRegion->LFO3Frequency, - start_level_mid, + pLFO3->trigger(fromGigLfoWave(pRegion->LFO3WaveForm), + pRegion->LFO3Frequency, + pRegion->LFO3Phase, + LFO::start_level_max, // see https://sourceforge.net/p/linuxsampler/mailman/linuxsampler-devel/thread/2189307.cNP0Xbctxq%40silver/#msg36774029 lfo3_internal_depth, pRegion->LFO3ControlDepth, - false, + pRegion->LFO3FlipPhase, pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); - pLFO3->update(pLFO3->ExtController ? GetGigEngineChannel()->ControllerTable[pLFO3->ExtController] : 0); + pLFO3->updateByMIDICtrlValue(pLFO3->ExtController ? GetGigEngineChannel()->ControllerTable[pLFO3->ExtController] : 0); + pLFO3->setScriptDepthFactor( + pNote->Override.PitchLFODepth.Value, + pNote->Override.PitchLFODepth.Final + ); + if (pNote->Override.PitchLFOFreq.isFinal()) + pLFO3->setScriptFrequencyFinal(pNote->Override.PitchLFOFreq.Value, pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); + else + pLFO3->setScriptFrequencyFactor(pNote->Override.PitchLFOFreq.Value, pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); } } float Voice::CalculateCutoffBase(uint8_t MIDIKeyVelocity) { float cutoff = pRegion->GetVelocityCutoff(MIDIKeyVelocity); if (pRegion->VCFKeyboardTracking) { - cutoff *= RTMath::CentsToFreqRatioUnlimited((MIDIKey - pRegion->VCFKeyboardTrackingBreakpoint) * 100); + cutoff *= RTMath::CentsToFreqRatioUnlimited((MIDIKey() - pRegion->VCFKeyboardTrackingBreakpoint) * 100); } return cutoff; } + // This is just called when the voice is triggered. On any subsequent cutoff + // controller changes ProcessCutoffEvent() is called instead. float Voice::CalculateFinalCutoff(float cutoffBase) { - int cvalue; + // if the selected filter type is an official GigaStudio filter type + // then we preserve the original (no matter how odd) historical GSt + // behaviour identically; for our own filter types though we deviate to + // more meaningful behaviours where appropriate + const bool isGStFilter = isGStFilterType(pRegion->VCFType); + + // get current cutoff CC or velocity value (always 0..127) + float cvalue; if (VCFCutoffCtrl.controller) { cvalue = GetGigEngineChannel()->ControllerTable[VCFCutoffCtrl.controller]; if (pRegion->VCFCutoffControllerInvert) cvalue = 127 - cvalue; - // VCFVelocityScale in this case means Minimum cutoff - if (cvalue < pRegion->VCFVelocityScale) cvalue = pRegion->VCFVelocityScale; - } - else { + if (isGStFilter) { + // VCFVelocityScale in this case means "minimum cutoff" for GSt + if (cvalue < MinCutoff()) cvalue = MinCutoff(); + } else { + // for our own filter types we interpret "minimum cutoff" + // differently: GSt handles this as a simple hard limit with the + // consequence that a certain range of the controller is simply + // dead; so for our filter types we rather remap that to + // restrain within the min_cutoff..127 range as well, but + // effectively spanned over the entire controller range (0..127) + // to avoid such a "dead" lower controller zone + cvalue = MinCutoff() + (cvalue / 127.f) * float(127 - MinCutoff()); + } + } else { + // in case of velocity, VCFVelocityScale parameter is already + // handled on libgig side (so by calling + // pRegion->GetVelocityCutoff(velo) in CalculateCutoffBase() above) cvalue = pRegion->VCFCutoff; } - float fco = cutoffBase * float(cvalue); + + float fco = cutoffBase * cvalue; if (fco > 127.0f) fco = 127.0f; + // the filter implementations of the original GSt filter types take an + // abstract cutoff parameter range of 0..127, ... + if (isGStFilter) + return fco; + + // ... whereas our own filter types take a cutoff parameter in Hz, so + // remap here 0 .. 127 [lin] -> 21 Hz .. 18 kHz [x^4] (center @2.2 kHz) + fco = (fco + 29.f) / (127.f + 29.f); + fco = fco * fco * fco * fco * 18000.f; + if (fco > 0.49f * pEngine->SampleRate) + fco = 0.49f * pEngine->SampleRate; return fco; } @@ -417,7 +562,7 @@ ctrl = 83; break; case ::gig::vcf_cutoff_ctrl_aftertouch: - ctrl = 128; + ctrl = CTRL_TABLE_IDX_AFTERTOUCH; break; case ::gig::vcf_cutoff_ctrl_none: default: @@ -452,44 +597,117 @@ } void Voice::TriggerEG1(const EGInfo& egInfo, double velrelease, double velocityAttenuation, uint sampleRate, uint8_t velocity) { - EG1.trigger(uint(RgnInfo.EG1PreAttack), - RgnInfo.EG1Attack * egInfo.Attack, - RgnInfo.EG1Hold, - RgnInfo.EG1Decay1 * egInfo.Decay * velrelease, - RgnInfo.EG1Decay2 * egInfo.Decay * velrelease, - RgnInfo.EG1InfiniteSustain, - uint(RgnInfo.EG1Sustain), - RgnInfo.EG1Release * egInfo.Release * velrelease, + EG1.setStateOptions( + pRegion->EG1Options.AttackCancel, + pRegion->EG1Options.AttackHoldCancel, + pRegion->EG1Options.Decay1Cancel, + pRegion->EG1Options.Decay2Cancel, + pRegion->EG1Options.ReleaseCancel + ); + EG1.trigger(pRegion->EG1PreAttack, + (pNote && pNote->Override.Attack.isFinal()) ? + pNote->Override.Attack.Value : + RTMath::Max(pRegion->EG1Attack, 0.0316) * egInfo.Attack, + pRegion->EG1Hold, + (pNote && pNote->Override.Decay.isFinal()) ? + pNote->Override.Decay.Value : + pRegion->EG1Decay1 * egInfo.Decay * velrelease, + (pNote && pNote->Override.Decay.isFinal()) ? + pNote->Override.Decay.Value : + pRegion->EG1Decay2 * egInfo.Decay * velrelease, + pRegion->EG1InfiniteSustain, + (pNote && pNote->Override.Sustain.Final) ? + uint(pNote->Override.Sustain.Value * 1000.f) : + pRegion->EG1Sustain * (pNote ? pNote->Override.Sustain.Value : 1.f), + (pNote && pNote->Override.Release.isFinal()) ? + pNote->Override.Release.Value : + RTMath::Max(pRegion->EG1Release * velrelease, 0.014) * egInfo.Release, velocityAttenuation, sampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); } void Voice::TriggerEG2(const EGInfo& egInfo, double velrelease, double velocityAttenuation, uint sampleRate, uint8_t velocity) { + EG2.setStateOptions( + pRegion->EG2Options.AttackCancel, + pRegion->EG2Options.AttackHoldCancel, + pRegion->EG2Options.Decay1Cancel, + pRegion->EG2Options.Decay2Cancel, + pRegion->EG2Options.ReleaseCancel + ); EG2.trigger(uint(RgnInfo.EG2PreAttack), - RgnInfo.EG2Attack * egInfo.Attack, + (pNote && pNote->Override.CutoffAttack.isFinal()) ? + pNote->Override.CutoffAttack.Value : + RgnInfo.EG2Attack * egInfo.Attack, false, - RgnInfo.EG2Decay1 * egInfo.Decay * velrelease, - RgnInfo.EG2Decay2 * egInfo.Decay * velrelease, + (pNote && pNote->Override.CutoffDecay.isFinal()) ? + pNote->Override.CutoffDecay.Value : + RgnInfo.EG2Decay1 * egInfo.Decay * velrelease, + (pNote && pNote->Override.CutoffDecay.isFinal()) ? + pNote->Override.CutoffDecay.Value : + RgnInfo.EG2Decay2 * egInfo.Decay * velrelease, RgnInfo.EG2InfiniteSustain, - uint(RgnInfo.EG2Sustain), - RgnInfo.EG2Release * egInfo.Release * velrelease, + (pNote && pNote->Override.CutoffSustain.Final) ? + uint(pNote->Override.CutoffSustain.Value * 1000.f) : + uint(RgnInfo.EG2Sustain), + (pNote && pNote->Override.CutoffRelease.isFinal()) ? + pNote->Override.CutoffRelease.Value : + RgnInfo.EG2Release * egInfo.Release * velrelease, velocityAttenuation, sampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); } void Voice::ProcessGroupEvent(RTList::Iterator& itEvent) { - dmsg(4,("Voice %x processGroupEvents event type=%d", this, itEvent->Type)); + dmsg(4,("Voice %p processGroupEvents event type=%d", (void*)this, itEvent->Type)); // TODO: The SustainPedal condition could be wrong, maybe the // check should be if this Voice is in release stage or is a // release sample instead. Need to test this in GSt. - if (itEvent->Param.Note.Key != MIDIKey || - !GetGigEngineChannel()->SustainPedal) { - dmsg(4,("Voice %x - kill", this)); + // -- Andreas + // + // Commented sustain pedal check out. I don't think voices of the same + // note should be stopped at all, because it doesn't sound naturally + // with a drumkit. + // -- Christian, 2013-01-08 + if (itEvent->Param.Note.Key != HostKey() /*|| + !GetGigEngineChannel()->SustainPedal*/) { + dmsg(4,("Voice %p - kill", (void*)this)); // kill the voice fast pEG1->enterFadeOutStage(); } } + void Voice::CalculateFadeOutCoeff(float FadeOutTime, float SampleRate) { + EG1.CalculateFadeOutCoeff(FadeOutTime, SampleRate); + } + + int Voice::CalculatePan(uint8_t pan) { + int p; + // Gst behaviour: -64 and 63 are special cases + if (RgnInfo.Pan == -64) p = pan * 2 - 127; + else if (RgnInfo.Pan == 63) p = pan * 2; + else p = pan + RgnInfo.Pan; + + if (p < 0) return 0; + if (p > 127) return 127; + return p; + } + + release_trigger_t Voice::GetReleaseTriggerFlags() { + release_trigger_t flags = + (pRegion->NoNoteOffReleaseTrigger) ? + 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 + switch (pRegion->SustainReleaseTrigger) { + case ::gig::sust_rel_trg_none: + break; + case ::gig::sust_rel_trg_maxvelocity: + flags |= release_trigger_sustain_maxvelocity; + break; + case ::gig::sust_rel_trg_keyvelocity: + flags |= release_trigger_sustain_keyvelocity; + break; + } + return flags; + } + }} // namespace LinuxSampler::gig