.discretize - .sendCollection

Hi there, wonderful people of SC!

I’m practicing with the GrainBuf Ugen on a machine with Windows, SC 3.7.2 version.

In the Help documentation, I’ve found this first example :

s.boot;

(
var winenv;

b = Buffer.read(s, Platform.resourceDir +/+ “sounds/a11wlk01-44_1.aiff”);
// a custom envelope
winenv = Env([0, 1, 0], [0.5, 0.5], [8, -8]);
z = Buffer.sendCollection(s, winenv.discretize, 1);

SynthDef(\buf_grain_test, { |out, gate = 1, amp = 1, sndbuf, envbuf|
var pan, env, freqdev;
// use mouse x to control panning
pan = MouseX.kr(-1, 1);
env = EnvGen.kr(
Env([0, 1, 0], [1, 1], \sin, 1),
gate,
levelScale: amp,
doneAction: 2);
Out.ar(out,
GrainBuf.ar(2, Impulse.kr(10), 0.1, sndbuf, LFNoise1.kr.range(0.5, 2),
LFNoise2.kr(0.1).range(0, 1), 2, pan, envbuf) * env)
}).add;
)

// use built-in env
x = Synth(\buf_grain_test, [\sndbuf, b, \envbuf, -1])

// switch to the custom env
x.set(\envbuf, z)
x.set(\envbuf, -1);

x.set(\gate, 0);

Now, everything is fine, and I like the idea of building a customized envelope for the individual grains.

But I’m having a hard time with two topics :

  • I can’t figure out what the method .discretize really means, although I can imagine it. Alas, Help file doesn’t really help here.
  • I’m trying to understand the difference between the .sendCollection and . loadCollection

I’m quite surprised as well to find out that the info on the Help file about this latter method (.sendCollection) are not matching:

Help file reports this :

sendCollection (collection, startFrame: 0, wait: -1, action),

while on the IDE I can see this :

sendCollection (server, collection, numChannels, wait, action).

Is this a known bug or error?

Thank you in advance for your kind help!

.discretize samples the envelope at equally-spaced time points.

You’re right to note that the method is undocumented – would be worth filing a bug report for that.

You could actually investigate it for yourself in the meantime.

e = Env([0, 1, 0.5, 0], [0.1, 0.4, 0.8]);
-> an Env

e.discretize(512);
-> Signal[ 0.0, 0.025440312922001, 0.050880625844002, 0.076320938766003, 0.101761251688, 0.12720157206059, 0.15264187753201, 0.17808219790459, 0.20352250337601, 0.22896282374859, 0.25440314412117, 0.27984344959259, 0.30528375506401, 0.33072406053543, 0.35616439580917, 0.38160470128059, 0.40704500675201, 0.43248531222343, 0.45792564749718, 0.4833659529686, 0.50880628824234, 0.53424656391144, 0.55968689918518, 0.58512717485428, 0.61056751012802, 0.63600784540176, 0.66144812107086, 0.6868884563446, 0.712328791618...etc...

OK, so it’s a big long list of numbers. One way to look at a long list of numbers is to plot it.

e.discretize(512).plot;

… and you’ll see the envelope shape.

I’m trying to understand the difference between the .sendCollection and .loadCollection

loadCollection uses a temporary disk file (faster for large arrays). sendCollection sends all of the data using network messages (a bit less work for the system, if the array is small – the default discretize size is 1024, which counts as small in this context).

Help file reports this:

sendCollection (collection, startFrame: 0, wait: -1, action),

while on the IDE I can see this:

sendCollection (server, collection, numChannels, wait, action).

Is this a known bug or error?

No. It’s correct.

There are two sendCollection methods:

  • One belongs to the Buffer class: Buffer.sendCollection(...). Its job is to create and allocate a new buffer and fill it with the given collection. Because it’s a new buffer, you have to tell it which server to use and how many channels it should have. http://doc.sccode.org/Classes/Buffer.html#*sendCollection

  • The other belongs to Buffer instances: aBufferObjectThatIAlreadyMade.sendCollection(...). This method uses an existing buffer and puts the array’s contents into it. Because the buffer already exists, the server and number of channels are already known, so you don’t have to shouldn’t state them again. Buffer | SuperCollider 3.12.2 Help

hjh

3 Likes

Dear James,

thank you so much for your precious and detailed answer!

It would have been very hard for me to figure out the .discretize thing as clearly as you did without your valuable help!

As for the sendCollection, I see I made confusion between class method and instance method…

Thank you so much, again!