--- linuxsampler/trunk/src/engines/gig/Voice.cpp 2006/01/28 16:55:30 831 +++ linuxsampler/trunk/src/engines/gig/Voice.cpp 2007/01/06 11:02:58 1010 @@ -29,12 +29,6 @@ namespace LinuxSampler { namespace gig { - const float Voice::FILTER_CUTOFF_COEFF(CalculateFilterCutoffCoeff()); - - float Voice::CalculateFilterCutoffCoeff() { - return log(CONFIG_FILTER_CUTOFF_MAX / CONFIG_FILTER_CUTOFF_MIN); - } - Voice::Voice() { pEngine = NULL; pDiskThread = NULL; @@ -104,9 +98,12 @@ // calculate volume 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) + // For 16 bit samples, we downscale by 32768 to convert from + // int16 value range to DSP value range (which is + // -1.0..1.0). For 24 bit, we downscale from int32. + float volume = velocityAttenuation / (pSample->BitDepth == 16 ? 32768.0f : 32768.0f * 65536.0f); - Volume *= pDimRgn->SampleAttenuation; + volume *= pDimRgn->SampleAttenuation * pEngineChannel->GlobalVolume * GLOBAL_VOLUME; // the volume of release triggered samples depends on note length if (Type == type_release_trigger) { @@ -114,30 +111,39 @@ pEngineChannel->pMIDIKeyInfo[MIDIKey].NoteOnTime) / pEngine->SampleRate; float attenuation = 1 - 0.01053 * (256 >> pDimRgn->ReleaseTriggerDecay) * noteLength; if (attenuation <= 0) return -1; - Volume *= attenuation; + volume *= attenuation; } // select channel mode (mono or stereo) SYNTHESIS_MODE_SET_CHANNELS(SynthesisMode, pSample->Channels == 2); + // select bit depth (16 or 24) + SYNTHESIS_MODE_SET_BITDEPTH24(SynthesisMode, pSample->BitDepth == 24); // get starting crossfade volume level + float crossfadeVolume; switch (pDimRgn->AttenuationController.type) { case ::gig::attenuation_ctrl_t::type_channelaftertouch: - CrossfadeVolume = 1.0f; //TODO: aftertouch not supported yet + crossfadeVolume = Engine::CrossfadeCurve[CrossfadeAttenuation(pEngineChannel->ControllerTable[128])]; break; case ::gig::attenuation_ctrl_t::type_velocity: - CrossfadeVolume = CrossfadeAttenuation(itNoteOnEvent->Param.Note.Velocity); + crossfadeVolume = Engine::CrossfadeCurve[CrossfadeAttenuation(itNoteOnEvent->Param.Note.Velocity)]; break; case ::gig::attenuation_ctrl_t::type_controlchange: //FIXME: currently not sample accurate - CrossfadeVolume = CrossfadeAttenuation(pEngineChannel->ControllerTable[pDimRgn->AttenuationController.controller_number]); + crossfadeVolume = Engine::CrossfadeCurve[CrossfadeAttenuation(pEngineChannel->ControllerTable[pDimRgn->AttenuationController.controller_number])]; break; case ::gig::attenuation_ctrl_t::type_none: // no crossfade defined default: - CrossfadeVolume = 1.0f; + crossfadeVolume = 1.0f; } - PanLeft = Engine::PanCurve[64 - pDimRgn->Pan]; - PanRight = Engine::PanCurve[64 + pDimRgn->Pan]; + VolumeLeft = volume * Engine::PanCurve[64 - pDimRgn->Pan]; + VolumeRight = volume * Engine::PanCurve[64 + pDimRgn->Pan]; + + float subfragmentRate = pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE; + CrossfadeSmoother.trigger(crossfadeVolume, subfragmentRate); + VolumeSmoother.trigger(pEngineChannel->MidiVolume, subfragmentRate); + PanLeftSmoother.trigger(pEngineChannel->GlobalPanLeft, subfragmentRate); + PanRightSmoother.trigger(pEngineChannel->GlobalPanRight, subfragmentRate); finalSynthesisParameters.dPos = pDimRgn->SampleStartOffset; // offset where we should start playback of sample (0 - 2000 sample points) Pos = pDimRgn->SampleStartOffset; @@ -146,13 +152,15 @@ long cachedsamples = pSample->GetCache().Size / pSample->FrameSize; DiskVoice = cachedsamples < pSample->SamplesTotal; + const DLS::sample_loop_t& loopinfo = pDimRgn->pSampleLoops[0]; + if (DiskVoice) { // voice to be streamed from disk 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 - RAMLoop = (pSample->Loops && pSample->LoopEnd <= MaxRAMPos); + RAMLoop = (pDimRgn->SampleLoops && (loopinfo.LoopStart + loopinfo.LoopLength) <= MaxRAMPos); - if (pDiskThread->OrderNewStream(&DiskStreamRef, pSample, MaxRAMPos, !RAMLoop) < 0) { + if (pDiskThread->OrderNewStream(&DiskStreamRef, pDimRgn, MaxRAMPos, !RAMLoop) < 0) { dmsg(1,("Disk stream order failed!\n")); KillImmediately(); return -1; @@ -161,15 +169,15 @@ } else { // RAM only voice MaxRAMPos = cachedsamples; - RAMLoop = (pSample->Loops != 0); + RAMLoop = (pDimRgn->SampleLoops != 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; + loop.uiStart = loopinfo.LoopStart; + loop.uiEnd = loopinfo.LoopStart + loopinfo.LoopLength; + loop.uiSize = loopinfo.LoopLength; } // calculate initial pitch value @@ -192,7 +200,7 @@ eg1controllervalue = 0; break; case ::gig::eg1_ctrl_t::type_channelaftertouch: - eg1controllervalue = 0; // TODO: aftertouch not yet supported + eg1controllervalue = pEngineChannel->ControllerTable[128]; break; case ::gig::eg1_ctrl_t::type_velocity: eg1controllervalue = itNoteOnEvent->Param.Note.Velocity; @@ -223,11 +231,23 @@ pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); } +#ifdef CONFIG_INTERPOLATE_VOLUME // setup initial volume in synthesis parameters - fFinalVolume = getVolume() * EG1.getLevel(); - finalSynthesisParameters.fFinalVolumeLeft = fFinalVolume * PanLeft * pEngineChannel->GlobalPanLeft; - finalSynthesisParameters.fFinalVolumeRight = fFinalVolume * PanRight * pEngineChannel->GlobalPanRight; +#ifdef CONFIG_PROCESS_MUTED_CHANNELS + if (pEngineChannel->GetMute()) { + finalSynthesisParameters.fFinalVolumeLeft = 0; + finalSynthesisParameters.fFinalVolumeRight = 0; + } + else +#else + { + float finalVolume = pEngineChannel->MidiVolume * crossfadeVolume * EG1.getLevel(); + finalSynthesisParameters.fFinalVolumeLeft = finalVolume * VolumeLeft * pEngineChannel->GlobalPanLeft; + finalSynthesisParameters.fFinalVolumeRight = finalVolume * VolumeRight * pEngineChannel->GlobalPanRight; + } +#endif +#endif // setup EG 2 (VCF Cutoff EG) { @@ -238,7 +258,7 @@ eg2controllervalue = 0; break; case ::gig::eg2_ctrl_t::type_channelaftertouch: - eg2controllervalue = 0; // TODO: aftertouch not yet supported + eg2controllervalue = pEngineChannel->ControllerTable[128]; break; case ::gig::eg2_ctrl_t::type_velocity: eg2controllervalue = itNoteOnEvent->Param.Note.Velocity; @@ -316,12 +336,15 @@ pLFO1->ExtController = 0; // no external controller bLFO1Enabled = false; } - if (bLFO1Enabled) pLFO1->trigger(pDimRgn->LFO1Frequency, - start_level_max, - lfo1_internal_depth, - pDimRgn->LFO1ControlDepth, - pDimRgn->LFO1FlipPhase, - pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); + if (bLFO1Enabled) { + pLFO1->trigger(pDimRgn->LFO1Frequency, + start_level_min, + lfo1_internal_depth, + pDimRgn->LFO1ControlDepth, + pDimRgn->LFO1FlipPhase, + pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); + pLFO1->update(pLFO1->ExtController ? pEngineChannel->ControllerTable[pLFO1->ExtController] : 0); + } } @@ -359,12 +382,15 @@ pLFO2->ExtController = 0; // no external controller bLFO2Enabled = false; } - if (bLFO2Enabled) pLFO2->trigger(pDimRgn->LFO2Frequency, - start_level_max, - lfo2_internal_depth, - pDimRgn->LFO2ControlDepth, - pDimRgn->LFO2FlipPhase, - pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); + if (bLFO2Enabled) { + pLFO2->trigger(pDimRgn->LFO2Frequency, + start_level_max, + lfo2_internal_depth, + pDimRgn->LFO2ControlDepth, + pDimRgn->LFO2FlipPhase, + pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); + pLFO2->update(pLFO2->ExtController ? pEngineChannel->ControllerTable[pLFO2->ExtController] : 0); + } } @@ -384,8 +410,8 @@ break; case ::gig::lfo3_ctrl_aftertouch: lfo3_internal_depth = 0; - pLFO3->ExtController = 0; // TODO: aftertouch not implemented yet - bLFO3Enabled = false; // see TODO comment in line above + pLFO3->ExtController = 128; + bLFO3Enabled = true; break; case ::gig::lfo3_ctrl_internal_modwheel: lfo3_internal_depth = pDimRgn->LFO3InternalDepth; @@ -394,20 +420,23 @@ break; case ::gig::lfo3_ctrl_internal_aftertouch: lfo3_internal_depth = pDimRgn->LFO3InternalDepth; - pLFO1->ExtController = 0; // TODO: aftertouch not implemented yet - bLFO3Enabled = (lfo3_internal_depth > 0 /*|| pDimRgn->LFO3ControlDepth > 0*/); // see TODO comment in line above + pLFO1->ExtController = 128; + bLFO3Enabled = (lfo3_internal_depth > 0 || pDimRgn->LFO3ControlDepth > 0); break; default: lfo3_internal_depth = 0; pLFO3->ExtController = 0; // no external controller bLFO3Enabled = false; } - if (bLFO3Enabled) pLFO3->trigger(pDimRgn->LFO3Frequency, - start_level_mid, - lfo3_internal_depth, - pDimRgn->LFO3ControlDepth, - false, - pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); + if (bLFO3Enabled) { + pLFO3->trigger(pDimRgn->LFO3Frequency, + start_level_mid, + lfo3_internal_depth, + pDimRgn->LFO3ControlDepth, + false, + pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); + pLFO3->update(pLFO3->ExtController ? pEngineChannel->ControllerTable[pLFO3->ExtController] : 0); + } } @@ -449,7 +478,9 @@ case ::gig::vcf_cutoff_ctrl_genpurpose8: VCFCutoffCtrl.controller = 83; break; - case ::gig::vcf_cutoff_ctrl_aftertouch: //TODO: not implemented yet + case ::gig::vcf_cutoff_ctrl_aftertouch: + VCFCutoffCtrl.controller = 128; + break; case ::gig::vcf_cutoff_ctrl_none: default: VCFCutoffCtrl.controller = 0; @@ -507,15 +538,13 @@ else { cvalue = pDimRgn->VCFCutoff; } - cutoff *= float(cvalue) * 0.00787402f; // (1 / 127) - if (cutoff > 1.0) cutoff = 1.0; - cutoff = (cutoff < 0.5 ? cutoff * 4826 - 1 : cutoff * 5715 - 449); - if (cutoff < 1.0) cutoff = 1.0; + cutoff *= float(cvalue); + if (cutoff > 127.0f) cutoff = 127.0f; // calculate resonance - float resonance = (float) (VCFResonanceCtrl.controller ? VCFResonanceCtrl.value : pDimRgn->VCFResonance) * 0.00787f; // 0.0..1.0 + float resonance = (float) (VCFResonanceCtrl.controller ? VCFResonanceCtrl.value : pDimRgn->VCFResonance); - VCFCutoffCtrl.fvalue = cutoff - 1.0; + VCFCutoffCtrl.fvalue = cutoff; VCFResonanceCtrl.fvalue = resonance; } else { @@ -592,7 +621,7 @@ } } - sample_t* ptr = DiskStreamRef.pStream->GetReadPtr(); // get the current read_ptr within the ringbuffer where we read the samples from + sample_t* ptr = (sample_t*)DiskStreamRef.pStream->GetReadPtr(); // get the current read_ptr within the ringbuffer where we read the samples from // render current audio fragment Synthesize(Samples, ptr, Delay); @@ -645,7 +674,7 @@ * for the given time. * * @param itEvent - iterator pointing to the next event to be processed - * @param End - youngest time stamp where processing should be stopped + * @param End - youngest time stamp where processing should be stopped */ void Voice::processTransitionEvents(RTList::Iterator& itEvent, uint End) { for (; itEvent && itEvent->FragmentPos() <= End; ++itEvent) { @@ -664,7 +693,7 @@ * the given time. * * @param itEvent - iterator pointing to the next event to be processed - * @param End - youngest time stamp where processing should be stopped + * @param End - youngest time stamp where processing should be stopped */ void Voice::processCCEvents(RTList::Iterator& itEvent, uint End) { for (; itEvent && itEvent->FragmentPos() <= End; ++itEvent) { @@ -687,7 +716,13 @@ } if (pDimRgn->AttenuationController.type == ::gig::attenuation_ctrl_t::type_controlchange && itEvent->Param.CC.Controller == pDimRgn->AttenuationController.controller_number) { - processCrossFadeEvent(itEvent); + CrossfadeSmoother.update(Engine::CrossfadeCurve[CrossfadeAttenuation(itEvent->Param.CC.Value)]); + } + if (itEvent->Param.CC.Controller == 7) { // volume + VolumeSmoother.update(Engine::VolumeCurve[itEvent->Param.CC.Value]); + } else if (itEvent->Param.CC.Controller == 10) { // panpot + PanLeftSmoother.update(Engine::PanCurve[128 - itEvent->Param.CC.Value]); + PanRightSmoother.update(Engine::PanCurve[itEvent->Param.CC.Value]); } } else if (itEvent->Type == Event::type_pitchbend) { // if pitch bend event processPitchEvent(itEvent); @@ -701,31 +736,16 @@ PitchBend = pitch; } - void Voice::processCrossFadeEvent(RTList::Iterator& itEvent) { - CrossfadeVolume = CrossfadeAttenuation(itEvent->Param.CC.Value); - fFinalVolume = getVolume(); - } - - float Voice::getVolume() { - #if CONFIG_PROCESS_MUTED_CHANNELS - return pEngineChannel->GetMute() ? 0 : (Volume * CrossfadeVolume * pEngineChannel->GlobalVolume); - #else - return Volume * CrossfadeVolume * pEngineChannel->GlobalVolume; - #endif - } - void Voice::processCutoffEvent(RTList::Iterator& itEvent) { int ccvalue = itEvent->Param.CC.Value; if (VCFCutoffCtrl.value == ccvalue) return; VCFCutoffCtrl.value == ccvalue; if (pDimRgn->VCFCutoffControllerInvert) ccvalue = 127 - ccvalue; if (ccvalue < pDimRgn->VCFVelocityScale) ccvalue = pDimRgn->VCFVelocityScale; - float cutoff = CutoffBase * float(ccvalue) * 0.00787402f; // (1 / 127) - if (cutoff > 1.0) cutoff = 1.0; - cutoff = (cutoff < 0.5 ? cutoff * 4826 - 1 : cutoff * 5715 - 449); - if (cutoff < 1.0) cutoff = 1.0; + float cutoff = CutoffBase * float(ccvalue); + if (cutoff > 127.0f) cutoff = 127.0f; - VCFCutoffCtrl.fvalue = cutoff - 1.0; // needed for initialization of fFinalCutoff next time + VCFCutoffCtrl.fvalue = cutoff; // needed for initialization of fFinalCutoff next time fFinalCutoff = cutoff; } @@ -733,10 +753,10 @@ // convert absolute controller value to differential const int ctrldelta = itEvent->Param.CC.Value - VCFResonanceCtrl.value; VCFResonanceCtrl.value = itEvent->Param.CC.Value; - const float resonancedelta = (float) ctrldelta * 0.00787f; // 0.0..1.0 + const float resonancedelta = (float) ctrldelta; fFinalResonance += resonancedelta; // needed for initialization of parameter - VCFResonanceCtrl.fvalue = itEvent->Param.CC.Value * 0.00787f; + VCFResonanceCtrl.fvalue = itEvent->Param.CC.Value; } /** @@ -748,8 +768,8 @@ * @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.pOutLeft = &pEngineChannel->pChannelLeft->Buffer()[Skip]; + finalSynthesisParameters.pOutRight = &pEngineChannel->pChannelRight->Buffer()[Skip]; finalSynthesisParameters.pSrc = pSrc; RTList::Iterator itCCEvent = pEngineChannel->pEvents->first(); @@ -763,27 +783,31 @@ 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 finalSynthesisParameters.fFinalPitch = PitchBase * PitchBend; - fFinalVolume = getVolume(); fFinalCutoff = VCFCutoffCtrl.fvalue; fFinalResonance = VCFResonanceCtrl.fvalue; // process MIDI control change and pitchbend events for this subfragment processCCEvents(itCCEvent, iSubFragmentEnd); + float fFinalVolume = VolumeSmoother.render() * CrossfadeSmoother.render(); +#ifdef CONFIG_PROCESS_MUTED_CHANNELS + if (pEngineChannel->GetMute()) fFinalVolume = 0; +#endif + // 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) { + // 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() == EGADSR::segment_end)) { EG1.enterFadeOutStage(); itKillEvent = Pool::Iterator(); } @@ -814,14 +838,14 @@ if (EG3.active()) finalSynthesisParameters.fFinalPitch *= EG3.render(); // process low frequency oscillators - if (bLFO1Enabled) fFinalVolume *= pLFO1->render(); + if (bLFO1Enabled) fFinalVolume *= (1.0f - pLFO1->render()); if (bLFO2Enabled) fFinalCutoff *= pLFO2->render(); if (bLFO3Enabled) finalSynthesisParameters.fFinalPitch *= RTMath::CentsToFreqRatio(pLFO3->render()); // if filter enabled then update filter coefficients if (SYNTHESIS_MODE_GET_FILTER(SynthesisMode)) { - finalSynthesisParameters.filterLeft.SetParameters(fFinalCutoff + 1.0, fFinalResonance, pEngine->SampleRate); - finalSynthesisParameters.filterRight.SetParameters(fFinalCutoff + 1.0, fFinalResonance, pEngine->SampleRate); + finalSynthesisParameters.filterLeft.SetParameters(fFinalCutoff, fFinalResonance, pEngine->SampleRate); + finalSynthesisParameters.filterRight.SetParameters(fFinalCutoff, fFinalResonance, pEngine->SampleRate); } // do we need resampling? @@ -835,12 +859,16 @@ finalSynthesisParameters.uiToGo = iSubFragmentEnd - i; #ifdef CONFIG_INTERPOLATE_VOLUME finalSynthesisParameters.fFinalVolumeDeltaLeft = - (fFinalVolume * fFinalPanLeft - finalSynthesisParameters.fFinalVolumeLeft) / finalSynthesisParameters.uiToGo; + (fFinalVolume * VolumeLeft * PanLeftSmoother.render() - + finalSynthesisParameters.fFinalVolumeLeft) / finalSynthesisParameters.uiToGo; finalSynthesisParameters.fFinalVolumeDeltaRight = - (fFinalVolume * fFinalPanRight - finalSynthesisParameters.fFinalVolumeRight) / finalSynthesisParameters.uiToGo; + (fFinalVolume * VolumeRight * PanRightSmoother.render() - + finalSynthesisParameters.fFinalVolumeRight) / finalSynthesisParameters.uiToGo; #else - finalSynthesisParameters.fFinalVolumeLeft = fFinalVolume * fFinalPanLeft; - finalSynthesisParameters.fFinalVolumeRight = fFinalVolume * fFinalPanRight; + finalSynthesisParameters.fFinalVolumeLeft = + fFinalVolume * VolumeLeft * PanLeftSmoother.render(); + finalSynthesisParameters.fFinalVolumeRight = + fFinalVolume * VolumeRight * PanRightSmoother.render(); #endif // render audio for one subfragment RunSynthesisFunction(SynthesisMode, &finalSynthesisParameters, &loop); @@ -854,7 +882,7 @@ 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) { + if (pDimRgn->SampleLoops && Pos <= pDimRgn->pSampleLoops[0].LoopStart && pDimRgn->pSampleLoops[0].LoopStart < newPos) { EG1.update(EGADSR::event_hold_end, pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE); }