Create ringtones out of WAV and MP3 files on Linux

by Rudd-O published 2006/09/18 13:45:00 GMT+0, last modified 2013-06-26T03:24:18+00:00
If you have one of those old Nokia phones that only allows AMR files to be used as ringtones, here's a short recipe to create them.

 As long as you have a WAV or MP3 file, the SoX package, and the GStreamer gstreamer-plugins-ugly package installed, you can use these scripts to convert from MP3 or WAV to AMR, and back from AMR to WAV. Perfect for creating ringtones for your mobile phone out of sound clips or favorite songs!

The wavtoamr script:




if [ "$2" == "" ] ; then echo "usage: wavtoamr wavfile amrfile" exit 1 fi

midwav=mktemp /tmp/wavtoamrXXXXX midamr=mktemp /tmp/wavtoamrXXXXX sox "$1" -r 8000 -c 1 "$midwav.wav" avg resample && gst-launch-0.10 filesrc location="$midwav.wav" ! wavparse ! amrnbenc ! filesink location="$midamr" && echo '#!AMR' > "$2" && cat "$midamr" >> "$2" rm -f "$midwav.wav" rm -f "$midamr"


The amrtowav script:




if [ "$2" == "" ] ; then echo "usage: amrtowav amrfile wavfile" exit 1 fi

tmpfile=mktemp dd if="$1" of="$tmpfile" bs=1 count=10000000 skip=6 && gst-launch-0.10 filesrc location="$tmpfile" ! amrnbparse ! amrnbdec ! wavenc ! filesink location="$2" rm -f "$tmpfile"


The mp3toamr script:




if [ "$2" == "" ] ; then echo "usage: mp3toamr mp3file amrfile" exit 1 fi

midwav=mktemp /tmp/mp3toamrXXXXX

gst-launch-0.10 filesrc location="$1" ! mad ! audioconvert ! wavenc ! filesink location="$midwav" && wavtoamr "$midwav" "$2"

rm -f "$midwav"


To convert, just run the appropriate command against the file you want converted.

Homework for you: improve error handling and make it work without temporary files -- that is, with standard input and standard output pipes, and redirecting output from the invoked commands to standard error. Don't forget to send me the improvements!