Making Music with Sonic Pi
Hans Gruendel
An Edutainment Edutainment Factory Book
20.2.2016
1 2
Content Introduction.......................................................................................................................................4
3 Te First !on".............................. !on"................................................................ .................................. .................................. ................................. ..# .. # 4 Te Gra$ical %ser Inter&ace.............................................. Inter&ace................................................................................ .................................. .............................. 6 # 'usic Teory ( )ery sim$le.............................. sim$le................................................................ .................................. ................................ ......................... ............. ............. ......... ..* * #.1
+itc............................................................................................................................................*
#.2 ,en"t o& te Tone................................ one.................................................................. .................................. ................................. ............. ...... ............. ...... #.3
'II..........................................................................................................................................10
6 Te !ynta/ o& !onic +i...................................................... +i..................... ................................. .................................. ................... ............. ............ ........... ..... 11 6.1 odin" Ty$es.............................. 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.............................. !ounds................................................................ .................................. ...................... ................ ........... ..... 14 * Te ode Editor........................................... .................................. ................................ ......................... ............. ............. ............ ..... 1* *.1 ,ine umers..................................................... .................................. ........................... .......... ...... .... 1* *.2
a)i"ation.................................................................................................................................1-
*.3 5i"t 'ouse Button 'B57........................... 'B57............................................................. .................................. .............................. ........................ ............. ............ ..... 1*.4 o$y and +aste........................................................ +aste......................................................................................... ................................. .............................. ... 21 - Error Handlin"................................ .................................. .................................. .............................. ........................ ...... 22 10 5andom ontrol.............................................. ontrol............................................................................... ................................. .................................. ............ ........ .... 23 10.1 5andom umers.................................................. umers.................................................................................... .................................. ....................... ................. ............ ........ ..23 23 10.2
!election.................................................................................................................................2#
10.3 5andom !on"................................ !on".................................................................. .................................. .................................. ..................... .............. ......... 26 11 +arameter8 +arameter8 !ynt and !am$le............................ !am$le............................................................. ................................. .................................. ......... ....... ..2* 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
12.4 :it onstruct8 Eco and oter E&&ects................................................ E&&ects................................................................ ...................... ............. ............ ..... 3# 12.4.1
istortion.........................................................................................................................3
12.4.2
Eco.................................................................................................................................3*
12.4.3
5e)er..............................................................................................................................3*
13 ;ariales and Aritmetic <$erations...................................... .................................. .............. ........ ............. ....... 40 13.1
;ariales.................................................................................................................................40
13.2
ontrols...................................................................................................................................43
13.3 Aritmetic <$erations................................................ <$erations................................................................................. ................................. ........................... .44 14 ata !tructures........................................ .................................. ................................. ............. ...... ............. ...... 46 14.1 ,ists and 5in"s............................... 5in"s................................................................ ................................. .................................. ....................... ................. ...... 4 14.2
ords.....................................................................................................................................4*
14.1
!cales......................................................................................................................................4-
1#
Functions.......................................................................................................................................#1
16 !$eci&ic Terms o& te +ro"rammer............................................ +ro"rammer.............................................................................. .................................. ................. .......... ......... ..#6 #6 1 !$eci&ic Terms o& te 'usician.......................... 'usician............................................................ .................................. .................................. ......... #
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
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
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. 1=1
Sonic Pi command Te First !on"
1=2 1=3 1=4
$lay 8c4 slee$ 1 $lay 8e4
"xplanation 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. !onic +i $lays te note c4. o not &or"et te colon. For a second? no ne note is $layed. !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.
'usic ontrol
+re&erences
Y,.
ode Editor
,o" ;ieer
:orks$ace Tas
Hel$ !election
Hel$ ;ieer
Figure %$ GUI elements of Sonic Pi
!r.
Sonic Pi GUI element
2=1
5un
2=2 2=3
!to$ !a)e
2=4
5ec
#
ode Editor
6
,o" ;ieer Hel$
*
Hel$ ;ieer
"xplanation 'usic ontrol 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. All current son"s sto$ it a click. 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". !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 :rite your son" in te code editor. In &act? it is e)en te only $lace to enter sometin" in !onic +i. !os in cronolo"ical order? ic as een $layed. Here you can searc &or el$. Te temes are undled? and te sim$ler tin"s come &irst. !os at is selected in te Hel$ selection. Ta#le %. The elements of the Sonic Pi interface.
,earned &or $ro"rammin" Topic :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 tool tat el$s you in te com$letion o& commands is inte"rated. Additional ene&its o& an IE ill e descried &urter elo. 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..
r. 1
2
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.
E
F G
E
F
G A
G
A B
A
B
E
E
Figure ,. /e#oard with natural and half tone 0es
: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
1 2
+itc increase +itc decrease
sar$ &lat
s &
Sonic Pi example cs4 e
Ta#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
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 in seconds. 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.
5#3 MI&I Te determination o& te $itc e kno no. A&ter te command >$lay> e a)e to rite te note it a colon includin" a numer &or te s$eci&ic octa)e? &or e/am$le >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>.
:en do I use no ic kind o& descri$tionC Actually? tat does not matter.
7 7
=1 0 1 2 3 4 # 6 * -
21 33 4# # 6*1 -3 10# 11
78 7is6 : 10 22 34 46 #* 0 *2 -4 106 11*
92:3
C
92:3
C
11 23 3# 4 #1 *3 -# 10 11-
0 12 24 36 4* 60 2 *4 -6 10* 120
C8 Cis6&e s 1 13 2# 3 461 3 *# - 10121
& & 2 14 26 3* #0 62 4 *6 -* 110 122
&8 &is6"e s 3 14 2 3#1 63 # * -111 123
"
F
"
F
4 16 2* 40 #2 64 6 ** 100 112 124
# 1 241 #3 6# *101 113 12#
F8 Fis6Ge s 6 1* 30 42 #4 66 * -0 102 114 126
G
G8
G
Gis
131 43 ## 6 -1 103 11# 12
* 20 32 44 #6 6* *0 -2 104 116 12*
Ta#le . -I&I num#ering
'
The Synta( of Sonic $i
Te code editor dis$lays e)erytin" tat makes te son". And tats all in En"lis. Te di&&erent colors also stands out. Te colors indicate to te ty$e o& command itsel&.
'#1 Coding Types
'#1#1
Co""ents
Actually not true commands? ut indis$ensale &or all coders are te comments. In !onic +i tey are marked it a as at te e"innin" o& te line. omments are alays colored in "ray.
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 start and &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 slee$ 2 end
Tat is te &irst sound
Code '. Comments
that should #e done :ritin" comments is asically "ood :rite comments in En"lis as $ossile :ritin" comments in En"lis ene)er $ossile E/$lain music and code it comment
that should #e left
:rite comments in German
'#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$
ommands must e ritten in !onic +i e/actly as tey are de&ined. Tis also a$$lies to te case= insensiti)e. Basically loercase is to use in !onic +i. %$$ercase or miss$ellin" results in !onic +i to an error messa"e. Fortunately? tere is an automatic code com$letion in !onic +i &unction.
Figure =. 7utomatic code completion in Sonic Pi
that should #e done :en I am uite sure? I rite te command. :en I am unsure? ten I use te automatic code com$letion &unction.
that should #e left ommands ritten in ca$itals. In !onic +i all commands are ritten in loer case. Try to "uess a com$lete command. Tere is an automatic com$letion &unctionality.
'#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
,earned &or $ro"rammin" r. 1
2 3
To$ic %se &or $ro"rammin" ene)er $ossile a $ro"rammin" en)ironment called IE Inte"rated e)elo$ment En)ironment7. To ad)anta"es you already kno8 di&&erent $arts o& your code are dis$layed in di&&erent colors. And o&ten tool tat el$s you in te com$letion o& commands is inte"rated. Additional ene&its o& an IE e discussed later. 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. +arameters &or controllin" te &unctionality can e "i)en to many ut not all7 commands. Ta#le $ (earned for programming ? part %.
)
* si"ple Melody +ith different Sounds
Tis time e ant to $ro"ram a sim$le melody in !onic +i. !o a seuence o& tones? controlled y $itc? len"t and reaks. Ten e can"e te sound. But &irst te melody. ou can sim$ly co$y te ode 4 into !onic +i editor indo.
!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.#
$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.
Figure 11. Sound +prophet+
eed to kno at syntesiJers are $resent at all? $lease )isit te ta >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
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 P i
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
$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 1
2
Teme 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. 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 E)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.
,
that should #e left 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 -.
,#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.
Figure 1. "xtra -9@ commands
!r.
Command
1
%ndo
/e#oard Shortcuts Alt 9Q
2
5edo
3
-eaning
7pplication
%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.
Alt 9@!i&t9 Q
'akin" te undo command yet
ut
Alt 9 R
4
o$y
Alt 9
#
+aste
Alt 9;
ut out te marked area o$y te marked area Insert te cut or co$ied ran"e at te
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. 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
$oint ere te cursor is
6
elete
elete te marked area
!elect All
Alt 9 A
!elect all on tis ta
son" and ant to use it in anoter? ten you co$ied &rom one editor ta and inserted in anoter. ou can also sim$ly $ress te >el> key on te keyoard. I& you e)er is to cancel all or co$y all? ten you can mark e)erytin" e&oreand
Ta#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 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.
that should #e left 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.
-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.
Figure 1. Incorrect code in line 1>.
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
E/$lanation8 1 &or 27? means tat 2 ar"uments are e/$ected? ut only 1 "i)en
that should #e done Be&ore lon" ondered eter sometin" is ri"t or ron"? you sould try it and ceck te error messa"e in case o& $rolems. In !onic +i error messa"es are alays in En"lis. Tis is also te case in many oter lan"ua"es and tats a "ood tin". I& you do not understand te error messa"e? te est to do ould e to co$y it into te searc en"ine o& o& te internet.
that should #e left ot ceck te error messa"e e/actly? and ten try to "uess te error
16 0ando" Control 5andom $lays an im$ortant role in many e)ents. But at is random e/actlyC +roaly you kno some systems tat ork randomly. A ell=knon is te drain" o& te lottery numers or te troin" o& a dice $layin" a "ame. Te coincidence o& e)ents is due to te im$ossiility o& $redictaility o& e)ents and te conce$t o& randomness is closely linked to te teory o& $roaility.
16#1 0ando" .u"/ers In !onic +i te metod rrand can e used &or "eneratin" random numers. ou can Dust try it. 5andom control Hans Gruendel 1*.0*.201# $lay rrand#0? 07 Code . -ethod rrand for generating random num#ers.
:en you run tis e/am$le? te note 60.-63 is dis$layed in te ,o" ;ieer. Altou" it is ell knon tat no 'II note it e/actly tis )alue e/ists. Tere are indeed only inte"er )alues. !onic +i ten rounded u$ &rom 60.-63 to sim$ly a 61. But tats not te only tin" to onder. E)en more amaJin" is tat te randomly "enerated numer is knon in ad)ance. :ats tat &or a coincidenceC
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
1
use_random_seed n
2
rrandr1? r27
3
rand r2
4
rrandMin1? n27
#
randMin1? n27
-eaning InitialiJes te random numer "enerator. From initialiJation de$ends te )alue tat is "enerated y te &olloin" random metods. +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 reates a random numer eteen 0 and 52? or i& no $arameter is used only eteen 0 and 1 :orks like rrand? oe)er? te random numer is alays an inte"er :orks like rand? oe)er? te random numer is alays an inte"er
Ta#le =. -ethods for generating random num#ers
"xample use_random_seed 5
slee$ rrand0.2? 0.#7
slee$ rand
$lay rrandMi40? #07
$lay randMi#07
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
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.
5andom Hans Gruendel 1*.0-.201#
de&ine 8myMloo$ do useMsynt 8t303 sam$le 8drumMassMard? rate8 rrand1? 27 slee$ 0.2# sam$le 8drumMassMard? rate8 rrand1? 37 sam$le 8drumMcymalMclosed? rate8 rrand0.#? 17 slee$ 0.2 sam$le 8drumMcymalMclosed? rate8 rrand1? 27 slee$ 0.3 sam$le 8drumMcymalMclosed? rate8 rrand2? 37 $lay coosecord8c2? 8maDor77? release8 1.#? cuto&&8 rrand60? 1107?am$80.2 slee$ 0.2# end
de&ine 8coir do 2.times do Vid/1V $anMaroundL =1 9 id/1W0.1 sam$le 8amiMcoir? rate8 1.1? am$8 0.4? $an8 $anMaround slee$ 0.2# id/1 9L 0.1 end 4.times do Vid/1V $anMaroundL =1 9 id/1W0.1 sam$le 8amiMcoir? rate8 rrand1? 1.27? am$8 1.#? $an8 $anMaround slee$ 0.# id/1 9L 0.1
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
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.
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.
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
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
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.# end end Code 11.inEthreads command code
<& course? te uestion arises eter $arallel treads are alays necessary? or en tey actually make sense. First o& all? tey are not necessary at all. !e)eral instruments can e coded itout treads. I& tones are to e $layed at te same time? ten one @$lay metod could &ollo anoter directly. Te @slee$ instructions you a)e to calculate accordin"ly. Te ad)anta"e is tat te tem$oral seuence o& sounds can e seen directly in your code. Te donside is te di&&iculty to e)aluate te @slee$ times and te increasin" com$le/ity ic makes it di&&icult to maintain te code.
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>.
Figure %>. Cue and Snc
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
: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 end Code 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.
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
inMtread do withEfx $echo do sam$le 8amiM$iano slee$ 6 end end 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
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
Figure %%. :elp for the distortion effect
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.#.
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 is 0.#.
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.
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 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. attenuation. Te de&ault )alue is 0.#. All e&&ects ork in !onic +i in te same manner. 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 $arameters o& all e&&ects. on)eniently on)eniently?? te )alues tat can take $arameters? $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 :en a son" consists o& $arallel $arts? i.e. &or e/am$le o& se)eral )oices? ten you sould structure tese in 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.
that should #e left istriute successi)e $arts tat elon" to"eter uite clear to se)eral treads.
13 :aria/ aria/les les and *rith"etic *rith"etic ;perati ;perations ons !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/l aria/les es 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. <& course it is $ossile to encode te a$$ro$riate numer o& @$lay instructions and to adDust eac tone. But its also a lot o& ork. For a sim$ler solution $lease take a look at ode 1#. Te 1#. Te crucial $oints a)e een marked old. First? tere is a kind o& euation? ic in reality is an assi"nment8 tone L 60 !o 2 are tin"s done. First? F irst? te )ariale is created it te name >tone>. !econdly? !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
tone H tone 1 end end Code 1. Jaria#les
Because te code contains a loo$ 10 tones are $layed.Fi"ure $layed. Fi"ure 23 sos te code editor and te lo" indo. indo. :en runnin" te code te )alue o& te )ariale increases. :it eac $layin" te tone $itc increases y 1.
Figure %'. Ja Jaria#les ria#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". $ro"rammin". tone L tone 9 1 is not ri"t &or any )alue tat is used in tone? de&initely. I&? 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 &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 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 247 it te out$ut o& te &olloin" error messa"e8 @unde&ined local )ariale or metod Xkneu
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. <& course you can use any meanin"&ul name. And you sould a)oid names tat a)e already een allocated y !onic +i suc as >useMsynt>. In te increased )ariale is )isile. Te lo" )ie sos a )alue o& 60 &or te &irst sound in te loo$. Tus >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
)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 )alue descriin" 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.
Figure %. Using controls to modif parameters
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 %4. 7rithmetic 5peration
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.
:it K Eco Hans Gruendel 20.0*.201# <$erators 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 8c3? 8e3? 8"3? 8c4? 8d3? 8&3? 8a3? 8d4U.rin" useMsynt 8&m $lay $lay allMtonestoneU end Code 1. 7ddition of a num#er and a varia#le
that should #e done ;ariales sould a)e sel&=e/$lanatory names. ames like >soundM$iano> are "ood.
that should #e left ;ariales sould a)e En"lis? sel&=e/$lanatory name. ames suc as >!1> or >Yla)ierMYlan"> 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.
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
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.
Figure %;. C maKor and C minor triads
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 %=. 7ll availa#le chords in Sonic Pi are visi#le after pressing the comma 0e.
: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 '>. The C maKor scale is plaed
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 a note? 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
: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.
Figure '1. An astonisin" numer o& scales are a)ailale in !onic +i
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 '%. 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
$lay num useMsynt 8tri $lay num end Code 1;. "xample of a function
In tis e/am$le? te &unction is called tice. num>. In te &unction de&inition? te )ariale is included in a )ertical ar >V>. Te trans&er o& only one )ariale is already uite $ractical. But o does te use o& &eatures in case you ant to additionally adDust tone len"t. !o ten you need 2 )ariales. ode 1- is e/actly tis e/am$le. Te $arameters are alays se$arated y a comma. Tis a$$lies ot to te &unction call? and &or te de&inition o& te &unction ere te to $arameter names are ten enclosed y te )ertical ars. Te assi"nment o& te )alues in te callin" &unction to te )ariales in te de&inition is done trou" te order8 Te &irst )alue is assi"ned to te &irst $arameter? te second )alue to te second $arameter? and so on. ;ariales =2 Hans Gruendel 2#.0*.201# inMtread do sound1 64? 1 slee$ 2 sound1 61? 3 slee$ 2 end de&ine 8sound1 do Vnum? len"tV useMsynt 8&m $lay num? release8 len"t useMsynt 8$rettyMell $lay num? release8 len"t useMsynt 8sine $lay num? release8 len"t useMsynt 8tri $lay num? release8 len"t
end Code 1=. Function with % varia#les
: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
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 end Code %1. Function and ring structure
that should #e done Functionality tat is used at se)eral locations sould e $acked in a &unction. Functions sould a)e a meanin"&ul name. Good style is to $ut in &ront o& eac &unction a comment tat e/$lains te &unction.
that should #e left
e)er $ut te same code to di&&erent locations.
,earned &or $ro"rammin" o 1
To$ic 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 &unction only as a command? ten tat is not enou".
2
;ariales are im$ortant. Tey sould alays e used instead o& numers. And tey sould a)e a meanin"&ul i.e. sel&=e/$lanatory7? name. Tere are 2 $laces to introduce )ariales8 First at te e"innin" o& a $ro"ram or &unction and second en tey are needed. And I tink te second o$tion is te etter one and I ould also recommend it. <$tion 1 makes only sense i& one $ro)ides a ole unc o& )ariales tat are initialiJed it )alues. Ta#le 1>$ (earned for programming.
1' Specific Ter"s of the $rogra""er 7rguments Te )alues $assed to a &unction to control te ea)ior o& it.
Code ode is e/ecutale te/t. Tis means tere is a $ro"ram tat uses te code and tere&ore does sometin". In !onic +i music is made? in oter $ro"rams calculations are $er&ormed or data are stored suitale.
Comments omments e/$lain te code and ill not e e/ecuted. Eac comment e"ins in !onic +i it a .
&efault In many so&tare $ro"rams de&aults are used. Tey are usually used en te associated control $arameters are not s$eci&ied.
"ditor In "eneral? an editor is a tool to rite in te/t and in te orld o& $ro"rammin" an editor is used to enter te code. In !onic +i is te editor $art o& a $ro"ram. Te editor o&ten $ro)ides additional &eatures suc as a color codin". Tat means? )arious com$onents o& te code are dis$layed in di&&erent colors. Anoter im$ortant &eature is te automatic code su$$lement. All tis $ro)ides !onic +i.
"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$>.
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.
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.................................................................................................1Fi"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"...................................................2Fi"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
Fi"ure 26. %sin" controls to modi&y $arameters...................................................................................44 Fi"ure 2. Aritmetic <$eration...........................................................................................................4# Fi"ure 2*. maDor and minor triads..................................................................................................4* Fi"ure 2-. All a)ailale cords in !onic +i are )isile a&ter $ressin" te comma key.............................4Fi"ure 30. Te maDor scale is $layed..................................................................................................#0 Fi"ure 31. An astonisin" numer o& scales are a)ailale in !onic +i....................................................#1 Fi"ure 32. Te &unction sound1............................................................................................................#2 Fi"ure 33. Te &unction sound1 is called in line - it to &e $arameters...........................................#4
Tables Tale 18 !im$le commands &or te &irst son" in !onic +i.........................................................................6 Tale 2. Te elements o& te !onic +i inter&ace...................................................................................... Tale 38 ,earned &or $ro"rammin" = $art 1.............................................................................................* Tale 4. 'odi&ication o& te $itc...........................................................................................................Tale #. 'II numerin"......................................................................................................................11 Tale 68 ,earned &or $ro"rammin" ( $art 2..........................................................................................14 Tale 8 ,earned &or $ro"rammin" = 2...................................................................................................1* Tale *. '5B commands......................................................................................................................21 Tale -. 'etods &or "eneratin" random numers..............................................................................24 Tale 108 ,earned &or $ro"rammin".....................................................................................................#6
Code ode 1. Te )ery &irst e/am$le...............................................................................................................# ode 2. 'idi e/am$le...........................................................................................................................10 ode 3. omments...............................................................................................................................12 ode 4. !on" 2......................................................................................................................................16