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 读取WAV文件并绘制波形图
aa Python 读取WAV文件并绘制波形图 ffmpeg -i test_pcm_mulaw.wav -f wav -codec:a pcm_s16le -ar 8000 -ac 1 out.wa ...
- Python解析Wav文件并绘制波形的方法
资源下载 #本文PDF版下载 Python解析Wav文件并绘制波形的方法 #本文代码下载 Wav波形绘图代码 #本文实例音频文件night.wav下载 音频文件下载 (石进-夜的钢琴曲) 前言 在现在 ...
- python 播放 wav 文件
未使用其他库, 只是使用 pywin32 调用系统底层 API 播放 wav 文件. # Our raison d'etre - playing sounds import pywintypes im ...
- python读取wav文件并播放[pyaudio/wave]
#!/usr/bin/python # encoding:utf-8 import pyaudio import wave CHUNK = 1024 # 从目录中读取语音 wf = wave.open ...
- Python绘制wav文件音频图(静态)[matplotlib/wave]
#!/usr/bin/env python # -*- coding: utf-8 -*- """ 绘制波形图 plottingWaveform.py "&qu ...
- python统计wav文件的时长
import wave import os.path # 音频存放文件夹绝对路径 filedir = '/Users/111/PycharmProjects/TextClassify/wav' lis ...
- 【转】Python处理wave文件
#本文PDF版下载 Python解析Wav文件并绘制波形的方法 #本文代码下载 Wav波形绘图代码 #本文实例音频文件night.wav下载 音频文件下载 (石进-夜的钢琴曲) 前言 在现在繁忙的生活 ...
- 纯Python给ulaw wav文件加头
最近在处理wav相关文件,碰见一工具产生的ualw文件不带header,顺手用python给wav格式文件加头处理,让普通播放器也能播放. (原文:http://www.cnblogs.com/ryh ...
- wav文件系列_2_Python实现读写
本文介绍了 Python 实现音频读写的方法.Python wave 模块提供便捷的 wav 文件操作.该模块并不支持压缩与解压,但支持单声道/立体声的转换. 参考: [1] wave — Read ...
随机推荐
- videojs播放直播源rtmp时画面在左上角解决方案
问题描述:https://stackoverflow.com/questions/30383135/videojs-live-rtmp-stream-player-and-video-size-iss ...
- Hive元数据找回
如果不小心删除了了hive的元数据文件(/user/hive/warehouse),只要先前core-site.xml文件中设置了fs.trash.interval属性就可以找回.hdfs会为用户创建 ...
- React组件性能调优
React是一个专注于UI层的框架,它使用虚拟DOM技术,以保证它UI的高速渲染:使用单向数据流,因此它数据绑定更加简单:那么它内部是如何保持简单高效的UI渲染呢?这种渲染机制有可能存在什么性能问题呢 ...
- ROS使用小知识点
输入 rosrun rqt_graph rqt_graph 可以打开一个界面观察节点与话题的关系 绿色和蓝色的是节点 红色的是话题 查看ros中额的tf转换信息 rosrun rqt_tf_tree ...
- SERvet的表单
首先要建立好数据库. 好了,先写好两个静态页面,分别负责登录和注册. 代码清单1:login.jsp(登录页面) <%-- Document : login Created on : 2013- ...
- 解决OSError: cannot identify image file <_io.BytesIO object at 0x000001ABB1D9DE60>
解决方法: 不支持.jpg文件,用opencv将文件转为png格式即可.
- 2017 cinemachine
只有2017以上才可以使用~ 导入Asset store中的cinemachine,会在上方显示cinemachine.每创建一个虚拟摄像机,就会在主摄像机下创建组件 cinemachine Bra ...
- P2930 [USACO09HOL]假期绘画Holiday Painting
线段树水题,考虑到只有15列,所以我们对于每一列,我们都去维护一个线段树. 现在来考虑一下修改操作,因为每次修改的时候,我们都是将黑的改成白的,白的改成黑的,所以我们对线段树的每个节点维护当前这段区间 ...
- Java Native
native 什么是Native Method 简单地讲,一个Native Method就是一个java调用非java代码的接口.一个Native Method是这样一个java的方法:该方法的实现由 ...
- Flagr 架构
来自官方文档 https://checkr.github.io/flagr/#/flagr_overview There are three components in the flagr, Flag ...