8. Music_advanced
8.1. Random notes Task
Tasks
Random notes Scaffold:
from microbit import * import random import music notes = ['c', 'd', 'e', 'f', 'g', 'a', 'b'] octaves = [3, 4, 5] durations = [1, 2, 4, 8] def get_random_note(notes, octaves, durations): note = random.choice(......) # convert numbers to strings so they can be joined octave = str(random.choice(.......)) duration = str(random.choice(.......)) full_note = ..... + ...... + ":" + ...... return ....... while True: random_note = get_random_note(....., ......, ...... ) music.play(.....)
from microbit import *
import random
import music
notes = ['c', 'd', 'e', 'f', 'g', 'a', 'b']
octaves = [3, 4, 5]
durations = [1, 2, 4, 8]
def get_random_note(notes, octaves, durations):
note = random.choice(notes)
# convert numbers to strings so they can be joined
octave = str(random.choice(octaves))
duration = str(random.choice(durations))
full_note = note + octave + ":" + duration
return full_note
while True:
random_note = get_random_note(notes, octaves, durations)
music.play(random_note)
Tasks
Include a note modification parameter to sharpen, flatten or leave the note unchanged.
Create a function, get_random_notes, to return a random list of notes, given the number of notes. Use a for-loop to add the random notes to a list within the function. Play the notes when the A-button is pressed.
Rewrite the function, get_random_notes, to use list comprehension.
Include a note modification parameter to sharpen, flatten or leave the note unchanged.
from microbit import *
import random
import music
notes = ["c", "d", "e", "f", "g", "a", "b"]
modifiers = ["#", "b", ""]
octaves = [3, 4, 4, 4, 4, 5]
durations = [2, 3, 4, 2, 3, 4, 2, 3, 4, 8]
def get_random_note(notes, modifiers, octaves, durations):
note = random.choice(notes)
modifier = random.choice(modifiers)
octave = str(random.choice(octaves))
duration = str(random.choice(durations))
full_note = note + modifier + octave + ":" + duration
return full_note
while True:
random_note = get_random_note(notes, modifiers, octaves, durations)
music.play(random_note)
Create a function, get_random_notes, to return a random list of notes, given the number of notes. Use a for-loop to add the random notes to a list within the function. Play the notes when the A-button is pressed.
from microbit import *
import random
import music
notes = ["c", "d", "e", "f", "g", "a", "b"]
octaves = [3, 4, 4, 4, 4, 5]
durations = [2, 3, 4, 2, 3, 4, 2, 3, 4, 8]
def get_random_note(notes, octaves, durations):
note = random.choice(notes)
octave = str(random.choice(octaves))
duration = str(random.choice(durations))
full_note = note + octave + ":" + duration
return full_note
def get_random_notes(note_count):
random_notes = []
for i in range(note_count):
random_notes.append(get_random_note(notes, octaves, durations))
return random_notes
while True:
if button_a.is_pressed():
random_notes = get_random_notes(10)
music.play(random_notes)
sleep(2000)
Rewrite the function, get_random_notes, to use list comprehension.
from microbit import *
import random
import music
notes = ["c", "d", "e", "f", "g", "a", "b"]
octaves = [3, 4, 4, 4, 4, 5]
durations = [2, 3, 4, 2, 3, 4, 2, 3, 4, 8]
def get_random_note(notes, octaves, durations):
note = random.choice(notes)
octave = str(random.choice(octaves))
duration = str(random.choice(durations))
full_note = note + octave + ":" + duration
return full_note
def get_random_notes(note_count):
return [get_random_note(notes, octaves, durations) for i in range(note_count)]
while True:
random_notes = get_random_notes(10)
music.play(random_notes)
sleep(2000)
8.2. Scales generator
from microbit import *
import music
def get_scale_steps(key_type):
major_steps = [2, ......, 1]
minor_steps = [2, ......, 2]
if key_type == "M":
return major_steps
else:
return ......
def get_2oct(octave, notes):
notes_oct1 = [i + str(octave) for i in notes]
notes_oct2 = [..... + str(octave + ....) for .... in ........]
notes_2oct = notes_oct1 + notes_oct2
return notes_2oct
def get_key_notes(key_note, key_type):
major_sharp_keys = ["c", "g", "d", "a", "e", "b", "f#", "c#"]
# major_flat_keys = ["c", "f", "bb", "eb", "ab", "db", "gb", "cb"]
minor_sharp_keys = ["a", "e", "b", "f#", "c#", "g#", "d#", "a#"]
# minor_flat_keys = ["a", "d", "g", "c", "f", "bb", "eb", "ab"]
if key_type == "M":
sharp_keys = ......
else:
sharp_keys = .....
sharp_key_notes = ["c", "c#", "d", "d#", "e", "f", "f#", "g", "g#", "a", "a#", "b"]
flat_key_notes = ["c", "db", "d", "eb", "e", "f", "gb", "g", "ab", "a", "bb", "b"]
if key_note in ......:
return sharp_key_notes
else:
return flat_key_notes
def get_scale(key_note, key_type, octave):
scale_steps = get_scale_steps(......)
notes = get_key_notes(......)
notes2oct = get_2oct(....., ........)
note_index = notes2oct.index(..... + str(.....))
scale = [notes2oct[.........]]
for i in scale_steps:
note_index += i
scale.append(notes2oct[........])
return scale
while True:
if button_a.is_pressed():
scale_notes = get_scale("g", "M", 4)
# print(scale_notes)
music.play(scale_notes)
sleep(1000)
scale_notes = get_scale("e", "m", 4)
# print(scale_notes)
music.play(scale_notes)
sleep(1000)
elif button_b.is_pressed():
scale_notes = get_scale("f", "M", 4)
# print(scale_notes)
music.play(scale_notes)
sleep(1000)
scale_notes = get_scale("d", "m", 4)
# print(scale_notes)
music.play(scale_notes)
sleep(1000)
from microbit import *
import music
def get_scale_steps(key_type):
major_steps = [2, 2, 1, 2, 2, 2, 1]
minor_steps = [2, 1, 2, 2, 1, 2, 2]
if key_type == "M":
return major_steps
else:
return minor_steps
def get_2oct(octave, notes):
notes_oct1 = [i + str(octave) for i in notes]
notes_oct2 = [i + str(octave + 1) for i in notes]
notes_2oct = notes_oct1 + notes_oct2
return notes_2oct
def get_key_notes(key_note, key_type):
major_sharp_keys = ["c", "g", "d", "a", "e", "b", "f#", "c#"]
# major_flat_keys = ["c", "f", "bb", "eb", "ab", "db", "gb", "cb"]
minor_sharp_keys = ["a", "e", "b", "f#", "c#", "g#", "d#", "a#"]
# minor_flat_keys = ["a", "d", "g", "c", "f", "bb", "eb", "ab"]
if key_type == "M":
sharp_keys = major_sharp_keys
else:
sharp_keys = minor_sharp_keys
sharp_key_notes = ["c", "c#", "d", "d#", "e", "f", "f#", "g", "g#", "a", "a#", "b"]
flat_key_notes = ["c", "db", "d", "eb", "e", "f", "gb", "g", "ab", "a", "bb", "b"]
if key_note in sharp_keys:
return sharp_key_notes
else:
return flat_key_notes
def get_scale(key_note, key_type, octave):
scale_steps = get_scale_steps(key_type)
notes = get_key_notes(key_note, key_type)
notes2oct = get_2oct(octave, notes)
note_index = notes2oct.index(key_note + str(octave))
scale = [notes2oct[note_index]]
for i in scale_steps:
note_index += i
scale.append(notes2oct[note_index])
return scale
while True:
if button_a.is_pressed():
scale_notes = get_scale("g", "M", 4)
# print(scale_notes)
music.play(scale_notes)
sleep(1000)
scale_notes = get_scale("e", "m", 4)
# print(scale_notes)
music.play(scale_notes)
sleep(1000)
elif button_b.is_pressed():
scale_notes = get_scale("f", "M", 4)
# print(scale_notes)
music.play(scale_notes)
sleep(1000)
scale_notes = get_scale("d", "m", 4)
# print(scale_notes)
music.play(scale_notes)
sleep(1000)
Tasks
Create a function, get_random_notes(notes, note_count=5), to return a random list of notes, given the notes and the number of notes, with default 5. Use list comprehension to generate the list. Play 5 random notes from the g major and 5 from the a minor scales on button pressing.
from microbit import *
import music
import random
def get_scale_steps(key_type):
major_steps = [2, 2, 1, 2, 2, 2, 1]
minor_steps = [2, 1, 2, 2, 1, 2, 2]
if key_type == "M":
return major_steps
else:
return minor_steps
def get_2oct(octave, notes):
notes_oct1 = [i + str(octave) for i in notes]
notes_oct2 = [i + str(octave + 1) for i in notes]
notes_2oct = notes_oct1 + notes_oct2
return notes_2oct
def get_key_notes(key_note, key_type):
major_sharp_keys = ["c", "g", "d", "a", "e", "b", "f#", "c#"]
# major_flat_keys = ["c", "f", "bb", "eb", "ab", "db", "gb", "cb"]
minor_sharp_keys = ["a", "e", "b", "f#", "c#", "g#", "d#", "a#"]
# minor_flat_keys = ["a", "d", "g", "c", "f", "bb", "eb", "ab"]
if key_type == "M":
sharp_keys = major_sharp_keys
else:
sharp_keys = minor_sharp_keys
sharp_key_notes = ["c", "c#", "d", "d#", "e", "f", "f#", "g", "g#", "a", "a#", "b"]
flat_key_notes = ["c", "db", "d", "eb", "e", "f", "gb", "g", "ab", "a", "bb", "b"]
if key_note in sharp_keys:
return sharp_key_notes
else:
return flat_key_notes
def get_scale(key_note, key_type, octave):
scale_steps = get_scale_steps(key_type)
notes = get_key_notes(key_note, key_type)
notes2oct = get_2oct(octave, notes)
note_index = notes2oct.index(key_note + str(octave))
scale = [notes2oct[note_index]]
for i in scale_steps:
note_index += i
scale.append(notes2oct[note_index])
return scale
def get_random_notes(notes, note_count=5):
random_notes = [random.choice(notes) for i in range(note_count)]
return random_notes
while True:
if button_a.is_pressed():
random_notes = get_random_notes(get_scale("g", "M", 4), 5)
print(random_notes)
music.play(random_notes)
sleep(1000)
elif button_b.is_pressed():
random_notes = get_random_notes(get_scale("a", "m", 4), 5)
print(random_notes)
music.play(random_notes)
sleep(1000)
Exercises
Set up two microbits and send a key from one to the other and have it play on the receiver.
Create a dictionary of keys and their notes and save it to a file to be accessed on the microbit.
8.3. Accelerometer based notes
from microbit import *
import music
accelerometer.set_range(1)
play_notes = ["E3", "F#3", "G3", "A3", "B3",
"E4", "F#4", "G4", "A4", "B4",
"E5", "F#5", "G5", "A5", "B5",
"E6", "F#6", "G6", "A6", "B6"]
play_durations = ["1", "2", "4", "8", "16"]
duration_len = len(play_durations)
note_len = len(play_notes)
play_music = True
while True:
#use A to toggle music
if button_a.was_pressed():
play_music = not play_music
if not play_music:
continue
#get accelerometer readings
x_reading = abs(accelerometer.get_x())
y_reading = abs(accelerometer.get_y())
# use above 1023 incase some microbits give slightly higher readings
scaled_x = scale(x_reading, from_=(-1200, 1200), to=(-note_len +1, note_len -1))
scaled_y = scale(y_reading, from_=(-1200, 1200), to=(-duration_len +1, duration_len -1))
#get a note based on tilt
music.play(play_notes[scaled_x] + ":" + play_durations[scaled_y])
Exercise
Use the accelerometer to control 8 notes of a scale over just one octave.