[Templates] [RFC] Template::Plugin::MP3

darren chamberlain dlc@users.sourceforge.net
Wed, 6 Nov 2002 08:27:05 -0500


--IiVenqGWf+H9Y6IX
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

* darren chamberlain <dlc@users.sourceforge.net> [2002-11-06 07:41]:
> And so on.  Comments?

...and here is the actual attachment.

(darren)

-- 
In a sense Christianity is like Jazz - if you need to ask the
questions you won't understand the answers.
    -- Bob Billing

--IiVenqGWf+H9Y6IX
Content-Type: application/x-perl
Content-Disposition: attachment; filename="MP3.pm"
Content-Transfer-Encoding: quoted-printable

#=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D -*-Perl-*-=0A#=0A# Template::Plugin::M=
P3=0A#=0A# DESCRIPTION=0A#=0A#   Template Toolkit plugin for MP3::Info=0A#=
=0A# AUTHOR=0A#   Darren Chamberlain <dlc@users.sourceforge.net>=0A#=0A# CO=
PYRIGHT=0A#   This module is free software; you can redistribute it and/or=
=0A#   modify it under the same terms as Perl itself.=0A#=0A# REVISION=0A# =
  $Id$=0A#=0A#=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=0A=0Apackage Template::Plugin::MP3;=0A=0Arequire 5.004;=
=0A=0Ause strict;=0Ause vars qw( $VERSION $AUTOLOAD );=0Ause base qw( Templ=
ate::Plugin );=0A=0Amy $ETYPE =3D "mp3";=0A=0Ause File::Spec::Functions qw(=
catfile);=0Ause MP3::Info ();=0Ause Template::Plugin;=0A=0A$VERSION =3D spr=
intf("%d.%02d", q$Revision$ =3D~ /(\d+)\.(\d+)/);=0A=0A=0A# ---------------=
-------------------------------------------------------=0A# new($context, $=
name, \%config)=0A#=0A# Creates a new MP3 instance.  Requires the name of a=
n MP3 file,=0A# either as the first positional argument or as the 'name' el=
ement of=0A# the configuration hash.  The user can also specify a base dire=
ctory=0A# (using 'dir'), which will be used as a base for the aforementione=
d=0A# name.=0A# -----------------------------------------------------------=
-----------=0Asub new {=0A    my $config =3D ref($_[-1]) eq 'HASH' ? pop @_=
 : { };=0A    my ($class, $context, $file) =3D @_;=0A    my ($dir, $mp3);=
=0A=0A    # Allow filename to be positional or named=0A    $file =3D $confi=
g->{'name'}=0A        if (! defined $file && defined $config->{'name'});=0A=
=0A    # Allowances for Template::Plugin::File instances=0A    $file =3D $f=
ile->name=0A        if (UNIVERSAL::can($file, "name"));=0A=0A    # Allow fi=
lename to be relative to a root directory=0A    $file =3D catfile($config->=
{'dir'}, $file)=0A        if defined $config->{'dir'};=0A=0A    -e $file or=
 $context->throw($ETYPE, "File '$file' does not exist");=0A=0A    $mp3 =3D =
MP3::Info->new($file)=0A        || $context->throw($ETYPE,=0A            "C=
an't create MP3::Info object for mp3 file '$file'");=0A=0A    if (defined $=
config->{'utf8'}) {=0A        MP3::Info::use_mp3_utf8($config->{'utf8'});=
=0A    }=0A=0A    bless {=0A        _CONTEXT =3D> $context,=0A        _MP3 =
    =3D> $mp3,=0A    } =3D> $class;=0A}=0A=0Asub AUTOLOAD {=0A    my $self =
=3D shift;=0A   (my $a =3D $AUTOLOAD) =3D~ s/.*:://;=0A=0A    $self->{ _MP3=
 }->$a(@_);=0A}=0A=0Asub mp3_genres    { [ @MP3::Info::mp3_genres    ] }=0A=
