Resampling a Wave file?


#1

Hello,

I’m reading a Wave file using the following steps:

  1. Creating a File object.

  2. Creating a FileInputStream.

  3. Creating a WavAudioFormat reader for the file stream

  4. Creating an AudioFormatReaderSource for the reader.

    Then I call prepareToPlay on the AudioFormatReaderSource and I read blocks from it. This works great! However, I want to handle situations where the output sample rate is different than the Wave file’s sample rate. I thought that AudioFormatReaderSource would handle this, because prepareToPlay takes the output sample rate as a parameter, but that’s not the case. So, I’m pretty sure I need to use a ResamplingAudioSource in the chain… somewhere. Unfortunately, I’m really uncertain what to wrap in the ResamplingAudioSource. The only AudioSource that I have is the AudioFormatReaderSource, but if I create a ResamplingAudioSource out of it, I’ll lose functions like setLooping() and getTotalLength() and setNextReadPosition() that I need access to, but which are part of the AudioFormatReaderSource, and aren’t part of ResamplingAudioSource.

    What’s the best method for doing this kind of resampling when reading a file?

Thanks!
Dan


#2

have a look at http://www.rawmaterialsoftware.com/api/classAudioTransportSource.html

just pass your AudioFormatReaderSource and the original sample rate of the file to the AudioTransportSource::setSource method.

You can then prepare the AudioTransportSource with your desired output sample rate and it will resample as required. It’s also positionable, just don’t forget to call ::start() though or you’ll just get silent output.