-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrecord.py
More file actions
38 lines (28 loc) · 816 Bytes
/
record.py
File metadata and controls
38 lines (28 loc) · 816 Bytes
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
import pyaudio
import wave
CHUNK = 1024
FORMAT = pyaudio.paInt16 #paInt8
CHANNELS = 2
RATE = 16000 #sample rate
RECORD_SECONDS = 7
def record_and_save(file_path):
p = pyaudio.PyAudio()
frames = []
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
frames.append(data)
stream.stop_stream()
stream.close()
p.terminate()
# TODO: make a temp file
wf = wave.open(file_path, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()