Skip to content

Instantly share code, notes, and snippets.

@mossbanay
Created February 27, 2018 22:11
Show Gist options
  • Save mossbanay/0f8ba54d80e0464d7802f6ef5dcc124e to your computer and use it in GitHub Desktop.
Save mossbanay/0f8ba54d80e0464d7802f6ef5dcc124e to your computer and use it in GitHub Desktop.
from microbit import *
import music
notes = ['A1', 'B1', 'C1', 'D1', 'E1', 'F1', 'G1', 'A2', 'B2', 'C2', 'D2', 'E2', 'F2', 'G2', 'A3', 'B3', 'C3', 'D3', 'E3', 'F3', 'G3', 'A4', 'B4', 'C4', 'D4', 'E4', 'F4', 'G4', 'A5', 'B5', 'C5', 'D5', 'E5', 'F5', 'G5', 'A6', 'B6', 'C6', 'D6', 'E6', 'F6', 'G6', 'A7', 'B7', 'C7', 'D7', 'E7', 'F7', 'G7']
"""
for note in notes:
music.play('{}:4'.format(note))
print('playing {}:4'.format(note))
"""
def get_next_note(note):
return notes[notes.index(note)+1 % len(notes)]
assert get_next_note('A1')=='B1'
assert get_next_note('G1')=='A2'
TEMPO = 120
LOOP_LENGTH = 16
START_NOTE = 'E2'
LENGTHS = ['4', '3']
loop = [[] for _ in range(LOOP_LENGTH)]
current_note = START_NOTE
duration_flag = False
countdown = 0
j = 30
while True:
for i in range(LOOP_LENGTH):
if countdown == 0:
if len(loop[i]) >= 1:
duration_flag = not duration_flag
loop[i].append(current_note + ':' + LENGTHS[duration_flag])
countdown = int(LENGTHS[duration_flag])
j-=1
current_note = get_next_note(current_note)
countdown -= 1
for note in loop[i]:
music.play(note, wait=False)
print('playing {}'.format(note))
sleep(60000/TEMPO)
if j == 0:
break
if j == 0:
break
for i, e in enumerate(loop):
print(i, e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment