#! /usr/bin/perl -w
#
# dv2wav.pl -- extract wav files from dv tapes. 
#
# (C) 2006,2007 jw@suse.de Novell Inc.
# Distribute under GPLv2
#
# 2006-11-17, jw, 	initial draft.
# 2006-11-23, jw, 	-r option fixed.
# 2007-01-25, jw, 	-c , -n, and -o added.
# 2007-01-29, jw, 	-g added.
# 2007-02-16, jw, 	-d fixed. nonexistant destdir crashes smil2wav.
# 
##########################################
# $ cd ~/src/video
# $ tar xvf smilutils-0.3.0.tar.gz
# $ cd smilutils-0.3.0
# $ yast -i libxml2-devel SDL-devel imlib2 gdk-pixbuf-devel libdv libdv-devel imlib-config imlib-devel
# $ env CXXFLAGS=-fpermissive ./configure
# $ make
# $ make install
# -> smil2wav
#
# yast -i normalize
# -> normalize

use Data::Dumper;
my $version	= '0.6';
my $destdir	= '.';
my $smil2wav 	= 'smil2wav';
my $normalize 	= 'normalize';
my $do_normalize = 1;
my $verbose	= 1;
my $noop	= 0;
my $concat      = 0;
my $dbfs        = -12;
my $gain        = '+6'; 
my $concat_glue = ',';
my $outfilename;

push @ARGV, "-h" unless scalar @ARGV;

while (defined(my $arg = shift))
  {
    $verbose++		if $arg eq '-v';	
    $verbose = 0	if $arg eq '-q';	
    $do_normalize = 0	if $arg eq '-r';	
    $concat = 1         if $arg eq '-c';
    $outfilename = shift if $arg eq '-o';
    $destdir = shift	if $arg eq '-d';
    $gain = shift	if $arg eq '-g';
    $noop++		if $arg eq '-n';
    last if $arg eq '--';
    unless ($arg =~ m{^-[rdqvncog]})
      {
        unless ($arg =~ m{^-})
	  {
	    unshift @ARGV, $arg;
	    last;
	  }
        print STDERR qq{
dv2wav $version
A simple wav extractor for dv files.

$0 [options] tape.dv ...

Valid options are:

  -d destdir	Write .wav files into destdir. Default: '$destdir'
  -r            Raw mode. Do not run $normalize.
  -g gain       Apply gain (max +12) when running $normalize. Default: '$gain'

  -c            Write one concatenated wav file from all listed dv files.
                The filename is a '$concat_glue'-separated list of the input file names
		unless -o is used to specify the name.
                Default: convert file by file.
  -o outfile    Specify the output filename. Implies -c. 
                destdir applies unless outfile starts with a '/'.

  -n            Do nothing, just print out commands.
  -v            Be more verbose. 
  -q            Be quiet

};
        exit 0;
      }
  }

$normalize .= ' -q' unless $verbose;
$normalize .= ' -v' if $verbose > 1;
$normalize .= sprintf " -a %ddbFS", $dbfs+$gain if $gain;

my $count = scalar @ARGV;

my @basenames = map { my $a = $_; $a =~ s{\.[^\./]*$}{}; $a =~ s{^.*/}{}; $a } @ARGV;
$concat = 1 if defined $outfilename;

die "destdir '$destdir' does not exist\n" unless -d $destdir;

for my $dv (@ARGV)
  {
    $count--;
    my $name = $basenames[0];
    my $outname = "$destdir/$name.wav";
    if ($concat)
      {
	$outname = "$destdir/" . join(',', @basenames) . ".wav";
	if (defined $outfilename)
	  {
	    $outname = $outfilename;
	    $outname .= ".wav" unless $outname =~ m{\.\w+$};
	    $outname = "$destdir/" . $outname unless $outname =~ m{^/};
	  }
	$dv = join "' '", @ARGV;
      }
    runcmd("$smil2wav -a '$outname' '$dv'");
    runcmd("$normalize '$outname'") if $do_normalize;	# normalize works inplace. nice.
    last if $concat;

    print "$count files to go...\n" if $count and $verbose;
  }
exit 0;

###############################

sub runcmd
{
  my ($cmd) = @_;
  print "$cmd\n" if $verbose or $noop;
  return if $noop;
  system $cmd and die "$cmd: failed: $@ $!";
}

