#!/usr/bin/perl

# GiantDisc audio transcoder
#
# Autor: Rolf Brugger
# Date:  Aug 2003
#



use IO::Socket;
use Getopt::Std;
use lib '/home/music/bin';
use gdgentools;
use DBI;


if (   !getopts ('sdvf:b:') # ":" means mandatory parameter
    || !(defined $opt_f)
    || !(defined $opt_b)
    ){ 
  print <<EOF;

Usage: 
  gdtranscode.pl [options] audiofiles...

GiantDisc audio file transcoder. Translates between the audio formats 
wav, mp3, ogg and flac  (only partly implemented right now)

The result is written to the same file name (if the source and target 
formats are the same), or to a file with the same name but changed 
extension (if the source and target formats differ).

Mandatory options:
  -f format      target audio format. One of mp3, ogg or flac
  -b bitrate     target bitrate. Must be a valid bitrate for 
                 the target audio format in kb/sec (64, 80, ...)

optional:
  -d             delete source file when it was transcoded
  -s             write status to file /home/music/outbox/exportstatus.txt
  -v             verbose
  
Example:
  gdtranscode.pl -f ogg -b 64 -v song.mp3
  
EOF
  exit 0;
}



### init stuff


if ($opt_v) {print "transcode input files into format $opt_f at $opt_b b/sec...\n\n";}

my ($src, $dst);
my ($src_pipe, $dst_pipe);

my $nbfiles = scalar @ARGV;
my $cnt = 0;

### loop trough audio files
while ($src = shift @ARGV){
  $cnt++;
  print "transcoding file $cnt/$nbfiles ($src)\n";
  if (defined $opt_s){
    system "echo \"transcoding file $cnt/$nbfiles ($src)\" > /home/music/outbox/exportstatus.txt";
  }
  $src_pipe = "";
  $dst_pipe = "";
  
  ### get source file format -- pipe source
  if ($src =~ m/(.+)\.(\w+)/){
    my $src_basename  = $1;
    my $src_extension = $2;

    if ($src_extension eq "mp3"){
      $src_pipe = "mpg123 --stdout --quiet \"$src\"";
    }

    elsif ($src_extension eq "ogg"){
      $src_pipe = "ogg123 -d raw -o byteorder:little --quiet "
                 ." --file - \"$src\"";
    }

    elsif ($src_extension eq "flac"){
      $src_pipe = "flac --stdout --decode --silent --endian=little "
                 ." --sign=signed --force-raw-format \"$src\"";
    }
    else{
      print "ERROR: unknown source format $src_extension\n";
    }
    


    ### get target file format -- pipe destination
    my $dst_name = $src_basename.".".$opt_f;
    if ($opt_f eq "mp3"){
      $dst_pipe = "lame -r -x --silent -b $opt_b - \"$dst_name-tmp\"";
    }

    elsif ($opt_f eq "ogg"){
      $dst_pipe = "oggenc - --raw --quiet -b $opt_b -o \"$dst_name-tmp\"";
    }

    elsif ($opt_f eq "flac"){
      $dst_pipe = "";
    }

    else{
      print "ERROR: unknown target format $opt_f\n";
    }


    ### transcode the file
    #
    # What goes through the pipe?
    # Format: raw uncompressed audio data 
    #         16 bits/sample, 44100 samples/sec, little-endian, stereo
    
    if ((length($src_pipe)>0) && (length($dst_pipe)>0)){
      #print "Exec:  $src_pipe | $dst_pipe\n";
      system "$src_pipe | $dst_pipe\n";
    }
    
    if (defined $opt_d){ system "rm \"$src\"";}
    system "mv \"$dst_name-tmp\" \"$dst_name\"";
    
  }
  else{
    print "ERROR: could not extract format of source file $src\n";
  }
}

if (defined $opt_s){
  system "echo ' ' > /home/music/outbox/exportstatus.txt"; # empty status
}

####################################################################################
# ogg filter pipe:
# ogg123 -d raw -o byteorder:big -f - filename.ogg  | lame -r - - | netcat -n 192.168.1.30 2020
#
# flac filter pipe:
# flac -c -d -s --endian=big --sign=signed --force-raw-format filename.flac | lame -r - - | netcat -n 192.168.1.30 2020
#
