blib/lib/Music/BachChoralHarmony.pm

filter: Statementstmt 97.5% 158 / 162 Branchbran 90.0% 81 / 90 Conditioncond 78.2% 18 / 23 MC/DCmcdc 46.6% 7 / 15 Subroutinesub 87.5% 14 / 16 Podpod 100% 3 / 3 totaltotal 90.9% 281 / 309 CC 67 SCAR 29.8
CC
67
Cov
93%
search
32.0
_search_param
18.9
parse
14.3
CRAP 19.7
↑ Summary
1 package Music::BachChoralHarmony;
2 2 our $AUTHORITY = 'cpan:GENE';
3  
4 # ABSTRACT: Parse the UCI Bach choral harmony data set
5  
6 2 our $VERSION = '0.0412';
7  
8 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 ▶use Text::CSV ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use File::ShareDir qw/ dist_dir /;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use List::Util qw/ any /;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17  
18 has data_file => (
19 is => 'ro',
20 0 ▶ default => sub { dist_dir('Music-BachChoralHarmony') . '/jsbach_chorals_harmony.data' },
Subroutine __ANON__: not called CC=1 SCAR=6.9
21 2 );
22  
23  
24 has key_title => (
25 is => 'ro',
26 0 ▶ default => sub { dist_dir('Music-BachChoralHarmony') . '/jsbach_BWV_keys_titles.txt' },
Subroutine __ANON__: not called CC=1 SCAR=6.9
27 2 );
28  
29  
30 has data => (
31 is => 'rw',
32 init_arg => undef,
33 1 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
34 2 );
35  
36  
37 1 ▶sub parse {
Subroutine parse: called CC=14 SCAR=26.6
Pod documented
38 1 my ($self) = @_;
39  
40 # Collect the key signatures and titles
41 1 my %data;
42  
43 1 ▶ open my $fh, '<', $self->key_title
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $self->key_title or die "Can't read ", $self->key_title, ": $!" 1 0 1
44 or die "Can't read ", $self->key_title, ": $!";
45  
46 1 ▶ while ( my $line = readline($fh) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 64 1 65
47 64 chomp $line;
48 64 ▶ next if $line =~ /^\s*$/ || $line =~ /^#/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*$/ or $line =~ /^#/ 4 60 64
Condition100%3/3
$line =~ /^\s*$/ or $line =~ /^#/
l!l&&r!l&&!r
2260
MC/DC100%2/2
$line =~ /^\s*$/ or $line =~ /^#/
$line =~ /^\s*$/ $line =~ /^#/
Truth table100%3/3
A or B
A= $line =~ /^\s*$/B= $line =~ /^#/
ABresult
1X1
011
000
49 60 my @parts = split /\s+/, $line, 4;
50 60 $data{ $parts[0] } = {
51 bwv => $parts[1],
52 key => $parts[2],
53 title => $parts[3],
54 };
55 }
56  
57 1 close $fh;
58  
59 # Collect the events
60 1 ▶ my $csv = Text::CSV->new( { binary => 1 } )
Branch: 1 branch
BranchTrueFalseTotal
my $csv = "Text::CSV"->new({"binary", 1}) or die "Can't use CSV: ", "Text::CSV"->error_diag 1 0 1
61 or die "Can't use CSV: ", Text::CSV->error_diag;
62  
63 1 ▶ open $fh, '<', $self->data_file
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", $self->data_file or die "Can't read ", $self->data_file, ": $!" 1 0 1
64 or die "Can't read ", $self->data_file, ": $!";
65  
66 1 my $progression;
67  
68 # 000106b_ 2 YES NO NO NO YES NO NO YES NO NO NO NO E 5 C_M
69 1 ▶ while ( my $row = $csv->getline($fh) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $csv->getline($fh)) 5665 1 5666
70  
71 5665 ( my $id = $row->[0] ) =~ s/\s*//g;
72  
73 5665 my $notes = '';
74  
75 5665 for my $note ( 2 .. 13 ) {
76 67980 ▶ $notes .= $row->[$note] eq 'YES' ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$row->[$note] eq 'YES' ? : 18627 49353 67980
77 }
78  
79 5665 ( my $bass = $row->[14] ) =~ s/\s*//g;
80 5665 ( my $accent = $row->[15] ) =~ s/\s*//g;
81 5665 ( my $chord = $row->[16] ) =~ s/\s*//g;
82  
83 5665 ▶ $progression->{$id}{key} ||= $data{$id}{key};
Condition66%2/3
$progression->{$id}{"key"} ||= $data{$id}{"key"}
l!l&&r!l&&!r
5605600
MC/DC0%0/2
$progression->{$id}{"key"} ||= $data{$id}{"key"}
$progression->{$id}{"key"} $data{$id}{"key"}
Truth table66%2/3
A ||= B
A= $progression->{$id}{"key"}B= $data{$id}{"key"}
ABresult
1X1
011
000
84 5665 ▶ $progression->{$id}{bwv} ||= $data{$id}{bwv};
Condition66%2/3
$progression->{$id}{"bwv"} ||= $data{$id}{"bwv"}
l!l&&r!l&&!r
5605600
MC/DC0%0/2
$progression->{$id}{"bwv"} ||= $data{$id}{"bwv"}
$progression->{$id}{"bwv"} $data{$id}{"bwv"}
Truth table66%2/3
A ||= B
A= $progression->{$id}{"bwv"}B= $data{$id}{"bwv"}
ABresult
1X1
011
000
85 5665 ▶ $progression->{$id}{title} ||= $data{$id}{title};
Condition66%2/3
$progression->{$id}{"title"} ||= $data{$id}{"title"}
l!l&&r!l&&!r
5605600
MC/DC0%0/2
$progression->{$id}{"title"} ||= $data{$id}{"title"}
$progression->{$id}{"title"} $data{$id}{"title"}
Truth table66%2/3
A ||= B
A= $progression->{$id}{"title"}B= $data{$id}{"title"}
ABresult
1X1
011
000
86  
87 5665 my $struct = {
88 notes => $notes,
89 bass => $bass,
90 accent => $accent,
91 chord => $chord,
92 };
93  
94 5665 push @{ $progression->{$id}{events} }, $struct;
95 }
96  
97 1 ▶ $csv->eof or die $csv->error_diag;
Branch: 1 branch
BranchTrueFalseTotal
$csv->eof or die $csv->error_diag 1 0 1
98 1 close $fh;
99  
100 1 $self->data($progression);
101  
102 1 return $self->data;
103 }
104  
105  
106 63 ▶sub search {
Subroutine search: called CC=32 SCAR=34.7
Pod documented
107 63 my ( $self, %args ) = @_;
108  
109 63 my %results = ();
110  
111 63 ▶ if ( $args{id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"id"}) 26 37 63
112 26 my @ids = split /\s+/, $args{id};
113  
114 26 for my $id ( @ids ) {
115 30 $results{$id} = $self->data->{$id};
116 }
117 }
118  
119 63 ▶ if ( $args{key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"key"}) 12 51 63
120 12 ▶ my @iter = keys %results ? keys %results : keys %{ $self->data };
Branch: 1 branch
BranchTrueFalseTotal
keys %results ? : 5 7 12
121  
122 12 my @keys = split /\s+/, $args{key};
123  
124 12 for my $id ( @iter ) {
125 427 ▶ if ( $results{$id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($results{$id}) { } 7 420 427
126 delete $results{$id}
127 7 ▶ unless any { $_ eq $results{$id}{key} } @keys;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
unless &any(sub { $_ eq $results{$id}{"key"}; } , @keys) 2 5 7
128 }
129 else {
130 $results{$id} = $self->data->{$id}
131 420 ▶ if any { $_ eq $self->data->{$id}{key} } @keys;
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if &any(sub { $_ eq $self->data->{$id}{"key"}; } , @keys) 80 340 420
132 }
133 }
134 }
135  
136 63 ▶ if ( $args{bass} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"bass"}) 8 55 63
137 8 %results = $self->_search_param( bass => $args{bass}, \%results );
138 }
139  
140 63 ▶ if ( $args{chord} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"chord"}) 8 55 63
141 8 %results = $self->_search_param( chord => $args{chord}, \%results );
142 }
143  
144 63 ▶ if ( $args{notes} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"notes"}) 27 36 63
145 27 ▶ my @iter = keys %results ? keys %results : keys %{ $self->data };
Branch: 1 branch
BranchTrueFalseTotal
keys %results ? : 11 16 27
146  
147 27 ▶ my $and = $args{notes} =~ /&/ ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$args{'notes'} =~ /&/ ? : 9 18 27
148 27 ▶ my $re = $and ? qr/\s*&\s*/ : qr/\s+/;
Branch: 1 branch
BranchTrueFalseTotal
$and ? : 9 18 27
149  
150 27 my @notes = split $re, $args{notes};
151  
152 27 my %index = (
153 'C' => 0,
154 'C#' => 1,
155 'Db' => 1,
156 'D' => 2,
157 'D#' => 3,
158 'Eb' => 3,
159 'E' => 4,
160 'F' => 5,
161 'F#' => 6,
162 'Gb' => 6,
163 'G' => 7,
164 'G#' => 8,
165 'Ab' => 8,
166 'A' => 9,
167 'A#' => 10,
168 'Bb' => 10,
169 'B' => 11,
170 );
171  
172 27 ID: for my $id ( @iter ) {
173 972 my %and_notes = ();
174  
175 972 my $match = 0;
176  
177 972 for my $event ( @{ $self->data->{$id}{events} } ) {
178 92219 my @bitstring = split //, $event->{notes};
179  
180 92219 my $i = 0;
181  
182 92219 for my $bit ( @bitstring ) {
183 1106628 ▶ if ( $bit ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bit) 303415 803213 1106628
184 303415 for my $note ( sort @notes ) {
185 530590 ▶ if ( defined $index{$note} && $i == $index{$note} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $index{$note} and $i == $index{$note}) 49038 481552 530590
Condition100%3/3
defined $index{$note} and $i == $index{$note}
!ll&&!rl&&r
5754342400949038
MC/DC100%2/2
defined $index{$note} and $i == $index{$note}
defined $index{$note} $i == $index{$note}
Truth table100%3/3
A and B
A= defined $index{$note}B= $i == $index{$note}
ABresult
0X0
100
111
186 49038 ▶ if ( $and ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($and) { } 20572 28466 49038
187 20572 $and_notes{$note}++;
188 }
189 else {
190 28466 $match++;
191 }
192 }
193 }
194 }
195  
196 1106628 $i++;
197 }
198 }
199  
200 972 ▶ if ( $and ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($and) { } 305 667 972
201 305 ▶ if ( keys %and_notes ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %and_notes) 292 13 305
202 292 my %notes;
203 292 @notes{@notes} = undef;
204  
205 292 my $i = 0;
206  
207 292 for my $n ( keys %and_notes ) {
208 $i++
209 547 ▶ if exists $notes{$n};
Branch: 1 branch
BranchTrueFalseTotal
if exists $notes{$n} 547 0 547
210 }
211  
212 292 ▶ if ( $i == scalar keys %notes ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == scalar keys %notes) { } 198 94 292
213 198 $results{$id} = $self->data->{$id};
214 }
215 else {
216 delete $results{$id}
217 94 ▶ if $results{$id};
Branch: 1 branch
BranchTrueFalseTotal
if $results{$id} 2 92 94
218 }
219 }
220 }
221 else {
222 667 ▶ if ( $results{$id} && $match <= 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($results{$id} and $match <= 0) { } 2 665 667
elsif ($match > 0) { } 547 118 665
Condition100%3/3
$results{$id} and $match <= 0
!ll&&!rl&&r
66052
MC/DC100%2/2
$results{$id} and $match <= 0
$results{$id} $match <= 0
Truth table100%3/3
A and B
A= $results{$id}B= $match <= 0
ABresult
0X0
100
111
223 2 delete $results{$id};
224 }
225 elsif ( $match > 0 ) {
226 547 $results{$id} = $self->data->{$id};
227 }
228 }
229 }
230 }
231  
232 63 return \%results;
233 }
234  
235  
236 20 ▶sub bits2notes {
Subroutine bits2notes: called CC=6 SCAR=17.9
Pod documented
237 20 my ( $self, $string, $accidental ) = @_;
238  
239 20 ▶ $accidental ||= 'b';
Condition100%2/2
$accidental ||= "b"
l!l
614
MC/DC100%1/1
$accidental ||= "b"
$accidental
Truth table100%2/2
A
A= $accidental
Aresult
11
00
240  
241 20 my @notes = ();
242  
243 2 ▶ no warnings 'qw';
Subroutine BEGIN: called CC=1 SCAR=0.0
244 20 my @positions = qw( C C#|Db D D#|Eb E F F#|Gb G G#|Ab A A#|Bb B );
245  
246 20 my @bits = split //, $string;
247  
248 20 my $i = 0;
249  
250 20 for my $bit ( @bits ) {
251 240 ▶ if ( $bit ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bit) 21 219 240
252 21 my @note = split /\|/, $positions[$i];
253 21 my $note = '';
254  
255 21 ▶ if ( @note > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@note > 1) { } 12 9 21
256 12 ▶ $note = $accidental eq '#' ? $note[0] : $note[1];
Branch: 1 branch
BranchTrueFalseTotal
$accidental eq '#' ? : 6 6 12
257 }
258 else {
259 9 $note = $note[0];
260 }
261  
262 21 push @notes, $note;
263 }
264  
265 240 $i++;
266 }
267  
268 20 return \@notes;
269 }
270  
271 16 ▶sub _search_param {
Subroutine _search_param: called CC=18 SCAR=29.4
272 16 my ( $self, $name, $param, $seen ) = @_;
273  
274 16 ▶ my @iter = keys %$seen ? keys %$seen : keys %{ $self->data };
Branch: 1 branch
BranchTrueFalseTotal
keys %$seen ? : 8 8 16
275  
276 16 my %results = ();
277  
278 16 ▶ my $and = $param =~ /&/ ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$param =~ /&/ ? : 4 12 16
279 16 ▶ my $re = $and ? qr/\s*&\s*/ : qr/\s+/;
Branch: 1 branch
BranchTrueFalseTotal
$and ? : 4 12 16
280  
281 16 my %notes = ();
282 16 @notes{ split $re, $param } = undef;
283  
284 16 ID: for my $id ( @iter ) {
285 488 my %and_notes = ();
286  
287 488 my $match = 0;
288  
289 488 for my $event ( @{ $self->data->{$id}{events} } ) {
290 46616 for my $note ( keys %notes ) {
291 69924 ▶ if ( $note eq $event->{$name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($note eq $event->{$name}) 3516 66408 69924
292 3516 ▶ if ( $and ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($and) { } 1172 2344 3516
293 1172 $and_notes{$note}++;
294 }
295 else {
296 2344 $match++;
297 }
298 }
299 }
300 }
301  
302 488 ▶ if ( $and ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($and) { } 122 366 488
303 122 ▶ if ( keys %and_notes ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %and_notes) 86 36 122
304 86 my $i = 0;
305  
306 86 for my $n ( keys %and_notes ) {
307 $i++
308 86 ▶ if exists $notes{$n};
Branch: 1 branch
BranchTrueFalseTotal
if exists $notes{$n} 86 0 86
309 }
310  
311 86 ▶ if ( $i == scalar keys %notes ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == scalar keys %notes) { } 0 86 86
312 0 $results{$id} = $self->data->{$id};
313 }
314 else {
315 delete $results{$id}
316 86 ▶ if $results{$id};
Branch: 1 branch
BranchTrueFalseTotal
if $results{$id} 0 86 86
317 }
318 }
319 }
320 else {
321 366 ▶ if ( $results{$id} && $match <= 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($results{$id} and $match <= 0) { } 0 366 366
elsif ($match > 0) { } 172 194 366
Condition33%1/3
$results{$id} and $match <= 0
!ll&&!rl&&r
36600
MC/DC0%0/2
$results{$id} and $match <= 0
$results{$id} $match <= 0
Truth table33%1/3
A and B
A= $results{$id}B= $match <= 0
ABresult
0X0
100
111
322 0 delete $results{$id};
323 }
324 elsif ( $match > 0 ) {
325 172 $results{$id} = $self->data->{$id};
326 }
327 }
328 }
329  
330 16 return %results;
331 }
332  
333 2 1;
334  
335 __END__
↑ Summary