1 Star 0 Fork 0

opencdr/ASRT_SpeechRecognition

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
speech-recorder.py 1.19 KB
一键复制 编辑 原始数据 按行查看 历史
AI柠檬 提交于 2018-10-23 10:40 . 添加一个python录音程序
import wave
from pyaudio import PyAudio,paInt16
framerate=16000
NUM_SAMPLES=2000
channels=1
sampwidth=2
TIME=10
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*8:#控制录音时间
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():
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()
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/opencdr/ASRT_SpeechRecognition.git
git@gitee.com:opencdr/ASRT_SpeechRecognition.git
opencdr
ASRT_SpeechRecognition
ASRT_SpeechRecognition
master

搜索帮助