问题描述
我想使用 Python 访问 wav 文件并以允许我分析它的形式写入其内容(比如数组).
I want to use Python to access a wav-file and write its content in a form which allows me to analyze it (let's say arrays).
- 我听说audiolab"是一个合适的工具(它将 numpy 数组转换为 wav,反之亦然).
- 我已经安装了audiolab",但是我的 numpy 版本有问题(我无法从 numpy.testing 导入测试器").我有 1.1.1.numpy 的版本.
我在 numpy (1.4.0) 上安装了更新版本.但后来我得到了一组新的错误:
- I heard that "audiolab" is a suitable tool for that (it transforms numpy arrays into wav and vica versa).
- I have installed the "audiolab" but I had a problem with the version of numpy (I could not "from numpy.testing import Tester"). I had 1.1.1. version of numpy.
I have installed a newer version on numpy (1.4.0). But then I got a new set of errors:
回溯(最近一次调用最后一次):文件test.py",第 7 行,在导入 scikits.audiolab文件/usr/lib/python2.5/site-packages/scikits/audiolab/init.py",第 25 行,在从 pysndfile 导入格式信息,sndfile文件/usr/lib/python2.5/site-packages/scikits/audiolab/pysndfile/init.py",第 1 行,在从 _sndfile 导入 Sndfile,格式,available_file_formats,available_encodings文件numpy.pxd",第 30 行,在 scikits.audiolab.pysndfile._sndfile (scikits/audiolab/pysndfile/_sndfile.c:9632)ValueError: numpy.dtype 似乎不是正确的类型对象
Traceback (most recent call last): File "test.py", line 7, in import scikits.audiolab File "/usr/lib/python2.5/site-packages/scikits/audiolab/init.py", line 25, in from pysndfile import formatinfo, sndfile File "/usr/lib/python2.5/site-packages/scikits/audiolab/pysndfile/init.py", line 1, in from _sndfile import Sndfile, Format, available_file_formats, available_encodings File "numpy.pxd", line 30, in scikits.audiolab.pysndfile._sndfile (scikits/audiolab/pysndfile/_sndfile.c:9632)ValueError: numpy.dtype does not appear to be the correct type object
我放弃了使用 audiolab 并认为我可以使用wave"包来读取 wav 文件.我问了一个问题,但人们建议改用 scipy.好的,我决定专注于 scipy(我有 0.6.0. 版本).
I gave up to use audiolab and thought that I can use "wave" package to read in a wav-file. I asked a question about that but people recommended to use scipy instead. OK, I decided to focus on scipy (I have 0.6.0. version).
但是当我尝试执行以下操作时:
But when I tried to do the following:
从 scipy.io 导入 wavfile
x = wavfile.read('/usr/share/sounds/purple/receive.wav')
from scipy.io import wavfile
x = wavfile.read('/usr/share/sounds/purple/receive.wav')
我得到以下信息:
Traceback (most recent call last):
File "test3.py", line 4, in <module>
from scipy.io import wavfile
File "/usr/lib/python2.5/site-packages/scipy/io/__init__.py", line 23, in <module>
from numpy.testing import NumpyTest
ImportError: cannot import name NumpyTest
- 所以,我放弃了使用 scipy.我可以只使用wave包吗?我不需要太多.我只需要以人类可读的格式获取 wav 文件的内容,然后我会弄清楚如何处理它.
推荐答案
你试过 wave 模块了吗?它有更少的依赖:
Have you tried the wave module? It has fewer dependencies:
http://docs.python.org/library/wave.html
def everyOther (v, offset=0):
return [v[i] for i in range(offset, len(v), 2)]
def wavLoad (fname):
wav = wave.open (fname, "r")
(nchannels, sampwidth, framerate, nframes, comptype, compname) = wav.getparams ()
frames = wav.readframes (nframes * nchannels)
out = struct.unpack_from ("%dh" % nframes * nchannels, frames)
# Convert 2 channles to numpy arrays
if nchannels == 2:
left = array (list (everyOther (out, 0)))
right = array (list (everyOther (out, 1)))
else:
left = array (out)
right = left
这篇关于使用 Python [摘要] 读取 wav 文件的最简单方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!