-
Notifications
You must be signed in to change notification settings - Fork 2
/
recordvoice.py
59 lines (49 loc) · 1.3 KB
/
recordvoice.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
54
55
56
57
58
#!/usr/bin/env python3
#encoding:utf-8
import wave
from pyaudio import PyAudio,paInt16
framerate=8000
NUM_SAMPLES=2000
channels=1
sampwidth=2
TIME=2
def save_wave_file(filename,data):
'''save the date to the wavfile'''
wf=wave.open(filename,'wb')
wf.setnchannels(channels)
wf.setsampwidth(sampwidth)
wf.setframerate(framerate)
wf.writeframes(b"".join(data))
wf.close()
def my_record():
pa=PyAudio()
stream=pa.open(format = paInt16,channels=1,
rate=framerate,input=True,
frames_per_buffer=NUM_SAMPLES)
my_buf=[]
count=0
while count<TIME*60:#控制录音时间
string_audio_data = stream.read(NUM_SAMPLES)
my_buf.append(string_audio_data)
count+=1
print('.')
#save_wave_file('01.wav',my_buf)
#stream.close()
chunk=2014
def play():
save_wave_file('01.wav',my_buf)
stream.close()
wf=wave.open(r"01.wav",'rb')
p=PyAudio()
stream=p.open(format=p.get_format_from_width(wf.getsampwidth()),channels=
wf.getnchannels(),rate=wf.getframerate(),output=True)
while True:
data=wf.readframes(chunk)
if data=="":break
stream.write(data)
stream.close()
p.terminate()
#if __name__ == '__main__':
#my_record()
#print('Over!')
#play()