makeWebAudio.sh
author Jim Hague <jim.hague@acm.org>
Sun, 01 Sep 2013 23:13:06 +0100
changeset 394 ae9c05d2aafd
parent 393 c5d404bf68a3
child 399 aeef7b1ca0ad
permissions -rwxr-xr-x
Make slow speed web audio files.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
43
d92717f8130c First go at producing a very simple website.
Jim Hague <jim.hague@laicatc.com>
parents:
diff changeset
     1
#!/bin/bash
d92717f8130c First go at producing a very simple website.
Jim Hague <jim.hague@laicatc.com>
parents:
diff changeset
     2
#
223
0ef955669a9a Make transposed tunes use the original audio.
Jim Hague <jim.hague@acm.org>
parents: 197
diff changeset
     3
# Make audio required for the website but not for the book.
43
d92717f8130c First go at producing a very simple website.
Jim Hague <jim.hague@laicatc.com>
parents:
diff changeset
     4
# They go into web/<book>.
d92717f8130c First go at producing a very simple website.
Jim Hague <jim.hague@laicatc.com>
parents:
diff changeset
     5
#
d92717f8130c First go at producing a very simple website.
Jim Hague <jim.hague@laicatc.com>
parents:
diff changeset
     6
d92717f8130c First go at producing a very simple website.
Jim Hague <jim.hague@laicatc.com>
parents:
diff changeset
     7
