|
1 | 1 | # Live Coding |
| 2 | + |
| 3 | +One of the most exciting aspects of Sonic Pi is that it enables you to |
| 4 | +write and *modify code live* to make music, just like you might perform |
| 5 | +live with a guitar. This means that given some practice you can take |
| 6 | +Sonic Pi on stage and gig with it. |
| 7 | + |
| 8 | +## Free your mind |
| 9 | + |
| 10 | +Before we get into the real details of how Sonic Pi works in this |
| 11 | +tutorial, I'd like to give you an experience of what it's like to live |
| 12 | +code. Don't worry if you don't understand much (or any) of this. Just |
| 13 | +try to hold onto your seats and enjoy... |
| 14 | + |
| 15 | +## A live loop |
| 16 | + |
| 17 | +Let's get started, copy the folling code into an empty workspace above: |
| 18 | + |
| 19 | +``` |
| 20 | +live_loop :flibble do |
| 21 | + sample :bd_haus, rate: 1 |
| 22 | + sleep 0.5 |
| 23 | +end |
| 24 | +``` |
| 25 | + |
| 26 | +Now, press the `Run` button and you'll hear a nice fast bass drum |
| 27 | +beating away. Now, whilst it's still running, change the sleep |
| 28 | +value. Try something higher like `1`. Press the `Run` button again - |
| 29 | +notice how the drum speed has changed. Also, *remember this moment*, this |
| 30 | +is the first time you've live coded with Sonic Pi and it's unlikely to |
| 31 | +be your last... |
| 32 | + |
| 33 | +Let's add something else into the mix. Above `sample :bd_haus` add the |
| 34 | +line `sample :ambi_choir, rate: 0.3`. Your code should look like this: |
| 35 | + |
| 36 | + |
| 37 | +``` |
| 38 | +live_loop :flibble do |
| 39 | + sample :ambi_choir, rate: 0.3 |
| 40 | + sample :bd_haus, rate: 1 |
| 41 | + sleep 1 |
| 42 | +end |
| 43 | +``` |
| 44 | + |
| 45 | +Now, play around. Change the rates - what happens when you use high |
| 46 | +values, or small values? What happens if you choose a really small |
| 47 | +`sleep` value? Can you make it go so fast your computer can't keep up? |
| 48 | + |
| 49 | +Try commenting one of the `sample` lines out by adding a `#` to the |
| 50 | +beginning: |
| 51 | + |
| 52 | +``` |
| 53 | +live_loop :flibble do |
| 54 | + sample :ambi_choir, rate: 0.3 |
| 55 | +# sample :bd_haus, rate: 1 |
| 56 | + sleep 1 |
| 57 | +end |
| 58 | +
|
| 59 | +``` |
| 60 | + |
| 61 | +Notice how it tells the computer to ignore it, so we don't hear it. This |
| 62 | +is called a comment. In Sonic Pi we can use comments to remove and add |
| 63 | +things into the mix. |
| 64 | + |
| 65 | +Finally, let me leave you something to play with. Take the code below, |
| 66 | +and copy it into a spare workspace. Now, don't try to understand it too |
| 67 | +much other than see that there are two loops - so two things going round |
| 68 | +at the same time. Now, do what you do best - experiment and play |
| 69 | +around. Here are some suggestions |
| 70 | + |
| 71 | +* Try changing the blue `rate:` values to hear the sample sound change |
| 72 | +* Try changing the `sleep` times and hear that both loops can spin round at |
| 73 | +different rates. |
| 74 | +* Try uncommenting the two sample lines and enjoy the sound of the |
| 75 | +guitar played backwards. |
| 76 | +* Try changing any of the blue numbers |
| 77 | + |
| 78 | + |
| 79 | +Remember to press `Run` and you'll hear the change next time the loop |
| 80 | +goes round. If you end up in a pickle, don't worry - hit `Stop`, delete |
| 81 | +the code in the workspace and paste a fresh copy in and you're ready to |
| 82 | +jam again. Making mistakes is how you'll learn the quickest... |
| 83 | + |
| 84 | + |
| 85 | +``` |
| 86 | +live_loop :guit do |
| 87 | + with_fx :flanger, mix: 1, depth: 5 do |
| 88 | + sample :guit_em9, rate: 0.5 |
| 89 | + end |
| 90 | +# sample :guit_em9, rate: -0.5 |
| 91 | +# sample :guit_em9, rate: -0.25 |
| 92 | + sleep 8 |
| 93 | +end |
| 94 | +
|
| 95 | +live_loop :boom do |
| 96 | + with_fx :reverb, room: 0.9 do |
| 97 | + sample :bd_boom, amp: 10, rate: 1 |
| 98 | + end |
| 99 | + sleep 8 |
| 100 | +end |
| 101 | +``` |
| 102 | + |
0 commit comments