blib/lib/Music/Bassline/Generator.pm

filter: Statementstmt 93.8% 182 / 194 Branchbran 77.3% 65 / 84 Conditioncond 49.4% 42 / 85 MC/DCmcdc 3.0% 1 / 33 Subroutinesub 95.1% 39 / 41 Podpod 100% 1 / 1 totaltotal 75.3% 330 / 438 CC 76 SCAR 44.9
CC
76
Cov
80%
generate
162.4
_closest
4.0
_parse_chord
2.0
CRAP 89.0
↑ Summary
1 package Music::Bassline::Generator;
2 2 our $AUTHORITY = 'cpan:GENE';
3  
4 # ABSTRACT: Generate musical basslines
5  
6 2 our $VERSION = '0.0609';
7  
8 2 ▶use 5.024;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Data::Dumper::Compact qw(ddc);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use List::SomeUtils qw(first_index);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use List::Util qw(any min uniq);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use Music::Chord::Note ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use Music::Note ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use Music::Scales qw(get_scale_notes get_scale_MIDI);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use Music::VoiceGen ();
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use Set::Array ();
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use Try::Tiny qw(try catch);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 2 ▶use constant E1 => 28; # lowest note on a bass guitar in standard tuning
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 2 with('Music::PitchNum');
27  
28  
29 has guitar => (
30 is => 'ro',
31 isa => \&_boolean,
32 17 ▶ default => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
33 2 );
34  
35  
36 has wrap => (
37 is => 'ro',
38 10 ▶ isa => sub { croak 'not valid' unless $_[0] =~ /^[0A-G][#b]?\d?$/ },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] =~ /^[0A-G][#b]?\d?$/u 1 9 10
39 8 ▶ default => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
40 2 );
41  
42  
43 has modal => (
44 is => 'ro',
45 isa => \&_boolean,
46 10 ▶ default => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
47 2 );
48  
49  
50 has chord_notes => (
51 is => 'ro',
52 isa => \&_boolean,
53 16 ▶ default => sub { 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
54 2 );
55  
56  
57 has keycenter => (
58 is => 'ro',
59 16 ▶ isa => sub { croak 'not a valid pitch' unless $_[0] =~ /^[A-G][#b]?$/ },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] =~ /^[A-G][#b]?$/u 1 15 16
60 14 ▶ default => sub { 'C' },
Subroutine __ANON__: called CC=1 SCAR=0.0
61 2 );
62  
63  
64 has intervals => (
65 is => 'ro',
66 17 ▶ isa => sub { croak 'not an array reference' unless ref $_[0] eq 'ARRAY' },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "ARRAY" 1 16 17
67 16 ▶ default => sub { [qw(-3 -2 -1 1 2 3)] },
Subroutine __ANON__: called CC=1 SCAR=0.0
68 2 );
69  
70  
71 has octave => (
72 is => 'ro',
73 14 ▶ isa => sub { croak 'not a positive integer' unless $_[0] =~ /^\d+$/ },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] =~ /^\d+$/u 1 13 14
74 12 ▶ default => sub { 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
75 2 );
76  
77  
78 has scale => (
79 is => 'ro',
80 13 ▶ isa => sub { croak 'not a code reference' unless ref $_[0] eq 'CODE' },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "CODE" 1 12 13
81 2 builder => 1,
82 );
83  
84 12 ▶sub _build_scale {
Subroutine _build_scale: called CC=2 SCAR=6.9
85 12 my ($self) = @_;
86 12 ▶ if ($self->modal) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->modal) { } 4 8 12
87 5 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
88 5 my ($chord) = @_;
89 5 my ($chord_note) = _parse_chord($chord);
90 5 my @modes = qw( ionian dorian phrygian lydian mixolydian aeolian locrian );
91 5 my @key_notes = get_scale_notes($self->keycenter, $modes[0]);
92 5 ▶ my $position = first_index { $_ eq $chord_note } @key_notes;
Subroutine __ANON__: called CC=1 SCAR=0.0
93 5 ▶ my $scale = $position >= 0 ? $modes[$position] : $modes[0];
Branch: 1 branch
BranchTrueFalseTotal
$position >= 0 ? : 5 0 5
94 5 return $scale;
95 4 };
96 }
97 else {
98 8 ▶ return sub { $_[0] =~ /^[A-G][#b]?m/ ? 'minor' : 'major' };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ /^[A-G][#b]?m/u ? : 1 10 11
99 }
100 }
101  
102  
103 has tonic => (
104 is => 'ro',
105 isa => \&_boolean,
106 10 ▶ default => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
107 2 );
108  
109  
110 2 has positions => (
111 is => 'ro',
112 );
113  
114  
115 has verbose => (
116 is => 'ro',
117 isa => \&_boolean,
118 1 ▶ default => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
119 2 );
120  
121 75 ▶sub _boolean {
Subroutine _boolean: called CC=2 SCAR=6.9
122 75 my ($arg) = @_;
123 75 ▶ croak 'not a boolean' unless $arg =~ /^[01]$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $arg =~ /^[01]$/u 5 70 75
124 }
125  
126  
127 10 ▶sub generate {
Subroutine generate: called CC=63 SCAR=50.9
Pod documented
128 10 my ($self, $chord, $num, $next_chord) = @_;
129  
130 10 ▶ $chord ||= 'C';
Condition50%1/2
$chord ||= "C"
l!l
100
MC/DC0%0/1
$chord ||= "C"
$chord
Truth table50%1/2
A
A= $chord
Aresult
11
00
131 10 ▶ $num ||= 4;
Condition50%1/2
$num ||= 4
l!l
100
MC/DC0%0/1
$num ||= 4
$num
Truth table50%1/2
A
A= $num
Aresult
11
00
132  
133 10 ▶ if ($chord =~ /^(.+)\//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chord =~ m[^(.+)/]u) 1 9 10
134 1 $chord = $1;
135 }
136 10 my ($chord_note, $flavor) = _parse_chord($chord);
137  
138 10 my $next_chord_note;
139 10 ▶ ($next_chord_note) = _parse_chord($next_chord)
Branch: 1 branch
BranchTrueFalseTotal
if $next_chord 1 9 10
140 if $next_chord;
141  
142 10 ▶ print "CHORD: $chord => $chord_note, $flavor\n" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 10 10
143 10 ▶ print "NEXT: $next_chord => $next_chord_note\n" if $self->verbose && $next_chord;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose and $next_chord 0 10 10
Condition33%1/3
$self->verbose and $next_chord
!ll&&!rl&&r
1000
MC/DC0%0/2
$self->verbose and $next_chord
$self->verbose $next_chord
Truth table33%1/3
A and B
A= $self->verboseB= $next_chord
ABresult
0X0
100
111
144  
145 10 my $scale = $self->scale->($chord);
146 10 ▶ my $next_scale = defined $next_chord ? $self->scale->($next_chord) : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $next_chord ? : 1 9 10
147  
148 10 my $cn = Music::Chord::Note->new;
149  
150 10 my @notes = map { $self->pitchnum($_) }
151 $cn->chord_with_octave($chord, $self->octave);
152  
153 10 my @pitches;
154 10 ▶ if ($self->positions && $scale) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->positions and $scale) { } 1 9 10
elsif ($scale) { } 9 0 9
Condition66%2/3
$self->positions and $scale
!ll&&!rl&&r
901
MC/DC50%1/2
$self->positions and $scale
$self->positions $scale
Truth table66%2/3
A and B
A= $self->positionsB= $scale
ABresult
0X0
100
111
155 1 my @scale = get_scale_MIDI($chord_note, $self->octave, $scale);
156 1 for my $n (0 .. $#scale) {
157 7 ▶ push @pitches, $scale[$n] if grep { $_ == $n } @{ $self->positions->{$scale} };
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ == $n;} @{$self->positions->{$scale};} 1 6 7
158 }
159 }
160 elsif ($scale) {
161 9 @pitches = get_scale_MIDI($chord_note, $self->octave, $scale);
162 }
163 else {
164 0 @pitches = ();
165 }
166  
167 10 ▶ my @next_pitches = $next_scale ? get_scale_MIDI($next_chord_note, $self->octave, $next_scale) : ();
Branch: 1 branch
BranchTrueFalseTotal
$next_scale ? : 1 9 10
168  
169 # Add unique chord notes to the pitches
170 10 ▶ if ($self->chord_notes) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->chord_notes) 8 2 10
171 8 ▶ print "CHORD NOTES\n" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 8 8
172 8 for my $n (@notes) {
173 26 ▶ if (not any { $_ == $n } @pitches) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
unless (&any(sub { $_ == $n; } , @pitches)) 2 24 26
174 2 push @pitches, $n;
175 2 ▶ if ($self->verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->verbose) 0 2 2
176 0 my $x = $self->pitchname($n);
177 0 print "\tADD: $x\n";
178 }
179 }
180 }
181 }
182 10 @pitches = sort { $a <=> $b } @pitches; # Pitches are midi numbers
183  
184 # Determine if we should skip certain notes given the chord flavor
185 10 my @tones = get_scale_notes($chord_note, $scale);
186 10 ▶ print "\t$scale SCALE: ", ddc(\@tones) if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 10 10
187 10 my @fixed;
188 10 for my $p (@pitches) {
189 66 my $n = Music::Note->new($p, 'midinum');
190 66 my $x = $n->format('isobase');
191 # Inspect both # & b
192 66 ▶ if ($x =~ /#/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($x =~ /#/u) { } 8 58 66
elsif ($x =~ /b/u) { } 0 58 58
193 8 $n->en_eq('flat');
194 }
195 elsif ($x =~ /b/) {
196 0 $n->en_eq('sharp');
197 }
198 66 my $y = $n->format('isobase');
199 66 ▶ if (($flavor =~ /[#b]5/ && $tones[4] && ($x eq $tones[4] || $y eq $tones[4]))
Branch: 1 branch
BranchTrueFalseTotal
if ($flavor =~ /[#b]5/u and $tones[4] and $x eq $tones[4] || $y eq $tones[4] or $flavor =~ /7/u and not $flavor =~ /[Mm]7/u and $tones[6] and $x eq $tones[6] || $y eq $tones[6] or $flavor =~ /[#b]9/u and $tones[1] and $x eq $tones[1] || $y eq $tones[1] or $flavor =~ /dim/u and $tones[2] and $x eq $tones[2] || $y eq $tones[2] or $flavor =~ /dim/u and $tones[6] and $x eq $tones[6] || $y eq $tones[6] or $flavor =~ /aug/u and $tones[6] and $x eq $tones[6] || $y eq $tones[6]) 3 63 66
Condition50%36/72
$flavor =~ /[#b]5/u and $tones[4] and $x eq $tones[4] || $y eq $tones[4] or $flavor =~ /7/u and not $flavor =~ /[Mm]7/u and $tones[6] and $x eq $tones[6] || $y eq $tones[6] or $flavor =~ /[#b]9/u and $tones[1] and $x eq $tones[1] || $y eq $tones[1] or $flavor =~ /dim/u and $tones[2] and $x eq $tones[2] || $y eq $tones[2] or $flavor =~ /dim/u and $tones[6] and $x eq $tones[6] || $y eq $tones[6] or $flavor =~ /aug/u and $tones[6] and $x eq $tones[6] || $y eq $tones[6]
l!l&&r!l&&!r
3063
$flavor =~ /[#b]5/u and $tones[4] and $x eq $tones[4] || $y eq $tones[4] or $flavor =~ /7/u and not $flavor =~ /[Mm]7/u and $tones[6] and $x eq $tones[6] || $y eq $tones[6] or $flavor =~ /[#b]9/u and $tones[1] and $x eq $tones[1] || $y eq $tones[1] or $flavor =~ /dim/u and $tones[2] and $x eq $tones[2] || $y eq $tones[2] or $flavor =~ /dim/u and $tones[6] and $x eq $tones[6] || $y eq $tones[6]
l!l&&r!l&&!r
3063
$flavor =~ /[#b]5/u and $tones[4] and $x eq $tones[4] || $y eq $tones[4] or $flavor =~ /7/u and not $flavor =~ /[Mm]7/u and $tones[6] and $x eq $tones[6] || $y eq $tones[6] or $flavor =~ /[#b]9/u and $tones[1] and $x eq $tones[1] || $y eq $tones[1] or $flavor =~ /dim/u and $tones[2] and $x eq $tones[2] || $y eq $tones[2]
l!l&&r!l&&!r
3063
$flavor =~ /[#b]5/u and $tones[4] and $x eq $tones[4] || $y eq $tones[4] or $flavor =~ /7/u and not $flavor =~ /[Mm]7/u and $tones[6] and $x eq $tones[6] || $y eq $tones[6] or $flavor =~ /[#b]9/u and $tones[1] and $x eq $tones[1] || $y eq $tones[1]
l!l&&r!l&&!r
3063
$flavor =~ /[#b]5/u and $tones[4] and $x eq $tones[4] || $y eq $tones[4] or $flavor =~ /7/u and not $flavor =~ /[Mm]7/u and $tones[6] and $x eq $tones[6] || $y eq $tones[6]
l!l&&r!l&&!r
2163
$flavor =~ /[#b]5/u and $tones[4] and $x eq $tones[4] || $y eq $tones[4]
!ll&&!rl&&r
50142
$flavor =~ /[#b]5/u and $tones[4]
!ll&&!rl&&r
50016
$x eq $tones[4] or $y eq $tones[4]
l!l&&r!l&&!r
2014
$flavor =~ /7/u and not $flavor =~ /[Mm]7/u and $tones[6] and $x eq $tones[6] || $y eq $tones[6]
!ll&&!rl&&r
5671
$flavor =~ /7/u and not $flavor =~ /[Mm]7/u and $tones[6]
!ll&&!rl&&r
5608
$flavor =~ /7/u and not $flavor =~ /[Mm]7/u
!ll&&!rl&&r
43138
$x eq $tones[6] or $y eq $tones[6]
l!l&&r!l&&!r
107
$flavor =~ /[#b]9/u and $tones[1] and $x eq $tones[1] || $y eq $tones[1]
!ll&&!rl&&r
6300
$flavor =~ /[#b]9/u and $tones[1]
!ll&&!rl&&r
6300
$x eq $tones[1] or $y eq $tones[1]
l!l&&r!l&&!r
000
$flavor =~ /dim/u and $tones[2] and $x eq $tones[2] || $y eq $tones[2]
!ll&&!rl&&r
6300
$flavor =~ /dim/u and $tones[2]
!ll&&!rl&&r
6300
$x eq $tones[2] or $y eq $tones[2]
l!l&&r!l&&!r
000
$flavor =~ /dim/u and $tones[6] and $x eq $tones[6] || $y eq $tones[6]
!ll&&!rl&&r
6300
$flavor =~ /dim/u and $tones[6]
!ll&&!rl&&r
6300
$x eq $tones[6] or $y eq $tones[6]
l!l&&r!l&&!r
000
$flavor =~ /aug/u and $tones[6] and $x eq $tones[6] || $y eq $tones[6]
!ll&&!rl&&r
6300
$flavor =~ /aug/u and $tones[6]
!ll&&!rl&&r
6300
$x eq $tones[6] or $y eq $tones[6]
l!l&&r!l&&!r
000
MC/DC0%0/25
$flavor =~ /[#b]5/u and $tones[4] and $x eq $tones[4] || $y eq $tones[4] or $flavor =~ /7/u and not $flavor =~ /[Mm]7/u and $tones[6] and $x eq $tones[6] || $y eq $tones[6] or $flavor =~ /[#b]9/u and $tones[1] and $x eq $tones[1] || $y eq $tones[1] or $flavor =~ /dim/u and $tones[2] and $x eq $tones[2] || $y eq $tones[2] or $flavor =~ /dim/u and $tones[6] and $x eq $tones[6] || $y eq $tones[6] or $flavor =~ /aug/u and $tones[6] and $x eq $tones[6] || $y eq $tones[6]
too many conditions
200 ||
201 ($flavor =~ /7/ && $flavor !~ /[Mm]7/ && $tones[6] && ($x eq $tones[6] || $y eq $tones[6]))
202 ||
203 ($flavor =~ /[#b]9/ && $tones[1] && ($x eq $tones[1] || $y eq $tones[1]))
204 ||
205 ($flavor =~ /dim/ && $tones[2] && ($x eq $tones[2] || $y eq $tones[2]))
206 ||
207 ($flavor =~ /dim/ && $tones[6] && ($x eq $tones[6] || $y eq $tones[6]))
208 ||
209 ($flavor =~ /aug/ && $tones[6] && ($x eq $tones[6] || $y eq $tones[6]))
210 ) {
211 3 ▶ print "\tDROP: $x\n" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 3 3
212 3 next;
213 }
214 63 push @fixed, $p;
215 }
216  
217 10 ▶ if ($self->guitar) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->guitar) 0 10 10
218 0 ▶ @fixed = sort { $a <=> $b } map { $_ < E1 ? $_ + 12 : $_ } @fixed;
Branch: 1 branch
BranchTrueFalseTotal
$_ < 28 ? : 0 0 0
219 }
220  
221 10 ▶ if ($self->wrap) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->wrap) 1 9 10
222 1 my $n = Music::Note->new($self->wrap, 'ISO');
223 1 $n = $n->format('midinum');
224 1 ▶ @fixed = sort { $a <=> $b } map { $_ > $n ? $_ - 12 : $_ } @fixed;
Branch: 1 branch
BranchTrueFalseTotal
$_ > $n ? : 6 1 7
225 }
226  
227 # Make sure there are no duplicate pitches
228 10 @fixed = uniq @fixed;
229 10 ▶ $self->_verbose_notes('NOTES', @fixed) if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 10 10
230  
231 10 my @chosen;
232 10 ▶ if (@fixed > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fixed > 1) { } 9 1 10
233 9 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
234 9 my $voice = Music::VoiceGen->new(
235 pitches => \@fixed,
236 intervals => $self->intervals,
237 );
238  
239 # Try to start the phrase in the middle of the scale
240 9 $voice->context($fixed[int @fixed / 2]);
241  
242 # Get a passage of quasi-random pitches
243 9 @chosen = map { $voice->rand } 1 .. $num;
244 }
245 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
246 # warn "Can't instantiate Music::VoiceGen: $_\n";
247 0 @chosen = ($fixed[0]) x $num;
248 9 };
249 }
250 else {
251 1 @chosen = ($fixed[0]) x $num;
252 }
253  
254 # Choose the right note given the scale if the tonic is set
255 10 ▶ if ($self->tonic) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->tonic) 2 8 10
256 2 $chosen[0] = $fixed[0];
257 }
258  
259 # Intersect with the next-chord pitches
260 10 ▶ if ($next_chord) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next_chord) 1 9 10
261 1 my $A1 = Set::Array->new(@fixed);
262 1 my $A2 = Set::Array->new(@next_pitches);
263 1 my @intersect = @{ $A1->intersection($A2) };
264 1 ▶ $self->_verbose_notes('INTERSECT', @intersect) if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 1 1
265 # Anticipate the next chord
266 1 ▶ if (@intersect) {
Branch: 1 branch
BranchTrueFalseTotal
if (@intersect) 1 0 1
267 1 ▶ if (my $closest = _closest($chosen[-2] || $chosen[-1], \@intersect)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $closest = _closest($chosen[-2] || $chosen[-1], \@intersect)) 1 0 1
Condition33%1/3
$chosen[-2] || $chosen[-1]
l!l&&r!l&&!r
100
MC/DC0%0/2
$chosen[-2] || $chosen[-1]
$chosen[-2] $chosen[-1]
Truth table33%1/3
A || B
A= $chosen[-2]B= $chosen[-1]
ABresult
1X1
011
000
268 1 $chosen[-1] = $closest;
269 }
270 }
271 }
272  
273 # Show them what they've won, Bob!
274 10 ▶ $self->_verbose_notes('CHOSEN', @chosen) if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 10 10
275  
276 10 return \@chosen;
277 }
278  
279 16 ▶sub _parse_chord {
Subroutine _parse_chord: called CC=2 SCAR=7.0
280 16 my ($chord) = @_;
281 16 my $chord_note;
282 my $flavor;
283 16 ▶ if ($chord =~ /^([A-G][#b]?)(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chord =~ /^([A-G][#b]?)(.*)$/u) 16 0 16
284 16 $chord_note = $1;
285 16 $flavor = $2;
286 }
287 16 return $chord_note, $flavor;
288 }
289  
290 # Show a phrase of midinums as ISO notes
291 0 ▶sub _verbose_notes {
Subroutine _verbose_notes: not called CC=1 SCAR=6.9
292 0 my ($self, $title, @notes) = @_;
293 0 @notes = map { $self->pitchname($_) } @notes;
294 0 print "\t$title: ", ddc(\@notes);
295 }
296  
297 # Find the closest absolute difference to the key, in the list
298 1 ▶sub _closest {
Subroutine _closest: called CC=4 SCAR=13.9
299 1 my ($key, $list) = @_;
300 # Remove the key from the list
301 1 $list = [ grep { $_ != $key } @$list ];
302 1 ▶ return undef unless @$list;
Branch: 1 branch
BranchTrueFalseTotal
unless @$list 0 1 1
303 # Find the absolute difference
304 1 my @diff = map { abs($key - $_) } @$list;
305 1 my $min = min @diff;
306 1 my @closest;
307 # Get all the minimum elements of list
308 1 for my $n (0 .. $#diff) {
309 4 ▶ next if $diff[$n] != $min;
Branch: 1 branch
BranchTrueFalseTotal
if $diff[$n] != $min 2 2 4
310 2 push @closest, $list->[$n];
311 }
312 # Return a random minimum
313 1 return $closest[int rand @closest];
314 }
315  
316 2 1;
317  
318 __END__
↑ Summary