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多线程方式执行多个bat代码
Jun 07 Python
Python利用IPython提高开发效率
Aug 10 Python
Python使用requests及BeautifulSoup构建爬虫实例代码
Jan 24 Python
tensorflow获取变量维度信息
Mar 10 Python
解决python字典对值(值为列表)赋值出现重复的问题
Jan 20 Python
django框架基于模板 生成 excel(xls) 文件操作示例
Jun 19 Python
用Python实现校园通知更新提醒功能
Nov 23 Python
Spring Boot中使用IntelliJ IDEA插件EasyCode一键生成代码详细方法
Mar 20 Python
Django中从mysql数据库中获取数据传到echarts方式
Apr 07 Python
python 实现仿微信聊天时间格式化显示的代码
Apr 17 Python
简述 Python 的类和对象
Aug 21 Python
手把手教你配置JupyterLab 环境的实现
Feb 02 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
深入理解:XML与对象的序列化与反序列化
2013/06/08 PHP
smarty基础之拼接字符串的详解
2013/06/18 PHP
php 判断网页是否是utf8编码的方法
2014/06/06 PHP
让任务管理器中的CPU跳舞的js代码
2008/11/01 Javascript
jquery实现奇偶行赋值不同css值
2012/02/17 Javascript
写自已的js类库需要的核心代码
2012/07/16 Javascript
图片延迟加载的实现代码(模仿懒惰)
2013/03/29 Javascript
JS打开层/关闭层/移动层动画效果的实例代码
2013/05/11 Javascript
JavaScript中把数字转换为字符串的程序代码
2013/06/19 Javascript
jquery实现弹出窗口效果的实例代码
2013/11/28 Javascript
javascript实现别踩白块儿小游戏程序
2015/11/22 Javascript
jQuery-1.9.1源码分析系列(十一)DOM操作续之克隆节点
2015/12/01 Javascript
jQuery实现立体式数字动态增加(animate方法)
2016/12/21 Javascript
a标签置灰不可点击的实现方法
2017/02/06 Javascript
jQuery使用正则验证15/18身份证的方法示例
2017/04/27 jQuery
文本溢出插件jquery.dotdotdot.js使用方法详解
2017/06/22 jQuery
Javascript的console['']常用输入方法汇总
2018/04/26 Javascript
vue项目中使用tinymce编辑器的步骤详解
2018/09/11 Javascript
如何使用 vue + d3 画一棵树
2018/12/03 Javascript
js实现图片跟随鼠标移动效果
2019/10/16 Javascript
Vue实现简单计算器
2021/01/20 Vue.js
[04:36]DOTA2国际邀请赛 ti3精彩集锦
2013/08/19 DOTA
[02:28]PWL开团时刻DAY3——Ink Ice与DeMonsTer之间的勾心斗角
2020/11/03 DOTA
对于Python中线程问题的简单讲解
2015/04/03 Python
将Python代码打包为jar软件的简单方法
2015/08/04 Python
利用Python中的pandas库对cdn日志进行分析详解
2017/03/07 Python
解决pycharm的Python console不能调试当前程序的问题
2019/01/20 Python
python 进程的几种创建方式详解
2019/08/29 Python
python关于调用函数外的变量实例
2019/12/26 Python
Python字符串hashlib加密模块使用案例
2020/03/10 Python
html5版canvas自由拼图实例
2014/10/15 HTML / CSS
泰坦健身器材:Titan Fitness
2018/02/13 全球购物
银行优秀员工事迹
2014/02/06 职场文书
帮一个朋友写的求职信
2014/08/09 职场文书
详解CocosCreator消息分发机制
2021/04/16 Javascript
小程序实现悬浮按钮的全过程记录
2021/10/16 HTML / CSS