Making Music With Sonic Pi

Embed Size (px)

Citation preview

  • 8/18/2019 Making Music With Sonic Pi

    1/61

  • 8/18/2019 Making Music With Sonic Pi

    2/61

    1 Content  

    2 Introduction.......................................................................................................................................4

    3 Te First !on".....................................................................................................................................#

    4 Te Gra$ical %ser Inter&ace.............................................................................................................. 6

    # 'usic Teory ( )ery sim$le...............................................................................................................*

    #.1 +itc............................................................................................................................................*

    #.2 ,en"t o& te Tone...................................................................................................................... -

    #.3 'II..........................................................................................................................................10

    6 Te !ynta/ o& !onic +i...................................................................................................................... 11

    6.1 odin" Ty$es.............................................................................................................................11

    6.1.1 omments..........................................................................................................................11

    6.1.2 ommands.........................................................................................................................13

    6.1.3 +arameters.........................................................................................................................14

    A sim$le 'elody it di&&erent !ounds...........................................................................................14

    * Te ode Editor............................................................................................................................... 1*

    *.1 ,ine umers............................................................................................................................1*

    *.2 a)i"ation.................................................................................................................................1-

    *.3 5i"t 'ouse Button 'B57.......................................................................................................1-

    *.4 o$y and +aste..........................................................................................................................21

    - Error Handlin".................................................................................................................................. 22

    10 5andom ontrol............................................................................................................................. 23

    10.1 5andom umers...................................................................................................................23

    10.2 !election.................................................................................................................................2#

    10.3 5andom !on"..........................................................................................................................26

    11 +arameter8 !ynt and !am$le........................................................................................................2*

    11.1 En)elo$es................................................................................................................................2-

    12 +ro"ram !tructures........................................................................................................................30

    12.1 Treads...................................................................................................................................30

    12.2 ue 9!ync8 Gi)e Inserts...........................................................................................................33

    12.3 ,oo$s.......................................................................................................................................34

  • 8/18/2019 Making Music With Sonic Pi

    3/61

    12.4 :it onstruct8 Eco and oter E&&ects..................................................................................3#

    12.4.1 istortion.........................................................................................................................3

    12.4.2 Eco.................................................................................................................................3*

    12.4.3 5e)er..............................................................................................................................3*

    13 ;ariales and Aritmetic

  • 8/18/2019 Making Music With Sonic Pi

    4/61

    2 Introduction

    'usic is one o& te truly "reat acie)ements o& mankind. And tere is no man on eart o does not

    like music. !o&tare and com$uters are a recent acie)ement and many $rocesses and industries are

    no reliant. !onic +i &orms $ike eteen music and so&tare.

    E)ery cild can learn te asics o& $ro"rammin" it !onic +i. It is a lot o& &un doin" tis ile

    learnin" a lot aout music.

    Tis ook descries te &undamentals o& !onic +i e/$lained in suc a ay tat a cild can understand

    it. Te ook is suitale as an accom$animent to teacin" as ell as &or sel&=learnin".

    Ha)e &un

  • 8/18/2019 Making Music With Sonic Pi

    5/61

    3 The First Song

    +ro"rammers kno te >Hello :orld> as te )ery &irst instance? i& tey learn a $ro"rammin"

    lan"ua"e. Hoe)er? e make music ere? and are tere&ore $layin" te &irst son" see Fi"ure 17.

    Figure 1. The Graphical User Interface of Sonic Pi with a simple example

    Te son" consists o& 3 se$arate tones. To $er&orm tem? you can sim$ly co$y te &ramed lines elo

    in a indo o& !onic +i and $ress te @5un utton. %ses &or te est te normal co$y and $aste

    &unctions? or e)en easier? ut someat more ork8 Ty$in" te code in one o& te !onic +i code

    indos.

    Te First !on"

    $lay 8c4

    slee$ 1

    $lay 8e4

    slee$ 1

    $lay 8"4

    Code 1. The ver first example

  • 8/18/2019 Making Music With Sonic Pi

    6/61

    And ten ear 3 tones in succession. E)en i& you can easily understand at a$$ens ere? I ill

    e/$lain it rie&ly? line y line.

    !r. Sonic Pi command "xplanation1=1 Te First !on" omments are marked it a @. E)ery te/t ri"t on te

    line? !onic +i ill not $lay. Tus? te &irst line is a comment.

    1=2 $lay 8c4 !onic +i $lays te note c4. o not &or"et te colon.

    1=3 slee$ 1 For a second? no ne note is $layed.

    1=4 $lay 8e4 !onic +i $lays te note e4.Ta#le 1$ Simple commands for the first song in Sonic Pi

    !onic +i orks te commands &rom te to$ donards. Te comment on te &irst line is i"nored.

    :at sound on te $iano keyoard is

    c4C

    Tis is e/$lained in detail in a$ter #. Here only tis8 is te &irst note o& te maDor scale. And te

    4 re$resents te 4t octa)e. e4 is ten 2 ole tones i"er? and "4 a"ain 2 tones i"er. >!lee$ 1>

    re$resents a one=second $ause.

    E)ery time $usin" te @5un utton? you ill ear te 3 ee$s. Is it $ossile to can"e te $itcC

    es? surely. I& I re$lace te 4 y a #? te ole little tune starts a &ull octa)e i"er. I& I re$lace te >c4>

    y a >d4>? te &irst done ill e eard as a .

    4 The Graphical User Interface

    Te "ra$ical user inter&ace G%I7 makes te introduction into !onic +i es$ecially sim$le Fi"ure 27.

    Its all to"eter? at is needed &or a uick start. Te music can e started? sto$$ed? sa)ed and

    recorded y $usin" te a$$ro$riate uttons.

  • 8/18/2019 Making Music With Sonic Pi

    7/61

    !r. Sonic Pi GUI

    element

    "xplanation

    'usic ontrol

    2=1 5un By clickin" tis utton? te code in te current orks$ace ill e $layed=

    e)en i& yet anoter son" in tis or any oter orks$ace runs.

    2=2 !to$ All current son"s sto$ it a click.

    2=3 !a)e Te code o& a son" can e stored outside o& !onic +i. Ty$ically? te code

    &or eac orks$ace is automatically sa)ed en you close te $ro"ram

    and is te ne/t o$enin" a"ain a)ailale. Tats y tere are asically 2

    reasons to sa)e te code e/ternally8 I a)e all e/istin" ork s$aces

    occu$ied and need an em$ty one. !o I delete te code and a)e s$ace

    &or a ne son".

    2=4 5ec !tarts recordin" and te associated "eneration o& a >a)> &ile. Anoter

    click sto$s te recordin". 5ecorded is at can e eard eteen te

    to clicks. In case no son" is $layed ack? te recordin" is em$ty.

    :indo

    # ode Editor :rite your son" in te code editor. In &act? it is e)en te only $lace to

    enter sometin" in !onic +i.

    6 ,o" ;ieer !os in cronolo"ical order? ic as een $layed.

    Hel$ Here you can searc &or el$. Te temes are undled? and te sim$ler

    tin"s come &irst.

    * Hel$ ;ieer !os at is selected in te Hel$ selection.Ta#le %. The elements of the Sonic Pi interface.

    Figure %$ GUI elements of Sonic Pi

    Y,.

    ode Editor ,o" ;ieer

    Hel$ ;ieerHel$ !election

    :orks$ace Tas

    'usic ontrol +re&erences

  • 8/18/2019 Making Music With Sonic Pi

    8/61

    ,earned &or $ro"rammin"

    r. Topic

    1 :ene)er $ossile use a $ro"rammin" en)ironment &or codin"? called IE

    Inte"rated &e)elo$ment "n)ironment7. To ad)anta"es you already kno8 1.

    di&&erent $arts o& your code are dis$layed in di&&erent colors. 2. And o&ten a tooltat el$s you in te com$letion o& commands is inte"rated. Additional ene&its o&

    an IE ill e descried &urter elo.

    2 omments are im$ortant. Eac son" eac $ro"ram7 sould e $ro)ided at te

    e"innin" it a comment at least? containin" te name etc..Ta#le '$ (earned for programming ) part 1.

    5 Music Theory !ery si"ple

    'usic teory is $era$s already said too muc at tis $oint. :e are talkin" aout te $itc and

    tone len"t.

    5#1 $itch

    Te $itc is s$eci&ied as te &irst $arameter o& te >$lay> metod. Te >$lay> metod it te

    $arameter c4 $lays a c o& te &ourt octa)e? see Fi"ure 3.

    Figure '. The *pla+)method with the parameter *c,+

    Te $resentation o& te notes on te $iano keyoard is son in Fi"ure 4. Tere are natural tones

    te ite keys7 and # al& te lack keys7. Te =maDor scale starts it a . Tis is &olloed y ? E?

    F? G? A and B. :en startin" it te tone A? it is uite easy to memoriJe te notes8 A? B? ? ? E? F? G?so Dust te &irst letters o& te al$aet.

  • 8/18/2019 Making Music With Sonic Pi

    9/61

    :it te sounds o& lack uttons is a it more com$licated. ,et us look &or te tone &or e/am$le. It

    as a lack key to te ri"t and to te le&t. Te lack key ri"t &rom te $lays a semitone i"er?

    called =sar$ in te &i"ure7. Te lack key le&t &rom te $lays a semitone loer? called =&lat in te &i"ure7.

    • Te increase o& a al& tone is called >sar$> and is are)iated as >s> in !onic +i.

    • Te reduction to a al& tone is called >&lat> and is are)iated it >&> in !onic +i.

    !r. -odification "nglish Sonic Pi Sonic Pi

    example

    1 +itc increase sar$ s cs4

    2 +itc decrease &lat & eTa#le ,. -odification of the pitch

    5#2 %ength of the Tone

    Te tone len"t is controlled in !onic +i it a ole set o& $arameters o& te >$lay> metod. Ho

    lon" te note is eld? is set )ia te >5elease> $arameter. For e/am$le? it >+lay8 c#? 5elease8 1.3>? a

    note is ke$t &or 1.3 seconds. Fi"ure # sos an e/am$le. First? te release $arameter is set to te

    )alue 1? tat is 1 second? and terea&ter to te )alue 2.

    Figure . Set the length of a tone using a *release+ parameter

    Figure ,. /e#oard with natural and half tone 0es

    E F G A B E

    E

    F

    G

    G

    A

    A

    B

    E

  • 8/18/2019 Making Music With Sonic Pi

    10/61

    Ho is te relation o& tese $arameters to te usual tone len"ts like a uarter or a al& noteC Is

    tere any $ro)ision? o to con)ert one systems into te oterC Te anser is un&ortunately no? and

    te reason &or tis is tat !onic +i uses an asolute system? ile te con)entional music descri$tion

    o$erates it a relati)e system. Tis means tat a uarter note as not alays te same len"t inseconds. 5ater? it is so tat te com$oser sets te s$eed o& te $iece or $art o& a $iece? and ten

    "i)e te note len"ts. But e)en tis de&inition is not asolute and te $er&ormer or conductor as a

    certain clearance.

    But &or a s$eci&ic? sin"le $iece o& music? an assi"nment can e made )ery ell. c#>. In !onic +i tere is indeed

    anoter ay. :e can rite instead a sin"le numer? as son in Fi"ure 6.

    Figure . Pitch as conventional 2c3 and -I&I 24%3 description

    In &act? >c#> corres$onds to te numer 2. ou ill ear e/actly te same sound.

    ou can try it sim$ly y co$yin" ode 2 in te !onic +i ode Editor and ten run it.

    'II

    $lay 8c#

    slee$ 0.#

    $lay 2

    Code %. -idi example.

    Te same sound ould e eard tice. >8c#> and >2> tus mean te same tin". Hoe)er? tere is a

    little di&&erence. Te normal tone reuires a colon a&ter te >$lay>? te 'II numer does not. As a

    numer o& tem? incidentally? son in lue? ile te note turns red. Altou" it is not so im$ortant

    &or te o$eration o& !onic +i8 'II stands &or >'usical Instrument i"ital Inter&ace> i.e. >di"ital

    inter&ace &or musical instruments>.

  • 8/18/2019 Making Music With Sonic Pi

    11/61

    :en do I use no ic kind o& descri$tionC Actually? tat does not matter.

  • 8/18/2019 Making Music With Sonic Pi

    12/61

    Figure 4. The comment in the first line starts with the hash

    omments are a ser)ice &or oters and &or te coder. 'y use&ul tou"ts aout a $iece can e a

    comment. I& I ant any in&ormation to $ass on? ten I use te comments. At least I can rite te son"

    title? my name and te date o& ori"in as a comment in te &irst lines. In $rinci$le? oe)er? comments

    are $ossile at any $oint. In te &olloin" e/am$le tere are 3 di&&erent ty$es o& comments8

    1. A comment lock it "eneral in&ormation in te &irst lines? ic is additionally limited y a startand &inis line? see Fi"ure .

    2. A sin"le comment line? indicatin" te e"innin" o& te &irst )oice

    3. A comment eind a $iece o& code descriin" e/actly tis.

    GB E/am$le 1 Bac tc

    Hans Gruendel

    1*.0.201#

    ;oice 1

    id/1 L 0

    inMtread do

      4.times do

      useMsynt 8&m

      $lay 84? release8 1? am$81 9 id/1 Tat is te &irst sound

      slee$ 2

      endCode '. Comments

    that should #e done that should #e left

    :ritin" comments is asically "ood :rite

    comments in En"lis as $ossile

    :ritin" comments in En"lis ene)er $ossile :rite comments in German

    E/$lain music and code it comment

  • 8/18/2019 Making Music With Sonic Pi

    13/61

    '#1#2 Co""ands

    ommands a$$ear in !onic +i in lack color. In Fi"ure *? to commands can e seen8 >$lay> and

    >slee$>.

    Figure ;. Two commands in Sonic Pi$

  • 8/18/2019 Making Music With Sonic Pi

    14/61

    '#1#3 $ara"eters

    In !onic +i )alues can e "i)en to commands. Tis is understandale look at te command >slee$>. At

    least? you must de&ine o lon" sould te slee$ e. !onic +i inter$rets te )alue in seconds. >!lee$

    2> tere&ore means tat ere 2 seconds notin" sould a$$en. A &ractions o& seconds can e

    s$eci&ied as $ause len"ts? &or e/am$le? 2.# means a $ause o& 2 N seconds. Te numers as

    $arameters are son in lue.

    In addition to te numers as $arameters? tere are also strin"s as $arameters $ossile. As an

    e/am$le? ere are te $itc o& te >$lay> command. >+lay8 c4> $lays te sound c te 4t octa)e.

    Tese strin"s tat are not numers? are dis$layed in red. Fi"ure 10 sos te use o& commands.

    Figure 1>. Parameters are given to the methods

  • 8/18/2019 Making Music With Sonic Pi

    15/61

    !on" 2

    Hans Gruendel 1*.0*.201#

    +art 1

    $lay 8"#? release8 1

    slee$ 1

    $lay 8a#? release8 1

    slee$ 1

    $lay 8"#? release8 0.#slee$ 0.#

    $lay 8? release8 0.2#

    slee$ 0.2#

    $lay 8e#? release8 0.#

    slee$ 0.#

    $lay 8? release8 0.#

    slee$ 0.#

    +art 2

    $lay 8"#? release8 1

    slee$ 1$lay 8a#? release8 1

    slee$ 1

    $lay 8"#? release8 0.#

    slee$ 0.#

    $lay 8? release8 0.2#

    slee$ 0.2#

    $lay 8e#? release8 0.#

    slee$ 0.#

    $lay 8? release8 0.#

    slee$ 0.#

    +art 3

    $lay 8"#? release8 1

    slee$ 1

    $lay 8"#? release8 1

    slee$ 1

    $lay 8a#? release8 1

    slee$ 1

    $lay 8#? release8 0.#

    slee$ 0.#

    $lay 8c6? release8 0.#

    slee$ 0.#

  • 8/18/2019 Making Music With Sonic Pi

    16/61

    $lay 8#? release8 1

    slee$ 1

    $lay 8a#? release8 1

    slee$ 1

    $lay 8"#? release8 2

    slee$ 2

    Code ,. Song %

    But at some $oint e)en te most eauti&ul ee$ as lost its a$$eal. Time to can"e te sound. In

    !onic +i tat orks it te >useMsynt> command. synts>? see Fi"ure 12.

    Figure 1%. 7ll snths from Sonic Pi

    Incidentally? te set synt is )alid until te ne/t useMsynt command is e/ecuted. I& no more synt

    commands are in tis son"? ten tis synt is used until te end o& te son".

    And at a$$ens en you ould not use any >useMsynt> commandC Ouite sim$ly. Ten te

    $reset synt >ee$> ill e used.

    I& you tried some synts ten you ill certainly a)e noticed tat te &irst 6 notes are $layed tice.

    Ho te code looks no? te lines 4=1# and 1*=2- are identical. Tis doule o& course te e&&ort en

  • 8/18/2019 Making Music With Sonic Pi

    17/61

    enterin" te code. It ould e nice i& you could say !onic +i tat some lines are to e $layed 2 or

    more times. And indeed tere is suc a command8 >/. times do ... end >. In Fi"ure 13? te usa"e can

    e seen? ic makes it $ossile to out$ut te sound "# and te one=second $ause tice.

    Figure 1'. The repeat loop in Sonic Pi

    Te loo$ command orks like tis8

    1. Aead te $art to loo$ I rite >n.times do>? it >n> indicatin" te numer o& re$etitions.

    2. Beind te $art to loo$ I rite >end>.

    3. Te intermediate lines I indent. For tis I use 2 s$aces or eually te >Ta> key.

    I can summariJe tere&ore +art 1 and +art 2? see ode #. Te 2 $oints at te e"innin" o& eac line

    itin te re$eat loo$ indicates te indentation.

    !on" 2

    Hans Gruendel 1*.0*.201#

    useMsynt 8ee$

    +art 1P2

    2.times do

      $lay 8"#? release8 1

      slee$ 1

      $lay 8a#? release8 1

      slee$ 1

  • 8/18/2019 Making Music With Sonic Pi

    18/61

      $lay 8"#? release8 0.#

      slee$ 0.#

      $lay 8? release8 0.2#

      slee$ 0.2#

      $lay 8e#? release8 0.#

      slee$ 0.#  $lay 8? release8 0.#

      slee$ 0.#

    end

    Code . Part 1 and % of Song % com#ined in a loop

    ,earned &or $ro"rammin"

    o Teme

    1 ode tat re$eats itsel& is to a)oid. Tis $rinci$le is later addressed se)eral times?

    ecause it is really im$ortant. E)ery $ro"rammin" lan"ua"e as commands tat

    allo Dust tat.

    2 Indentin" is im$ortant to im$ro)e readaility. And !onic +i su$$orts us also. !im$ly

    ty$e te A,T and ' key to"eter? and already e)erytin" is automatically indented

    in tis indo. Tere are e)en $ro"rammin" lan"ua"es tat are committed to te

    ri"t indentation? oterise te $ro"ram is not runnin".Ta#le 4$ (earned for programming ) %

    that should #e done that should #e leftE)ery no and ten? you sould indent te code

    automatically. Te A,T 9 ' sould e used

    &reuently. ou can easily detect eter te

    son" is structured $ro$erly.

    It is a aste o& time to indent te code y and.

    , The Code -ditor

    ,#1 %ine .u"/ers

    !onic +i indicates te line numers. Tats "ood &or orientation in codin". But e)en more im$ortant

    are te line numers i& an error messa"e a$$ears. Because eac messa"e also includes te line

    numer? so you kno in ic line te error occurs? see a$ter -.

  • 8/18/2019 Making Music With Sonic Pi

    19/61

    ,#2 .a!igation

    As in any te/t editor? tere is a cursor tat tells you ere you can make can"es in te code.

    In te code indo? you can na)i"ate it te arro keys o& your keyoard. ,ar"er Dum$s can

    $er&ormed it te screen u$ and As in any te/t editor? tere is a cursor o& tells you ere you can

     Dust make can"es in te code

    In te code indo? you can na)i"ate it te arro keys o& your keyoard. ,ar"er Dum$s you can

    $er&orm it te $a"e u$ and $a"e don keys? or usin" te mouse and scroll ars see Fi"ure 147.

    Figure 1,. Code "ditor with scroll #ars

    ,#3 0ight Mouse utton M0

    :it te ri"t mouse utton one can o&ten so commands tat &it te current situation. I& you click

    it te ri"t mouse utton in te code editor? ten a menu it te entries and sortcuts ill

    a$$ear see Fi"ure 1#7.

  • 8/18/2019 Making Music With Sonic Pi

    20/61

    Figure 1. "xtra -9@ commands

    !r. Command /e#oard

    Shortcuts

    -eaning 7pplication

    1 %ndo Alt 9Q %ndo te last

    command

    Ima"ine tat you)e

    can"ed sometin" in

    te code and ten

    realiJe tat it as not a

    "ood idea. ou could

    no make te can"e

    manually undone. 'uc

    easier and sa&er to use

    te >%ndo> command.

    ou can do te

    command multi$le times

    consecuti)ely.

    2 5edo Alt 9@!i&t9 Q 'akin" te undo

    command yet

    ou)e Dust undo a ole

    series o& commands y

    $ressin" Alt 9 Q? ten

    realiJe? oe)er? tat it

    as one too many.

    Instead o& reconstructin"

    te $ast no tedious?

    you can restore it it

    redo.

    3 ut Alt 9 R ut out te marked

    area

    Tese 3 commands

    elon" to"eter and

    indeed $airs8 ut and

    $aste or co$y and $aste.

    For e/am$le? i& you a)e

    a "ood drum track in a

    4 o$y Alt 9 o$y te marked

    area

    # +aste Alt 9; Insert te cut or

    co$ied ran"e at te

  • 8/18/2019 Making Music With Sonic Pi

    21/61

    $oint ere te

    cursor is

    son" and ant to use it

    in anoter? ten you

    co$ied &rom one editor

    ta and inserted in

    anoter.

    6 elete elete te markedarea

    ou can also sim$ly $resste >el> key on te

    keyoard.

    !elect All Alt 9 A !elect all on tis

    ta

    I& you e)er is to

    cancel all or co$y all?

    ten you can mark

    e)erytin" e&oreandTa#le ;. -@9 commands

    Te so called? conte/t menu o$ens en you $us te ri"t mouse utton. Te commands in tis

    menu are customiJed to te situation. :ic is "ood? ecause you a)e a "ood o)er)ie o&

    commands a)ailale and usale in tis moment.

    that should #e done that should #e left

    Be&ore lon" searcin" in te main menu? Dust

    look in te conte/t menu. Tere you ill &ind te

    currently most im$ortant commands.

    Try to memoriJe te commands tat are idden

    eind te 'B5.

    ,#4 Copy and $aste

    !ometin" to co$y and $aste to anoter location orks in many $ro"rams. Tis can e an ima"e or a

    te/t or a tale. And it also orks it te code in te editor. Sust markin" te accordin" code it te

    keyoard? and ten cut K co$y and $aste tem at oter locations.

    As e/$lained ao)e? !onic +i su$$orts te ole tin" it keyoard sortcuts. +era$s you)e

    noticed tat te >Alt> key is used in !onic +i. And maye youre used to it &rom many oter $ro"rams

    to use te >trl> key. Te trl key orks in !onic +i actually.

    And it "ets e)en etter. ou can also insert te code in oter $ro"rams? &or e/am$le? a di&&erent editor

    or in an email. Because code are alays ordinary caracters? so letters and numers. Tis orks it

    te code o& oter $ro"rammin" lan"ua"es? too. Tat is use&ul &or many acti)ities8

    1. ou)e ritten sometin" in te code editor and ant it to mo)e to anoter location? ecause you

    ant to can"e te order o& your code.

    2. ou ant to sa)e your code outside o& !onic +i. Tus you can co$y it into anoter $ro"ram.

    3. ou ant to send to someone your code as email. Tus you can Dust co$y it in an o$en e=mail.

  • 8/18/2019 Making Music With Sonic Pi

    22/61

    -rror andling

    +ro"rammin" lan"ua"es are )ery accurate. Te use and te s$ellin" o& all instructions must e strictly

    adered to. Tis is also in !onic +i.

    !onic +i cecked only durin" $layack o& te son" i& it contains errors. I& tis is te case? ten sto$s

    !onic +i at te &irst occurrin" error and dis$lays an error messa"e. Te error messa"e includes an

    indication o& te numer o& ork s$ace and te line. Ten tere is a sort concise descri$tion o& te

    &ault. And e)en etter8 !onic $i identi&ies te ro at te line e"innin" it a small arro. Fi"ure 16

    sos an e/am$le.

    Te metod

    scale

    e/$ected in te &olloin" rackets 2 ar"uments? namely &irst te key and ten te ty$e o& scale. Te

    $ossile ty$es are $rede&ined in !onic +i. >de> is not a ty$e? and accordin"ly te error messa"e

    occurs8

    5untime Error8 orks$ace 0? line 10U %nknon scale name8 de

    :at error messa"e is "i)en? i& only one ar"ument is $assedC Tats easy8

    @untime "rror$ Awor0space >B line 1> Drong num#er of arguments 21 for %3

    Figure 1. Incorrect code in line 1>.

  • 8/18/2019 Making Music With Sonic Pi

    23/61

  • 8/18/2019 Making Music With Sonic Pi

    24/61

    In &act? tats not a real coincidence. Furtermore? !onic +i $lay e)ery time te same note? i& e $ress

    >5un>. Tis ea)ior is uite ty$ical &or $ro"rammin" lan"ua"es. Te numers created de$ends on a

    start )alue. To can"e te )alue? e a)e to set tis start )alue y usin" te

    useErandomEseed n 

    metod? erein n is an aritrary natural numer.

    5andom control 2

    Hans Gruendel

    1*.0*.201#

    useMrandomMseed 20$lay rrand#0? 07

    Code 4. -ethod rrand for generating random num#ers with seed in advance.

    E)ery time I no can"e te )alue o& n? ten te tone can"es. Incidentally? te tone remains

    eteen #0 and 0? ecause tese 2 numers are "i)en en te metod en callin". Tey are te

    loer and u$$er limit. Tale - lists te random metods on !onic +i.

    !r. -ethod -eaning "xample

    1   use_random_seed n InitialiJes te random

    numer "enerator. From

    initialiJation de$ends te

    )alue tat is "enerated y

    te &olloin" random

    metods.

    use_random_seed 5

    2 rrandr1? r27 +roduces randomly a )alue

    eteen 51 and 52?

    e/clusi)e tose to limits.

    In te e/am$le to te ri"t?

    0.2 or 0.# is ne)er returned

    slee$ rrand0.2? 0.#7

    3 rand r2 reates a random numer

    eteen 0 and 52? or i& no

    $arameter is used only

    eteen 0 and 1

    slee$ rand

    4 rrandMin1? n27 :orks like rrand? oe)er?

    te random numer is

    alays an inte"er

    $lay rrandMi40? #07

    # randMin1? n27 :orks like rand? oe)er?

    te random numer is

    alays an inte"er

    $lay randMi#07

    Ta#le =. -ethods for generating random num#ers

  • 8/18/2019 Making Music With Sonic Pi

    25/61

    16#2 Selection

    Te com$letely &ree "eneration o& random numers is one tin". !li"tly restricti)e o$erates te

    @coose metod. @coose randomly cooses &rom a list o& a )alue. Te code &or tis is son in

    ode *

    Qu&allssteuerun" 2

    Hans Gruendel

    1*.0*.201#

    $lay coose60? 61? 62U7

    Code ;. -ethod choose for the random selection

    %sin" te already knon @times metod te ea)ior o& @coose can e etter understood. In te

    lo" )ieer out$ut is son. @coose &irst selects te )alue 60? ten &ollo 61? 60? 61? 61? 62? 60? 62?

    60? 60.

    Figure 14. The choose method selects from the values >B 1B and 1

  • 8/18/2019 Making Music With Sonic Pi

    26/61

    16#3 0ando" Song

    >ormal> son"s include not really random elements and tere&ore o&ten sound uite static = suc as

    canned. :e kno small random de)iations &rom li)e $er&ormances o& ands or indi)idual musicians.

    E)eryone usin" !onic +i is aare o& te static caracter o& tis son"s.

    5andom elements are introduced to a)oid tis.

  • 8/18/2019 Making Music With Sonic Pi

    27/61

      end

    end

    de&ine 8armonics do Vnum? tonics? coL*0? resL0.1? am$L1V  useMsynt 8&m

      num.times do

      $lay cordtonics.coose? 8m117.coose? res8 res? cuto&&8 rrandco = 40? co 9 207? am$8 0.# W am$?

    attack8 0? release8 rrand0.#? 1.#7? $an8 rrand=0.? 0.7

      slee$ 0.2#? 0.#? 0.#? 0.#? 1? 1U.coose

      end

    end

    inMtreadname8 8loo$er17 do  loo$ do

      armonics6? 8&2? 8&3? 8&1? 8e2? 8e4? 8e3U7

      end

    end

    inMtreadname8 8loo$er7 do

      loo$ do

      myMloo$  end

    end

    inMtreadname8 8loo$er27 do

      loo$ do

      coir

      end

    end

    de&ine 8sound1 do VnumV

      useMsynt 8&m  $lay num

      useMsynt 8$rettyMell

      $lay num

      useMsynt 8sine

      $lay num

      useMsynt 8tri

      $lay num

    end

  • 8/18/2019 Making Music With Sonic Pi

    28/61

    inMtreadname8 8loo$er17 do

      loo$ do

      sound18c27

      slee$ 1

      sound18"27  slee$ 1

      sound18&27

      slee$ 1

      sound18c27

      slee$ 1

      end

    end

    Code =. @andom song

    I& you ant to &ind out ere random metods are used in tis son"? ten searc &or te strin"

    >rand>.

    11 $ara"eter7 Synth and Sa"ple

    +arameters s$eci&y o a metod ould do it task. Tat sometin" needs to e done ill e

    controlled y te metod call itsel&? ere its all aout te o. Te code 10 is a sim$le e/am$le o&

    tis. ,ets look at te @$lay metod.

    +arameter

    Hans Gruendel

    1*.0*.201#

    useMsynt8darkMamience

    $lay8"1? am$86? release82

    Code 1>. Parameter of the pla method

    In te code? a colon a&ter te ord >$lay> is &olloed immediately &rom te tone? ere "1. Tis is

    &olloed y &urter $arameters? ic are se$arated y a comma. Te $arameter @am$ is set to te

    )alue o& 6? te $arameter @release is set to te )alue o& 2.

    Te metod @$lay e/$ect &irst o& all te )alue o& te $itc. Ten te &olloin" $arameters are not

    tied to a $articular order. !onic +i detects te $arameters y te identi&ier ic is $ut in &ront o& te

    colon.

  • 8/18/2019 Making Music With Sonic Pi

    29/61

    A ty$o in te $arameter identi&ierse.". $lay8 "3? am$8 6? relaese8 27 creates no error messa"e in !onic

    +i. 5ater? tis $arameter is i"nored. ot i"nored oe)er is te asence o& a comma or colon. Tere

    is indeed an error messa"e "i)en. 'ore aout te error messa"es &rom !onic +i in a$ter -.

    :ic $arameters are no a)ailale &or eac metod. Te $arameters commonly used can e

    learned y eart )ery uickly. Basically? tere is a documentation o& all $arameters. For @$lay? te

    $arameter descri$tions can e &ound in te el$ in @synts &or eac a)ailale syntesiJers? see .

    Figure 1;. The parameter of the pla method and there meaning

    11#1 -n!elopes

    Te time seuence o& te )olume o& a sound is descried y its en)elo$e. Tis is not only &or !onic +i

    te case? ut also &or oter electronic tone "enerators. Also e)ery natural instrument as at least7

    one en)elo$e. An instrument or a sound can e lon" or sort? it can immediately e noisy or sloly

    aloud? it can sin" or sudden end. And in !onic +i? some o& te $arameters o& te @$lay are used to

    $roduce an en)elo$e. sos a "ra$ical re$resentation o& tis.

  • 8/18/2019 Making Music With Sonic Pi

    30/61

    Figure 1=. "nvelope parameters of the pla method

    Te sound is di)ided into 4 sections. @attack s$eci&ies o lon" te sound "ets louder and reaces

    te @Attack ,e)el. Ten te sound )olume decreases as lon" as te @decay $arameter s$eci&ies.

    Tis is &olloed y @sustain and @release. All time $arameters are s$eci&ied in seconds. Te sum o&

    all time $arameter s$eci&ies te len"t o& te sound.

    that should #e done that should #e left

    +arameters are so im$ortant. In !onic +i

    $arameters are de&ined y te name.

    12 $rogra" Structures

    12#1 Threads

    Treads are a)ailale in many oter lan"ua"es. Tey are used to $rocess multi$le tasks in $arallel. In

    !onic +i tey ser)e to encode se)eral instruments se$arately. An e/am$le &or tis is son in ode

    11. ou Dust a)e to tell !onic +i ere te $arallel $ortion e"ins inMtread do7 and ere it ends

    end7. E)erytin" in eteen is $aralleled. Bot commands are son in old in te e/am$le.

    treads

    Hans Gruendel

    20.0*.201#

    inEthread do

  • 8/18/2019 Making Music With Sonic Pi

    31/61

      slee$ 1

      1#.times do

      sam$le 8dMJome

      slee$ 2

      end

    end

    inEthread do

      slee$ 16

      4.times do

      useMsynt 8&m

      $lay 8a3? attack8 0.01? release8 1

      slee$ 0.#

      $lay 8a4? attack8 0.01?release8 1

      slee$ 0.#

      $lay 8c#? attack8 0.01?release8 1

      slee$ 0.#  $lay 8e#? attack8 0.01?release8 1

      slee$ 0.#

      $lay 8"3? attack8 0.01? release8 1

      slee$ 0.#

      $lay 8"4? attack8 0.01? release8 1

      slee$ 0.#

      $lay 84? attack8 0.01? release8 1

      slee$ 0.#

      $lay 8d#? attack8 0.01? release8 1

      slee$ 0.#

      $lay 8e3? attack8 0.01? release8 1  slee$ 0.#

      $lay 8"4? attack8 0.01? release8 1

      slee$ 0.#

      $lay 8a4? attack8 0.01? release8 1

      slee$ 0.#

      $lay 8e#? attack8 0.01? release8 1

      slee$ 0.#

      $lay 8&3? attack8 0.01? release8 1

      slee$ 0.#

      $lay 8&4? attack8 0.01? release8 1

      slee$ 0.#  $lay 8a4? attack8 0.01? release8 1

      slee$ 0.#

      $lay 8c#? attack8 0.01? release8 1

      slee$ 0.#

      end

    end

    inEthread do

      #.times do Vid/3V

      itM&/ 8re)er do

  • 8/18/2019 Making Music With Sonic Pi

    32/61

      useMsynt 8&m

      $lay 8a3? attack8 0.01? release8 2? am$81= 0.2Wid/3

      $lay 8a4? attack8 0.01?release8 2? am$81 = 0.2Wid/3

      $lay 8c#? attack8 0.01?release8 2? am$81 = 0.2Wid/3

      $lay 8e#? attack8 0.01?release8 2? am$81 = 0.2Wid/3

      slee$ 2  $lay 8"3? release8 2? am$81 = 0.2Wid/3

      $lay 8"4? release8 2? am$81 = 0.2Wid/3

      $lay 84? release8 2? am$81 = 0.2Wid/3

      $lay 8d#? release8 2? am$81 = 0.2Wid/3

      slee$ 2

      $lay 8e3? release8 2? am$81 = 0.2Wid/3

      $lay 8"4? release8 2? am$81 = 0.2Wid/3

      $lay 8a4? release8 2? am$81 = 0.2Wid/3

      $lay 8e#? release8 2? am$81 = 0.2Wid/3

      slee$ 1.2#  $lay 8&3? release8 2? am$81 = 0.2Wid/3

      $lay 8&4? release8 2? am$81 = 0.2Wid/3

      $lay 8a4? release8 2? am$81 = 0.2Wid/3

      $lay 8c#? release8 2? am$81 = 0.2Wid/3

      slee$ 2.#

      end

      end

    end

    inEthread do  slee$ -

      #.times do

    sam$le 8drumMassMard? rate80.#? am$8 1.3

      slee$ 2

      sam$le 8drumMassMard? rate80.#? am$8 1.3

      slee$ 2

      sam$le 8drumMassMard? rate80.#? am$8 1.3

      slee$ 1.2#

      sam$le 8drumMassMard? rate80.#? am$8 1.3

      slee$ 2.#

      endend

    Code 11.inEthreads command code

  • 8/18/2019 Making Music With Sonic Pi

    33/61

    12#2 Cue 8Sync7 Gi!e Inserts

    In a $iece o& music? te indi)idual $arts are alays related to eac oter. And sometimes tey are

    $layed e)en simultaneously. Tis is &or e/am$le in te )arious instruments o& te son" o& te case.

    And sometimes tey are attaced to eac oter. A ty$ical e/am$le is te can"e &rom )erse and

    corus.

    :it te commands >cue> and >sync> you can no enter in !onic +i inserts. >ue> "i)es te insert

    and >sync> ill retrie)e it.

    %sin" a $arameter &or te commands >cue> and >sync> let !onic +i knos at elon"s to"eter

    res$ecti)ely. Te only $arameter is tere&ore te name o& te insert and may e aritrary. Te

    &olloin" e/am$le uses te name >tickMdrum>.

    Cue$ tic0Edrum

    snc tic0Edrum

    >ue> means in te music tat te insert is "i)en? &or e/am$le? y a conductor. >!ync> is te

    are)iation &or >syncroniJe> and acce$ts te insert. Fi"ure 20 sos an e/am$le. ue "i)es te

    insert to >tickMdrum>.

    ode 12 uses tis e/am$le a"ain. In tat? you can reco"niJe o ue and !ync orks. At te

    e"innin"? $lease $ut te comment si"n e&ore it8

    cue8 tickMdrum

    !8 tickMdrum

    Figure %>. Cue and Snc

  • 8/18/2019 Making Music With Sonic Pi

    34/61

    :ell? drumMea)yMkick ould e $layed tice? at 0.6 seconds and 1.6 seconds. otin" else ould

    e e/$ected usin" aitin" $eriods o& 0.6 seconds slee$ 0.67 and o& 1 second slee$ 17. :itout

    uncomment drumMea)yMkick is $layed at 3 seconds and 6 seconds e/actly en te ue call is

    e/ecuted.

    o? at a$$ens en te ue or te !ync call missin"C

    a7 ue missin"8 It ill not e e/ecuted? at stands eind te associated !ync call.

    7 !ync is missin"8 ue is i"nored and te code in te 2nd tread runs immediately.

    Incidentally? te !ync command must e run alays &irst. :ould tis e e/ecuted a&ter te ue call?

    ten te ue ill e i"nored.

    ue and !ync 2

    Hans Gruendel

    20.0-.201#

    inMtread do

      2.times do

      sam$le 8"uitMeMslide

      slee$ 3

      cue 8tickMdrum  end

    end

    inMtread do

      slee$ 0.6

     sync 8tickMdrum

      2. times do

      sam$le 8drumMea)yMkick

      slee$ 1

      end

    endCode 1%. Cue and Snc example

    12#3 %oops

    'usic is re$etition. Tere are almost no $ieces o& music in ic note seuences are not re$eated.

    om$letely o)iously a$$ens tis in te $ercussion tracks o& $o$ music. Tey are e)en so dominant

    tat 30 or more years a"o? te &irst drum macines a)e een used in ic te seuence o& sounds

    as really uite ri"id.

  • 8/18/2019 Making Music With Sonic Pi

    35/61

    ode 13 is a loo$ sam$le. Te command tat starts te loo$ is @/.times do? ere / is te numer o& 

    iterations. Also 0 is $ossile? ten te loo$ is not e/ecuted. ecimals are also ecce$ted in &or / in

    !onic +i. Te )alue is sim$ly rounded. !o 4.# means 4 iterations.

    All commands u$ to te ne/t @end are loo$ed.

    ,oo$s

    Hans Gruendel

    20.0*.201#

    inMtread do

      slee$ 1  1#.times do

      sam$le 8dMJome

      slee$ 2

      end

    end

    Code 1'. (oops

    12#4 9ith Construct7 -cho and other -ffects

    !onic +i o&&ers it synts and sam$les already a lar"e numer o& sounds. Anyay? sometimes tey

    are not at you e/$ect. E&&ects can no modi&y tese sounds. Ty$ical e&&ects tat occur outside o&

    music in e)eryday li&e? are re)er and delay. 5e)er is $roduced in a lar"e room en te sound is

    tron )ery uickly ack &rom te alls. Te eco is $roduced y te same mecanism? oe)er? te

    sur&aces at ic te sound is re&lected is muc &arter aay? suc as rock alls in te mountains.

    !onic +i makes e&&ects usale y te command @itM&/. As can e seen in ode 14? @itM&/ is

    used y a lock. It starts it @itM&/8 e&&ect name do and ends it @end.

    :it K Eco

    Hans Gruendel

    20.0*.201#

    sam$le 8amiM$iano

    slee$ 2

  • 8/18/2019 Making Music With Sonic Pi

    36/61

    inMtread do

      withEfx $echo do

      sam$le 8amiM$iano

      slee$ 6

      endend

    Code 1,. DithEfx used for the echo effect

    :at e&&ects are tereC In !onic +i te e&&ects are e/$lained in te >F/> ta o& el$? as son in Fi"ure

    21. Eac e&&ect can e modi&ied usin" $arameters.

    Figure %1. "ffects in Sonic Pi

    A com$lete descri$tion o& te e&&ect and also any oter e&&ect7 can e &ound in te el$? as son in

    Fi"ure 22. Te descri$tion is structured like te &olloin"8

    1. All $arameters o& te e&&ect are mentioned

    2. A small e/am$le it no $arameters is "i)en

    3. A rie& descri$tion o& te e&&ect8 at it does

    4. Te numer? in ic te e&&ect as introduced

    #. A detailed descri$tion o& all $arameters it te ran"e in ic te $arameters can e )aried and

    te de&ault )alue. Tis is te )alue tat uses te e&&ect? i& te $arameter is not s$eci&ied y us en

  • 8/18/2019 Making Music With Sonic Pi

    37/61

    e call it.

    3 im$ortant e&&ects ill e e/$lained ere it teir $arameters.

    12#4#1 &istortion

    >istortion> is a ty$ical "uitar e&&ects. Te sound is someo unclean and dirty at makes it more

    interestin" tan a clean sound. In te ode Editor tat ould look like tis8

    itM&/8 istortion do

    +lay8 "4

    end

    o aout te $ossile $arameters o& distortion8

    amp

    :e kno tis $arameter already &rom te command >$lay>. Here? as tere is >am$> an are)iation

    &or >am$li&ier> and re&erred to te "ain o& te sound. ;alues eteen 0 and 1 are $ossile. As you can

    ima"ine? means am$ L 0 tat e ill ear no distortion ile am$ L 1 is te normal )olume tat is

    already $reset.

    mix

    !ets te ratio o& ori"inal sound and te distorted sound. 0 means tat only te ori"inal sound is

    eard? 1? only te distorted sound is eard. Te de&ault )alue is 0.#.

    Figure %%. :elp for the distortion effect

  • 8/18/2019 Making Music With Sonic Pi

    38/61

    preEamp

    Be&ore my sound "oes into te am$li&iers no &i"urati)ely s$eakin"7 I can adDust te )olume. 0

    means silent? 1 is loud. e&ault )alue is 1.

    distort

    Tat is te stren"t or te le)el o& distortion. 0 ere means little? 1 means a lot. Te de&ault )alue is0.#.

    12#4#2 -cho

    Te eco e&&ect in !onic +i does e/actly at one ould e/$ect8 a sound is re$eated once or se)eral

    times7 uieter in a certain time inter)al.

    useMsynt8 F'

    itM&/ eco? delay8 0.2#? decay8 *7 do+lay ##

    end

    o to te $ossile $arameters o& eco8

    amp

    :e kno tis $arameter already &rom te e&&ect >istortion> Here a"ain >am$> is te are)iation &or

    >am$li&ier> and re&erred to te "ain o& sound. ;alues eteen 0 and 1 are $ossile.

    mix

    !ee te descri$tion o& te e&&ect >istortion>.

    preEamp

    !ee te descri$tion o& te e&&ect >istortion>.

    phase

    !$eci&ies te time inter)al in seconds eteen te tones o& indi)idual eco. Te de&ault )alue is 0.2#.

    deca

    Indicates te numer o& re$etitions.

    maxEphase

    !$eci&ies te ma/imum $ossile $eriod o& ecoes in seconds. Te de&ault )alue is. 2.

    12#4#3 0e!er/

    5e)er is an e&&ect in ic te tone sounds as i& it is $layed in a lar"e room or a den. In $o$ music?

    re)er is te standard e&&ect on te )ocals and corres$ondin"ly ides$read.

  • 8/18/2019 Making Music With Sonic Pi

    39/61

    itM&/8 5e)er do

    +lay8 c4

    end

    amp

    !ee te descri$tion o& te e&&ect >istortion>.

    mix

    !ee te descri$tion o& te e&&ect >istortion>. e&ault is 0.4.

    preEamp

    !ee te descri$tion o& te e&&ect >istortion>. e&ault 1.

    room

    Te stren"t o& te room8 0 means no s$ace and tere&ore no re)er? 1 means a stron" re)er. Te

    de&ault )alue is 0.6.

    damp

    Hi" &reuencies i.e. te i" $ro$ortion in te sound7 are muted. 0 denotin" no dam$in"? and 1

    ma/imum attenuation. Te de&ault )alue is 0.#.

    All e&&ects ork in !onic +i in te same manner. Alays te sounds to e $layed it an e&&ect? starts

    it

    itM&/

    and ends it

    end

    It is not necessarily ort to memoriJe all te $arameters o& all e&&ects. on)eniently? te )alues tat

    can take $arameters? are set uite lo"ical. 0 means notin" or minimum? 1 is te ma/imum? )alues

    eteen 0 and 1 a)e an increase in e&&ect.

    that should #e done that should #e left

    :en a son" consists o& $arallel $arts? i.e. &or

    e/am$le o& se)eral )oices? ten you sould

    structure tese in treads.

    istriute successi)e $arts tat elon" to"eter

    uite clear to se)eral treads.

    5e$eatin" structures e/ist in almost e)ery $iece

    o& music. A)oid tese re$etitions in te code.

    Alays use loo$s.

  • 8/18/2019 Making Music With Sonic Pi

    40/61

    13 :aria/les and *rith"etic ;perations

    !o &ar e a)e een ale to make a lot in +i !onic ut still ad no contact it )ariales. In oter

    $ro"rammin" lan"ua"es tis is di&&erent. !ince )ariales are e/tremely im$ortant. Tis ca$ter

    e/$lains o )ariales are used.

    13#1 :aria/les

    Te &olloin" e/am$le sould so at a )ariale is and o it orks.

    Ima"ine you ant to $roduce a series o& tones? and &rom note to note te tone sould e i"er. tone>. !econdly? a )alue o& 60 is

    assi"ned to it. To lines elo te )ariale 'II $itc is 607 is $layed8

    $lay sound

    And ten te )ariale is incremented y te )alue 1.

    tone L tone 9 1

    ;ariales

    Hans Gruendel

    2#.0*.201#

    inMtread do

      useMsynt 8&m

     $lay 8"4

      slee$ 2

      tone H >

      10.times do

      $lay tone

      slee$ 1

  • 8/18/2019 Making Music With Sonic Pi

    41/61

      tone H tone 1

      end

    end

    Code 1. Jaria#les

    Because te code contains a loo$ 10 tones are $layed.Fi"ure 23 sos te code editor and te lo"indo. :en runnin" te code te )alue o& te )ariale increases. :it eac $layin" te tone $itc

    increases y 1.

    Figure %'. Jaria#les

    A ord aout te assi"nment. From te 'at classes your knos te usa"e o& te eual si"n? and

    also )ariales and euations. Te eual si"n means in all o& matematics? tat te le&t and ri"t o&

    te eual si"n is te same )alue or te same term. Tis is di&&erent in so&tare $ro"rammin".

    tone L tone 9 1 is not ri"t &or any )alue tat is used in tone? de&initely. I&? &or e/am$le tone as a

    )alue o& 60? ten tere ould e 60 L 61 and tat ould e ron". :e must not &or"et tat te eual

    si"n in so&tare $ro"rammin" stands &or an assi"nment. Here? te term ri"t o& te eual si"n is

    e)aluated tone 9 17 and assi"ned to te )ariale on te le&t o& te eual si"n.

    It sould e noted tat eac )ariale is assi"ned to a numer at te time o& code e/ecution. I& tis is

    not te case? ten it comes to a termination o& te $ro"ram see Fi"ure 247 it te out$ut o& te

    &olloin" error messa"e8

    @unde&ined local )ariale or metod Xkneu

  • 8/18/2019 Making Music With Sonic Pi

    42/61

    Te error messa"e a$$ears in En"lis? ic is also te case in many oter $ro"rammin" lan"ua"es.

    But it is relati)ely easy to understand at in oter lan"ua"es ould not alays e te case8 Te local

    )ariale or metod >kneu> is not de&ined. !o it is not knon and it as not a numerical )alue

    assi"ned.

    'ore aout error messa"es in a$ter -.

    Figure %,. Un0nown varia#le and error message while program execution

    Tere is a second? etter ay to incor$orate a counter in a loo$8 usin" te loo$ command @/ times

    do it te counter as a $arameter. For tis it must e enclosed in )ertical ars8 V loo$counter V. loo$counter> as te )alue Jero in te &irst "o. In eac &urter iterations te )alue is increased

    y 1. Te $itc ill can"e accordin"ly.

    Te use o& a )ariale in loo$s is only one reason to ork it )ariales. Tere are to im$ortant

    more8

    1. esi"nation o& numers

    umers in te ode are &airly normal. In !onic +i numers are used &or many $arameters. At &irst

    "lance? tese numers are also $lausile. ,ater? or &or oter $eo$le? tey are not so trans$arent. Tat

    is y one o& te asic $rinci$les? not to use numers ma"ic numers7 as a $arameter? ut assi"n

    sel&=e/$lanatory )ariales and ten use tese. It is $ossile to use te )ariale in many $laces. I& te

  • 8/18/2019 Making Music With Sonic Pi

    43/61

    )alue sould e can"ed? it needs to e done Dust at only one location in te code? namely? ere te

    allocation takes $lace.

    2. !tora"e o& return )alues

    'etods can "i)e ack a return )alue a&ter &inisin". Tat could e a result o& a calculation or a )aluedescriin" te success state o& te metod run. Tese return )alues can e assi"ned to )ariales and

    later e used.

    Figure %. Jaria#le as loop parameter

    13#2 Controls

    I& you a)e $layed a sound once and ill re$eat tis can"ed sli"tly? ten ontrols o&&er te rele)ant

    &unctionality. For tis te $lay command is assi"ned to a )ariale and tis )ariale can later e

    modi&ied it te control=command and te a$$ro$riate $arameters. It ould look like tis8

    s L $lay 0? release8 6

    slee$ 1

    control s? note8 6#

    Here? te $itc is can"ed &rom 0 to 6#. A sim$le e/am$le to ontrols are son in Fi"ure 26.

    ontrols to can"e $arameters.

  • 8/18/2019 Making Music With Sonic Pi

    44/61

    Te can"e a$$ened suddenly ere. But !onic +i also allos a slidin" can"e. Tis can e done y

    usin" te slide $arameter. I& you ant to can"e te $itc &or e/am$le? tis mi"t e done as &ollos8

    myControl = play 60, release: 5, note_slide: 1

    sleep 0.5

    control myControl, note: 55

    13#3 *rith"etic ;perations

    From te mat classes e already kno some o$erations? only e call tem te 4 asic aritmetic

    o$erations. !o tese are8

    $lus8 9

    minus8 =

    times8 W

    di)ided8 K

    All o& tese aritmetic o$erators can e used eteen )ariales or numers. Fi"ure 2 sos an

    e/am$le o& te addition o& to numers. In te lo" )ieer it can e seen tat a total o& 3 seconds

    aitin" time a$$ens until te second note is $layed.

    Figure %. Using controls to modif parameters

  • 8/18/2019 Making Music With Sonic Pi

    45/61

    Tis e/am$le clearly sos te codin" o& te addition o& to numers. Hoe)er? one can onder

    at ad)anta"e it o&&ers. It ould e easier to rite

    !lee$ 3

    slee$ 3

    orrect it only makes sense i& you use )ariales? as can e seen in ode 16. Te >rin"McMd> &unction is

    called tice. i> and ten it >i 9 1>. >i> is incremented it eac $ass o& te

    loo$.

    16.times do ViV

      rin"McMd i

      slee$ 0.2#

      rin"McMd i92

      slee$ 0.2#

      end

    Te resultin" numer is di&&erent &or eac loo$. Tats y you can not enter te same numer as a

    result o& te addition.

    Figure %4. 7rithmetic 5peration

  • 8/18/2019 Making Music With Sonic Pi

    46/61

    :it K Eco

    Hans Gruendel

    20.0*.201#

    are

    not "ood.

    14 &ata Structures

    ;ariales e a)e already met. In !onic +i it is $ossile to comine multi$le )alues or )ariales in a

    structure. ,ater in te code? tese structures can e used and modi&ied.

  • 8/18/2019 Making Music With Sonic Pi

    47/61

    14#1 %ists and 0ings

    A list is a series o& )alues or )ariales enclosed in suare rackets and se$arated y a comma and

    could look like tis8

    #0? ##? 60U

    Tis list contains 3 )alues it te command8

    $lay #2? ##? #-U

    you can $lay all te )alues simultaneously. !o tis one line ill re$lace te tree lines8

    $lay #2

    $lay ##

    $lay #-

    :it a list so you can already sa)e ty$in". But list )alues need not e numers. I can also assi"n a

    )alue to a )ariale in ad)ance and ten $lu" into a list8

    eton L #0

    $lay eton? 60U

    !ometimes you a)e a i""er list and Dust ant to use &rom tat a sin"le )alue. Tere is a sim$le ay

    in !onic +i to do so. I& you ant to use? &or e/am$le? te tird )alue o& a list? ten use an inde/ y

    $uttin" a 2 into te rackets eind te list name8

    $lay eton? 60? 66? 0U2U

    Actually? you mi"t tink to used a 3U to access te tird element. But do not &or"et te 0. 0U

    tere&ore $oints to te &irst element? 1U to te second? 2U to te tird and so one.

    Tat seems a it akard at &irst "lance? ecause instead o& 

    $lay eton? 60? 66? 0U2U

    you could rite directly

    $lay 0

    But lists alays make sense i& te elements ere "atered to"eter anyay and you ant to access

    tem )ia a )ariale.

    ,ists a)e a e"innin" and an end. But i& tey are &ormed to a circle? ten tey are called a rin". 5in"s

    are also data structures in !onic +i. For tem tere is no end. Accordin"ly? you can access elements o&

    tem o)er any i" inde/ = e)en i& only a )ery limited numer o& items a)ailale. And o orks

    tatC ;ery easily. :it eac ne encirclement o& te rin"? te inde/ is not e)en set to Jero? ut

    sim$ly continues to e counted. I& you a)e &or e/am$le a rin" a)in" 4 elements? ten te &irst

  • 8/18/2019 Making Music With Sonic Pi

    48/61

    element is as alays addressed y te inde/ 0. But also 4? *? 12? etc ould ork. Te second element

    can ten e addressed y te inde/ 1 and # and -? etc..

    o you a)e to kno o to create a rin". Tere are 2 ays? ut I ant to $resent ere only one.

    Sust a$$end to te list a $oint and a&terard te ord @rin"? as son in te &olloin" e/am$le.

    40? 60? 6-? ##U.rin"

    Te notation is almost7 consistent it many oter $ro"rammin" lan"ua"es? ere you can modi&y

    oDects usin" metods. Tat list ..U is ere te oDect? and te metod @rin" "enerates te rin" and

    te $oint is set in eteen to let !onic +i kno tat to tin"s e/ists ere.

    14#2 Chords

    ords are collections o& tones like lists are sometimes. ords a)e te ad)anta"e tat tey are

    already de&ined and usale immediately. In te e/am$le in Fi"ure 2* a maDor and in =minor triads

    are $layed. In "eneral te ar"uments o& te cord metod are te asis tone &irst and second te

    ty$e o& te cord.

    It is )isile in te lo" )ieer tat te cords are uite similar. Actually? only te tone in te middle is

    di&&erent. Besides te maDor and minor cords? !onic +i knos a lot o& oter cords? sometimes also

    less knon as te au"mented or diminised se)ent cord etcZ

    oin" e/$eriments it cords is &un. And te est is tat you need not to kno tem all y eard. In

    te code editor !onic +i sos to you all ty$es o& cords a)ailale. Sust $ress te comma key a&ter

    you rote te asic tone? see Fi"ure 2-.

    Figure %;. C maKor and C minor triads

  • 8/18/2019 Making Music With Sonic Pi

    49/61

    :it te metod $lay cord 7 all te notes o& a cord are $layed simultaneously. eed to $lay all

    sounds in succession? ten you sould use >$layM$attern> instead o& >$lay>.

    $layM$attern cord8c4? 8dim7

    Te $auses eteen te notes are e/actly one second. And i& you ant to use a di&&erent reak time?

    ten one "i)e it as in e/tra $arameter8

    $layM$atternMtimed cord8E3? 8m7? 0.2#

    14#1 Scales

    A scale is a seuence o& ascendin" tones. Fi"ure 30 sos a code e/am$le? ic $lays te =maDor

    scale. Tis scale contains * tones? and e"ins as te name su""ests? it a tone . Tis is &olloed y

    6 more tones and ten tere is a a"ain? no an octa)e i"er. From te &irst to te second te

    &reuency o& te sound doules. Tis is a caracteristic &eature o& eac scale.

    Figure %=. 7ll availa#le chords in Sonic Pi are visi#le after pressing the comma 0e.

  • 8/18/2019 Making Music With Sonic Pi

    50/61

    Te easiest ay to $lay a musical scale is to use te $lay command. A&ter te $lay &ollos te

    command >scale> it 2 ar"uments in $arenteses8 First? te root o& te scale? &olloed y te ty$e

    o& scale. Belo it is an e/am$le &or a maDor scale8

    $lay scale8c4? 8maDor7

    Beind te ri"t racket oter $arameters can &ollo &or te $lay command. !ince >c4> re$resents anote? I can also use te a$$ro$riate 'II numerical )alue? &or e/am$le? i& I rite a counter in a loo$.

    Tis is done in ode 1.

    !cales

    Hans Gruendel

    1*.0-.201#

    useMsynt 8&m

    4.times do Vi/V

      $layM$atternMtimed scalei/9#0? 8maDor7? 0.2#

      slee$ 1

    end

    Code 14. Scales

    Figure '>. The C maKor scale is plaed

  • 8/18/2019 Making Music With Sonic Pi

    51/61

    :at you no a)e to kno aout scalesC In Euro$e e$tatonic scales are ides$read? ic

    consists o& di&&erent tones as te name su""ests. :at ty$es o& scales are a)ailale in !onic +iC

    Here 2 e/am$les8

    maDor8 Tese scales consist o& te &olloin" ste$s8 &ull tone = &ull tone ( semi tone = &ull tone = &ull tone

    = &ull tone ( semi tone

    minor8 Tese scales consist o& te &olloin" ste$s8 &ull tone = semitone = &ull tone = &ull tone = semitone

    = &ull tone = &ull tone

    !onic +i could so all a)ailale scale ty$es =see Fi"ure 31.

    15 Functions

    Functions are com$onents in te code tat do sometin" s$eci&ic. ou can de&ine te &unction once

    and ten call it any numer o& times. !o you can sa)e uite a it o& code and ill a)oid re$etition in

    te code. As an e/am$le can e seen in te &unction >sound1> in te code editor? see Fi"ure 32.

    Figure '1. An astonisin" numer o& scales are a)ailale in !onic +i

  • 8/18/2019 Making Music With Sonic Pi

    52/61

    Figure '%. The function sound1

    Eac &unction e"ins it te keyord @de&ine and ends it @end. All lines eteen tem elon"

    to te &unction.

    In te e/am$le >sound1> a distincti)e sound? ic consists o& 4 asic sounds &or te corres$ondin"

    !ynts are set in te &unction. Te $itc &or all sounds is te same? ecause e)ery play  statement uses

    te ar"ument num. ou sim$ly can co$y ode 1* and try it.

    Functions

    Hans Gruendel

    2#.0*.201#

    inMtread do

     sound1 64

      slee$ 2

     sound1 61

    end

    de&ine 8sound1 do VnumV

      useMsynt 8&m

      $lay num

      useMsynt 8$rettyMell

      $lay num

      useMsynt 8sine

  • 8/18/2019 Making Music With Sonic Pi

    53/61

      $lay num

      useMsynt 8tri

      $lay num

    end

    Code 1;. "xample of a function

    In tis e/am$le? te &unction is called tice.

  • 8/18/2019 Making Music With Sonic Pi

    54/61

    :at a$$ens i& te numer o& $arameters o& te call and te &unction de&inition does not matcC

    :ell? ten tere is an error messa"e ic is son in Fi"ure 33. 'ore on tis in a$ter -.

    Figure ''. The function sound1 is called in line = with to few parameters

    ,ets look at te e/am$le care&ully and consider at can e im$ro)ed. I can tink o& 3 tin"s

    s$ontaneously8

    1. ame o& &unction8 sound1 says already at tis is aout? namely a sound? ut te >1> is still )ery

    uns$eci&ic. !oundM&m P 3 ould clearly e etter ere.

    2. o e/$lanation o& at te &unction does8 still missin" te comment.

    3. In !onic +i and in many oter lan"ua"es ar"uments can e $rede&ined it a &i/ed )alue. ode 20 indicates o tis orks. Te second ar"ument o& te )alue 0.1 is assi"ned. Tere are no 2 ays.

    Eiter I call te &unction it 2 ar"uments? ten tese 2 are used? or Ill call it 1 ar"ument? and

    ten te )alue o& 0.1 is automatically used &or te second ar"ument.

    de&ine 8sound1 do Vnum? len"t L 0.1V

      useMsynt 8&m

      $lay num? release8 len"t

      useMsynt 8$rettyMell

      $lay num? release8 len"t

  • 8/18/2019 Making Music With Sonic Pi

    55/61

      useMsynt 8sine

      $lay num? release8 len"t

      useMsynt 8tri

      $lay num? release8 len"t

    end

    Code %>. &efault of arguments

    Anoter e/am$le is son in ode 21. Te loo$ $arameter i  is $assed to te

    &unction ring_c_d  en callin" it. It is irrele)ant tat te $arameter in te

    &unction de&inition as a di&&erent name8 tone. !ince te loo$ is done 16 times?

    te &unction is called 16 times and 16 sounds are $layed. As te rin" structure

    contains * tones? it is alked trou" com$letely 2 times.

    &unctions and rin"s =3

    Hans Gruendel

    2#.0*.201#

    inMtread do

      16.times do ViV

      rin"McMd i  slee$ 0.2#

      rin"McMd i92

      slee$ 0.2#

      end

    end

    de&ine 8rin"McMd do VtoneV

      allMtones L 8c4? 8e4? 8"4? 8c#? 8d4? 8&4? 8a4? 8d#U.rin"

      useMsynt 8&m

      $lay $lay allMtonestoneU

    endCode %1. Function and ring structure

    that should #e done that should #e left

    Functionality tat is used at se)eral locations

    sould e $acked in a &unction.

    Functions sould a)e a meanin"&ul name. e)er $ut te same code to di&&erent locations.

    Good style is to $ut in &ront o& eac &unction a

    comment tat e/$lains te &unction.

  • 8/18/2019 Making Music With Sonic Pi

    56/61

    ,earned &or $ro"rammin"

    o To$ic

    1 Functions are an im$ortant $art o& $ro"rammin". Te $rinci$le is8 de&ined once and

    called at se)eral locations in te $ro"ram. And Dust as im$ortant8 A &unction sould

    alays $ro)ide a lo"ically limited &unctionality. o more and no less. I& a &unctiononly as a command? ten tat is not enou".

  • 8/18/2019 Making Music With Sonic Pi

    57/61

    "rror

    [ see error messa"es

    "rror -essages

    ode may contain errors. I& te code is e/ecuted? it must e e/actly consistent. I& not te $ro"ram

    $rints an error messa"e. !onic +i also out$uts te line numer? so tat te error can e &ound easily.

    Functions

    Functions are $arts o& te code dedicated to a certain $ur$ose. Te code is de&ined once in a &unction

    and can e called as o&ten as anted.

    -ethods

    'etods and &unctions are $retty muc te same. A collection o& commands? to"eter ser)e a

    $ur$ose.

    -I&I

    Are)iation &or >'usical Instrument i"ital Inter&ace>? ic is a de&inition o& a music notation.

    Parameter

    see [ ar"uments

    Programming (anguage

    Includes te commands to e/ecute. +ro"rammin" lan"ua"es are alays uniue.

    Jaria#le

    A )ariale is a container ic can store a sin"le )alue or e)en multi$le )alues ic can e modi&ied

    &rom te code.

    1) Specific Ter"s of the Musician

    Chord

    !e)eral notes are $layed to"eter and a)e teir on caracteristics. An e/am$le is te maDor triad.

    FreLuenc

    umer o& re$etiti)e e)ents in one second.

    /e

    Te smallest unit o& a musical $iece.

    Pause

    o sounds are $layed. Te command is >slee$>.

  • 8/18/2019 Making Music With Sonic Pi

    58/61

    Sample

    A &airly sort $assa"e o& a $iece o& music or a sound tat as recorded and ten used in anoter

    son".

    Scale

    The ascendin" seuence o& tones tat a)e a certain distance &rom eac oter.

    Time

    Te $oolin" o& indi)idual notes in a "rou$. Is te same len"t &or a $articular $iece o& music? &or

    e/am$le? a tree=uarter 3K47 time.

  • 8/18/2019 Making Music With Sonic Pi

    59/61

    Figures

    Fi"ure 1. Te Gra$ical %ser Inter&ace o& !onic +i it a sim$le e/am$le..............................................#

    Fi"ure 28 G%I elements o& !onic +i..........................................................................................................

    Fi"ure 3. Te \$lay=metod it te $arameter \c4...........................................................................*

    Fi"ure 4. Yeyoard it natural and al& tone keys................................................................................-

    Fi"ure #. !et te len"t o& a tone usin" a \release $arameter..............................................................-

    Fi"ure 6. +itc as con)entional c#7 and 'II 27 descri$tion.............................................................10

    Fi"ure . Te comment in te &irst line starts it te as.................................................................12

    Fi"ure *. To commands in !onic +i8 >$lay> and >slee$>......................................................................13

    Fi"ure -. Automatic code com$letion in !onic +i..................................................................................13

    Fi"ure 10. +arameters are "i)en to te metods >$lay> and >slee$>....................................................14

    Fi"ure 11. !ound $ro$et..................................................................................................................16

    Fi"ure 12. All synts &rom !onic +i........................................................................................................16

    Fi"ure 13. Te re$eat loo$ in !onic +i...................................................................................................1

    Fi"ure 14. ode Editor it scroll ars.................................................................................................1-

    Fi"ure 1#. E/tra 'B5 commands..........................................................................................................20

    Fi"ure 16. Incorrect code in line 10......................................................................................................22

    Fi"ure 1. Te coose metod selects &rom te )alues 60? 61? and 61................................................2#

    Fi"ure 1*. Te $arameter o& te @$lay metod and tere meanin"...................................................2-

    Fi"ure 1-. En)elo$e $arameters o& te @$lay metod........................................................................30

    Fi"ure 20. ue and !ync........................................................................................................................33

    Fi"ure 21. E&&ects in !onic +i.................................................................................................................36

    Fi"ure 22. Hel$ &or te distortion e&&ect...............................................................................................3

    Fi"ure 23. ;ariales..............................................................................................................................41

    Fi"ure 24. %nknon )ariale and error messa"e ile $ro"ram e/ecution.........................................42

    Fi"ure 2#. ;ariale as loo$ $arameter..................................................................................................43

  • 8/18/2019 Making Music With Sonic Pi

    60/61

    Fi"ure 26. %sin" controls to modi&y $arameters...................................................................................44

    Fi"ure 2. Aritmetic

  • 8/18/2019 Making Music With Sonic Pi

    61/61

    ode #. +art 1 and 2 o& !on" 2 comined in a loo$..............................................................................1*

    ode 6. 'etod rrand &or "eneratin" random numers......................................................................23

    ode . 'etod rrand &or "eneratin" random numers it seed in ad)ance....................................24

    ode *. 'etod coose &or te random selection...............................................................................2#

    ode -. 5andom son"...........................................................................................................................2*

    ode 10. +arameter o& te @$lay metod...........................................................................................2*

    ode 11.inMtreads command code.....................................................................................................32

    ode 12. ue and !ync e/am$le...........................................................................................................34

    ode 13. ,oo$s.....................................................................................................................................3#

    ode 14. :itM&/ used &or te eco e&&ect...........................................................................................36

    ode 1#. ;ariales................................................................................................................................41

    ode 16. Addition o& a numer and a )ariale.....................................................................................46

    ode 1. !cales.....................................................................................................................................#0

    ode 1*. E/am$le o& a &unction............................................................................................................#3

    ode 1-. Function it 2 )ariales.......................................................................................................#3

    ode 20. e&ault o& ar"uments.............................................................................................................##

    ode 21. Function and rin" structure...................................................................................................##