python實(shí)現(xiàn)錄音小程序
本文為大家分享了python實(shí)現(xiàn)錄音小程序的具體代碼,供大家參考,具體內(nèi)容如下
學(xué)習(xí)目標(biāo):掌握python的pyaudio擴(kuò)展包和Wave模塊錄制語(yǔ)音的方法
- Wav音頻:聲道數(shù),采樣頻率,量化位數(shù)
python Wav包是自帶的,pyaudio需要下載
pip3 install pyaudio
python讀Wav文件:
fp=wave.open('','rb')
nf=fp.getnframes()#獲取文件的采樣點(diǎn)數(shù)量
print('sampwidth:',fp.getsampwidth())
print('framerate:',fp.getframerate())
print('channels:',fp.getnchannels())
f_len=nf*2#文件長(zhǎng)度計(jì)算,每個(gè)采樣2個(gè)字節(jié)
audio_data=fp.readframes(nf)
python寫(xiě)Wav文件:
def save_wave_file(filename,data): '''save the date to the wavfile''' wf=wave.open(filename,'wb') wf.setnchannels(channels)#聲道 wf.setsampwidth(sampwidth)#采樣字節(jié) 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:#控制錄音時(shí)間
string_audio_data = stream.read(NUM_SAMPLES)#一次性錄音采樣字節(jié)大小
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:#控制錄音時(shí)間
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()
更多用法詳見(jiàn) pyaudio文檔
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- python實(shí)現(xiàn)網(wǎng)頁(yè)錄音效果
- python實(shí)現(xiàn)錄音功能(可隨時(shí)停止錄音)
- Python調(diào)用Windows API函數(shù)編寫(xiě)錄音機(jī)和音樂(lè)播放器功能
- Python人工智能之路 之PyAudio 實(shí)現(xiàn)錄音 自動(dòng)化交互實(shí)現(xiàn)問(wèn)答
- python 通過(guò)麥克風(fēng)錄音 生成wav文件的方法
- python實(shí)現(xiàn)播放音頻和錄音功能示例代碼
- 基于python實(shí)現(xiàn)百度語(yǔ)音識(shí)別和圖靈對(duì)話
- 使用Python和百度語(yǔ)音識(shí)別生成視頻字幕的實(shí)現(xiàn)
- python版百度語(yǔ)音識(shí)別功能
- python調(diào)用百度REST API實(shí)現(xiàn)語(yǔ)音識(shí)別
- python調(diào)用百度語(yǔ)音識(shí)別api
- python實(shí)現(xiàn)百度語(yǔ)音識(shí)別api
- python錄音并調(diào)用百度語(yǔ)音識(shí)別接口的示例
相關(guān)文章
python中turtle庫(kù)的簡(jiǎn)單使用教程
這篇文章主要給大家介紹了關(guān)于python中turtle庫(kù)的簡(jiǎn)單使用教程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
對(duì)python numpy.array插入一行或一列的方法詳解
今天小編就為大家分享一篇對(duì)python numpy.array插入一行或一列的方法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-01-01
Python?Pandas?中的數(shù)據(jù)結(jié)構(gòu)詳解
這篇文章主要介紹了Python?Pandas?中的數(shù)據(jù)結(jié)構(gòu)詳解,Pandas有三種數(shù)據(jù)結(jié)構(gòu)Series、DataFrame和Panel,文章圍繞主題展開(kāi)更多相關(guān)內(nèi)容需要的小伙伴可以參考一下2022-06-06
python數(shù)據(jù)可視化使用pyfinance分析證券收益示例詳解
Python 爬蟲(chóng)批量爬取網(wǎng)頁(yè)圖片保存到本地的實(shí)現(xiàn)代碼
解決python問(wèn)題 Traceback (most recent call&n

