Truncating a buffer after it's been created

So, newbie question.

Is there a way to shorten the length of a buffer, after it’s been created using Buffer.alloc?

For instance: I create my (empty) buffer using Buffer.alloc, with 10s worth of frames.

I use it to capture audio in live performance (using RecordBuf), but I only capture 7s of material.

I want to then truncate the buffer length to just that 7s of audio (effectively reduce numFrames).

Thanks!

I don’t think there’s any way to do this directly - I think you can only free the allocated memory of a Buffer all at once. You could of course always allocate a new Buffer with the necessary length, copy over the data from the initial Buffer (using Buffer's .copydata method) and then free the initial Buffer. However, I don’t really understand your specific use case here or why you would need to do this at all - are you working with very large buffers and trying to save up memory space?

Thanks, yes I’d thought of that solution. I was just wanting to see if there was a more direct way of trimming down the existing buffer, just because I’ve got limited programming time and I want to limit the proliferation of buffers (I’m using several arrays of buffers already).

I’m not short of memory space, but I want to do this as some of my synths assume the whole buffer contains meaningful audio (eg granulators that process the whole buffer, PlayBuf that loops the whole buffer etc). It just means more programming… (lazy me)

I think you could do that with a routine. Declare the number of frames as a variable and then have the routine change the value.
Not sure though, not at my computer at the moment but I’ll give it a shot tomorrow

You can always also just shorten the length of the amount of the buffer the Ugens are playing since you’re “not short on memory”

There’s no way to change a buffer’s size.

Your choices are as stated above: copy the part of the buffer that you want into a new buffer (then free the original), or edit your SynthDefs to use only a specified part of the buffer.

“More programming” but no alternative.

hjh

Thanks all, lookin’ that way!