神剑山庄资源网 Design By www.hcban.com
例如从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为下采样的结果,类型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对wav文件的重采样实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
标签:
Python,wav文件,重采样
神剑山庄资源网 Design By www.hcban.com
神剑山庄资源网
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
神剑山庄资源网 Design By www.hcban.com
暂无Python对wav文件的重采样实例的评论...
更新日志
2024年11月18日
2024年11月18日
- ABC唱片-鲍比达·新民乐《满江红》[APE+CUE]
- 许嵩.2014-不如吃茶去【海蝶】【WAV+CUE】
- 周笔畅.2024-HAVE.A.GOOD.NIGHT【SN.Music】【FLAC分轨】
- 周笔畅.2024-HAVE.A.NICE.DAY【SN.Music】【FLAC分轨】
- 证声音乐图书馆《真夏派对 x 迪斯可》[FLAC/分轨][380.78MB]
- 证声音乐图书馆《星空下 爵士钢琴》[320K/MP3][65.88MB]
- 证声音乐图书馆《星空下 爵士钢琴》[FLAC/分轨][283.95MB]
- 沙门怀一《竹山听雨HQ》头版限量[低速原抓WAV+CUE]
- 沙门怀一《于喁·壹HQ》头版限量编号[低速原抓WAV+CUE]
- 群星《魅音绝唱》黑胶CD【WAV】
- 腾格尔.2002-四十独白【风潮】【WAV+CUE】
- 陈明真.1992-到哪里找那么好的人【华星】【WAV+CUE】
- 黄凯芹.2012-廿五年3CD【环球】【WAV+CUE】
- 证声音乐图书馆《七夕 爵士情缘》[320K/MP3][64.8MB]
- 证声音乐图书馆《七夕 爵士情缘》[FLAC/分轨][327.79MB]