PlayTab/LyChord.pm

filter: Statementstmt 90.5% 125 / 138 Branchbran 76.7% 43 / 56 Conditioncond 57.1% 8 / 14 MC/DCmcdc 44.4% 4 / 9 Subroutinesub 100% 6 / 6 Podpod 0.0% 0 / 1 totaltotal 83.0% 186 / 224 CC 38 SCAR 36.8
CC
38
Cov
85%
parse
44.8
BEGIN
1.0
BEGIN
1.0
CRAP 39.7
« PlayTab/Output.pm ↑ Summary PlayTab/Chord.pm »
1 #! perl
2  
3 # Author : Johan Vromans
4 # Created On : Tue Jan 15 15:59:16 2008
5 # Last Modified By: Johan Vromans
6 # Last Modified On: Mon Mar 9 10:59:50 2015
7 # Update Count : 24
8 # Status : Unknown, Use with caution!
9  
10 package App::Music::PlayTab::LyChord;
11  
12 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 3 our $VERSION = "1.006";
16  
17 3 ▶use App::Music::PlayTab::Note;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶use base qw(App::Music::PlayTab::Chord);
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 132 ▶sub parse {
Subroutine parse: called CC=38 SCAR=38.0
Pod undocumented
22 132 my ($self, $chord) = @_;
23  
24 132 ▶ $self = $self->new unless ref($self);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 132 132
25  
26 132 $self->{_unparsed} = $chord;
27 132 ▶ $self->{_debug} = 1 if $chord =~ s/^\?//;
Branch: 1 branch
BranchTrueFalseTotal
if $chord =~ s/^\?// 0 132 132
28 132 $self->{_isrest} = 0;
29 132 delete( $self->{bass} );
30  
31 132 my $key = $chord;
32 132 my $mod = '';
33  
34 # Catch rests.
35 132 ▶ if ( $chord =~ /(^[rs])(\d+\.*)?/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chord =~ /(^[rs])(\d+\.*)?/) 7 125 132
36 7 $self->{_isrest} = 1;
37 7 ▶ if ( defined $2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $2) 6 1 7
38 6 my ($dur, $xt) = $2 =~ /^(\d+)(\.*)$/;
39 6 $dur = $self->duration_base / $dur;
40 6 $dur *= 1.5 foreach split(//, $xt);
41 6 $self->{duration} = $dur;
42 }
43 7 return $self;
44 }
45  
46 # Treat power chords as modifications.
47 125 $chord =~ s;^([[a-g](?:es|is)?(\d+\.*)?)/;$1:/;;
48  
49 # Separate the chord key from the modifications.
50 125 ▶ if ( $chord =~ /(^[a-g](?:es|is)?)(\d+\.*)?(?::(.*))?/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chord =~ /(^[a-g](?:es|is)?)(\d+\.*)?(?::(.*))?/) 125 0 125
51 125 $key = $1;
52 125 $mod = $3;
53 125 ▶ if ( defined $2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $2) 75 50 125
54 75 my ($dur, $xt) = $2 =~ /^(\d+)(\.*)$/;
55 75 $dur = $self->duration_base / $dur;
56 75 $dur *= 1.5 foreach split(//, $xt);
57 75 $self->{duration} = $dur;
58 }
59 }
60  
61 # Parse key.
62 125 delete $self->{key};
63 125 eval { $self->{key} = App::Music::PlayTab::Note->parse($key) };
64 croak("Unrecognized pitch in chord: ".$self->{_unparsed})
65 125 ▶ unless defined $self->{key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"key"} 0 125 125
66  
67 # Encodings: a bit is set in $chflags for every note in the chord.
68 # The corresponding element of $chmods is 0 (natural), -1
69 # (lowered), 1 (raised) or undef (suppressed).
70  
71 125 my $chflags = '';
72 125 my @chmods = (0) x 14;
73  
74 # Assume major triad.
75 125 vec($chflags,3,1) = 1;
76 125 vec($chflags,5,1) = 1;
77 125 $chmods[3] = 0;
78 125 $chmods[5] = 0;
79  
80 # Then other modifications.
81 125 ▶ while ( $mod && $mod ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mod and $mod ne "") 58 125 183
Condition66%2/3
$mod and $mod ne ""
!ll&&!rl&&r
125058
MC/DC50%1/2
$mod and $mod ne ""
$mod $mod ne ""
Truth table66%2/3
A and B
A= $modB= $mod ne ""
ABresult
0X0
100
111
82  
83 58 ▶ if ( $mod =~ /^maj7?(?:\.?(.*))?/ ) { # Maj7
Branch: 1 branch
BranchTrueFalseTotal
if ($mod =~ /^maj7?(?:\.?(.*))?/) 3 55 58
84 3 $mod = $+;
85 3 vec($chflags,7,1) = 1;
86 3 $chmods[7] = 1;
87 3 next;
88 }
89 55 ▶ if ( $mod =~ /^m(?:in)?7(?:\.?(.*))?/ ) { # Minor triad 7
Branch: 1 branch
BranchTrueFalseTotal
if ($mod =~ /^m(?:in)?7(?:\.?(.*))?/) 1 54 55
90 1 $mod = $+;
91 1 vec($chflags,7,1) = 1;
92 1 $chmods[7] = 0;
93 1 vec($chflags,3,1) = 1;
94 1 $chmods[3] = -1;
95 1 next;
96 }
97 54 ▶ if ( $mod =~ /^m(?:\.?(.*))?/ ) { # Minor triad
Branch: 1 branch
BranchTrueFalseTotal
if ($mod =~ /^m(?:\.?(.*))?/) 36 18 54
98 36 $mod = $+;
99 36 vec($chflags,3,1) = 1;
100 36 $chmods[3] = -1;
101 36 next;
102 }
103  
104 # Transform 7sus4 into something we can parse.
105 18 $mod =~ s/^(\d+)sus(\d?)/sus$2.$1/;
106  
107 18 ▶ if ( $mod =~ /^sus2(?:\.(.*))?/ ) { # Suspended second
Branch: 1 branch
BranchTrueFalseTotal
if ($mod =~ /^sus2(?:\.(.*))?/) 1 17 18
108 1 $mod = $+;
109 1 vec($chflags,3,1) = 0;
110 1 undef $chmods[3];
111 1 next;
112 }
113 17 ▶ if ( $mod =~ /^sus4?(?:\.(.*))?/ ) { # Suspended fourth
Branch: 1 branch
BranchTrueFalseTotal
if ($mod =~ /^sus4?(?:\.(.*))?/) 3 14 17
114 3 $mod = $+;
115 3 vec($chflags,4,1) = 1; # does it?
116 3 undef $chmods[3];
117 3 $chmods[4] = 0;
118 3 next;
119 }
120 14 ▶ if ( $mod =~ /^aug(?:\.?(.*))?/ ) { # Augmented
Branch: 1 branch
BranchTrueFalseTotal
if ($mod =~ /^aug(?:\.?(.*))?/) 1 13 14
121 1 $mod = $+;
122 1 vec($chflags,5,1) = 1;
123 1 $chmods[5] = 1;
124 1 next;
125 }
126 13 ▶ if ( $mod =~ /^dim(?:\.?(.*))?/ ) { # Diminished
Branch: 1 branch
BranchTrueFalseTotal
if ($mod =~ /^dim(?:\.?(.*))?/) 1 12 13
127 1 $mod = $+;
128 1 vec($chflags,3,1) = 1;
129 1 vec($chflags,5,1) = 1;
130 1 vec($chflags,7,1) = 1;
131 1 $chmods[3] = -1;
132 1 $chmods[5] = -1;
133 1 $chmods[7] = -1;
134 1 next;
135 }
136 # if ( $mod =~ /^%(.*)/ ) { # half-diminished 7
137 # $mod = $+;
138 # $chflags = '';
139 # vec($chflags,3,1) = 1;
140 # vec($chflags,5,1) = 1;
141 # vec($chflags,7,1) = 1;
142 # $chmods[3] = -1;
143 # $chmods[5] = -1;
144 # $chmods[7] = 0;
145 # next;
146 # }
147 12 ▶ if ( $mod =~ /^(2|5|6|7|9|10|11|13)([-+])?\.?(.*)?/ ) { # addition
Branch: 1 branch
BranchTrueFalseTotal
if ($mod =~ /^(2|5|6|7|9|10|11|13)([-+])?\.?(.*)?/) 10 2 12
148 10 $mod = $3;
149 # 13th implies 11th implies 9th implies 7th...
150 10 ▶ if ( $1 > 7 && !(vec($chflags,7,1)) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 > 7 and not vec($chflags, 7, 1)) 5 5 10
Condition66%2/3
$1 > 7 and not vec($chflags, 7, 1)
!ll&&!rl&&r
505
MC/DC50%1/2
$1 > 7 and not vec($chflags, 7, 1)
$1 > 7 not vec($chflags, 7, 1)
Truth table66%2/3
A and not(B)
A= $1 > 7B= not vec($chflags, 7, 1)
ABresult
0X0
100
111
151 5 vec($chflags,7,1) = 1;
152 5 $chmods[7] = 0;
153 }
154 10 ▶ if ( $1 > 10 && !(vec($chflags,9,1)) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 > 10 and not vec($chflags, 9, 1)) 3 7 10
Condition66%2/3
$1 > 10 and not vec($chflags, 9, 1)
!ll&&!rl&&r
703
MC/DC50%1/2
$1 > 10 and not vec($chflags, 9, 1)
$1 > 10 not vec($chflags, 9, 1)
Truth table66%2/3
A and not(B)
A= $1 > 10B= not vec($chflags, 9, 1)
ABresult
0X0
100
111
155 3 vec($chflags,9,1) = 1;
156 3 $chmods[9] = 0;
157 }
158 10 ▶ if ( $1 > 11 && !(vec($chflags,11,1)) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 > 11 and not vec($chflags, 11, 1)) 2 8 10
Condition66%2/3
$1 > 11 and not vec($chflags, 11, 1)
!ll&&!rl&&r
802
MC/DC50%1/2
$1 > 11 and not vec($chflags, 11, 1)
$1 > 11 not vec($chflags, 11, 1)
Truth table66%2/3
A and not(B)
A= $1 > 11B= not vec($chflags, 11, 1)
ABresult
0X0
100
111
159 2 vec($chflags,11,1) = 1;
160 2 $chmods[11] = 1;
161 }
162 10 vec($chflags,$1,1) = 1;
163 10 $chmods[$1] = 0;
164 10 ▶ if ( defined $2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $2) 1 9 10
165 1 ▶ $chmods[$1] = ($2 eq '+') ? 1 : -1;
Branch: 1 branch
BranchTrueFalseTotal
$2 eq '+' ? : 0 1 1
166 }
167 10 next;
168 }
169 2 ▶ if ( $mod =~ /^\^(\d+)(?:\.(.*))?/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mod =~ /^\^(\d+)(?:\.(.*))?/) 2 0 2
170 2 $mod = $2;
171 2 vec($chflags,$1,1) = 1;
172 2 undef $chmods[$1];
173 2 next;
174 }
175  
176 # Power chords.
177 0 ▶ if ( $mod =~ /^\/(.+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mod =~ m[^/(.+)]) 0 0 0
178 0 my @ch = split(/\//, $1);
179 0 foreach my $c ( @ch ) {
180 # my $p = eval { App::Music::PlayTab::Note->parse($c) };
181 0 my $p = eval { App::Music::PlayTab::Chord->parse($c) };
182 croak("Unrecognized bass of chord: ".$self->{_unparsed})
183 0 ▶ unless defined $p;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $p 0 0 0
184 0 ▶ $self->{bass} ||= [];
Condition0%0/2
$self->{"bass"} ||= []
l!l
00
MC/DC0%0/1
$self->{"bass"} ||= []
$self->{"bass"}
Truth table0%0/2
A
A= $self->{"bass"}
Aresult
11
00
185 0 push(@{$self->{bass}}, $p);
186 }
187 0 last;
188 }
189 0 croak("Unrecognized modification of chord: ".$self->{_unparsed});
190 }
191  
192 125 my @vec = (0);
193 125 for ( 1..13 ) {
194 1625 ▶ next unless vec($chflags,$_,1);
Branch: 1 branch
BranchTrueFalseTotal
unless vec $chflags, $_, 1 1348 277 1625
195 277 ▶ next unless defined $chmods[$_];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $chmods[$_] 5 272 277
196 272 push (@vec, (0,0,2,4,5,7,9,10,12,14,16,17,19,21)[$_]+$chmods[$_]);
197 }
198  
199 125 $self->{vec} = [@vec];
200  
201 warn("=> Chord ", $self->{_unparsed}, ": ", $self->{key}->key,
202 " (", $self->{key}->name, ") [ @vec ]",
203 $self->{bass}
204 0 ? (" {" . join("/", map { $_->{key}->key } @{$self->{bass}}) . "}")
205 : "",
206 "\n")
207 125 ▶ if $self->{_debug};
Branch: 2 branches
BranchTrueFalseTotal
if $self->{"_debug"} 0 125 125
$self->{'bass'} ? : 0 0 0
208  
209 125 $self;
210 }
211  
212 3 1;
213  
214 __END__
« PlayTab/Output.pm ↑ Summary PlayTab/Chord.pm »