Supercollider 3 – More Function Stuff

audio, supercollider

Day Three of 30 Days of Supercollider

This will be a short one.

There is some more weirdness with functions in SC that I didn’t think of yesterday. This one is actually a pretty cool language feature. Just something you don’t see in most languages. It has to do with the way methods are called, or I guess I should say the way messages are sent to objects.

Yesterday I was using syntax like this to play a unit generator:

{ SinOsc.ar(400) }.play;

And that’s fine. But there’s an alternate syntax that does the same thing:

play({ SinOsc.ar(400) });

In other words, you can send the play message to a function instance, or you call the play method, passing in a function to play. Both are equivalent and probably one converts to the other in the back end.

Similarly, you can send a value message to a function, or pass a function to the value method.

{ 42 }.value;

value({ 42 });

This goes way beyond functions though. In Supercollider, if you want to send a message to the Post window, you use the postln method. You can do it like this:

postln("hello");

Or you can send the postln method to whatever you want to print.

"hello".postln;

This can be really useful for debugging, because in addition to posting the value to the Post window, it will return the value that it just posted, so tacking on postln to something is completely transparent to the logic of your code. For example…

(
f = {
	a = rand(10);
	b = rand(10);
	a * b;
};
f.value;
)

This function chooses two random numbers and returns their product. But say instead of rand, you were calling some function that returned an important value. But your code is not doing what you expect it to so you want to trace out the values of a and b. In many languages, you’d need to add more lines for the postln calls:

(
f = {
	a = rand(10);
    postln(a);
	b = rand(10);
    postln(b);
	a * b;
};
f.value;
)

But in Supercollider, you can just do this:

(
f = {
	a = rand(10).postln;
	b = rand(10).postln;
	a * b;
};
f.value;
)

The values get posted and the code continues to work as expected with no side effects caused the the post.

Some other examples:

All the array functions.

reverse([1, 2, 3, 4, 5]);

// or...

[1, 2, 3, 4, 5].reverse;

Numbers:

squared(4);

// or

4.squared;

This can look a bit confusing at first when applied to floating point numbers.

0.7.cos;

1.0.cubed;

But you get used to it. The hardest part is that when you’re learning and looking at other people’s examples, some will use one form of the syntax, and some will use the other form, sometimes even mixing them. So it’s best to get used to both ways.

One thought on “Supercollider 3 – More Function Stuff

  1. This will be an index of the articles I post about Supercollider. Warning: I don’t know a lot about Supercollider yet. This will be a journal of my discoveries as much as anything else. I pro…

Leave a Reply