--- linuxsampler/trunk/src/engines/gig/Voice.cpp 2005/08/16 17:14:25 738 +++ linuxsampler/trunk/src/engines/gig/Voice.cpp 2005/11/05 10:59:37 799 @@ -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() { @@ -138,7 +139,8 @@ PanLeft = 1.0f - float(RTMath::Max(pDimRgn->Pan, 0)) / 63.0f; PanRight = 1.0f - float(RTMath::Min(pDimRgn->Pan, 0)) / -64.0f; - 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, @@ -253,7 +252,6 @@ EG2.trigger(pDimRgn->EG2PreAttack, pDimRgn->EG2Attack * eg2attack, false, - pSample->LoopStart, pDimRgn->EG2Decay1 * eg2decay * velrelease, pDimRgn->EG2Decay2 * eg2decay * velrelease, pDimRgn->EG2InfiniteSustain, @@ -469,8 +467,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 +488,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 +496,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 +543,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 +562,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 +584,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 +602,6 @@ break; } - // Reset synthesis event lists - pEngineChannel->pEvents->clear(); - // Reset delay Delay = 0; @@ -624,8 +616,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 +637,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,7 +684,8 @@ 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) { @@ -713,8 +706,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; } @@ -737,34 +732,47 @@ * @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(); - + if (Skip) { // skip events that happened before this voice was triggered while (itCCEvent && itCCEvent->FragmentPos() <= Skip) ++itCCEvent; while (itNoteEvent && itNoteEvent->FragmentPos() <= Skip) ++itNoteEvent; } - + + uint killPos; + if (itKillEvent) killPos = RTMath::Min(itKillEvent->FragmentPos(), pEngine->MaxFadeOutPos); + uint i = Skip; while (i < Samples) { int iSubFragmentEnd = RTMath::Min(i + CONFIG_DEFAULT_SUBFRAGMENT_SIZE, Samples); - + // initialize all final synthesis parameters - fFinalPitch = PitchBase * PitchBend; + finalSynthesisParameters.fFinalPitch = PitchBase * PitchBend; #if CONFIG_PROCESS_MUTED_CHANNELS - fFinalVolume = this->Volume * this->CrossfadeVolume * (pEngineChannel->GetMute() ? 0 : pEngineChannel->GlobalVolume)); + fFinalVolume = this->Volume * this->CrossfadeVolume * (pEngineChannel->GetMute() ? 0 : pEngineChannel->GlobalVolume); #else fFinalVolume = this->Volume * this->CrossfadeVolume * pEngineChannel->GlobalVolume; #endif fFinalCutoff = VCFCutoffCtrl.fvalue; fFinalResonance = VCFResonanceCtrl.fvalue; - + // process MIDI control change and pitchbend events for this subfragment processCCEvents(itCCEvent, iSubFragmentEnd); // 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: @@ -788,39 +796,56 @@ fFinalCutoff *= EG2.getLevel(); break; // noop } - fFinalPitch *= RTMath::CentsToFreqRatio(EG3.render()); - + if (EG3.active()) finalSynthesisParameters.fFinalPitch *= RTMath::CentsToFreqRatio(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; - - // select the appropriate synthesis mode - SYNTHESIS_MODE_SET_INTERPOLATE(SynthesisMode, fFinalPitch != 1.0f); - + // 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.fFinalVolumeLeft = fFinalVolume * PanLeft; + finalSynthesisParameters.fFinalVolumeRight = fFinalVolume * PanRight; + finalSynthesisParameters.uiToGo = iSubFragmentEnd - i; + // render audio for one subfragment - RunSynthesisFunction(SynthesisMode, *this, iSubFragmentEnd, pSrc, i); + RunSynthesisFunction(SynthesisMode, &finalSynthesisParameters, &loop); + + const double newPos = Pos + (iSubFragmentEnd - i) * finalSynthesisParameters.fFinalPitch; - // increment envelopes' positions + // increment envelopes' positions if (EG1.active()) { - EG1.increment(steps); - if (!EG1.toStageEndLeft()) EG1.update(EGADSR::event_stage_end, this->Pos, fFinalPitch, pEngine->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 (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, pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); } if (EG2.active()) { - EG2.increment(steps); - if (!EG2.toStageEndLeft()) EG2.update(EGADSR::event_stage_end, this->Pos, fFinalPitch, pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); + EG2.increment(1); + if (!EG2.toStageEndLeft()) EG2.update(EGADSR::event_stage_end, pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); } - EG3.increment(steps); + EG3.increment(1); if (!EG3.toEndLeft()) EG3.update(); // neutralize envelope coefficient if end reached + + Pos = newPos; + i = iSubFragmentEnd; } }