Change sample rate of SoundFile (downsample)

I am doing some batch processing of audio files, e.g. converting to FLAC or other formats, changing bit depth. I’d also like to be able to downsample (e.g. from 96kHz to 48 or 44.1kHz), to make alternate versions of my files, maintaining as much quality as possible. Changing the sample rate in SoundFile.openWrite doesn’t actually resample, it just sets the new file’s rate accordingly, so the saved audio file will be longer (or shorter), but with the original number of sample frames.

Is there any “built-in” way to do it (or a Quark)? Or will I have to use something like NRT rendering (with a PlayBuf or BufRd synth) to do this?


I’ve found something like this, to do linear interpolation on the Signal that holds the interleaved SoundFile data. If d is the data (Signal), and f is its SoundFile, I can do something like this:

d.clump(f.numChannels).flop.collect(_.resamp1(f.numFrames * ~newSampleRate / f.sampleRate))

The re-flop and flatten to get the new, resampled Signal. Seems to work…

I’d strongly recommend an external tool like sox to do it properly. Downsampling requires filtering or you’ll get aliasing, and linear interpolation is likely to introduce noise in upper frequencies. (Cubic interpolation as in PlayBuf is better, but by itself, won’t control aliasing.)


1 Like

Thanks for the suggestion. In the end, I figured out how to do it in batch in Audacity. It took me a bit of searching to figure out how to resample to a different sample rate in a Macro (there is no “Resample” scripting command exposed – even though there is a menu command for it – you need to use e.g. SetProject:Rate="48000").