#!/usr/bin/perl
package main;
use Term::ANSIColor;
# WMA2mp3: Decode a WMA and make an mp3. 
$cpr=
'####################################################################
# This Perl script is free software; you can redistribute it
# and/or modify it under the terms of the GNU General Public
# License (as published by the Free Software Foundation) or the
# Artistic License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with this program; if not, write to the Free
# Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
####################################################################';

# Version
$VERSION = 0.1;

## GET READY.
print colored ['cyan'], "$0: == GETTING READY ==\n";

# Catch signals.
sub catch_zap {
  my $signame = shift;
  print colored ['cyan'], "$0: == CLEANING UP ==\n";
  unlink $fifo if ($fifo);
  die "Somebody sent me a SIG$signame";
}
$SIG{INT} = \&catch_zap;
$SIG{TERM}= \&catch_zap;
# Check for prereqs.
use Config;
my(@path) = split /$Config{'path_sep'}/, $ENV{'PATH'};
foreach (@path) {
  if (-f "$_/mplayer") {
    $mplayer = 1;
  }
  if (-f "$_/lame") {
    $lame = 1;
  }
}
if (! $mplayer) {
  die "$0: Can't find MPlayer\n  Error";
}
if (! $lame) {
  die "$0: Can't file LAME\n  Error";
}

# Get command-line options.
$help = <<END;
$0: [args] [wma] [mp3]
  --verbose            -v: Print extra info.
  --help               -h: Print this message.
  --version              : Print the version.
END
foreach (@ARGV) {
  if ((/^\-v$/) || (/^\-\-verbose$/)) {
    print colored ['green'], "$0: Got arg: verbose\n";
    $verbose = 1;
  }
  elsif ((/^\-\-fifo$/) || (/^\-f$/)) {
    $fifo = "$1";
  }
  elsif ((/^\-\-help$/) || (/^\-h$/)) {
    print $help;
    exit;
  }
  elsif (/^\-\-version$/) {
    print "wma2mp3 $VERSION\n";
    print "$cpr\n";
    exit;
  }
  elsif ((/^\-\-stream=(.+)$/) || (/^\-s=(.+)$/)) {
    print colored ['green'], "$0: Ripping stream: $1\n" if ($verbose);
    $stream="$1";
  }
  elsif ((/^\-\-title=(.+)$/) || (/^\-t=(.+)$/)) {
    print colored ['green'], "$0: Ripping title: $1\n" if ($verbose);
    $title="$1";
  }
  else { 
    if (! $wma) {
      print colored ['green'], "$0: Reading from file: $_\n" if ($verbose);
      $wma = "$_";
    } else {
      print colored ['green'], "$0: Writing to file: $_\n" if ($verbose);
      $file = "$_";
    }
  }
}

# Sanity check 1: Check arguments.
if (! $file) {
  die "$0: Please specify file\n Error";
}

if (! $file) {
  die "$0: Please specify wma\n Error";
}

# Sanity check 2: Default FIFO.
if (! $fifo) {
  $fifo = "soundpipe";
}

# Check for FIFO.
if ((! -p "$fifo") && (-f "$fifo")) {
  print "$0: \"$fifo\" is not a FIFO\n$0: Remaking it." if ($verbose);
  unlink "$fifo";
}
if (! -p "$fifo") {
  use POSIX qw(mkfifo);
  $main::mkfifo=1;
  mkfifo("$fifo", 0700) || die "$0: Can't create FIFO: $!\n Error";
}

## START RIPPIMG
print colored ['cyan'], "$0: == ENCODING ==\n";
print colored ['blue'], "$0: Starting LAME: lame -b 64 $fifo '$file'...\n" if ($verbose);
open(LAME, "lame -b 64 $fifo '$file' |");
print colored ['blue'], "$0: Starting MPlayer: mplayer -quiet -ao pcm:file=$fifo -vo null -vc dummy '$wma'...\n" if ($verbose);
open(MPLAYER,"mplayer -quiet -ao pcm:file=$fifo -vo null -vc dummy '$wma' 2>&1 |");
while (<MPLAYER>) {
  print "mplayer: $_" if ($verbose);
}
while (<LAME>) {
  print "lame: $_";
}

## CLEAN UP
print colored ['cyan'], "$0: == CLEANING UP ==\n";
unlink "$fifo";

=pod

=head1 NAME

wma2mp3 - Convert a WMA to an MP3

=head1 SYNOPSIS

wma2mp3 [options] [wma] [mp3]

=head1 DESCRIPTION

B<wma2mp3> converts a WMA to an MP3 (SURPRISE!!!) using L<MPlayer|mplayer(1)> 
and pipe-ing the audio through L<LAME|lame(1)>. Don't worry, it doesn't 
encode in real-time. It accepts various options to specify how it rips, but 
you usually don't have to use them.

=head1 OPTIONS

=over 1
=item B<--verbose|-v>
Print extra info.

=item B<--help|-h>
Print this message.

=back

=head1 EXAMPLES

 # Encodes to F<movie.mp3>
 wma2mp3 movie.wma movie.mp3

 # Encodes to F<movie.mp3> verbosely.
 wma2mp3 -v movie.wma movie.mp3

=head1 SCRIPT CATEGORIES

Audio : MP3

=head1 AUTHOR

Michael Howell <m_howell123@yahoo.com>

=head1 COPYRIGHT

Copyright (c) 2007 Michael Howell. All rights reserved.
This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.

=head1 SEE ALSO

L<mplayer(1)> L<lame(1)>

=cut