#!/usr/bin/perl

# --------------------------------------------------------------------
#
# mono2abi
#
# xdowl (and early versions of xdphys) used to produce .mono files. 
# .mono files contained data for two monaural abi curves.  Support
# for .mono files was dropped from xdview when the "mono" curve option
# was removed from xdphys (c. 1998).  As such, today's xdview cannot read
# .mono files.
#
# mono2abi converts a .mono file to two .abi files, to allow the data to 
# be viewed by xdview.
#
# Usage:
#
#    mono2abi <filename.mono>
#
#    This will create two .abi files: filename.L.abi and filename.R.abi 
#    containing the monaural abi curves for the left and right side, 
#    respectively
#
# --------------------------------------------------------------------
sub extract_values ($) {

	my $filename = shift(@_);
	my $start;
	my $stop;
	my $side;
	my $found = 0;
	my $nleft = 0;
	my $nspont = 0;
	my $nright = 0;

	open(FILE, "< $filename");
	while(<FILE>) {
		chomp;
		/mono.Start/ && do {
			@fields = split(/=/);
			$start=@fields[1];
			$found=$found+1;
			next;
		};
		/mono.Stop/ && do {
			@fields = split(/=/);
			$stop=@fields[1];
			$found=$found+1;
			next;
		};
		/mono.Step/ && do {
			@fields = split(/=/);
			$step=@fields[1];
			$found=$found+1;
			next;
		};
		/^depvar=.*L=/ && do { $nleft = $nleft + 1; };
		/^depvar=.*R=/ && do { $nright = $nright + 1; };
		/^depvar=-6666/ && do { $nspont = $nspont + 1; };
	}
	close(FILE);

	($found != 3) && die "couldn't find all the mono fields: $found\n";

	return("$start:$stop:$step",$nleft,$nright,$nspont);
}


sub write_abi_file ($$$$) {
	
	my ($filename,$range,$side,$nrasters)=@_;
	my $abi_filename;
	my $other_side;
	my $print_it;

	$_=$filename;
	s/\.mono$/\.$side.abi/g;
	$abi_filename=$_;

	$_=$side;
	/L/ && do { $other_side = "R" };
	/R/ && do { $other_side = "L" };

	$print_it=1;

	open(OLDFILE, "< $filename");
	open(NEWFILE, "> $abi_filename");
	while($line = <OLDFILE>) {
		$_=$line;
		/^;; dowl mono/ && do { 
			s/^;; dowl mono/;; dowl abi/g;
			$line=$_;
			print NEWFILE $line;
			next;
		};
		/^mono.Stim/ && do { 
			s/^mono.Stim/abi.stim/g;
			$line=$_;
			print NEWFILE $line;
			next;
		};
		/^mono/ && do { next; };
		/^depvar=mono/ && do { next; };
		/^PARAMS/ && do { 
			print NEWFILE $line;
			print NEWFILE "abi.range=$range\n";
			print NEWFILE "abi.itd=0\n";
			print NEWFILE "abi.iid=0\n";
			print NEWFILE "abi.bc=100\n";
			print NEWFILE "abi.mono=$side\n";
			print NEWFILE "depvar=abi (dB)\n";
			next;
		};
		/^Side=/ && do {
			print NEWFILE "Side=$side\n";
			next;
		};
		/^nrasters=/ && do {
			print NEWFILE "nrasters=$nrasters\n";
			next;
		};
		/^depvar=-6666/ && do {
			$print_it=1;
			print NEWFILE $line;
			next;
		};
		/^depvar=.*$side=/ && do {
			$print_it=1;
			# extract the dbspl
			@fields=split(/ /);
			$dbspl=$fields[2];
			print NEWFILE "depvar=$dbspl <$dbspl; 0; 0; 100; $side; 0>\n";
			next;
		};
		/^depvar=.*$other_side=/ && do {
			$print_it=0;
		};

		$print_it && do { print NEWFILE $line; };
	}

	close(NEWFILE);
	close(OLDFILE);

}

sub usage () {

	print " Usage:\n";
	print "\n";
	print "  mono2abi <filename>.mono\n";
	print "\n";
	print "  This will create two .abi files: <filename>.L.abi and <filename>.R.abi \n";
	print "  containing the monaural abi curves for the left and right side, \n";
	print "  respectively\n";

	exit(0);
}

# ====================
# Main
# ====================

@ARGV != 1 && do { usage;};

($range,$nleft,$nright,$nspont) = &extract_values($ARGV[0]);
&write_abi_file($ARGV[0],$range,"L",$nleft+$nspont);
&write_abi_file($ARGV[0],$range,"R",$nright+$nspont);

