--- linuxsampler/trunk/src/engines/common/AbstractVoice.cpp 2009/11/20 20:13:08 2032 +++ linuxsampler/trunk/src/engines/common/AbstractVoice.cpp 2011/12/09 17:04:24 2298 @@ -3,8 +3,8 @@ * LinuxSampler - modular, streaming capable sampler * * * * Copyright (C) 2003,2004 by Benno Senoner and Christian Schoenebeck * - * Copyright (C) 2005-2009 Christian Schoenebeck * - * Copyright (C) 2009 Grigor Iliev * + * Copyright (C) 2005-2008 Christian Schoenebeck * + * Copyright (C) 2009-2011 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 * @@ -26,13 +26,12 @@ namespace LinuxSampler { - AbstractVoice::AbstractVoice() { + AbstractVoice::AbstractVoice(SignalUnitRack* pRack): pSignalUnitRack(pRack) { pEngineChannel = NULL; - pLFO1 = new LFOUnsigned(1.0f); // amplitude EG (0..1 range) - pLFO2 = new LFOUnsigned(1.0f); // filter EG (0..1 range) - pLFO3 = new LFOSigned(1200.0f); // pitch EG (-1200..+1200 range) + pLFO1 = new LFOUnsigned(1.0f); // amplitude LFO (0..1 range) + pLFO2 = new LFOUnsigned(1.0f); // filter LFO (0..1 range) + pLFO3 = new LFOSigned(1200.0f); // pitch LFO (-1200..+1200 range) PlaybackState = playback_state_end; - KeyGroup = 0; SynthesisMode = 0; // set all mode bits to 0 first // select synthesis implementation (asm core is not supported ATM) #if 0 // CONFIG_ASM && ARCH_X86 @@ -44,14 +43,26 @@ finalSynthesisParameters.filterLeft.Reset(); finalSynthesisParameters.filterRight.Reset(); + + pEq = NULL; + bEqSupport = false; } AbstractVoice::~AbstractVoice() { if (pLFO1) delete pLFO1; if (pLFO2) delete pLFO2; if (pLFO3) delete pLFO3; + + if(pEq != NULL) delete pEq; } - + + void AbstractVoice::CreateEq() { + if(!bEqSupport) return; + if(pEq != NULL) delete pEq; + pEq = new EqSupport; + pEq->InitEffect(GetEngine()->pAudioOutputDevice); + } + /** * Resets voice variables. Should only be called if rendering process is * suspended / not running. @@ -100,19 +111,26 @@ Type = VoiceType; MIDIKey = itNoteOnEvent->Param.Note.Key; + MIDIVelocity = itNoteOnEvent->Param.Note.Velocity; + MIDIPan = pEngineChannel->ControllerTable[10]; + if (MIDIPan == 0 && pEngineChannel->GlobalPanRight == 1) MIDIPan = 64; // workaround used to determine whether the MIDI pan has not been set PlaybackState = playback_state_init; // mark voice as triggered, but no audio rendered yet Delay = itNoteOnEvent->FragmentPos(); itTriggerEvent = itNoteOnEvent; itKillEvent = Pool::Iterator(); - KeyGroup = iKeyGroup; + MidiKeyBase* pKeyInfo = GetMidiKeyInfo(MIDIKey); + + pGroupEvents = iKeyGroup ? pEngineChannel->ActiveKeyGroups[iKeyGroup] : 0; SmplInfo = GetSampleInfo(); RgnInfo = GetRegionInfo(); InstrInfo = GetInstrumentInfo(); + + AboutToTrigger(); // calculate volume const double velocityAttenuation = GetVelocityAttenuation(itNoteOnEvent->Param.Note.Velocity); - float volume = CalculateVolume(velocityAttenuation); + float volume = CalculateVolume(velocityAttenuation) * pKeyInfo->Volume; if (volume <= 0) return -1; // select channel mode (mono or stereo) @@ -123,8 +141,8 @@ // get starting crossfade volume level float crossfadeVolume = CalculateCrossfadeVolume(itNoteOnEvent->Param.Note.Velocity); - VolumeLeft = volume * AbstractEngine::PanCurve[64 - RgnInfo.Pan]; - VolumeRight = volume * AbstractEngine::PanCurve[64 + RgnInfo.Pan]; + VolumeLeft = volume * pKeyInfo->PanLeft * AbstractEngine::PanCurve[64 - RgnInfo.Pan]; + VolumeRight = volume * pKeyInfo->PanRight * AbstractEngine::PanCurve[64 + RgnInfo.Pan]; float subfragmentRate = GetEngine()->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE; CrossfadeSmoother.trigger(crossfadeVolume, subfragmentRate); @@ -132,13 +150,12 @@ PanLeftSmoother.trigger(pEngineChannel->GlobalPanLeft, subfragmentRate); PanRightSmoother.trigger(pEngineChannel->GlobalPanRight, subfragmentRate); - finalSynthesisParameters.dPos = RgnInfo.SampleStartOffset; // offset where we should start playback of sample (0 - 2000 sample points) - Pos = RgnInfo.SampleStartOffset; - // Check if the sample needs disk streaming or is too short for that long cachedsamples = GetSampleCacheSize() / SmplInfo.FrameSize; DiskVoice = cachedsamples < SmplInfo.TotalFrameCount; + SetSampleStartOffset(); + if (DiskVoice) { // voice to be streamed from disk if (cachedsamples > (GetEngine()->MaxSamplesPerCycle << CONFIG_MAX_PITCH)) { MaxRAMPos = cachedsamples - (GetEngine()->MaxSamplesPerCycle << CONFIG_MAX_PITCH) / SmplInfo.ChannelCount; //TODO: this calculation is too pessimistic and may better be moved to Render() method, so it calculates MaxRAMPos dependent to the current demand of sample points to be rendered (e.g. in case of JACK) @@ -175,26 +192,16 @@ // the length of the decay and release curves are dependent on the velocity const double velrelease = 1 / GetVelocityRelease(itNoteOnEvent->Param.Note.Velocity); - // setup EG 1 (VCA EG) - { + if (pSignalUnitRack == NULL) { // setup EG 1 (VCA EG) // get current value of EG1 controller double eg1controllervalue = GetEG1ControllerValue(itNoteOnEvent->Param.Note.Velocity); // calculate influence of EG1 controller on EG1's parameters EGInfo egInfo = CalculateEG1ControllerInfluence(eg1controllervalue); - EG1.trigger ( - RgnInfo.EG1PreAttack, - RgnInfo.EG1Attack * egInfo.Attack, - RgnInfo.EG1Hold, - RgnInfo.EG1Decay1 * egInfo.Decay * velrelease, - RgnInfo.EG1Decay2 * egInfo.Decay * velrelease, - RgnInfo.EG1InfiniteSustain, - RgnInfo.EG1Sustain, - RgnInfo.EG1Release * egInfo.Release * velrelease, - velocityAttenuation, - GetEngine()->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE - ); + TriggerEG1(egInfo, velrelease, velocityAttenuation, GetEngine()->SampleRate, itNoteOnEvent->Param.Note.Velocity); + } else { + pSignalUnitRack->Trigger(); } #ifdef CONFIG_INTERPOLATE_VOLUME @@ -207,7 +214,12 @@ else #else { - float finalVolume = pEngineChannel->MidiVolume * crossfadeVolume * EG1.getLevel(); + float finalVolume; + if (pSignalUnitRack == NULL) { + finalVolume = pEngineChannel->MidiVolume * crossfadeVolume * pEG1->getLevel(); + } else { + finalVolume = pEngineChannel->MidiVolume * crossfadeVolume * pSignalUnitRack->GetEndpointUnit()->GetVolume(); + } finalSynthesisParameters.fFinalVolumeLeft = finalVolume * VolumeLeft * pEngineChannel->GlobalPanLeft; finalSynthesisParameters.fFinalVolumeRight = finalVolume * VolumeRight * pEngineChannel->GlobalPanRight; @@ -215,52 +227,43 @@ #endif #endif - // setup EG 2 (VCF Cutoff EG) - { - // get current value of EG2 controller - double eg2controllervalue = GetEG2ControllerValue(itNoteOnEvent->Param.Note.Velocity); + if (pSignalUnitRack == NULL) { + // setup EG 2 (VCF Cutoff EG) + { + // get current value of EG2 controller + double eg2controllervalue = GetEG2ControllerValue(itNoteOnEvent->Param.Note.Velocity); + + // calculate influence of EG2 controller on EG2's parameters + EGInfo egInfo = CalculateEG2ControllerInfluence(eg2controllervalue); + + TriggerEG2(egInfo, velrelease, velocityAttenuation, GetEngine()->SampleRate, itNoteOnEvent->Param.Note.Velocity); + } - // calculate influence of EG2 controller on EG2's parameters - EGInfo egInfo = CalculateEG2ControllerInfluence(eg2controllervalue); - EG2.trigger ( - RgnInfo.EG2PreAttack, - RgnInfo.EG2Attack * egInfo.Attack, - false, - RgnInfo.EG2Decay1 * egInfo.Decay * velrelease, - RgnInfo.EG2Decay2 * egInfo.Decay * velrelease, - RgnInfo.EG2InfiniteSustain, - RgnInfo.EG2Sustain, - RgnInfo.EG2Release * egInfo.Release * velrelease, - velocityAttenuation, - GetEngine()->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE - ); - } + // setup EG 3 (VCO EG) + { + // if portamento mode is on, we dedicate EG3 purely for portamento, otherwise if portamento is off we do as told by the patch + bool bPortamento = pEngineChannel->PortamentoMode && pEngineChannel->PortamentoPos >= 0.0f; + float eg3depth = (bPortamento) + ? RTMath::CentsToFreqRatio((pEngineChannel->PortamentoPos - (float) MIDIKey) * 100) + : RTMath::CentsToFreqRatio(RgnInfo.EG3Depth); + float eg3time = (bPortamento) + ? pEngineChannel->PortamentoTime + : RgnInfo.EG3Attack; + EG3.trigger(eg3depth, eg3time, GetEngine()->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); + dmsg(5,("PortamentoPos=%f, depth=%f, time=%f\n", pEngineChannel->PortamentoPos, eg3depth, eg3time)); + } - // setup EG 3 (VCO EG) - { - // if portamento mode is on, we dedicate EG3 purely for portamento, otherwise if portamento is off we do as told by the patch - bool bPortamento = pEngineChannel->PortamentoMode && pEngineChannel->PortamentoPos >= 0.0f; - float eg3depth = (bPortamento) - ? RTMath::CentsToFreqRatio((pEngineChannel->PortamentoPos - (float) MIDIKey) * 100) - : RTMath::CentsToFreqRatio(RgnInfo.EG3Depth); - float eg3time = (bPortamento) - ? pEngineChannel->PortamentoTime - : RgnInfo.EG3Attack; - EG3.trigger(eg3depth, eg3time, GetEngine()->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); - dmsg(5,("PortamentoPos=%f, depth=%f, time=%f\n", pEngineChannel->PortamentoPos, eg3depth, eg3time)); + // setup LFO 1 (VCA LFO) + InitLFO1(); + // setup LFO 2 (VCF Cutoff LFO) + InitLFO2(); + // setup LFO 3 (VCO LFO) + InitLFO3(); } - // setup LFO 1 (VCA LFO) - InitLFO1(); - // setup LFO 2 (VCF Cutoff LFO) - InitLFO2(); - // setup LFO 3 (VCO LFO) - InitLFO3(); - - #if CONFIG_FORCE_FILTER const bool bUseFilter = true; #else // use filter only if instrument file told so @@ -306,6 +309,11 @@ return 0; // success } + + void AbstractVoice::SetSampleStartOffset() { + finalSynthesisParameters.dPos = RgnInfo.SampleStartOffset; // offset where we should start playback of sample (0 - 2000 sample points) + Pos = RgnInfo.SampleStartOffset; + } /** * Synthesizes the current audio fragment for this voice. @@ -316,17 +324,58 @@ * @param Skip - number of sample points to skip in output buffer */ void AbstractVoice::Synthesize(uint Samples, sample_t* pSrc, uint Skip) { + bool delay = false; // Whether the voice playback should be delayed for this call + + if (pSignalUnitRack != NULL) { + uint delaySteps = pSignalUnitRack->GetEndpointUnit()->DelayTrigger(); + if (delaySteps > 0) { // delay on the endpoint unit means delay of the voice playback + if (delaySteps >= Samples) { + pSignalUnitRack->GetEndpointUnit()->DecreaseDelay(Samples); + delay = true; + } else { + pSignalUnitRack->GetEndpointUnit()->DecreaseDelay(delaySteps); + Samples -= delaySteps; + Skip += delaySteps; + } + } + } + AbstractEngineChannel* pChannel = pEngineChannel; - finalSynthesisParameters.pOutLeft = &pChannel->pChannelLeft->Buffer()[Skip]; - finalSynthesisParameters.pOutRight = &pChannel->pChannelRight->Buffer()[Skip]; - finalSynthesisParameters.pSrc = pSrc; + MidiKeyBase* pMidiKeyInfo = GetMidiKeyInfo(MIDIKey); + + const bool bVoiceRequiresDedicatedRouting = + pEngineChannel->GetFxSendCount() > 0 && + (pMidiKeyInfo->ReverbSend || pMidiKeyInfo->ChorusSend); + + const bool bEq = + pSignalUnitRack != NULL && pSignalUnitRack->HasEq() && pEq->HasSupport(); + + if (bEq) { + pEq->GetInChannelLeft()->Clear(); + pEq->GetInChannelRight()->Clear(); + finalSynthesisParameters.pOutLeft = &pEq->GetInChannelLeft()->Buffer()[Skip]; + finalSynthesisParameters.pOutRight = &pEq->GetInChannelRight()->Buffer()[Skip]; + pSignalUnitRack->UpdateEqSettings(pEq); + } else if (bVoiceRequiresDedicatedRouting) { + finalSynthesisParameters.pOutLeft = &GetEngine()->pDedicatedVoiceChannelLeft->Buffer()[Skip]; + finalSynthesisParameters.pOutRight = &GetEngine()->pDedicatedVoiceChannelRight->Buffer()[Skip]; + } else { + finalSynthesisParameters.pOutLeft = &pChannel->pChannelLeft->Buffer()[Skip]; + finalSynthesisParameters.pOutRight = &pChannel->pChannelRight->Buffer()[Skip]; + } + finalSynthesisParameters.pSrc = pSrc; RTList::Iterator itCCEvent = pChannel->pEvents->first(); RTList::Iterator itNoteEvent; GetFirstEventOnKey(MIDIKey, itNoteEvent); + RTList::Iterator itGroupEvent; + if (pGroupEvents) itGroupEvent = pGroupEvents->first(); + if (itTriggerEvent) { // skip events that happened before this voice was triggered while (itCCEvent && itCCEvent->FragmentPos() <= Skip) ++itCCEvent; + while (itGroupEvent && itGroupEvent->FragmentPos() <= Skip) ++itGroupEvent; + // we can't simply compare the timestamp here, because note events // might happen on the same time stamp, so we have to deal on the // actual sequence the note events arrived instead (see bug #112) @@ -347,7 +396,11 @@ // drivers that use Samples < MaxSamplesPerCycle). // End the EG1 here, at pos 0, with a shorter max fade // out time. - EG1.enterFadeOutStage(Samples / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); + if (pSignalUnitRack == NULL) { + pEG1->enterFadeOutStage(Samples / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); + } else { + // TODO: + } itKillEvent = Pool::Iterator(); } else { killPos = RTMath::Min(itKillEvent->FragmentPos(), maxFadeOutPos); @@ -364,6 +417,11 @@ // process MIDI control change and pitchbend events for this subfragment processCCEvents(itCCEvent, iSubFragmentEnd); + uint8_t pan = MIDIPan; + if (pSignalUnitRack != NULL) pan = pSignalUnitRack->GetEndpointUnit()->CaluclatePan(pan); + + PanLeftSmoother.update(AbstractEngine::PanCurve[128 - pan]); + PanRightSmoother.update(AbstractEngine::PanCurve[pan]); finalSynthesisParameters.fFinalPitch = Pitch.PitchBase * Pitch.PitchBend; float fFinalVolume = VolumeSmoother.render() * CrossfadeSmoother.render(); @@ -373,46 +431,73 @@ // process transition events (note on, note off & sustain pedal) processTransitionEvents(itNoteEvent, iSubFragmentEnd); + processGroupEvents(itGroupEvent, iSubFragmentEnd); + + if (pSignalUnitRack == NULL) { + // if the voice was killed in this subfragment, or if the + // filter EG is finished, switch EG1 to fade out stage + if ((itKillEvent && killPos <= iSubFragmentEnd) || + (SYNTHESIS_MODE_GET_FILTER(SynthesisMode) && + pEG2->getSegmentType() == EG::segment_end)) { + pEG1->enterFadeOutStage(); + itKillEvent = Pool::Iterator(); + } - // if the voice was killed in this subfragment, or if the - // filter EG is finished, switch EG1 to fade out stage - if ((itKillEvent && killPos <= iSubFragmentEnd) || - (SYNTHESIS_MODE_GET_FILTER(SynthesisMode) && - EG2.getSegmentType() == gig::EGADSR::segment_end)) { - EG1.enterFadeOutStage(); - itKillEvent = Pool::Iterator(); - } - - // process envelope generators - switch (EG1.getSegmentType()) { - case gig::EGADSR::segment_lin: - fFinalVolume *= EG1.processLin(); - break; - case gig::EGADSR::segment_exp: - fFinalVolume *= EG1.processExp(); - break; - case gig::EGADSR::segment_end: - fFinalVolume *= EG1.getLevel(); - break; // noop - } - switch (EG2.getSegmentType()) { - case gig::EGADSR::segment_lin: - fFinalCutoff *= EG2.processLin(); - break; - case gig::EGADSR::segment_exp: - fFinalCutoff *= EG2.processExp(); - break; - case gig::EGADSR::segment_end: - fFinalCutoff *= EG2.getLevel(); - break; // noop - } - if (EG3.active()) finalSynthesisParameters.fFinalPitch *= EG3.render(); - - // process low frequency oscillators - if (bLFO1Enabled) fFinalVolume *= (1.0f - pLFO1->render()); - if (bLFO2Enabled) fFinalCutoff *= pLFO2->render(); - if (bLFO3Enabled) finalSynthesisParameters.fFinalPitch *= RTMath::CentsToFreqRatio(pLFO3->render()); + // process envelope generators + switch (pEG1->getSegmentType()) { + case EG::segment_lin: + fFinalVolume *= pEG1->processLin(); + break; + case EG::segment_exp: + fFinalVolume *= pEG1->processExp(); + break; + case EG::segment_end: + fFinalVolume *= pEG1->getLevel(); + break; // noop + case EG::segment_pow: + fFinalVolume *= pEG1->processPow(); + break; + } + switch (pEG2->getSegmentType()) { + case EG::segment_lin: + fFinalCutoff *= pEG2->processLin(); + break; + case EG::segment_exp: + fFinalCutoff *= pEG2->processExp(); + break; + case EG::segment_end: + fFinalCutoff *= pEG2->getLevel(); + break; // noop + case EG::segment_pow: + fFinalCutoff *= pEG2->processPow(); + break; + } + if (EG3.active()) finalSynthesisParameters.fFinalPitch *= EG3.render(); + // process low frequency oscillators + if (bLFO1Enabled) fFinalVolume *= (1.0f - pLFO1->render()); + if (bLFO2Enabled) fFinalCutoff *= pLFO2->render(); + if (bLFO3Enabled) finalSynthesisParameters.fFinalPitch *= RTMath::CentsToFreqRatio(pLFO3->render()); + } else { + // if the voice was killed in this subfragment, or if the + // filter EG is finished, switch EG1 to fade out stage + /*if ((itKillEvent && killPos <= iSubFragmentEnd) || + (SYNTHESIS_MODE_GET_FILTER(SynthesisMode) && + pEG2->getSegmentType() == EG::segment_end)) { + pEG1->enterFadeOutStage(); + itKillEvent = Pool::Iterator(); + }*/ + // TODO: ^^^ + + fFinalVolume *= pSignalUnitRack->GetEndpointUnit()->GetVolume(); + fFinalCutoff = pSignalUnitRack->GetEndpointUnit()->CalculateFilterCutoff(fFinalCutoff); + fFinalResonance = pSignalUnitRack->GetEndpointUnit()->CalculateResonance(fFinalResonance); + + finalSynthesisParameters.fFinalPitch = + pSignalUnitRack->GetEndpointUnit()->CalculatePitch(finalSynthesisParameters.fFinalPitch); + + } + // limit the pitch so we don't read outside the buffer finalSynthesisParameters.fFinalPitch = RTMath::Min(finalSynthesisParameters.fFinalPitch, float(1 << CONFIG_MAX_PITCH)); @@ -445,36 +530,70 @@ fFinalVolume * VolumeRight * PanRightSmoother.render(); #endif // render audio for one subfragment - RunSynthesisFunction(SynthesisMode, &finalSynthesisParameters, &loop); + if (!delay) RunSynthesisFunction(SynthesisMode, &finalSynthesisParameters, &loop); - // stop the rendering if volume EG is finished - if (EG1.getSegmentType() == gig::EGADSR::segment_end) break; + if (pSignalUnitRack == NULL) { + // stop the rendering if volume EG is finished + if (pEG1->getSegmentType() == EG::segment_end) break; + } else { + // stop the rendering if the endpoint unit is not active + if (!pSignalUnitRack->GetEndpointUnit()->Active()) break; + } const double newPos = Pos + (iSubFragmentEnd - i) * finalSynthesisParameters.fFinalPitch; - // increment envelopes' positions - if (EG1.active()) { + if (pSignalUnitRack == NULL) { + // increment envelopes' positions + if (pEG1->active()) { + + // if sample has a loop and loop start has been reached in this subfragment, send a special event to EG1 to let it finish the attack hold stage + if (SmplInfo.HasLoops && Pos <= SmplInfo.LoopStart && SmplInfo.LoopStart < newPos) { + pEG1->update(EG::event_hold_end, GetEngine()->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); + } - // if sample has a loop and loop start has been reached in this subfragment, send a special event to EG1 to let it finish the attack hold stage - if (SmplInfo.HasLoops && Pos <= SmplInfo.LoopStart && SmplInfo.LoopStart < newPos) { - EG1.update(gig::EGADSR::event_hold_end, GetEngine()->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); + pEG1->increment(1); + if (!pEG1->toStageEndLeft()) pEG1->update(EG::event_stage_end, GetEngine()->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); } - - EG1.increment(1); - if (!EG1.toStageEndLeft()) EG1.update(gig::EGADSR::event_stage_end, GetEngine()->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); - } - if (EG2.active()) { - EG2.increment(1); - if (!EG2.toStageEndLeft()) EG2.update(gig::EGADSR::event_stage_end, GetEngine()->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); + if (pEG2->active()) { + pEG2->increment(1); + if (!pEG2->toStageEndLeft()) pEG2->update(EG::event_stage_end, GetEngine()->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); + } + EG3.increment(1); + if (!EG3.toEndLeft()) EG3.update(); // neutralize envelope coefficient if end reached + } else { + // if sample has a loop and loop start has been reached in this subfragment, send a special event to EG1 to let it finish the attack hold stage + /*if (SmplInfo.HasLoops && Pos <= SmplInfo.LoopStart && SmplInfo.LoopStart < newPos) { + pEG1->update(EG::event_hold_end, GetEngine()->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); + }*/ + // TODO: ^^^ + + if (!delay) pSignalUnitRack->Increment(); } - EG3.increment(1); - if (!EG3.toEndLeft()) EG3.update(); // neutralize envelope coefficient if end reached Pos = newPos; i = iSubFragmentEnd; } + + if (delay) return; + + if (bVoiceRequiresDedicatedRouting) { + if (bEq) { + pEq->RenderAudio(Samples); + pEq->GetOutChannelLeft()->CopyTo(GetEngine()->pDedicatedVoiceChannelLeft, Samples); + pEq->GetOutChannelRight()->CopyTo(GetEngine()->pDedicatedVoiceChannelRight, Samples); + } + optional effectSendLevels[2] = { + pMidiKeyInfo->ReverbSend, + pMidiKeyInfo->ChorusSend + }; + GetEngine()->RouteDedicatedVoiceChannels(pEngineChannel, effectSendLevels, Samples); + } else if (bEq) { + pEq->RenderAudio(Samples); + pEq->GetOutChannelLeft()->MixTo(pChannel->pChannelLeft, Samples); + pEq->GetOutChannelRight()->MixTo(pChannel->pChannelRight, Samples); + } } - + /** * Process given list of MIDI control change and pitch bend events for * the given time. @@ -491,26 +610,30 @@ if (itEvent->Param.CC.Controller == VCFResonanceCtrl.controller) { processResonanceEvent(itEvent); } - if (itEvent->Param.CC.Controller == pLFO1->ExtController) { - pLFO1->update(itEvent->Param.CC.Value); - } - if (itEvent->Param.CC.Controller == pLFO2->ExtController) { - pLFO2->update(itEvent->Param.CC.Value); - } - if (itEvent->Param.CC.Controller == pLFO3->ExtController) { - pLFO3->update(itEvent->Param.CC.Value); + if (pSignalUnitRack == NULL) { + if (itEvent->Param.CC.Controller == pLFO1->ExtController) { + pLFO1->update(itEvent->Param.CC.Value); + } + if (itEvent->Param.CC.Controller == pLFO2->ExtController) { + pLFO2->update(itEvent->Param.CC.Value); + } + if (itEvent->Param.CC.Controller == pLFO3->ExtController) { + pLFO3->update(itEvent->Param.CC.Value); + } } if (itEvent->Param.CC.Controller == 7) { // volume VolumeSmoother.update(AbstractEngine::VolumeCurve[itEvent->Param.CC.Value]); } else if (itEvent->Param.CC.Controller == 10) { // panpot - PanLeftSmoother.update(AbstractEngine::PanCurve[128 - itEvent->Param.CC.Value]); - PanRightSmoother.update(AbstractEngine::PanCurve[itEvent->Param.CC.Value]); + MIDIPan = itEvent->Param.CC.Value; } } else if (itEvent->Type == Event::type_pitchbend) { // if pitch bend event processPitchEvent(itEvent); } ProcessCCEvent(itEvent); + if (pSignalUnitRack != NULL) { + pSignalUnitRack->ProcessCCEvent(itEvent); + } } } @@ -537,16 +660,34 @@ */ void AbstractVoice::processTransitionEvents(RTList::Iterator& itEvent, uint End) { for (; itEvent && itEvent->FragmentPos() <= End; ++itEvent) { - if (itEvent->Type == Event::type_release) { - EG1.update(gig::EGADSR::event_release, GetEngine()->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); - EG2.update(gig::EGADSR::event_release, GetEngine()->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); - } else if (itEvent->Type == Event::type_cancel_release) { - EG1.update(gig::EGADSR::event_cancel_release, GetEngine()->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); - EG2.update(gig::EGADSR::event_cancel_release, GetEngine()->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); + // some voice types ignore note off + if (!(Type & (Voice::type_one_shot | Voice::type_release_trigger | Voice::type_controller_triggered))) { + if (itEvent->Type == Event::type_release) { + EnterReleaseStage(); + } else if (itEvent->Type == Event::type_cancel_release) { + if (pSignalUnitRack == NULL) { + pEG1->update(EG::event_cancel_release, GetEngine()->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); + pEG2->update(EG::event_cancel_release, GetEngine()->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); + } else { + pSignalUnitRack->CancelRelease(); + } + } } } } + /** + * Process given list of events aimed at all voices in a key group. + * + * @param itEvent - iterator pointing to the next event to be processed + * @param End - youngest time stamp where processing should be stopped + */ + void AbstractVoice::processGroupEvents(RTList::Iterator& itEvent, uint End) { + for (; itEvent && itEvent->FragmentPos() <= End; ++itEvent) { + ProcessGroupEvent(itEvent); + } + } + /** @brief Update current portamento position. * * Will be called when portamento mode is enabled to get the final @@ -556,8 +697,12 @@ * @param itNoteOffEvent - event which causes this voice to die soon */ void AbstractVoice::UpdatePortamentoPos(Pool::Iterator& itNoteOffEvent) { - const float fFinalEG3Level = EG3.level(itNoteOffEvent->FragmentPos()); - pEngineChannel->PortamentoPos = (float) MIDIKey + RTMath::FreqRatioToCents(fFinalEG3Level) * 0.01f; + if (pSignalUnitRack == NULL) { + const float fFinalEG3Level = EG3.level(itNoteOffEvent->FragmentPos()); + pEngineChannel->PortamentoPos = (float) MIDIKey + RTMath::FreqRatioToCents(fFinalEG3Level) * 0.01f; + } else { + // TODO: + } } /** @@ -604,14 +749,35 @@ volume *= GetSampleAttenuation() * pEngineChannel->GlobalVolume * GLOBAL_VOLUME; // the volume of release triggered samples depends on note length - if (Type == Voice::type_release_trigger) { + if (Type & Voice::type_release_trigger) { float noteLength = float(GetEngine()->FrameTime + Delay - GetNoteOnTime(MIDIKey) ) / GetEngine()->SampleRate; - float attenuation = 1 - 0.01053 * (256 >> RgnInfo.ReleaseTriggerDecay) * noteLength; - volume *= attenuation; + volume *= GetReleaseTriggerAttenuation(noteLength); } return volume; } + + float AbstractVoice::GetReleaseTriggerAttenuation(float noteLength) { + return 1 - RgnInfo.ReleaseTriggerDecay * noteLength; + } + + void AbstractVoice::EnterReleaseStage() { + if (pSignalUnitRack == NULL) { + pEG1->update(EG::event_release, GetEngine()->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); + pEG2->update(EG::event_release, GetEngine()->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); + } else { + pSignalUnitRack->EnterReleaseStage(); + } + } + + bool AbstractVoice::EG1Finished() { + if (pSignalUnitRack == NULL) { + return pEG1->getSegmentType() == EG::segment_end; + } else { + return !pSignalUnitRack->GetEndpointUnit()->Active(); + } + } + } // namespace LinuxSampler