PlayTab/Chord.pm

filter: Statementstmt 96.3% 210 / 218 Branchbran 86.4% 83 / 96 Conditioncond 69.2% 9 / 13 MC/DCmcdc 50.0% 4 / 8 Subroutinesub 91.6% 11 / 12 Podpod 0.0% 0 / 8 totaltotal 89.3% 317 / 355 CC 60 SCAR 32.1
CC
60
Cov
92%
parse
36.3
name
20.1
transpose
7.9
CRAP 24.9
« PlayTab/LyChord.pm ↑ Summary PlayTab/Output/PostScript.pm »
1 #! perl
2  
3 # Author : Johan Vromans
4 # Created On : Wed Aug 22 22:33:31 2007
5 # Last Modified By: Johan Vromans
6 # Last Modified On: Sun Jan 18 15:51:41 2015
7 # Update Count : 31
8 # Status : Unknown, Use with caution!
9  
10 package App::Music::PlayTab::Chord;
11  
12 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 6 our $VERSION = "1.010";
16  
17 6 ▶use App::Music::PlayTab::Note;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 240 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
21 240 my $pkg = shift;
22 240 bless {}, $pkg;
23 }
24  
25 343 ▶sub parse {
Subroutine parse: called CC=36 SCAR=35.9
Pod undocumented
26 343 my ($self, $chord) = @_;
27  
28 343 ▶ $self = $self->new unless ref($self);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 343 343
29 343 delete $self->{bass};
30 343 delete $self->{high};
31 343 $self->{_unparsed} = $chord;
32 343 $chord = lc($chord);
33 343 $self->{_debug} = $chord =~ s/^\?//;
34 343 my $key = $chord;
35 343 my $mod = '';
36  
37 # Separate the chord key from the modifications.
38 343 ▶ if ( $chord =~ /(^[a-g*](\#|b|s(?!us)|es|is)?)(.*)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chord =~ /(^[a-g*](\#|b|s(?!us)|es|is)?)(.*)/) 343 0 343
39 343 $key = $1;
40 343 $mod = $3;
41 }
42  
43 # Parse key.
44 343 eval { $self->{key} = App::Music::PlayTab::Note->parse($key) };
45 croak("Unrecognized pitch in chord: ".$self->{_unparsed})
46 343 ▶ unless defined $self->{key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"key"} 0 343 343
47  
48 # Encodings: a bit is set in $chflags for every note in the chord.
49 # The corresponding element of $chmods is 0 (natural), -1
50 # (lowered), 1 (raised) or undef (suppressed).
51  
52 343 my $chflags = '';
53 343 my @chmods = (0) x 14;
54  
55 # Assume major triad.
56 343 vec($chflags,3,1) = 1;
57 343 vec($chflags,5,1) = 1;
58 343 $chmods[3] = 0;
59 343 $chmods[5] = 0;
60  
61 343 $mod =~ s/^-/min/; # Minor triad
62 343 $mod =~ s/^\+/aug/; # Augmented triad
63 343 $mod =~ s/^0/dim/; # Diminished
64  
65 # Then other modifications.
66 343 ▶ while ( $mod ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mod ne "") 99 339 438
67  
68 99 ▶ if ( $mod =~ /^[() ._](.*)/ ) { # syntactic sugar
Branch: 1 branch
BranchTrueFalseTotal
if ($mod =~ /^[() ._](.*)/) 11 88 99
69 11 $mod = $+;
70 11 next;
71 }
72 88 ▶ if ( $mod =~ /^maj7?(.*)/ ) { # Maj7
Branch: 1 branch
BranchTrueFalseTotal
if ($mod =~ /^maj7?(.*)/) 8 80 88
73 8 $mod = $+;
74 8 vec($chflags,7,1) = 1;
75 8 $chmods[7] = 1;
76 8 next;
77 }
78 80 ▶ if ( $mod =~ /^(min|m)7(.*)/ ) { # Minor triad + 7
Branch: 1 branch
BranchTrueFalseTotal
if ($mod =~ /^(min|m)7(.*)/) 7 73 80
79 7 $mod = $+;
80 7 vec($chflags,7,1) = 1;
81 7 $chmods[7] = 0;
82 7 vec($chflags,3,1) = 1;
83 7 $chmods[3] = -1;
84 7 next;
85 }
86 73 ▶ if ( $mod =~ /^(min|m)(.*)/ ) { # Minor triad
Branch: 1 branch
BranchTrueFalseTotal
if ($mod =~ /^(min|m)(.*)/) 12 61 73
87 12 $mod = $+;
88 12 vec($chflags,3,1) = 1;
89 12 $chmods[3] = -1;
90 12 next;
91 }
92 61 ▶ if ( $mod =~ /^sus2(.*)/ ) { # Suspended second
Branch: 1 branch
BranchTrueFalseTotal
if ($mod =~ /^sus2(.*)/) 1 60 61
93 1 $mod = $+;
94 1 vec($chflags,3,1) = 0;
95 1 undef $chmods[3];
96 1 next;
97 }
98 60 ▶ if ( $mod =~ /^sus4?(.*)/ ) { # Suspended fourth
Branch: 1 branch
BranchTrueFalseTotal
if ($mod =~ /^sus4?(.*)/) 3 57 60
99 3 $mod = $+;
100 3 vec($chflags,4,1) = 1; # does it?
101 3 undef $chmods[3];
102 3 $chmods[4] = 0;
103 3 next;
104 }
105 57 ▶ if ( $mod =~ /^aug(.*)/ ) { # Augmented
Branch: 1 branch
BranchTrueFalseTotal
if ($mod =~ /^aug(.*)/) 2 55 57
106 2 $mod = $+;
107 2 vec($chflags,5,1) = 1;
108 2 $chmods[5] = 1;
109 2 next;
110 }
111 55 ▶ if ( $mod =~ /^(o|dim)(.*)/ ) { # Diminished
Branch: 1 branch
BranchTrueFalseTotal
if ($mod =~ /^(o|dim)(.*)/) 3 52 55
112 3 $mod = $+;
113 3 vec($chflags,3,1) = 1;
114 3 vec($chflags,5,1) = 1;
115 3 vec($chflags,7,1) = 1;
116 3 $chmods[3] = -1;
117 3 $chmods[5] = -1;
118 3 $chmods[7] = -1;
119 3 next;
120 }
121 52 ▶ if ( $mod =~ /^%(.*)/ ) { # half-diminished 7
Branch: 1 branch
BranchTrueFalseTotal
if ($mod =~ /^%(.*)/) 6 46 52
122 6 $mod = $+;
123 6 $chflags = '';
124 6 vec($chflags,3,1) = 1;
125 6 vec($chflags,5,1) = 1;
126 6 vec($chflags,7,1) = 1;
127 6 $chmods[3] = -1;
128 6 $chmods[5] = -1;
129 6 $chmods[7] = 0;
130 6 next;
131 }
132 46 ▶ if ( $mod =~ /^([\#b])?(2|5|6|7|9|10|11|13)(.*)/ ) { # addition
Branch: 1 branch
BranchTrueFalseTotal
if ($mod =~ /^([\#b])?(2|5|6|7|9|10|11|13)(.*)/) 36 10 46
133 36 $mod = $+;
134 # 13th implies 11th implies 9th implies 7th...
135 36 ▶ if ( $2 > 7 && !(vec($chflags,7,1)) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($2 > 7 and not vec($chflags, 7, 1)) 14 22 36
Condition100%3/3
$2 > 7 and not vec($chflags, 7, 1)
!ll&&!rl&&r
18414
MC/DC100%2/2
$2 > 7 and not vec($chflags, 7, 1)
$2 > 7 not vec($chflags, 7, 1)
Truth table100%3/3
A and not(B)
A= $2 > 7B= not vec($chflags, 7, 1)
ABresult
0X0
100
111
136 14 vec($chflags,7,1) = 1;
137 14 $chmods[7] = 0;
138 }
139 36 ▶ if ( $2 > 10 && !(vec($chflags,9,1)) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($2 > 10 and not vec($chflags, 9, 1)) 13 23 36
Condition66%2/3
$2 > 10 and not vec($chflags, 9, 1)
!ll&&!rl&&r
23013
MC/DC50%1/2
$2 > 10 and not vec($chflags, 9, 1)
$2 > 10 not vec($chflags, 9, 1)
Truth table66%2/3
A and not(B)
A= $2 > 10B= not vec($chflags, 9, 1)
ABresult
0X0
100
111
140 13 vec($chflags,9,1) = 1;
141 13 $chmods[9] = 0;
142 }
143 36 ▶ if ( $2 > 11 && !(vec($chflags,11,1)) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($2 > 11 and not vec($chflags, 11, 1)) 8 28 36
Condition66%2/3
$2 > 11 and not vec($chflags, 11, 1)
!ll&&!rl&&r
2808
MC/DC50%1/2
$2 > 11 and not vec($chflags, 11, 1)
$2 > 11 not vec($chflags, 11, 1)
Truth table66%2/3
A and not(B)
A= $2 > 11B= not vec($chflags, 11, 1)
ABresult
0X0
100
111
144 8 vec($chflags,11,1) = 1;
145 8 $chmods[11] = 1;
146 }
147 36 vec($chflags,$2,1) = 1;
148 36 $chmods[$2] = 0;
149 36 ▶ if ( defined $1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $1) 16 20 36
150 16 ▶ $chmods[$2] = ($1 eq '#') ? 1 : -1;
Branch: 1 branch
BranchTrueFalseTotal
$1 eq '#' ? : 4 12 16
151 }
152 36 next;
153 }
154 10 ▶ if ( $mod =~ /^no\s*(\d+)(st|nd|rd|th)?(.*)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mod =~ /^no\s*(\d+)(st|nd|rd|th)?(.*)/) 6 4 10
155 6 $mod = $+;
156 6 vec($chflags,$1,1) = 1;
157 6 undef $chmods[$1];
158 6 next;
159 }
160  
161 # High add-ons.
162 4 ▶ if ( $mod =~ /^\\([^\/]+)(.*)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mod =~ m[^\\([^/]+)(.*)]) 2 2 4
163 2 my @ch = split(/\\/, $1);
164 2 $mod = $2;
165 2 foreach my $c ( @ch ) {
166 2 my $p = eval { __PACKAGE__->new->parse($c) };
167 croak("Unrecognized add of chord: ".$self->{_unparsed})
168 2 ▶ unless defined $p;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $p 0 2 2
169 2 ▶ $self->{high} ||= [];
Condition50%1/2
$self->{"high"} ||= []
l!l
02
MC/DC0%0/1
$self->{"high"} ||= []
$self->{"high"}
Truth table50%1/2
A
A= $self->{"high"}
Aresult
11
00
170 2 push(@{$self->{high}}, $p);
171 }
172 2 last;
173 }
174  
175 # Power chords.
176 2 ▶ if ( $mod =~ /^\/(.+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mod =~ m[^/(.+)]) 2 0 2
177 2 my @ch = split(/\//, $1);
178 2 foreach my $c ( @ch ) {
179 2 my $p = eval { __PACKAGE__->new->parse($c) };
180 croak("Unrecognized bass of chord: ".$self->{_unparsed})
181 2 ▶ unless defined $p;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $p 0 2 2
182 2 ▶ $self->{bass} ||= [];
Condition50%1/2
$self->{"bass"} ||= []
l!l
02
MC/DC0%0/1
$self->{"bass"} ||= []
$self->{"bass"}
Truth table50%1/2
A
A= $self->{"bass"}
Aresult
11
00
183 2 push(@{$self->{bass}}, $p);
184 }
185 2 last;
186 }
187 0 croak("Unrecognized modification of chord: ".$self->{_unparsed});
188 }
189  
190 343 my @vec = (0);
191 343 for ( 1..13 ) {
192 4459 ▶ next unless vec($chflags,$_,1);
Branch: 1 branch
BranchTrueFalseTotal
unless vec $chflags, $_, 1 3678 781 4459
193 781 ▶ next unless defined $chmods[$_];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $chmods[$_] 9 772 781
194 772 push (@vec, (0,0,2,4,5,7,9,10,12,14,16,17,19,21)[$_]+$chmods[$_]);
195 }
196  
197 343 $self->{vec} = [@vec];
198  
199 warn("=> Chord ", $self->{_unparsed}, ": ", $self->{key}->key,
200 " (", $self->{key}->name, ") [ @vec ]\n")
201 343 ▶ if $self->{_debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_debug"} 0 343 343
202  
203 # Traditional chords do not include a duration.
204 343 $self->{duration} = 0;
205  
206 343 $self;
207 }
208  
209 179 ▶sub transpose {
Subroutine transpose: called CC=6 SCAR=20.7
Pod undocumented
210 179 my ($self, $xp) = @_;
211 179 ▶ return $self unless $xp;
Branch: 1 branch
BranchTrueFalseTotal
unless $xp 6 173 179
212 173 $self->{key}->transpose($xp);
213 173 ▶ if ( $self->{bass} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"bass"}) 0 173 173
214 0 $_->transpose($xp) for @{$self->{bass}};
215 }
216 173 ▶ if ( $self->{high} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"high"}) 0 173 173
217 0 $_->transpose($xp) for @{$self->{high}};
218 }
219 173 $self;
220 }
221  
222 342 ▶sub name {
Subroutine name: called CC=20 SCAR=30.0
Pod undocumented
223 342 my ($self) = @_;
224 342 my $res = $self->{key}->name;
225  
226 342 my @v = @{$self->{vec}};
227 342 my $v = "@v ";
228 342 shift (@v);
229  
230 342 ▶ if ( $v =~ s/^0 (2 )?4 (6|7|8) / / ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($v =~ s/^0 (2 )?4 (6|7|8) / /) { } 295 47 342
elsif ($v =~ s/^0 3 6 9 / /) { } 4 43 47
elsif ($v =~ s/^0 (2 )?3 (6|7|8) / /) { } 27 16 43
231 295 ▶ $res .= $2 == 8 ? '+' : '';
Branch: 1 branch
BranchTrueFalseTotal
$2 == 8 ? : 3 292 295
232 295 ▶ $v = ' 6' . $v if $2 == 6;
Branch: 1 branch
BranchTrueFalseTotal
if $2 == 6 0 295 295
233 295 ▶ $v = ' 2' . $v if defined $1;
Branch: 1 branch
BranchTrueFalseTotal
if defined $1 0 295 295
234 }
235 elsif ( $v =~ s/^0 3 6 9 / / ) {
236 4 $res .= 'o';
237 }
238 elsif ( $v =~ s/^0 (2 )?3 (6|7|8) / / ) {
239 27 ▶ if ( $2 == 6 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($2 == 6) { } 3 24 27
240 3 ▶ $res .= ( $v =~ s/^ 10 // ) ? '%' : 'o';
Branch: 1 branch
BranchTrueFalseTotal
$v =~ s/^ 10 // ? : 2 1 3
241 }
242 else {
243 24 $res .= 'm';
244 }
245 27 ▶ $v = ' 8' . $v if $2 == 8;
Branch: 1 branch
BranchTrueFalseTotal
if $2 == 8 0 27 27
246 27 ▶ $v = ' 2' . $v if defined $1;
Branch: 1 branch
BranchTrueFalseTotal
if defined $1 2 25 27
247 }
248 342 $v =~ s/^0 5 7 / 5 7 /;
249 342 $v =~ s/ 10 14 18 (21) / $1 /; # 13
250 342 $v =~ s/ 10 14 18 (20|22) / 10 $1 /; # 7#13 7b13
251 342 $v =~ s/ 10 14 (17) / $1 /; # 11
252 342 $v =~ s/ 10 14 (18) / 10 $1 /; # 7#11
253 342 $v =~ s/ 10 (14) / $1 /; # 9
254 342 $v =~ s/ 10 (15) / 10 $1 /; # 7#9
255 342 $v =~ s/ 11 14 18 (21|22) / $1 11 /; # 13#5
256 342 $v =~ s/ 11 14 (17|18) / $1 11 /; # 11#5
257 342 $v =~ s/ 11 (14|15) / $1 11 /; # 9#5
258 342 ▶ if ( $v =~ s/ 10 / / ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($v =~ s/ 10 / /) { } 14 328 342
elsif ($v =~ s/^( \d| 10|) 11 / $1/) { } 3 325 328
259 14 $res .= '7';
260 }
261 elsif ( $v =~ s/^( \d| 10|) 11 / $1/ ) {
262 3 $res .= 'maj7';
263 }
264 342 ▶ if ( $v =~ s/ 5 7 / / ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($v =~ s/ 5 7 / /) { } 6 336 342
elsif ($v =~ s/^0 7 / /) { } 6 330 336
elsif ($v =~ s/^0 4 / /) { } 4 326 330
265 6 $res .= 'sus4';
266 }
267 elsif ( $v =~ s/^0 7 / / ) {
268 6 $res .= 'sus2';
269 }
270 elsif ( $v =~ s/^0 4 / / ) {
271 4 $res .= 'no5';
272 }
273 342 my $res1 = $res; # for debug
274  
275 342 chop ($v);
276 342 $v =~ s/^ //;
277 342 @v = split(' ', $v);
278 342 foreach ( @v ) {
279 28 $res .= '('.('1','b2','2','b3','3','4','b5','5','#5','6','7','#7','8','b9','9','#9','b11','11','#11','12','b13','13')[$_].')';
280 }
281 342 my $res0 = $res;
282 342 $res =~ s/^([^\(]*[^\d])?\((\d+)\)([^\d][^\(]*|)$/$1$2$3/;
283 342 $res =~ s/7?(6|\(6\))(9|\(9\))/6.9/;
284 342 $res =~ s/(4|\(4\))(5|\(5\))/sus4/;
285 342 $res =~ s/(1|\(1\))(5|\(5\))/sus2/;
286  
287 2 $res = join("\\", $res, map { $_->name } @{$self->{high}})
288 342 ▶ if $self->{high};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"high"} 2 340 342
289 2 $res = join("/", $res, map { $_->name } @{$self->{bass}})
290 342 ▶ if $self->{bass};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"bass"} 2 340 342
291  
292 warn("=> Chord ", $self->{_unparsed}, ": ", $self->{key}->key,
293 0 " (", $self->{key}->name, ") [ @{$self->{vec}} ] ->",
294 " $res1 [ $v ] -> $res0 -> $res\n")
295 342 ▶ if $self->{_debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_debug"} 0 342 342
296  
297 342 return $res;
298 }
299  
300 169 ▶sub duration {
Subroutine duration: called CC=1 SCAR=0.0
Pod undocumented
301 169 my ($self) = @_;
302 169 $self->{duration};
303 }
304  
305 162 ▶sub duration_base {
Subroutine duration_base: called CC=1 SCAR=0.0
Pod undocumented
306 162 my ($self) = @_;
307 162 16;
308 }
309  
310 0 ▶sub bass {
Subroutine bass: not called CC=1 SCAR=6.9
Pod undocumented
311 0 my ( $self ) = @_;
312 0 $self->{bass};
313 }
314  
315 118 ▶sub is_rest {
Subroutine is_rest: called CC=1 SCAR=0.0
Pod undocumented
316 118 shift->{_isrest};
317 }
318  
319 6 1;
320  
321 __END__
« PlayTab/LyChord.pm ↑ Summary PlayTab/Output/PostScript.pm »