#!/usr/bin/perl -w

use Cwd;

$version    = "V1.0";

if ($#ARGV+1 == 0 || $ARGV[0] eq "-h" || $ARGV[0] eq "-H")
{   print 
    "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n".
    "Copyright CSTR                                     $version, 10/11/05, vst\n".
    "                                                                          \n".
    "Usage:  make_esps_f0  wav/*.wav                                           \n".
    "                                                                          \n".
    "Use Entropic's get_f0 with default parameters to buid pitch tracks in     \n".
    "Festival format from RIFF wave files.  Output files are written into      \n".
    "directory \"f0\".                                                         \n".
    "                                                                          \n".
    "To change parameters, copy parameter file Pget_f0 (same location as this  \n".
    "file) to your working directory and edit it.                              \n".
    "                                                                          \n".
    "Festival format:                                                          \n".
    "\$1:  time in s (shift is 2ms, starts with 0.002, not 0)                  \n".
    "\$2:  voicing 0/1                                                         \n".
    "\$3:  F0 in Hz or -1                                                      \n".
    "                                                                          \n".
    "                                                                          \n".
    "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
    exit;
}
# ===========================================================================
# ===========================================================================
$em = "ERROR in make_esps_f0: ";
if(! -d "f0")
{   die "$em directory f0/ does not exist\n";
}
system("which get_f0 >/dev/null") == 0 or die "$em need get_f0 installed\n";
system("which pplain >/dev/null") == 0 or die "$em need pplain installed\n";

$tmp_root = "/tmp/make_esps_f0.$$";
$sd = "$tmp_root.sd";  # ESPS wave format
$f0 = "$tmp_root.f0";  # ESPS F0 format
$F0 = "$tmp_root.F0";  # ASCII version

$ENV{USE_ESPS_COMMON} = "off"; # otherwise get_f0 writes to $HOME/.espscom,
# which not only leads to ugly diagnostics, but also to errors, if multiple
# instances of get_f0 are writing to this file at the same time.

# ===========================================================================
foreach $wav (@ARGV)
{   if(! -e $wav)
    {   die "$em file $wav does not exist\n";
    }
    system("ch_wave $wav -otype esps -F 16000 -o $sd");
    system("get_f0 -S 32 $sd $f0");
    system("pplain $f0 > $F0");

    $out_f0 = $wav;
    $out_f0 =~ s/wav/f0/g;
    open(IFD, $F0)         or die "$em cannot open $F0\n";
    open(OFD, ">".$out_f0) or die "$em cannot open $out_f0";

    $NumFrames = `cat $F0 | wc -l`;
    print OFD "EST_File Track\n".
              "DataType ascii\n".
              "NumFrames $NumFrames".
              "NumChannels 1\n".
              "NumAuxChannels 0\n".
              "EqualSpace 1\n".
              "BreaksPresent true\n".
              "Channel_0 F0\n".
              "file_type 13\n".
              "EST_Header_End\n";
    $t=0;
    while(<IFD>)
    {   @tokens = split;
        $Hz = $tokens[0];
        $t += 0.002;
        if($Hz == 0)
        {   printf OFD "%1.3f\t%d\t%7.3f\n", $t, 0, -1;
        }
        else
        {   printf OFD "%1.3f\t%d\t%7.3f\n", $t, 1, $Hz;
        }
    }
    close IFD;
    close OFD;
    system("rm $sd $f0 $F0");
    print "$out_f0 done\n";
}
# ===========================================================================
