Home

How to Use Note and Chord Names to Make Music on Your Raspberry Pi

|
|  Updated:  
2017-11-07 12:57:44
Getting Started with Raspberry Pi
Explore Book
Buy On Amazon
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

play :c4

To play the B one note before it, which is in the next lowest octave, you would use

play :b3

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.

MIDI Notes
Note 0 1 2 3 4 5 6 7 8 9
C 0 12 24 36 48 60 72 84 96 108 120
C# 1 13 25 37 49 61 73 85 97 109 121
D 2 14 26 38 50 62 74 86 98 110 122
D# 3 15 27 39 51 63 75 87 99 111 123
E 4 16 28 40 52 64 76 88 100 112 124
F 5 17 29 41 53 65 77 89 101 113 125
F# 6 18 30 42 54 66 78 90 102 114 126
G 7 19 31 43 55 67 79 91 103 115 127
G# 8 20 32 44 56 68 80 92 104 116
A 9 21 33 45 57 69 81 93 105 117
A# 10 22 34 46 58 70 82 94 106 118
B 11 23 35 47 59 71 83 95 107 119

Here's how you could code a fanfare using note names instead of numbers:

play :c4

sleep 0.5

play :e4

sleep 0.5

play :g4

sleep 0.5

play :c5

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 (play :cb4).

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 :major, :minor, or :diminished). There are also options for :major7, :minor7, :diminished7, and :dom7, among others. For a complete list, click Lang in the Help pane, and then select Chord. Try this:

play chord(:a3, :major)

sleep 1

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)

About This Article

This article is from the book: 

About the book author:

Mike Cook is a former professor in physics at Manchester Metropolitan University. His other books include Raspberry Pi Projects and Raspberry Pi Projects For Dummies.

Sean McManus is an expert technology and business author. His previous books include Mission Python, Coder Academy, and Cool Scratch Projects in Easy Steps.