#!/usr/bin/perl
#simple_player: let dermixd play a simple playlist (gaplessly)

#Copyright (c) 2006-2014 Thomas Orgis (thomas@orgis.org)
#This is Open Source, Distribution and Modification under the terms of the Artistic License
#see the file LICENSE in the ThOrMA package root or at a place some steps upwards directory-wise if you got this file in a separate package

use strict;

use FindBin qw($Bin);
use lib $Bin.'/../lib/dermixd';

use DerMixD::Control;
use Config::Param;

my $p = Config::Param::get
(
	{'info','simple non-interactive dermixd player; I just play files (paths feed via stdin) on the two channels of a running dermixd in a gapless manner','multi',1},
	[
		'cache',0,'c','cache files in local directory (useful when playing from a cd/dvd)',
		'cache_dir',$Bin,'d','directory to create cache files in',
		'cache_name','sp_channel','n','base name of cache files',
		'chana', '0', 'a', 'channel A',
		'chanb', '1', 'b', 'channel B',
		'normalize', 0, 'N', 'adjust tracks to common volume level',
		'level', '-6', 'l', 'level to normalize to'
	]
);

$SIG{INT} = \&Clean;

my $cprefix = $p->{cache_dir}.'/'.$p->{cache_name};
my %cached;
my $pal = new DerMixD::Control({'remote','localhost','echo',1});
unless($pal->{online}){die "Not online ($!)\n";}

$pal->Command(['cd', $ENV{PWD}]);

my $old;
my $a = $p->{chanb};
my $b = $p->{chana};

print "reading list of files from stdin\n";

while(<STDIN>)
{
	chomp;
	PlayTrack($_) if /^\s*[^#]/;
}

sub PlayTrack()
{
	my $file = shift;
	my $ret;
	if($p->{cache})
	{
		my $cache = $cprefix.$b;
		if($file =~ /(\.[^\/.]+)$/){ $cache .= $1; }
		print "caching $file as $cache...\n";\
		unlink($cache) if -f $cache;
		delete $cached{$cache};
		unless(system("cp '$file' '$cache'"))
		{
			#success
			$file = $cache;
			$cached{$file} = 1;
		}
		else{ print STDERR "Error caching ($!)\n"; }
		system("ls -l $file");
	}
	if
	(
		$ret = $pal->Command(['load',$b,$file])
		and
		$ret->[0] eq 'success'
	)
	{
		print "loaded $file on channel $b\n";
		if($p->{normalize})
		{
			$ret = $pal->Command(['scan', $b, 'power']);
			print "SCAN: $ret->[0]\n";
			my $preamp = 0;
			if($ret->[0] =~ /power: .*,\s+(\S+)\s*dBFS/)
			{
				print "LEVEL: $1 dBFS\n";
				$preamp = $p->{level} - $1;
			}
			$ret = $pal->Command(['preamp', $b, $preamp]);
			print "PREAMP: $ret->[0]\n";
		}
		#loaded, now wait for old track
		if($old)
		{
			print "OLD\n";
			
			if
			(
				$ret = $pal->Command(['follow',$a,$b])
				and
				$ret->[0] eq 'success'
			)
			{
				print "waiting for track on channel $a to end...\n";
				$pal->Command(['script',$b,1,'say','simplay_nextone']);
				$pal->WaitForSay('simplay_nextone');
				print " done\n";
			}
			else{ die "some serious problem!\n"; }
		}
		else{ $ret = $pal->Command(['start',$b]); }
		
		$old = $file;
		my $swap = $a;
		$a = $b;
		$b = $swap;				
	}
	else{ print "error loading $file on channel $b:\n".join("\n", @{$ret})."\n"; }

}

sub Clean
{
	if($p->{cache})
	{
		print "deleting cache files...\n";
		for(keys %cached)
		{
			print "$_\n";
			unlink($_) or print STDERR "error unlinking $_!\n";
		}
	}
	exit();
}
