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.
|
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.
|
Figure 23‑5 Sonic Pi workspace
Now try again to type the following and press [Run] button to execute it.
|
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.
|
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.
|
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.
|
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.
|
Finally, enter the following and press [Run] button to execute it. You can see that the entire contents are played twice.
|
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.
|
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.
|
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.
|
The example below is doing task using synth "fm".
|
■ 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.
|
In the example below, we worked with a sample called "loop_amen".
|
■ 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".
|
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.
|
|
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.
|
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.
|
Next, change another part of the code and try playing again. Then new music is played. Now you are playing really cool.