blib/lib/Music/Lyrics/LRC.pm

filter: Statementstmt 100% 91 / 91 Branchbran 59.3% 19 / 32 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 18 / 18 Podpod 100% 8 / 8 totaltotal 91.2% 136 / 149 CC 20 SCAR 13.7
CC
20
Cov
89%
load
8.1
save
6.1
set_tag
3.1
CRAP 3.9
↑ Summary
1 package Music::Lyrics::LRC;
2  
3 # Force me to write this properly
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # Target reasonably old Perl
9 3 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # Include required modules
12 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use English '-no_match_vars';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # Declare package version
16 3 our $VERSION = '0.17';
17  
18 # Patterns to match elements of the LRC file; these are somewhat tolerant
19 3 our %RE = (
20  
21 # A blank line
22 blank => qr{
23 \A # Start of string
24 \s* # Any whitespace
25 \z # End of string
26 }msx,
27  
28 # A meta tag line
29 tag => qr{
30 \A # Start of string
31 \s* # Any whitespace
32 \[ # Opening left bracket
33 ([^:\r\n]+) # Tag name, capture
34 : # Colon
35 (.*) # Tag value, capture
36 \] # Closing right bracket
37 \s* # Any whitespace
38 \z # End of string
39 }msx,
40  
41 # A lyric line
42 lyric => qr{
43 \A # Start of string
44 \s* # Any whitespace
45 \[ # Opening left bracket
46 (\d+) # Minutes, capture
47 : # Colon
48 ( # Seconds group, capture
49 \d{1,2} # Whole seconds
50 (?: # Group for fractional seconds
51 [.] # Period
52 \d+ # At least one digit
53 )? # End optional fractional seconds group
54 ) # End seconds group
55 \] # Closing right bracket
56 [\t ]* # Any tabs or spaces
57 ( # Lyric line group, capture
58 (?:.*\S)? # Anything ending with non-whitespace
59 ) # End lyric line group
60 \s* # Any whitespace
61 \z # End of string
62 }msx,
63 );
64  
65 # Parser functions to consume and process captures from the above patterns
66 my %parsers = (
67  
68 # A meta tag line
69 2 ▶ tag => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
70 2 my ( $self, $tag, $value ) = @_;
71 2 $self->set_tag( $tag, $value );
72 },
73  
74 # A lyric line
75 4 ▶ lyric => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
76 4 my ( $self, $min, $sec, $text ) = @_;
77  
78 # Calculate the number of milliseconds
79 4 my $msec = $self->_min_sec_to_msec( $min, $sec );
80  
81 # Push a lyric hashref onto our list
82 4 $self->add_lyric( $msec, $text );
83 },
84 3 );
85  
86 # Oldschool constructor
87 3 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
88 3 my ( $class, %opts ) = @_;
89  
90 # Declare a hash to build the object around
91 3 my %self;
92  
93 # Start with empty tags and lyrics
94 3 $self{tags} = {};
95 3 $self{lyrics} = [];
96  
97 # Read in the "verbose" flag if defined, default to zero
98 $self{verbose} =
99 exists $opts{verbose}
100 ? !!$opts{verbose}
101 3 ▶ : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'verbose'} ? : 0 3 3
102  
103 # Perlician, bless thyself
104 3 return bless \%self, $class;
105 }
106  
107 # Read-only accessor for lyrics, sorted by time
108 4 ▶sub lyrics {
Subroutine lyrics: called CC=1 SCAR=0.0
Pod documented
109 4 my $self = shift;
110 4 my @lyrics = sort { $a->{time} <=> $b->{time} } @{ $self->{lyrics} };
111 4 return \@lyrics;
112 }
113  
114 # Read-only accessor for tags
115 1 ▶sub tags {
Subroutine tags: called CC=1 SCAR=0.0
Pod documented
116 1 my $self = shift;
117 1 my %tags = %{ $self->{tags} };
118 1 return \%tags;
119 }
120  
121 # Add a new lyric to the object
122 8 ▶sub add_lyric {
Subroutine add_lyric: called CC=3 SCAR=11.3
Pod documented
123 8 my ( $self, $time, $text ) = @_;
124  
125 # Check parameters
126 8 ▶ int $time >= 0
Branch: 1 branch
BranchTrueFalseTotal
int $time >= 0 or croak("Bad lyric time") 8 0 8
127 or croak 'Bad lyric time';
128 8 ▶ $text !~ m/ [\r\n] /msx
Branch: 1 branch
BranchTrueFalseTotal
$text =~ / [\r\n] /msx and croak("Bad lyric line") 0 8 8
129 or croak 'Bad lyric line';
130  
131 # Push the lyric onto our list
132 8 return push @{ $self->{lyrics} },
133 {
134 time => $time,
135 text => $text,
136 };
137 }
138  
139 # Set the value of a tag
140 4 ▶sub set_tag {
Subroutine set_tag: called CC=3 SCAR=11.4
Pod documented
141 4 my ( $self, $name, $value ) = @_;
142  
143 # Check parameters
144 4 ▶ $name !~ m/ [:\r\n] /msx
Branch: 1 branch
BranchTrueFalseTotal
$name =~ / [:\r\n] /msx and croak("Bad tag name") 0 4 4
145 or croak 'Bad tag name';
146  
147 # Tag content cannot have vertical whitespace
148 4 ▶ $value !~ m/ [\r\n] /msx
Branch: 1 branch
BranchTrueFalseTotal
$value =~ / [\r\n] /msx and croak("Bad tag value") 0 4 4
149 or croak 'Bad tag value';
150  
151 # Set the tag's value on our hash
152 4 return ( $self->{tags}{$name} = $value );
153 }
154  
155 # Unset a tag
156 1 ▶sub unset_tag {
Subroutine unset_tag: called CC=3 SCAR=11.4
Pod documented
157 1 my ( $self, $name ) = @_;
158  
159 # Check parameters
160 1 ▶ $name !~ m/ [:\r\n] /msx
Branch: 1 branch
BranchTrueFalseTotal
$name =~ / [:\r\n] /msx and croak("Bad tag name") 0 1 1
161 or croak 'Bad tag name';
162 1 ▶ exists $self->{tags}{$name}
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{"tags"}{$name} or carp("Tag not set") 1 0 1
163 or carp 'Tag not set';
164  
165 # Delete the tag's value
166 1 return defined delete $self->{tags}{$name};
167 }
168  
169 # Parse an LRC file from a given filehandle
170 1 ▶sub load {
Subroutine load: called CC=8 SCAR=20.9
Pod documented
171 1 my ( $self, $fh ) = @_;
172  
173 # Panic if this doesn't look like a filehandle
174 1 ▶ ref $fh eq 'GLOB'
Branch: 1 branch
BranchTrueFalseTotal
ref $fh eq "GLOB" or croak("Not a filehandle") 1 0 1
175 or croak 'Not a filehandle';
176  
177 # Iterate through lines
178 1 ▶ LINE: while ( my $line = <$fh> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 10 1 11
179  
180 # Iterate through line types until one matches
181 10 TYPE: for my $type (qw(lyric tag blank)) {
182 20 ▶ my @vals = $line =~ $RE{$type}
Branch: 1 branch
BranchTrueFalseTotal
my(@vals) = $line =~ /$Music::Lyrics::LRC::RE{$type}/ or next TYPE 9 11 20
183 or next TYPE;
184 9 ▶ exists $parsers{$type}
Branch: 1 branch
BranchTrueFalseTotal
exists $parsers{$type} or next LINE 6 3 9
185 or next LINE;
186 6 $parsers{$type}->( $self, @vals );
187 6 next LINE;
188 }
189  
190 # No line format match, warn if verbose
191 1 ▶ warn "Unknown format for line $NR\n" if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 1 1
192 }
193  
194 # Check we got to the end of the file
195 1 ▶ eof $fh or die "Failed file read: $ERRNO\n";
Branch: 1 branch
BranchTrueFalseTotal
eof $fh or die "Failed file read: $Music::Lyrics::LRC::ERRNO\n" 1 0 1
196  
197 # All done, return the number of lyrics we have now
198 1 return scalar @{ $self->lyrics };
199 }
200  
201 # Write an LRC file to a given filehandle
202 1 ▶sub save {
Subroutine save: called CC=6 SCAR=18.1
Pod documented
203 1 my ( $self, $fh ) = @_;
204  
205 # Panic if this doesn't look like a filehandle
206 1 ▶ ref $fh eq 'GLOB'
Branch: 1 branch
BranchTrueFalseTotal
ref $fh eq "GLOB" or croak("Not a filehandle") 1 0 1
207 or croak 'Not a filehandle';
208  
209 # Start counting lines written
210 1 my $lines = 0;
211  
212 # Iterate through tags
213 1 for my $name ( sort keys %{ $self->{tags} } ) {
214 1 my $value = $self->{tags}{$name};
215 1 ▶ $lines += printf {$fh} "[%s:%s]\n", $name, $value
Branch: 1 branch
BranchTrueFalseTotal
$lines += printf({$fh;} "[%s:%s]\n", $name, $value) or die "Failed tag write: $Music::Lyrics::LRC::ERRNO\n" 1 0 1
216 or die "Failed tag write: $ERRNO\n";
217 }
218  
219 # Iterate through lyrics (sorted by time)
220 1 for my $lyric ( @{ $self->lyrics } ) {
221  
222 # Convert milliseconds to timestamp hash
223 2 my $msec = $lyric->{time};
224 2 my ( $min, $sec ) = $self->_msec_to_min_sec($msec);
225  
226 # Write the line to the file, counting the lines
227 2 $lines += printf {$fh} "[%02u:%05.2f]%s\n", $min, $sec, $lyric->{text}
228 2 ▶ or die "Failed lyric write: $ERRNO\n";
Branch: 1 branch
BranchTrueFalseTotal
$lines += printf({$fh;} "[%02u:%05.2f]%s\n", $min, $sec, $lyric->{"text"}) or die "Failed lyric write: $Music::Lyrics::LRC::ERRNO\n" 2 0 2
229 }
230  
231 # Return the number of lines written
232 1 return $lines;
233 }
234  
235 # Named constants for the conversion functions
236 # This stands for "millisecond factors"
237 3 my %MSF = (
238 sec => 1_000,
239 min => 60_000,
240 );
241  
242 # Convert a minutes-seconds pair to milliseconds
243 4 ▶sub _min_sec_to_msec {
Subroutine _min_sec_to_msec: called CC=1 SCAR=0.0
244 4 my ( $self, $min, $sec ) = @_;
245 4 my $msec = 0;
246 4 $msec += int $min * $MSF{min};
247 4 $msec += $sec * $MSF{sec};
248 4 return $msec;
249 }
250  
251 # Convert milliseconds to a minutes-seconds pair
252 2 ▶sub _msec_to_min_sec {
Subroutine _msec_to_min_sec: called CC=1 SCAR=0.0
253 2 my ( $self, $msec ) = @_;
254 2 my $min = int $msec / $MSF{min};
255 2 my $sec = ( int $msec ) % $MSF{min} / $MSF{sec};
256 2 return ( $min, $sec );
257 }
258  
259 3 1;
260  
261 __END__
↑ Summary