mp3 fft with python

Dmitry Borisov jbors at mail.ru
Wed Aug 18 08:46:02 CEST 2004


From: "Garett Shulman"
Sent: Tuesday, August 17, 2004 6:42 PM
Subject: mp3 fft with python


> Hello, I would like to do a fft on an mp3 in python. I beleive I have
> all of the fft stuff straight in my mind but am not sure of the best way
> to get the sample data into a python array. I ran accross a web site a
> while back which suggested using sox to convert a wav file into a raw
> sample file and then open the raw file with python. However, I did not
> bookmark this site when I came accross it and cannot seem to find it
> now. If anyone has any suggestions as to a good way to get sample data
> out of an mp3 and into a python array for an fft I would greatly
> appreciate your suggestions. Thanks a lot. -Garett

Not sure if it'll work for you. But here it is:

SAMPLES= 500
BANDS= 20
NUM_FREQS= 256

import pymedia.audio.acodec as acodec
import pymedia.audio.sound as sound

dec= acodec.Decoder( 'mp3' )
analyzer= sound.SpectrAnalyzer( 1, SAMPLES, NUM_FREQS )
...
s= f.read( 20000 )
while len( s ):
  r= dec.decode( s )
  if len( r.data ):
      # Get fft info as bands
      bands= analyzer.asBands( BANDS, s )
      freqs= analyzer.asFrequencies( s )
      ...

http://pymedia.sourceforge.net

You may need to take a look at the examples/sound_viz.py in the source
tarball.
Dmitry/




More information about the Python-list mailing list