AnyData/Format/Mp3.pm untested

filter: Statementstmt 0.0% 0 / 65 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 16 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 111 CC 3 SCAR 11.5
CC
3
Cov
0%
read_fields
6.0
get_mp3_tag
6.0
new
2.0
CRAP 3.1
1 ######################################################################
2 package AnyData::Format::Mp3;
3 ######################################################################
4 #
5 # copyright 2000 by Jeff Zucker <jeff@vpservices.com>
6 # all rights reserved
7 #
8 ######################################################################
9  
10 =head1 NAME
11  
12 AnyData::Format::Mp3 - tied hash and DBI access to Mp3 files
13  
14 =head1 SYNOPSIS
15  
16 use AnyData;
17 my $playlist = adTie( 'Passwd', ['c:/My Music/'] );
18 while (my $song = each %$playlist){
19 print $song->{artist} if $song->{genre} eq 'Reggae'
20 }
21  
22 OR
23  
24 use DBI
25 my $dbh = DBI->connect('dbi:AnyData:');
26 $dbh->func('playlist','Mp3,['c:/My Music'],'ad_catalog');
27 my $playlist = $dbh->selectall_arrayref( qq{
28 SELECT artist, title FROM playlist WHERE genre = 'Reggae'
29 });
30 # ... other DBI/SQL operations
31  
32 =head1 DESCRIPTION
33  
34 This module provides a tied hash interface and a DBI/SQL interface to MP files. It creates an in-memory database or hash from the Mp3 files themselves without actually creating a separate database file. This means that the database is automatically updated just by moving files in or out of the directories.
35  
36 Many mp3 (mpeg three) music files contain a header describing the song
37 name, artist, and other information about the music.
38  
39 Simply choose 'Mp3' as the format and give a reference to an array of directories containing mp3 files. Each file in those directories will become a record containing the fields:
40  
41 song
42 artist
43 album
44 year
45 genre
46 filename
47 filesize
48  
49 This module is a submodule of the AnyData.pm and DBD::AnyData.pm modules. Refer to their documentation for further details.
50  
51 =head1 AUTHOR & COPYRIGHT
52  
53 copyright 2000, Jeff Zucker <jeff@vpservices.com>
54 all rights reserved
55  
56 =cut
57 use strict;
58 use warnings;
59 use AnyData::Format::Base;
60 use AnyData::Storage::FileSys;
61 use AnyData::Storage::File;
62 use vars qw( @ISA $VERSION);
63 @AnyData::Format::Mp3::ISA = qw( AnyData::Format::Base );
64  
65 $VERSION = '0.12';
66  
67 sub new {
68 my $class = shift;
69 my $self = shift || {};
70 #use Data::Dumper; die Dumper $self;
71 my $dirs = $self->{dirs} || $self->{file_name} || $self->{recs};
72 $self->{col_names} = 'song,artist,album,year,genre,filename,filesize';
73 $self->{recs} =
74 $self->{records} = get_data( $dirs );
75 return bless $self, $class;
76 }
77  
78 sub storage_type { 'RAM'; }
79  
80 sub read_fields {
81 my $self = shift;
82 my $thing = shift;
83 return @$thing if ref $thing eq 'ARRAY';
84 return split ',', $thing;
85 }
86  
87 sub write_fields { die "WRITING NOT IMPLEMENTED FOR FORMAT Mp3"; }
88  
89 sub get_data {
90 my $dirs = shift;
91 my $table = [];
92 my @files = AnyData::Storage::FileSys::get_filename_parts(
93 {},
94 part => 'ext',
95 re => 'mp3',
96 dirs => $dirs
97 );
98 for my $file_info(@files) {
99 my $file = $file_info->[0];
100 my $cols = get_mp3_tag($file) || next;
101 my $filesize = -s $file;
102 $filesize = sprintf "%1.fmb", $filesize/1000000;
103 my @row = (@$cols,$file,$filesize);
104 push @$table, \@row;
105 # 'file_name,path,ext,fullpath,size,'
106 # 'name,artist,album,year,comment,genre';
107 }
108 return $table;
109 }
110  
111 sub get_mp3_tag {
112 my($file) = shift;
113 my $adf = AnyData::Storage::File->new;
114 my(undef,$fh,undef) = $adf->open_local_file($file,'r');
115 local $/ = '';
116 $fh->seek(-128,2);
117 my $str = <$fh> || '';
118 $fh->close;
119 return undef if !($str =~ /^TAG/);
120 #$file = sprintf("%-255s",$file);
121 #$str =~ s/^TAG(.*)/$file$1/;
122 $str =~ s/^TAG(.*)/$1/;
123 my $genre = $str;
124 $genre =~ s/^.*(.)$/$1/g;
125 $str =~ s/(.)$//g;
126 $genre = unpack( 'C', $genre );
127 my @genres =("Blues", "Classic Rock", "Country", "Dance", "Disco", "Funk", "Grunge", "Hip-Hop", "Jazz", "Metal", "New Age", "Oldies", "Other", "Pop", "R&B", "Rap", "Reggae", "Rock", "Techno", "Industrial", "Alternative", "Ska", "Death Metal", "Pranks", "Soundtrack", "Eurotechno", "Ambient", "Trip-Hop", "Vocal", "Jazz+Funk", "Fusion", "Trance", "Classical", "Instrumental", "Acid", "House", "Game", "Sound Clip", "Gospel", "Noise", "Alternative Rock", "Bass", "Soul", "Punk", "Space", "Meditative", "Instrumental Pop", "Instrumental Rock", "Ethnic", "Gothic", "Darkwave", "Techno-Industrial", "Electronic", "Pop-Folk", "Eurodance", "Dream", "Southern Rock", "Comedy", "Cult", "Gangsta", "Top 40", "Christian Rap", "Pop/Funk", "Jungle", "Native American", "Cabaret", "New Wave", "Psychadelic", "Rave", "Show Tunes", "Trailer", "Lo-Fi", "Tribal", "Acid Punk", "Acid Jazz", "Polka", "Retro", "Musical", "Rock & Roll", "Hard Rock", "Folk", "Folk/Rock", "National Folk", "Swing", "Fast-Fusion", "Bebop", "Latin", "Revival", "Celtic", "Bluegrass", "Avantgarde", "Gothic Rock", "Progressive Rock", "Psychedelic Rock", "Symphonic Rock", "Slow Rock", "Big Band", "Chorus", "Easy Listening", "Acoustic", "Humour", "Speech", "Chanson", "Opera", "Chamber Music", "Sonata", "Symphony", "Booty Bass", "Primus", "Porn Groove", "Satire", "Slow Jam", "Club", "Tango", "Samba", "Folklore", "Ballad", "Power Ballad", "Rhytmic Soul", "Freestyle", "Duet", "Punk Rock", "Drum Solo", "Acapella", "Euro-House", "Dance Hall", "Goa", "Drum & Bass", "Club-House", "Hardcore", "Terror", "Indie", "BritPop", "Negerpunk", "Polsk Punk", "Beat", "Christian Gangsta Rap", "Heavy Metal", "Black Metal", "Crossover", "Contemporary Christian", "Christian Rock", "Unknown");
128 $genre = $genres[$genre] || '';
129 my @cols = unpack 'A30 A30 A30 A4 A30', $str;
130 my $comment = pop @cols;
131 #print $comment;
132 @cols = map{$_ || ''} @cols;
133 push @cols, $genre;
134 return \@cols;
135 }
136 1;