简体   繁体   English

从mp3读取振幅数据

[英]Read amplitude data from mp3

I am trying to write some code that will extract the amplitude data from an mp3 as a function of time. 我正在尝试编写一些代码,这些代码将从mp3中提取幅度数据作为时间的函数。 I wrote up a rough version on MATLAB a while back using this function: http://labrosa.ee.columbia.edu/matlab/mp3read.html However I am having trouble finding a Python equivalent. 我在MATLAB上写了一个粗略的版本,不久前使用了这个函数: http//labrosa.ee.columbia.edu/matlab/mp3read.html但是我找不到Python的等价物。

I've done a lot of research, and so far I've gathered that I need to use something like mpg321 to convert the .mp3 into a .wav. 我已经做了很多研究,到目前为止我已经收集到了我需要使用像mpg321这样的东西来将.mp3转换为.wav。 I haven't been able to figure out how to get that to work. 我一直无法弄清楚如何让它发挥作用。

The next step will be reading the data from the .wav file, which I also haven't had any success with. 下一步将是从.wav文件中读取数据,我也没有取得任何成功。 Has anyone done anything similar or could recommend some libraries to help with this? 有没有人做过类似的事情或者可以推荐一些图书馆帮助解决这个问题 Thanks! 谢谢!

You can use the subprocess module to call mpg123 : 您可以使用subprocess模块调用mpg123

import subprocess
import sys

inname = 'foo.mp3'
outname = 'out.wav'
try:
    subprocess.check_call(['mpg123', '-w', outname, inname])
except CalledProcessError as e:
    print e
    sys.exit(1)

For reading wav files you should use the wave module, like this: 要读取wav文件,你应该使用wave模块,如下所示:

import wave
import numpy as np

wr = wave.open('input.wav', 'r')
sz = 44100 # Read and process 1 second at a time.
da = np.fromstring(wr.readframes(sz), dtype=np.int16)
wr.close()
left, right = da[0::2], da[1::2]

After that, left and right contain the samples of the same channels. 在此之后, leftright含有相同信道的样本。

You can find a more elaborate example here . 你可以在这里找到更详细的例子。

Here is a project in pure python where you can decode an MP3 file about 10x slower than realtime: http://portalfire.wordpress.com/category/pymp3/ 这是一个纯python项目,你可以解码比实时慢10倍的MP3文件: http//portalfire.wordpress.com/category/pymp3/

The rest is done by Fourier mathematics etc.: 其余的由傅立叶数学等完成:

How to analyse frequency of wave file 如何分析波形文件的频率

and have a look at the python module wave : 并看看python模块wave

http://docs.python.org/2/library/wave.html http://docs.python.org/2/library/wave.html

Pymedia库似乎很稳定, 可以满足您的需求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM