~ubuntu-branches/ubuntu/utopic/cdrdao/utopic

« back to all changes in this revision

Viewing changes to contrib/mp32dao/mp3handler.pm

  • Committer: Bazaar Package Importer
  • Author(s): Andrew Suffield
  • Date: 2004-06-24 22:33:16 UTC
  • Revision ID: james.westby@ubuntu.com-20040624223316-534onzugaeeyq61j
Tags: upstream-1.1.9
ImportĀ upstreamĀ versionĀ 1.1.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# This program is free software; you can redistribute it and/or modify
 
3
# it under the terms of the GNU General Public License as published by
 
4
# the Free Software Foundation; either version 2 of the License, or
 
5
# (at your option) any later version.
 
6
#
 
7
# This program is distributed in the hope that it will be useful,
 
8
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
# GNU Library General Public License for more details.
 
11
#
 
12
# You should have received a copy of the GNU General Public License
 
13
# along with this program; if not, write to the Free Software
 
14
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
15
#
 
16
#Copyright 2001 Giuseppe Corbelli - cowo@lugbs.linux.it
 
17
#
 
18
# This package is inherited by Mediahandler and provides mp3 info access
 
19
# and decoding. See the attached UML diagram for access methods.
 
20
# Everything should be done using methods.
 
21
 
 
22
package mp3handler;
 
23
require BaseInfo;
 
24
@ISA = qw(BaseInfo);
 
25
use MP3::Info; 
 
26
#use Data::Dumper; 
 
27
use strict; 
 
28
use File::Basename;
 
29
use vars qw($AUTOLOAD);
 
30
 
 
31
# 2 Args
 
32
# 1: self name
 
33
# 2: scalar containing filename
 
34
sub new {
 
35
        my ($proto) = shift; 
 
36
        my ($class) = ref($proto)||$proto;
 
37
 
 
38
        #Inherit from BaseInfo
 
39
        my $self = $class->SUPER::new();        
 
40
        #Checks on input file
 
41
        $self->Filename(shift);
 
42
        if (! $self->Filename)  {
 
43
                $self->Error("No input file defined.");
 
44
                return -1;
 
45
        }
 
46
        if (! -r $self->Filename)       {
 
47
                $self->Error("Input file is not readable");
 
48
                return -1;
 
49
        }
 
50
        if (-z $self->Filename) {
 
51
                $self->Error("Input file has 0 size");
 
52
                return -1;
 
53
        }
 
54
        #Identify available mp3 decoder
 
55
        foreach my $dir (split (/\:/, $ENV{'PATH'}))    {
 
56
                if ( (-x "$dir/lame") && (-s "$dir/lame") )     {
 
57
                        $self->decoder_type('lame');
 
58
                        $self->decoder("$dir/lame");
 
59
                        last;
 
60
                }
 
61
                if ( (-x "$dir/mpg321") && (-s "$dir/mpg321") ) {
 
62
                        $self->decoder_type('mpg321');
 
63
                        $self->decoder("$dir/mpg321");
 
64
                        last;
 
65
                }
 
66
                if ( (-x "$dir/mpg123") && (-s "$dir/mpg123") ) {
 
67
                        $self->decoder_type('mpg123');
 
68
                        $self->decoder("$dir/mpg123");
 
69
                        last;
 
70
                }
 
71
        }
 
72
        if ( !$self->decoder )  {
 
73
                $self->Error("Cannot find any of the supported mp3 decoders in \$PATH.");
 
74
                return -1;
 
75
        }
 
76
        
 
77
        my $ID3tagref = MP3::Info::get_mp3tag ($self->Filename);
 
78
        my $MP3info = MP3::Info::get_mp3info ($self->Filename);
 
79
        
 
80
        #First try the lowercase version, then the first char uppercase, last the all-uppercase
 
81
        foreach my $n (qw(artist album title bitrate year comment genre mm ss secs frequency))  {
 
82
                if ($ID3tagref->{$n})   {
 
83
                        $self->$n ($ID3tagref->{"$n"});
 
84
                }       elsif   ($ID3tagref->{"\u$n"})  {
 
85
                        $self->$n ($ID3tagref->{"\u$n"});
 
86
                }       elsif   ($ID3tagref->{"\U$n"})  {
 
87
                        $self->$n ($ID3tagref->{"\U$n"});
 
88
                }       else    {
 
89
                        $self->$n ('');
 
90
                }
 
91
        }
 
92
        $MP3info->{"STEREO"} ? $self->channels(2) : $self->channels(1);
 
93
        $self->debug(1);
 
94
        bless ($self, $class);
 
95
        return $self;
 
96
}
 
97
 
 
98
sub type        {
 
99
        return "mp3";
 
100
}
 
101
 
 
102
#Decodes the file in mp3handler instance to the file specified as Outputfile in same instance
 
103
#Use system() and external decoder to do the work. Return value is external tool's one.
 
104
sub to_wav      {
 
105
        my ($self) = shift;
 
106
        my (@temp, $cmdline);
 
107
        if (! -w dirname($self->Filename) )     {
 
108
                $self->Error("Output directory is not writable");
 
109
                return -1;
 
110
        }
 
111
        printf ("\nDecoding file %s", $self->Filename) if ($self->debug);
 
112
        if (!$self->Outfile)    {
 
113
                $_ = $self->Filename; s/\.mp3/\.wav/i;
 
114
                $self->Outfile ($_);
 
115
                print ("\n\tNo outputfile defined. Used ".$self->Outfile."\n") if ($self->debug);
 
116
        } else  {
 
117
                printf (" to file %s\n", $self->Outfile) if ($self->debug);
 
118
        }
 
119
        if ( (-e $self->Outfile) && (-s $self->Outfile) && ($self->debug) )     {
 
120
                print $self->Outfile." exists, skipping mp3 decode\n" if ($self->debug);
 
121
                return 0;
 
122
        }
 
123
        if ($self->decoder_type =~ /lame/i)     {
 
124
                $cmdline = $self->decoder." --decode --mp3input -S ".quotemeta ($self->Filename)." ".quotemeta ($self->Outfile);
 
125
        }       elsif ($self->decoder_type =~ /mpg321/i)        {
 
126
                $cmdline = $self->decoder." -q -w ".quotemeta ($self->Outfile)." ".quotemeta ($self->Filename);
 
127
        }       else    {
 
128
                $cmdline = $self->decoder." -q -s ".quotemeta ($self->Filename)." \> ".quotemeta ($self->Outfile);
 
129
        }
 
130
        system ("".$cmdline);
 
131
        return $? >> 8;
 
132
}
 
133
 
 
134
#No args
 
135
#Returns 0
 
136
sub print_file_info     {
 
137
        my ($self) = @_;
 
138
        printf ("\nFilename : %s, type %s\n", $self->Filename, $self->type);
 
139
        printf ("\tArtist name: %s\t", $self->artist);
 
140
        printf ("Album name: %s\t", $self->album);
 
141
        printf ("Song title: %s\n", $self->title);
 
142
        printf ("\tYear: %s\t", $self->year);
 
143
        printf ("Song comment: %s\t", $self->comment);
 
144
        printf ("Genre: %s\n", $self->genre);
 
145
        printf ("\tDuration: %d min and %d sec\t", $self->durationMM, $self->durationSS);
 
146
        printf ("Average Bitrate: %d kb/s\n", $self->Avgbr);
 
147
        printf ("\t%d channel(s), %d HZ", $self->channels, ($self->frequency) * 1000);
 
148
        return 0;
 
149
}
 
150
 
 
151
#Arguments
 
152
#       $trackno: Track number
 
153
# Optional: $filename: filename of .inf file
 
154
sub write_inf   {
 
155
        my ($self) = shift;
 
156
        my ($trackno) = shift;
 
157
        my ($inffilename);
 
158
        if (@_) {
 
159
                $inffilename = shift;
 
160
        }       else    {
 
161
            $inffilename = $self->Filename;
 
162
                $inffilename =~ s/mp3/inf/i;
 
163
        }
 
164
    my ($inffilehandle);
 
165
    open ($inffilehandle, ">$inffilename");
 
166
    if (!$inffilehandle)        {last};
 
167
    my (@tl) = localtime (time ());
 
168
    $_ = sprintf ("#Created by mp32dao.pl on %02d/%02d/%d %02d:%02d:%02d\n", 
 
169
        $tl[3], $tl[4]+1, $tl[5]+1900, $tl[2], $tl[1], $tl[0]);
 
170
    print $inffilehandle $_;
 
171
    print $inffilehandle "#Source file is ".$self->Filename."\n";
 
172
    print $inffilehandle "#Report bugs to Giuseppe \"Cowo\" Corbelli <cowo\@lugbs.linux.it>\n\n";
 
173
    print $inffilehandle "Performer=\t'".$self->Artist."'\n";
 
174
    print $inffilehandle "Tracktitle=\t'".$self->Title."'\n";
 
175
    print $inffilehandle "Albumtitle=\t'".$self->Album."'\n";
 
176
    print $inffilehandle "Tracknumber=\t".($trackno)."\n";
 
177
    close ($inffilehandle);
 
178
}
 
179
 
 
180
1;