How to load into different buffers small parts of a larger soundfile


I have a soundfile of 524288 samples and i would like to subdivide it into 256 parts of 2048 samples each and load them into 256 different buffers. Does anyone know how? SoundFile unfortunately does not have a startFrame and numFrames arg so i could specify which part of it to write on the buffer. Any ideas?



I had the exact same need :slight_smile: . Apparently, you don’t need to use the SoundFile class for this. I think this class is designed to get metadata about a file rather than manipulating it.

Actually, you can use the Buffer class directly to do this, because its method read allows to specify a file, a startFrame and a numFrames argument.

Here is what I tried, just change the values to fit your needs :

var filePath = Platform.userHomeDir ++ "/SC/AudioFiles/celloMono.wav";
var buffersNumber = 4;
var buffersSize = 256;
var buffers = Array.newClear(buffersNumber); { |n|
	var buffer =
		startFrame: buffersSize * n,
		numFrames: buffersSize
	buffers.put(n, buffer);
	CmdPeriod.doOnce({ });
} );

Please note that writing into a buffer is an asynchronous task, so the example above isn’t really usable as it is. You might have to store the buffers inside a global variable (i.e. declare the Array as a global variable), or recursively load buffers until they’re all done using the action argument of the read method.

[Dindoleon] this was spot on!!!
thank you so much for your quick response!