if [ $# != 1 ]; then
223
0ef955669a9a Make transposed tunes use the original audio.
Jim Hague <jim.hague@acm.org>
parents: 197
diff changeset
     8
    echo "Usage: makeWebAudio.sh <book dir name>"
43
d92717f8130c First go at producing a very simple website.
Jim Hague <jim.hague@laicatc.com>
parents:
diff changeset
     9
    exit 1
d92717f8130c First go at producing a very simple website.
Jim Hague <jim.hague@laicatc.com>
parents:
diff changeset
    10
fi
d92717f8130c First go at producing a very simple website.
Jim Hague <jim.hague@laicatc.com>
parents:
diff changeset
    11
d92717f8130c First go at producing a very simple website.
Jim Hague <jim.hague@laicatc.com>
parents:
diff changeset
    12
dir=`pwd`
d92717f8130c First go at producing a very simple website.
Jim Hague <jim.hague@laicatc.com>
parents:
diff changeset
    13
d92717f8130c First go at producing a very simple website.
Jim Hague <jim.hague@laicatc.com>
parents:
diff changeset
    14
booke=$dir/$1
d92717f8130c First go at producing a very simple website.
Jim Hague <jim.hague@laicatc.com>
parents:
diff changeset
    15
builddir=$dir/web/$1
d92717f8130c First go at producing a very simple website.
Jim Hague <jim.hague@laicatc.com>
parents:
diff changeset
    16
d92717f8130c First go at producing a very simple website.
Jim Hague <jim.hague@laicatc.com>
parents:
diff changeset
    17
mkdir -p $builddir
d92717f8130c First go at producing a very simple website.
Jim Hague <jim.hague@laicatc.com>
parents:
diff changeset
    18
394
ae9c05d2aafd Make slow speed web audio files.
Jim Hague <jim.hague@acm.org>
parents: 393
diff changeset
    19
# Make MP3 and OGG files for the input .abc. In case we're generating
ae9c05d2aafd Make slow speed web audio files.
Jim Hague <jim.hague@acm.org>
parents: 393
diff changeset
    20
# to a live site (which we won't be), do this to temp files and rename
ae9c05d2aafd Make slow speed web audio files.
Jim Hague <jim.hague@acm.org>
parents: 393
diff changeset
    21
# into place to make updates as atomic as possible.
ae9c05d2aafd Make slow speed web audio files.
Jim Hague <jim.hague@acm.org>
parents: 393
diff changeset
    22
makeaudiofiles()
ae9c05d2aafd Make slow speed web audio files.
Jim Hague <jim.hague@acm.org>
parents: 393
diff changeset
    23
{
ae9c05d2aafd Make slow speed web audio files.
Jim Hague <jim.hague@acm.org>
parents: 393
diff changeset
    24
    name=`basename $1 .abc`
ae9c05d2aafd Make slow speed web audio files.
Jim Hague <jim.hague@acm.org>
parents: 393
diff changeset
    25
    tmpname=${name}.tmp
ae9c05d2aafd Make slow speed web audio files.
Jim Hague <jim.hague@acm.org>
parents: 393
diff changeset
    26
ae9c05d2aafd Make slow speed web audio files.
Jim Hague <jim.hague@acm.org>
parents: 393
diff changeset
    27
    abc2midi $1 -o $builddir/${tmpname}.mid
ae9c05d2aafd Make slow speed web audio files.
Jim Hague <jim.hague@acm.org>
parents: 393
diff changeset
    28
    timidity -OwM -o $builddir/${tmpname}.wav $builddir/${tmpname}.mid
ae9c05d2aafd Make slow speed web audio files.
Jim Hague <jim.hague@acm.org>
parents: 393
diff changeset
    29
    lame -m m -V 9 --quiet $builddir/${tmpname}.wav $builddir/${tmpname}.mp3
ae9c05d2aafd Make slow speed web audio files.
Jim Hague <jim.hague@acm.org>
parents: 393
diff changeset
    30
    # Timidity can generate OGG directly. But we need to generate WAV
ae9c05d2aafd Make slow speed web audio files.
Jim Hague <jim.hague@acm.org>
parents: 393
diff changeset
    31
    # for lame, and oggenc produces smaller output. OGG is needed for
ae9c05d2aafd Make slow speed web audio files.
Jim Hague <jim.hague@acm.org>
parents: 393
diff changeset
    32
    # Firefox's audio tag. FF doesn't support MP3, some others support
ae9c05d2aafd Make slow speed web audio files.
Jim Hague <jim.hague@acm.org>
parents: 393
diff changeset
    33
    # MP3 but not OGG.
ae9c05d2aafd Make slow speed web audio files.
Jim Hague <jim.hague@acm.org>
parents: 393
diff changeset
    34
    oggenc -Q -q 0 -o $builddir/${tmpname}.ogg $builddir/${tmpname}.wav
ae9c05d2aafd Make slow speed web audio files.
Jim Hague <jim.hague@acm.org>
parents: 393
diff changeset
    35
ae9c05d2aafd Make slow speed web audio files.
Jim Hague <jim.hague@acm.org>
parents: 393
diff changeset
    36
    mv $builddir/${tmpname}.mid $builddir/${name}.mid
ae9c05d2aafd Make slow speed web audio files.
Jim Hague <jim.hague@acm.org>
parents: 393
diff changeset
    37
    mv $builddir/${tmpname}.mp3 $builddir/${name}.mp3
ae9c05d2aafd Make slow speed web audio files.
Jim Hague <jim.hague@acm.org>
parents: 393
diff changeset
    38
    mv $builddir/${tmpname}.ogg $builddir/${name}.ogg
ae9c05d2aafd Make slow speed web audio files.
Jim Hague <jim.hague@acm.org>
parents: 393
diff changeset
    39
    rm $builddir/${tmpname}.wav
ae9c05d2aafd Make slow speed web audio files.
Jim Hague <jim.hague@acm.org>
parents: 393
diff changeset
    40
}
ae9c05d2aafd Make slow speed web audio files.
Jim Hague <jim.hague@acm.org>
parents: 393
diff changeset
    41
ae9c05d2aafd Make slow speed web audio files.
Jim Hague <jim.hague@acm.org>
parents: 393
diff changeset
    42
# Generate audio files and slow speed (currently half speed) audio files.
43
d92717f8130c First go at producing a very simple website.
Jim Hague <jim.hague@laicatc.com>
parents:
diff changeset
    43
find $booke -name "*.abc" | sort |
d92717f8130c First go at producing a very simple website.
Jim Hague <jim.hague@laicatc.com>
parents:
diff changeset
    44
    while read filename
d92717f8130c First go at producing a very simple website.
Jim Hague <jim.hague@laicatc.com>
parents:
diff changeset
    45
    do
394
ae9c05d2aafd Make slow speed web audio files.
Jim Hague <jim.hague@acm.org>
parents: 393
diff changeset
    46
        makeaudiofiles $filename
ae9c05d2aafd Make slow speed web audio files.
Jim Hague <jim.hague@acm.org>
parents: 393
diff changeset
    47
43
d92717f8130c First go at producing a very simple website.
Jim Hague <jim.hague@laicatc.com>
parents:
diff changeset
    48
        name=`basename $filename .abc`
394
ae9c05d2aafd Make slow speed web audio files.
Jim Hague <jim.hague@acm.org>
parents: 393
diff changeset
    49
        slowspeedfilename="slowspeed-${name}.abc"
43
d92717f8130c First go at producing a very simple website.
Jim Hague <jim.hague@laicatc.com>
parents:
diff changeset
    50
394
ae9c05d2aafd Make slow speed web audio files.
Jim Hague <jim.hague@acm.org>
parents: 393
diff changeset
    51
        # Prepare slow speed audio files.
ae9c05d2aafd Make slow speed web audio files.
Jim Hague <jim.hague@acm.org>
parents: 393
diff changeset
    52
        # The tempo is either a plain number, or <notelen>=<number>.
ae9c05d2aafd Make slow speed web audio files.
Jim Hague <jim.hague@acm.org>
parents: 393
diff changeset
    53
        tempo=`$dir/abcfield.py --field Q $filename`
ae9c05d2aafd Make slow speed web audio files.
Jim Hague <jim.hague@acm.org>
parents: 393
diff changeset
    54
        if [ -z $tempo ]; then
ae9c05d2aafd Make slow speed web audio files.
Jim Hague <jim.hague@acm.org>
parents: 393
diff changeset
    55
            echo "Warning: $filename has no tempo. Using 120."
ae9c05d2aafd Make slow speed web audio files.
Jim Hague <jim.hague@acm.org>
parents: 393
diff changeset
    56
            tempo="120"
ae9c05d2aafd Make slow speed web audio files.
Jim Hague <jim.hague@acm.org>
parents: 393
diff changeset
    57
        fi
ae9c05d2aafd Make slow speed web audio files.
Jim Hague <jim.hague@acm.org>
parents: 393
diff changeset
    58
        pos=`expr index $tempo '='`
ae9c05d2aafd Make slow speed web audio files.
Jim Hague <jim.hague@acm.org>
parents: 393
diff changeset
    59
        numtempo=${tempo:pos}
ae9c05d2aafd Make slow speed web audio files.
Jim Hague <jim.hague@acm.org>
parents: 393
diff changeset
    60
        notelenprefix=${tempo:0:pos}
ae9c05d2aafd Make slow speed web audio files.
Jim Hague <jim.hague@acm.org>
parents: 393
diff changeset
    61
        # Make new tempo half original speed.
ae9c05d2aafd Make slow speed web audio files.
Jim Hague <jim.hague@acm.org>
parents: 393
diff changeset
    62
        newtempo=$(( $numtempo / 2 ))
ae9c05d2aafd Make slow speed web audio files.
Jim Hague <jim.hague@acm.org>
parents: 393
diff changeset
    63
        # Insert new tempo and delete old. Old may not exist,
ae9c05d2aafd Make slow speed web audio files.
Jim Hague <jim.hague@acm.org>
parents: 393
diff changeset
    64
        # so do this rather than overwrite.
ae9c05d2aafd Make slow speed web audio files.
Jim Hague <jim.hague@acm.org>
parents: 393
diff changeset
    65
        sed -e "/^Q:/d" -e "/^K:/aQ: ${notelenprefix}${newtempo}" $filename > $builddir/$slowspeedfilename
ae9c05d2aafd Make slow speed web audio files.
Jim Hague <jim.hague@acm.org>
parents: 393
diff changeset
    66
        makeaudiofiles $builddir/$slowspeedfilename
43
d92717f8130c First go at producing a very simple website.
Jim Hague <jim.hague@laicatc.com>
parents:
diff changeset
    67
    done