-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio.py
38 lines (29 loc) · 963 Bytes
/
audio.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
from numpy import fromstring
from numpy import int16
from numpy import fft
from numpy import absolute
from numpy import array as nparray
from numpy import log as nplog
from pyaudio import paInt16
from pyaudio import PyAudio
class Audio():
def __init__(self):
self.THRESHOLD = 200
self.CHUNK_SIZE = 1024
self.RATE = 22100
p = PyAudio()
self.stream = p.open(format=paInt16, channels=1, rate=self.RATE, input=True, output=True, frames_per_buffer=self.CHUNK_SIZE)
def set_threshold(self, new_th):
self.THRESHOLD = new_th
def silence(self, npdata):
if max(npdata) < self.THRESHOLD:
npdata = nparray([0 for i in range(self.CHUNK_SIZE)], int16)
return npdata
def recordHz(self):
chunk = self.stream.read(self.CHUNK_SIZE)
npdata = fromstring(chunk, dtype=int16)
#sample_width = p.get_sample_size(FORMAT)
npdata = self.silence(npdata)
Y = fft.rfft(npdata, self.CHUNK_SIZE)
Y_abs = nplog(absolute(Y) + 1)
return Y_abs