only complain about missing albums if $verbose, complain about artists always
[id3fs.git] / lib / ID3FS / AudioFile / Mp3.pm
1 package ID3FS::AudioFile::Mp3;
2
3 use strict;
4 use warnings;
5 use MP3::Tag;
6
7 sub new
8 {
9     my $proto=shift;
10     my $class=ref($proto) || $proto;
11     my $self={};
12     bless($self,$class);
13
14     $self->{path}=shift;
15     $self->{mp3}=MP3::Tag->new($self->{path});
16     $self->get_tags();
17     $self->{v1}=undef;
18     $self->{v1}=$self->{mp3}->{ID3v1} if(exists($self->{mp3}->{ID3v1}));
19     $self->{v2}=undef;
20     $self->{v2}=$self->{mp3}->{ID3v2} if(exists($self->{mp3}->{ID3v2}));
21
22     $self->{tags}={};
23
24     return $self;
25 }
26
27 sub choose
28 {
29     my ($self, $func)=@_;
30     my $thing=undef;
31     if(defined($self->{v2}))
32     {
33         $thing=$self->{v2}->$func();
34     }
35     if(defined($self->{v1}) && (!defined($thing) || !length($thing)))
36     {
37         $thing=$self->{v1}->$func();
38     }
39     return $thing;
40 }
41
42 sub artist    { shift->choose("artist"); }
43 sub album     { shift->choose("album");  }
44 # We don't care if year is not set
45 sub year      { shift->choose("year");   }
46 sub audiotype { return "mp3";            }
47 sub haspic    { return undef;            } # FIXME
48 sub v1genre
49 {
50     my($self)=@_;
51     my $genre=undef;
52     $genre=$self->{v1}->genre() if(defined($self->{v1}));
53     return $genre;
54 }
55
56 sub tags
57 {
58     my $self=shift;
59     return() unless(exists($self->{mp3}->{ID3v2}) && defined($self->{mp3}->{ID3v2}));
60     return($self->{mp3}->{ID3v2}->genre());
61 }
62
63 sub get_tags
64 {
65     my ($self)=@_;
66     # MP3::Tag->get_tags shows cryptic debug info via print when it finds
67     # an unhandled id3v2 version, in addition to the warning, so use
68     # select to send prints to /dev/null
69     my $oldout=undef;
70     if(open(NULL,">/dev/null"))
71     {
72         $oldout=select(NULL);
73     }
74     eval { $self->{mp3}->get_tags; };
75     warn("$self->{path}: $@\n") if($@);
76     if(defined($oldout))
77     {
78         select($oldout);
79         close(NULL);
80     }
81 }
82
83 1;