-
Notifications
You must be signed in to change notification settings - Fork 0
/
microphone_recorder.py
53 lines (45 loc) · 1.57 KB
/
microphone_recorder.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
"""
This file contains the utilities related to the microphone input.
"""
import pyaudio
import numpy as np
import threading
import atexit
class MicrophoneRecorder(object):
"""
A recorder class that starts listening to the user microphone.
Class inspired by the SciPy 2015 Vispy talk opening example, see https://github.com/vispy/vispy/pull/928.
"""
def __init__(self, sample_rate=44100, chunksize=1024):
self.sample_rate = sample_rate
self.chunksize = chunksize
self.p = pyaudio.PyAudio()
self.stream = self.p.open(format=pyaudio.paInt16,
channels=1,
rate=self.sample_rate,
input=True,
frames_per_buffer=self.chunksize,
stream_callback=self.new_frame)
self.lock = threading.Lock()
self.stop = False
self.frames = []
atexit.register(self.close)
def new_frame(self, data, frame_count, time_info, status):
data = np.frombuffer(data, 'int16')
with self.lock:
self.frames.append(data)
if self.stop:
return None, pyaudio.paComplete
return None, pyaudio.paContinue
def get_frames(self):
with self.lock:
frames = self.frames
self.frames = []
return frames
def start(self):
self.stream.start_stream()
def close(self):
with self.lock:
self.stop = True
self.stream.close()
self.p.terminate()