20

Click here to load reader

01 Programming

Embed Size (px)

Citation preview

Page 1: 01 Programming

8/19/2019 01 Programming

http://slidepdf.com/reader/full/01-programming 1/20

SC Programming Hints

SuperCollider is a full programming language,and its helpful to see how to code somestandard programming operations

Page 2: 01 Programming

8/19/2019 01 Programming

http://slidepdf.com/reader/full/01-programming 2/20

Syntax for SuperCollider as aProgramming Language

SuperCollider is an object-oriented languagebased on Smalltalk !t has standard facilities of

computer languages which ! will outline for youhere

Comments

""this is a comment

"# this is also a comment #"

Page 3: 01 Programming

8/19/2019 01 Programming

http://slidepdf.com/reader/full/01-programming 3/20

$rackets

%e usually group related bits of code together,and try to keep things neat and tidy &our wholepatch of code will typically be put within curlybrackets

'""my patch goes in here(

but we also use brackets for other reasons-

' ( ""for grouping expressions together orpassing arguments to methods, ieSin)scar'**+(

""function ie arg a. a#a

/ 0 ""array 'list of data( ie /1,2,3,*0

Page 4: 01 Programming

8/19/2019 01 Programming

http://slidepdf.com/reader/full/01-programming 4/20

SuperCollider code gets full of nestedexpressions- you look at the selections withinbrackets to check on the scope 'le4el of

nesting( of code fragments

!f you don5t keep an eye on the tidiness of yourcode you5ll get 6uickly confused about whatgoes with what

 7ry double clicking around any of the bracketsin the following code8

'""9rst block of code

'""9rst nested block starts

'

Page 5: 01 Programming

8/19/2019 01 Programming

http://slidepdf.com/reader/full/01-programming 5/20

""second nested block(

(

(

:ariables and ;ssignment

!n programming, we need to store our datasomewhere, e4en if it5s just data telling us the

initial conditions for our calculations <ormaking music, because our programs run o4ertime, we need to store information to keeptrack of the current state of a=airs

Page 6: 01 Programming

8/19/2019 01 Programming

http://slidepdf.com/reader/full/01-programming 6/20

'4ar a,b,c. ""4ariables always getde9ned 9rst in a block of code

a>1. ""assign some 4alues to our4ariablesb>a.c> b#?.

/a,b,c0postln.

a>*. ""assign some new 4alues to our4ariablesb>[email protected]> c#?. ""this is )B because c has a4alue already by now

/a,b,c0postln.(

Page 7: 01 Programming

8/19/2019 01 Programming

http://slidepdf.com/reader/full/01-programming 7/20

 7his is 9ne8

'4ar da4e.

da4e> ?.

da4epostln.(

 7his is wrong and SuperCollider will complain8

'da4e>?.da4epostln.(

Page 8: 01 Programming

8/19/2019 01 Programming

http://slidepdf.com/reader/full/01-programming 8/20

ncapsulation

;nother standard facility of programming

languages is the ability to de9ne your ownfunctions, aprocess called encapsulation

%itness8

'

4ar myfunction.

myfunction> arg a. a#a. ""de9ne myown s6uaring function

Page 9: 01 Programming

8/19/2019 01 Programming

http://slidepdf.com/reader/full/01-programming 9/20

myfunction4alue'D(postln. ""run myfunction with an input of D and post the result(

 7his is useful because it stops us repeatingsimilar code all o4er the placeE

Spot the di=erence in tidiness of code 'don5tworry about the 9ne detail of ;rrayconstruction for now(

'4ar scale, current. ""code for making arandom scale

Page 10: 01 Programming

8/19/2019 01 Programming

http://slidepdf.com/reader/full/01-programming 10/20

current>F+.scale> ;rray9ll'D, 4ar old. old>current.current>current@rrand'1,3(. old(. ""construct

for making an ;rray to a recipe- explainedmore in the next partscalepostln.

current>F+.scale> ;rray9ll'D, 4ar old. old>current.current>current@rrand'1,3(. old(.scalepostln.

current>F+.scale> ;rray9ll'D, 4ar old. old>current.current>current@rrand'1,3(. old(.scalepostln.(

'4ar makescale.

Page 11: 01 Programming

8/19/2019 01 Programming

http://slidepdf.com/reader/full/01-programming 11/20

makescale> 4ar current.current>F+.;rray9ll'D, 4ar old. old>current.

current>current@rrand'1,3(. old.(..

3do'makescale4aluepostln.(.(

/<unctions0""select the text within brackets then press cmd@H to explore some moreabout control Gow

Looping

Page 12: 01 Programming

8/19/2019 01 Programming

http://slidepdf.com/reader/full/01-programming 12/20

 7he good thing about computers is that theyfollow instructions in a way that would be 4erymonotonous for a humanE 7hey can do tasks

4ery rapidly o4er and o4er because they arecapable of looping

'2+do'o4er and o4erpostln.( ""the functionhere is the thing to call each time round theloop(

'4ar n.

n>+.

while'nI?, n>n@1. keep goingpostln.((

Page 13: 01 Programming

8/19/2019 01 Programming

http://slidepdf.com/reader/full/01-programming 13/20

'for'1, ?, arg i. ipostln. (. ""start at 1, step by1 each time until you reach ?(

'for$y'1, ?, 2, arg i. ipostln. (. ""start at 1,step by 2 each time until you reach ?(

 &ou ha4e to be careful that you don5t set thecomputer to looping fore4er, which will crashyour computer

Page 14: 01 Programming

8/19/2019 01 Programming

http://slidepdf.com/reader/full/01-programming 14/20

Conditional xecution

the control Gow in SuperCollider is straightdown the page8

'me 9rstEpostln.now meEpostln.

don5t forget meEpostln.(

Sometimes we want things to happen basedupon whether something else has occured 7oallow this,programming languages ha4e conditional

execution- if some logical condition is met,dosomething

'

Page 15: 01 Programming

8/19/2019 01 Programming

http://slidepdf.com/reader/full/01-programming 15/20

me 9rstEpostln.now meEpostln.if'+?coin, don5t forget meEpostln.,you

forgot meEpostln(.""roll a dice- heads we do one thing, tails thesecond(

an if statement is used like this-

if 'logical test which returns true or false, dothis if true, do this if false(

Jun these lines one at a time8

if'true, trueE, falseE(postln.

if'false, trueE, falseE(postln.

Page 16: 01 Programming

8/19/2019 01 Programming

http://slidepdf.com/reader/full/01-programming 16/20

'4ar a,b.

a>?.

b> if'a>>F, 11, K(.

bpostln.(

[Control-Structures] ""select the text within brackets then press cmd@d to

explore some more about control Gow

Page 17: 01 Programming

8/19/2019 01 Programming

http://slidepdf.com/reader/full/01-programming 17/20

Classes

<inally, in SuperCollider you ha4e the possibilityof writing your own functions as classes withinthe language- this means they are alwaysa4ailable to you in your code Such writing ofclasses is really helpful once you get to thestage of otherwise ha4ing 4ery big longconfusing 9les &ou can break up your codeo4er many 9les using classes

Classes are also a good conceptual tool forencapsulating functions and the data that

function works with 7his is part of good objectoriented programming

%e5ll return to this later in the course, but !5llpoint out that you54e seen and used manyclasses already8

Sin)scSC%indowouseM

Page 18: 01 Programming

8/19/2019 01 Programming

http://slidepdf.com/reader/full/01-programming 18/20

; note on object oriented programming inSuperCollider 'you may want to come back tothis later(

 7ext beginning with a capital letter is a classname

a dot then a function is a method of the object

Page 19: 01 Programming

8/19/2019 01 Programming

http://slidepdf.com/reader/full/01-programming 19/20

L<Sawar ""ar is the method

select the method and hit Cmd@& to see any

classes which respond to that method

 &ou can see deri4ed classes of a superclass byusing the dumpClassSubtree method

NOendumpClassSubtree

4erything deri4es from the )bject class, so do)bjectdumpClassSubtree to see the wholehierachy

 &ou can add your own extensions to

SuperCollider by writing your own classes

%hen you want to add new classes, you addthem as code in sc 9les to the SCClassLibraryfolder 'we5ll do this much later in the course(

 7hen you recompile the class library using

Cmd@B 8 if there are syntax errors in yourcode, the library won5t compile, but hopefullyyou5ll see error messages that will help youidentify the problem

Page 20: 01 Programming

8/19/2019 01 Programming

http://slidepdf.com/reader/full/01-programming 20/20