#!/usr/bin/perl

# cd2mp3 - MP3 manipulation utility - JAH 21/01/01
# Uses LAME and Dagrab 
# This program can be freely distributed, modified, sold etc.. as 
# long as this banner message and links to my website are retained. 

###CONTRIBUTIONS###
###################

# Contribution by Derek Harper based on Ver 0.8
# Amendments to version 0.8 :
#    Fixed option 5 (Quit)
#    Fixed wildcard filename input for lame in entire CD option
#    Entire CD option now deletes wav files progressively, using less disk space

###################

# Default values

$default_sample_rate = "128";
$default_cdrom = "/dev/cdrom";

# Print welcome screen

print "\n\n  /================================================\\ \n";
print "  |           Cd2Mp3 - V0.81 21/01/01 JAH           |\n";  
print "  |          http://www.jamiesdomain.org.uk         |\n";
print "  |================================================|\n";
print "  |   Please check dagrab and LAME are installed   |\n";
print "  |              In your path                      |\n";
print "  |================================================|\n";
print "  |   This free software project is sponsored by   |\n";
print "  |    Psi-Domain - http://www.psi-domain.co.uk    |\n";
print "  \\================================================/\n"; 

sub check {
     if ($presamp =~ /\b56\b/) {
         $finalsamp = "56";
        }
     elsif ($presamp =~ /\b128\b/) {
         $finalsamp = "128";
        }
     elsif ($presamp =~ /\b256\b/) {
         $finalsamp = "256";
        }
     else {
        print("\n $default_sample_rate\K default used.\n ");
         $finalsamp = $default_sample_rate;
       }
}
      
# This function makes sure the info passed to LAME is correct

sub sytx_correct {
            if ($track <= 9) {
             $fileprfx = "track0";
             }
            else {
             $fileprfx = "track";
            }
}

# Function to run dagrab and LAME, and clean up any temp files

sub encode {
           system ("dagrab -v -d $drive $track");
           print "\n$fileprfx$track Ripped!! Encoding to MP3..\n";
           system ("lame -b $finalsamp $fileprfx$track.wav $outputmp3"); 
           system ("rm $fileprfx$track.wav");
           print "\nMP3 Created as $outputmp3. Thanks for using cd2mp3.\n\n"; 
}

# Function to ask for sample rate

sub ask_sample_rate {
   print "Enter Sampling rate. Default is $default_sample_rate\K \(Values are 56, 128 or 256\): ";
   print STDIN " ";
   $presamp = <STDIN>;
   check();
   print "\n Using a bitrate of $finalsamp\K \n";
}

# Function to ask for mp3 output file name

sub ask_output_mp3 {
   print "\nEnter the path and output file name \(ie /home/jamie/coolmp3.mp3\): ";
   print STDIN " ";
   chop($outputmp3 = <STDIN>);
   print "\n MP3 Will be stored as $outputmp3\n";
}

# Function to ask for location of CD-ROM drive

sub ask_cdrom_drive {
   print "\nWhere is your CD-ROM Drive? Default $default_cdrom : ";
   print STDIN " ";
   chop($drive = <STDIN>);
   if ($drive eq "") {
      $drive = $default_cdrom;
   }
   print "\n Using $drive";
}

# Encode single track

sub encode_track {
   # Sampling rate input and checking
   ask_sample_rate();
   # output file
   ask_output_mp3();
   # ask for cdrom
   ask_cdrom_drive();
   # get track number
   print "\nWhat track do you want to encode \(Use Track Number Ie 5 For Track5\): ";
   print STDIN " ";
   chop($track = <STDIN>);
   sytx_correct();
   print "\n Now Ripping $fileprfx$track!\n\n";
   # Execute the encoding routine
   encode();
   exit;
}

# Entire disk routine

sub entire {
           ask_cdrom_drive();
           ask_sample_rate();
           print "\nOutput directory for mp3's \(ie: /usr/home/sparc/mp3\): ";
           print STDIN " ";
  	   chop($dest = <STDIN>);
           print "\nOK, encoding entire CD, output to $dest\n\n";
	   system("dagrab -v -d $drive -a -f $dest/track\%02d.wav");
	   while($lame_input_wav = <$dest/*.wav>) {
	      system("lame -b $finalsamp $lame_input_wav");
	      qx{rm $lame_input_wav};
	   }
           print "\nCD encoded in $dest. Thanks for using cd2mp3!\n\n";
           exit;
}	
           
# .WAV file routine

sub wav {
        ask_cdrom_drive();
        print "\nOutput .WAV file to \(ie: /usr/home/sparc/audio/mytrack.wav\): ";
        print STDIN " ";
        chop($wavout = <STDIN>);
        print "\n OK, output file will be $wavout\n";
        print "\nWhich track do you want to rip? \(Use Track Number Ie 5 For Track5\): ";
        print STDIN " ";
        chop($wavrip = <STDIN>);
        print "\n Ripping Track $wavrip ! \n\n";
        system("dagrab -d $drive -v -f $wavout $wavrip");
        print "\n Track $wavrip Ripped ! Thanks for using cd2mp3 \n\n";
        exit;
} 

# .WAV to MP3

sub w2m { 
           print "\n";
           ask_sample_rate();
           print "\nEnter full path to .WAV file to encode \(ie /usr/home/sparc/mysong.wav\): ";
           print STDIN " ";
           chop($wavpath = <STDIN>);
           print "\nEnter destination for MP3 \(ie /usr/home/sparc/mysong.mp3\): ";
           print STDIN " ";
           chop($wavdest = <STDIN>);
           print "\nDelete .WAV file after encode? \(y/n\): ";
           print STDIN " ";  
           chop($dele = <STDIN>);
           print "\n Encoding $wavpath \n";
           system("lame -b $finalsamp $wavpath $wavdest");
             if ($dele =~ /\by\b/) {
		      system("rm $wavpath");
                  }
             else {
               }
           print "\n $wavpath encoded to mp3. Thanks for using cd2mp3 \n\n";
           exit;
      }

menu();

sub menu {
   print "\n";
   print "1.\) Encode a single track\n";
   print "2.\) Encode the entire CD\n";
   print "3.\) Rip a track from CD to a .WAV file\n";
   print "4.\) Encode a .WAV file to MP3\n";
   print "5.\) Quit cd2mp3\n";
   print "\nWhat would you like to do today?: ";
   print STDIN " ";
   $eors = <STDIN>;
   if ($eors =~ /\b1\b/) {
          print "\nOK, encoding a single track from CD\n";
          print "\n  Hit Enter to accept defaults\n\n";
          encode_track();
      }
   elsif ($eors =~ /\b2\b/) {
          print "\nOK, encoding entire CD\n";
          print "\n  Hit Enter to accept defaults\n\n";
          entire();
      }
   elsif ($eors =~ /\b3\b/) {
          print "\nOK, Ripping a Track to .WAV file\n";
          print "\n  Hit Enter to accept defaults\n";
          wav();
      }
   elsif ($eors =~ /\b4\b/) {
          print "\nOK, Encoding a .WAV file\n";
          print "\n  Hit Enter to accept defaults\n";
          w2m();
      }
   elsif ($eors =~ /\b5\b/) {
	  print "\nBye!\n\n";
      }
   else {
         print "\nBad choice, please enter either 1,2,3,4 or 5. Try again\n";
         menu();
       }
}

