"Frere Jacques"
https://python.microbit.org/v/3/ideas/fr%C3%A8re-jacques
from microbit import *
import music
passage1 =["c4:4", "d4:4", "e4:4", "c4:4"]
passage 2= ["e4:4", "f4:4", "g4:8"]
passage3 =["g4:2","a4:2","g4:2", "f4:2","e4:4","c4:4"]
passage4 =["c4:4","g3:4","c4:8"]
for x in range(2):
music.play(passage1)
for x in range(2):
music.play(passage2)
for x in range(2):
music.play(passage3)
for x in range(2):
music.play(passage4)
import music
Once you have imported the music library, it is possible to programme your device to play notes (or tones).
In Micro Python, we use lists to play a sequence of notes. Here is the code for the opening bars of ‘Frere Jaques’:
tune = ["c4:4", "d4:4", "e4:4", "c4:4", "c4:4", "d4:4", "e4:4", "c4:4", "e4:4", "f4:4", "g4:8", "e4:4", "f4:4", "g4:8"]
Each note is expressed as a string of characters like this:
“c4:4”
Where C = note, 4 = octave and :4 = duration (beat or ticks). Note names are case sensitive.
Example
Try the following example:
from microbit import *
import music
tune = ["c4:4", "d4:4", "e4:4", "c4:4", "c4:4", "d4:4", "e4:4", "c4:4", "e4:4", "f4:4", "g4:8", "e4:4", "f4:4", "g4:8"]
music.play(tune)
Did you recognise the tune?
Transcribing songs from sheet music
If we want to re-create our favourite songs on our micro:bit, we first need a basic understanding of sheet music.
Here’s a reminder of the most common notes used in a musical score: