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之PyQt6对话框的实现

    Python之PyQt6对话框的实现

    这篇文章主要介绍了Python之PyQt6对话框的实现,文章内容详细,简单易懂,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2023-01-01
  • django接入新浪微博OAuth的方法

    django接入新浪微博OAuth的方法

    这篇文章主要介绍了django接入新浪微博OAuth的方法,实例分析了django针对新浪微博OAuth接口的接入技巧,需要的朋友可以参考下
    2015-06-06
  • matlab绘制局部放大图图文教程

    matlab绘制局部放大图图文教程

    这篇文章主要给大家介绍了关于matlab绘制局部放大图的相关资料,所谓局部放大即呈现子图,以显示局部细节,需要的朋友可以参考下
    2023-07-07
  • python识别文字(基于tesseract)代码实例

    python识别文字(基于tesseract)代码实例

    这篇文章主要介绍了python识别文字(基于tesseract)代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-08-08
  • TensorFlow用expand_dim()来增加维度的方法

    TensorFlow用expand_dim()来增加维度的方法

    今天小编就为大家分享一篇TensorFlow用expand_dim()来增加维度的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-07-07
  • Python实现.gif图片拆分为.png图片的简单示例

    Python实现.gif图片拆分为.png图片的简单示例

    有时候需要把GIF图片分解成一张一张的静态图,jpg或者png格式,下面这篇文章主要给大家介绍了关于Python实现.gif图片拆分为.png图片的相关资料,需要的朋友可以参考下
    2023-01-01
  • 在pycharm中为项目导入anacodna环境的操作方法

    在pycharm中为项目导入anacodna环境的操作方法

    这篇文章主要介绍了在pycharm中为项目导入anacodna环境的操作方法,本文图文并茂通过实例详解的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-02-02
  • Python命名空间及作用域原理实例解析

    Python命名空间及作用域原理实例解析

    这篇文章主要介绍了Python命名空间及作用域原理实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-08-08
  • Python中时间类型的JSON数据转换

    Python中时间类型的JSON数据转换

    在Python中,处理时间和日期数据以及与JSON数据的相互转换是常见的任务,本文主要为大家详细如何在Python中处理时间类型的JSON数据转换,需要的小伙伴可以参考下
    2024-02-02
  • 8种常用的Python工具

    8种常用的Python工具

    这篇文章主要介绍了8种常用的Python工具,帮助大家更好的学习Python,感兴趣的朋友可以了解下
    2020-08-08

最新评论