Python对wav文件的重采样实例

所属分类: 脚本专栏 / python 阅读数: 475
收藏 0 赞 0 分享

例如从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实现图像几何变换的方法,实例分析了Python基于Image模块实现图像翻转、旋转、改变大小等操作的相关技巧,非常简单实用,需要的朋友可以参考下
收藏 0 赞 0 分享

Python中的urllib模块使用详解

这篇文章主要介绍了Python中的urllib模块使用详解,是Python入门学习中的基础知识,需要的朋友可以参考下
收藏 0 赞 0 分享

Python的多态性实例分析

这篇文章主要介绍了Python的多态性,以实例形式深入浅出的分析了Python在面向对象编程中多态性的原理与实现方法,需要的朋友可以参考下
收藏 0 赞 0 分享

python生成IP段的方法

这篇文章主要介绍了python生成IP段的方法,涉及Python文件读写及随机数操作的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

python操作redis的方法

这篇文章主要介绍了python操作redis的方法,包括Python针对redis的连接、设置、获取、删除等常用技巧,具有一定参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

python妹子图简单爬虫实例

这篇文章主要介绍了python妹子图简单爬虫,实例分析了Python爬虫程序所涉及的页面源码获取、进度显示、正则匹配等技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

分析用Python脚本关闭文件操作的机制

这篇文章主要介绍了分析用Python脚本关闭文件操作的机制,作者分Python2.x版本和3.x版本两种情况进行了阐述,需要的朋友可以参考下
收藏 0 赞 0 分享

python实现搜索指定目录下文件及文件内搜索指定关键词的方法

这篇文章主要介绍了python实现搜索指定目录下文件及文件内搜索指定关键词的方法,可实现针对文件夹及文件内关键词的搜索功能,需要的朋友可以参考下
收藏 0 赞 0 分享

python中getaddrinfo()基本用法实例分析

这篇文章主要介绍了python中getaddrinfo()基本用法,实例分析了Python中使用getaddrinfo方法进行IP地址解析的基本技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

python查找指定具有相同内容文件的方法

这篇文章主要介绍了python查找指定具有相同内容文件的方法,涉及Python针对文件操作的相关技巧,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多