--- linuxsampler/trunk/src/engines/gig/Voice.cpp 2005/03/14 22:35:44 460 +++ linuxsampler/trunk/src/engines/gig/Voice.cpp 2005/06/08 21:00:06 617 @@ -35,13 +35,13 @@ const int Voice::FILTER_UPDATE_MASK(CalculateFilterUpdateMask()); float Voice::CalculateFilterCutoffCoeff() { - return log(FILTER_CUTOFF_MIN / FILTER_CUTOFF_MAX); + return log(CONFIG_FILTER_CUTOFF_MIN / CONFIG_FILTER_CUTOFF_MAX); } int Voice::CalculateFilterUpdateMask() { - if (FILTER_UPDATE_PERIOD <= 0) return 0; + if (CONFIG_FILTER_UPDATE_STEPS <= 0) return 0; int power_of_two; - for (power_of_two = 0; 1<FragmentPos() > pEngine->MaxSamplesPerCycle) { // FIXME: should be removed before the final release (purpose: just a sanity check for debugging) + #if CONFIG_DEVMODE + if (itNoteOnEvent->FragmentPos() > pEngine->MaxSamplesPerCycle) { // just a sanity check for debugging dmsg(1,("Voice::Trigger(): ERROR, TriggerDelay > Totalsamples\n")); } + #endif // CONFIG_DEVMODE Type = type_normal; MIDIKey = itNoteOnEvent->Param.Note.Key; pRegion = pInstrument->GetRegion(MIDIKey); - PlaybackState = playback_state_ram; // we always start playback from RAM cache and switch then to disk if needed + PlaybackState = playback_state_init; // mark voice as triggered, but no audio rendered yet Delay = itNoteOnEvent->FragmentPos(); itTriggerEvent = itNoteOnEvent; itKillEvent = Pool::Iterator(); @@ -296,7 +298,7 @@ DiskVoice = cachedsamples < pSample->SamplesTotal; if (DiskVoice) { // voice to be streamed from disk - MaxRAMPos = cachedsamples - (pEngine->MaxSamplesPerCycle << 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) + 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) { @@ -331,10 +333,15 @@ this->PitchBend = RTMath::CentsToFreqRatio(((double) PitchBend / 8192.0) * 200.0); // pitchbend wheel +-2 semitones = 200 cents } - Volume = pDimRgn->GetVelocityAttenuation(itNoteOnEvent->Param.Note.Velocity) / 32768.0f; // we downscale by 32768 to convert from int16 value range to DSP value range (which is -1.0..1.0) + const double velocityAttenuation = pDimRgn->GetVelocityAttenuation(itNoteOnEvent->Param.Note.Velocity); + + Volume = velocityAttenuation / 32768.0f; // we downscale by 32768 to convert from int16 value range to DSP value range (which is -1.0..1.0) Volume *= pDimRgn->SampleAttenuation; + // the length of the decay and release curves are dependent on the velocity + const double velrelease = 1 / pDimRgn->GetVelocityRelease(itNoteOnEvent->Param.Note.Velocity); + // setup EG 1 (VCA EG) { // get current value of EG1 controller @@ -364,15 +371,16 @@ pDimRgn->EG1Attack + eg1attack, pDimRgn->EG1Hold, pSample->LoopStart, - pDimRgn->EG1Decay1 + eg1decay, - pDimRgn->EG1Decay2 + eg1decay, + (pDimRgn->EG1Decay1 + eg1decay) * velrelease, + (pDimRgn->EG1Decay2 + eg1decay) * velrelease, pDimRgn->EG1InfiniteSustain, pDimRgn->EG1Sustain, - pDimRgn->EG1Release + eg1release, + (pDimRgn->EG1Release + eg1release) * velrelease, // the SSE synthesis implementation requires // the vca start to be 16 byte aligned SYNTHESIS_MODE_GET_IMPLEMENTATION(SynthesisMode) ? - Delay & 0xfffffffc : Delay); + Delay & 0xfffffffc : Delay, + velocityAttenuation); } @@ -405,12 +413,13 @@ pDimRgn->EG2Attack + eg2attack, false, pSample->LoopStart, - pDimRgn->EG2Decay1 + eg2decay, - pDimRgn->EG2Decay2 + eg2decay, + (pDimRgn->EG2Decay1 + eg2decay) * velrelease, + (pDimRgn->EG2Decay2 + eg2decay) * velrelease, pDimRgn->EG2InfiniteSustain, pDimRgn->EG2Sustain, - pDimRgn->EG2Release + eg2release, - Delay); + (pDimRgn->EG2Release + eg2release) * velrelease, + Delay, + velocityAttenuation); } @@ -535,15 +544,15 @@ } - #if FORCE_FILTER_USAGE + #if CONFIG_FORCE_FILTER const bool bUseFilter = true; #else // use filter only if instrument file told so const bool bUseFilter = pDimRgn->VCFEnabled; - #endif // FORCE_FILTER_USAGE + #endif // CONFIG_FORCE_FILTER SYNTHESIS_MODE_SET_FILTER(SynthesisMode, bUseFilter); if (bUseFilter) { - #ifdef OVERRIDE_FILTER_CUTOFF_CTRL - VCFCutoffCtrl.controller = OVERRIDE_FILTER_CUTOFF_CTRL; + #ifdef CONFIG_OVERRIDE_CUTOFF_CTRL + VCFCutoffCtrl.controller = CONFIG_OVERRIDE_CUTOFF_CTRL; #else // use the one defined in the instrument file switch (pDimRgn->VCFCutoffController) { case ::gig::vcf_cutoff_ctrl_modwheel: @@ -579,10 +588,10 @@ VCFCutoffCtrl.controller = 0; break; } - #endif // OVERRIDE_FILTER_CUTOFF_CTRL + #endif // CONFIG_OVERRIDE_CUTOFF_CTRL - #ifdef OVERRIDE_FILTER_RES_CTRL - VCFResonanceCtrl.controller = OVERRIDE_FILTER_RES_CTRL; + #ifdef CONFIG_OVERRIDE_RESONANCE_CTRL + VCFResonanceCtrl.controller = CONFIG_OVERRIDE_RESONANCE_CTRL; #else // use the one defined in the instrument file switch (pDimRgn->VCFResonanceController) { case ::gig::vcf_res_ctrl_genpurpose3: @@ -601,23 +610,23 @@ default: VCFResonanceCtrl.controller = 0; } - #endif // OVERRIDE_FILTER_RES_CTRL + #endif // CONFIG_OVERRIDE_RESONANCE_CTRL - #ifndef OVERRIDE_FILTER_TYPE + #ifndef CONFIG_OVERRIDE_FILTER_TYPE FilterLeft.SetType(pDimRgn->VCFType); FilterRight.SetType(pDimRgn->VCFType); #else // override filter type - FilterLeft.SetType(OVERRIDE_FILTER_TYPE); - FilterRight.SetType(OVERRIDE_FILTER_TYPE); - #endif // OVERRIDE_FILTER_TYPE + FilterLeft.SetType(CONFIG_OVERRIDE_FILTER_TYPE); + FilterRight.SetType(CONFIG_OVERRIDE_FILTER_TYPE); + #endif // CONFIG_OVERRIDE_FILTER_TYPE VCFCutoffCtrl.value = pEngineChannel->ControllerTable[VCFCutoffCtrl.controller]; VCFResonanceCtrl.value = pEngineChannel->ControllerTable[VCFResonanceCtrl.controller]; // calculate cutoff frequency float cutoff = (!VCFCutoffCtrl.controller) - ? exp((float) (127 - itNoteOnEvent->Param.Note.Velocity) * (float) pDimRgn->VCFVelocityScale * 6.2E-5f * FILTER_CUTOFF_COEFF) * FILTER_CUTOFF_MAX - : exp((float) VCFCutoffCtrl.value * 0.00787402f * FILTER_CUTOFF_COEFF) * FILTER_CUTOFF_MAX; + ? exp((float) (127 - itNoteOnEvent->Param.Note.Velocity) * (float) pDimRgn->VCFVelocityScale * 6.2E-5f * FILTER_CUTOFF_COEFF) * CONFIG_FILTER_CUTOFF_MAX + : exp((float) VCFCutoffCtrl.value * 0.00787402f * FILTER_CUTOFF_COEFF) * CONFIG_FILTER_CUTOFF_MAX; // calculate resonance float resonance = (float) VCFResonanceCtrl.value * 0.00787f; // 0.0..1.0 @@ -626,7 +635,7 @@ } Constrain(resonance, 0.0, 1.0); // correct resonance if outside allowed value range (0.0..1.0) - VCFCutoffCtrl.fvalue = cutoff - FILTER_CUTOFF_MIN; + VCFCutoffCtrl.fvalue = cutoff - CONFIG_FILTER_CUTOFF_MIN; VCFResonanceCtrl.fvalue = resonance; FilterUpdateCounter = -1; @@ -686,6 +695,10 @@ switch (this->PlaybackState) { + case playback_state_init: + this->PlaybackState = playback_state_ram; // we always start playback from RAM cache and switch then to disk if needed + // no break - continue with playback_state_ram + case playback_state_ram: { if (RAMLoop) SYNTHESIS_MODE_SET_LOOP(SynthesisMode, true); // enable looping @@ -723,7 +736,7 @@ // add silence sample at the end if we reached the end of the stream (for the interpolator) if (DiskStreamRef.State == Stream::state_end) { - const int maxSampleWordsPerCycle = (pEngine->MaxSamplesPerCycle << MAX_PITCH) * pSample->Channels + 6; // +6 for the interpolator algorithm + const int maxSampleWordsPerCycle = (pEngine->MaxSamplesPerCycle << CONFIG_MAX_PITCH) * pSample->Channels + 6; // +6 for the interpolator algorithm if (sampleWordsLeftToRead <= maxSampleWordsPerCycle) { // remember how many sample words there are before any silence has been added if (RealSampleWordsLeftToRead < 0) RealSampleWordsLeftToRead = sampleWordsLeftToRead; @@ -910,7 +923,7 @@ // calculate the influence length of this event (in sample points) uint end = (itNextCutoffEvent) ? itNextCutoffEvent->FragmentPos() : Samples; - cutoff = exp((float) itCutoffEvent->Param.CC.Value * 0.00787402f * FILTER_CUTOFF_COEFF) * FILTER_CUTOFF_MAX - FILTER_CUTOFF_MIN; + cutoff = exp((float) itCutoffEvent->Param.CC.Value * 0.00787402f * FILTER_CUTOFF_COEFF) * CONFIG_FILTER_CUTOFF_MAX - CONFIG_FILTER_CUTOFF_MIN; // apply cutoff frequency to the cutoff parameter sequence for (uint i = itCutoffEvent->FragmentPos(); i < end; i++) { @@ -963,8 +976,8 @@ biquad_param_t bqmain; float prev_cutoff = pEngine->pSynthesisParameters[Event::destination_vcfc][0]; float prev_res = pEngine->pSynthesisParameters[Event::destination_vcfr][0]; - FilterLeft.SetParameters( &bqbase, &bqmain, prev_cutoff + FILTER_CUTOFF_MIN, prev_res, pEngine->SampleRate); - FilterRight.SetParameters(&bqbase, &bqmain, prev_cutoff + FILTER_CUTOFF_MIN, prev_res, pEngine->SampleRate); + FilterLeft.SetParameters( &bqbase, &bqmain, prev_cutoff + CONFIG_FILTER_CUTOFF_MIN, prev_res, pEngine->SampleRate); + FilterRight.SetParameters(&bqbase, &bqmain, prev_cutoff + CONFIG_FILTER_CUTOFF_MIN, prev_res, pEngine->SampleRate); pEngine->pBasicFilterParameters[0] = bqbase; pEngine->pMainFilterParameters[0] = bqmain; @@ -977,8 +990,8 @@ { prev_cutoff = pEngine->pSynthesisParameters[Event::destination_vcfc][i]; prev_res = pEngine->pSynthesisParameters[Event::destination_vcfr][i]; - FilterLeft.SetParameters( &bqbase, &bqmain, prev_cutoff + FILTER_CUTOFF_MIN, prev_res, pEngine->SampleRate); - FilterRight.SetParameters(&bqbase, &bqmain, prev_cutoff + FILTER_CUTOFF_MIN, prev_res, pEngine->SampleRate); + FilterLeft.SetParameters( &bqbase, &bqmain, prev_cutoff + CONFIG_FILTER_CUTOFF_MIN, prev_res, pEngine->SampleRate); + FilterRight.SetParameters(&bqbase, &bqmain, prev_cutoff + CONFIG_FILTER_CUTOFF_MIN, prev_res, pEngine->SampleRate); } } @@ -1037,9 +1050,10 @@ * @param itKillEvent - event which caused the voice to be killed */ void Voice::Kill(Pool::Iterator& itKillEvent) { - //FIXME: just two sanity checks for debugging, can be removed + #if CONFIG_DEVMODE if (!itKillEvent) dmsg(1,("gig::Voice::Kill(): ERROR, !itKillEvent !!!\n")); if (itKillEvent && !itKillEvent.isValid()) dmsg(1,("gig::Voice::Kill(): ERROR, itKillEvent invalid !!!\n")); + #endif // CONFIG_DEVMODE if (itTriggerEvent && itKillEvent->FragmentPos() <= itTriggerEvent->FragmentPos()) return; this->itKillEvent = itKillEvent;