How does Buffer.set work exactly

When I load a sound to the buffer and then execute this code:

(
for (0, b.numFrames, { arg i;  b.set(i, 0.5)});
)

Why does the resulting waveform look like this:

When I print each value in the buffer, they are all 0.5. Why wouldn’t this just create a plot with a straight line at 0.5??

You can look up the implementation of set and see for yourself: https://github.com/supercollider/supercollider/blob/develop/SCClassLibrary/Common/Control/Buffer.sc#L395

	set { arg index, float ... morePairs;
		server.listSendMsg(this.setMsg(index, float, *morePairs));
	}

So it’s sending a server message to set the value.

And your loop is sending a separate server message for every sample in the buffer… even though messages are local, this is a high-stress way to do things, likely to expose communication breakdowns.

To set every sample in a buffer, fill will be a few thousand times more efficient.

http://doc.sccode.org/Classes/Buffer.html#-fill

Or, if you’re filling with data and not just a single value, setn can do up to 13000 values in one message. Reducing stress on the network layer by four orders of magnitude is much more likely to be successful.

hjh

2 Likes

Thanks for the help. That makes a lot more sense.