-
Notifications
You must be signed in to change notification settings - Fork 0
/
PitchSensor.py
151 lines (128 loc) · 3.92 KB
/
PitchSensor.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import keyboard
import time
import pyaudio
import sys
import numpy as np
import aubio
space_pressed = False
left_pressed = False
right_pressed = False
LEFT_KEY = "a"
RIGHT_KEY = "d"
def avg(l):
return reduce(lambda x, y: x + y, l) / len(l)
def round(num, by):
return (num if(num % by == 0) else (num - (num % by)))
def unpress(should_release_space = True):
global right_pressed
global left_pressed
global space_pressed
if left_pressed:
print LEFT_KEY + " unpressed"
keyboard.release(LEFT_KEY)
left_pressed = False
if right_pressed:
print RIGHT_KEY + " unpressed"
keyboard.release(RIGHT_KEY)
right_pressed = False
if should_release_space and space_pressed:
print "space unpressed"
space_pressed = False
keyboard.release("space")
def press(key):
global right_pressed
global left_pressed
global space_pressed
if key == RIGHT_KEY:
if not right_pressed:
if left_pressed:
print LEFT_KEY + " unpressed"
keyboard.release(LEFT_KEY)
left_pressed = False
print RIGHT_KEY + " pressed"
keyboard.press(RIGHT_KEY)
right_pressed = True
if key == LEFT_KEY:
if not left_pressed:
if right_pressed:
print RIGHT_KEY + " unpressed"
keyboard.release(RIGHT_KEY)
right_pressed = False
print LEFT_KEY + " pressed"
keyboard.press(LEFT_KEY)
left_pressed = True
if key == "space" and not space_pressed:
print "space pressed"
keyboard.press("space")
space_pressed = True
# initialise pyaudio
p = pyaudio.PyAudio()
# open stream
buffer_size = 1024
pyaudio_format = pyaudio.paFloat32
n_channels = 1
samplerate = 44100
stream = p.open(format=pyaudio.paFloat32,
channels=1, rate=44100, input=True,
frames_per_buffer=1024)
# Aubio's pitch detection.
pDetection = aubio.pitch("default", 2048,
2048//2, 44100)
# Set unit.
pDetection.set_unit("Hz")
pDetection.set_silence(-40)
outputsink = None
record_duration = None
press_interval = 0.1
record_interval = 0.1
total_time = 1200
stime = time.time()
right_threshold = 600
volume_threshold = 0.07
sleep_between_rounds = 0.01
pitch = None
print "Starting"
while True:
try:
pitches = []
t = time.time()
while( time.time() < t + record_interval):
audiobuffer = stream.read(buffer_size)
signal = np.fromstring(audiobuffer, dtype=np.float32)
data = stream.read(1024)
samples = np.fromstring(data, dtype=aubio.float_type)
pitch = round(int(pDetection(samples)[0]), 25)
volume = np.sum(samples**2)/len(samples)
if(volume > volume_threshold):
pitches += [pitch]
print pitches
should_press_space = filter(lambda x: x > 2000, pitches)
pitches = filter(lambda x: x < 2000, pitches)
if pitches or should_press_space:
if (should_press_space):
press("space")
if pitches:
pitch = avg(pitches)
print pitch
if pitch > 20:
if(pitch > 550 or pitch < 500):
press(RIGHT_KEY)
else:
press(LEFT_KEY)
else:
unpress(should_release_space = not should_press_space)
pitch = None
else:
unpress(should_release_space = not should_press_space)
pitch = None
else:
unpress(should_release_space = not should_press_space)
pitch = None
time.sleep(sleep_between_rounds)
except KeyboardInterrupt, e:
print "Stopping"
break
stream.stop_stream()
stream.close()
p.terminate()
print "Exiting"