[1] | 1 | package MP3::Find::Base; |
---|
| 2 | |
---|
| 3 | use strict; |
---|
| 4 | use warnings; |
---|
| 5 | |
---|
| 6 | use vars qw($VERSION); |
---|
| 7 | use Carp; |
---|
| 8 | |
---|
| 9 | $VERSION = '0.01'; |
---|
| 10 | |
---|
| 11 | my %format_codes = ( |
---|
| 12 | a => 'ARTIST', |
---|
| 13 | t => 'TITLE', |
---|
| 14 | b => 'ALBUM', |
---|
| 15 | n => 'TRACKNUM', |
---|
| 16 | y => 'YEAR', |
---|
| 17 | g => 'GENRE', |
---|
| 18 | ); |
---|
| 19 | |
---|
| 20 | sub new { |
---|
| 21 | my $invocant = shift; |
---|
| 22 | my $class = ref $invocant || $invocant; |
---|
[2] | 23 | my %options = @_; |
---|
| 24 | my $self = \%options; |
---|
[1] | 25 | bless $self, $class; |
---|
| 26 | } |
---|
| 27 | |
---|
| 28 | sub find_mp3s { |
---|
| 29 | my $self = shift; |
---|
| 30 | my %opt = @_; |
---|
| 31 | |
---|
| 32 | my $dir = $opt{dir} || $ENV{HOME}; |
---|
| 33 | my @DIRS = ref $dir eq 'ARRAY' ? @$dir : ($dir); |
---|
| 34 | |
---|
| 35 | my %QUERY = %{ $opt{query} || {} }; |
---|
| 36 | |
---|
| 37 | # array ref for multiple sort fields, but allow |
---|
| 38 | # a simple scalar for single values |
---|
| 39 | my @SORT = $opt{sort} ? |
---|
| 40 | (ref $opt{sort} eq 'ARRAY' ? @{ $opt{sort} } : ($opt{sort})) : |
---|
| 41 | (); |
---|
| 42 | |
---|
| 43 | foreach (keys %QUERY) { |
---|
| 44 | # so we don't have spurious warnings when trying to match against undef |
---|
| 45 | delete $QUERY{$_} unless defined $QUERY{$_}; |
---|
[2] | 46 | # package everything uniformly, so subclasses don't need to unpack it |
---|
[1] | 47 | $QUERY{$_} = [ $QUERY{$_} ] unless ref $QUERY{$_} eq 'ARRAY'; |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | # do the search |
---|
| 51 | my @results = $self->search(\%QUERY, \@DIRS, \@SORT, \%opt); |
---|
| 52 | |
---|
| 53 | # maybe they want the unformatted data |
---|
| 54 | return @results if $opt{no_format}; |
---|
| 55 | |
---|
| 56 | if ($opt{printf}) { |
---|
| 57 | # printf style output format |
---|
| 58 | foreach (@results) { |
---|
| 59 | my $output = $opt{printf}; |
---|
| 60 | for my $code (keys %format_codes) { |
---|
| 61 | |
---|
| 62 | while ($output =~ m/%((-\d)?\d*)$code/g) { |
---|
| 63 | # field size modifier |
---|
| 64 | my $modifier = $1 || ''; |
---|
| 65 | # figure out the size of the formating code |
---|
| 66 | my $code_size = 2 + length($modifier); |
---|
| 67 | my $value = sprintf("%${modifier}s", $_->{$format_codes{$code}} || ''); |
---|
| 68 | substr($output, pos($output) - $code_size, $code_size, $value); |
---|
| 69 | } |
---|
| 70 | } |
---|
| 71 | # to allow literal '%' |
---|
| 72 | $output =~ s/%%/%/g; |
---|
| 73 | $_ = $output; |
---|
| 74 | } |
---|
| 75 | } else { |
---|
| 76 | # just the filenames, please |
---|
| 77 | @results = map { $_->{FILENAME} } @results; |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | return @results; |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | sub search { |
---|
| 84 | croak "Method 'search' not implemented in " . __PACKAGE__; |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | # module return |
---|
| 88 | 1; |
---|
| 89 | |
---|
| 90 | =head1 NAME |
---|
| 91 | |
---|
[2] | 92 | MP3::Find::Base - Base class for MP3::Find finders |
---|
[1] | 93 | |
---|
| 94 | =head1 SYNOPSIS |
---|
| 95 | |
---|
[2] | 96 | package MyFinder; |
---|
| 97 | use base 'MP3::Find::Base'; |
---|
[1] | 98 | |
---|
[2] | 99 | sub search { |
---|
| 100 | my $self = shift; |
---|
| 101 | my ($query, $dirs, $sort, $options) = @_; |
---|
| 102 | |
---|
| 103 | # do something to find and sort the mp3s... |
---|
| 104 | my @results = do_something(...); |
---|
| 105 | |
---|
| 106 | return @results; |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | package main; |
---|
| 110 | my $finder = MyFinder->new; |
---|
| 111 | |
---|
| 112 | # see MP3::Find for details about %options |
---|
| 113 | print "$_\n" foreach $finder->find_mp3s(\%options); |
---|
[1] | 114 | |
---|
| 115 | =head1 DESCRIPTION |
---|
| 116 | |
---|
[2] | 117 | This is the base class for the classes that actually do the |
---|
| 118 | searching and sorting for L<MP3::Find>. |
---|
[1] | 119 | |
---|
[2] | 120 | =head1 METHODS |
---|
[1] | 121 | |
---|
[2] | 122 | =head2 new |
---|
[1] | 123 | |
---|
[2] | 124 | Really simple constructor. If you pass it a hash of options, it |
---|
| 125 | will hang on to them for you. |
---|
[1] | 126 | |
---|
[2] | 127 | =head2 search |
---|
[1] | 128 | |
---|
[2] | 129 | This is the one you should override in your subclass. If you |
---|
| 130 | don't, the base class C<search> method will croak. |
---|
[1] | 131 | |
---|
[2] | 132 | The C<search> method is called by the C<find_mp3s> method with |
---|
| 133 | the following arguments: the finder object, a hashref of query |
---|
| 134 | parameters, an arrayref of directories to search, and a hashref |
---|
| 135 | of miscellaneous options. |
---|
[1] | 136 | |
---|
[2] | 137 | The search method should return a list of hashrefs representing |
---|
| 138 | the results of the search. Each hashref should have the following |
---|
| 139 | keys (all except C<FILENAME> are derived from the keys returned |
---|
| 140 | by the C<get_mp3tag> and C<get_mp3Info> functions from L<MP3::Info>): |
---|
[1] | 141 | |
---|
[2] | 142 | FILENAME |
---|
| 143 | |
---|
| 144 | TITLE |
---|
| 145 | ARTIST |
---|
| 146 | ALBUM |
---|
| 147 | YEAR |
---|
| 148 | COMMENT |
---|
| 149 | GENRE |
---|
| 150 | TRACKNUM |
---|
| 151 | |
---|
| 152 | VERSION -- MPEG audio version (1, 2, 2.5) |
---|
| 153 | LAYER -- MPEG layer description (1, 2, 3) |
---|
| 154 | STEREO -- boolean for audio is in stereo |
---|
| 155 | |
---|
| 156 | VBR -- boolean for variable bitrate |
---|
| 157 | BITRATE -- bitrate in kbps (average for VBR files) |
---|
| 158 | FREQUENCY -- frequency in kHz |
---|
| 159 | SIZE -- bytes in audio stream |
---|
| 160 | OFFSET -- bytes offset that stream begins |
---|
| 161 | |
---|
| 162 | SECS -- total seconds |
---|
| 163 | MM -- minutes |
---|
| 164 | SS -- leftover seconds |
---|
| 165 | MS -- leftover milliseconds |
---|
| 166 | TIME -- time in MM:SS |
---|
| 167 | |
---|
| 168 | COPYRIGHT -- boolean for audio is copyrighted |
---|
| 169 | PADDING -- boolean for MP3 frames are padded |
---|
| 170 | MODE -- channel mode (0 = stereo, 1 = joint stereo, |
---|
| 171 | -- 2 = dual channel, 3 = single channel) |
---|
| 172 | FRAMES -- approximate number of frames |
---|
| 173 | FRAME_LENGTH -- approximate length of a frame |
---|
| 174 | VBR_SCALE -- VBR scale from VBR header |
---|
[1] | 175 | |
---|
| 176 | |
---|
[2] | 177 | =head2 find_mp3s |
---|
[1] | 178 | |
---|
[2] | 179 | The method that should be called by the program doing the searching. |
---|
[1] | 180 | |
---|
[2] | 181 | See L<MP3::Find> for an explanation of the options that can be passed |
---|
| 182 | to C<find_mp3s>. |
---|
[1] | 183 | |
---|
| 184 | =head1 TODO |
---|
| 185 | |
---|
[2] | 186 | More format codes? Possibly look into using L<String::Format> |
---|
[1] | 187 | |
---|
| 188 | =head1 SEE ALSO |
---|
| 189 | |
---|
[2] | 190 | L<MP3::Find>, L<MP3::Find::Filesystem>, L<MP3::Find::DB> |
---|
| 191 | |
---|
[1] | 192 | See L<MP3::Info> for more information about the fields you can |
---|
| 193 | search and sort on. |
---|
| 194 | |
---|
| 195 | =head1 AUTHOR |
---|
| 196 | |
---|
| 197 | Peter Eichman <peichman@cpan.org> |
---|
| 198 | |
---|
| 199 | =head1 COPYRIGHT AND LICENSE |
---|
| 200 | |
---|
| 201 | Copyright (c) 2006 by Peter Eichman. All rights reserved. |
---|
| 202 | |
---|
| 203 | This program is free software; you can redistribute it and/or |
---|
| 204 | modify it under the same terms as Perl itself. |
---|
| 205 | |
---|
| 206 | =cut |
---|