Functions with several statements

Hi there

I’m new to SuperCollider, so any advice is much appreciated. My question is: is it true that any message sent to a function with several statements is executed on the last statement only? Thanks!

x = {, 0, [0.5, 0.5]);, 0, [0.5, 0.5]);
) //plays only the 400Hz tone, and…

x = {, 0, [0.5, 0.5]);, 0, [0.5, 0.5]);
) //plays only the 200Hz tone. Why is that exactly?

If you define a function, the last line determines what is returned from the function. In your case you call “play” on the return value of a function that returns a single SinOsc.
If you wanted to hear both simultaneously, you could add them up:

x = { ( +*0.5 ! 2 }.play;

1 Like

Hi! is a convenience method that

a) turns your function into a SynthDef,

b) sends it to the server, and then

c) creates a Synth from / of it.

Part of a) is to assume the last statement in your function is producing the signal that is to be output, and create a corresponding for you.

See e.g.

(Audio section)

I messed around with SC a very great deal before I really understood this, so your question is absolutely justified

Btw, you can have both of your sines by enclosing them in brackets and separating with a comma instead of semicolon. Then they are interpreted as the two halves of a single, 2-channel signal. Also have a look at Mix and Splay to combine several signals into one.

fork{loop{h=[5,7,8].choose*(2**(2 .. 8).choose);play{{,h+(h/64)),0,0.1)}!64)*,1/4,0,0,2)};2.wait}};

Have fun! It’s a great time to be alive


1 Like

Thanks both for your helpful responses, that’s much appreciated!