How to Use Note and Chord Names to Make Music on Your Raspberry Pi
Sonic Pi enables you to use proper note names instead of MIDI numbers to play music on the RaspberryPi by using the name of the note (a letter from A to G), plus the number of the octave it’s in.
For example, to play a middle C, you can use
To play the B one note before it, which is in the next lowest octave, you would use
The Log shows that Sonic Pi plays notes 60 and 59 respectively. You can check the note names and numbers to confirm that this is what you expected.
Here’s how you could code a fanfare using note names instead of numbers:
If you want to use a sharp note, insert the letter s in the note name (for example,
play :cs4) and use b for a flat note (
You can also use names to play chords. You tell Sonic Pi the lowest note in the chord and add which type of chord you want (try
:diminished). There are also options for
:dom7, among others. For a complete list, click Lang in the Help pane, and then select Chord. Try this:
play chord(:a3, :major)
play chord(:a3, :minor)
In each case, it plays three notes at the same time. If you look at the note numbers in the Log, you can see that the middle note was one pitch lower in the second chord because it’s a minor chord. Again, you can use Table 14-1 to check the note numbers Sonic Pi displays against the musical note names.
The chord is returned as a list, and you can use
play_pattern to hear the notes of the chord in a sequence, like this:
play_pattern chord(:a3, :major)
play_pattern chord(:a3, :minor)