sub winamp_genres { [ @MP3::Info::winamp_genres ] }=0A=0Asub genres {=0A   =
 my @mp3_genres =3D mp3_genres;=0A    my @winamp_genres =3D winamp_genres;=
=0A    return [ @mp3_genres, @winamp_genres ]=0A}=0A=0A1;=0A=0A__END__=0A=
=0A=3Dhead1 NAME=0A=0ATemplate::Plugin::MP3 - Interface to the MP3::Info Mo=
dule=0A=0A=3Dhead1 SYNOPSIS=0A=0A    [% USE mp3 =3D MP3("Montana.mp3") %]=
=0A=0A    [% mp3.title %]=0A    [% mp3.album %]=0A=0A    # perldoc MP3::Inf=
o for more ideas=0A=0A=3Dhead1 DESCRIPTION=0A=0ATemplate::Plugin::MP3 provi=
des a simple wrapper for using MP3::Info in=0Aobject oriented mode; see L<M=
P3::Info> for more details.=0A=0A=3Dhead1 CONSTRUCTOR and CONFIGURATION =0A=
=0ATemplate::Plugin::MP3 takes a filename as its primary argument:=0A=0A   =
 [% USE MP3("Tryin_To_Grow_A_Chin.mp3") %]=0A=0AOptional configuration info=
 can also be specified in the constructor:=0A=0A    [% USE MP3("Camarillo_B=
rillo.mp3", utf8 =3D> 1, dir =3D> "/mp3") %]=0A=0AThe name of the file can =
also be specified as a namd parameter=0A(C<name>):=0A=0A    [% USE MP3(name=
 =3D> "A_Token_Of_My_Extreme.mp3", dir =3D> "/mp3") %]=0A=0ATemplate::Plugi=
n::MP3 understands the following options:=0A=0A=3Dover 8=0A=0A=3Ditem B<nam=
e>=0A=0AThe name of the MP3 file.  Note that if both a positional argument =
and=0Aa C<name> parameter are passed the positional argument will take=0Apr=
ecedence.=0A=0A=3Ditem B<dir>=0A=0ASpecify a base directory name; will be p=
repended to the filename, if=0Ait is defined.=0A=0A=3Ditem B<utf8>=0A=0ADet=
ermines whether results should be returned in UTF-8, as handled by=0AMP3::I=
nfo's use_mp3_utf8() function.  See L<MP3::Info/use_mp3_utf8>.=0ANote that =
this requires L<Unicode::String|Unicode::String>.=0A=0A=3Dback=0A=0AIf the =
constructor cannot create an instance using the filename=0Apassed, a C<plug=
in.mp3> Exception is thrown, which will need to be=0Acaught appropriately:=
=0A=0A    [% TRY %]=0A        [% USE mp3 =3D MP3("Willie The Pimp.mp3") %]=
=0A    [% CATCH plugin.mp3 %]=0A        Can't find that MP3; are you sure y=
ou spelled it right?=0A    [% CATCH %]=0A        Unexpected exception: [% e=
rror %]=0A    [% END %]=0A=0A=3Dhead1 METHODS=0A=0ATemplate::Plugin::MP3 pr=
ovides the following, mostly intuitive,=0Amethods:=0A=0A=3Dover 16=0A=0A=3D=
item B<file>=0A=0AName of the file.=0A=0A=3Ditem B<artist>=0A=0AName of the=
 artist.=0A=0A=3Ditem B<album>=0A=0AName of the album.=0A=0A=3Ditem B<track=
num>=0A=0ATrack number of the song.=0A=0A=3Ditem B<bitrate>=0A=0ABitrate at=
 which the mp3 was encoded.=0A=0A=3Ditem B<size>=0A=0ASize of the file, in =
bytes.=0A=0A=3Ditem B<time>, B<secs>, B<mm>, B<ss>, B<ms>=0A=0ALength of th=
e song, in various permutations.  For example:=0A=0A=3Dover 8=0A=0A=3Ditem =
B<time>=0A=0A03:37=0A=0A=3Ditem B<secs>=0A=0A217.0253125=0A=0A=3Ditem B<mm>=
=0A=0A3=0A=0A=3Ditem B<ss>=0A=0A27=0A=0A=3Ditem B<ms>=0A=0A25.3125000000125=
=0A=0A=3Dback=0A=0A=3Ditem B<genre>=0A=0AGenre of the MP3.=0A=0A=3Ditem B<t=
agversion>=0A=0AFull name of the version of the MP3 tag, e.g. "ID3v1.1"=0A=
=0A=3Ditem B<version>=0A=0AVersion of the MP3 tag: 1 or 2=0A=0A=3Dback=0A=
=0AMP3::Info defines some other fields that I don't grok; try=0A=0A    [% M=
P3.Dump %]=0A=0Ato see them all.=0A=0AOf course, all of the above methods d=
on't return the advertised value=0Aif the tag being read does not contain u=
seful information.=0A=0A=3Dhead1 OTHER STUFF=0A=0ATemplate::Plugin::MP3 pro=
vides access to the @mp3_genres and=0A@winamp_genres arrays via the mp3_gen=
res() and winamp_genres() class=0Amethods, or collectively through the genr=
es() class method:=0A=0A    [% FOREACH genre =3D MP3.genres %]=0A       * [=
% genre;=0A    END %]=0A=0A
--IiVenqGWf+H9Y6IX--