Hi! I have been following examples on the forums and the tutorials to try to do a simple crossfade between buffers using SmoothedValue. The code works fine up until the second crossfade event, where a SmoothedValue object doesn’t revert its currentValue into 0, but instead caps on 1, making the crossfade useless. Here is how I’m using the class. First in the header, I define:
juce::SmoothedValue<double, juce::ValueSmoothingTypes::Linear> smoothedGain;
Then in the .cpp I declare and initialize it in prepareToPlay() as follows:
smoothedGain.reset(sampleRate, 0.01); smoothedGain.setTargetValue(1);
And finally in getNextAudioBlock() I, at the beginning run this line (that I found from a forum post and don’t quite understand the reasoning of):
smoothedGain.setTargetValue(1);
And finally in the event of a crossfade, I do this:
if (CF_FLAG == true) { float sampleCF = 0; lastFilterState = currentFilterState; CF_FLAG = false; auto* inBufferL = bufferToFill.buffer->getReadPointer(0, bufferToFill.startSample); auto* inBufferR = bufferToFill.buffer->getReadPointer(1, bufferToFill.startSample); auto* cfBufferL = scratchBuffer.getReadPointer(0, 0); auto* cfBufferR = scratchBuffer.getReadPointer(1, 0); for (int i = 0; i < bufferToFill.numSamples; i++) { const auto gain = smoothedGain.getNextValue(); sampleCF = cfBufferL[i] * (1.0 - gain) + inBufferL[i] * gain; block.setSample(0, i, sampleCF); sampleCF = cfBufferR[i] * (1.0 - gain) + inBufferR[i] * gain; block.setSample(1, i, sampleCF); }
What would be going wrong here? There doesn’t seem to be a method to reset the currentValue back to zero (it was said on another post that the .reset() should only be used in prepareToPlay() once).
Thank you!