Resampling audio from buffer

Hi there. I’ve previously used juce::ResamplingAudioSource to play a sound file at a different speed, and now I’m trying to resample a juce::AudioBuffer . However, I’m encountering a problem with the resampling process, as it produces a very glitchy sound.

getNextAudioBlock(const juce::AudioSourceChannelInfo& bufferToFill)
{
    juce::AudioBuffer<float> buffer(*bufferToFill.buffer);

//other processing code 

  ResampledMemorySource memSource(bufferToResample, resamplingRatio);
  //prepare resampler
  memSource.prepareToPlay(bufferToResample.getNumSamples(), spec.sampleRate);
  //resample the buffer
  memSource.getNextAudioBlock(juce::AudioSourceChannelInfo(resampledBuffer));
  ////Copy bufferToResample to buffer
  buffer.makeCopyOf(resampledBuffer);
}

ResamplingMemorySource is a simple class to resample a juce::AudioBuffer using juce::MemoryAudioSource and juce::ResamplingAudioSource classes.
What should I do to fix this problem?

getNextAudioBlock() is your audio thread. So there are a few problems:

  1. you are allocating memory in the audio thread, which might stall the processing
  2. you ignore the bufferToFill.startSample and bufferToFill.numSamples, so there might be unintentional samples copied into
  3. your prepareToPlay should have been called in advance. The MemoryAudioSource should be a member for that purpose.
  4. makeCopyOf also allocates memory, see 1.
1 Like

Really thanks. I should have paid attention to these. Being attentive to these points fixed the glitchy sound.

Great! Glad it worked

1 Like