8

我想使用 Python 访问一个 wav 文件并以允许我分析它的形式写入它的内容(比如说数组)。

  1. 我听说“audiolab”是一个合适的工具(它将numpy数组转换为wav,反之亦然)。
  2. 我已经安装了“audiolab”,但是 numpy 的版本有问题(我不能“从 numpy.testing 导入测试器”)。我有 1.1.1。numpy 的版本。
  3. 我在 numpy (1.4.0) 上安装了更新版本。但后来我得到了一组新的错误:

    Traceback(最近一次调用最后):文件“test.py”,第 7 行,在 import scikits.audiolab 文件“/usr/lib/python2.5/site-packages/scikits/audiolab/init .py ”,第 25 行,从 pysndfile 导入 formatinfo,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 does not seem to be the correct type object

  4. 我放弃了使用 audiolab 并认为我可以使用“wave”包来读取 wav 文件。我问了一个问题,但人们建议改用 scipy。好的,我决定专注于 scipy(我有 0.6.0. 版本)。

  5. 但是当我尝试执行以下操作时:

    从 scipy.io 导入 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
  1. 所以,我放弃了使用 scipy。我可以只使用wave包吗?我不需要太多。我只需要具有人类可读格式的 wav 文件内容,然后我会弄清楚如何处理它。
4

8 回答 8

13

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
于 2010-04-08T18:05:21.490 回答
6

我在 std lib 中的 wave 模块上编写了一个简单的包装器。它被称为pydub,它有一种方法可以从音频数据中读取样本作为整数。

>>> from pydub import AudioSegment
>>> song = AudioSegment.from_wav("your_song.wav")
<pydub.audio_segment.AudioSegment at 0x1068868d0>

>>> # This song is stereo
>>> song.channels
2

>>> # get the 5000th "frame" in the song
>>> frame = song.get_frame(5000)

>>> sample_left, sample_right = frame[:2], frame[2:]
>>> def sample_to_int(sample): 
        return int(sample.encode("hex"), 16)

>>> sample_to_int(sample_left)
8448

>>> sample_to_int(sample_right)
9984

希望这会有所帮助

于 2012-12-05T22:58:44.977 回答
5

您还可以使用 wave 模块和 numpy.fromstring() 函数将其转换为数组

import wave
import numpy

fp = wave.open('test.wav')
nchan = fp.getnchannels()
N = fp.getnframes()
dstr = fp.readframes(N*nchan)
data = numpy.fromstring(dstr, numpy.int16)
data = numpy.reshape(data, (-1,nchan))
于 2014-06-20T01:46:21.857 回答
5

这对我来说已经足够了

import numpy as np
x = np.fromfile(open('song.wav'),np.int16)[24:]

它忽略了前 24 个值,因为那不是音频,而是标题。

另外,如果文件是立体声的,你的通道会有交替的索引,所以我通常先用 Audacity 把它减少为单声道。

于 2011-04-22T18:10:19.277 回答
2

在尝试了很多不起作用的东西之后,我使用了Use (Python) Gstreamer 中的解码库来解码音频(到 PCM 数据)并构建一个函数来将原始 pcm 数据解析为 scipy 数组。

很好,可以打开 gstreamer 可以打开的任何音频文件:http: //gist.github.com/592776(有关使用信息,请参阅测试和文件末尾)

于 2010-09-22T23:36:04.573 回答
1

audiolab是最好的方法,但它并不适用于所有环境,开发人员也没有在开发它。我仍在使用 Python 2.5,所以我可以使用它。

你安装了libsndfile吗?

于 2010-03-17T03:37:45.580 回答
1

audiolab似乎不再维护,您应该尝试PySoundFile

安装很简单:

pip install PySoundFile --user

并读取声音文件:

import soundfile as sf
x, fs = sf.read('/usr/share/sounds/purple/receive.wav')

查看有关用于处理声音文件的不同 Python 库的概述

于 2015-09-17T11:56:21.803 回答
0

pydub提供了一个更简单的解决方案,无需安装任何依赖项(对于 wav 文件)。我目前在生产中使用这种方法没有任何问题。

from pydub import AudioSegment
awesome_song = AudioSegment.from_wav('awesome_song.wav')
print('Duration in seconds is {}'.format(awesome_song.duration_seconds))
于 2017-08-18T11:35:49.517 回答