| 1 | package MP3::Cut::Gapless; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 2 | our $VERSION = '0.03'; | |||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 2 | require XSLoader; | |||||||||||||||||||||||||||
| 8 | 2 | XSLoader::load('MP3::Cut::Gapless', $VERSION); | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 29 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=7 SCAR=19.7
Pod
documented
| |||||||||||||||||||||||||||||
| 11 | 29 | my ( $class, %args ) = @_; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 29 | ▶ | if ( !$args{file} && !$args{cue} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$args{"file"} or $args{"cue"}
MC/DC0%0/2
$args{"file"} or $args{"cue"}
$args{"file"}
$args{"cue"}
Truth table66%2/3
A or B
A= $args{"file"}B= $args{"cue"}
| |||||||||||||||||||||||||||||
| 14 | 0 | die "Either file or cue argument must be specified"; | |||||||||||||||||||||||||||
| 15 | } | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 29 | my $self = bless \%args, $class; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 29 | ▶ | if ( $self->{cue} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 20 | 2 | $self->_parse_cue; | |||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 29 | ▶ | if ( !-f $self->{file} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | 0 | die "Invalid file: $self->{file}"; | |||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 29 | ▶ | if ( $self->{cache_dir} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | 3 | require Digest::MD5; | |||||||||||||||||||||||||||
| 29 | 3 | require File::Spec; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 3 | ▶ | if ( !-d $self->{cache_dir} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 1 | require File::Path; | |||||||||||||||||||||||||||
| 33 | 1 | File::Path::mkpath( $self->{cache_dir} ); | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | # Cache key is filename + size + mtime so if the file changes | ||||||||||||||||||||||||||||
| 37 | # we will not read a stale cache file. | ||||||||||||||||||||||||||||
| 38 | 3 | my ($size, $mtime) = (stat $self->{file})[7, 9]; | |||||||||||||||||||||||||||
| 39 | $self->{cache_file} = File::Spec->catfile( | ||||||||||||||||||||||||||||
| 40 | $self->{cache_dir}, | ||||||||||||||||||||||||||||
| 41 | 3 | Digest::MD5::md5_hex( $self->{file} . $size . $mtime ) . '.mllt' | |||||||||||||||||||||||||||
| 42 | ); | ||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | # Pre-scan the file for the range we will be cutting | ||||||||||||||||||||||||||||
| 46 | 29 | $self->_init; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 28 | return $self; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 2 | ▶ | sub tracks { | ||||||||||||||||||||||||||
|
Subroutine
tracks: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 52 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 2 | ▶ | return @{ $self->{_tracks} || [] }; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->{'_tracks'} || []
MC/DC0%0/1
$self->{'_tracks'} || []
$self->{'_tracks'}
Truth table50%1/2
A
A= $self->{'_tracks'}
| |||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 4 | ▶ | sub write { | ||||||||||||||||||||||||||
|
Subroutine
write: called CC=5 SCAR=16.5
Pod
documented
| |||||||||||||||||||||||||||||
| 58 | 4 | my ( $self, $track, $filename ) = @_; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 4 | ▶ | if ( !$filename ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | # Default filename is "position - performer - title.mp3" | ||||||||||||||||||||||||||||
| 62 | 0 | $filename = join(' - ', $track->{position}, $track->{performer}, $track->{title}) . '.mp3'; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 4 | ▶ | if ( -e $filename ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 0 | warn "$filename already exists, will not overwrite\n"; | |||||||||||||||||||||||||||
| 67 | 0 | return; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | # Reset XS counter for read() | ||||||||||||||||||||||||||||
| 71 | 4 | $self->__reset_read(); | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 4 | delete $self->{start_ms}; | |||||||||||||||||||||||||||
| 74 | 4 | delete $self->{end_ms}; | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 4 | $self->{start_ms} = $track->{start_ms}; | |||||||||||||||||||||||||||
| 77 | 4 | ▶ | $self->{end_ms} = $track->{end_ms} if $track->{end_ms}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 4 | print "Writing $filename...\n"; | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 4 | open my $fh, '>', $filename; | |||||||||||||||||||||||||||
| 82 | 4 | ▶ | while ( $self->read( my $buf, 65536 ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 4 | syswrite $fh, $buf; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | 4 | close $fh; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | # read() is implemented in XS | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 29 | ▶ | sub _init { | ||||||||||||||||||||||||||
|
Subroutine
_init: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 91 | 29 | my $self = shift; | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 29 | ▶ | open $self->{_fh}, '<', $self->{file} || die "Unable to open $self->{file} for reading"; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->{'file'} || die("Unable to open $self->{'file'} for reading")
MC/DC0%0/1
$self->{'file'} || die("Unable to open $self->{'file'} for reading")
$self->{'file'}
Truth table50%1/2
A
A= $self->{'file'}
| |||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 29 | binmode $self->{_fh}; | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | # XS init | ||||||||||||||||||||||||||||
| 98 | 29 | $self->{_mp3c} = $self->__init(@_); | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 2 | ▶ | sub _parse_cue { | ||||||||||||||||||||||||||
|
Subroutine
_parse_cue: called CC=6 SCAR=18.0
| |||||||||||||||||||||||||||||
| 102 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 2 | require Audio::Cuefile::Parser; | |||||||||||||||||||||||||||
| 105 | 2 | require MP3::Cut::Gapless::Track; | |||||||||||||||||||||||||||
| 106 | 2 | require File::Spec; | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 2 | my $cue = Audio::Cuefile::Parser->new( $self->{cue} ); | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 2 | ▶ | if ( !$self->{file} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 2 | ▶ | $self->{file} = $cue->file || die "No FILE entry found in cue sheet"; | ||||||||||||||||||||||||||
|
Condition50%1/2
$cue->file || die('No FILE entry found in cue sheet')
MC/DC0%0/1
$cue->file || die('No FILE entry found in cue sheet')
$cue->file
Truth table50%1/2
A
A= $cue->file
| |||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | # Handle relative path | ||||||||||||||||||||||||||||
| 114 | 2 | my ($vol, $dirs, $file) = File::Spec->splitpath( $self->{file} ); | |||||||||||||||||||||||||||
| 115 | 2 | ▶ | if ( $dirs !~ m{^[/\\]} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 2 | my ($cvol, $cdirs, undef) = File::Spec->splitpath( File::Spec->rel2abs( $self->{cue} ) ); | |||||||||||||||||||||||||||
| 117 | 2 | $self->{file} = File::Spec->rel2abs( $self->{file}, File::Spec->catdir($cvol, $cdirs) ); | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 2 | $self->{_tracks} = []; | |||||||||||||||||||||||||||
| 122 | 2 | for my $track ( $cue->tracks ) { | |||||||||||||||||||||||||||
| 123 | 8 | push @{ $self->{_tracks} }, MP3::Cut::Gapless::Track->new($track); | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | # Set end_ms values, last track will fill to the end | ||||||||||||||||||||||||||||
| 127 | 2 | ▶ | for ( my $i = 0; $i < scalar @{ $self->{_tracks} } - 1; $i++ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | 6 | $self->{_tracks}->[$i]->{end_ms} = $self->{_tracks}->[$i + 1]->{start_ms}; | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 29 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 133 | 29 | my $self = shift; | |||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 29 | close $self->{_fh}; | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 29 | ▶ | $self->__cleanup( $self->{_mp3c} ) if exists $self->{_mp3c}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 2 | 1; | |||||||||||||||||||||||||||
| 141 | __END__ | ||||||||||||||||||||||||||||