blib/lib/Music/Cadence.pm

filter: Statementstmt 100% 173 / 173 Branchbran 82.2% 74 / 90 Conditioncond 78.9% 30 / 38 MC/DCmcdc 50.0% 12 / 24 Subroutinesub 100% 20 / 20 Podpod 100% 1 / 1 totaltotal 89.6% 310 / 346 CC 62 SCAR 34.7
CC
62
Cov
92%
cadence
51.9
_generate_chord
11.0
_invert_chord
6.0
CRAP 32.3
↑ Summary
1 package Music::Cadence;
2 3 our $AUTHORITY = 'cpan:GENE';
3  
4 # ABSTRACT: Generate musical cadence chords
5  
6 3 our $VERSION = '0.1512';
7  
8 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Data::Dumper::Compact qw(ddc);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use List::Util qw(any);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Music::Chord::Note ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Music::Chord::Positions ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use Music::Note ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use Music::Scales qw(get_scale_notes);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use Music::ToRoman ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 3 with('Music::PitchNum');
20  
21  
22 has verbose => (
23 is => 'ro',
24 23 ▶ default => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
25 3 );
26  
27  
28 has key => (
29 is => 'ro',
30 21 ▶ default => sub { 'C' },
Subroutine __ANON__: called CC=1 SCAR=0.0
31 3 );
32  
33  
34 has scale => (
35 is => 'ro',
36 23 ▶ default => sub { 'major' },
Subroutine __ANON__: called CC=1 SCAR=0.0
37 3 );
38  
39  
40 has octave => (
41 is => 'ro',
42 17 ▶ default => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
43 3 );
44  
45  
46 has format => (
47 is => 'ro',
48 13 ▶ default => sub { 'isobase' },
Subroutine __ANON__: called CC=1 SCAR=0.0
49 3 );
50  
51  
52 has seven => (
53 is => 'ro',
54 14 ▶ default => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
55 3 );
56  
57  
58 has picardy => (
59 is => 'ro',
60 23 ▶ default => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
61 3 );
62  
63  
64 60 ▶sub cadence {
Subroutine cadence: called CC=47 SCAR=39.5
Pod documented
65 60 my ( $self, %args ) = @_;
66  
67 60 my $cadence = [];
68  
69 60 ▶ my $key = $args{key} || $self->key;
Condition66%2/3
$args{'key'} || $self->key
l!l&&r!l&&!r
19410
MC/DC0%0/2
$args{'key'} || $self->key
$args{'key'} $self->key
Truth table66%2/3
A || B
A= $args{'key'}B= $self->key
ABresult
1X1
011
000
70 60 ▶ my $scale = $args{scale} || $self->scale;
Condition66%2/3
$args{'scale'} || $self->scale
l!l&&r!l&&!r
6540
MC/DC0%0/2
$args{'scale'} || $self->scale
$args{'scale'} $self->scale
Truth table66%2/3
A || B
A= $args{'scale'}B= $self->scale
ABresult
1X1
011
000
71 60 ▶ my $octave = $args{octave} // $self->octave;
Condition66%2/3
$args{"octave"} // $self->octave
l!l&&r!l&&!r
13470
MC/DC0%0/2
$args{"octave"} // $self->octave
$args{"octave"} $self->octave
Truth table66%2/3
A // B
A= $args{"octave"}B= $self->octave
ABresult
1X1
011
000
72 60 ▶ my $picardy = $args{picardy} || $self->picardy;
Condition66%2/3
$args{'picardy'} || $self->picardy
l!l&&r!l&&!r
3057
MC/DC50%1/2
$args{'picardy'} || $self->picardy
$args{'picardy'} $self->picardy
Truth table66%2/3
A || B
A= $args{'picardy'}B= $self->picardy
ABresult
1X1
011
000
73 60 ▶ my $type = $args{type} || 'perfect';
Condition100%2/2
$args{'type'} || 'perfect'
l!l
4416
MC/DC100%1/1
$args{'type'} || 'perfect'
$args{'type'}
Truth table100%2/2
A
A= $args{'type'}
Aresult
11
00
74 60 ▶ my $leading = $args{leading} || 1;
Condition100%2/2
$args{'leading'} || 1
l!l
1446
MC/DC100%1/1
$args{'leading'} || 1
$args{'leading'}
Truth table100%2/2
A
A= $args{'leading'}
Aresult
11
00
75 60 ▶ my $variation = $args{variation} || 1;
Condition100%2/2
$args{'variation'} || 1
l!l
456
MC/DC100%1/1
$args{'variation'} || 1
$args{'variation'}
Truth table100%2/2
A
A= $args{'variation'}
Aresult
11
00
76 60 ▶ my $inversion = $args{inversion} || 0;
Condition100%2/2
$args{'inversion'} || 0
l!l
1941
MC/DC100%1/1
$args{'inversion'} || 0
$args{'inversion'}
Truth table100%2/2
A
A= $args{'inversion'}
Aresult
11
00
77  
78 60 ▶ die 'unknown leader' if $leading < 1 or $leading > 7;
Branch: 1 branch
BranchTrueFalseTotal
if $leading < 1 or $leading > 7 1 59 60
Condition66%2/3
$leading < 1 or $leading > 7
l!l&&r!l&&!r
0159
MC/DC50%1/2
$leading < 1 or $leading > 7
$leading < 1 $leading > 7
Truth table66%2/3
A or B
A= $leading < 1B= $leading > 7
ABresult
1X1
011
000
79  
80 59 my @scale_notes = get_scale_notes( $key, $scale );
81  
82 59 ▶ if ( $type eq 'perfect' ) {
Branch: 7 branches
BranchTrueFalseTotal
if ($type eq 'perfect') { } 15 44 59
elsif ($type eq 'imperfect' and $inversion) { } 16 28 44
elsif ($type eq 'imperfect') { } 4 24 28
elsif ($type eq 'evaded' and $self->seven) { } 2 22 24
elsif ($type eq 'plagal') { } 4 18 22
elsif ($type eq 'half') { } 13 5 18
elsif ($type eq 'deceptive') { } 4 1 5
83 15 my $chord = $self->_generate_chord( $key, $scale, $scale_notes[4], $octave );
84 12 push @$cadence, $chord;
85  
86 12 $chord = $self->_generate_chord( $key, $scale, $scale_notes[0], $octave );
87 # Add another top note, but an octave above
88 12 my $top = $chord->[0];
89 12 ▶ if ( $self->format eq 'midinum' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->format eq 'midinum') { } 6 6 12
90 6 $top += 12;
91 }
92 else {
93 6 ▶ if ( $top =~ /^(.+?)(\d+)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($top =~ /^(.+?)(\d+)$/) 4 2 6
94 4 my $note = $1;
95 4 my $octave = $2;
96 4 $top = $note . ++$octave;
97 }
98 }
99 12 ▶ print ddc($top) if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 12 12
100 12 push @$chord, $top;
101 12 ▶ print ddc($chord) if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 12 12
102 12 push @$cadence, $chord;
103 }
104 ▶ elsif ( $type eq 'imperfect' && $inversion ) {
Condition100%3/3
$type eq "imperfect" and $inversion
!ll&&!rl&&r
24416
MC/DC100%2/2
$type eq "imperfect" and $inversion
$type eq "imperfect" $inversion
Truth table100%3/3
A and B
A= $type eq "imperfect"B= $inversion
ABresult
0X0
100
111
105 16 my $chord = $self->_generate_chord( $key, $scale, $scale_notes[4], $octave );
106 $chord = $self->_invert_chord( $chord, $inversion->{1}, $octave )
107 16 ▶ if $inversion->{1};
Branch: 1 branch
BranchTrueFalseTotal
if $inversion->{1} 16 0 16
108 16 push @$cadence, $chord;
109  
110 16 $chord = $self->_generate_chord( $key, $scale, $scale_notes[0], $octave );
111 $chord = $self->_invert_chord( $chord, $inversion->{2}, $octave )
112 16 ▶ if $inversion->{2};
Branch: 1 branch
BranchTrueFalseTotal
if $inversion->{2} 10 6 16
113 16 push @$cadence, $chord;
114 }
115 elsif ( $type eq 'imperfect' ) {
116 4 ▶ my $note = $variation == 1 ? $scale_notes[4] : $scale_notes[6];
Branch: 1 branch
BranchTrueFalseTotal
$variation == 1 ? : 2 2 4
117 4 my $chord = $self->_generate_chord( $key, $scale, $note, $octave );
118 4 ▶ print ddc($chord) if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 4 4
119 4 push @$cadence, $chord;
120  
121 4 $chord = $self->_generate_chord( $key, $scale, $scale_notes[0], $octave );
122 4 ▶ print ddc($chord) if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 4 4
123 4 push @$cadence, $chord;
124 }
125 ▶ elsif ( $type eq 'evaded' && $self->seven ) {
Condition66%2/3
$type eq "evaded" and $self->seven
!ll&&!rl&&r
2202
MC/DC50%1/2
$type eq "evaded" and $self->seven
$type eq "evaded" $self->seven
Truth table66%2/3
A and B
A= $type eq "evaded"B= $self->seven
ABresult
0X0
100
111
126 2 ▶ if ( $inversion ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inversion) { } 1 1 2
127 $inversion->{1} = 3
128 1 ▶ unless defined $inversion->{1};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $inversion->{1} 0 1 1
129 $inversion->{2} = 1
130 1 ▶ unless defined $inversion->{2};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $inversion->{2} 0 1 1
131 }
132 else {
133 1 $inversion = { 1 => 3, 2 => 1 };
134 }
135  
136 2 my $chord = $self->_generate_chord( $key, $scale, $scale_notes[4], $octave );
137 2 $chord = $self->_invert_chord( $chord, $inversion->{1}, $octave );
138 2 ▶ print ddc($chord) if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 2 2
139 2 push @$cadence, $chord;
140  
141 2 $chord = $self->_generate_chord( $key, $scale, $scale_notes[0], $octave );
142 2 $chord = $self->_invert_chord( $chord, $inversion->{2}, $octave );
143 2 ▶ print ddc($chord) if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 2 2
144 2 push @$cadence, $chord;
145 }
146 elsif ( $type eq 'plagal' ) {
147 4 my $chord = $self->_generate_chord( $key, $scale, $scale_notes[3], $octave );
148 4 ▶ print ddc($chord) if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 4 4
149 4 push @$cadence, $chord;
150  
151 4 $chord = $self->_generate_chord( $key, $scale, $scale_notes[0], $octave );
152 4 ▶ print ddc($chord) if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 4 4
153 4 push @$cadence, $chord;
154 }
155 elsif ( $type eq 'half' ) {
156 13 my $chord = $self->_generate_chord( $key, $scale, $scale_notes[ $leading - 1 ], $octave );
157 $chord = $self->_invert_chord( $chord, $inversion->{1}, $octave )
158 13 ▶ if $inversion && $inversion->{1};
Branch: 1 branch
BranchTrueFalseTotal
if $inversion and $inversion->{1} 2 11 13
Condition66%2/3
$inversion and $inversion->{1}
!ll&&!rl&&r
1102
MC/DC50%1/2
$inversion and $inversion->{1}
$inversion $inversion->{1}
Truth table66%2/3
A and B
A= $inversionB= $inversion->{1}
ABresult
0X0
100
111
159 13 ▶ print ddc($chord) if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 13 13
160 13 push @$cadence, $chord;
161  
162 13 $chord = $self->_generate_chord( $key, $scale, $scale_notes[4], $octave );
163 $chord = $self->_invert_chord( $chord, $inversion->{2}, $octave )
164 13 ▶ if $inversion && $inversion->{2};
Branch: 1 branch
BranchTrueFalseTotal
if $inversion and $inversion->{2} 0 13 13
Condition66%2/3
$inversion and $inversion->{2}
!ll&&!rl&&r
1120
MC/DC0%0/2
$inversion and $inversion->{2}
$inversion $inversion->{2}
Truth table66%2/3
A and B
A= $inversionB= $inversion->{2}
ABresult
0X0
100
111
165 13 ▶ print ddc($chord) if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 13 13
166 13 push @$cadence, $chord;
167 }
168 elsif ( $type eq 'deceptive' ) {
169 4 my $chord = $self->_generate_chord( $key, $scale, $scale_notes[4], $octave );
170 4 ▶ print ddc($chord) if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 4 4
171 4 push @$cadence, $chord;
172  
173 4 ▶ my $note = $variation == 1 ? $scale_notes[5] : $scale_notes[3];
Branch: 1 branch
BranchTrueFalseTotal
$variation == 1 ? : 2 2 4
174 4 $chord = $self->_generate_chord( $key, $scale, $note, $octave );
175 4 ▶ print ddc($chord) if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 4 4
176 4 push @$cadence, $chord;
177 }
178 else {
179 1 die 'unknown cadence';
180 }
181  
182 55 ▶ if ( $picardy ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($picardy) 3 52 55
183 3 ▶ if ( $self->format eq 'midinum' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->format eq 'midinum') { } 1 2 3
184 1 $cadence->[1][1]++;
185 }
186 else {
187 2 my $note = Music::Note->new( $cadence->[1][1], $self->format );
188 2 my $num = $note->format('midinum');
189 2 $num++;
190 2 $note = Music::Note->new( $num, 'midinum' );
191 2 $cadence->[1][1] = $note->format( $self->format );
192 }
193 }
194  
195 55 return $cadence;
196 }
197  
198 32 ▶sub _invert_chord {
Subroutine _invert_chord: called CC=6 SCAR=17.9
199 32 my ( $self, $chord, $inversion, $octave ) = @_;
200  
201 32 my $mcp = Music::Chord::Positions->new;
202  
203 32 ▶ if ( $self->format eq 'midinum' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->format eq 'midinum') { } 2 30 32
204 2 $chord = $mcp->chord_inv( $chord, inv_num => $inversion );
205 }
206 else { # Perform these gymnastics to convert named notes to inverted named notes:
207 # Strip the octave if present
208 30 ▶ $chord = [ map { s/\d+//; $_ } @$chord ]
Branch: 1 branch
BranchTrueFalseTotal
if $octave 18 12 30
209 if $octave;
210  
211 # Convert the chord into pitch-class representation
212 30 my $pitches = [ map { $self->pitchnum( $_ . -1 ) } @$chord ];
213  
214 # Do the inversion!
215 30 $pitches = $mcp->chord_inv( $pitches, inv_num => $inversion );
216  
217 # Convert the pitch-classes back to named notes
218 30 $chord = [ map { $self->pitchname($_) } @$pitches ];
219  
220 # Clean-up the chord
221 30 for ( @$chord ) {
222 106 ▶ if ( $octave ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($octave) { } 64 42 106
223 64 s/-1/$octave/;
224 64 s/0/$octave + 1/e;
225 }
226 else {
227 42 s/-1//;
228 42 s/0//;
229 }
230  
231 106 ▶ if ( $self->format eq 'midi' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->format eq "midi") 12 94 106
232 12 s/#/s/;
233 12 s/b/f/;
234 }
235 }
236 }
237  
238 32 return $chord;
239 }
240  
241 113 ▶sub _generate_chord {
Subroutine _generate_chord: called CC=11 SCAR=24.0
242 113 my ( $self, $key, $scale, $note, $octave ) = @_;
243  
244 # Know what chords should be diminished
245 113 my %diminished = (
246 ionian => 'vii',
247 major => 'vii',
248 dorian => 'vi',
249 phrygian => 'v',
250 lydian => 'iv',
251 mixolydian => 'iii',
252 aeolian => 'ii',
253 minor => 'ii',
254 locrian => 'i',
255 );
256  
257 113 ▶ die 'unknown scale' unless exists $diminished{$scale};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $diminished{$scale} 1 112 113
258  
259 112 my $mtr = Music::ToRoman->new(
260 scale_note => $key,
261 scale_name => $scale,
262 chords => 0,
263 );
264  
265 # Figure out if the chord is diminished, minor, or major
266 111 my $roman = $mtr->parse($note);
267 111 ▶ my $type = $roman =~ /^$diminished{$scale}$/ ? 'dim' : $roman =~ /^[a-z]/ ? 'm' : '';
Branch: 2 branches
BranchTrueFalseTotal
$roman =~ /^$diminished{$scale}$/ ? : 11 100 111
$roman =~ /^[a-z]/ ? : 11 89 100
268  
269 111 ▶ $type .= 7
Branch: 1 branch
BranchTrueFalseTotal
if $self->seven 54 57 111
270 if $self->seven;
271  
272 111 my $mcn = Music::Chord::Note->new;
273  
274 # Get the notes of the chord (without an octave)
275 111 my @notes = $mcn->chord( $note . $type );
276  
277 111 ▶ if ( $self->format eq 'midi' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->format eq 'midi') { } 8 103 111
elsif ($self->format eq 'midinum') { } 14 89 103
elsif ($self->format ne 'isobase') { } 1 88 89
278 # Convert the sharps and flats
279 8 for ( @notes ) {
280 26 s/#/s/;
281 26 s/b/f/;
282 }
283 }
284 elsif ( $self->format eq 'midinum' ) {
285 # Convert the notes to midinum format
286 14 @notes = map { $self->pitchnum( $_ . $octave ) } @notes;
287 }
288 elsif ( $self->format ne 'isobase' ) {
289 1 die 'unknown format';
290 }
291  
292 # Append the octave if defined and the format is not midinum
293 110 ▶ @notes = map { $_ . $octave } @notes
Branch: 1 branch
BranchTrueFalseTotal
if $octave and $self->format ne "midinum" 26 84 110
Condition100%3/3
$octave and $self->format ne "midinum"
!ll&&!rl&&r
741026
MC/DC100%2/2
$octave and $self->format ne "midinum"
$octave $self->format ne "midinum"
Truth table100%3/3
A and B
A= $octaveB= $self->format ne "midinum"
ABresult
0X0
100
111
294 if $octave && $self->format ne 'midinum';
295  
296 110 return \@notes;
297 }
298  
299 3 1;
300  
301 __END__
↑ Summary