c2e821fb6deac7c380693e894e17404ccbd12604
[id3fs.git] / lib / ID3FS / AudioFile / Flac.pm
1 package ID3FS::AudioFile::Flac;
2
3 use strict;
4 use warnings;
5 use Audio::FLAC::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     my $flac=Audio::FLAC::Header->new($self->{path});
16     $self->{tags}=$flac->tags();
17     return $self;
18 }
19
20 sub get
21 {
22     my ($self, $tag, $complain)=@_;
23     for my $key (keys %{$self->{tags}})
24     {
25         if($key =~ /$tag/i &&
26            defined($self->{tags}->{$key})  &&
27            length($self->{tags}->{$key})   &&
28            $self->{tags}->{$key} =~ /\S+/)
29         {
30             my $val=$self->{tags}->{$key};
31             $val =~ s/\//-/g; # drop slashes
32             return $val;
33         }
34     }
35     warn("$self->{path}: no $tag defined in FLAC comments\n") if($complain);
36     return undef;
37 }
38
39 sub artist    { shift->get("ARTIST", 1); }
40 sub album     { shift->get("ALBUM", 1);  }
41 sub audiotype { return "flac";           }
42 sub haspic    { return undef;            } # FIXME
43 sub v1genre   { return undef;            } # ID3 only
44
45 # We don't care if year is not set
46 sub year
47 {
48     my ($self)=@_;
49     my $date=shift->get("DATE", 0);
50     return undef unless($date);
51     if($date =~/(\d\d\d\d)/)
52     {
53         $date=$1;
54     }
55     return $date;
56 }
57
58 sub tags
59 {
60     my $self=shift;
61     my @tags=();
62     my $tags={};
63     for my $key (keys %{$self->{tags}})
64     {
65         if($key =~ /genre/i &&
66            defined($self->{tags}->{$key})  &&
67            length($self->{tags}->{$key})   &&
68            $self->{tags}->{$key} =~ /\S+/)
69         {
70             push(@tags, $self->{tags}->{$key});
71         }
72     }
73     # combine then split on commas
74     # so multiple comma-delimited tags will work
75     @tags=split(/\s*,\s*/, join(', ', @tags));
76     for my $tag (@tags)
77     {
78         if($tag=~/([^\/]+)\/(.*)/)
79         {
80             my $tagname=$1;
81             my $tagval=$2;
82             $tagval=~s/\//-/g;
83             $self->{tags}->{$tagname}=$tagval;
84         }
85         else
86         {
87             $self->{tags}->{$tag}=undef;
88         }
89     }
90     return $self->{tags};
91 }
92
93 1;
94