#!/bin/sh
#
# Create Compressed/Decompressed files
#
# use: 
# ./CompressAll [<name>] ....
#
# <name> should be the prefix to the <name>SLspeech target directory
# which receives the audio file (the same directory structure
# as the original SLspeech directory).
#
# You can add your own compression/decompression pipelines
# using the existing ones as a mould. However, you can
# also construct your own mirroring the directory structure
# of ../../SLspeech/home/compress/SLspeech
#
# For example:
# ./CompressAll Ogg MP3 Ogg40
#
# Will compress/decompress all files in 
# ../../SLspeech/home/compress/SLspeech
#
# and write them to:
# ../../SLspeech/home/compress/OggSLspeech
# ../../SLspeech/home/compress/MP3SLspeech
# ../../SLspeech/home/compress/Ogg40SLspeech
#    
# Copyright (C) 2002  R.J.J.H. van Son
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
# 
#
currenthomedir='../../../../SLspeech/home/compress'

# Iterate over compression types
# Do not touch (de-)compressed files that are newer than the original files
# But remove (de-)compressed files that are older than the original files
for Compression in ${@:-Ogg MP3 Ogg40 SOM}; do

  # Ogg Vorbis (default)
  if [[ $Compression == 'Ogg' ]]; then
	find $currenthomedir/SLspeech -name '*.aifc' ! -type d -exec perl -e \
	'$in="{}";$out=$in;$out=~s@compress/SLspeech@compress/OggSLspeech@g;$out=~s/\.aifc/_Ogg\.wav/g;\
	 `mkdir -p \$(dirname $out);rm -f $out;oggenc -Q $in -o - \| \
	 ogg123 -q -d wav -f $out -` unless -s "$out" && -M "$out" < -M "$in";' ';'
  fi;
  
  #
  # MP3 (CD quality)
  if [[ $Compression == 'MP3' ]]; then
	find $currenthomedir/SLspeech -name '*.aifc' ! -type d -exec perl -e \
	'$in="{}";$out=$in;$out=~s@compress/SLspeech@compress/MP3SLspeech@g;$out=~s/\.aifc/_MP3\.wav/g;\
	`mkdir -p \$(dirname $out);rm -f $out;notlame -h -m m --preset cd --quiet $in - \| 
	notlame -h --silent --mp3input --decode - $out 2>/dev/null` unless -s "$out" && -M "$out" < -M "$in";' ';'
  fi;
  
  #
  # Ogg Vorbis 40 kbs
  if [[ $Compression == 'Ogg40' ]]; then
	find $currenthomedir/SLspeech -name '*.aifc' ! -type d -exec perl -e \
	'$in="{}";$out=$in;$out=~s@compress/SLspeech@compress/Ogg40SLspeech@g;$out=~s/\.aifc/_Ogg40\.wav/g;\
	 `mkdir -p \$(dirname $out);rm -f $out; oggenc -b 40 -Q $in -o - \| \
	 ogg123 -q -d wav -f $out -` unless -s "$out" && -M "$out" < -M "$in";' ';'
  fi;
  
  #
  # Chain all
  if [[ $Compression == 'SOM' ]]; then
        find $currenthomedir/SmdSLspeech -name '*.wav' ! -type d -exec perl -e \
         '$in="{}";$out=$in;$out=~s@compress/SmdSLspeech@compress/SOMSLspeech@g;$out=~s/_Smd\.wav/_SOM\.wav/ig;\
         `mkdir -p \$(dirname $out);rm -f $out; oggenc -Q $in -o - | ogg123 -q -d wav -f - - | notlame -h -m m --preset cd --quiet - - | notlame -h --silent --mp3input --decode - $out 2>/dev/null` unless -s "$out" && -M "$out" < -M "$in";' ';'
  fi;

done;
