--- linuxsampler/trunk/src/engines/gig/Voice.cpp 2004/04/27 09:21:58 56 +++ linuxsampler/trunk/src/engines/gig/Voice.cpp 2004/09/07 09:32:21 233 @@ -27,14 +27,23 @@ namespace LinuxSampler { namespace gig { - // FIXME: no support for layers (nor crossfades) yet + // TODO: no support for crossfades yet const float Voice::FILTER_CUTOFF_COEFF(CalculateFilterCutoffCoeff()); + const int Voice::FILTER_UPDATE_MASK(CalculateFilterUpdateMask()); + float Voice::CalculateFilterCutoffCoeff() { return log(FILTER_CUTOFF_MIN / FILTER_CUTOFF_MAX); } + int Voice::CalculateFilterUpdateMask() { + if (FILTER_UPDATE_PERIOD <= 0) return 0; + int power_of_two; + for (power_of_two = 0; 1<pOutputLeft = pAudioOutputDevice->Channel(0)->Buffer(); - this->pOutputRight = pAudioOutputDevice->Channel(1)->Buffer(); - this->MaxSamplesPerCycle = pAudioOutputDevice->MaxSamplesPerCycle(); - this->SampleRate = pAudioOutputDevice->SampleRate(); - } - void Voice::SetEngine(Engine* pEngine) { this->pEngine = pEngine; @@ -95,7 +97,7 @@ pLFO3 = new LFO(-1200.0f, 1200.0f, LFO::propagation_middle_balanced, pVCOManipulator, pEngine->pEventPool); // +-1 octave (+-1200 cents) max. this->pDiskThread = pEngine->pDiskThread; - dmsg(1,("Voice::SetEngine()\n")); + dmsg(6,("Voice::SetEngine()\n")); } /** @@ -105,9 +107,10 @@ * @param pNoteOnEvent - event that caused triggering of this voice * @param PitchBend - MIDI detune factor (-8192 ... +8191) * @param pInstrument - points to the loaded instrument which provides sample wave(s) and articulation data + * @param iLayer - layer number this voice refers to (only if this is a layered sound of course) * @returns 0 on success, a value < 0 if something failed */ - int Voice::Trigger(Event* pNoteOnEvent, int PitchBend, ::gig::Instrument* pInstrument) { + int Voice::Trigger(Event* pNoteOnEvent, int PitchBend, ::gig::Instrument* pInstrument, int iLayer) { if (!pInstrument) { dmsg(1,("voice::trigger: !pInstrument\n")); exit(EXIT_FAILURE); @@ -122,24 +125,115 @@ pTriggerEvent = pNoteOnEvent; if (!pRegion) { - std::cerr << "Audio Thread: No Region defined for MIDI key " << MIDIKey << std::endl << std::flush; + std::cerr << "gig::Voice: No Region defined for MIDI key " << MIDIKey << std::endl << std::flush; Kill(); return -1; } - //TODO: current MIDI controller values are not taken into account yet - ::gig::DimensionRegion* pDimRgn = NULL; - for (int i = pRegion->Dimensions - 1; i >= 0; i--) { // Check if instrument has a velocity split - if (pRegion->pDimensionDefinitions[i].dimension == ::gig::dimension_velocity) { - uint DimValues[5] = {0,0,0,0,0}; + // get current dimension values to select the right dimension region + //FIXME: controller values for selecting the dimension region here are currently not sample accurate + uint DimValues[5] = {0,0,0,0,0}; + for (int i = pRegion->Dimensions - 1; i >= 0; i--) { + switch (pRegion->pDimensionDefinitions[i].dimension) { + case ::gig::dimension_samplechannel: + DimValues[i] = 0; //TODO: we currently ignore this dimension + break; + case ::gig::dimension_layer: + DimValues[i] = iLayer; + // if this is the 1st layer then spawn further voices for all the other layers + if (iLayer == 0) + for (int iNewLayer = 1; iNewLayer < pRegion->pDimensionDefinitions[i].zones; iNewLayer++) + pEngine->LaunchVoice(pNoteOnEvent, iNewLayer); + break; + case ::gig::dimension_velocity: DimValues[i] = pNoteOnEvent->Velocity; - pDimRgn = pRegion->GetDimensionRegionByValue(DimValues[4],DimValues[3],DimValues[2],DimValues[1],DimValues[0]); - break; + break; + case ::gig::dimension_channelaftertouch: + DimValues[i] = 0; //TODO: we currently ignore this dimension + break; + case ::gig::dimension_releasetrigger: + DimValues[i] = 0; //TODO: we currently ignore this dimension + break; + case ::gig::dimension_keyboard: + DimValues[i] = (uint) pNoteOnEvent->Key; + break; + case ::gig::dimension_modwheel: + DimValues[i] = pEngine->ControllerTable[1]; + break; + case ::gig::dimension_breath: + DimValues[i] = pEngine->ControllerTable[2]; + break; + case ::gig::dimension_foot: + DimValues[i] = pEngine->ControllerTable[4]; + break; + case ::gig::dimension_portamentotime: + DimValues[i] = pEngine->ControllerTable[5]; + break; + case ::gig::dimension_effect1: + DimValues[i] = pEngine->ControllerTable[12]; + break; + case ::gig::dimension_effect2: + DimValues[i] = pEngine->ControllerTable[13]; + break; + case ::gig::dimension_genpurpose1: + DimValues[i] = pEngine->ControllerTable[16]; + break; + case ::gig::dimension_genpurpose2: + DimValues[i] = pEngine->ControllerTable[17]; + break; + case ::gig::dimension_genpurpose3: + DimValues[i] = pEngine->ControllerTable[18]; + break; + case ::gig::dimension_genpurpose4: + DimValues[i] = pEngine->ControllerTable[19]; + break; + case ::gig::dimension_sustainpedal: + DimValues[i] = pEngine->ControllerTable[64]; + break; + case ::gig::dimension_portamento: + DimValues[i] = pEngine->ControllerTable[65]; + break; + case ::gig::dimension_sostenutopedal: + DimValues[i] = pEngine->ControllerTable[66]; + break; + case ::gig::dimension_softpedal: + DimValues[i] = pEngine->ControllerTable[67]; + break; + case ::gig::dimension_genpurpose5: + DimValues[i] = pEngine->ControllerTable[80]; + break; + case ::gig::dimension_genpurpose6: + DimValues[i] = pEngine->ControllerTable[81]; + break; + case ::gig::dimension_genpurpose7: + DimValues[i] = pEngine->ControllerTable[82]; + break; + case ::gig::dimension_genpurpose8: + DimValues[i] = pEngine->ControllerTable[83]; + break; + case ::gig::dimension_effect1depth: + DimValues[i] = pEngine->ControllerTable[91]; + break; + case ::gig::dimension_effect2depth: + DimValues[i] = pEngine->ControllerTable[92]; + break; + case ::gig::dimension_effect3depth: + DimValues[i] = pEngine->ControllerTable[93]; + break; + case ::gig::dimension_effect4depth: + DimValues[i] = pEngine->ControllerTable[94]; + break; + case ::gig::dimension_effect5depth: + DimValues[i] = pEngine->ControllerTable[95]; + break; + case ::gig::dimension_none: + std::cerr << "gig::Voice::Trigger() Error: dimension=none\n" << std::flush; + break; + default: + std::cerr << "gig::Voice::Trigger() Error: Unknown dimension\n" << std::flush; } } - if (!pDimRgn) { // if there was no velocity split - pDimRgn = pRegion->GetDimensionRegionByValue(0,0,0,0,0); - } + ::gig::DimensionRegion* pDimRgn = pRegion->GetDimensionRegionByValue(DimValues[4],DimValues[3],DimValues[2],DimValues[1],DimValues[0]); pSample = pDimRgn->pSample; // sample won't change until the voice is finished @@ -148,7 +242,7 @@ DiskVoice = cachedsamples < pSample->SamplesTotal; if (DiskVoice) { // voice to be streamed from disk - MaxRAMPos = cachedsamples - (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 << 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) { @@ -179,7 +273,7 @@ { double pitchbasecents = pDimRgn->FineTune * 10; if (pDimRgn->PitchTrack) pitchbasecents += (MIDIKey - (int) pDimRgn->UnityNote) * 100; - this->PitchBase = RTMath::CentsToFreqRatio(pitchbasecents); + this->PitchBase = RTMath::CentsToFreqRatio(pitchbasecents) * (double(pSample->SamplesPerSecond) / double(pEngine->pAudioOutputDevice->SampleRate())); this->PitchBend = RTMath::CentsToFreqRatio(((double) PitchBend / 8192.0) * 200.0); // pitchbend wheel +-2 semitones = 200 cents } @@ -305,7 +399,7 @@ pDimRgn->LFO1ControlDepth, pEngine->ControllerTable[pLFO1->ExtController], pDimRgn->LFO1FlipPhase, - this->SampleRate, + pEngine->SampleRate, Delay); } @@ -343,6 +437,7 @@ pDimRgn->LFO2ControlDepth, pEngine->ControllerTable[pLFO2->ExtController], pDimRgn->LFO2FlipPhase, + pEngine->SampleRate, Delay); } #endif // ENABLE_FILTER @@ -380,7 +475,7 @@ pDimRgn->LFO3ControlDepth, pEngine->ControllerTable[pLFO3->ExtController], false, - this->SampleRate, + pEngine->SampleRate, Delay); } @@ -478,8 +573,8 @@ VCFCutoffCtrl.fvalue = cutoff - FILTER_CUTOFF_MIN; VCFResonanceCtrl.fvalue = resonance; - FilterLeft.SetParameters(cutoff, resonance, SampleRate); - FilterRight.SetParameters(cutoff, resonance, SampleRate); + FilterLeft.SetParameters(cutoff, resonance, pEngine->SampleRate); + FilterRight.SetParameters(cutoff, resonance, pEngine->SampleRate); FilterUpdateCounter = -1; } @@ -510,7 +605,7 @@ void Voice::Render(uint Samples) { // Reset the synthesis parameter matrix - pEngine->ResetSynthesisParameters(Event::destination_vca, this->Volume); + pEngine->ResetSynthesisParameters(Event::destination_vca, this->Volume * pEngine->GlobalVolume); pEngine->ResetSynthesisParameters(Event::destination_vco, this->PitchBase); #if ENABLE_FILTER pEngine->ResetSynthesisParameters(Event::destination_vcfc, VCFCutoffCtrl.fvalue); @@ -535,6 +630,11 @@ pLFO3->Process(Samples); + #if ENABLE_FILTER + CalculateBiquadParameters(Samples); // calculate the final biquad filter parameters + #endif // ENABLE_FILTER + + switch (this->PlaybackState) { case playback_state_ram: { @@ -567,8 +667,8 @@ } // add silence sample at the end if we reached the end of the stream (for the interpolator) - if (DiskStreamRef.State == Stream::state_end && DiskStreamRef.pStream->GetReadSpace() < (MaxSamplesPerCycle << MAX_PITCH) / pSample->Channels) { - DiskStreamRef.pStream->WriteSilence((MaxSamplesPerCycle << MAX_PITCH) / pSample->Channels); + if (DiskStreamRef.State == Stream::state_end && DiskStreamRef.pStream->GetReadSpace() < (pEngine->MaxSamplesPerCycle << MAX_PITCH) / pSample->Channels) { + DiskStreamRef.pStream->WriteSilence((pEngine->MaxSamplesPerCycle << MAX_PITCH) / pSample->Channels); this->PlaybackState = playback_state_end; } @@ -748,6 +848,52 @@ #endif // ENABLE_FILTER } + #if ENABLE_FILTER + /** + * Calculate all necessary, final biquad filter parameters. + * + * @param Samples - number of samples to be rendered in this audio fragment cycle + */ + void Voice::CalculateBiquadParameters(uint Samples) { + if (!FilterLeft.Enabled) return; + + biquad_param_t bqbase; + 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, prev_res, pEngine->SampleRate); + pEngine->pBasicFilterParameters[0] = bqbase; + pEngine->pMainFilterParameters[0] = bqmain; + + float* bq; + for (int i = 1; i < Samples; i++) { + // recalculate biquad parameters if cutoff or resonance differ from previous sample point + if (!(i & FILTER_UPDATE_MASK)) if (pEngine->pSynthesisParameters[Event::destination_vcfr][i] != prev_res || + pEngine->pSynthesisParameters[Event::destination_vcfc][i] != prev_cutoff) { + prev_cutoff = pEngine->pSynthesisParameters[Event::destination_vcfc][i]; + prev_res = pEngine->pSynthesisParameters[Event::destination_vcfr][i]; + FilterLeft.SetParameters(&bqbase, &bqmain, prev_cutoff, prev_res, pEngine->SampleRate); + } + + //same as 'pEngine->pBasicFilterParameters[i] = bqbase;' + bq = (float*) &pEngine->pBasicFilterParameters[i]; + bq[0] = bqbase.a1; + bq[1] = bqbase.a2; + bq[2] = bqbase.b0; + bq[3] = bqbase.b1; + bq[4] = bqbase.b2; + + // same as 'pEngine->pMainFilterParameters[i] = bqmain;' + bq = (float*) &pEngine->pMainFilterParameters[i]; + bq[0] = bqmain.a1; + bq[1] = bqmain.a2; + bq[2] = bqmain.b0; + bq[3] = bqmain.b1; + bq[4] = bqmain.b2; + } + } + #endif // ENABLE_FILTER + /** * Interpolates the input audio data (no loop). * @@ -765,8 +911,8 @@ InterpolateOneStep_Stereo(pSrc, i, pEngine->pSynthesisParameters[Event::destination_vca][i], pEngine->pSynthesisParameters[Event::destination_vco][i], - pEngine->pSynthesisParameters[Event::destination_vcfc][i], - pEngine->pSynthesisParameters[Event::destination_vcfr][i]); + pEngine->pBasicFilterParameters[i], + pEngine->pMainFilterParameters[i]); } } else { // Mono Sample @@ -774,8 +920,8 @@ InterpolateOneStep_Mono(pSrc, i, pEngine->pSynthesisParameters[Event::destination_vca][i], pEngine->pSynthesisParameters[Event::destination_vco][i], - pEngine->pSynthesisParameters[Event::destination_vcfc][i], - pEngine->pSynthesisParameters[Event::destination_vcfr][i]); + pEngine->pBasicFilterParameters[i], + pEngine->pMainFilterParameters[i]); } } } @@ -799,8 +945,8 @@ InterpolateOneStep_Stereo(pSrc, i, pEngine->pSynthesisParameters[Event::destination_vca][i], pEngine->pSynthesisParameters[Event::destination_vco][i], - pEngine->pSynthesisParameters[Event::destination_vcfc][i], - pEngine->pSynthesisParameters[Event::destination_vcfr][i]); + pEngine->pBasicFilterParameters[i], + pEngine->pMainFilterParameters[i]); if (Pos > pSample->LoopEnd) { Pos = pSample->LoopStart + fmod(Pos - pSample->LoopEnd, pSample->LoopSize);; LoopCyclesLeft--; @@ -811,8 +957,8 @@ InterpolateOneStep_Stereo(pSrc, i, pEngine->pSynthesisParameters[Event::destination_vca][i], pEngine->pSynthesisParameters[Event::destination_vco][i], - pEngine->pSynthesisParameters[Event::destination_vcfc][i], - pEngine->pSynthesisParameters[Event::destination_vcfr][i]); + pEngine->pBasicFilterParameters[i], + pEngine->pMainFilterParameters[i]); } } else { // render loop (endless loop) @@ -820,8 +966,8 @@ InterpolateOneStep_Stereo(pSrc, i, pEngine->pSynthesisParameters[Event::destination_vca][i], pEngine->pSynthesisParameters[Event::destination_vco][i], - pEngine->pSynthesisParameters[Event::destination_vcfc][i], - pEngine->pSynthesisParameters[Event::destination_vcfr][i]); + pEngine->pBasicFilterParameters[i], + pEngine->pMainFilterParameters[i]); if (Pos > pSample->LoopEnd) { Pos = pSample->LoopStart + fmod(Pos - pSample->LoopEnd, pSample->LoopSize); } @@ -835,8 +981,8 @@ InterpolateOneStep_Mono(pSrc, i, pEngine->pSynthesisParameters[Event::destination_vca][i], pEngine->pSynthesisParameters[Event::destination_vco][i], - pEngine->pSynthesisParameters[Event::destination_vcfc][i], - pEngine->pSynthesisParameters[Event::destination_vcfr][i]); + pEngine->pBasicFilterParameters[i], + pEngine->pMainFilterParameters[i]); if (Pos > pSample->LoopEnd) { Pos = pSample->LoopStart + fmod(Pos - pSample->LoopEnd, pSample->LoopSize);; LoopCyclesLeft--; @@ -847,8 +993,8 @@ InterpolateOneStep_Mono(pSrc, i, pEngine->pSynthesisParameters[Event::destination_vca][i], pEngine->pSynthesisParameters[Event::destination_vco][i], - pEngine->pSynthesisParameters[Event::destination_vcfc][i], - pEngine->pSynthesisParameters[Event::destination_vcfr][i]); + pEngine->pBasicFilterParameters[i], + pEngine->pMainFilterParameters[i]); } } else { // render loop (endless loop) @@ -856,8 +1002,8 @@ InterpolateOneStep_Mono(pSrc, i, pEngine->pSynthesisParameters[Event::destination_vca][i], pEngine->pSynthesisParameters[Event::destination_vco][i], - pEngine->pSynthesisParameters[Event::destination_vcfc][i], - pEngine->pSynthesisParameters[Event::destination_vcfr][i]); + pEngine->pBasicFilterParameters[i], + pEngine->pMainFilterParameters[i]); if (Pos > pSample->LoopEnd) { Pos = pSample->LoopStart + fmod(Pos - pSample->LoopEnd, pSample->LoopSize);; }