-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdemo_audio_feedback.py
67 lines (51 loc) · 1.86 KB
/
demo_audio_feedback.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
from utils import transcribe, estimateBaseFreq, dtw
import sounddevice as sd
from scipy.io import wavfile
import sys
WHISTLE_PASSPHRASE = "wav_files/ppA.wav" # you can also record your own! Record at 16 kHz
DURATION = 3 # length that you will record for input whistles
THRESHOLD = 3 # threshold after DTW comparison
def keylock(dur, Fs, original_key, thresh=3):
"""
:param dur: duration of recording in seconds
:param Fs: sampling rate
:param original_key:
:param thresh: threshold for DTW comparison
:return:
"""
print("STARTING RECORDING")
in_key = sd.rec(int(dur * Fs), samplerate=Fs, channels=1, blocking=True).flatten()
print("FINISHED RECORDING")
k = []
for audio in (in_key, original_key):
t = transcribe(Fs, audio)
k.append(t / estimateBaseFreq(t))
d = dtw(k[0], k[1])
# put your desired action here!
result_audio(d, thresh)
def result_audio(score, thresh):
if score < thresh:
print("OPEN SESAME! Score: {}".format(score))
Fs, bell = wavfile.read("wav_files/bell_short.wav")
sd.play(bell, samplerate=Fs, blocking=True)
else:
print("WRONG! Score: {}".format(score))
Fs, buzzer = wavfile.read("wav_files/buzzer_short.wav")
sd.play(buzzer, samplerate=Fs, blocking=True)
if __name__ == "__main__":
python_version = 2
if sys.version_info > (3, 0):
python_version = 3
# provide passphrase here
Fs, ppA = wavfile.read(WHISTLE_PASSPHRASE)
while True:
print('#' * 80)
print("Press 'r + Enter' to record and 'q + Enter' to quit")
if python_version == 2:
char = raw_input()
else:
char = input()
if char == 'r':
keylock(DURATION, Fs, ppA, thresh=THRESHOLD)
elif char == 'q':
break