使用 Python [摘要] 读取 wav 文件的最简单方法是什么?

发布于 2024-08-17 19:15:49 字数 1440 浏览 9 评论 0原文

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

  1. 我听说“audiolab”是一个合适的工具(它将 numpy 数组转换为 wav,反之亦然)。
  2. 我已经安装了“audiolab”,但 numpy 的版本有问题(我无法“from numpy.testing import Tester”)。我有1.1.1。 numpy 的版本。
  3. 我已经在 numpy (1.4.0) 上安装了更新版本。但后来我遇到了一组新的错误:

    回溯(最近一次调用最后一次): 文件“test.py”,第 7 行,位于 导入 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、格式、可用文件格式、可用编码 文件“numpy.pxd”,第 30 行,位于 scikits.audiolab.pysndfile._sndfile 中 (scikits/audiolab/pysndfile/_sndfile.c:9632) ValueError: numpy.dtype 似乎不是正确的类型对象

  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 文件内容,然后我就会弄清楚如何处理它。

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).

  1. I heard that "audiolab" is a suitable tool for that (it transforms numpy arrays into wav and vica versa).
  2. 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.
  3. I have installed a newer version on numpy (1.4.0). But then I got a new set of errors:

    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

  4. 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).

  5. But when I tried to do the following:

    from scipy.io import wavfile
    x = wavfile.read('/usr/share/sounds/purple/receive.wav')

I get the following:

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. So, I gave up to use scipy. Can I use just wave package? I do not need much. I just need to have content of wav-file in human readable format and than I will figure out what to do with that.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

风蛊 2024-08-24 19:15:49

您尝试过波形模块吗?它的依赖项较少:

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

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
看海 2024-08-24 19:15:49

我在标准库中的 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

希望这有帮助

I wrote a simple wrapper over the wave module in the std lib. it's called pydub and it has a method for reading samples from the audio data as ints.

>>> 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

Hopefully this helps

病女 2024-08-24 19:15:49

这对我来说已经足够好了

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

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

另外,如果文件是立体声,您的通道将具有交替索引,因此我通常首先使用 Audacity 将其降低为单声道。

This is good enough for me

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

It ignores the first 24 values, because that's not audio, it the header.

Also, if the file was stereo, your channels will have alternating indexes, So I usually just reduce it to mono with Audacity first.

混浊又暗下来 2024-08-24 19:15:49

您还可以使用 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))

You can also use the wave module along with the numpy.fromstring() function to convert it to an array

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))
刘备忘录 2024-08-24 19:15:49

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

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

After trying so many things that does not work I used the decode library from Use (Python) Gstreamer to decode audio (to PCM data) and build a function to parse the raw pcm data into a scipy array.

It's nice and can open any audio file that gstreamer can open:
http://gist.github.com/592776 (see Test and the end of the file for usage info)

拥有 2024-08-24 19:15:49

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

您是否安装了libsndfile

audiolab is the best way, but it doesn't work in every environment and the developer's not working on it. I'm still using Python 2.5 so I can use it.

Did you install libsndfile?

水水月牙 2024-08-24 19:15:49

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

安装很简单:

pip install PySoundFile --user

并读取声音文件:

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

看看这个 用于处理声音文件的不同 Python 库的概述

audiolab seems to be not maintained anymore, you should try PySoundFile.

Installation is simple:

pip install PySoundFile --user

And reading a sound file as well:

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

Have a look at this overview about different Python libraries for handling sound files.

↘紸啶 2024-08-24 19:15:49

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

from pydub import AudioSegment
awesome_song = AudioSegment.from_wav('awesome_song.wav')
print('Duration in seconds is {}'.format(awesome_song.duration_seconds))

pydub provides an even easier solution without any dependencies needing to be installed (for wav files). I'm currently using this method in production without any issues.

from pydub import AudioSegment
awesome_song = AudioSegment.from_wav('awesome_song.wav')
print('Duration in seconds is {}'.format(awesome_song.duration_seconds))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文