blib/lib/MP3/Mplib.pm

filter: Statementstmt 77.8% 81 / 104 Branchbran 53.5% 15 / 28 Conditioncond 34.2% 12 / 35 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 78.2% 18 / 23 Podpod 100% 14 / 14 totaltotal 62.7% 140 / 223 CC 28 SCAR 16.3
CC
28
Cov
65%
add_to_v1tag
20.0
get_v2header
12.0
header
6.0
CRAP 5.1
↑ Summary Mplib.xs »
1 package MP3::Mplib;
2  
3 5 ▶use 5.005003;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use Errno;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 5 require Exporter;
9 5 require DynaLoader;
10 5 ▶use AutoLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 5 ▶use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $VERSION $AUTOLOAD);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 5 @ISA = qw(Exporter DynaLoader);
15  
16 # Items to export into callers namespace by default. Note: do not export
17 # names by default without a very good reason. Use EXPORT_OK instead.
18 # Do not simply export all your public functions/methods/constants.
19  
20 # This allows declaration use MP3::Mplib ':all';
21 # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
22 # will save memory.
23  
24 5 @EXPORT = qw(
25 MP_EERROR
26 MP_EFCOMPR
27 MP_EFENCR
28 MP_EFNF
29 MP_EVERSION
30 ISO_8859_1
31 UTF16
32 UTF16BE
33 UTF8
34 );
35  
36 5 %EXPORT_TAGS = (
37 'constants' => [ qw(
38 MP_ALBUM
39 MP_ARTIST
40 MP_COMMENT
41 MP_EERROR
42 MP_EFCOMPR
43 MP_EFENCR
44 MP_EFNF
45 MP_EVERSION
46 MP_GENRE
47 MP_TITLE
48 MP_TRACK
49 MP_YEAR), @EXPORT ],
50 'functions' => [ qw(
51 get_header
52 get_tag
53 get_id3v2_header
54 set_tag
55 delete_tags
56 clean_up
57 dump_structure), @EXPORT ],
58 );
59
60 5 $EXPORT_TAGS{ 'all' } = [ @{ $EXPORT_TAGS{'constants'} },
61 5 @{ $EXPORT_TAGS{'functions'} },
62 @EXPORT ];
63  
64 5 @EXPORT_OK = ( @{ $EXPORT_TAGS{'constants'} },
65 5 @{ $EXPORT_TAGS{'functions'} } );
66  
67  
68 5 $VERSION = '0.02';
69  
70 24 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=5 SCAR=16.2
71 # This AUTOLOAD is used to 'autoload' constants from the constant()
72 # XS function. If a constant is not found then control is passed
73 # to the AUTOLOAD in AutoLoader.
74  
75 24 my $constname;
76 24 ($constname = $AUTOLOAD) =~ s/.*:://;
77 24 ▶ croak "& not defined" if $constname eq 'constant';
Branch: 1 branch
BranchTrueFalseTotal
if $constname eq "constant" 0 24 24
78 24 ▶ my $val = constant($constname, @_ ? $_[0] : 0);
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 6 18 24
79 24 ▶ if ($! != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($! != 0) 4 20 24
80 4 ▶ if ($!{EINVAL}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($!{'EINVAL'}) { } 4 0 4
81 4 $AutoLoader::AUTOLOAD = $AUTOLOAD;
82 4 goto &AutoLoader::AUTOLOAD;
83 }
84 else {
85 0 croak "Your vendor has not defined MP3::Mplib macro $constname";
86 }
87 }
88
89 5 ▶ { no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
90 20 ▶ *$AUTOLOAD = sub { $val };
Subroutine __ANON__: called CC=1 SCAR=0.0
91 }
92 20 goto &$AUTOLOAD;
93 }
94  
95 5 bootstrap MP3::Mplib $VERSION;
96  
97 # Preloaded methods go here.
98  
99 # Autoload methods go after =cut, and are processed by the autosplit program.
100  
101 =head1 NAME
102  
103 MP3::Mplib - Speedy access to id3v1 and id3v2 tags
104  
105 =head1 SYNOPSIS
106  
107 use MP3::Mplib;
108  
109 my $mp3 = MP3::Mplib->new("/path/to/file.mp3");
110 my $v1tag = $mp3->get_v1tag;
111 my $v2tag = $mp3->get_v2tag;
112
113 while (my ($key, $val) = each %$v1tag) {
114 print "$key: $val\n";
115 }
116
117 while (my ($key, $val) = each %$v2tag) {
118 ...
119 }
120  
121 $mp3->add_to_v2tag( { TYER => 2002 } );
122  
123 =head1 DESCRIPTION
124  
125 MP3::Mplib is a wrapper around Stefan Podkowinski's mplib written in C. It combines the best of both worlds: C's speed and Perl's nice object-orientedness. Note that B<MP3::Mplib> ships with its own version of mplib (currently 1.0.1).
126  
127 There is no sophistaced class hierarchy. You simply create a B<MP3::Mplib> object. Each method either returns a native Perl data-structure or takes one. Bang. That's it.
128  
129 =head1 METHODS
130  
131 =over 4
132  
133 =item B<new(file)>
134  
135 Constructor that takes a filename as only argument.
136  
137 my $mp3_object = MP3::Mplib->new("file.mp3");
138  
139 =back
140  
141 =cut
142  
143 5 ▶sub new {
Subroutine new: called CC=2 SCAR=7.4
Pod documented
144 5 my ($class, $file) = @_;
145 5 ▶ $class = ref($class) || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
050
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
146 5 my $self = { _mp_file => $file,
147 _mp_id3v1 => undef,
148 _mp_id3v2 => undef,
149 _mp_header => undef,
150 _mp_v2header => undef,};
151 5 bless $self => $class;
152 }
153  
154 =pod
155  
156 =over 4
157  
158 =item B<header>
159  
160 Returns a hash-ref to the mpeg-header.
161  
162 my $mpeg_header = $mp3->header;
163 print $mpeg_header->{bitrate}, "\n";
164 ...
165  
166 The hash-ref contains the following fields:
167  
168 =over 8
169  
170 =item * I<syncword> (integer)
171  
172 =item * I<version> (string)
173  
174 =item * I<layer> (string)
175  
176 =item * I<protbit> (boolean)
177  
178 =item * I<bitrate> (string)
179  
180 =item * I<samplingfreq> (string)
181  
182 =item * I<padbit> (boolean)
183  
184 =item * I<privbit> (boolean)
185  
186 =item * I<mode> (string)
187  
188 =item * I<mode_ext> (boolean)
189  
190 =item * I<copyright> (boolean)
191  
192 =item * I<originalhome> (boolean)
193  
194 =item * I<emphasis> (boolean)
195  
196 =back
197  
198 =back
199  
200 Z<>
201  
202 =cut
203  
204 0 ▶sub header {
Subroutine header: not called CC=2 SCAR=17.9
Pod documented
205 0 my $self = shift;
206 0 ▶ return ($self->{_mp_header} ||= get_header($self->{_mp_file}));
Condition0%0/3
$self->{"_mp_header"} ||= &get_header($self->{"_mp_file"})
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"_mp_header"} ||= &get_header($self->{"_mp_file"})
$self->{"_mp_header"} &get_header($self->{"_mp_file"})
Truth table0%0/3
A ||= B
A= $self->{"_mp_header"}B= &get_header($self->{"_mp_file"})
ABresult
1X1
011
000
207 }
208  
209 =pod
210  
211 =over 4
212  
213 =item B<get_v1tag>
214  
215 Returns the id3v1 tag as a hash-ref.
216  
217 my $tag = $mp3->get_v1tag;
218 print $tag->{title}, "\n";
219  
220 The hash-ref contains the following fields:
221  
222 =over 8
223  
224 =item * I<artist>
225  
226 =item * I<title>
227  
228 =item * I<album>
229  
230 =item * I<genre>
231  
232 =item * I<track>
233  
234 =item * I<year>
235  
236 =item * I<comment>
237  
238 =back
239  
240 =back
241  
242 Z<>
243  
244 =cut
245  
246 4 ▶sub get_v1tag {
Subroutine get_v1tag: called CC=3 SCAR=11.7
Pod documented
247 4 my $self = shift;
248 4 ▶ return ($self->{_mp_id3v1} ||= get_tag($self->{_mp_file}, 1)
Condition60%3/5
$self->{"_mp_id3v1"} ||= &get_tag($self->{"_mp_file"}, 1) || {}
l!l&&r!l&&!r
040
&get_tag($self->{'_mp_file'}, 1) || {}
l!l
31
MC/DC0%0/2
$self->{"_mp_id3v1"} ||= &get_tag($self->{"_mp_file"}, 1) || {}
$self->{"_mp_id3v1"} &get_tag($self->{'_mp_file'}, 1)
Truth table33%1/3
A ||= B
A= $self->{"_mp_id3v1"}B= &get_tag($self->{'_mp_file'}, 1)
ABresult
1X1
011
000
249 || { } );
250 }
251  
252 =pod
253  
254 =over 4
255  
256 =item B<get_v2tag>
257  
258 Returns the id3v2 tag as a hash-ref.
259  
260 my $tag = $mp3->get_v2tag;
261 print $tag->{TIT1}, "\n";
262  
263 The hash-ref returned can contain up to 74 fields. All but the "COMM" and "WXXX" field have a single value if present.
264  
265 The "COMM" field is the id3v2-equivalent of the 'comment' tag and contains three sub-categories:
266  
267 =over 8
268  
269 =item * I<text>
270  
271 The actual comment as an arbitrarily long text.
272  
273 =item * I<short>
274  
275 A short description of the comment.
276  
277 =item * I<lang>
278  
279 The language of the comment as 3-byte string, e.g. "ENG" for English or "GER" for German.
280  
281 =back
282  
283 The "WXXX" field looks like this:
284  
285 =over 8
286  
287 =item * I<url>
288  
289 The URL that is referenced.
290  
291 =item * I<description>
292  
293 An additional description.
294  
295 =back
296  
297 =back
298  
299 Example:
300  
301 my $tag = $mp3->get_v2tag;
302 if (exists $tag->{COMM}) {
303 print "Language: $tag->{COMM}->{lang}\n";
304 print "Short: $tag->{COMM}->{short}\n";
305 print "Text: $tag->{COMM}->{text}\n";
306 }
307  
308 =cut
309  
310 5 ▶sub get_v2tag {
Subroutine get_v2tag: called CC=3 SCAR=11.1
Pod documented
311 5 my $self = shift;
312 5 ▶ return ($self->{_mp_id3v2} ||= get_tag($self->{_mp_file}, 2)
Condition80%4/5
$self->{"_mp_id3v2"} ||= &get_tag($self->{"_mp_file"}, 2) || {}
l!l&&r!l&&!r
140
&get_tag($self->{'_mp_file'}, 2) || {}
l!l
31
MC/DC0%0/2
$self->{"_mp_id3v2"} ||= &get_tag($self->{"_mp_file"}, 2) || {}
$self->{"_mp_id3v2"} &get_tag($self->{'_mp_file'}, 2)
Truth table66%2/3
A ||= B
A= $self->{"_mp_id3v2"}B= &get_tag($self->{'_mp_file'}, 2)
ABresult
1X1
011
000
313 || { } );
314 }
315  
316 =pod
317  
318 =over 4
319  
320 =item B<get_v2header>
321  
322 Returns a hash-ref to the id3v2 header. This can contain the following fields:
323  
324 =over 8
325  
326 =item * I<ver_minor>
327  
328 The 'x' as in 2.x.1
329  
330 =item * I<ver_revision>
331  
332 The 'x' as in 2.3.x
333  
334 =item * I<unsync>
335  
336 =item * I<experimental>
337  
338 =item * I<footer>
339  
340 =item * I<total_tag_size>
341  
342 =item * I<extended_header>
343  
344 A reference to the extended header if present.
345  
346 =back
347  
348 The extended header has the following fields:
349  
350 =over 8
351  
352 =item * I<size>
353  
354 =item * I<flag_bytes>
355  
356 =item * I<no_flag_bytes>
357  
358 =item * I<is_update>
359  
360 =item * I<crc_data_present>
361  
362 =item * I<crc_data_length>
363  
364 =item * I<crc_data>
365  
366 =item * I<restrictions>
367  
368 =item * I<restrictions_data_length>
369  
370 =item * I<restrictions_item_data>
371  
372 =back
373  
374 Since the extended header field only exists when the MP3 file has such a header, check for existance before accessing it:
375  
376 my $header = $mp3->get_v2header;
377 if (exists $header->{extended_header}) {
378 print $header->{extended_header}->{size}, "\n";
379 ...
380 }
381  
382 =back
383  
384 =cut
385  
386 0 ▶sub get_v2header {
Subroutine get_v2header: not called CC=3 SCAR=24.8
Pod documented
387 0 my $self = shift;
388 return ($self->{_mp_v2header} ||= get_id3v2_header($self->{_mp_file})
389 0 ▶ || { } );
Condition0%0/5
$self->{"_mp_v2header"} ||= &get_id3v2_header($self->{"_mp_file"}) || {}
l!l&&r!l&&!r
000
&get_id3v2_header($self->{'_mp_file'}) || {}
l!l
00
MC/DC0%0/2
$self->{"_mp_v2header"} ||= &get_id3v2_header($self->{"_mp_file"}) || {}
$self->{"_mp_v2header"} &get_id3v2_header($self->{'_mp_file'})
Truth table0%0/3
A ||= B
A= $self->{"_mp_v2header"}B= &get_id3v2_header($self->{'_mp_file'})
ABresult
1X1
011
000
390 }
391  
392 =pod
393  
394 =over 4
395  
396 =item B<set_v1tag(tag, [encoding])>
397  
398 Sets the id3v1-tag. If there was no such tag previously, it is created. An existing one is replaced.
399  
400 It takes one obligatory argument, namely a reference to a hash. This hash has the keys described in C<get_v1tag()>.
401  
402 'encoding' is an optional argument that determines the character encoding used for the data. It is either of the constants ISO_8859_1, UTF16, UTF16BE or UTF8. If none is given, ISO_8859_1 is used.
403  
404 $mp3->set_v1tag( { title => 'Help',
405 artist => 'The Beatles',
406 track => 1,
407 year => 1966,
408 genre => 'Oldies',
409 comment => 'A Beatles-song', }, &UTF16 );
410  
411 Returns a true value on success, false otherwise. In this case, check C<$mp3-E<gt>error>.
412  
413 =back
414  
415 =cut
416
417 3 ▶sub set_v1tag {
Subroutine set_v1tag: called CC=4 SCAR=14.3
Pod documented
418 3 my ($self, $tag, $enc) = @_;
419
420 3 ▶ croak "MP3::Mplib::set_v1tag expects a hash-ref as first argument"
Branch: 1 branch
BranchTrueFalseTotal
if ref $tag ne "HASH" 0 3 3
421 if ref $tag ne 'HASH';
422
423 3 ▶ if (set_tag($self->{_mp_file}, 1, $tag, $enc || &ISO_8859_1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&set_tag($self->{"_mp_file"}, 1, $tag, $enc || &ISO_8859_1)) 1 2 3
Condition33%1/3
$enc || &ISO_8859_1
l!l&&r!l&&!r
003
MC/DC0%0/2
$enc || &ISO_8859_1
$enc &ISO_8859_1
Truth table33%1/3
A || B
A= $encB= &ISO_8859_1
ABresult
1X1
011
000
424 # forces re-parsing on next get_v1header
425 1 undef $self->{_mp_id3v1};
426 1 return 1;
427 }
428 2 return;
429 }
430  
431 =pod
432  
433 =over 4
434  
435 =item B<add_to_v1tag(tag, [encoding])>
436  
437 Adds (or rather merges) 'tag' with the currently existing id3v1-tag. Existing fields are replaced. Use this to add a field to an already existing tag. 'tag' is meant to be a hash-reference:
438  
439 $mp3->add_to_v1tag( { TITLE => 'some title',
440 ALBUM => 'some album', } );
441  
442 Returns a true value on success, false otherwise in which case you should check C<$mp3-E<gt>error>.
443  
444 =back
445  
446 =cut
447  
448 0 ▶sub add_to_v1tag {
Subroutine add_to_v1tag: not called CC=4 SCAR=30.0
Pod documented
449 0 my ($self, $tag, $enc) = @_;
450
451 0 ▶ croak "MP3::Mplib::add_to_v1tag expects a hash-ref as first argument"
Branch: 1 branch
BranchTrueFalseTotal
if ref $tag ne "HASH" 0 0 0
452 if ref $tag ne 'HASH';
453
454 0 my %old_tag = %{ $self->get_v1tag };
455  
456 0 ▶ if (set_tag($self->{_mp_file}, 1,
Branch: 1 branch
BranchTrueFalseTotal
if (&set_tag($self->{"_mp_file"}, 1, {%old_tag, %$tag}, $enc || &ISO_8859_1)) 0 0 0
Condition0%0/3
$enc || &ISO_8859_1
l!l&&r!l&&!r
000
MC/DC0%0/2
$enc || &ISO_8859_1
$enc &ISO_8859_1
Truth table0%0/3
A || B
A= $encB= &ISO_8859_1
ABresult
1X1
011
000
457 { %old_tag, %$tag }, $enc || &ISO_8859_1)) {
458 0 undef $self->{_mp_id3v1};
459 0 return 1;
460 }
461 0 return;
462 }
463
464 =pod
465  
466 =over 4
467  
468 =item B<set_v2tag(tag, [encoding])>
469  
470 Sets the id3v2 tag. This has the same semantics as C<set_v1tag>, so read its description first.
471  
472 It differs in that id3v2 tags have different field names. They are always four ccharacter long uppercased strings. Since there are 74 of them, their explanation is not included in here. Please see the specifications at B<http://www.id3.org/>.
473  
474 A number of fields are special in that they don't just take one value. You can pass a hash-ref to them instead:
475  
476 =over 8
477  
478 =item * I<COMM> [comment field]
479  
480 Possible fields are I<'text'>, I<'short'> and I<'lang'>. I<'lang'> is always a three character upper-case string. When just an ordinary string is passed, this will become I<'text'>. I<'short'> will be left empty in this case and I<'lang'> is set to 'ENG'.
481  
482 =item * I<WXXX> [used defined link frame]
483  
484 Possible fields are I<'url'> and I<'description'>. The content of I<'url'> has to be a ISO-8859-1 encoded string regardless of which 'encoding' you passed to C<set_v2tag>. When just an ordinary string is passed, this will become I<'url'>. I<'description'> will remain empty in this case.
485  
486 =back
487  
488 Further examples:
489  
490 my $tag = { TIT2 => 'Help',
491 COMM => { lang => 'ENG',
492 text => 'Long comment ...',
493 short => 'short description', },
494 WXXX => 'http://www.beatles.com',
495 TEXT => 'Paul McCartney', # the text writer
496 TPE1 => 'Paul McCartney', # the lead performer
497 TPE2 => 'The Beatles', # the band
498 ..., };
499 $mp3->set_v2header($tag);
500  
501 In the above, the I<'WXXX'> could have also looked like this:
502  
503 WXXX => { url => 'http://www.beatles.com',
504 description => 'a webpage', },
505  
506 or somesuch.
507  
508 This method has the same return values as C<set_v1tag>.
509  
510 =back
511  
512 =cut
513  
514 1 ▶sub set_v2tag {
Subroutine set_v2tag: called CC=4 SCAR=15.9
Pod documented
515 1 my ($self, $tag, $enc) = @_;
516
517 1 ▶ croak "MP3::Mplib::set_v2tag expects a hash-ref as first argument"
Branch: 1 branch
BranchTrueFalseTotal
if ref $tag ne "HASH" 0 1 1
518 if ref $tag ne 'HASH';
519
520 1 ▶ if (set_tag($self->{_mp_file}, 2, $tag, $enc || &ISO_8859_1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&set_tag($self->{"_mp_file"}, 2, $tag, $enc || &ISO_8859_1)) 1 0 1
Condition33%1/3
$enc || &ISO_8859_1
l!l&&r!l&&!r
001
MC/DC0%0/2
$enc || &ISO_8859_1
$enc &ISO_8859_1
Truth table33%1/3
A || B
A= $encB= &ISO_8859_1
ABresult
1X1
011
000
521 # forces re-parsing on next get_v2header
522 1 undef $self->{_mp_id3v2};
523 1 return 1;
524 }
525 0 return;
526 }
527  
528 =pod
529  
530 =over 4
531  
532 =item B<add_to_v2tag>
533  
534 The id3v2 equivalent to C<add_to_v1tag>. It has the same semantics, both regarding the arguments it gets as well as its return value.
535  
536 =back
537  
538 =cut
539  
540 1 ▶sub add_to_v2tag {
Subroutine add_to_v2tag: called CC=4 SCAR=15.2
Pod documented
541 1 my ($self, $tag, $enc) = @_;
542 1 ▶ croak "MP3::Mplib::add_to_v2tag expects a hash-ref as first argument"
Branch: 1 branch
BranchTrueFalseTotal
if ref $tag ne "HASH" 0 1 1
543 if ref $tag ne 'HASH';
544  
545 1 my %oldtag = %{ $self->get_v2tag };
546  
547 1 ▶ if (set_tag($self->{_mp_file}, 2,
Branch: 1 branch
BranchTrueFalseTotal
if (&set_tag($self->{"_mp_file"}, 2, {%oldtag, %$tag}, $enc || &ISO_8859_1)) 1 0 1
Condition33%1/3
$enc || &ISO_8859_1
l!l&&r!l&&!r
001
MC/DC0%0/2
$enc || &ISO_8859_1
$enc &ISO_8859_1
Truth table33%1/3
A || B
A= $encB= &ISO_8859_1
ABresult
1X1
011
000
548 { %oldtag, %$tag }, $enc || &ISO_8859_1)) {
549 1 undef $self->{_mp_id3v2};
550 1 return 1;
551 }
552 0 return;
553 }
554
555
556 =pod
557  
558 =over 4
559  
560 =item B<del_v1tag>
561  
562 Deletes the id3v2 tag from the file.
563  
564 =back
565  
566 =cut
567  
568 1 ▶sub del_v1tag {
Subroutine del_v1tag: called CC=2 SCAR=7.4
Pod documented
569 1 my $self = shift;
570 1 ▶ if (delete_tags($self->{_mp_file}, 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&delete_tags($self->{"_mp_file"}, 1)) 1 0 1
571 1 undef $self->{_mp_id3v1};
572 1 return 1;
573 }
574 0 return 0;
575 }
576  
577 =pod
578  
579 =over 4
580  
581 =item B<del_v2tag>
582  
583 Deletes the id3v2 tag. Correctly speaking, it deletes I<all> id3v2 tags in the file. But since B<MP3::Mplib> does not allow access to particular id3v2 tags this distinction should not matter.
584  
585 =back
586  
587 =cut
588  
589 1 ▶sub del_v2tag {
Subroutine del_v2tag: called CC=2 SCAR=7.4
Pod documented
590 1 my $self = shift;
591 1 ▶ if (delete_tags($self->{_mp_file}, 2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&delete_tags($self->{"_mp_file"}, 2)) 1 0 1
592 1 undef $self->{_mp_id3v2};
593 1 return 1;
594 }
595 0 return 0;
596 }
597  
598 =pod
599  
600 =over 4
601  
602 =item B<clean_up>
603  
604 Some MP3s may have a very extensive id3v2-tag with some fields showing up several times. If you don't like that, use this method to wipe out all but the first frame of each type.
605  
606 This method doesn't return anything meaningful since it never fails.
607  
608 =back
609  
610 =cut
611  
612 0 ▶sub clean_up {
Subroutine clean_up: not called CC=1 SCAR=6.9
Pod documented
613 0 my $self = shift;
614 0 _clean_up($self->{_mp_file});
615 0 return 1;
616 }
617  
618 =pod
619  
620 =over 4
621  
622 =item B<dump_structure>
623  
624 Mainly used as a debugging tool (or if you are just curious to peek at all the tags and fields present in an MP3), this method dumps the tag-structure of the MP3 to STDOUT.
625  
626 This method doesn't return anything meaningful since it never fails.
627  
628 =back
629  
630 =cut
631  
632 0 ▶sub dump_structure {
Subroutine dump_structure: not called CC=1 SCAR=6.9
Pod documented
633 0 my $self = shift;
634 0 _dump_structure($self->{_mp_file});
635 0 return 1;
636 }
637  
638 =pod
639  
640 =over 4
641  
642 =item B<error>
643  
644 As always, things can get wrong on certain operations. The statement that did not perform as expected always returns a false value in which case this functions returns a reference to a hash that you can inspect further. Keys of the hash are field names of an id3-tag while the corresponding values are set to an error-code (see L<MP3::Mplib/Error Codes>).
645  
646 There is a special key: I<mp_file> relates to the file rather than to an individual tag-field. This might be set when you try to write a new tag on a read-only file.
647  
648 Note that C<error> can be called either as instance-method, class-method or function:
649  
650 my %err = $mp3->error;
651 my %err = MP3::Mplib->error;
652 my %err = MP3::Mplib::error();
653  
654 It is not exported so you have to qualify it when calling it as function.
655  
656 =back
657  
658 =cut
659  
660 2 ▶sub error { return { split /\034/, $MP3::Mplib::Error || '' } }
Subroutine error: called CC=2 SCAR=7.6
Pod documented
Condition50%1/2
$MP3::Mplib::Error || ''
l!l
20
MC/DC0%0/1
$MP3::Mplib::Error || ''
$MP3::Mplib::Error
Truth table50%1/2
A
A= $MP3::Mplib::Error
Aresult
11
00
661  
662 =head1 EXPORTS
663  
664 =head2 Default exports
665  
666 B<MP3::Mplib> exports a couple of constants by default. If you don't want them, include the module in your scripts like this:
667  
668 use MP3::Mplib ();
669  
670 Please note that each of these constants has to be called as subroutine, either with empty trailing paranteses or a leading ampersand:
671  
672 my $c = ISO_8859_1; # NOT OK!
673  
674 # instead:
675
676 my $c = &ISO_8859_1;
677 # or
678 my $c = ISO_8859_1();
679  
680 =over 4
681  
682 =item B<Encodings>
683  
684 =over 8
685  
686 =item * I<ISO_8859_1>
687  
688 =item * I<UTF16>
689  
690 =item * I<UTF16BE>
691  
692 =item * I<UTF8>
693  
694 =back
695  
696 Z<>
697  
698 =item B<Error codes>
699  
700 =over 8
701  
702 =item * I<MP_EERROR>
703  
704 This is a non-specific error code that is - according to the mplib.h - returned under circumstances that cannot happen. :-)
705  
706 =item * I<MP_EFNF>
707  
708 The specified field does not exist in the tag.
709  
710 =item * I<MP_EFCOMPR>
711  
712 The value for this field has been compressed and can thus not be retrieved.
713  
714 =item * I<MP_EFENCR>
715  
716 The value for this field has been encrypted and can thus not be retrieved.
717  
718 =item * I<MP_EVERSION>
719  
720 Tag has a version set that is not supported by the library.
721  
722 =back
723  
724 =back
725  
726 =head2 Optional exports
727  
728 B<MP3::Mplib> has a couple of tags you can import:
729  
730 use MP3::Mplib qw(:constants)
731 use MP3::Mplib qw(:functions)
732 use MP3::Mplib qw(:all)
733  
734 =over 4
735  
736 =item B<:constants>
737  
738 This exports additional constants used for identifying id3v1 tag fields. These are integers so you B<cannot> use them as hash-keys for C<set_v1header> or get C<get_v1header>!
739  
740 =over 8
741  
742 =item * I<MP_ARTIST>
743  
744 =item * I<MP_TITLE>
745  
746 =item * I<MP_ALBUM>
747  
748 =item * I<MP_GENRE>
749  
750 =item * I<MP_COMMENT>
751  
752 =item * I<MP_YEAR>
753  
754 =item * I<MP_TRACK>
755  
756 =back
757  
758 Z<>
759  
760 =item B<:functions>
761  
762 Seven functions are exported by this tag. Use those when you want the functional interface:
763  
764 =over 8
765  
766 =item * I<get_header>
767  
768 =item * I<get_tag>
769  
770 =item * I<get_id3v2_header>
771  
772 =item * I<set_tag>
773  
774 =item * I<delete_tags>
775  
776 =item * I<clean_up>
777  
778 =item * I<dump_structure>
779  
780 =back
781  
782 For a description see L<FUNCTIONS>.
783  
784 =back
785  
786 =head1 FUNCTIONS
787  
788 When using the functional interface, you are using directly the functions defined through XS code. All of them have a prototype so that you can safely omit the parens when using them.
789  
790 =over 4
791  
792 =item B<get_header $file>
793  
794 Returns a hash-ref to the mpeg-header. Fields are described under C<header> in L<"METHODS">.
795  
796 =item B<get_tag $file, $version>
797  
798 Returns a hash-ref to the id3-tag for the given C<$file>. C<$version> must either be 1 or 2.
799  
800 Returns undef if no tag of the specified C<$version> has been found.
801  
802 =item B<get_id3v2_header $file>
803  
804 Returns a hash-ref to the id3v2-header for the given C<$file>. Fields are described under C<get_v2header> in L<"METHODS">.
805  
806 =item B<set_tag $file, $version, $tag, [$enc]>
807  
808 Sets the tag of C<$version> for C<$file> to C<$tag> which has to be a hash-reference. C<$enc> is optional but if given has to be one of the constants described under "Encodings" in L<"EXPORTS">. It defaults to C<ISO_8859_1>.
809  
810 Returns a true value on success, false otherwise. Check C<MP3::Mplib::error()> in this case.
811  
812 =item B<delete_tags $file, $version>
813  
814 Deletes all tags of C<$version> in C<$file>.
815  
816 Returns a true value on success false otherwise. Currently C<MP3::Mplib::error()> is B<not> set when this functions fails.
817  
818 =item B<clean_up $file>
819  
820 Cleans up the id3v2-tag of C<$file>. See C<clean_up> in L<"METHODS"> for details.
821  
822 =item B<dump_structure $file>
823  
824 Dumps the tag-structure of C<$file> to STDOUT. See C<dump_structure> in L<"METHODS"> for details.
825  
826 =back
827  
828 =head1 PLATFORMS
829  
830 =over 4
831  
832 =item B<Operating systems>
833  
834 My development environment is a Debian box. In theory I should have access to some Solaris machines in my university but forgot my login. That means it's currently just tested under Linux.
835  
836 I'd be grateful for anyone sending me remarks how well (or even at all) this module works on his/her machine. If someone is using ActiveState under Windows and did in fact install it succesfully I'd be even happier to hear about that. Perhaps this person would even be so kind to help me packing up a PPM package or even become the Windows-maintainer. Since I am lacking VisualC I am closed out here.
837  
838 =item B<Perl versions and compilers>
839  
840 I've tested it with the following compilers and Perl versions:
841  
842 =over 8
843  
844 =item * gcc 2.95.4 / Perl 5.005_03
845  
846 =item * gcc 2.95.4 / Perl 5.6.1
847  
848 =item * gcc 3.0.4 / Perl 5.8.0
849  
850 =back
851  
852 =back
853  
854 =head1 KNOWN BUGS
855  
856 The underlying C library is incomplete with respect to parsing id3v2 tags. This is ok with simple tags, like all the I<Txxx> frames. It is not so ok with more complicated ones, such as I<WXXX>. I contacted mplib's author with a request for adding missing functionality. The mplib's author said that they might be added at some other time.
857  
858 Currently, some missing functionality has been added by me (notably support for I<WXXX>).
859  
860 The current object-oriented interface could be considered a bug. There is only the C<MP3::Mplib> object and no separate objects for id3v1- or id3v2-tags. Adding those, however, would require additions to the XS-portion of this module. Read: A lot of additional work.
861  
862 =head1 TODO
863  
864 Well, see L<"KNOWN BUGS">.
865  
866 =over 4
867  
868 =item B<Missing functionality from mplib>
869  
870 Some functionality from the mplib hasn't yet been incorporated into this library:
871  
872 =over 8
873  
874 =item * I<mp_convert_to_v1(id3_tag* tag)>
875  
876 Converts an id3v2-tag into an id3v1-tag.
877  
878 =item * I<mp_convert_to_v2(id3_tag* tag)>
879  
880 Vice versa.
881  
882 =item * I<mp_is_valid_v1_value(int field, char* value)>
883  
884 Does some checks on an id3v1 field value. Oddly enough, there is no equivalent for id3v2 fields in the library.
885  
886 =back
887  
888 =item B<Additional functionality>
889  
890 I would like to have support for the I<APIC> frame (attached picture). Something like the following would be nice:
891  
892 $mp3->attach($file, $description);
893  
894 Probably much more, but I am in modest-mode right now.
895  
896 =item B<Error handling>
897  
898 Always an issue with me. Each object should have its own error reporting mechanism.
899  
900 =item B<Tests>
901  
902 No tests yet for functional interface, nor for the header access.
903  
904 =back
905  
906 =head1 VERSION
907  
908 This document describes version 0.02.
909  
910 =head1 AUTHOR AND COPYRIGHT
911  
912 Tassilo v. Parseval <tassilo.von.parseval@rwth-aachen.de>
913  
914 Copyright (c) 2002-2004 Tassilo von Parseval.
915 This program is free software; you can redistribute it and/or
916 modify it under the same terms as Perl itself.
917  
918 =head1 SEE ALSO
919  
920 B<http://www.id3.org/> for the id3 specifications.
921  
922 B<http://mplib.sourceforge.net/> if you want to visit mplib's home.
923  
924 =cut
925  
926 5 1;
927  
928 __END__
↑ Summary Mplib.xs »