Raspberry Pi-SonicPi (sound programming)
Sonic Pi is an open-source programming environment designed to explore and teach programming concepts by creating sound production music processes.
The Code executed by Sonic Pi is based on Ruby, which is a beautiful and concise programming language. This means that you can write a lot of code without having to worry too much about syntax and braces-though important for complex programs.
1. getting started (start learning)
You can find the Sonic Pi program in Education of applications menu of Raspbin system. Open this program and you will see a window similar to the following:
This is the Sonic Pi program interface. The window contains three parts. The biggest part is where you write code. We call it the programming panel. The control window in the upper-right corner is the output panel, which displays information about the execution process of your program. The window in the lower right is the error panel. When there is an error in your code, the error message is displayed here.
2. MAKING SOUNDS)
Enter the following content in Workspace 1:
play 60
Click Play now, and the note will be executed. MIDI sets the value 60 to C.
TryPlay 60ChangePley 60Check for any errors.
Enter the following code:
play 60play 67play 69
Click the Play button once.
These notes are executed so quickly that they sound as if they were simultaneously voiced. Use sleep to add pause between notes:
play 60sleep 1play 67sleep 2play 69
3. a tune: frere jacques (A Song: jaces)
The start of this song is:
C d e c or a 60 62 64 60 Note in MIDI.
Comparison of notes and MIDI notes:
C |
D |
E |
F |
G |
A |
B |
60 |
62 |
64 |
65 |
67 |
69 |
71 |
The following is a piece of music:
play 60sleep 0.5play 62sleep 0.5play 64sleep 0.5play 60sleep 0.5
4. LOOPING)
To execute a series of commands repeatedly, you can use a loop. The following is an example of a Ruby language loop:
2.times do play 60 sleep 0.5 play 62 sleep 0.5 play 64 sleep 0.5 play 60 sleep 0.5end
5. FUNCTIONS (method)
You can define a series of executions in a method. You can call this method multiple times in the future to avoid copying and pasting multiple lines of code:
def frere play 60 sleep 0.5 play 62 sleep 0.5 play 64 sleep 0.5 play 60 sleep 0.5end
You can enter frere to call this method later. For example, call in a loop:
4.times do frereend
6. SYNTHS (synthesizer)
The synthesizer enables the play method to make different sound effects. The default synthesizer is "pretty_bell", but you can change it by yourself:
"pretty_bell""dull_bell""beep""saw_beep""fm"
Try different synthesizer:
with_synth "fm"2.times do play 60 sleep 0.5 play 62 sleep 0.5end
7. THREADS (thread)
You can use a thread to play two songs at the same time. Similar to a loop, it is also a code block ending with the end Keyword:
in_thread do with_synth "saw_beep" 2.times do play 60 sleep 0.5 play 67 sleep 0.5 endend
8. You can use multiple WORKSPACES in the Sonic Pi program window. This means you can try your code block in other workspaces without deleting the current code. We recommend that you use other workspaces to try the code for implementation or sandbox testing. 9. If you choose to save sonic pi files as FILES, you can return them or share them with others later. The Sonic Pi file is a simple text file, so you can view and edit it on another computer, or run it on another Raspberry Pi.