Simple Audio Playback Script

Posted by JD 10/31/2009 at 09:00

Below is a script that will playback a group of audio files in order, grouped by day. Suppose you have files named like this


File-1×01.mp3
.
File-1×12.mp3
File-2×01.mp3
.
File-2×10.mp3
File-3×01.mp3
.
File-3×10.mp3
.
File-7×10.mp3
File-8×01.mp3
.
File-8×10.mp3

and want to play group Yx1-6 followed by group Yx6-12 daily. If you just wanted to do this for 1 set of files, it would be easier to just use `at` to play them. But you might have 10-50 files like this and only want to worry about setting up playback once a month or so.

Here’s the bonehead shell script that I’m using to accomplish this.


#!/bin/sh

  1. Program to playback audio tapes in order
  2. based on day of the month – best to start on 1st.
    #
  3. It isn’t pretty, but it works assuming you want to cover
  4. half a lesson each day. The filenames look like this:
  5. File-8×10.mp3

MP=/usr/bin/mplayer
DIR=/Data/Audio/Session1
FILE_ROOT=File
DATE=`date “+%d”` # Returns the day of the month
ODD=`expr $DATE % 2`
START_GRP=`expr 1 + $DATE / 2` # pick a start date
ODD_START_NO=“01 02 03 04 05 06”
EVEN_START_NO=“06 07 08 09 10 11 12”
FILE_EXT=mp3

  1. Changing the 0 to a 1 will toggle which group of files to begin
    if [ $ODD = “0” ] ; then
    START_NO=$ODD_START_NO
    else
    START_NO=$EVEN_START_NO
    fi

for count in $START_NO; do
afile=“$DIR/$FILE_ROOT-${START_GRP}x$count.$FILE_EXT”
if [ -f “$afile” ] ; then
$MP “$afile”
else
echo " File missing: $afile"
fi
done
exit;

So, it isn’t very pretty and it is dependent on starting the script on the first of the month. Since today happens to be Oct 31 and I just finished the first group, I tweaked the EVEN/ODD and date modulus to jump 1 day ahead tomorrow – Nov 1. It will fail when a month roles over to the next month.

A fix to that problem would be to convert the date into a Julian day of the year, DOTY, and subtract off the current DOTY from the starting date. Check out date "+%j" for more on Julian dates. Of course, then it will break at the new year, so perhaps getting the number of seconds since epoch and performing calculations based on that would be even better? Even that method will break in 2038. At some point, the complexity outweighs the difficulty to implement.

Lastly, we need to setup crontab to run the script, playing the file.


1 6 * * * /home/jp/bin/daily_audio.sh

Enjoy.

Trackbacks

Use the following link to trackback from your own site:
https://blog.jdpfu.com/trackbacks?article_id=369