Python对wav文件的重采样实例

 更新时间:2020年02月25日 10:04:31   作者:AYE89  
今天小编就为大家分享一篇Python对wav文件的重采样实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

例如从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 matplotlib:plt.scatter() 大小和颜色参数详解

    python matplotlib:plt.scatter() 大小和颜色参数详解

    这篇文章主要介绍了python matplotlib:plt.scatter() 大小和颜色参数详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-04-04
  • Python calendar模块详情

    Python calendar模块详情

    这篇文章主要介绍了 Python calendar模块,Python 专门为了处理日历提供了calendar日历模块,下面文章基于time模块和datetime模块展开,具有一定的参考价值,需要的朋友可以参考一下
    2021-11-11
  • python数字图像处理skimage读取显示与保存图片

    python数字图像处理skimage读取显示与保存图片

    这篇文章主要为大家介绍了python数字图像处理使用skimage读取显示与保存图片示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-06-06
  • Django 项目重命名的实现步骤解析

    Django 项目重命名的实现步骤解析

    这篇文章主要介绍了Django 项目重命名的实现步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-08-08
  • matplotlib简介,安装和简单实例代码

    matplotlib简介,安装和简单实例代码

    这篇文章主要介绍了matplotlib简介,安装和简单实例代码,具有一定借鉴价值,需要的朋友可以参考下。
    2017-12-12
  • 让你相见恨晚的十个Python骚操作

    让你相见恨晚的十个Python骚操作

    这篇文章主要给大家介绍了十个让你相见恨晚的Python骚操作,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-11-11
  • Pytorch实现网络部分层的固定不进行回传更新问题及思路详解

    Pytorch实现网络部分层的固定不进行回传更新问题及思路详解

    这篇文章主要介绍了Pytorch实现网络部分层的固定不进行回传更新,实现思路就是利用tensor的requires_grad,每一个tensor都有自己的requires_grad成员,值只能为True和False,具体内容详情跟随小编一起看看吧
    2021-08-08
  • Python程序打包exe报错的几种解决方法

    Python程序打包exe报错的几种解决方法

    本文主要介绍了Python程序打包exe报错的几种解决方法,文中通过几种解决方法的介绍非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2023-08-08
  • Python基于gevent实现高并发代码实例

    Python基于gevent实现高并发代码实例

    这篇文章主要介绍了Python基于gevent实现高并发代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-05-05
  • python beautifulsoup4 模块详情

    python beautifulsoup4 模块详情

    本文主要介绍了python beautifulsoup4模块详情,BeautifulSoup4是一款python解析库,主要用于解析HTML和XML,在爬虫知识体系中解析 HTML 会比较多一些,下文更多相关内容,需要的小伙伴可以参考一下
    2022-05-05

最新评论