--- linuxsampler/trunk/src/engines/sfz/Voice.cpp 2010/08/10 12:05:19 2114 +++ linuxsampler/trunk/src/engines/sfz/Voice.cpp 2013/01/20 19:01:53 2403 @@ -4,7 +4,7 @@ * * * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * * Copyright (C) 2005 - 2008 Christian Schoenebeck * - * Copyright (C) 2009 - 2010 Christian Schoenebeck and Grigor Iliev * + * Copyright (C) 2009 - 2013 Christian Schoenebeck and Grigor Iliev * * * * 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 * @@ -31,8 +31,10 @@ namespace LinuxSampler { namespace sfz { - Voice::Voice() { + typedef LinuxSampler::VoiceBase SfzVoice; + Voice::Voice(): SignalRack(this), SfzVoice(&SignalRack) { pEngine = NULL; + bEqSupport = true; } Voice::~Voice() { @@ -71,31 +73,50 @@ ri.UnityNote = pRegion->pitch_keycenter; ri.FineTune = pRegion->tune + pRegion->transpose * 100; ri.Pan = int(pRegion->pan * 0.63); // convert from -100..100 to -64..63 - ri.SampleStartOffset = 0; // TODO: + ri.SampleStartOffset = pRegion->offset ? *(pRegion->offset) : 0; - ri.EG1PreAttack = pRegion->ampeg_start * 10; - ri.EG1Attack = pRegion->ampeg_attack; - ri.EG1Hold = pRegion->ampeg_hold; - ri.EG1Decay1 = pRegion->ampeg_decay; - ri.EG1Decay2 = pRegion->ampeg_decay; - ri.EG1Sustain = pRegion->ampeg_sustain * 10; - ri.EG1InfiniteSustain = true; - ri.EG1Release = pRegion->ampeg_release; - - ri.EG2PreAttack = pRegion->fileg_start * 10; - ri.EG2Attack = pRegion->fileg_attack; - //ri.EG2Hold = pRegion->fileg_hold; // TODO: - ri.EG2Decay1 = pRegion->fileg_decay; - ri.EG2Decay2 = pRegion->fileg_decay; - ri.EG2Sustain = pRegion->fileg_sustain * 10; - ri.EG2InfiniteSustain = true; - ri.EG2Release = pRegion->fileg_release; - - ri.EG3Attack = pRegion->pitcheg_attack; - ri.EG3Depth = 0; // TODO: - ri.VCFEnabled = false; // TODO: - ri.VCFType = ::gig::vcf_type_lowpass; // TODO: - ri.VCFResonance = 0; // TODO: + ri.VCFEnabled = pRegion->cutoff; + switch (pRegion->fil_type) { + case ::sfz::LPF_1P: + ri.VCFType = Filter::vcf_type_1p_lowpass; + break; + case ::sfz::LPF_2P: + ri.VCFType = Filter::vcf_type_2p_lowpass; + break; + case ::sfz::LPF_4P: + ri.VCFType = Filter::vcf_type_4p_lowpass; + break; + case ::sfz::LPF_6P: + ri.VCFType = Filter::vcf_type_6p_lowpass; + break; + case ::sfz::HPF_1P: + ri.VCFType = Filter::vcf_type_1p_highpass; + break; + case ::sfz::HPF_2P: + ri.VCFType = Filter::vcf_type_2p_highpass; + break; + case ::sfz::HPF_4P: + ri.VCFType = Filter::vcf_type_4p_highpass; + break; + case ::sfz::HPF_6P: + ri.VCFType = Filter::vcf_type_6p_highpass; + break; + case ::sfz::BPF_1P: + case ::sfz::BPF_2P: + ri.VCFType = Filter::vcf_type_2p_bandpass; + break; + case ::sfz::BRF_1P: + case ::sfz::BRF_2P: + ri.VCFType = Filter::vcf_type_2p_bandreject; + break; + case ::sfz::APF_1P: + case ::sfz::PKF_2P: + default: + ri.VCFEnabled = false; + break; + } + + ri.VCFResonance = pRegion->resonance; // rt_decay is in dB. Precalculate a suitable value for exp in // GetReleaseTriggerAttenuation: -ln(10) / 20 * rt_decay @@ -113,15 +134,17 @@ } double Voice::GetSampleAttenuation() { - return exp(LN_10_DIV_20 * pRegion->volume); + return exp(LN_10_DIV_20 * pRegion->volume) * pRegion->amplitude / 100; } double Voice::GetVelocityAttenuation(uint8_t MIDIKeyVelocity) { - return pRegion->amp_velcurve[MIDIKeyVelocity]; + float offset = -pRegion->amp_veltrack; + if (offset <= 0) offset += 100; + return (offset + pRegion->amp_veltrack * pRegion->amp_velcurve[MIDIKeyVelocity]) / 100; } double Voice::GetVelocityRelease(uint8_t MIDIKeyVelocity) { - return 0.9; // TODO: + return 127.0 / MIDIKeyVelocity; } void Voice::ProcessCCEvent(RTList::Iterator& itEvent) { @@ -133,19 +156,6 @@ }*/ // TODO: ^^^ } - 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 (cutoff > 127.0f) cutoff = 127.0f; - - VCFCutoffCtrl.fvalue = cutoff; // needed for initialization of fFinalCutoff next time - fFinalCutoff = cutoff;*/ // TODO: ^^^ - } - double Voice::CalculateCrossfadeVolume(uint8_t MIDIKeyVelocity) { /*float crossfadeVolume; switch (pRegion->AttenuationController.type) { @@ -206,32 +216,7 @@ return eg; } - void Voice::TriggerEG1(const EGInfo& egInfo, double velrelease, double velocityAttenuation, uint sampleRate, uint8_t velocity) { - - // TODO: controller modulation - - // first check if there is a v2 EG for amplitude - for (int i = 0 ; i < pRegion->eg.size() ; i++) { - if (pRegion->eg[i].amplitude > 0) { - // TODO: actually use the value of the amplitude parameter - pEG1 = &EG1; - EG1.trigger(pRegion->eg[i], sampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE, velocity); - return; - } - } - - // otherwise use the v1 EGADSR - pEG1 = &EGADSR1; - EGADSR1.trigger(uint(RgnInfo.EG1PreAttack), - RgnInfo.EG1Attack, - RgnInfo.EG1Hold, - RgnInfo.EG1Decay1, - uint(RgnInfo.EG1Sustain), - RgnInfo.EG1Release, - sampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); - } - - double Voice::GetEG2ControllerValue(uint8_t MIDIKeyVelocity) { + double Voice::GetEG2ControllerValue(uint8_t MIDIKeyVelocity) { /*double eg2controllervalue = 0; switch (pRegion->EG2Controller.type) { case ::gig::eg2_ctrl_t::type_none: // no controller defined @@ -267,233 +252,18 @@ return eg; } - void Voice::InitLFO1() { - /*uint16_t lfo1_internal_depth; - switch (pRegion->LFO1Controller) { - case ::gig::lfo1_ctrl_internal: - lfo1_internal_depth = pRegion->LFO1InternalDepth; - pLFO1->ExtController = 0; // no external controller - bLFO1Enabled = (lfo1_internal_depth > 0); - break; - case ::gig::lfo1_ctrl_modwheel: - lfo1_internal_depth = 0; - pLFO1->ExtController = 1; // MIDI controller 1 - bLFO1Enabled = (pRegion->LFO1ControlDepth > 0); - break; - case ::gig::lfo1_ctrl_breath: - lfo1_internal_depth = 0; - pLFO1->ExtController = 2; // MIDI controller 2 - bLFO1Enabled = (pRegion->LFO1ControlDepth > 0); - break; - case ::gig::lfo1_ctrl_internal_modwheel: - lfo1_internal_depth = pRegion->LFO1InternalDepth; - pLFO1->ExtController = 1; // MIDI controller 1 - bLFO1Enabled = (lfo1_internal_depth > 0 || pRegion->LFO1ControlDepth > 0); - break; - case ::gig::lfo1_ctrl_internal_breath: - lfo1_internal_depth = pRegion->LFO1InternalDepth; - pLFO1->ExtController = 2; // MIDI controller 2 - bLFO1Enabled = (lfo1_internal_depth > 0 || pRegion->LFO1ControlDepth > 0); - break; - default: - lfo1_internal_depth = 0; - pLFO1->ExtController = 0; // no external controller - bLFO1Enabled = false; - } - if (bLFO1Enabled) { - pLFO1->trigger(pRegion->LFO1Frequency, - start_level_min, - lfo1_internal_depth, - pRegion->LFO1ControlDepth, - pRegion->LFO1FlipPhase, - pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); - pLFO1->update(pLFO1->ExtController ? GetSfzEngineChannel()->ControllerTable[pLFO1->ExtController] : 0); - }*/ // TODO: ^^^ - bLFO1Enabled = false; - } - - void Voice::InitLFO2() { - /*uint16_t lfo2_internal_depth; - switch (pRegion->LFO2Controller) { - case ::gig::lfo2_ctrl_internal: - lfo2_internal_depth = pRegion->LFO2InternalDepth; - pLFO2->ExtController = 0; // no external controller - bLFO2Enabled = (lfo2_internal_depth > 0); - break; - case ::gig::lfo2_ctrl_modwheel: - lfo2_internal_depth = 0; - pLFO2->ExtController = 1; // MIDI controller 1 - bLFO2Enabled = (pRegion->LFO2ControlDepth > 0); - break; - case ::gig::lfo2_ctrl_foot: - lfo2_internal_depth = 0; - pLFO2->ExtController = 4; // MIDI controller 4 - bLFO2Enabled = (pRegion->LFO2ControlDepth > 0); - break; - case ::gig::lfo2_ctrl_internal_modwheel: - lfo2_internal_depth = pRegion->LFO2InternalDepth; - pLFO2->ExtController = 1; // MIDI controller 1 - bLFO2Enabled = (lfo2_internal_depth > 0 || pRegion->LFO2ControlDepth > 0); - break; - case ::gig::lfo2_ctrl_internal_foot: - lfo2_internal_depth = pRegion->LFO2InternalDepth; - pLFO2->ExtController = 4; // MIDI controller 4 - bLFO2Enabled = (lfo2_internal_depth > 0 || pRegion->LFO2ControlDepth > 0); - break; - default: - lfo2_internal_depth = 0; - pLFO2->ExtController = 0; // no external controller - bLFO2Enabled = false; - } - if (bLFO2Enabled) { - pLFO2->trigger(pRegion->LFO2Frequency, - start_level_max, - lfo2_internal_depth, - pRegion->LFO2ControlDepth, - pRegion->LFO2FlipPhase, - pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); - pLFO2->update(pLFO2->ExtController ? GetSfzEngineChannel()->ControllerTable[pLFO2->ExtController] : 0); - }*/ // TODO: ^^^ - bLFO2Enabled = false; - } - - void Voice::InitLFO3() { - /*uint16_t lfo3_internal_depth; - switch (pRegion->LFO3Controller) { - case ::gig::lfo3_ctrl_internal: - lfo3_internal_depth = pRegion->LFO3InternalDepth; - pLFO3->ExtController = 0; // no external controller - bLFO3Enabled = (lfo3_internal_depth > 0); - break; - case ::gig::lfo3_ctrl_modwheel: - lfo3_internal_depth = 0; - pLFO3->ExtController = 1; // MIDI controller 1 - bLFO3Enabled = (pRegion->LFO3ControlDepth > 0); - break; - case ::gig::lfo3_ctrl_aftertouch: - lfo3_internal_depth = 0; - pLFO3->ExtController = 128; - bLFO3Enabled = true; - break; - case ::gig::lfo3_ctrl_internal_modwheel: - lfo3_internal_depth = pRegion->LFO3InternalDepth; - pLFO3->ExtController = 1; // MIDI controller 1 - bLFO3Enabled = (lfo3_internal_depth > 0 || pRegion->LFO3ControlDepth > 0); - break; - case ::gig::lfo3_ctrl_internal_aftertouch: - lfo3_internal_depth = pRegion->LFO3InternalDepth; - pLFO1->ExtController = 128; - bLFO3Enabled = (lfo3_internal_depth > 0 || pRegion->LFO3ControlDepth > 0); - break; - default: - lfo3_internal_depth = 0; - pLFO3->ExtController = 0; // no external controller - bLFO3Enabled = false; - } - if (bLFO3Enabled) { - pLFO3->trigger(pRegion->LFO3Frequency, - start_level_mid, - lfo3_internal_depth, - pRegion->LFO3ControlDepth, - false, - pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); - pLFO3->update(pLFO3->ExtController ? GetSfzEngineChannel()->ControllerTable[pLFO3->ExtController] : 0); - }*/ // TODO: ^^^ - bLFO3Enabled = false; - } - float Voice::CalculateCutoffBase(uint8_t MIDIKeyVelocity) { - /*float cutoff = pRegion->GetVelocityCutoff(MIDIKeyVelocity); - if (pRegion->VCFKeyboardTracking) { - cutoff *= exp((MIDIKeyVelocity - pRegion->VCFKeyboardTrackingBreakpoint) * 0.057762265f); // (ln(2) / 12) - } - return cutoff;*/ // TODO: ^^^ - return 1.0f; + float cutoff = *pRegion->cutoff; + cutoff *= RTMath::CentsToFreqRatioUnlimited( + MIDIKeyVelocity / 127.0f * pRegion->fil_veltrack + + (MIDIKey - pRegion->fil_keycenter) * pRegion->fil_keytrack); + return cutoff; } float Voice::CalculateFinalCutoff(float cutoffBase) { - /*int cvalue; - if (VCFCutoffCtrl.controller) { - cvalue = GetSfzEngineChannel()->ControllerTable[VCFCutoffCtrl.controller]; - if (pRegion->VCFCutoffControllerInvert) cvalue = 127 - cvalue; - // VCFVelocityScale in this case means Minimum cutoff - if (cvalue < pRegion->VCFVelocityScale) cvalue = pRegion->VCFVelocityScale; - } - else { - cvalue = pRegion->VCFCutoff; - } - float fco = cutoffBase * float(cvalue); - if (fco > 127.0f) fco = 127.0f; - - return fco;*/ // TODO: ^^^ - return 127.0f; - } - - uint8_t Voice::GetVCFCutoffCtrl() { - /*uint8_t ctrl; - switch (pRegion->VCFCutoffController) { - case ::gig::vcf_cutoff_ctrl_modwheel: - ctrl = 1; - break; - case ::gig::vcf_cutoff_ctrl_effect1: - ctrl = 12; - break; - case ::gig::vcf_cutoff_ctrl_effect2: - ctrl = 13; - break; - case ::gig::vcf_cutoff_ctrl_breath: - ctrl = 2; - break; - case ::gig::vcf_cutoff_ctrl_foot: - ctrl = 4; - break; - case ::gig::vcf_cutoff_ctrl_sustainpedal: - ctrl = 64; - break; - case ::gig::vcf_cutoff_ctrl_softpedal: - ctrl = 67; - break; - case ::gig::vcf_cutoff_ctrl_genpurpose7: - ctrl = 82; - break; - case ::gig::vcf_cutoff_ctrl_genpurpose8: - ctrl = 83; - break; - case ::gig::vcf_cutoff_ctrl_aftertouch: - ctrl = 128; - break; - case ::gig::vcf_cutoff_ctrl_none: - default: - ctrl = 0; - break; - } - - return ctrl;*/ // TODO: ^^^ - return 0; - } - - uint8_t Voice::GetVCFResonanceCtrl() { - /*uint8_t ctrl; - switch (pRegion->VCFResonanceController) { - case ::gig::vcf_res_ctrl_genpurpose3: - ctrl = 18; - break; - case ::gig::vcf_res_ctrl_genpurpose4: - ctrl = 19; - break; - case ::gig::vcf_res_ctrl_genpurpose5: - ctrl = 80; - break; - case ::gig::vcf_res_ctrl_genpurpose6: - ctrl = 81; - break; - case ::gig::vcf_res_ctrl_none: - default: - ctrl = 0; - } - - return ctrl;*/ // TODO: ^^^ - return 0; + float cutoff = cutoffBase; + if (cutoff > 0.49 * pEngine->SampleRate) cutoff = 0.49 * pEngine->SampleRate; + return cutoff; } float Voice::GetReleaseTriggerAttenuation(float noteLength) { @@ -503,16 +273,39 @@ void Voice::ProcessGroupEvent(RTList::Iterator& itEvent) { dmsg(4,("Voice %x processGroupEvents event type=%d", this, itEvent->Type)); - if (itEvent->Param.Note.Key != MIDIKey) { + if (itEvent->Type == Event::type_control_change || + (Type & Voice::type_controller_triggered) || + itEvent->Param.Note.Key != MIDIKey) { dmsg(4,("Voice %x - kill", this)); if (pRegion->off_mode == ::sfz::OFF_NORMAL) { // turn off the voice by entering release envelope stage EnterReleaseStage(); } else { // kill the voice fast - pEG1->enterFadeOutStage(); + SignalRack.EnterFadeOutStage(); } } } + + void Voice::SetSampleStartOffset() { + if (DiskVoice && RgnInfo.SampleStartOffset > pSample->MaxOffset) { + // The offset is applied to the RAM buffer + finalSynthesisParameters.dPos = 0; + Pos = 0; + } else { + finalSynthesisParameters.dPos = RgnInfo.SampleStartOffset; // offset where we should start playback of sample + Pos = RgnInfo.SampleStartOffset; + } + } + + void Voice::CalculateFadeOutCoeff(float FadeOutTime, float SampleRate) { + SignalRack.CalculateFadeOutCoeff(FadeOutTime, SampleRate); + } + + int Voice::CalculatePan(uint8_t pan) { + // the value isn't limited to [0, 127] here, as this is done + // later in SignalUnit.CalculatePan + return pan + RgnInfo.Pan; + } }} // namespace LinuxSampler::sfz