Seven Minute Server

Aug 20, 2010 - 2 minute read - Fun

Goofing with Audio

My partner’s doing some speech analysis stuff, and introduced me to sox, a self-described “Swiss Army Knife of sound processing.” I was goofing with it today to convert spectrograms of mp3s to animated GIFs. This is quick and very dirty, but should work.


On Ubuntu, install sox, the mp3 plug-in for sox, and imagemagick:

sudo apt-get install sox libsox-fmt-mp3 imagemagick


  1. Copy the text of this script into a file:

      # Get input file
      if [ -z $1 ]; then
        echo "No mp3 file provided. Use ./makemeasammich /path/to/mymp3.mp3"
        exit 1;
       # Get seconds in audio file
       s=`sox "$audiofile" -n stat 2>&1 |grep Length |awk '{print $3}'`
       seconds=`echo $s/1 |bc`
       echo $audiofile is $seconds seconds long
       while [ $slice -lt $seconds ]
         echo "Processing seconds starting at: $slice"
         sox "$audiofile" -n remix -r trim $slice spectrogram
         mv spectrogram.png $slice.png
         slice=`expr $slice + 9`
      for i in `ls *.png`; do
        convert $i $i.gif;
      # use colors 32 to compress a bit
      convert -colors 32 -delay 100 -loop 1 *.gif $audiofile_animated.gif
  2. Run the script like so:

    sh /home/jen/mysong.mp3

And you should get something like the following, except it’ll be larger and animated:

Audio Output

You can change the convert line to change animation/looping settings; removing “colors -32” will give you better quality (much larger filesize).