batch-converting audio using soX (for arduino waveshield)

i’m currently experimenting with the waveshield a.k.a. audioshield for arduino by ladyada. it’s a pretty handy extension for arduino thats add’s the capability to output audio and read/write sd-cards with arduino, they also provide nice libraries to work with. in respect to  the limited calculating power of the arduino it can only play uncompressed wave files that are mono, have a samplerate of maximum 22khz and a resolution of 16 bit. this is quite ok for most things you would like to do with it, but you first have to convert most wave files you have to this format.

on the waveshield’s website there are instructions for the conversion with itunes and audacity, but they also point to converting files with soX. soX (sound eXchange) calls itself to be the “swiss army knife of sound processing programs” and, yes, it is! it’s a small cross-platform command-line utility that can perform manipulations and conversion of audio in all thinkable kinds of formats – the list of features is truely impressive.

i only wanted to use it to convert wavefiles in different folders to a waveshield-compatible format without touching the original files while preserving the original folderstructure, so i wrote a little bash-script to do this. you might use the script for just that purpose as is. nevertheless it should be easy to edit the script for doing all kinds of batchprocessing with soX, i.e. editing the volume of your audio data or even apply effects to them. just have a look a soX’ manpage to see what you can do with it.


# converts all .wav-files in the subfolders of where
# this script is executed to
# .wav 22khz, 16bit, little endian, mono
# i.e. for using them with the arduino waveshield:
# all converted files will be in the new subdirectory
# "converted" inside the director this script is
# executed; the original files will not be touched
# you have to install sox prior to running this script
# look into the repos of your distribution first
# Author: boundaryfunctions | 2010-02-23

#enable for loops over items with spaces in their name

if [ ! -d converted/ ]; then
 mkdir converted/
for dir in `ls ./`
  if [ ! $dir == "converted" ]; then
    if [ -d "./$dir" ]; then
     cd $dir
     echo "Converting all .wav-files in \"$PWD\"."
     if [ ! -d ../converted/$dir ]; then
       mkdir ../converted/$dir
     for i in *.wav; do
       echo "Converting File \"./$dir/$i\"..."
       sox --guard -V1 -t wav "$i" --endian little -c 1 -r 22050 "../converted/$dir/$i" polyphase
       if [ $? -eq 0 ]
         echo "Successfully converted File \"./$dir/$i\"."
         echo "File \"./$dir/$i\" could not be converted. Aborting!"
         exit 1
     echo "Succesfully converted all Files in \"./$dir\"."
     echo -e "\n"
     cd ..
echo "Successfully converted all .wav-Files to \"$PWD/converted\"."
exit 0

2 thoughts on “batch-converting audio using soX (for arduino waveshield)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s