blib/lib/Music/ModalFunction.pm

filter: Statementstmt 100% 99 / 99 Branchbran 74.1% 43 / 58 Conditioncond 44.4% 4 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 100% 22 / 22 Podpod 100% 3 / 3 totaltotal 87.2% 171 / 196 CC 37 SCAR 18.9
CC
37
Cov
88%
pivot_chord_keys
11.3
_generate_database
11.2
chord_key
8.8
CRAP 6.6
↑ Summary
1 package Music::ModalFunction;
2 3 our $AUTHORITY = 'cpan:GENE';
3  
4 # ABSTRACT: Query for modal and scalar musical functions
5  
6 3 our $VERSION = '0.0600';
7  
8 3 ▶use feature 'state';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use AI::Prolog ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use MIDI::Util qw(midi_format);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use Music::Note ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use Music::Scales qw(get_scale_notes);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19  
20 3 has [qw(chord_note chord mode_note mode mode_function mode_roman key_note key key_function key_roman)] => (
21 is => 'ro',
22 );
23  
24 has [qw(verbose use_scales hash_results)] => (
25 is => 'ro',
26 63 ▶ isa => sub { croak "$_[0] is not a boolean" unless $_[0] =~ /^[01]$/ },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] =~ /^[01]$/ 0 63 63
27 49 ▶ default => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
28 3 );
29  
30 has _chord_key => (
31 is => 'ro',
32 init_arg => undef,
33 21 ▶ default => sub { [qw(method chord_note chord key_note key key_function key_roman)] },
Subroutine __ANON__: called CC=1 SCAR=0.0
34 3 );
35  
36 has _pivot_chord_keys => (
37 is => 'ro',
38 init_arg => undef,
39 21 ▶ default => sub { [qw(method chord_note chord mode_note mode mode_function mode_roman key_note key key_function key_roman)] },
Subroutine __ANON__: called CC=1 SCAR=0.0
40 3 );
41  
42 has _roman_key => (
43 is => 'ro',
44 init_arg => undef,
45 21 ▶ default => sub { [qw(method mode mode_roman key key_roman)] },
Subroutine __ANON__: called CC=1 SCAR=0.0
46 3 );
47  
48 3 has [qw(_modes _scales _database)] => (
49 is => 'lazy',
50 init_arg => undef,
51 );
52  
53 2 ▶sub _build__modes {
Subroutine _build__modes: called CC=1 SCAR=0.0
54 return {
55 2 ionian => [
56 { chord => 'maj', roman => 'r_I', function => 'tonic' },
57 { chord => 'min', roman => 'r_ii', function => 'supertonic' },
58 { chord => 'min', roman => 'r_iii', function => 'mediant' },
59 { chord => 'maj', roman => 'r_IV', function => 'subdominant' },
60 { chord => 'maj', roman => 'r_V', function => 'dominant' },
61 { chord => 'min', roman => 'r_vi', function => 'submediant' },
62 { chord => 'dim', roman => 'r_vii', function => 'leading_tone' },
63 ],
64 dorian => [
65 { chord => 'min', roman => 'r_i', function => 'parallel_minor' },
66 { chord => 'min', roman => 'r_ii', function => 'supertonic' },
67 { chord => 'maj', roman => 'r_III', function => 'mediant' },
68 { chord => 'maj', roman => 'r_IV', function => 'subdominant' },
69 { chord => 'min', roman => 'r_v', function => 'dominant' },
70 { chord => 'dim', roman => 'r_vi', function => 'submediant' },
71 { chord => 'maj', roman => 'r_VII', function => 'subtonic' },
72 ],
73 phrygian => [
74 { chord => 'min', roman => 'r_i', function => 'parallel_minor' },
75 { chord => 'maj', roman => 'r_II', function => 'supertonic' },
76 { chord => 'maj', roman => 'r_III', function => 'mediant' },
77 { chord => 'min', roman => 'r_iv', function => 'subdominant' },
78 { chord => 'dim', roman => 'r_v', function => 'dominant' },
79 { chord => 'maj', roman => 'r_VI', function => 'submediant' },
80 { chord => 'min', roman => 'r_vii', function => 'subtonic' },
81 ],
82 lydian => [
83 { chord => 'maj', roman => 'r_I', function => 'tonic' },
84 { chord => 'maj', roman => 'r_II', function => 'supertonic' },
85 { chord => 'min', roman => 'r_iii', function => 'mediant' },
86 { chord => 'dim', roman => 'r_iv', function => 'subdominant' },
87 { chord => 'maj', roman => 'r_V', function => 'dominant' },
88 { chord => 'min', roman => 'r_vi', function => 'submediant' },
89 { chord => 'min', roman => 'r_vii', function => 'leading_tone' },
90 ],
91 mixolydian => [
92 { chord => 'maj', roman => 'r_I', function => 'tonic' },
93 { chord => 'min', roman => 'r_ii', function => 'supertonic' },
94 { chord => 'dim', roman => 'r_iii', function => 'mediant' },
95 { chord => 'maj', roman => 'r_IV', function => 'subdominant' },
96 { chord => 'min', roman => 'r_v', function => 'dominant' },
97 { chord => 'min', roman => 'r_vi', function => 'submediant' },
98 { chord => 'maj', roman => 'r_VII', function => 'subtonic' },
99 ],
100 aeolian => [
101 { chord => 'min', roman => 'r_i', function => 'parallel_minor' },
102 { chord => 'dim', roman => 'r_ii', function => 'supertonic' },
103 { chord => 'maj', roman => 'r_III', function => 'mediant' },
104 { chord => 'min', roman => 'r_iv', function => 'subdominant' },
105 { chord => 'min', roman => 'r_v', function => 'dominant' },
106 { chord => 'maj', roman => 'r_VI', function => 'submediant' },
107 { chord => 'maj', roman => 'r_VII', function => 'subtonic' },
108 ],
109 locrian => [
110 { chord => 'dim', roman => 'r_i', function => 'parallel_minor' },
111 { chord => 'maj', roman => 'r_II', function => 'supertonic' },
112 { chord => 'min', roman => 'r_iii', function => 'mediant' },
113 { chord => 'min', roman => 'r_iv', function => 'subdominant' },
114 { chord => 'maj', roman => 'r_V', function => 'dominant' },
115 { chord => 'maj', roman => 'r_VI', function => 'submediant' },
116 { chord => 'min', roman => 'r_vii', function => 'subtonic' },
117 ],
118 }
119 }
120  
121 3 ▶sub _build__scales {
Subroutine _build__scales: called CC=1 SCAR=0.0
122 return {
123 3 harmonic_minor => [
124 { chord => 'min', roman => 'r_i', function => 'parallel_minor' },
125 { chord => 'dim', roman => 'r_ii', function => 'supertonic' },
126 { chord => 'aug', roman => 'r_III', function => 'mediant' },
127 { chord => 'min', roman => 'r_iv', function => 'subdominant' },
128 { chord => 'maj', roman => 'r_V', function => 'dominant' },
129 { chord => 'maj', roman => 'r_VI', function => 'submediant' },
130 { chord => 'dim', roman => 'r_vii', function => 'subtonic' },
131 ],
132 melodic_minor => [
133 { chord => 'min', roman => 'r_i', function => 'parallel_minor' },
134 { chord => 'min', roman => 'r_ii', function => 'supertonic' },
135 { chord => 'aug', roman => 'r_III', function => 'mediant' },
136 { chord => 'maj', roman => 'r_IV', function => 'subdominant' },
137 { chord => 'maj', roman => 'r_V', function => 'dominant' },
138 { chord => 'dim', roman => 'r_vi', function => 'submediant' },
139 { chord => 'dim', roman => 'r_vii', function => 'subtonic' },
140 ],
141 pentatonic => [
142 { chord => 'maj', roman => 'r_I', function => 'tonic' },
143 { chord => 'min', roman => 'r_ii', function => 'supertonic' },
144 { chord => 'min', roman => 'r_iii', function => 'mediant' },
145 { chord => 'maj', roman => 'r_IV', function => 'subdominant' },
146 { chord => 'min', roman => 'r_vi', function => 'submediant' },
147 ],
148 pentatonic_minor => [
149 { chord => 'min', roman => 'r_i', function => 'parallel_minor' },
150 { chord => 'dim', roman => 'r_ii', function => 'supertonic' },
151 { chord => 'min', roman => 'r_iv', function => 'subdominant' },
152 { chord => 'min', roman => 'r_v', function => 'dominant' },
153 { chord => 'maj', roman => 'r_VI', function => 'submediant' },
154 ],
155 blues => [
156 { chord => 'min', roman => 'r_i', function => 'parallel_minor' },
157 { chord => 'maj', roman => 'r_III', function => 'supertonic' },
158 { chord => 'sus4', roman => 'r_IV', function => 'subdominant' },
159 { chord => 'maj', roman => 'r_bV', function => 'flat5' },
160 { chord => 'min', roman => 'r_v', function => 'dominant' },
161 { chord => 'sus4', roman => 'r_VII', function => 'leading_tone' },
162 ],
163 diminished => [
164 { chord => 'maj', roman => 'r_I', function => 'tonic' },
165 { chord => 'dim', roman => 'r_bii', function => 'flat2' },
166 { chord => 'maj', roman => 'r_bIII', function => 'flat3' },
167 { chord => 'dim', roman => 'r_iii', function => 'mediant' },
168 { chord => 'maj', roman => 'r_bV', function => 'flat5' },
169 { chord => 'dim', roman => 'r_v', function => 'dominant' },
170 { chord => 'maj', roman => 'r_VI', function => 'submediant' },
171 { chord => 'dim', roman => 'r_vii', function => 'leading_tone' },
172 ],
173 augmented => [
174 { chord => 'aug', roman => 'r_I', function => 'tonic' },
175 { chord => 'aug', roman => 'r_bIII', function => 'flat3' },
176 { chord => 'maj', roman => 'r_III', function => 'mediant' },
177 { chord => 'maj', roman => 'r_V', function => 'dominant' },
178 { chord => 'maj', roman => 'r_VI', function => 'submediant' },
179 { chord => 'aug', roman => 'r_VII', function => 'leading_tone' },
180 ],
181 }
182 }
183  
184 21 ▶sub _build__database {
Subroutine _build__database: called CC=3 SCAR=11.0
185 21 my ($self) = @_;
186 21 state %cache;
187 21 ▶ my $key = ref($self) . ':' . ($self->use_scales ? 'scales' : 'modes');
Branch: 1 branch
BranchTrueFalseTotal
$self->use_scales ? : 10 11 21
188 21 ▶ return $cache{$key} //= $self->_generate_database;
Condition66%2/3
$cache{$key} //= $self->_generate_database
l!l&&r!l&&!r
1740
MC/DC0%0/2
$cache{$key} //= $self->_generate_database
$cache{$key} $self->_generate_database
Truth table66%2/3
A //= B
A= $cache{$key}B= $self->_generate_database
ABresult
1X1
011
000
189 }
190  
191 4 ▶sub _generate_database {
Subroutine _generate_database: called CC=11 SCAR=24.2
192 4 my ($self) = @_;
193  
194 # consider every note
195 4 my @chromatic = get_scale_notes('c', 'chromatic', 0, 'b');
196 4 my $database = '';
197  
198 4 ▶ my $list = $self->use_scales ? $self->_scales : $self->_modes;
Branch: 1 branch
BranchTrueFalseTotal
$self->use_scales ? : 2 2 4
199  
200 # build a prolog fact for each base note
201 4 for my $base (@chromatic) {
202 48 my ($mode_base) = map { lc } midi_format(0, $base);
203  
204 # consider each mode or scale properties
205 48 for my $item (sort keys %$list) {
206 # get the notes of the base note mode or scale
207 336 my @notes = get_scale_notes($base, $item);
208 # warn "Basics: $base $item [@notes]\n" if $self->verbose;
209  
210 336 my @pitches; # notes suitable for the prolog database
211  
212 # convert the notes to flatted, lower-case
213 336 for my $note (@notes) {
214 2232 my $n = Music::Note->new($note, 'isobase');
215 2232 ▶ $n->en_eq('flat') if $note =~ /#/;
Branch: 1 branch
BranchTrueFalseTotal
if $note =~ /#/ 318 1914 2232
216 2232 push @pitches, map { lc } midi_format(0, $n->format('isobase'));
217 }
218  
219 336 my $i = 0; # increment
220  
221 336 for my $pitch (@pitches) {
222 # get the properties of the given mode or scale
223 2232 my ($chord, $function, $roman);
224 2232 ▶ if ($self->use_scales) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->use_scales) { } 1056 1176 2232
225 1056 $chord = $self->_scales->{$item}[$i]{chord};
226 1056 $function = $self->_scales->{$item}[$i]{function};
227 1056 $roman = $self->_scales->{$item}[$i]{roman};
228 }
229 else {
230 1176 $chord = $self->_modes->{$item}[$i]{chord};
231 1176 $function = $self->_modes->{$item}[$i]{function};
232 1176 $roman = $self->_modes->{$item}[$i]{roman};
233 }
234  
235 # append to the database of facts
236 2232 ▶ $database .= "chord_key($pitch, $chord, $mode_base, $item, $function, $roman).\n"
Branch: 1 branch
BranchTrueFalseTotal
if $chord and $function and $roman 2232 0 2232
Condition33%2/6
$chord and $function and $roman
!ll&&!rl&&r
002232
$chord and $function
!ll&&!rl&&r
002232
MC/DC0%0/3
$chord and $function and $roman
$chord $function $roman
Truth table25%1/4
A and B and C
A= $chordB= $functionC= $roman
ABCresult
0XX0
10X0
1100
1111
237 if $chord && $function && $roman;
238  
239 2232 $i++;
240 }
241 }
242 }
243 # append the prolog rules
244 4 $database .= <<'RULES';
245 pivot_chord_keys(ChordNote, Chord, Key1Note, Key1, Key1Function, Key1Roman, Key2Note, Key2, Key2Function, Key2Roman) :-
246 % bind the chord to the function of the first key
247 chord_key(ChordNote, Chord, Key1Note, Key1, Key1Function, Key1Roman),
248 % bind the chord to the function of the second key
249 chord_key(ChordNote, Chord, Key2Note, Key2, Key2Function, Key2Roman),
250 % the functions cannot be the same
251 Key1Function \= Key2Function.
252  
253 roman_key(Mode, ModeRoman, Key, KeyRoman) :-
254 chord_key(_, _, _, Mode, ModeFunction, ModeRoman),
255 chord_key(_, _, _, Key, KeyFunction, KeyRoman),
256 ModeFunction \= KeyFunction.
257 RULES
258 # warn "Database: $database\n" if $self->verbose;
259  
260 4 return $database;
261 }
262  
263  
264 6 ▶sub chord_key {
Subroutine chord_key: called CC=7 SCAR=21.8
Pod documented
265 6 my ($self) = @_;
266 6 ▶ my $query = sprintf 'chord_key(%s, %s, %s, %s, %s, %s).',
Branch: 6 branches
BranchTrueFalseTotal
defined $self->chord_note ? : 6 0 6
defined $self->chord ? : 6 0 6
defined $self->key_note ? : 0 6 6
defined $self->key ? : 0 6 6
defined $self->key_function ? : 4 2 6
defined $self->key_roman ? : 0 6 6
267 defined $self->chord_note ? $self->chord_note : 'ChordNote',
268 defined $self->chord ? $self->chord : 'Chord',
269 defined $self->key_note ? $self->key_note : 'KeyNote',
270 defined $self->key ? $self->key : 'Key',
271 defined $self->key_function ? $self->key_function : 'KeyFunction',
272 defined $self->key_roman ? $self->key_roman : 'KeyRoman';
273 6 return $self->_querydb('chord_key', $query);
274 }
275  
276  
277 11 ▶sub pivot_chord_keys {
Subroutine pivot_chord_keys: called CC=11 SCAR=24.2
Pod documented
278 11 my ($self) = @_;
279 11 ▶ my $query = sprintf 'pivot_chord_keys(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s).',
Branch: 10 branches
BranchTrueFalseTotal
defined $self->chord_note ? : 6 5 11
defined $self->chord ? : 6 5 11
defined $self->mode_note ? : 9 2 11
defined $self->mode ? : 5 6 11
defined $self->mode_function ? : 0 11 11
defined $self->mode_roman ? : 0 11 11
defined $self->key_note ? : 5 6 11
defined $self->key ? : 7 4 11
defined $self->key_function ? : 4 7 11
defined $self->key_roman ? : 0 11 11
280 defined $self->chord_note ? $self->chord_note : 'ChordNote',
281 defined $self->chord ? $self->chord : 'Chord',
282 defined $self->mode_note ? $self->mode_note : 'ModeNote',
283 defined $self->mode ? $self->mode : 'Mode',
284 defined $self->mode_function ? $self->mode_function : 'ModeFunction',
285 defined $self->mode_roman ? $self->mode_roman : 'ModeRoman',
286 defined $self->key_note ? $self->key_note : 'KeyNote',
287 defined $self->key ? $self->key : 'Key',
288 defined $self->key_function ? $self->key_function : 'KeyFunction',
289 defined $self->key_roman ? $self->key_roman : 'KeyRoman';
290 11 return $self->_querydb('pivot_chord_keys', $query);
291 }
292  
293  
294 1 ▶sub roman_key {
Subroutine roman_key: called CC=5 SCAR=18.2
Pod documented
295 1 my ($self) = @_;
296 1 ▶ my $query = sprintf 'roman_key(%s, %s, %s, %s).',
Branch: 4 branches
BranchTrueFalseTotal
defined $self->mode ? : 1 0 1
defined $self->mode_roman ? : 1 0 1
defined $self->key ? : 1 0 1
defined $self->key_roman ? : 0 1 1
297 defined $self->mode ? $self->mode : 'Mode',
298 defined $self->mode_roman ? $self->mode_roman : 'ModeRoman',
299 defined $self->key ? $self->key : 'Key',
300 defined $self->key_roman ? $self->key_roman : 'KeyRoman';
301 1 return $self->_querydb('roman_key', $query);
302 }
303  
304 18 ▶sub _querydb {
Subroutine _querydb: called CC=4 SCAR=13.9
305 18 my ($self, $method, $query) = @_;
306  
307 18 ▶ warn "$method query: $query\n" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 18 18
308  
309 # A fresh engine per query avoids relying on AI::Prolog to fully reset
310 # its internal state between unrelated queries on a shared engine
311 18 my $prolog = AI::Prolog->new($self->_database);
312 18 $prolog->query($query);
313  
314 18 my $attr = '_' . $method;
315  
316 18 my @return;
317  
318 18 ▶ while (my $result = $prolog->results) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $result = $prolog->results) 189 18 207
319 #warn __PACKAGE__,' L',__LINE__,' ',,"R: @$result\n";
320 189 ▶ if ($self->hash_results) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->hash_results) { } 17 172 189
321 17 my %result;
322 17 @result{ @{ $self->$attr } } = @$result;
323 17 push @return, \%result;
324 }
325 else {
326 172 push @return, $result;
327 }
328 }
329  
330 18 return \@return;
331 }
332  
333 3 1;
334  
335 __END__
↑ Summary