Python对wav文件的重采样实例


Posted in Python onFebruary 25, 2020

例如从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 相关文章推荐
Python subprocess模块学习总结
Mar 13 Python
如何高效使用Python字典的方法详解
Aug 31 Python
django表单实现下拉框的示例讲解
May 29 Python
python 读取文本文件的行数据,文件.splitlines()的方法
Jul 12 Python
python复制列表时[:]和[::]之间有什么区别
Oct 16 Python
pybind11和numpy进行交互的方法
Jul 04 Python
解决pycharm下os.system执行命令返回有中文乱码的问题
Jul 07 Python
django之状态保持-使用redis存储session的例子
Jul 28 Python
Python树莓派学习笔记之UDP传输视频帧操作详解
Nov 15 Python
Keras 数据增强ImageDataGenerator多输入多输出实例
Jul 03 Python
用python 绘制茎叶图和复合饼图
Feb 26 Python
Python可变与不可变数据和深拷贝与浅拷贝
Apr 06 Python
python实现打砖块游戏
Feb 25 #Python
Python实现企业微信机器人每天定时发消息实例
Feb 25 #Python
Django 设置多环境配置文件载入问题
Feb 25 #Python
python中resample函数实现重采样和降采样代码
Feb 25 #Python
python实现的分层随机抽样案例
Feb 25 #Python
Python可变对象与不可变对象原理解析
Feb 25 #Python
Python 使用 environs 库定义环境变量的方法
Feb 25 #Python
You might like
PHP.MVC的模板标签系统(二)
2006/09/05 PHP
php 获取全局变量的代码
2011/04/21 PHP
PHP 自定义错误处理函数trigger_error()
2013/03/26 PHP
php使用curl检测网页是否被百度收录的示例分享
2014/01/31 PHP
PHP调用.NET的WebService 简单实例
2015/03/27 PHP
php对象和数组相互转换的方法
2015/05/12 PHP
json 定义
2008/06/10 Javascript
Javascript调用C#代码
2011/01/17 Javascript
jquery事件机制扩展插件 jquery鼠标右键事件。
2011/12/26 Javascript
Jquery Uploadify上传带进度条的简单实例
2014/02/12 Javascript
js判断手机号是否正确并返回的实现代码
2017/01/17 Javascript
Python-基础-入门 简介
2014/08/09 Python
Python中有趣在__call__函数
2015/06/21 Python
使用python实现生成用户信息
2017/03/20 Python
Python内置函数 next的具体使用方法
2017/11/24 Python
python thrift搭建服务端和客户端测试程序
2018/01/17 Python
python中字符串变二维数组的实例讲解
2018/04/03 Python
Python Tkinter模块实现时钟功能应用示例
2018/07/23 Python
Python解决pip install时出现的Could not fetch URL问题
2019/08/01 Python
如何通过安装HomeBrew来安装Python3
2020/12/23 Python
Python图像处理之膨胀与腐蚀的操作
2021/02/07 Python
html5 Canvas画图教程(9)—canvas中画出矩形和圆形
2013/01/09 HTML / CSS
欧洲品牌瓷器餐具网上商店:Porzellantreff.de
2018/04/04 全球购物
Notino法国:购买香水和化妆品
2019/04/15 全球购物
美国沙龙美发产品购物网站:Hair.com by L’Oreal
2020/11/09 全球购物
JSF面试题:如何管量web层中的Bean,用什么标签。如何通过jsp页面与Bean绑定在一起进行处理?
2012/10/05 面试题
社区安全检查制度
2014/02/03 职场文书
家长写给孩子的评语
2014/04/18 职场文书
2014年教师党员公开承诺书
2014/05/28 职场文书
节能环保口号
2014/06/12 职场文书
十佳青年事迹材料
2014/08/21 职场文书
大学生实习证明范文(5篇)
2014/09/18 职场文书
学术会议通知
2015/04/15 职场文书
新党员入党决心书
2015/09/22 职场文书
《雷雨》教学反思
2016/02/20 职场文书
2019年中,最受大众欢迎的6本新书
2019/08/07 职场文书