-
Notifications
You must be signed in to change notification settings - Fork 0
/
voiceController.py
78 lines (58 loc) · 2.24 KB
/
voiceController.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
import aubio
import numpy as np
import pyaudio
import time
import argparse
import queue
import music21
#idea for code obtained from tutorial at https://www.makeartwithpython.com/blog/voice-controlled-flappy-bird/
# parser = argparse.ArgumentParser()
# parser.add_argument("-input", required=False, type=int, help="Audio Input Device")
# args = parser.parse_args()
#
# if args.input is None:
# print("No input device specified. Printing list of input devices now: ")
# p = pyaudio.PyAudio()
# for i in range(p.get_device_count()):
# print("Device number (%i): %s" % (i, p.get_device_info_by_index(i).get('name')))
# print("Run this program with -input 1, or the number of the input you'd like to use.")
# exit()
# PyAudio object.
p = pyaudio.PyAudio()
# Open stream.
stream = p.open(format=pyaudio.paFloat32,
channels=1, rate=44100, input=True,
input_device_index=0, frames_per_buffer=4096)
time.sleep(1)
# Aubio's pitch detection.
pDetection = aubio.pitch("default", 2048, 2048//2, 44100)
# Set unit.
pDetection.set_unit("Hz")
pDetection.set_silence(-40)
Q = queue.Queue()
def get_current_note(volume_thresh=0.01, printOut=False):
"""Returns the Note Currently Played on the q object when audio is present
Keyword arguments:
volume_thresh -- the volume threshold for input. defaults to 0.01
printOut -- whether or not to print to the terminal. defaults to False
"""
current_pitch = music21.pitch.Pitch()
while True:
data = stream.read(1024, exception_on_overflow=False)
samples = np.frombuffer(data,
dtype=aubio.float_type)
pitch = pDetection(samples)[0]
# Compute the energy (volume) of the
# current frame.
volume = np.sum(samples**2)/len(samples) * 100
if pitch and volume > volume_thresh: # adjust with your mic!
current_pitch.frequency = pitch
else:
continue
if printOut:
print(current_pitch)
else:
current = current_pitch.nameWithOctave
Q.put({'Note': current, 'Cents': current_pitch.microtone.cents})
if __name__ == '__main__':
get_current_note(volume_thresh=0.001, printOut=True)