-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio.py
40 lines (26 loc) · 793 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
39
40
import alsaaudio
RATE = 44100/2
BUFFER_SIZE = 30; # in seconds
buffer = []
def start_recording():
mixer = alsaaudio.Mixer(control="Mic")
mixer.setvolume(90, 0, alsaaudio.PCM_CAPTURE)
#inp = alsaaudio.PCM(alsaaudio.PCM_CAPTURE, alsaaudio.PCM_NONBLOCK)
inp = alsaaudio.PCM(alsaaudio.PCM_CAPTURE)
inp.setchannels(1)
inp.setrate(RATE)
inp.setformat(alsaaudio.PCM_FORMAT_S16_LE)
inp.setperiodsize(160)
print("* starting recording")
while True:
if (len(buffer) > int(RATE / 920 * BUFFER_SIZE)):
buffer.pop(0);
l, data = inp.read()
if l > 0:
buffer.append(data)
def get_framerate():
return RATE
def clear_buffer():
buffer = [] # flush buffer
def get_buffer():
return b''.join(buffer)