#!/usr/bin/perl

##################################################
#
# GiantDisc mp3 Jukebox
# 
# © 2000 - 2002, Rolf Brugger
#
##################################################

# gdcompr.pl: GiantDisc compress wav file
# reads tracks to be ripped from stdin (pipe)
#

use lib '/home/music/bin';
use gdparams;
use gdgentools;
use DBI;
use strict;

### Global constants
my $pl_list = 0;
my $rp_list = 1;
my $co_list = 2;
my $cr_list = 3;

### Parameters
if(scalar(@ARGV) != 3){print("gdcompr needs two parameters (db-host, id and nb)\n");exit(0);}
my $dbhost   = $ARGV[0];
my $playerid = $ARGV[1];
my $audiochannel = $ARGV[2]; 

### open DB connection
my $dbh = DBI->connect("DBI:mysql:GiantDisc:".$dbhost, "music", undef)
    or die "unable to connect to GiantDisc db";

### set termination handler
$SIG{TERM} = sub { print "gdcompr killed\n"; $dbh->disconnect; exit;}; # set Kill event handler

my $base =    gdparams::gdbase();

my $logtarget  = gdgentools::param_value($dbh, $playerid, "logtarget");
my $mp3encoder = gdgentools::param_value($dbh, $playerid, "mp3encoder");

  
open OUTLOG, ">/home/music/tmp/gdcompr.log";
select OUTLOG;

#if($logtarget eq "stdout"){
#  print ("Compress Deamon Started for player $playerid channel $audiochannel\n");
#}
#else{
#  open OUTLOG, ">/dev/null";
#  select OUTLOG;
#}

### Get compress arguments
while(<STDIN>){
  my $argline=$_;
  my($action, $tracknb, $trackid, $encformat, $length, $filepath, $filename,
	$artist, $title) = split /\t/, $argline;
  sleep 2;
#  print "gdcompr: $tracknb. $artist - $title enc:$mp3encoder, targ:$logtarget\n";
#  print "  ID: $trackid, BR: $bitrate, LEN: $length\n";
#  print "  $filepath/$filename\n";

  my ($audiofmt, $bitrate) = split ' ', $encformat;  # split "mp3 128"
  my $wavfile    = "$filepath/$filename-$tracknb.wav";
  my $targetfile = "$filepath/$filename-$tracknb.".$audiofmt;

  print "Encoding $tracknb. $artist - $title to $targetfile at $bitrate\n";

  ### Compress it
  my $bitratearg;
  if ($audiofmt eq "mp3"){
    ### encode mp3 format ###
    if ($mp3encoder =~ m/l3enc/i){	### Fraunhofer l3enc
      $bitratearg="-br 128000";
      if (   $bitrate eq "64"  || $bitrate eq "96" || $bitrate eq "112"
          || $bitrate eq "128" || $bitrate eq "192"){
        $bitratearg="-br ".$bitrate."000";
      }
      print("l3enc $wavfile $targetfile $bitratearg -hq\n");
      system("l3enc $wavfile $targetfile $bitratearg -hq");
    }
    
    if ($mp3encoder =~ m/lame/i){	### Lame encoder (or older 'notlame')
      #$bitratearg=" -b 128";
      $bitratearg="--vbr-new -b 128";
      if (   $bitrate eq "64"  || $bitrate eq "96"  || $bitrate eq "112"
          || $bitrate eq "128" || $bitrate eq "160" || $bitrate eq "192"
          || $bitrate eq "256"){
        #$bitratearg="-b $bitrate";
        $bitratearg="--vbr-new -b $bitrate";
      }
      print("$mp3encoder -S -h $bitratearg $wavfile $targetfile\n");
      system("$mp3encoder -S -h $bitratearg $wavfile $targetfile >/dev/null");
      # option -S (silent) needed, otherwise it doesn't work if started as cronjob
    }
  }
  
  if ($audiofmt eq "ogg"){
    ### encode ogg format ###
    if (16<=$bitrate && $bitrate <= 320){
      print("oggenc $wavfile -b $bitrate $targetfile\n");
      system("oggenc $wavfile -b $bitrate $targetfile >/dev/null");
    }
    else{print "ERROR: strange target bitrate '$bitrate'\n";}
  }
  
  if ($audiofmt eq "flac"){
    ### encode flac format ###
    if (16<=$bitrate && $bitrate <= 320){
      print("flac $wavfile -o $targetfile\n");
      system("flac $wavfile -o $targetfile >/dev/null");
    }
    else{print "ERROR: strange target bitrate '$bitrate'\n";}
  }
  
  ### remove first track from compressionlist
  gdgentools::tracklist_del_item($dbh, $playerid, $co_list, 0);

  print("Removing file $wavfile\n");
  unlink $wavfile;

}

$dbh->disconnect;
print ("Teminated: gdcompr.pl\n");

#
