--- linuxsampler/trunk/src/engines/gig/Voice.cpp 2005/09/03 11:14:30 769 +++ linuxsampler/trunk/src/engines/gig/Voice.cpp 2006/01/28 16:55:30 831 @@ -3,7 +3,7 @@ * LinuxSampler - modular, streaming capable sampler * * * * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * - * Copyright (C) 2005 Christian Schoenebeck * + * Copyright (C) 2005, 2006 Christian Schoenebeck * * * * 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 * @@ -23,6 +23,7 @@ #include "../../common/Features.h" #include "Synthesizer.h" +#include "Profiler.h" #include "Voice.h" @@ -49,10 +50,10 @@ #else SYNTHESIS_MODE_SET_IMPLEMENTATION(SynthesisMode, false); #endif - SYNTHESIS_MODE_SET_PROFILING(SynthesisMode, true); + SYNTHESIS_MODE_SET_PROFILING(SynthesisMode, Profiler::isEnabled()); - FilterLeft.Reset(); - FilterRight.Reset(); + finalSynthesisParameters.filterLeft.Reset(); + finalSynthesisParameters.filterRight.Reset(); } Voice::~Voice() { @@ -135,10 +136,11 @@ CrossfadeVolume = 1.0f; } - PanLeft = 1.0f - float(RTMath::Max(pDimRgn->Pan, 0)) / 63.0f; - PanRight = 1.0f - float(RTMath::Min(pDimRgn->Pan, 0)) / -64.0f; + PanLeft = Engine::PanCurve[64 - pDimRgn->Pan]; + PanRight = Engine::PanCurve[64 + pDimRgn->Pan]; - Pos = pDimRgn->SampleStartOffset; // offset where we should start playback of sample (0 - 2000 sample points) + finalSynthesisParameters.dPos = pDimRgn->SampleStartOffset; // offset where we should start playback of sample (0 - 2000 sample points) + Pos = pDimRgn->SampleStartOffset; // Check if the sample needs disk streaming or is too short for that long cachedsamples = pSample->GetCache().Size / pSample->FrameSize; @@ -148,11 +150,7 @@ MaxRAMPos = cachedsamples - (pEngine->MaxSamplesPerCycle << CONFIG_MAX_PITCH) / pSample->Channels; //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) // check if there's a loop defined which completely fits into the cached (RAM) part of the sample - if (pSample->Loops && pSample->LoopEnd <= MaxRAMPos) { - RAMLoop = true; - LoopCyclesLeft = pSample->LoopPlayCount; - } - else RAMLoop = false; + RAMLoop = (pSample->Loops && pSample->LoopEnd <= MaxRAMPos); if (pDiskThread->OrderNewStream(&DiskStreamRef, pSample, MaxRAMPos, !RAMLoop) < 0) { dmsg(1,("Disk stream order failed!\n")); @@ -163,14 +161,16 @@ } else { // RAM only voice MaxRAMPos = cachedsamples; - if (pSample->Loops) { - RAMLoop = true; - LoopCyclesLeft = pSample->LoopPlayCount; - } - else RAMLoop = false; + RAMLoop = (pSample->Loops != 0); dmsg(4,("RAM only voice launched (Looping: %s)\n", (RAMLoop) ? "yes" : "no")); } - + if (RAMLoop) { + loop.uiTotalCycles = pSample->LoopPlayCount; + loop.uiCyclesLeft = pSample->LoopPlayCount; + loop.uiStart = pSample->LoopStart; + loop.uiEnd = pSample->LoopEnd; + loop.uiSize = pSample->LoopSize; + } // calculate initial pitch value { @@ -214,7 +214,6 @@ EG1.trigger(pDimRgn->EG1PreAttack, pDimRgn->EG1Attack * eg1attack, pDimRgn->EG1Hold, - pSample->LoopStart, pDimRgn->EG1Decay1 * eg1decay * velrelease, pDimRgn->EG1Decay2 * eg1decay * velrelease, pDimRgn->EG1InfiniteSustain, @@ -224,6 +223,11 @@ pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); } + // setup initial volume in synthesis parameters + fFinalVolume = getVolume() * EG1.getLevel(); + finalSynthesisParameters.fFinalVolumeLeft = fFinalVolume * PanLeft * pEngineChannel->GlobalPanLeft; + finalSynthesisParameters.fFinalVolumeRight = fFinalVolume * PanRight * pEngineChannel->GlobalPanRight; + // setup EG 2 (VCF Cutoff EG) { @@ -253,7 +257,6 @@ EG2.trigger(pDimRgn->EG2PreAttack, pDimRgn->EG2Attack * eg2attack, false, - pSample->LoopStart, pDimRgn->EG2Decay1 * eg2decay * velrelease, pDimRgn->EG2Decay2 * eg2decay * velrelease, pDimRgn->EG2InfiniteSustain, @@ -266,8 +269,16 @@ // setup EG 3 (VCO EG) { - double eg3depth = RTMath::CentsToFreqRatio(pDimRgn->EG3Depth); - EG3.trigger(eg3depth, pDimRgn->EG3Attack, pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); + // 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(pDimRgn->EG3Depth); + float eg3time = (bPortamento) + ? pEngineChannel->PortamentoTime + : pDimRgn->EG3Attack; + EG3.trigger(eg3depth, eg3time, pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); + dmsg(5,("PortamentoPos=%f, depth=%f, time=%f\n", pEngineChannel->PortamentoPos, eg3depth, eg3time)); } @@ -469,8 +480,8 @@ #endif // CONFIG_OVERRIDE_RESONANCE_CTRL #ifndef CONFIG_OVERRIDE_FILTER_TYPE - FilterLeft.SetType(pDimRgn->VCFType); - FilterRight.SetType(pDimRgn->VCFType); + finalSynthesisParameters.filterLeft.SetType(pDimRgn->VCFType); + finalSynthesisParameters.filterRight.SetType(pDimRgn->VCFType); #else // override filter type FilterLeft.SetType(CONFIG_OVERRIDE_FILTER_TYPE); FilterRight.SetType(CONFIG_OVERRIDE_FILTER_TYPE); @@ -490,6 +501,7 @@ if (VCFCutoffCtrl.controller) { cvalue = pEngineChannel->ControllerTable[VCFCutoffCtrl.controller]; if (pDimRgn->VCFCutoffControllerInvert) cvalue = 127 - cvalue; + // VCFVelocityScale in this case means Minimum cutoff if (cvalue < pDimRgn->VCFVelocityScale) cvalue = pDimRgn->VCFVelocityScale; } else { @@ -497,16 +509,13 @@ } cutoff *= float(cvalue) * 0.00787402f; // (1 / 127) if (cutoff > 1.0) cutoff = 1.0; - cutoff = exp(cutoff * FILTER_CUTOFF_COEFF) * CONFIG_FILTER_CUTOFF_MIN; + cutoff = (cutoff < 0.5 ? cutoff * 4826 - 1 : cutoff * 5715 - 449); + if (cutoff < 1.0) cutoff = 1.0; // calculate resonance - float resonance = (float) VCFResonanceCtrl.value * 0.00787f; // 0.0..1.0 - if (pDimRgn->VCFKeyboardTracking) { - resonance += (float) (itNoteOnEvent->Param.Note.Key - pDimRgn->VCFKeyboardTrackingBreakpoint) * 0.00787f; - } - Constrain(resonance, 0.0, 1.0); // correct resonance if outside allowed value range (0.0..1.0) + float resonance = (float) (VCFResonanceCtrl.controller ? VCFResonanceCtrl.value : pDimRgn->VCFResonance) * 0.00787f; // 0.0..1.0 - VCFCutoffCtrl.fvalue = cutoff - CONFIG_FILTER_CUTOFF_MIN; + VCFCutoffCtrl.fvalue = cutoff - 1.0; VCFResonanceCtrl.fvalue = resonance; } else { @@ -547,12 +556,11 @@ if (DiskVoice) { // check if we reached the allowed limit of the sample RAM cache - if (Pos > MaxRAMPos) { - dmsg(5,("Voice: switching to disk playback (Pos=%f)\n", Pos)); + if (finalSynthesisParameters.dPos > MaxRAMPos) { + dmsg(5,("Voice: switching to disk playback (Pos=%f)\n", finalSynthesisParameters.dPos)); this->PlaybackState = playback_state_disk; } - } - else if (Pos >= pSample->GetCache().Size / pSample->FrameSize) { + } else if (finalSynthesisParameters.dPos >= pSample->GetCache().Size / pSample->FrameSize) { this->PlaybackState = playback_state_end; } } @@ -567,8 +575,8 @@ KillImmediately(); return; } - DiskStreamRef.pStream->IncrementReadPos(pSample->Channels * (int(Pos) - MaxRAMPos)); - Pos -= int(Pos); + DiskStreamRef.pStream->IncrementReadPos(pSample->Channels * (int(finalSynthesisParameters.dPos) - MaxRAMPos)); + finalSynthesisParameters.dPos -= int(finalSynthesisParameters.dPos); RealSampleWordsLeftToRead = -1; // -1 means no silence has been added yet } @@ -589,10 +597,10 @@ // render current audio fragment Synthesize(Samples, ptr, Delay); - const int iPos = (int) Pos; + const int iPos = (int) finalSynthesisParameters.dPos; const int readSampleWords = iPos * pSample->Channels; // amount of sample words actually been read DiskStreamRef.pStream->IncrementReadPos(readSampleWords); - Pos -= iPos; // just keep fractional part of Pos + finalSynthesisParameters.dPos -= iPos; // just keep fractional part of playback position // change state of voice to 'end' if we really reached the end of the sample data if (RealSampleWordsLeftToRead >= 0) { @@ -607,9 +615,6 @@ break; } - // Reset synthesis event lists - pEngineChannel->pEvents->clear(); - // Reset delay Delay = 0; @@ -624,8 +629,8 @@ * suspended / not running. */ void Voice::Reset() { - FilterLeft.Reset(); - FilterRight.Reset(); + finalSynthesisParameters.filterLeft.Reset(); + finalSynthesisParameters.filterRight.Reset(); DiskStreamRef.pStream = NULL; DiskStreamRef.hStream = 0; DiskStreamRef.State = Stream::state_unused; @@ -645,11 +650,11 @@ void Voice::processTransitionEvents(RTList::Iterator& itEvent, uint End) { for (; itEvent && itEvent->FragmentPos() <= End; ++itEvent) { if (itEvent->Type == Event::type_release) { - EG1.update(EGADSR::event_release, this->Pos, fFinalPitch, pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); - EG2.update(EGADSR::event_release, this->Pos, fFinalPitch, pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); + EG1.update(EGADSR::event_release, pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); + EG2.update(EGADSR::event_release, pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); } else if (itEvent->Type == Event::type_cancel_release) { - EG1.update(EGADSR::event_cancel_release, this->Pos, fFinalPitch, pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); - EG2.update(EGADSR::event_cancel_release, this->Pos, fFinalPitch, pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); + EG1.update(EGADSR::event_cancel_release, pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); + EG2.update(EGADSR::event_cancel_release, pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); } } } @@ -692,18 +697,21 @@ void Voice::processPitchEvent(RTList::Iterator& itEvent) { const float pitch = RTMath::CentsToFreqRatio(((double) itEvent->Param.Pitch.Pitch / 8192.0) * 200.0); // +-two semitones = +-200 cents - fFinalPitch *= pitch; + finalSynthesisParameters.fFinalPitch *= pitch; PitchBend = pitch; } void Voice::processCrossFadeEvent(RTList::Iterator& itEvent) { CrossfadeVolume = CrossfadeAttenuation(itEvent->Param.CC.Value); + fFinalVolume = getVolume(); + } + + float Voice::getVolume() { #if CONFIG_PROCESS_MUTED_CHANNELS - const float effectiveVolume = CrossfadeVolume * Volume * (pEngineChannel->GetMute() ? 0 : pEngineChannel->GlobalVolume); + return pEngineChannel->GetMute() ? 0 : (Volume * CrossfadeVolume * pEngineChannel->GlobalVolume); #else - const float effectiveVolume = CrossfadeVolume * Volume * pEngineChannel->GlobalVolume; + return Volume * CrossfadeVolume * pEngineChannel->GlobalVolume; #endif - fFinalVolume = effectiveVolume; } void Voice::processCutoffEvent(RTList::Iterator& itEvent) { @@ -714,8 +722,10 @@ if (ccvalue < pDimRgn->VCFVelocityScale) ccvalue = pDimRgn->VCFVelocityScale; float cutoff = CutoffBase * float(ccvalue) * 0.00787402f; // (1 / 127) if (cutoff > 1.0) cutoff = 1.0; - cutoff = exp(cutoff * FILTER_CUTOFF_COEFF) * CONFIG_FILTER_CUTOFF_MIN - CONFIG_FILTER_CUTOFF_MIN; - VCFCutoffCtrl.fvalue = cutoff; // needed for initialization of fFinalCutoff next time + cutoff = (cutoff < 0.5 ? cutoff * 4826 - 1 : cutoff * 5715 - 449); + if (cutoff < 1.0) cutoff = 1.0; + + VCFCutoffCtrl.fvalue = cutoff - 1.0; // needed for initialization of fFinalCutoff next time fFinalCutoff = cutoff; } @@ -738,6 +748,10 @@ * @param Skip - number of sample points to skip in output buffer */ void Voice::Synthesize(uint Samples, sample_t* pSrc, uint Skip) { + finalSynthesisParameters.pOutLeft = &pEngineChannel->pOutputLeft[Skip]; + finalSynthesisParameters.pOutRight = &pEngineChannel->pOutputRight[Skip]; + finalSynthesisParameters.pSrc = pSrc; + RTList::Iterator itCCEvent = pEngineChannel->pEvents->first(); RTList::Iterator itNoteEvent = pEngineChannel->pMIDIKeyInfo[MIDIKey].pEvents->first(); @@ -746,17 +760,19 @@ while (itNoteEvent && itNoteEvent->FragmentPos() <= Skip) ++itNoteEvent; } + uint killPos; + if (itKillEvent) killPos = RTMath::Min(itKillEvent->FragmentPos(), pEngine->MaxFadeOutPos); + + float fFinalPanLeft = PanLeft * pEngineChannel->GlobalPanLeft; + float fFinalPanRight = PanRight * pEngineChannel->GlobalPanRight; + uint i = Skip; while (i < Samples) { int iSubFragmentEnd = RTMath::Min(i + CONFIG_DEFAULT_SUBFRAGMENT_SIZE, Samples); // initialize all final synthesis parameters - fFinalPitch = PitchBase * PitchBend; - #if CONFIG_PROCESS_MUTED_CHANNELS - fFinalVolume = this->Volume * this->CrossfadeVolume * (pEngineChannel->GetMute() ? 0 : pEngineChannel->GlobalVolume); - #else - fFinalVolume = this->Volume * this->CrossfadeVolume * pEngineChannel->GlobalVolume; - #endif + finalSynthesisParameters.fFinalPitch = PitchBase * PitchBend; + fFinalVolume = getVolume(); fFinalCutoff = VCFCutoffCtrl.fvalue; fFinalResonance = VCFResonanceCtrl.fvalue; @@ -766,6 +782,12 @@ // process transition events (note on, note off & sustain pedal) processTransitionEvents(itNoteEvent, iSubFragmentEnd); + // if the voice was killed in this subfragment switch EG1 to fade out stage + if (itKillEvent && killPos <= iSubFragmentEnd) { + EG1.enterFadeOutStage(); + itKillEvent = Pool::Iterator(); + } + // process envelope generators switch (EG1.getSegmentType()) { case EGADSR::segment_lin: @@ -789,44 +811,81 @@ fFinalCutoff *= EG2.getLevel(); break; // noop } - fFinalPitch *= RTMath::CentsToFreqRatio(EG3.render()); + if (EG3.active()) finalSynthesisParameters.fFinalPitch *= EG3.render(); // process low frequency oscillators if (bLFO1Enabled) fFinalVolume *= pLFO1->render(); if (bLFO2Enabled) fFinalCutoff *= pLFO2->render(); - if (bLFO3Enabled) fFinalPitch *= RTMath::CentsToFreqRatio(pLFO3->render()); + if (bLFO3Enabled) finalSynthesisParameters.fFinalPitch *= RTMath::CentsToFreqRatio(pLFO3->render()); // if filter enabled then update filter coefficients if (SYNTHESIS_MODE_GET_FILTER(SynthesisMode)) { - FilterLeft.SetParameters(fFinalCutoff, fFinalResonance, pEngine->SampleRate); - FilterRight.SetParameters(fFinalCutoff, fFinalResonance, pEngine->SampleRate); + finalSynthesisParameters.filterLeft.SetParameters(fFinalCutoff + 1.0, fFinalResonance, pEngine->SampleRate); + finalSynthesisParameters.filterRight.SetParameters(fFinalCutoff + 1.0, fFinalResonance, pEngine->SampleRate); } - // how many steps do we calculate for this next subfragment - const int steps = iSubFragmentEnd - i; + // do we need resampling? + const float __PLUS_ONE_CENT = 1.000577789506554859250142541782224725466f; + const float __MINUS_ONE_CENT = 0.9994225441413807496009516495583113737666f; + const bool bResamplingRequired = !(finalSynthesisParameters.fFinalPitch <= __PLUS_ONE_CENT && + finalSynthesisParameters.fFinalPitch >= __MINUS_ONE_CENT); + SYNTHESIS_MODE_SET_INTERPOLATE(SynthesisMode, bResamplingRequired); + + // prepare final synthesis parameters structure + finalSynthesisParameters.uiToGo = iSubFragmentEnd - i; +#ifdef CONFIG_INTERPOLATE_VOLUME + finalSynthesisParameters.fFinalVolumeDeltaLeft = + (fFinalVolume * fFinalPanLeft - finalSynthesisParameters.fFinalVolumeLeft) / finalSynthesisParameters.uiToGo; + finalSynthesisParameters.fFinalVolumeDeltaRight = + (fFinalVolume * fFinalPanRight - finalSynthesisParameters.fFinalVolumeRight) / finalSynthesisParameters.uiToGo; +#else + finalSynthesisParameters.fFinalVolumeLeft = fFinalVolume * fFinalPanLeft; + finalSynthesisParameters.fFinalVolumeRight = fFinalVolume * fFinalPanRight; +#endif + // render audio for one subfragment + RunSynthesisFunction(SynthesisMode, &finalSynthesisParameters, &loop); - // select the appropriate synthesis mode - SYNTHESIS_MODE_SET_INTERPOLATE(SynthesisMode, fFinalPitch != 1.0f); + // stop the rendering if volume EG is finished + if (EG1.getSegmentType() == EGADSR::segment_end) break; - // render audio for one subfragment - RunSynthesisFunction(SynthesisMode, *this, iSubFragmentEnd, pSrc, i); + const double newPos = Pos + (iSubFragmentEnd - i) * finalSynthesisParameters.fFinalPitch; // increment envelopes' positions if (EG1.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 (pSample->Loops && Pos <= pSample->LoopStart && pSample->LoopStart < newPos) { + EG1.update(EGADSR::event_hold_end, pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); + } + EG1.increment(1); - if (!EG1.toStageEndLeft()) EG1.update(EGADSR::event_stage_end, this->Pos, fFinalPitch, pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); + if (!EG1.toStageEndLeft()) EG1.update(EGADSR::event_stage_end, pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); } if (EG2.active()) { EG2.increment(1); - if (!EG2.toStageEndLeft()) EG2.update(EGADSR::event_stage_end, this->Pos, fFinalPitch, pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); + if (!EG2.toStageEndLeft()) EG2.update(EGADSR::event_stage_end, pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); } EG3.increment(1); if (!EG3.toEndLeft()) EG3.update(); // neutralize envelope coefficient if end reached + Pos = newPos; i = iSubFragmentEnd; } } + /** @brief Update current portamento position. + * + * Will be called when portamento mode is enabled to get the final + * portamento position of this active voice from where the next voice(s) + * might continue to slide on. + * + * @param itNoteOffEvent - event which causes this voice to die soon + */ + void Voice::UpdatePortamentoPos(Pool::Iterator& itNoteOffEvent) { + const float fFinalEG3Level = EG3.level(itNoteOffEvent->FragmentPos()); + pEngineChannel->PortamentoPos = (float) MIDIKey + RTMath::FreqRatioToCents(fFinalEG3Level) * 0.01f; + } + /** * Immediately kill the voice. This method should not be used to kill * a normal, active voice, because it doesn't take care of things like