# $Id: soundconvertrc_sample,v 1.3 2006-07-31 13:22:20 mitch Exp $
#
# configuration file for soundconvert.pl
# copy to ~/.soundconvertrc
# this is eval()ed on startup, so be sure not to have malicious code in here!

# add personalized typelist entries below:
$typelist->{'psyco_mp3out'} = {

    TYPE => 'sound',
    IO => 'o',
    NAME => 'MP3-stick',
    NEW_EXTENSION => 'mp3',
    CHECK_FOR_TOOLS => sub {
	my $have_mp3_info;
	BEGIN {
	    eval { require MP3::Info; };
	    $have_mp3_info = not $@;
	}
	if (not $have_mp3_info) {
	    warn "MP3 unavailable: Perl module MP3::Info not found";
	    return 0;
	}
	unless (defined which('lame')) {
	    warn "MP3 unavailable: binary lame not found";
	    return 0;
	}
	return 1;
    },
    GET_INFO => sub {
	# no-op, output only
	return {};
    },
    REMAP_INFO => {
    },
    DECODE_TO_WAV => sub {
	# no-op, output only
    },
    ENCODE_TO_NATIVE => sub {
	my $file = shift;
	my $tags = shift;
	my @call = ('lame',
		    '--abr','128',
		    '--vbr-new',
		    '-q','1',
		    '-',
		    $file
		    );
	
	print STDERR "  encoding: <@call>\n";
	exec { $call[0] } @call;
    },
    TAG_NATIVE => sub {
	my $file = shift;
	my $tags = shift;
	MP3::Info::set_mp3tag( $file, $tags );
    },

};

$typelist->{'audio/mp3_idv2'} = {
    TYPE => 'sound',
    IO => 'o',
    NAME => 'MP3v2',
    NEW_EXTENSION => 'mp3',
    CHECK_FOR_TOOLS => sub {
	my $have_mp3_tag;
	BEGIN {
	    eval { require MP3::Tag; };
	    $have_mp3_tag = not $@;
	}
	if (not $have_mp3_tag) {
	    warn "MP3v2 unavailable: Perl module MP3::Tag not found";
	    return 0;
	}
	unless (defined which('lame') or defined which('toolame')) {
	    warn "MP3v2 unavailable: neither binary lame nor binary toolame not found";
	    return 0;
	}
	return 1;
    },
    GET_INFO => sub {
	# no-op, output only
	return {};
    },
    REMAP_INFO => {
    },
    DECODE_TO_WAV => sub {
	# no-op, output only
	return {};
    },
    ENCODE_TO_NATIVE => sub {
	my $file = shift;
	my $tags = shift;
	my $binary = 'lame';
	unless (defined which($binary)) {
	    $binary = 'toolame';
	}
	my @call = ($binary,
		    '--preset','cbr','160',
		    '-',
		    $file
		    );
	
	print STDERR "  encoding: <@call>\n";
	exec { $call[0] } @call;
    },
    TAG_NATIVE => sub {
	my $file = shift;
	my $tags = shift;
	my $mp3 = MP3::Tag->new($file);
	my $id3v2 = $mp3->new_tag('ID3v2');
	my %taglist = (
	    'TITLE'    => 'TIT2',
	    'TRACKNUM' => 'TRCK',
	    'ARTIST'   => 'TPE1',
	    'ALBUM'    => 'TALB',
	    'YEAR'     => 'TYER',
	    #'GENRE'   => ???
	);
	foreach my $tag (keys %taglist) {
	    if (exists $tags->{$tag}) {
		$id3v2->add_frame($taglist{$tag}, $tags->{$tag});
	    }
	}
	$id3v2->write_tag();
    },
    
};

# change default encoder
$encoder = $typelist->{'psyco_mp3out'};
#$encoder = $typelist->{'audio/mpeg'};
#$encoder = $typelist->{'audio/flac'};
#$encoder = $typelist->{'application/ogg'};

