Introduction: Piano Pi - Play a Duet With Your Pi!
In this tutorial you will learn a simple way to program your Pi using Sonic Pi to create a piano accompaniment.
Remember the piano scene in the move Big? You will be programming the bass part of the song they play "Heart and Soul".
You will need:
Monitor, keyboard and mouse.
Speakers
Piano
Step 1: Open Sonic Pi on Jessie Raspbian
Pi comes pre-installed with the NOOBS card.
Navigate to Menu >> Programming >> Sonic Pi
Step 2: Getting Familiar With Sonic Pi
If this is your first time using Sonic Pi, click the "Help" button and walk through the first couple lessons to get familiar with the platform.
I went ahead and changed the editor to a dark screen by clicking on the "Prefs" button. You may also adjust the volume there.
At the bottom of the editor you will see "Buffer0" - "Buffer9". Here you can switch between code windows to create unique sounds and music for each Buffer. Find an empty Buffer to begin this project.
Step 3: Midi Numbers
Sonic Pi uses the Midi number system where each key on the piano has a number.
Middle C is 60. To begin the song, C is played twice. If you don't want notes to be played at the same time you must separate the notes with a rest or sleep. This is the duration of time in seconds before the next note is played. *Note a zero must precede the decimal if sleep is less than a second or you will get an error.
play 60
sleep 0.4
play 60
sleep 0.3
The next 3 notes will be played as a chord twice with a sleep in between each chord:
play 72
play 76
play 79
sleep 0.4
play 72
play 76
play 79
sleep 0.3
Go ahead and press the "Run ►" with your speakers on to hear your song so far!
Step 4: Second Verse, Same As the First!
The accompaniment on this piece repeats itself again and again. With Sonic Pi you can easily wrap the code you want repeated in a loop with the key words do and end:
loop do
play 60
...(lots of code)
sleep 0.4
end
Be careful though, using the word loop creates an infinite loop and the song will repeat until you press the "Stop ■" button. To limit the number of times the loop runs, simply type the number followed by .times:
5.times do
play 60
...(more code)
sleep 0.4
end