1. Music
Warning
1.1. Speaker V2
- speaker.off()
Use speaker.off() to turn off the speaker. This does not disable sound output to pin0.
- speaker.on()
Use speaker.on() to turn on the speaker.
1.2. Music Library
import music
at the top under from microbit import *
.from microbit import *
import music
1.3. Play notes
- music.play(notes, pin=pin0, wait=True, loop=False)
- Play the notes.Notes can be a string, such as ‘c1:4’, or a list of notes as strings, such as [‘c’, ‘d’, ‘e’]The duration and octave values are reset to their defaults (of 4 each) before the music is played.Octaves start on the note “C”.The output pin can be used to override the default pin0. pin=None prevents sounds from being played.If wait is set to True, playing is blocking, and the music will be played to the end.If loop is set to True, the music repeats until stop is called. Set wait to False to use this.
music.play(note)
to play a note given by the note
variable.from microbit import *
import music
note = 'c4:8'
music.play(note)
music.play(notes_list)
to play a list of notes in the notes_list
variable.from microbit import *
import music
notes_list = ['e4', 'f#', 'g', 'a', 'b', 'c5', 'd', 'e']
music.play(notes_list)
Tasks
Play the note e over and over again with 1 second between them.
Play the notes c, e, g over and over again with 1 second between replays.
Play the note e.
from microbit import *
import music
note = 'e'
while True:
music.play(note)
sleep(1000)
Play the notes c, e, g over and over again with 1 second between replays.
from microbit import *
import music
notes_list = ['c', 'e', 'g']
while True:
music.play(notes_list)
sleep(1000)
1.4. Notes
NOTE[octave][:duration]
.Tasks
Play the 5 notes: c, e, g, e, c in octave 4.
Starting with C in octave 4, play the next 6 notes that are each one semitone higher than the previous note. Use sharps as needed.
Starting with G flat in octave 5, play the next 6 notes that are each one semitone lower than the previous note. Use flats as needed.
Play the 5 notes: c, e, g, e, c in octave 4.
from microbit import *
import music
notes_list = ['c4:4', 'e', 'g', 'e', 'c']
while True:
music.play(notes_list)
sleep(1000)
Starting with C in octave 4, play the next 6 notes that are each one semitone higher than the previous note. Use sharps as needed.
from microbit import *
import music
notes_list = ['C4', 'C#', 'D', 'D#', 'E', 'F', 'F#']
while True:
music.play(notes_list)
sleep(1000)
Starting with G flat in octave 5, play the next 6 notes that are each one semitone lower than the previous note. Use flats as needed.
from microbit import *
import music
notes_list = ['Gb5', 'F', 'E', 'Eb', 'D', 'Db']
while True:
music.play(notes_list)
sleep(1000)
1.5. Tempo
- music.set_tempo(ticks=4, bpm=120)
- Sets the tempo for playback.The number of ticks, expressed as an integer, make a beat. The default is 4 ticks per beat.Each beat is to be played at a certain rate, beats per minute, expressed as an integer. The default is 120 bpm.
from microbit import *
import music
notes_list = ['e4', 'f#', 'g', 'a', 'b', 'c5', 'd', 'e']
music.set_tempo(ticks=4, bpm=120)
music.play(notes_list)
music.set_tempo(ticks=8, bpm=120)
music.play(notes_list)
music.set_tempo(ticks=8, bpm=240)
music.play(notes_list)
- music.get_tempo()
Gets the current tempo as a tuple of integers: (bpm, ticks).
from microbit import *
import music
music.set_tempo(ticks=4, bpm=240)
tempo_data = str(music.get_tempo())
display.scroll(tempo_data)
# (240, 4)
from microbit import *
import music
music.set_tempo(ticks=2, bpm=120)
tempo_data = music.get_tempo()
bpm = tempo_data[0]
ticks = tempo_data[1]
display.scroll(bpm)
display.scroll(ticks)
bpm, ticks = music.get_tempo()
.from microbit import *
import music
music.set_tempo(ticks=2, bpm=120)
bpm, ticks = music.get_tempo()
display.scroll(bpm)
display.scroll(ticks)
Tasks
Play the 5 notes: c, e, g, e, c with a tempo of 120, 180 and 240bpm without a for-loop.
Play the 3 notes: ‘e4:4’, ‘f#’, ‘g’ with a tempo of 120, 180 and 240bpm using a for-loop for the tempos.
Design a function that takes the list of 3 notes [‘e4:4’, ‘f#’, ‘g’] as one parameter; takes a tempo list of 120, 240, 360 , 480 and 600 bpm as a second parameter and a third parameter sleep_time with default value 1000. Use a repeat loop to set the tempo and play the notes_list.
Play the 5 notes: c, e, g, e, c with a tempo of 120, 180 and 240bpm without a for-loop.
from microbit import *
import music
notes_list = ['c4:4', 'e', 'g', 'e', 'c']
while True:
music.set_tempo(bpm=120)
music.play(notes_list)
sleep(1000)
music.set_tempo(bpm=180)
music.play(notes_list)
sleep(1000)
music.set_tempo(bpm=240)
music.play(notes_list)
sleep(1000)
Play the 3 notes: ‘e4:4’, ‘f#’, ‘g’ with a tempo of 120, 180 and 240bpm using a for-loop for the tempos.
from microbit import *
import music
notes_list = ['e4:4', 'f#', 'g']
while True:
for tempo in [120, 180, 240]:
music.set_tempo(bpm=tempo)
music.play(notes_list)
sleep(1000)
Design a function that takes the list of 3 notes [‘e4:4’, ‘f#’, ‘g’] as one parameter; takes a tempo list of 120, 240, 360 , 480 and 600 bpm as a second parameter and a third parameter sleep_time with default value 1000. Use a repeat loop to set the tempo and play the notes_list.
from microbit import *
import music
notes_list = ['e4:4', 'f#', 'g']
tempo_list = [120, 240, 360, 480, 600]
def tempo_play(notes_list, tempo_list, sleep_time=1000):
for tempo in tempo_list:
music.set_tempo(bpm=tempo)
music.play(notes_list)
sleep(sleep_time)
while True:
tempo_play(notes_list, tempo_list, sleep_time=1000)
1.6. Volume V2
- music.set_volume(volume)
- Configure the output volume of the microbit speaker and pins.volume is an integer between 0 and 255.
from microbit import *
import music
note = "c4:2"
while True:
set_volume(255)
music.play(note)
set_volume(128)
music.play(note)
set_volume(64)
music.play(note)
Tasks
Instead of playing the same note each time, play a different note “c4:2”, “e4:2”, “f#4:2” at a different volume.
Put the 3 sound levels in a list and use a for-loop to set the volume and play the note “c4:2”.
To the previous task, add the ability to stop the playing by exiting the
while True
loop on pressing the A-button via the use ofbreak
. Pressing the reset button on the back of the microbit will restart the code.
Instead of playing the same note each time, play a different note “c4:2”, “e4:2”, “f#4:2” at a different volume.
from microbit import *
import music
note0 = "c4:2"
note1 = "e4:2"
note2 = "f#4:2"
while True:
set_volume(255)
music.play(note0)
set_volume(128)
music.play(note1)
set_volume(64)
music.play(note2)
Put the 3 sound levels in a list and use a for-loop to set the volume and play the note “c4:2”.
from microbit import *
import music
note = "c4:2"
volumes = [255, 125, 64]
while True:
for v in volumes:
set_volume(v)
music.play(note)
To the previous task, add the ability to stop the playing by exiting the while True
loop on pressing the A-button via the use of break
. Pressing the reset button on the back of the microbit will restart the code.
from microbit import *
import music
note = "c4:2"
volumes = [255, 125, 64]
while True:
for v in volumes:
set_volume(v)
music.play(note)
if button_a.was_pressed():
break
1.7. Stop background music
- music.stop(pin=pin0)
- Stops all music playback on the built-in speaker and any pin outputting sound.An optional argument can be provided to specify a pin, eg. music.stop(pin=pin1).
wait=False
so that the music plays in the background.loop=True
so that the music loops forever.from microbit import *
import music
# Define the melody
melody1 = ['C4:4', 'C4:4', 'G4:4', 'G4:4',
'A4:4', 'A4:4', 'G4:8',
'F4:4', 'F4:4', 'E4:4', 'E4:4',
'D4:4', 'D4:4', 'C4:8']
# Play the melody
music.play(melody1, wait=False, loop=True)
display.scroll("A", wait=False, loop=True)
while True:
# Allow 2 seconds to choose to press the A-button
sleep(2000)
if button_a.was_pressed():
# Stop the melody
music.stop()
# exit the while loop
break
display.scroll("THE END")
Tasks
Add a rest equivalent to 4 crotchets to the end of the melody above so it provides a pause equivalent to one bar as the melody loops.
Modify the example above to have 2 melodies: melody1 = [‘E4:4’, ‘D:4’, ‘C:4’, ‘D:4’, ‘E:4’, ‘E:4’, ‘E:8’, ‘D:4’, ‘D:4’, ‘D:8’, ‘E:4’, ‘G:4’, ‘G:8’] and melody2 = [‘E4:4’, ‘D:4’, ‘C:4’, ‘D:4’, ‘E:4’, ‘E:4’, ‘E:4’, ‘E:4’, ‘D:4’, ‘D:4’, ‘E:4’, ‘D:4’, ‘C:16’]. Firstly, melody1 loops and can be stopped by the A-button. Then melody2 loops and can be stopped by the B-button.
Instead of playing the same note each time, play a different note “c4:2”, “e4:2”,”f#4:2” for a different volume.
from microbit import *
import music
# Define the melody
melody1 = ['C4:4', 'C4:4', 'G4:4', 'G4:4',
'A4:4', 'A4:4', 'G4:8',
'F4:4', 'F4:4', 'E4:4', 'E4:4',
'D4:4', 'D4:4', 'C4:8',
'R:16']
# Play the melody
music.play(melody1, wait=False, loop=True)
display.scroll("A", wait=False, loop=True)
while True: # Allow 2 seconds to choose to press the A-button
sleep(2000)
if button_a.was_pressed():
# Stop the melody
music.stop()
break
display.scroll("THE END")
Modify the example above to have 2 melodies: melody1 = [‘E4:4’, ‘D:4’, ‘C:4’, ‘D:4’, ‘E:4’, ‘E:4’, ‘E:8’, ‘D:4’, ‘D:4’, ‘D:8’, ‘E:4’, ‘G:4’, ‘G:8’] and melody2 = [‘E4:4’, ‘D:4’, ‘C:4’, ‘D:4’, ‘E:4’, ‘E:4’, ‘E:4’, ‘E:4’, ‘D:4’, ‘D:4’, ‘E:4’, ‘D:4’, ‘C:16’]. Firstly, melody1 loops and can be stopped by the A-button. Then melody2 loops and can be stopped by the B-button.
from microbit import *
import music
# Define the first melody
melody1 = ['E4:4', 'D:4', 'C:4', 'D:4',
'E:4', 'E:4', 'E:8',
'D:4', 'D:4', 'D:8',
'E:4', 'G:4', 'G:8']
# Define the second melody
melody2 = ['E4:4', 'D:4', 'C:4', 'D:4',
'E:4', 'E:4', 'E:4', 'E:4',
'D:4', 'D:4', 'E:4', 'D:4',
'C:16']
# Play the first melody
music.play(melody1, wait=False, loop=True)
display.scroll("A", wait=False, loop=True)
while True: # Allow 2 seconds to choose to press the A-button
sleep(2000)
if button_a.was_pressed():
# Stop the first melody
music.stop()
break
# Play the second melody
music.play(melody2, wait=False, loop=True)
display.scroll("B", wait=False, loop=True)
while True: # Allow 2 seconds to choose to press the B-button
sleep(2000)
if button_b.was_pressed():
# Stop the second melody
music.stop()
break
display.scroll("THE END")
1.8. Reset music
- music.reset()
- Resets the state of the following attributes as listed:ticks = 4; bpm = 120; duration = 4; octave = 4
1.9. Custom tunes
from microbit import *
import music
scale = {"name": "Scale", "notes": "C5 B A G F E D C", "tempo": 120}
reverse = {"name": "Reverse", "notes": "C D E F G A B C5", "tempo": 120}
mystery = {"name": "Mystery", "notes": "E B C5 A B G A F", "tempo": 120}
gilroy = {"name": "Gilroy", "notes": "A F E F D G E F", "tempo": 120}
falling = {"name": "Falling", "notes": "C5 A B G A F G E", "tempo": 120}
hopeful = {"name": "Hopeful", "notes": "G B A G C5 B A B", "tempo": 120}
tokyo = {"name": "Tokyo", "notes": "B A G A G F A C5", "tempo": 120}
paris = {"name": "Paris", "notes": "G F G A - F E D", "tempo": 120}
rising = {"name": "Rising", "notes": "E D G F B A C5 B", "tempo": 120}
sitka = {"name": "Sitka", "notes": "C5 G B A F A C5 B", "tempo": 120}
Challenge
Play each of the custom dictionaries notes.
Play each of the custom dictionaries notes.
from microbit import *
import music
scale = {"name": "Scale", "notes": "C5 B A G F E D C", "tempo": 240}
reverse = {"name": "Reverse", "notes": "C D E F G A B C5", "tempo": 240}
mystery = {"name": "Mystery", "notes": "E B C5 A B G A F", "tempo": 120}
gilroy = {"name": "Gilroy", "notes": "A F E F D G E F", "tempo": 120}
falling = {"name": "Falling", "notes": "C5 A B G A F G E", "tempo": 180}
hopeful = {"name": "Hopeful", "notes": "G B A G C5 B A B", "tempo": 180}
tokyo = {"name": "Tokyo", "notes": "B A G A G F A C5", "tempo": 180}
paris = {"name": "Paris", "notes": "G F G A r F E D", "tempo": 180}
rising = {"name": "Rising", "notes": "E D G F B A C5 B", "tempo": 180}
sitka = {"name": "Sitka", "notes": "C5 G B A F A C5 B", "tempo": 180}
# List of scales
scales = [scale, reverse, mystery, gilroy, falling, hopeful, tokyo, paris, rising, sitka]
# Loop over each scale
for selected_scale in scales:
# Parse the notes and tempo from the selected scale
notes = selected_scale["notes"].split(" ")
tempo = selected_scale["tempo"]
music.set_tempo(ticks=8, bpm=tempo)
music.play(notes, wait=True)
sleep(1000)