Hardware Reference
In-Depth Information
CHALLENGE
Can you recreate the rest of Twinkle Twinkle Little Star in code by translating
the following musical notes into MIDI note numbers? Refer to the table at
http://computermusicresource.com/midikeys.html to match the
notes to the correct numbers.
Each line could be put into a play_pattern [ ] data structure:
C C G G A A G (You have already written this part of the song)
F F E E D D C
G G F F E E D
G G F F E E D
C C G G A A G
F F E E D D C
Can you translate another song into MIDI notes and recreate it using Sonic Pi?
For one more challenge, try introducing variables to define the notes. For example:
C=60
D=62
play_pattern[C,C,G,G,A,A,D]
All the code between do and end is repeated; in this case, 2.times tells the pro-
gram to play it twice. You will see that the colour of the words do and end have
automatically changed to blue and are bold, as in Figure 7-8. Programs that use colours
for syntax highlighting in this way make it easier for you to read your code. In this
example it is important that the code you want to repeat is between do and end , so
Sonic Pi highlights those words to show you this.
You could change the value 2 to make the loop repeat more times. For example, if you
wanted to play the line five times you would type 5.times do , followed by the code
you want repeated, and then end .
Your Twinkle, Twinkle tune might not sound the way you expect it to sound. Can you
figure out what you may need to add to improve it?
A
It is good practice to indent your code when you create loops in code. This will
make it easier to read, especially if you are searching for a bug to fix to make
your music play. All code between do and end should be indented. Press the
spacebar on your keyboard twice to indent each line two spaces.
Search WWH ::




Custom Search