finish refactoring AudioFile
[id3fs.git] / lib / ID3FS / AudioFile / Ogg.pm
1 package ID3FS::AudioFile::Ogg;
2
3 use strict;
4 use warnings;
5 use Ogg::Vorbis::Header;
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->{ogg}=Ogg::Vorbis::Header->new($self->{path});
16     $self->{comments}=[ $self->{ogg}->comment_tags() ];
17     return $self;
18 }
19
20 sub get
21 {
22     my ($self, $tag, $complain)=@_;
23     for my $commenttype (@{$self->{comments}})
24     {
25         if($commenttype =~ /$tag/i)
26         {
27             my @comments=$self->{ogg}->comment($commenttype);
28             if(@comments)
29             {
30                 # take first comment with actual contents
31                 while(my $comment=shift @comments)
32                 {
33                     if(defined($comment) &&
34                        length($comment)  &&
35                        $comment =~ /\S+/)
36                     {
37                         $comment =~ s/\//-/g; # drop slashes
38                         return $comment;
39                     }
40                 }
41             }
42         }
43     }
44     warn("$self->{path}: no $tag defined in Ogg comments\n") if($complain);
45     return undef;
46 }
47
48 sub artist    { shift->get("Artist", 1); }
49 sub album     { shift->get("Album", 1);  }
50 sub audiotype { return "ogg";            }
51 sub haspic    { return undef;            } # FIXME
52 sub v1genre   { return undef;            } # ID3 only
53 # We don't care if year is not set
54 sub year      { shift->get("Date", 0);   }
55
56 sub tags
57 {
58     my $self=shift;
59     my @comments;
60     for my $commenttype (@{$self->{comments}})
61     {
62         if($commenttype =~ /genre/i)
63         {
64             push(@comments,$self->{ogg}->comment($commenttype));
65         }
66     }
67     return(@comments);
68 }
69
70 1;
71