Friday, August 20, 2010

Goofing with Audio

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

Requirements

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

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

Running

  1. Copy the text of this crappy script into a file:
    #!/bin/bash
    # Get input file
    audiofile=$1

    if [ -z $1 ]; then
    echo "No mp3 file provided. Use ./makemeasammich /path/to/mymp3.mp3"
    exit 1;
    fi
    # 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
    slice=0
    while [ $slice -lt $seconds ]
    do
    echo "Processing seconds starting at: $slice"
    sox "$audiofile" -n remix -r trim $slice spectrogram
    mv spectrogram.png $slice.png
    slice=`expr $slice + 9`
    done
    for i in `ls *.png`; do convert $i $i.gif; done;
    #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 makemeasammich.sh /home/jenisgoofy/mysong.mp3

    And you should get something like the following, except it'll be larger and animated (BlogSpot doesn't support animated GIFs and I'm too lazy to copy this anywhere right now):

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

0 comments:

Post a Comment