blib/lib/MIDI/Chord/Guitar.pm

filter: Statementstmt 96.1% 126 / 131 Branchbran 90.6% 29 / 32 Conditioncond 60.0% 12 / 20 MC/DCmcdc 33.3% 4 / 12 Subroutinesub 95.0% 19 / 20 Podpod 100% 3 / 3 totaltotal 88.5% 193 / 218 CC 33 SCAR 15.7
CC
33
Cov
91%
_find_fingering
9.8
fingering
6.0
voicings
6.0
CRAP 4.8
↑ Summary
1 package MIDI::Chord::Guitar;
2 2 our $AUTHORITY = 'cpan:GENE';
3  
4 # ABSTRACT: MIDI pitches for guitar chord voicings
5  
6 2 our $VERSION = '0.0706';
7  
8 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 ▶use Carp qw(croak);
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 zip);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Music::Note ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Text::CSV_XS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 2 with('Music::PitchNum');
21  
22  
23 2 has voicing_file => (
24 is => 'lazy',
25 );
26  
27 0 ▶sub _build_voicing_file {
Subroutine _build_voicing_file: not called CC=1 SCAR=6.9
28 0 my ($self) = @_;
29 0 my $file = eval { dist_dir('MIDI-Chord-Guitar') . '/midi-guitar-chord-voicings.csv' };
30 0 return $file;
31 }
32  
33  
34 2 has chords => (
35 is => 'lazy',
36 init_arg => undef,
37 );
38  
39 3 ▶sub _build_chords {
Subroutine _build_chords: called CC=5 SCAR=16.1
40 3 my ($self) = @_;
41  
42 3 my $file = $self->voicing_file;
43  
44 3 my %data;
45  
46 3 my $csv = Text::CSV_XS->new({ binary => 1 });
47  
48 3 ▶ open my $fh, '<', $file
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or croak("Can't read ${file}: $!") 3 0 3
49 or croak "Can't read $file: $!";
50  
51 3 ▶ while (my $row = $csv->getline($fh)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $csv->getline($fh)) 336 3 339
52 336 my $chord = shift @$row;
53 336 my $fingering = shift @$row;
54 336 push @{ $data{$chord}{fingering} }, $fingering;
55 336 my @notes;
56 336 for my $r (@$row) {
57 2016 ▶ push @notes, $r if $r ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $r ne "" 1500 516 2016
58 }
59 336 push @{ $data{$chord}{notes} }, \@notes;
60 }
61  
62 3 close $fh;
63  
64 3 return \%data;
65 }
66  
67  
68 8 ▶sub transform {
Subroutine transform: called CC=5 SCAR=16.1
Pod documented
69 8 my ($self, $target, $chord_name, $variation) = @_;
70  
71 8 $target = $self->pitchnum($target);
72 8 ▶ croak 'Invalid note' unless $target;
Branch: 1 branch
BranchTrueFalseTotal
unless $target 1 7 8
73  
74 7 ▶ $chord_name //= '';
Condition50%1/2
$chord_name //= ""
l!l
70
MC/DC0%0/1
$chord_name //= ""
$chord_name
Truth table50%1/2
A
A= $chord_name
Aresult
11
00
75  
76 7 my @notes;
77  
78 7 ▶ if (defined $variation) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $variation) { } 6 1 7
79 6 my $pitches = $self->chords->{ 'C' . $chord_name }{notes}[$variation];
80  
81 6 my $diff = $target - _lowest_c($pitches);
82  
83 6 @notes = map { $_ + $diff } @$pitches;
84 }
85 else {
86 1 for my $pitches (@{ $self->chords->{ 'C' . $chord_name }{notes} }) {
87 3 my $diff = $target - _lowest_c($pitches);
88 3 push @notes, [ map { $_ + $diff } @$pitches ];
89 }
90 }
91  
92 7 return \@notes;
93 }
94  
95 27 ▶sub _lowest_c {
Subroutine _lowest_c: called CC=3 SCAR=11.0
96 27 my ($pitches) = @_;
97  
98 27 my $lowest = 0;
99  
100 27 for my $c (48, 60, 72) {
101 37 ▶ if (any { $_ == $c } @$pitches) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (&any(sub { $_ == $c; } , @$pitches)) 25 12 37
102 25 $lowest = $c;
103 25 last;
104 }
105 }
106  
107 27 return $lowest;
108 }
109  
110  
111 3 ▶sub voicings {
Subroutine voicings: called CC=6 SCAR=17.9
Pod documented
112 3 my ($self, $chord_name, $format) = @_;
113  
114 3 ▶ $chord_name //= '';
Condition50%1/2
$chord_name //= ""
l!l
30
MC/DC0%0/1
$chord_name //= ""
$chord_name
Truth table50%1/2
A
A= $chord_name
Aresult
11
00
115 3 ▶ $format ||= '';
Condition100%2/2
$format ||= ""
l!l
21
MC/DC100%1/1
$format ||= ""
$format
Truth table100%2/2
A
A= $format
Aresult
11
00
116  
117 3 my $voicings = $self->chords->{ 'C' . $chord_name }{notes};
118  
119 3 ▶ if ($format) {
Branch: 1 branch
BranchTrueFalseTotal
if ($format) 2 1 3
120 2 my $temp;
121  
122 2 for my $chord (@$voicings) {
123 6 my $span;
124  
125 6 for my $n (@$chord) {
126 30 my $note = Music::Note->new($n, 'midinum')->format($format);
127 30 push @$span, $note;
128 }
129  
130 6 push @$temp, $span;
131 }
132  
133 2 $voicings = $temp;
134 }
135  
136 3 return $voicings;
137 }
138  
139  
140 10 ▶sub fingering {
Subroutine fingering: called CC=6 SCAR=18.0
Pod documented
141 10 my ($self, $target, $chord_name, $variation) = @_;
142  
143 10 $target = $self->pitchnum($target);
144  
145 10 ▶ $chord_name //= '';
Condition50%1/2
$chord_name //= ""
l!l
100
MC/DC0%0/1
$chord_name //= ""
$chord_name
Truth table50%1/2
A
A= $chord_name
Aresult
11
00
146  
147 10 my @fingering;
148  
149 10 ▶ if (defined $variation) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $variation) { } 8 2 10
150 8 my $fingering = $self->chords->{ 'C' . $chord_name }{fingering}[$variation];
151 8 my $pitches = $self->chords->{ 'C' . $chord_name }{notes}[$variation];
152  
153 8 my ($str, $p) = _find_fingering($target, $pitches, $fingering);
154  
155 8 ▶ push @fingering, $str . '-' . $p if $p >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $p >= 0 8 0 8
156 }
157 else {
158 2 for (zip $self->chords->{ 'C' . $chord_name }{notes}, $self->chords->{ 'C' . $chord_name }{fingering}) {
159 10 my ($pitches, $fingering) = @$_;
160  
161 10 my ($str, $p) = _find_fingering($target, $pitches, $fingering);
162  
163 10 ▶ push @fingering, $str . '-' . $p if $p >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $p >= 0 6 4 10
164 }
165 }
166  
167 10 return \@fingering;
168 }
169  
170 # XXX This is overly complicated, questionable logic
171 18 ▶sub _find_fingering {
Subroutine _find_fingering: called CC=9 SCAR=22.9
172 18 my ($target, $pitches, $fingering) = @_;
173  
174 18 my $diff = $target - _lowest_c($pitches);
175  
176 18 my ($str, $pos) = split /-/, $fingering;
177  
178 18 my $p = $pos + $diff;
179  
180 18 ▶ if ($pos != 1 && $str !~ /0/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($pos != 1 and not $str =~ /0/) { } 12 6 18
elsif ($p > 1 and $str =~ /0/) { } 2 8 10
Condition66%2/3
$pos != 1 and not $str =~ /0/
!ll&&!rl&&r
6012
MC/DC50%1/2
$pos != 1 and not $str =~ /0/
$pos != 1 not $str =~ /0/
Truth table66%2/3
A and not(B)
A= $pos != 1B= not $str =~ /0/
ABresult
0X0
100
111
181 12 ▶ if ($p == 0 && $str !~ /0/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($p == 0 and not $str =~ /0/) { } 4 8 12
elsif ($p != 0 and $str =~ /0/) { } 0 8 8
Condition66%2/3
$p == 0 and not $str =~ /0/
!ll&&!rl&&r
804
MC/DC50%1/2
$p == 0 and not $str =~ /0/
$p == 0 not $str =~ /0/
Truth table66%2/3
A and not(B)
A= $p == 0B= not $str =~ /0/
ABresult
0X0
100
111
182 4 $str = _decrement_fingering($str);
183 4 $p++;
184 }
185 ▶ elsif ($p != 0 && $str =~ /0/) {
Condition33%1/3
$p != 0 and $str =~ /0/
!ll&&!rl&&r
080
MC/DC0%0/2
$p != 0 and $str =~ /0/
$p != 0 $str =~ /0/
Truth table33%1/3
A and B
A= $p != 0B= $str =~ /0/
ABresult
0X0
100
111
186 0 $str = _increment_fingering($str);
187 }
188 }
189 ▶ elsif ($p > 1 && $str =~ /0/) {
Condition66%2/3
$p > 1 and $str =~ /0/
!ll&&!rl&&r
402
MC/DC50%1/2
$p > 1 and $str =~ /0/
$p > 1 $str =~ /0/
Truth table66%2/3
A and B
A= $p > 1B= $str =~ /0/
ABresult
0X0
100
111
190 2 $str = _increment_fingering($str);
191 2 $p--;
192 }
193  
194 18 return $str, $p;
195 }
196  
197  
198 2 ▶sub _increment_fingering {
Subroutine _increment_fingering: called CC=3 SCAR=11.0
199 2 my ($fingering) = @_;
200 2 my $incremented = '';
201 2 for my $char (split //, $fingering) {
202 12 ▶ $incremented .= $char =~ /\d/ ? $char + 1 : $char;
Branch: 1 branch
BranchTrueFalseTotal
$char =~ /\d/ ? : 10 2 12
203 }
204 2 return $incremented;
205 }
206  
207 4 ▶sub _decrement_fingering {
Subroutine _decrement_fingering: called CC=3 SCAR=11.0
208 4 my ($fingering) = @_;
209 4 my $decremented = '';
210 4 for my $char (split //, $fingering) {
211 24 ▶ $decremented .= $char =~ /\d/ ? $char - 1 : $char;
Branch: 1 branch
BranchTrueFalseTotal
$char =~ /\d/ ? : 20 4 24
212 }
213 4 return $decremented;
214 }
215  
216 2 1;
217  
218 __END__
↑ Summary