#! /usr/local/bin/perl
#


# New version of adcplay.
#
# Use the display variable to decide where to output the sound to.
# Allow command line volume control.  Allow command line specification
# of headphones or not.  Remain backwards compatible.  Rewritten in perl.

$! = 1;
$usage = "Usage: speechplay [options] <adcfile>\n".
         "       options are:\n".
         "         -x       use external speaker\n".
         "         -v[vol]  specify volume\n".
         "         -        use STDIN as input\n";

($#ARGV >= 0) || die($usage);

$host=`hostname`; chop($host);

$display=$ENV{"DISPLAY"};

# Set the values for the options.  

# Default values

$volume = 50;
$ext = "";
$speech = "/projects/cslu/speech";


# Check environmental variables for options

$ext = "-x" if ($ENV{"PLAY_EXT"});
$volume = $ENV{"PLAY_VOL"} if ($ENV{"PLAY_VOL"});


# Process arguments

foreach $arg (@ARGV) {
  if ($arg =~ /-v(.*)/) {   # volume control
    $volume = $1;
  }
  elsif ($arg =~ /-x/)  {    # use external speaker
    $ext = "-x";
  }
  elsif ($arg eq "-")   {    # use stdin as file
    $stdin = 1;
  }
  elsif ($arg =~ /^-/)  {die($usage);}
  else {
    die($usage) if(defined($adcfile) || $stdin);
    $arg =~ /(.*)/;
    $adcfile = $1;
  }
}

# Set the sound_at location.  Get this from the display variable.

if (($display eq ":0.0") || ($display eq "")) {
  $sound_at = $host;
}
else {
  ($sound_at,$junk)=split(/:/,$display);
}


# See if we are playing on the same machine that the display is at.  If so
# we must be on either a sun4 or decmips machine.

if ($sound_at eq $host) {
  &create_adcfile if($stdin);
  if (&sun4($sound_at))		# SUN4
    {
      system("$speech/new/bin/sun4/autogain $adcfile /usr/tmp/$$gain.adc");
      system("$speech/new/bin/sun4/sparcplay $ext -v $volume /usr/tmp/$$gain.adc");
    }
  elsif (&decmips($sound_at))	# DECMIPS
    {
      $ENV{"AUDIOFILE"} = "$host:0";
      system("$speech/new/bin/decmips/wav2mu $adcfile /usr/tmp/$$.mu");
      system("$speech/bin/decmips/aplay -f -g $volume /usr/tmp/$$.mu");
    }
  elsif (&alpha($sound_at))	# ALPHA
    {
      $ENV{"AUDIOFILE"} = "$host:0";
      system("$speech/new/bin/alpha/speech2mu -f $adcfile -o /usr/tmp/$$.mu");
      system("$speech/unsup/bin/alpha/aplay -f -g $volume /usr/tmp/$$.mu");
    }
  else {die("unknown host $host\n");}
}


# If we are playing on a different machine from the host, use rsh to play
# remotely unless we're on a mac or an xterm

else {				# $sound_at != $host
  if (&macintosh($sound_at))	# MACINTOSH
    {
      &create_adcfile if($stdin);
      system("wav2adc $adcfile /usr/tmp/$$.adc");
      system("rplay -host $sound_at /usr/tmp/$$.adc 2> /dev/null");
    }
  elsif (&xterm($sound_at))	# XTERM
    {
      $arch = `arch`; chop $arch;
      $create_adcfile if($stdin);
      if (($arch eq "decmips") || ($arch eq "sun4")) {
	system("$speech/unsup/bin/$arch/auctl set device 2 gain = 2");
	system("$speech/unsup/bin/$arch/speech2snd $adcfile | $speech/unsup/bin/$arch/auplay -v 100");
      }
      else {die("Can't play from arch $arch\n");}
    }
  else {
    $speechplay = "/projects/cslu/speech/new/bin/share/speechplay";
    $rshcommand = "cat $adcfile | rsh $sound_at $speechplay -v$volume $ext -";
    system($rshcommand);
  }
}

unlink(</usr/tmp/$$*.*>);


sub sun4 {
  local($sound_at) = @_;

  (($sound_at eq "cagney") ||
   ($sound_at eq "zeppo")  ||
   ($sound_at eq "gummo")  ||
   ($sound_at eq "abbott")  || 
   ($sound_at eq "costello")  || 
   ($sound_at eq "seinfeld")  ||        
   ($sound_at eq "gilda")  || 
   ($sound_at eq "chico") ||
   ($sound_at eq "sake") ||
   ($sound_at eq "anago") ||
   ($sound_at eq "vitus"));
}


sub decmips {
  local($sound_at) = @_;

  (($sound_at eq "calvin") ||
   ($sound_at eq "hobbes") ||
   ($sound_at eq "blondie") ||
   ($sound_at eq "dagwood"));
}

sub alpha {
  local($sound_at) = @_;

   (($sound_at eq "bart") ||
    ($sound_at eq "lisa"));
}


sub macintosh {
  local($sound_at) = @_;

  (($sound_at =~ /stanley.*/) ||
   ($sound_at =~ /oliver.*/)  ||
   ($sound_at =~ /jake.*/)    ||
   ($sound_at =~ /elwood.*/)  ||
   ($sound_at =~ /gracie.*/)  ||
   ($sound_at =~ /lily.*/)    ||
   ($sound_at =~ /butch.*/)    ||
   ($sound_at =~ /sundance.*/)    ||
   ($sound_at =~ /charlie.*/));
}


sub xterm {
  local($sound_at) = @_;

  (($sound_at =~ /xebra.*/) ||
   ($sound_at =~ /xpose.*/) ||
   ($sound_at =~ /xpect.*/));
}


sub create_adcfile {
  $adcfile = "/usr/tmp/$$.adcfile";
  system("cat > $adcfile");
}
