#!/usr/bin/perl -w

use strict;

# dj@nagy:~/tmp% COMMENT="1998 A&M Records; 540 974-2" abcde -l
# ~/.abcde.conf
#
# abcde -w "1998 A&M Records; 540 974-2"
# dj@nagy:~/tmp% abcde -w '1993 A\&M Records\; 540 126-2'
#
# cat ~/.abcde.conf 
# LOWDISK=y
# OGGENCOPTS="-q 9"
# OUTPUTFORMAT='${ARTISTFILE}/${ALBUMFILE}/${TRACKNUM}-${TRACKFILE}'
# VAOUTPUTFORMAT='Various/${ALBUMFILE}/${TRACKNUM}-${ARTISTFILE}-${TRACKFILE}'
#
#
print "run OGGENCODEROPTS=\"-q 9\" COMMENT=\"blah blah\" abcde -l"
## OUTPUTFORMAT='${ARTISTFILE}-${ALBUMFILE}/${TRACKNUM}.${TRACKFILE}'
# OUTPUTFORMAT='${ARTISTFILE}/${ALBUMFILE}/${TRACKNUM}-${TRACKFILE}'
## VAOUTPUTFORMAT='Various-${ALBUMFILE}/${TRACKNUM}.${ARTISTFILE}-${TRACKFILE}'
# VAOUTPUTFORMAT='Various/${ALBUMFILE}/${TRACKNUM}-${ARTISTFILE}-${TRACKFILE}'

exit 0

my $d = 0;  # set to 1 for debug
# my $d = 1;

my $quality = $ENV{CDRIP_QUALITY} || 7;

# empty @ARGV to get a sane diamond operator
my @argv = @ARGV;
@ARGV = ();

my @songlist;
while (<>) {
  chomp;
  push @songlist, $_;
}

$d and print "songlist: @songlist\n";

my $i = 0;
for my $s (@songlist) {
  $i++;
  next if ($s =~ /^skip$/);
  my ($ra, $rt) = split ' - ', $s;

  defined $ra or die "hu!? no artist in songlist file!\n";

  unless (defined $rt) {
	# we are not in sampler mode
	$rt = $ra;
	undef $ra;
  } 

  # songtitle
  my $t = $rt;
  $t =~ s/ /_/g;
  $t =~ s[/][_]g and warn "removed evil / from $t\n";

  my $a;
  if (defined $ra) {
	 # artist
	  $a = $ra;
	  $a =~ s/ /_/g;
	  $a =~ s[/][_]g;
  }

  my $w = $i;
  $w = "0".$i unless ($i > 9);

  if (defined $a) {
	 $w = "$w"."-"."$a"."-"."$t"; # filename for .wav
  } else {
	$w = "$w" . "-" . "$t";
  }

  # $m = $w.".mp3";     # filename for .mp3
  my $m = $w.".ogg";     # filename for .ogg

  # $call = "cdparanoia $i - | lamer - $m";

  # [09-Jan:22:21 koef] $oggenc -m 2 Track01.wav
  # [09-Jan:22:21 koef] mode 2 zou 128 kbit moeten doen
  # mp3's zijn meestal in 128. oggenc default 144 kbit, da vreet meer schijf

  # [20-Jun:12:17 Fruit] 1411 kbit
  # [20-Jun:12:17 Fruit] ik rip naar flac :)

  # [20-Jun:12:18 Fruit] ogg -q3 is voor de meeste muziek van mij goed genoeg
  # [20-Jun:12:18 Fruit] als er iets gevoeligs tussen zit pak ik meestal -q5

  # my $call = "cdparanoia -q $i - | oggenc -b 128 -o $m";
  # gives bitrate_nominal=128031

  # let's see if we can crank quality up somewhat: -q 7 gives
  # bitrate_nominal=224032
  my $call = "cdparanoia -q $i - | oggenc -q $quality -o \"$m\"";
  for (@argv) {
    $call .= " \"$_\"";
  }

  if (defined $ra) {
    $call .= " -a \"$ra\"";
  }
  $call .= " -t \"$rt\" -";

  warn "system($call)\n";
  system($call) unless $d;

  # system("id3 -t \"$rt\" $m") unless $d;
  # system("id3 -a \"$ra\" $m") unless $d;
}

__END__

=pod

=head1 NAME

dr_cdrip - rip audio cd's to ogg

=head1 SYNOPSIS

B<dr_cdrip> B<[>I<args>B<]> < I<id3-tag-file>

=head1 DESCIPTION

dr_cdrip sets up a cdparanoia(1) - oggenc(1) pipe for each track on your 
audio cd, so that the audio on your cd will be stored as .ogg files on your 
harddisk. I<args> will get passed to each songs' oggenc(1) invocation.

I<id3-tag-file> could look like this:

 Jaffa - Star 67
 Think of one - Den Antwaarpse Shabi
 Klute - Angry Woman
 Electrotwist - The Smurfer

Evil characters are discarded from the file, we don't want
them to appear in filenames. If a line lacks a ' - ', the
line is assumed to be a title. 

Defauls setting of oggenc(1)'s --quality level is 7, override by
setting the CDRIP_QUALITY environment variable.

Note: mp3's id3 tags seem to be called "comments" in the .ogg world.

=head1 EXAMPLE

 dj@gelfand ~$ export CDRIP_QUALITY=9
 dj@gelfand /h.../The_New_York_Contemporary_Five$ dr_cdrip \
  -l "The New York Contemporary Five" -a "Archie Shepp" \
  -c "description=Don Cherry: crnt, Archie Shepp: ts, Don \
  Moore: b, JC Moses:d" -c "genre=Jazz" -c "date=1963" \
  -c "location=Copenhagen" \
  -c "copyright=1991 Storyville Records" -c "license=All Rights Reserved" \
  -c "organization=Storyville" \
  -c "contact=http://www.storyville-records.com/" \
  -c "diskid=bd10c70d" < /tmp/songlist 

See DISCID HOWTO on
http://freedb.org/modules.php?name=Sections&sop=viewarticle&artid=6 for info on
disk id.  Beware!  A diskid comment is _not_ mentioned in the xiph.org
suggestions!

(oggenc's -t will be used by dr_cdrip. -a might be.)

supported extra comments are (see ogg123.c) 

 VERSION=, TRACKNUMBER=, ISRC=

ARTIST is -a, ALBUM is -l TITLE is -t.

VERSION is e.g. 'Biosphere Foo Mix'.  Multipe ARTIST tags can occur; this use
is encouraged if applicable.  -c foo will be displayed by ogg123 as 'Comment:
foo'.

Run something like:

 cdcd tracks | sed 's/^.*\]\ *//' | sed 's/\ $//'

to extract a songlist from a cddb database.

=head1 BUGS

The format of the songinfo input file is non-standard.  Support for cddb is
lacking.

=head1 SEE ALSO

http://xiph.org/ogg/vorbis/doc/v-comment.html

abcde - A Better CD Encoder at http://www.hispalinux.es/~data/abcde.php

=head1 VERSION, SOURCE

This file is maintained at http://git.mdcc.cx/draai

=head1 HISTORY

First version written 2001-01-09.  Last maintenance, as of 2007-08-22,
done at 2005-12-14.

=head1 COPYRIGHT

Copyright: © 2001-2005 Joost van Baal

This program is in the public domain.

=head1 AUTHOR

Joost van Baal <spamhere@mdcc.cx>

=cut

 
