码迷,mamicode.com
首页 > 其他好文 > 详细

录音功能,

时间:2021-02-10 13:18:22      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:==   aaaaa   sam   pen   pre   write   ide   close   span   

技术图片
import pyaudio
import wave
def get_audio(filepath):
    aa = str(input("是否开始录音?   (y/n)"))
    if aa == str("y") :
        CHUNK = 1024
        FORMAT = pyaudio.paInt16
        CHANNELS = 1                # 声道数
        RATE = 11025                # 采样率
        RECORD_SECONDS = 5          # 录音时间
        WAVE_OUTPUT_FILENAME = filepath
        p = pyaudio.PyAudio()

        stream = p.open(format=FORMAT,
                        channels=CHANNELS,
                        rate=RATE,
                        input=True,
                        frames_per_buffer=CHUNK)

        print("*"*5, "开始录音:请在5秒内输入语音", "*"*5)
        frames = []
        for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
            data = stream.read(CHUNK)
            frames.append(data)
        print("*"*5, "录音结束\n")

        stream.stop_stream()
        stream.close()
        p.terminate()

        wf = wave.open(WAVE_OUTPUT_FILENAME, wb)
        wf.setnchannels(CHANNELS)
        wf.setsampwidth(p.get_sample_size(FORMAT))
        wf.setframerate(RATE)
        wf.writeframes(b‘‘.join(frames))
        wf.close()
    elif aa == str(""):
        exit()
    else:
        print("语音录入失败,请重新开始")
        get_audio("D://aaaaa.wav")

get_audio("D://aaaaa.wav")
View Code

 

录音功能,

标签:==   aaaaa   sam   pen   pre   write   ide   close   span   

原文地址:https://www.cnblogs.com/3-wusen/p/14394574.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!