#!/usr/bin/perl
#
# Programmer:    Craig Stuart Sapp <craig@ccrma.stanford.edu>
# Creation Date: Sat Jan 18 23:22:35 PST 2003
# Last Modified: Sat Jan 18 23:22:35 PST 2003
# Filename:      smsHarmonics
# Syntax:        perl 5
#
# Description:   Interface for the SMS analysis/synthesis program to
#                extract harmonics from a harmonic sound.  Extra input 
#                parameters can be specified on the command line like this:
#                   smsHarmonics 2 input.wav output.wav DefaultPitch=200
#                which would add the line:
#                   DefaultPitch 200
#                to the analysis file.
#
# Usage:         smsHarmonics number-of-harmonics input.wav outout.wav [options]
#

use strict;

my $harmonics   = int($ARGV[0]);
my $inputfile   = $ARGV[1];
my $outputfile  = $ARGV[2];

if (@ARGV < 3 || $harmonics < 1) {
   print "Usage: $0 harmonics input.wav outout.wav\n";
}

my %parameters;
$parameters{'LowestPitch'}  = 50;
$parameters{'HighestPitch'} = 5000;
$parameters{'SineModel'}    = 1;
$parameters{'nSines'}       = $harmonics;

my $i;
for ($i=3; $i<@ARGV; $i++) {
   if ($ARGV[$i] =~ /(.*)=(.*)/) {
      $parameters{$1} = $2;
   }
}

open (ANALYSISFILE, ">.analysis-$inputfile") || die;
print ANALYSISFILE <<"EOT";
InputSoundFile $inputfile
OutputSmsFile .$inputfile.sms
EOT
my $key;
foreach $key (keys %parameters) {
  print ANALYSISFILE "$key $parameters{$key}\n";
}
close ANALYSISFILE;

open (SYNTHESISFILE, ">.synthesis-$inputfile") || die;
print SYNTHESISFILE <<"EOT";
InputSmsFile       .$inputfile.sms
OutputSoundFile    $outputfile
Type               1
EOT
close SYNTHESISFILE;


`sms analysis .analysis-$inputfile`;
`sms synthesis .synthesis-$inputfile`;

# clean up the temporary files
`rm -f .$inputfile.sms`;
`rm -f .analysis-$inputfile`;
`rm -f .synthesis-$inputfile`;