The Red Penguin

Using ResamplingAudioPlayer to implement variable speed playback


Thursday 30 July 2020

We're adding one more piece of functionality - to be able to change the speed which DJs often do.

1. The component we're going to use is called ResamplingAudioSource. So we need to add this to private: in .h

juce::ResamplingAudioSource resampleSource{&transportSource, false, 2};

2. Go to the .cpp file and to the PrepareToPlay function. Under transportSource.prepareToPlay(samplesPerBlockExpected, sampleRate); we can add

resampleSource.prepareToPlay(samplesPerBlockExpected, sampleRate);

3. In getNextAudioBlock we need to get rid of transportSource.getNextAudioBlock(bufferToFill); and change it to

resampleSource.getNextAudioBlock(bufferToFill);

4. We need to add a slider to change the speed. In .h we add

juce::Slider speedSlider;

5. In the .cpp file we add the three steps as before:

addAndMakeVisible(speedSlider);
speedSlider.addListener(this);
speedSlider.setBounds(0, rowH * 3, getWidth(), rowH);

6. Finally we need to add some code for the listener for the slider. So in MainComponent::sliderValueChanged we can add:
if (slider == &speedSlider)
{
resampleSource.setResamplingRatio(slider->getValue());
}

And that will get that working!