Skip to content

Commit af38f71

Browse files
authored
Add files via upload
1 parent 03dee4a commit af38f71

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

recording_voice_test.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import pyaudio
2+
import wave
3+
4+
# sample chunk size
5+
chunk = 1024
6+
# sample format: paFloat32, paInt32, paInt24, paInt16, paInt8, paUInt8, paCustomFormat
7+
sample_format = pyaudio.paInt16
8+
# sound channel
9+
channels = 2
10+
# sample frequency rate: 44100 ( CD ), 48000 ( DVD ), 22050, 24000, 12000 and 11025
11+
fs = 44100
12+
# recording seconds
13+
seconds = 5
14+
# recording file name
15+
filename = "test.wav"
16+
17+
p = pyaudio.PyAudio()
18+
# init pyaudio object
19+
20+
print("starting recording...")
21+
22+
# active voice stream
23+
stream = p.open(format=sample_format, channels=channels, rate=fs, frames_per_buffer=chunk, input=True)
24+
25+
frames = []
26+
# voice list
27+
28+
for i in range(0, int(fs / chunk * seconds)):
29+
# record voice into list
30+
data = stream.read(chunk)
31+
frames.append(data)
32+
33+
# stop recording
34+
stream.stop_stream()
35+
# close stream
36+
stream.close()
37+
p.terminate()
38+
39+
print('stop recording...')
40+
41+
# open voice file
42+
wf = wave.open(filename, 'wb')
43+
# set channel
44+
wf.setnchannels(channels)
45+
# set format
46+
wf.setsampwidth(p.get_sample_size(sample_format))
47+
# set sampling frequency rate
48+
wf.setframerate(fs)
49+
# save
50+
wf.writeframes(b''.join(frames))
51+
wf.close()

0 commit comments

Comments
 (0)