-
Notifications
You must be signed in to change notification settings - Fork 0
/
record_audio.py
41 lines (29 loc) · 892 Bytes
/
record_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
41
import pyaudio
import wave
import globals
def record_audio(wave_out_path,record_second):
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 16000
p = pyaudio.PyAudio()
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)
wf = wave.open(wave_out_path, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
#print("* recording")
for i in range(0, int(RATE / CHUNK * record_second)):
data = stream.read(CHUNK)
wf.writeframes(data)
#print("* done recording")
stream.stop_stream()
stream.close()
p.terminate()
wf.close()
#globals.initialize()
#record_audio('output_0'+str(globals.n)+'.wav', record_second=5)