-
Notifications
You must be signed in to change notification settings - Fork 0
/
note_player.py
89 lines (66 loc) · 2.05 KB
/
note_player.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
from microbit import *
import music
import math
# global vars - boo!
high_range = False
low_range = False
default_range = True
# for frequency range
upper_bound = 1000
lower_bound = -980
music_duration = 50
pause_duration = 50
max_duration = 200
min_duration = 100
while True:
# handle inputs
if button_a.is_pressed():
low_range = True
high_range = False
default_range = False
elif button_b.is_pressed():
low_range = False
high_range = True
default_range = False
else:
low_range = False
high_range = False
default_range = True
# hook up display to show current option
if low_range == True:
display.show(Image.CLOCK9)
elif high_range == True:
display.show(Image.CLOCK3)
elif default_range == True:
display.show(Image.CLOCK12)
# get accelerometer position
acc_x = accelerometer.get_x()
# normalise accelerometer range
if acc_x >= upper_bound:
acc_x = upper_bound
elif acc_x <= lower_bound:
acc_x = lower_bound
wide_frequency = acc_x + upper_bound
range_factor = 2000 * 0.33
low_range = True
# move frquency into correct range
if low_range == True:
frequency = int( (wide_frequency * 0.33) + ( 0 * range_factor ) )
elif high_range == True:
frequency = int( (wide_frequency * 0.33) + ( 2 * range_factor ) )
else:
frequency = int( (wide_frequency * 0.33) + ( 1 * range_factor ) )
# calculate a good duration
acc_y = accelerometer.get_y()
# normalise Y value (duration)
if acc_y >= upper_bound:
acc_y = upper_bound
elif acc_y <= lower_bound:
acc_y = lower_bound
duration = acc_y + upper_bound
div_factor = 2000 / max_duration
duration = math.ceil(duration / div_factor)
music_duration = max(duration, min_duration)
gesture = accelerometer.current_gesture()
if gesture == "face down":
music.pitch(frequency, music_duration)