Raspberry Pi_Eng_23.3.3 How to Use Sonic Pi


Published Book on Amazon


All of IOT Starting with the Latest Raspberry Pi from Beginner to Advanced – Volume 1
All of IOT Starting with the Latest Raspberry Pi from Beginner to Advanced – Volume 2


출판된 한글판 도서


최신 라즈베리파이(Raspberry Pi)로 시작하는 사물인터넷(IOT)의 모든 것 – 초보에서 고급까지 (상)
최신 라즈베리파이(Raspberry Pi)로 시작하는 사물인터넷(IOT)의 모든 것 – 초보에서 고급까지 (하)


Original Book Contents


23.3.3  How to Use Sonic Pi

 

23.3.3.1    Step 1 - First Sounds with Sonic Pi

 

First, select [Workspace 1] and enter the following. Then click [Run] button. What happens one day? Then you will hear a beep sound.

 

play 60

 

At this time, type the following and press [Run] button. This time you will hear no sound and you will see the area to display error message below the workspace. This indicates that there is an error when the entered command is wrong.

 

pley 60


Figure 23‑5 Sonic Pi workspace

 

Now try again to type the following and press [Run] button to execute it.

 

play 60
play 67
play 69

 

Then the computer will play a note in sequence one by one, but they sound so fast that they sound like they are playing at the same time, so it is difficult to distinguish exactly what they are. So let's try to distinguish between each note with some pause time. To do this, you can use the following command after each "play" statement.

 

sleep 1

 

The number specified after "sleep" statement represents time in seconds. Number "1" means 1 second, and you can use a decimal point like "0.5". Now, to create a clean tune,  type the following with "play" and "sleep" statements, and press [Run] button to listen to the sound.

 

play 60
sleep 0.5
play 67
sleep 0.5
play 69
sleep 0.5

 


 

23.3.3.2    Step 2 - Loop a Tune

 

Now that you have studied the basics of Sonic Pi, let's write some code to create a tune. Select [Workspace 2] and enter the following code.

 

play 60
sleep 0.5
play 62
sleep 0.5
play 64
sleep 0.5
play 60
sleep 0.5

 

When you press [Run] button, it play a tune. By the way, if this part of a song needs to be played twice, how can you repeat it twice? The simplest way would be to type the sentence twice. There is another way to do it, which is the loop processing we are going to explain.  Enter "do" "end" statement as follows, enter all of the above commands between "do" statement and "end" statement.

 

2.times do
     statement
end

 

Finally, enter the following and press [Run] button to execute it. You can see that the entire contents are played twice.

 

2.times do
  play 60
  sleep 0.5
  play 62
  sleep 0.5
  play 64
  sleep 0.5
  play 60
  sleep 0.5
end

 

In the above example, the sentences between "do" "end" are indented on the right. This is intended to better align the program code so that you can read the program code easier and easily find the bug if there is an error in running it later. You can just double-click on the space bar to indent a line of code.

 

In the previous example, we used to repeat the notes by specifying a repetition number, but if you want to repeat forever, use "loop do" and "end" statements. In other words, use the following format.

 

loop do
  play 60
  sleep 0.5
end

 


 

23.3.3.3    Step 3 - MIDI Note and Music Note

 

The numerical value we specify after "play" statement in front is a certain note. In fact they are MIDI notes. This means we need to convert it into a Sonic Pi MIDI note to play a song that is played on the piano. There are the following relations between two notes.

 

Music Note

C

D

E

F

G

A

B

MIDI Note

60

62

64

65

67

69

71

Table 23‑1 Relationship between Music note and MIDI note value

 

If you want to create a MIDI note for a song you want to play, it will be very annoying. Fortunately, Sonic Pi allows you to use standard sheet music notation too.

 

This time, let's type the followings in new workspace. Then try playing and checking if it is  same as the one you wrote with the previous MIDI note.

 

play :c4
sleep 0.5
play :d4
sleep 0.5
play :e4
sleep 0.5
play :c4
sleep 0.5

 


 

23.3.3.4    Change Sound Using Synthesizer

 

Now it is time to make the music tune sound more interesting. We can make these sounds by changing the synthesizer sound. The default synth on Sonic Pi is called beep. To use a different synth, you can use a statement of the following format above the sequence of code where you want to use it.

 

use_synth :name of synth

 

The example below is doing task using synth "fm".

 

use_synth :fm
2.times do
  play 60
  sleep 0.5
  play 67
  sleep 0.5
end

 

 

    Available Synths

 

Sonic Pi includes a number of clean synths in advance. To view its contents, press [Help] button on the top of the screen to display [Help] window, and select [Synths] from the tab on the left. Then click on the desired synth name for more information on how to use it.


 

 


 

23.3.3.5    Step 5 - Use Samples

 

In Sonic Pi, you can create music using only notes, or use samples to create music. Sample is a pre-recorded music or tune, and can be used directly putting it in the music. This makes it very simple to make your music sound amazing!.

 

To use the sample, add the following code statement in the sequence of  your music program you want to use it.

 

sample :name of sample

 

In the example below, we worked with a sample called "loop_amen".

 

2.times do
  sample :loop_amen
  sleep 1.753
end

 

    Available samples

 

Sonic Pi contains many samples. To see what is there, press [Help] button on the top of the screen to display [Help] window, and select [Samples] from the tab to the left. Then click on the name of the desired sample for more information on how to use it.


 

23.3.3.6    Step 6 - Playing Two Tunes at the Same Time

 

Music often has a repeating backing track with a separate melody played over the top. We have played only one tune so far, and now we will try to play two tune at the same time.

 

Open a new workspice to work on. When you play two tunes simultaneously, the code you want to use must be between "in_thread do" and "end" statement. Enter the desired tune under "in_thread do".

 

in_thread do
   codes for tune
end

 

Here, we used the sample as a backing track. This first '"hread" will work as a melody of our music. Below it, enter the code to be used as backing track or baseline as follows.

 

in_thread do
  loop do
    sample :loop_amen
    sleep 1.753
  end
end

 

in_thread do
  16.times do
    play 75
    sleep 1.753
    play 74
    sleep 0.25
  end
end

 

When all the works are completed, press [Run] button to start playing. You will then hear two threads playing at the same time.


 

23.3.3.7    Step 7 - Live Code!

 

Sonic Pi was developed as a live coding platform of music, so the code can be manipulated, changed, and adapted in real time. This means that coder can play their own songs rather than play pre-made programs. So, should not we try it?

 

In the new workspace, type the following.

 

define :play_my_synth do
  use_synth :prophet
    play 50, attack: 0.2, release: 1.3
  sleep 0.5
end
 
loop do
play_my_synth
end

 

Then try to play the following program. While playing, make a comment by typing "#" symbol at the beginning of each line as shown below. Check how the performance changes.

 

# loop do
#   play_my_synth
# end

 

Next, change another part of the code and try playing again. Then new music is played. Now you are playing really cool.