makeWebAudio.sh
author Jim Hague <jim.hague@acm.org>
Mon, 02 Sep 2013 10:15:56 +0100
changeset 399 aeef7b1ca0ad
parent 394 ae9c05d2aafd
child 400 f1c4abe4fc75
permissions -rwxr-xr-x
Make 1/4, 1/2 and 3/4 speed audio for tunes. Name them 'veryslow', 'slow' and 'littleslow'.

#!/bin/bash
#
# Make audio required for the website but not for the book.
# They go into web/<book>.
#

if [ $# != 1 ]; then
    echo "Usage: makeWebAudio.sh <book dir name>"
    exit 1
fi

dir=`pwd`

booke=$dir/$1
builddir=$dir/web/$1

mkdir -p $builddir

# Make MP3 and OGG files for the input .abc. In case we're generating
# to a live site (which we won't be), do this to temp files and rename
# into place to make updates as atomic as possible.
makeaudiofiles()
{
    name=`basename $1 .abc`
    tmpname=${name}.tmp

    abc2midi $1 -o $builddir/${tmpname}.mid
    timidity -OwM -o $builddir/${tmpname}.wav $builddir/${tmpname}.mid
    lame -m m -V 9 --quiet $builddir/${tmpname}.wav $builddir/${tmpname}.mp3
    # Timidity can generate OGG directly. But we need to generate WAV
    # for lame, and oggenc produces smaller output. OGG is needed for
    # Firefox's audio tag. FF doesn't support MP3, some others support
    # MP3 but not OGG.
    oggenc -Q -q 0 -o $builddir/${tmpname}.ogg $builddir/${tmpname}.wav

    mv $builddir/${tmpname}.mid $builddir/${name}.mid
    mv $builddir/${tmpname}.mp3 $builddir/${name}.mp3
    mv $builddir/${tmpname}.ogg $builddir/${name}.ogg
    rm $builddir/${tmpname}.wav
}

# Make audio for a new tempo for the abc file $1, giving the output files
# the same name with a prefix $2. The new tempo is the original tempo
# (120 used if not specified), multiplied by $3 and divided by $4.
makeaudiofortempo()
{
    name=`basename $filename .abc`
    newspeedfilename="$2-${name}.abc"

    # Prepare new speed audio files.
    # The tempo is either a plain number, or <notelen>=<number>.
    tempo=`$dir/abcfield.py --field Q $1`
    if [ -z $tempo ]; then
        echo "Warning: $1 has no tempo. Using 120."
        tempo="120"
    fi
    pos=`expr index $tempo '='`
    numtempo=${tempo:pos}
    notelenprefix=${tempo:0:pos}
    # Calculate new tempo.
    newtempo=$(( ( $numtempo * $3 ) / $4 ))
    # Insert new tempo and delete old. Old may not exist,
    # so do this rather than overwrite.
    sed -e "/^Q:/d" -e "/^K:/aQ: ${notelenprefix}${newtempo}" $1 > $builddir/$newspeedfilename
    makeaudiofiles $builddir/$newspeedfilename
    rm $builddir/$newspeedfilename
}

# Generate audio files and slow speed (currently half speed) audio files.
find $booke -name "*.abc" | sort |
    while read filename
    do
        makeaudiofiles $filename

        # Now make 1/4, 1/2 and 3/4 speed audio.
        makeaudiofortempo $filename "veryslow" 1 4
        makeaudiofortempo $filename "slow" 1 2
        makeaudiofortempo $filename "littleslow" 3 4
    done