Adding Buffers Together

Hi -

I’m curious if there is a standard way to take two buffers and combine them, consecutively, into a third buffer,
I was trying to use the following code, but it seems to stop reading after the first “copy” - and it would be great if there was a way to automatically trim/expand the buffer “d” to fit the new size. Thank you all.

(
b = Buffer.read(s, Platform.resourceDir +/+ "sounds/a11wlk01.wav");
c = Buffer.read(s, "/Users/wheeler/Desktop/render.flac");
d = Buffer.alloc(s, 200000);
b.copyData(d);
c.copyData(d);
)

Maybe like this? I suspect there are other/better/alternative ways to do it.

(
d = {
	PlayBuf.ar(1, b) + PlayBuf.ar(1, c);
}.asBuffer(max(b.duration, c.duration))
)
1 Like

FluidBufCompose in the FluCoMa library does exactly this.

Sam

1 Like

Sorry, what I meant by “combine them, consecutively” is have them one-after-the-other in a new buffer.

It still seems this may be possible with FluidBufCompose, by adjusting the destination start frame of the second buffer with the numFrames of the first buffer - but I’m not entirely sure how to do that?
Something like this…

(
fork{
    ~destination = Buffer(s);
    [~srcA,~srcB].do{
        arg src, index;
        FluidBufCompose.processBlocking(s,src, 
			destination:~destination,
			destGain:1,
			destStartFrame: index*src.numFrames,
			gain:-6.dbamp);
    };
    s.sync;
    ~destination.play;
}
)

Another way - I amended your original code:

(
// fork to make a Routine so sync can be used
fork {  
	b = Buffer.read(s, Platform.resourceDir +/+ "sounds/a11wlk01.wav");
	c = Buffer.read(s, "/Users/wheeler/Desktop/render.flac");
	s.sync;  // wait until buffers are updated
	d = Buffer.alloc(s, b.numFrames + c.numFrames); 
	b.copyData(d);
	c.copyData(d, dstStartAt: b.numFrames);
};
)

I seem to still be having a hard time getting the second buffer to write - did this work for you?

I’m not sure what you mean?
The above code worked ok for me here - I got a new buffer d with both c + d consecutively.
Can you show the code you used?

@TXMod - I used the same code - and I actually got it to work when using a11wlk01 twice - or trying a variety of smaller duration files. The problem seems to occur when using a longer file.

I’ve never use a flac file before in SC. I wonder if that might be an issue? (I’ve only used .wav and .aif).
If you run this code what does it post?

(
// fork to make a Routine so sync can be used
fork {  
	b = Buffer.read(s, Platform.resourceDir +/+ "sounds/a11wlk01.wav");
	c = Buffer.read(s, "/Users/wheeler/Desktop/render.flac");
	s.sync;  // waits until buffers are filled
	d = Buffer.alloc(s, b.numFrames + c.numFrames); 
	b.copyData(d);
	c.copyData(d, dstStartAt: b.numFrames);
	s.sync;
	b.numFrames.debug("b.numFrames");
	c.numFrames.debug("c.numFrames");
	d.numFrames.debug("d.numFrames");
};
)

Thanks for your patience - I think the issue was actually caused by not specifying the numChannels when copying the secondary buffer (which had 2, as opposed to the mono a11wlk01…

1 Like