#!/bin/sh
#
# Rami - commandline encoded audio recorder
# Copyright (C) 2002 Denis Rojo aka jaromil <jaromil@dyne.org>
#
# This source code is free software; you can redistribute it and/or
# modify it under the terms of the GNU Public License as published 
# by the Free Software Foundation; either version 2 of the License,
# or (at your option) any later version.
#
# This source code 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.
# Please refer to the GNU Public License for more details.
#
# You should have received a copy of the GNU Public License along with
# this source code; if not, write to:
# Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# "$Id: rami,v 1.1.1.1 2003/12/08 12:20:33 jaromil Exp $"

echo "[*] Rami 0.3 - simple sound record commandline interface" 
echo " .  Copyleft 2002 jaromil aka Rami <jaromil@dyne.org>"

# check for sox
SOX="`which sox 2> /dev/null`"
if [ -z $SOX ]; then
    echo "[!] sox not found! this program needs sox to work"
    exit 0
fi

AVAILABLE_FORMATS="[wav"
DEFAULT_FORMAT="wav"

#check for mp3
LAME="`which lame 2> /dev/null`"
if [ -z $LAME ]; then
    echo "[W] lame not found! install lame to encode in mp3 format"
else
    AVAILABLE_FORMATS="$AVAILABLE_FORMATS|mp3"
    DEFAULT_FORMAT="mp3"
fi

# check for ogg
OGGENC="`which oggenc 2> /dev/null`"
if [ -z $OGGENC ]; then
    echo "[W] oggenc not found! install vorbis-tools to encode in ogg format"
else
    AVAILABLE_FORMATS="$AVAILABLE_FORMATS|ogg"
    DEFAULT_FORMAT="ogg"
fi

# check for speex
SPXENC="`which speexenc 2> /dev/null`"
if [ -z $SPXENC ]; then
    echo "[W] speexenc not found! install speex to encode in spx format"
else
    AVAILABLE_FORMATS="$AVAILABLE_FORMATS|spx"
fi

AVAILABLE_FORMATS="$AVAILABLE_FORMATS]"

# setup defaults

FORMAT=$DEFAULT_FORMAT
RATE=22050
QUALITY=3
CHANNELS=2
VUMETER="`which vumeter 2> /dev/null`"
if [ $? == 1 ]; then VUMETER=""; fi


OPTS=`getopt -o hvQr:f:q:c:o:e: --long help,version,quiet,rate:,format:,quality:,channels:,output:,effect: \
      -n 'rami' -- "$@"`

eval set -- "$OPTS"

while true; do
    case "$1" in
	-h)
	    echo " .  rami [-hQrfqoe]"
	    echo " .  -v, --version  print out application info"
	    echo " .  -h, --help     print this small usage guide"
            if [ ! -z $VUMETER ]; then
                echo " .  -Q, --quiet    be quiet and don't show the vumeter"
            fi
	    echo " .  -r, --rate     samplerate in Hz - default: auto"
	    echo " .  -f, --format   encoding format $AVAILABLE_FORMATS - default: $DEFAULT_FORMAT"
	    echo " .  -q, --quality  encoding quality [1min - 9max] - default: 3"
	    echo " .  -c, --channels number of audio channels - default: 2"
	    echo " .  -o, --output   output to filename (without .ext) - default: date"
	    echo " .  -e, --effect   apply realtime sox effect (see man sox)"
	    exit 2
	    ;;
	-v)
	    echo " ."
	    echo "[*] what about this software?"
	    echo " ."
	    echo " .  well, you know the version: it is written up there and is showed every"
	    echo " .  time you run this program, so if you activated this option you want to"
	    echo " .  to know something more about it."
	    echo " ."
	    echo " .  Rami is a simple shell script, it is very much what UNIX style is about"
	    echo " .  and it simplifies you the task to record encoded audio on the fly, without"
	    echo " .  wasting your CPU resources with bloated visual interfaces."
	    echo " ."
	    echo " .  I started coding it because i needed it: i had to save my harddisk space"
	    echo " .  while recording some interviews around Palestine occupied territories"
	    echo " .  and i had only a 300mhz laptop with me which could'nt handle smoothly an"
	    echo " .  X interface. Therefore i started hacking around my own shell script to"
	    echo " .  record encoded audio while applying on it a sox highpass effect to cut"
	    echo " .  off the hiss of the laptop harddisk."
	    echo " .  Once i reached home safely enough i started cleaning up the script and"
	    echo " .  this is what comes out."
	    echo " ."
	    echo " .  Rami is the palestinian name that was given to me by Hadil, on the beach"
	    echo " .  of Sudania, in Gaza."
	    echo " ."
	    echo " .  I love things with a story, so that's all folks; i hope you enjoy."
	    echo
	    exit 2
	    ;;
        -Q)
            VUMETER=""
            shift 1
            ;;
	-r)
	    RATE=$2
	    shift 2
	    ;;
	-f)
	    FORMAT=$2
	    shift 2
	    ;;
	-q)
	    QUALITY=$2
	    shift 2
	    ;;
	-c) CHANNELS=$2
	    shift 2
	    ;;
	-o)
	    OUTPUT=$2
	    shift 2
	    ;;
	-e)
	    EFFECT=$2
	    shift 2
	    ;;
      --) shift; break ;;
      *) echo "error in given options"; exit 1 ;;
   esac
