Jump to content

SuperCollider Cookbook: Difference between revisions

From CCRMA Wiki
Ruviaro (talk | contribs)
No edit summary
Ruviaro (talk | contribs)
No edit summary
Line 1: Line 1:
'''SuperCollider Cookbook by Bruno'''
'''SuperCollider Cookbook by Bruno'''


This page collects short and simple code examples of useful stuff. These are quick "reminders" of how to do common things.
This page collects short and simple code examples of useful stuff. These are just quick "reminders" of how to do common things.


==Play a sound file, vary speed, reverse==
==Play a sound file, vary speed, reverse==

Revision as of 04:53, 9 June 2011

SuperCollider Cookbook by Bruno

This page collects short and simple code examples of useful stuff. These are just quick "reminders" of how to do common things.

Play a sound file, vary speed, reverse

create a buffer

~mysound = Buffer.read(s, "path/to/sound/file.wav");

simple play

{PlayBuf.ar(1, ~mysound)}.play; // number of channels and buffer

get sound file info

[~mysound.bufnum, ~mysound.numChannels, ~mysound.path, ~mysound.numFrames];

varying speed

{PlayBuf.ar(1, ~mysound, 2, loop: 1)}.play; // play 2x faster
{PlayBuf.ar(1, ~mysound, 0.5, loop: 1)}.play; // play at half the speed
{PlayBuf.ar(1, ~mysound, Line.kr(0.5, 2, 10), loop: 1)}.play; // playback speed goes from 0.5 to 2 in 10 seconds

changing direction

{PlayBuf.ar(1, ~mysound, -1, loop: 1)}.play; // reverse sound
{PlayBuf.ar(1, ~mysound, -0.5, loop: 1)}.play; // play at half the speed AND reversed

Play a MIDI file

Do this and that.

Looping

While

(
i = 0;
while ( { i < 5 }, { i = i + 1; "boing".postln });
)

For

for (3, 7, { arg i; i.postln }); // prints values 3 through 7

ForBy

forBy (0, 8, 2, { arg i; i.postln }); // prints values 0 through 8 by 2's

Do

do(9, {"Wow".postln}); // print "Wow" nine times
9.do({"Wow".postln}); // same as above
5.do({ arg item; item.postln }); // iterates from zero to four
do(5, {arg item; item.postln}); // same as above

Compare the output of these:

do(9, {arg whatevername; whatevername.postln}); // give whatever name you want to the arg of 'do'
do([9, 8, 3, 5], {arg whatevername; whatevername.postln}); // elements from collection (not a counter)
do([9, 8, 3, 5], {arg whatevs1, whatevs2; [whatevs2, whatevs1].postln}); // second argument works as a counter

More examples:

["zero", "first", "second", "third", 500].do({ arg item, i; [i, item].postln; });
do(["zero", "first", "second", "third", 500], {arg item, i; [i, item].postln});
"you".do({ arg item; item.postln }); // a String is a collection of characters
'they'.do({ arg item; item.postln }); // a Symbol is a singular item
(8..20).do({ arg item; item.postln }); // iterates from eight to twenty 
(8,10..20).do({ arg item; item.postln }); // iterates from eight to twenty, with stepsize two


If...Else

Syntax is if(condition, {true action}, {false action}). Examples:

if(10 == 10, {"10 is indeed equal to 10"}, {"false"})

if(condition, {true action}, {false action});

if(10 == 10, {"10 is indeed equal to 10"}, {"false"})

if((1 < 20).and(1.8.isInteger), {"very true"}, {"hmmm..."})

10.do({arg count; [count, if(count.odd, {"odd"}, {"even"})].postln})

(
84.do({arg count; if([0, 4, 7].includes(count%12), 
	{count.post; " is part of a C triad.".postln}, 
	{count.post; " is not part of a C triad".postln})})
)

50.do({if(1.0.rand.round(0.01).post > 0.5,  {" > 0.5".postln}, {" < 0.5".postln})})

50.do({if(1.0.rand > 0.5,  {"play a note".postln}, {"rest".postln})})

50.do({if(0.5.coin, {"play a note".postln}, {"rest".postln})}) // same as above

if((10.odd).or(10 < 20), {"true".postln}, {"false".postln})

Arrays

scramble reverse find nth

Amplitude Modulation

Frequency Modulation

Additive Synthesis

Subtractive Synthesis