AudioFile.h - how to play sample at different sampleRate?

i’m currently trying to build a module using Adam Stark’s AudioFile.h.

the test sample i’m loading has a sample rate of 44100, while Rack is running at 48000, making the playback of the sample happen faster than it should, causing the sound to be a slightly higher pitch and be over too quickly.

does anybody know how to get around this?

Yes, you must Do your own sample rate conversion. At the very least do a linear interpolation between samples.

thanks. do you know of any examples of this that i can draw from?

There was much discussion here about that not tool long ago.

i just found this post while searching.

is it really as simple as just incrementing the sample position by (file_sample_rate / rack_sample_rate)? that seems too simple for some reason.

You can do that (it’s called Zero Order Hold resampling), but the results won’t sound great, and you’ll end up with a lot of aliasing. Linear interpolation (blending between two adjacent samples) will help, but for best results you’ll want to use a proper sample rate converter on the buffer first. The Fundamental plugin uses Secret Rabbit Code (aka libsamplerate) which I have also had good experiences with.

4 Likes

thanks, i’m looking into this libsamplerate now. which of the Fundamental modules uses this? i’d like to look at an example to get an idea of how to use it.

Depends if you only need to do it once, or if you have to do it in real time for playback. Some of those fancy ones take a lot of cpu. For real time I use cubic interpolation. Works fine I the real world.

This is what I use. nothing very fancy: SquinkyVCV-main/CubicInterpolator.h at b6abfc4c07e7b3a8c3abffe9c333bf073190cb03 · kockie69/SquinkyVCV-main · GitHub

1 Like

The delay module uses it for pitch changes when modulating the delay time.

In your case, you can probably use the simple API to resample the entire sample on load (and when the sample rate changes); real-time processing shouldn’t be necessary.

1 Like

thanks, i see it. what i’m curious about is where the library is stored for the delay module to include it? i see the include but don’t see samplerate.h anywhere in the repository. do i only need the samplerate.h file from libsamplerate or do i need all the other libsamplerate files as well?

Did you sync the sub modules?

i’m gonna guess not, since i’m not sure what you mean.

Read in the manual how to build. It tells you.

if you mean this part, then yes i actually did do this when i first cloned the repo.

Clone the git repo’s submodules.

cd Fundamental
git submodule update --init --recursive

1 Like

Then it must be part of rack itself.

1 Like

yep that seems to be the case. i just tried including it in my module and it found it. thanks for the help.

thanks a bunch for this. turns out libsamplerate is included in Rack, so i managed to use it and now the sample is playing back perfectly. :slight_smile: thanks for all the help/suggestions, everyone.

4 Likes

This was discussed here:

1 Like