Skip to content

Commit cffd0e7

Browse files
authored
Add files via upload
1 parent 2efbbfb commit cffd0e7

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

test/temp_voice_test.py

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

0 commit comments

Comments
 (0)