久久国产成人av_抖音国产毛片_a片网站免费观看_A片无码播放手机在线观看,色五月在线观看,亚洲精品m在线观看,女人自慰的免费网址,悠悠在线观看精品视频,一级日本片免费的,亚洲精品久,国产精品成人久久久久久久

分享

python寫(xiě)一個(gè)錄音小程序

 xvdo 2021-11-12

python錄音

學(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:///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文檔

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,,所有內(nèi)容均由用戶(hù)發(fā)布,,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式,、誘導(dǎo)購(gòu)買(mǎi)等信息,,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,,請(qǐng)點(diǎn)擊一鍵舉報(bào),。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶(hù) 評(píng)論公約

    類(lèi)似文章 更多