|
| 1 | +# TiltMusic by Alex "Chozabu" P-B. September 2016. |
| 2 | +# |
| 3 | +# Tilt Y to change Pitch |
| 4 | +# press A to turn sound on or off |
| 5 | +# hold B and tilt X to change the note length |
| 6 | +# |
| 7 | +# A quick demo can be found at https://youtu.be/vvECQTDiWxQ |
| 8 | +# |
| 9 | +# This program has been placed into the public domain. |
| 10 | + |
| 11 | +from microbit import * |
| 12 | +import music |
| 13 | + |
| 14 | +#A selection of sharp notes |
| 15 | +notes = [233.08, 277.18, 311.13, 369.99, 415.30, |
| 16 | +466.16, 554.37, 622.25, 739.99, 830.61, 932.33, |
| 17 | +1108.73, 1244.51, 1479.98, 1661.22, 1864.66, |
| 18 | +2217.46, 2489.02, 2959.96, 3322.44, 3729.31, |
| 19 | +4434.92, 4978.03, 5919.91, 6644.88, 7458.62] |
| 20 | + |
| 21 | +#note lengths |
| 22 | +note_durations = [ |
| 23 | + 50, 100, 200, 400, 800 |
| 24 | +] |
| 25 | +durationlen = len(note_durations) |
| 26 | +notelen = len(notes) |
| 27 | + |
| 28 | +duration = 100 |
| 29 | + |
| 30 | +play_music = True |
| 31 | + |
| 32 | +while True: |
| 33 | + #get accelerometer readings |
| 34 | + xreading = abs(accelerometer.get_x()) |
| 35 | + yreading = abs(accelerometer.get_y()) |
| 36 | + |
| 37 | + #use a to toggle music |
| 38 | + if button_a.was_pressed(): |
| 39 | + play_music = not play_music |
| 40 | + if not play_music: |
| 41 | + continue |
| 42 | + |
| 43 | + #get a note based on tilt |
| 44 | + note = xreading*.01 |
| 45 | + pitch = notes[int(note)%notelen] |
| 46 | + |
| 47 | + #if b is pressed, alter the length based on tilt |
| 48 | + if button_b.is_pressed() == 1: |
| 49 | + #pitch *= .5 |
| 50 | + duration = note_durations[int(yreading*0.01)%durationlen] |
| 51 | + |
| 52 | + #play our sound! |
| 53 | + music.pitch(int(pitch), duration) |
| 54 | + |
0 commit comments