Nick Camps
Nick Camps

Reputation: 111

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

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. 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. Has anyone done anything similar or could recommend some libraries to help with this? Thanks!

Upvotes: 1

Views: 3291

Answers (3)

Roland Smith
Roland Smith

Reputation: 43495

You can use the subprocess module to call 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:

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.

You can find a more elaborate example here.

Upvotes: 1

hivert
hivert

Reputation: 10667

The Pymedia library seems to be stable and to deals with what you need.

Upvotes: 1

tamasgal
tamasgal

Reputation: 26259

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/

The rest is done by Fourier mathematics etc.:

How to analyse frequency of wave file

and have a look at the python module wave:

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

Upvotes: 1

Related Questions