• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/sh
2progname=mugeco version=0.1
3programr='Alexander Leidinger'
4progdate='7 Dec 2000'
5progdesc='MUltiGEnerationCOding'
6# NEEDS: getopt, lame
7# Please have a look at the DEFAULTS section.
8
9# $Id$
10
11usage() {
12cat << EOF
13** $progname v$version, $progdate **
14by $programr
15$progdesc
16usage: $progname [ <flags> ] -g <num> <file>
17    -v	       use builtin VBR options
18    -g <num>   number of generations
19    -h help
20
21 used
22  - env vars:
23    * LAME   : alternative encoder binary
24    * LAMEOPT: alternative encoder options
25  - VBR opts: $enc_vbr_opts
26  - CBR opts: $enc_cbr_opts
27EOF
28}
29
30# DEFAULTS
31
32# if you can, use getopt(1) (c)1997 by Frodo Looijaard <frodol@dds.nl>
33# it's in most modern unixen, or look at http://huizen.dds.nl/~frodol/
34: ${GETOPT=getopt}	# helper program
35# mktemp (optional) is also in most modern unixen (originally from OpenBSD)
36: ${MKTEMP=mktemp}	# helper program
37: ${TMPDIR:=/tmp}	# set default temp directory
38: ${LAME:=lame}		# path to LAME
39
40enc_cbr_opts="-b192 -h --lowpass 18 --lowpass-width 0"
41enc_vbr_opts="--vbr-mtrh --nspsytune -v -h -d -Y -X3"
42enc_opts=${LAMEOPT:-$enc_cbr_opts}
43num=			# default number of generations
44
45# DEFINE FUNCTIONS
46
47e() { echo "$progname: $*"; }
48die() {	    # usage:  die [ <exitcode> [ <errormessage> ] ]
49    trap '' 1 2 3 13 15
50    exitcode=0
51    [ $# -gt 0 ] && { exitcode=$1; shift; }
52    [ $# -gt 0 ] && e "Error: $*" >&2
53    exit $exitcode
54}
55
56# tfile()
57# this function creates temporary files.  'tfile temp' will make a tempfile
58# and put the path to it in the variable $temp (defaults to variable $tf)
59trap 'for f in $ztfiles; do rm -f "$f"; done' 0
60trap 'trap "" 1 2 3 13 15; exit 10' 1 2 3 13 15
61unset ztfiles
62tfile() {	# usage: tfile <variable_name>
63    ztf=`$MKTEMP -q $TMPDIR/$progname.XXXXXX 2>/dev/null`	# try mktemp
64    if [ $? -gt 0 -o -z "$ztf" ]; then	# if mktemp fails, do it unsafely
65	ztf=$TMPDIR/$LOGNAME.$progname.$$
66	[ -e "$ztf" ] && ztf= || { touch $ztf && chmod 600 $ztf; }
67    fi
68    [ "$ztf" -a -f "$ztf" ] || { echo Could not make tempfile; exit 8; }
69    ztfiles="$ztfiles $ztf"
70    eval ${1:-tf}='$ztf'
71}
72
73# PARSE COMMAND LINE
74
75options="g:vh"	# option string for getopt(1)
76help=; [ "$1" = -h -o "$1" = -help -o "$1" = --help ] && help=yes
77[ "$help" ] && { usage; die; }
78$GETOPT -T >/dev/null 2>&1
79[ $? -eq 4 ] && GETOPT="$GETOPT -n $progname -s sh" #frodol's getopt?
80eval set -- `$GETOPT "$options" "$@"`
81[ $# -lt 1 ] && { die 9 getopt failed; }
82while [ $# -gt 0 ]; do
83    case "$1" in
84    -g) num=$2; shift ;;
85    -v) enc_opts=$enc_cbr_opts ;;
86    -h)	help=y ;;
87    --)	shift; break ;;
88    *)	usage; die 9 "invalid command line syntax!" ;;
89    esac
90    shift
91done
92[ "$help" ] && { usage; die; }
93[ $# -eq 0 ] && { usage; die 9 no arguments; } #change or remove if desired
94# sanity checking
95[ "$num" ] && echo "$num"|grep -q '^[0-9]*$' && [ $num -ge 1 ] \
96    || die 1 please use the -g flag with a valid number
97
98# MAIN PROGRAM
99
100# what version of lame are we using?
101lame_vers=`$LAME 2>&1 | awk 'NR==1{print $3}'`
102
103# check filename
104[ -f "$1" ] || die 2 "'$1' isn't a file"
105echo "$1"|grep -qi '\.wav$' || die 2 "'$1' isn't a .wav"
106
107# make tempfiles
108base=`echo "$1"|sed 's/\.[^.]*$//'`
109dest=${base}_generation_$num.wav
110[ -e "$dest" ] && die 2 "'$dest' already exists"
111touch "$dest" || die 2 "couldn't create '$dest'"
112TMPDIR=. tfile tmpwav
113TMPDIR=. tfile tmpmp3
114cp -f "$1" "$tmpwav"
115
116# do the loop
117start=`date`
118i=1
119while [ $i -le $num ]; do
120    e "Working on file '$1', generation number $i..."
121
122    $LAME $enc_opts --tc "lame $lame_vers; Generation: $i" \
123	"$tmpwav" "$tmpmp3" || die 3 encoding failed
124    $LAME --decode --mp3input "$tmpmp3" "$tmpwav" || die 3 decoding failed
125
126    i=`expr $i + 1`
127done
128end=`date`
129
130# save the result
131ln -f "$tmpwav" "$dest"
132
133echo
134e "Start: $start"
135e "Stop : $end"
136
137die
138