#!/bin/bash

SFNAME="caltones.wav"

if  ( [ $# == 0 ] ) ; then  # print usage summary
  echo "mkcaltones  syntax: "
  echo "   mkcaltones defaults "
  echo "                or "
echo "mkcaltones  [sr integer] [bits  integer] [chan integer] [dur  integer]   [gain  -integer]"
  echo "With no arguments, a default duration of 15 seconds will be  used"
  echo " for each tone, with an additional 1 second silence between the tones. "
  echo " The default gain is -15 dB below maxamp."
  echo "If you wish to change these defaults, your arguments to the sr, bits, chan,"
   echo "gain and dur arguments MUST be integers."
  echo "Example 1:      mkcaltones  dur 12  gain -3"
  echo " Result: The 3 calibration tones will last 12 seconds each and will be -3 dB below maxamp."
  echo "Example 2:      mkcaltones  sr 96000 bits 24 chan 4 gain -3"
  echo " Result: sampling rate = 96000, word size = 24 bits, 4 channel output with"
  echo " each tone at -3 dB below maxamp."
  echo "For more details see the mkcaltones man page."
echo "    ---------------  "
   exit 1
fi

SR=44100
BITS=16
DUR=15
GAIN=-15
CHAN=2
while (( "$#" )); do
 # if ( [ "$1" = "defaults" ] || [ "$1" = "cd" ] ) ; then  shift; 
 if ( [ "$1" = "defaults" ] ) ; then  shift; 
 elif ( [ "$1" = "sr" ] ) ; then
  shift; SR=$1 ; shift;
 elif ( [ "$1" = "bits" ] ) ; then
  shift; BITS=$1 ; shift;
 elif ( [ "$1" = "dur" ] ) ; then
  shift; DUR=$1 ; shift;
 elif ( [ "$1" = "gain" ] ) ; then
  shift; GAIN=$1 ; shift;
 elif ( [ "$1" = "chan" ]  || [ "$1" = "chans" ] ) ; then
  shift; CHAN=$1 ; shift;
  else
   echo "Unrecognized argument -> $1 <-. Quitting."
   exit 1
 fi
done
# Check for bad arguments
# Output gain check
  if [ "$GAIN" -gt 0 ] ; then
  #  echo "ERROR: Output gain is set to greater than 0 dB ."
    echo "ERROR: Your + $GAIN dB argument exceeds 0 dB (maxamp). Quitting." ; exit 1
  fi
  if [ "$GAIN" -lt -18 ] ; then
    echo "WARNING: Your output gain is set very low: $GAIN dB ."
    echo "If this is not what you want, abort now with  control-c"
    echo "Output gains of -15, or between -15 and -1, are recommended."
    sleep 3
  fi
# Number of channels check:
if [ "$CHAN" -gt "8" ] ; then
        echo "ERROR: Too many output channels: $CHAN"
        echo "Maximum number of output channels is 8. Quitting."
        exit 1
   #if ( [ "$CHAN" != "1" ] && [ "$CHAN" != "2" ] && [ "$CHAN" != "4" ] ) ; then
   elif ( [ "$CHAN" != "1" ] && [ "$CHAN" != "2" ] && [ "$CHAN" != "4" ] && [ "$CHAN" != "5" ] && [ "$CHAN" != "6" ] && [ "$CHAN" != "8" ] ) ; then
      echo "ERROR: Invalid number of output channels -> $CHAN <-. Valid channel arguments"
      echo "are 1,2,4,5,6 and 8. Quitting."
      exit 1
  fi
 # Sampling rate check:
if  [ "$SR" -lt "20000" ] ; then
    echo "ERROR: Your $SR sampling rate is too low. Quitting."
    exit 1
  elif  [ "$SR" -gt "96000" ] ; then
  echo "ERROR: Your $SR sampling rate is too high. 96000 is the highest supported sampling rate. Quitting."
    exit 1
fi
if ( [ "$SR" != "44100" ] && [ "$SR" != "48000" ] && [ "$SR" != "96000" ]  && [ "$SR" != "88200" ] && [ "$SR" != "22050" ] && [ "$SR" != "24000" ] ) ; then
# if ( [ "$SR" != "44100" ] ) ; then
  echo "WARNING: Uncommon sampling rate: $SR "
    echo "If this is not what you want, abort now with  control-c"
fi
# Bit depth check:
   if ( [ "$BITS" != "16" ] && [ "$BITS" != "24" ] && [ "$BITS" != "32" ] ) ; then
        echo "ERROR: Incorrect bit depth : $BITS"
        echo "Only 16, 24 or 32 bit outputs are supported. Quitting."
        exit 1
  fi
# Duration check:
 if [ "$DUR" -gt "30" ] ; then
  echo "ERROR: Your duration for each tone, $DUR seconds is too long."
  echo "Maximum  duration for each tone is 30  (seconds). Quitting."
  exit 1
 fi
 if [ "$DUR" -lt "10" ] ; then
  echo "WARNING: Your duration for each tone, $DUR seconds probably is too short"
  echo "to be useful for calibration tones."
  echo " If this is not what you want abort now with a ctrl-c."
 echo "Recommended calibration tone durations are between 15 and 30 seconds."
 sleep 3 
 fi
# Tell 'em what we're going to do:
  echo " Creating a calibration tone soundfile called >> $SFNAME  <<"
  echo "  in $SFDIR."
  echo " The soundfile contains 3 tones, at 1 kHz, 10 kHz and 100 hz"
  echo " The duration of each tone is >> $DUR << seconds,"
  echo " separated by 1 second of silence. "
  echo " The gain of each tone is >> $GAIN << dB below maxamp. "
  echo "Sampling rate = $SR ; bit depth = $BITS bits, channels = $CHAN"

# Set starting times of the 3 tones, one second between each:
  STARTTIMES=( $DUR + 1 )
  DUTY=`expr $DUR + 300`

TEMPSCO=caltones.s
TEMPORC=caltones.orc
TEMPOUT=/tmp/tmp$$
## Prepare a Csound score file using score 11:
/bin/rm -f $TEMPSCO $TEMPORC
echo "*f1 0 8192 10 1. ; < sine wave " > $TEMPSCO
echo "i1 0 0 3; " >> $TEMPSCO
echo "p3 $STARTTIMES; " >> $TEMPSCO
echo "du  $DUTY ; " >> $TEMPSCO
echo "p4 nu 1000 / 10000/ 100;   < pitch " >> $TEMPSCO
echo "p5 $GAIN ; < amp in dB  0 = maxamp, -18 = 4000 " >> $TEMPSCO
echo "end; " >> $TEMPSCO

### Now prepare a Csound orchestra file:
echo "	sr=$SR " > $TEMPORC
KR=`expr $SR / 10`
echo "	kr=$KR " >> $TEMPORC
# echo "	ksmps=20 " >> $TEMPORC
echo "	nchnls=$CHAN " >> $TEMPORC
echo "	instr 1 " >> $TEMPORC
echo "iamp = ( p5 > 0 ?  p5 : - p5) " >> $TEMPORC
echo "iamp = (90 - iamp) " >> $TEMPORC
echo "iamp = (ampdb(iamp)) " >> $TEMPORC
echo "kamp    expseg 1, .1 , iamp, p3-.2, iamp, .1, 1 " >> $TEMPORC
echo "asine	oscili   kamp,  p4,  1 " >> $TEMPORC
if [ "$CHAN" = "1" ] ; then
 echo "	out  asine " >> $TEMPORC
  elif [ "$CHAN" = "2" ] ; then
     echo "	outs  asine , asine " >> $TEMPORC
  elif [ "$CHAN" = "4" ] ; then
     echo "	outq  asine,asine,asine,asine " >> $TEMPORC
  elif [ "$CHAN" = "5" ] ; then
     echo "	outc  asine,asine,asine,asine,asine " >> $TEMPORC
  elif [ "$CHAN" = "6" ] ; then
     echo "	outc  asine,asine,asine,asine,asine,asine " >> $TEMPORC
  elif [ "$CHAN" = "8" ] ; then
     echo "	outc  asine,asine,asine,asine,asine,asine,asine,asine " >> $TEMPORC
 fi
echo "	endin " >> $TEMPORC

mv  sout oldsout >& /dev/null ; score11 $TEMPSCO  >& /dev/null
mv  sout caltones.sco ; mv  oldsout sout >& /dev/null
# csound -d -W -o $SFDIR/$SFNAME  $TEMPORC caltones.sco >& /dev/null
csound -d -W -o $SFDIR/$SFNAME  $TEMPORC caltones.sco >& $TEMPOUT
echo "Csound output for the 3 tones:"
 #grep -e "M:" -e "TT" $TEMPOUT
 grep -e "M:" -e "TT" $TEMPOUT | sed -e 's/B\ \ /\ \ \ /'

 grep -e "overall amps:" -e "overall samples out of range:" $TEMPOUT
sfinfo $SFDIR/caltones.wav

/bin/rm -f $TEMPORC  $TEMPSCO $TEMPOUT caltones.sco
