Python對(duì)wav文件的重采樣實(shí)例
例如從2channel,4.41k hz 重采樣到 1 channel,16k hz
def downsampleWav(src, dst, inrate=44100, outrate=16000, inchannels=2, outchannels=1): import os,wave,audioop if not os.path.exists(src): print ('Source not found!') return False if not os.path.exists(os.path.dirname(dst)): os.makedirs(os.path.dirname(dst)) try: s_read = wave.open(src, 'r') s_write = wave.open(dst, 'w') except: print ('Failed to open files!') return False n_frames = s_read.getnframes() data = s_read.readframes(n_frames) try: converted = audioop.ratecv(data, 2, inchannels, inrate, outrate, None) if outchannels == 1: converted = audioop.tomono(converted[0], 2, 1, 0) except: print ('Failed to downsample wav') return False try: s_write.setparams((outchannels, 2, outrate, 0, 'NONE', 'Uncompressed')) s_write.writeframes(converted) except: print ('Failed to write wav') return False try: s_read.close() s_write.close() except: print ('Failed to close wav files') return False return True
若in和out都是單通道:
def downsampleWav(src, dst, inrate=48000, outrate=16000, inchannels=1, outchannels=1): import os,wave,audioop if not os.path.exists(src): print ('Source not found!') return False if not os.path.exists(os.path.dirname(dst)): os.makedirs(os.path.dirname(dst)) try: s_read = wave.open(src, 'rb') params = s_read.getparams() nchannels, sampwidth, framerate, nframes = params[:4] print(nchannels,sampwidth, framerate,nframes) s_write = wave.open(dst, 'wb') except: print ('Failed to open files!') return False n_frames = s_read.getnframes() data = s_read.readframes(n_frames) try: converted = audioop.ratecv(data, 2, inchannels, inrate, outrate, None) if outchannels == 1 and inchannels != 1: converted = audioop.tomono(converted[0], 2, 1, 0) except: print ('Failed to downsample wav') return False try: s_write.setparams((outchannels, 2, outrate, 0, 'NONE', 'Uncompressed')) s_write.writeframes(converted[0]) except Exception as e: print(e) print ('Failed to write wav') return False try: s_read.close() s_write.close() except: print ('Failed to close wav files') return False return True
方案二
y為下采樣的結(jié)果,類型np.ndarray
You can use Librosa's load() function,
import librosa
y, s = librosa.load('test.wav', sr=8000) # Downsample 44.1kHz to 8kHz
The extra effort to install Librosa is probably worth the peace of mind.
Pro-tip: when installing Librosa on Anaconda, you need to install ffmpeg as well, so
pip install librosa
conda install -c conda-forge ffmpeg
以上這篇Python對(duì)wav文件的重采樣實(shí)例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
python 地圖經(jīng)緯度轉(zhuǎn)換、糾偏的實(shí)例代碼
這篇文章主要介紹了python 地圖經(jīng)緯度轉(zhuǎn)換、糾偏的實(shí)例代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-08-08Python使用thread模塊實(shí)現(xiàn)多線程的操作
線程(Threads)是操作系統(tǒng)提供的一種輕量級(jí)的執(zhí)行單元,可以在一個(gè)進(jìn)程內(nèi)并發(fā)執(zhí)行多個(gè)任務(wù),每個(gè)線程都有自己的執(zhí)行上下文,包括棧、寄存器和程序計(jì)數(shù)器,本文給大家介紹了Python使用thread模塊實(shí)現(xiàn)多線程的操作,需要的朋友可以參考下2024-10-10python入門之語句(if語句、while語句、for語句)
這篇文章主要介紹了python入門之語句,主要包括if語句、while語句、for語句的使用,需要的朋友可以參考下2015-01-01python將秒數(shù)轉(zhuǎn)化為時(shí)間格式的實(shí)例
今天小編就為大家分享一篇python將秒數(shù)轉(zhuǎn)化為時(shí)間格式的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-09-09淺談Pandas Series 和 Numpy array中的相同點(diǎn)
今天小編就為大家分享一篇淺談Pandas Series 和 Numpy array中的相同點(diǎn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-06-06Python實(shí)現(xiàn)圖書管理系統(tǒng)設(shè)計(jì)
這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)圖書管理系統(tǒng)設(shè)計(jì),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03Python數(shù)據(jù)可視化正態(tài)分布簡單分析及實(shí)現(xiàn)代碼
這篇文章主要介紹了Python數(shù)據(jù)可視化正態(tài)分布簡單分析及實(shí)現(xiàn)代碼,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12詳解python架構(gòu)?PyNeuraLogic超越Transformers
這篇文章主要為大家介紹了python使用?PyNeuraLogic超越Transformers示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03Python爬蟲定時(shí)計(jì)劃任務(wù)的幾種常見方法(推薦)
這篇文章主要介紹了Python爬蟲定時(shí)計(jì)劃任務(wù)的幾種常見方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01