-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpitch_recognition.py
31 lines (24 loc) · 966 Bytes
/
pitch_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
import aubio
import numpy as np
import pyaudio
import time
from pynput.keyboard import Key, Controller
import mapping_keyboard as mk
def pitch_recognition(stream, pDetection, pitch_queue, current_pitch, printOut, mapping_list, volume_thresh=0.01):
keyboard = Controller()
while True:
data = stream.read(1024, exception_on_overflow=False)
samples = np.fromstring(data, dtype=aubio.float_type)
pitch = pDetection(samples)[0]
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
pitch_queue.put({'Note': current, 'Cents': current_pitch.microtone.cents})
pitch = str(current_pitch)
mk.mapping_keyboard(pitch, keyboard, mapping_list)