Search code examples
pythonpython-2.7mp3wav

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!


Solution

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