done

#BANDPASS="bandpass 300 1800"

SUCCESS=true

if [ -z $VUMETER ]; then VUMETER="cat"; fi
if [ -z $OUTPUT ]; then FILE="`date +%d%b%Y-%H:%M:%S`.$FORMAT";
else FILE="$OUTPUT.$FORMAT"; fi

echo " .  saving to $FILE"

case "$FORMAT" in
    
    "ogg")
	echo " .  OGG encoding at VBR quality $QUALITY [$FILE]"
	sox -t ossdsp -w -r$RATE -c$CHANNELS /dev/dsp \
	    -t raw -w -r$RATE -c$CHANNELS - $EFFECT | \
	    $VUMETER \
	    | oggenc -Q -q$QUALITY -r -R "$RATE" -C $CHANNELS - -o $FILE
	;;
    
    "mp3")
	LAMERATE=`echo "scale=1;$RATE/1000"|bc`
	if [ $CHANNELS==1 ]; then $LAMECHAN='m'; else $LAMECHAN='j'; fi
	echo " .  MP3 encoding at VRB quality $QUALITY $LAMERATEKhz [$FILE]"
	sox -t ossdsp -w -r"$RATE" -c$CHANNELS /dev/dsp \
	    -t raw -w -r"$RATE" -c$CHANNELS - $EFFECT | \
	    $VUMETER \
	    | lame --quiet -r -s$LAMERATE -x -m $LAMECHAN -V$QUALITY -h - - >$FILE
	;;
    
    "wav")
	echo " .  WAV encoding at 16bit $RATEHz mono [$FILE]"
	sox -t ossdsp -w -r$RATE -c$CHANNELS /dev/dsp $FILE $EFFECT
	;;

    "spx")
	echo " .  SPX encoding at 16bit $16Hz mono [$FILE]"
	sox -t ossdsp -w -r16000 -c$CHANNELS /dev/dsp \
	    -t raw -w -r16000 -c$CHANNELS - $EFFECT | \
	    $VUMETER \
	    | speexenc -w --le --quality $QUALITY - - >$FILE
	;;

    *)
	echo "[!] format $FORMAT is not supported"
	SUCCESS=false
	;;
esac

#   "flac")
#      echo " .  FLAC encoding from 16bit 22khz mono [$FILE]"
#      sox -V -t ossdsp -w -r$RATE -c$CHANNELS /dev/dsp \
#             -t raw -w -r22050 -c$CHANNELS - $EFFECT \
#             | flac -fl -fc 1 -fp 16 -fs 22050 -fr -o $FILE
#   ;;

echo "--"

if [ "$SUCCESS"=="true" ]; then
   echo " .  `ls -sh $FILE`"
   echo "[*] record ended succesfully"
   exit 1
else
   echo "[!] record aborted"
   exit 0
fi
