明霞山资源网 Design By www.htccd.com

本文为大家分享了python实现录音小程序的具体代码,供大家参考,具体内容如下

学习目标:掌握python的pyaudio扩展包和Wave模块录制语音的方法

  • Wav音频:声道数,采样频率,量化位数

python Wav包是自带的,pyaudio需要下载

pip3 install pyaudio

python读Wav文件:

fp=wave.open('','rb')
 nf=fp.getnframes()#获取文件的采样点数量
 print('sampwidth:',fp.getsampwidth())
 print('framerate:',fp.getframerate())
 print('channels:',fp.getnchannels())
 f_len=nf*2#文件长度计算,每个采样2个字节
 audio_data=fp.readframes(nf)

python写Wav文件:

def save_wave_file(filename,data):
 '''save the date to the wavfile'''
 wf=wave.open(filename,'wb')
 wf.setnchannels(channels)#声道
 wf.setsampwidth(sampwidth)#采样字节 1 or 2
 wf.setframerate(framerate)#采样频率 8000 or 16000
 wf.writeframes(b"".join(data))#https://stackoverflow.com/questions/32071536/typeerror-sequence-item-0-expected-str-instance-bytes-found
 wf.close()

利用PyAudio录音:

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*20:#控制录音时间
 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()

利用PyAudio播放音频

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()

完整录音播放的demo

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*20:#控制录音时间
 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()

更多用法详见 pyaudio文档

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

标签:
python录音小程序,python录音程序,python录音

明霞山资源网 Design By www.htccd.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
明霞山资源网 Design By www.htccd.com

RTX 5090要首发 性能要翻倍!三星展示GDDR7显存

三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。

首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。

据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。