脚本专栏 
首页 > 脚本专栏 > 浏览文章

Python对wav文件的重采样实例

(编辑:jimmy 日期: 2024/11/19 浏览:3 次 )

例如从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 序列化和反序列化库 MarshMallow 的用法实例代码
下一篇:python用pip install时安装失败的一系列问题及解决方法
一句话新闻
一文看懂荣耀MagicBook Pro 16
荣耀猎人回归!七大亮点看懂不只是轻薄本,更是游戏本的MagicBook Pro 16.
人们对于笔记本电脑有一个固有印象:要么轻薄但性能一般,要么性能强劲但笨重臃肿。然而,今年荣耀新推出的MagicBook Pro 16刷新了人们的认知——发布会上,荣耀宣布猎人游戏本正式回归,称其继承了荣耀 HUNTER 基因,并自信地为其打出“轻薄本,更是游戏本”的口号。
众所周知,寻求轻薄本的用户普遍更看重便携性、外观造型、静谧性和打字办公等用机体验,而寻求游戏本的用户则普遍更看重硬件配置、性能释放等硬核指标。把两个看似难以相干的产品融合到一起,我们不禁对它产生了强烈的好奇:作为代表荣耀猎人游戏本的跨界新物种,它究竟做了哪些平衡以兼顾不同人群的各类需求呢?
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网 SiteMap