-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutility.py
47 lines (38 loc) · 1.08 KB
/
utility.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
import aubio
import numpy as num
import pyaudio
import math
import wave
import time
import sys
name = ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"]
def findnote(freq, C0):
h = round(12*math.log(freq/C0,2))
octave = int(h // 12)
n =int(h % 12)
return (name[n], octave)
def colorize(string, errorper, tolerance):
if errorper <= tolerance:
return '\033[32m' + string + '\033[0m'
elif errorper < 0.33:
return '\033[93m' + string + '\033[0m'
elif errorper < 0.66:
return '\033[33m' + string + '\033[0m'
else:
return '\033[91m' + string + '\033[0m'
def initStream():
#PyAudio stream object.
p = pyaudio.PyAudio()
#Open the stream.
stream = p.open(format=pyaudio.paFloat32,
channels=1,rate=44100,input=True,
frames_per_buffer=1024)
return stream
def initDetector():
# Aubio's pitch detection.
pDetector = aubio.pitch("default", 2048,
2048//2, 44100)
# Set unit.
pDetector.set_unit("Hz")
pDetector.set_silence(-20)
return pDetector