Basic question, speed up and slow down audio in getNextAudioBlock

Hi there, I’m just 2 months into learning JUCE and have a long way to go. For the past couple of weeks I’ve been trying to speed up and slow down audio in the getNextAudioBlock member function, using ResamplingAudioSource() and setResamplingRatio(), however my knowledge is still too limited to figure this out. I have an audiofile loading and playing, and now hoping to get real time control of playback speed, hence the getNextAudioBlock and not simply altering the sample rate in prepareToPlay.

I would be grateful if anyone could point me in a direction to achieve this.

Thanks and apologies for the basic question!

void getNextAudioBlock (const AudioSourceChannelInfo& bufferToFill) override
if (readerSource.get() == nullptr)
transportSource.getNextAudioBlock (bufferToFill);

You will need to make the audiotransportsource use a resamplingaudiosource.

1 Like

Thanks very much Xenakios, appreciate your answer and do you mean like AudioTransportSource.ResamplingAudioSource(); ?

Nevermind, it doesn’t seem to work like that unfortunately. AudioTransportSource can’t use a ResamplingAudioSource as its audio source. I will try to see how it could be done.

1 Like


AudioTransportSource has a ResamplingAudioSource built in…
But it is not very well exposed, you can only set the ratio by tweaking the sourceSampleRateToCorrectFor in setSource().

I don’t like ResamplingAudioSource, since it does only linear interpolation. It would be worthwhile to create an alternative that uses LagrangeInterpolator internally and inherits PositionableAudioSource, so you can feed it into your AudioTransportSource.

1 Like

Can that be done during playback without causing glitches? I assumed it would be a glitch festival and didn’t even bother trying that.

1 Like

I would think so too. Didn’t see that during playback was in the request, so I thought I’d mention it.
Like I said, it is not very well exposed.
One would need to patch juce or ask for that method to be added…

EDIT, just re-read: “real time control of playback speed”… sorry :wink:

1 Like

It seems to do something a bit more involved than plain linear interpolation, there’s for example a custom filter involved in the code. (But I do recall the results even then are not great, you can easily come up with test signals that break it…)

1 Like

Appreciate the ideas, if anything else comes to mind I’m all ears. Thanks.

Implementing (and testing) this properly is quite work intensive and I am not sure if I really want to be looking more into this right now. :frowning:

Basically, what you should do is what Daniel suggested above : implement a PositionableAudioSource subclass that can do the variable speed playback and can be used together with AudioTransportSource as its audio source. While ResamplingAudioSource is not great, it can do the basic job of producing the varispeeded audio inside your PositionableAudioSource subclass. (If you want really high quality results, Juce does not have the tools to easily implement that anyway, you will need a 3rd party audio resampling library.)

1 Like

Thankyou Xenakios and Daniel, that’s really useful and gives me a direction to work towards. Thanks for taking the time to answer.

Did you ever find a solution to this?