1

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!

Nick Camps
  • 111
  • 1
  • 2
  • 13

3 Answers3

1

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.

Roland Smith
  • 42,427
  • 3
  • 64
  • 94
1

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

Community
  • 1
  • 1
tamasgal
  • 24,826
  • 18
  • 96
  • 135
1

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

hivert
  • 10,579
  • 3
  • 31
  • 56