-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput_stream_chord_recognition.py
56 lines (42 loc) · 1.45 KB
/
input_stream_chord_recognition.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
import threading
import numpy as np
import pyaudio
from GUI import GUI_text_with_scrollbar, update_text_area
from hmm import compute_chord_for_frame
finish_recognition = False
def chord_recognition():
CHUNK = 16384
FORMAT = pyaudio.paUInt8
CHANNELS = 1
RATE = 44100
FRAME_LENGTH = 0.5
THRESHOLD = 130
INPUT_DEVICE_INDEX = 3 # change this to your own if you're trying to test this
p = pyaudio.PyAudio()
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
output=False,
frames_per_buffer=CHUNK,
input_device_index=INPUT_DEVICE_INDEX)
while stream.is_active() and not finish_recognition:
snippet = []
for i in range(0, int(RATE / CHUNK * FRAME_LENGTH)):
data = stream.read(CHUNK)
snippet.append(data)
data = np.frombuffer(b''.join(snippet), dtype=np.uint8)
if max(data) > THRESHOLD:
chord = compute_chord_for_frame(data, RATE)
update_text_area(text_area, chord)
else:
update_text_area(text_area, ".")
stream.stop_stream()
stream.close()
p.terminate()
(window, text_area) = GUI_text_with_scrollbar()
chord_recognition_process = threading.Thread(target=chord_recognition)
chord_recognition_process.start()
window.mainloop()
finish_recognition = True
chord_recognition_process.join()