首页 > python教程

Python对wav文件的重采样实例

时间:2020-08-11 python教程 查看: 1041

例如从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实现企业微信机器人每天定时发消息实例
输入字:
相关知识
Python 实现图片色彩转换案例

我们在看动漫、影视作品中,当人物在回忆过程中,体现出来的画面一般都是黑白或者褐色的。本文将提供将图片色彩转为黑白或者褐色风格的案例详解,感兴趣的小伙伴可以了解一下。

python初学定义函数

这篇文章主要为大家介绍了python的定义函数,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助,希望能够给你带来帮助

图文详解Python如何导入自己编写的py文件

有时候自己写了一个py文件,想要把它导入到另一个py文件里面,所以下面这篇文章主要给大家介绍了关于Python如何导入自己编写的py文件的相关资料,需要的朋友可以参考下

python二分法查找实例代码

二分算法是一种效率比较高的查找算法,其输入的是一个有序的元素列表,如果查找元素包含在列表中,二分查找返回其位置,否则返回NONE,下面这篇文章主要给大家介绍了关于python二分法查找的相关资料,需要的朋友可以参考下