line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Music::ModalFunction; |
2
|
|
|
|
|
|
|
our $AUTHORITY = 'cpan:GENE'; |
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
# ABSTRACT: Inspect musical modal functions |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = '0.0313'; |
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
1395
|
use Moo; |
|
1
|
|
|
|
|
11943
|
|
|
1
|
|
|
|
|
5
|
|
9
|
1
|
|
|
1
|
|
1938
|
use strictures 2; |
|
1
|
|
|
|
|
1660
|
|
|
1
|
|
|
|
|
39
|
|
10
|
1
|
|
|
1
|
|
759
|
use AI::Prolog (); |
|
1
|
|
|
|
|
305739
|
|
|
1
|
|
|
|
|
33
|
|
11
|
1
|
|
|
1
|
|
19
|
use Carp qw(croak); |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
53
|
|
12
|
1
|
|
|
1
|
|
561
|
use MIDI::Util qw(midi_format); |
|
1
|
|
|
|
|
40033
|
|
|
1
|
|
|
|
|
70
|
|
13
|
1
|
|
|
1
|
|
452
|
use Music::Note (); |
|
1
|
|
|
|
|
1764
|
|
|
1
|
|
|
|
|
30
|
|
14
|
1
|
|
|
1
|
|
496
|
use Music::Scales qw(get_scale_notes); |
|
1
|
|
|
|
|
5689
|
|
|
1
|
|
|
|
|
84
|
|
15
|
1
|
|
|
1
|
|
512
|
use namespace::clean; |
|
1
|
|
|
|
|
12119
|
|
|
1
|
|
|
|
|
10
|
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
has [qw(chord_note chord mode_note mode mode_function mode_roman key_note key key_function key_roman)] => ( |
19
|
|
|
|
|
|
|
is => 'ro', |
20
|
|
|
|
|
|
|
); |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
has verbose => ( |
23
|
|
|
|
|
|
|
is => 'ro', |
24
|
|
|
|
|
|
|
isa => sub { croak "$_[0] is not a boolean" unless $_[0] =~ /^[01]$/ }, |
25
|
|
|
|
|
|
|
default => sub { 0 }, |
26
|
|
|
|
|
|
|
); |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
has _modes => ( |
29
|
|
|
|
|
|
|
is => 'lazy', |
30
|
|
|
|
|
|
|
); |
31
|
|
|
|
|
|
|
sub _build__modes { |
32
|
|
|
|
|
|
|
return { |
33
|
6
|
|
|
6
|
|
5943
|
ionian => [ |
34
|
|
|
|
|
|
|
{ chord => 'maj', roman => 'r_I', function => 'tonic' }, |
35
|
|
|
|
|
|
|
{ chord => 'min', roman => 'r_ii', function => 'supertonic' }, |
36
|
|
|
|
|
|
|
{ chord => 'min', roman => 'r_iii', function => 'mediant' }, |
37
|
|
|
|
|
|
|
{ chord => 'maj', roman => 'r_IV', function => 'subdominant' }, |
38
|
|
|
|
|
|
|
{ chord => 'maj', roman => 'r_V', function => 'dominant' }, |
39
|
|
|
|
|
|
|
{ chord => 'min', roman => 'r_vi', function => 'submediant' }, |
40
|
|
|
|
|
|
|
{ chord => 'dim', roman => 'r_vii', function => 'leading_tone' } |
41
|
|
|
|
|
|
|
], |
42
|
|
|
|
|
|
|
dorian => [ |
43
|
|
|
|
|
|
|
{ chord => 'min', roman => 'r_i', function => 'tonic' }, |
44
|
|
|
|
|
|
|
{ chord => 'min', roman => 'r_ii', function => 'supertonic' }, |
45
|
|
|
|
|
|
|
{ chord => 'maj', roman => 'r_III', function => 'mediant' }, |
46
|
|
|
|
|
|
|
{ chord => 'maj', roman => 'r_IV', function => 'subdominant' }, |
47
|
|
|
|
|
|
|
{ chord => 'min', roman => 'r_v', function => 'dominant' }, |
48
|
|
|
|
|
|
|
{ chord => 'dim', roman => 'r_vi', function => 'submediant' }, |
49
|
|
|
|
|
|
|
{ chord => 'maj', roman => 'r_VII', function => 'subtonic' } |
50
|
|
|
|
|
|
|
], |
51
|
|
|
|
|
|
|
phrygian => [ |
52
|
|
|
|
|
|
|
{ chord => 'min', roman => 'r_i', function => 'tonic' }, |
53
|
|
|
|
|
|
|
{ chord => 'maj', roman => 'r_II', function => 'supertonic' }, |
54
|
|
|
|
|
|
|
{ chord => 'maj', roman => 'r_III', function => 'mediant' }, |
55
|
|
|
|
|
|
|
{ chord => 'min', roman => 'r_iv', function => 'subdominant' }, |
56
|
|
|
|
|
|
|
{ chord => 'dim', roman => 'r_v', function => 'dominant' }, |
57
|
|
|
|
|
|
|
{ chord => 'maj', roman => 'r_VI', function => 'submediant' }, |
58
|
|
|
|
|
|
|
{ chord => 'min', roman => 'r_vii', function => 'subtonic' } |
59
|
|
|
|
|
|
|
], |
60
|
|
|
|
|
|
|
lydian => [ |
61
|
|
|
|
|
|
|
{ chord => 'maj', roman => 'r_I', function => 'tonic' }, |
62
|
|
|
|
|
|
|
{ chord => 'maj', roman => 'r_II', function => 'supertonic' }, |
63
|
|
|
|
|
|
|
{ chord => 'min', roman => 'r_iii', function => 'mediant' }, |
64
|
|
|
|
|
|
|
{ chord => 'dim', roman => 'r_iv', function => 'subdominant' }, |
65
|
|
|
|
|
|
|
{ chord => 'maj', roman => 'r_V', function => 'dominant' }, |
66
|
|
|
|
|
|
|
{ chord => 'min', roman => 'r_vi', function => 'submediant' }, |
67
|
|
|
|
|
|
|
{ chord => 'min', roman => 'r_vii', function => 'leading_tone' } |
68
|
|
|
|
|
|
|
], |
69
|
|
|
|
|
|
|
mixolydian => [ |
70
|
|
|
|
|
|
|
{ chord => 'maj', roman => 'r_I', function => 'tonic' }, |
71
|
|
|
|
|
|
|
{ chord => 'min', roman => 'r_ii', function => 'supertonic' }, |
72
|
|
|
|
|
|
|
{ chord => 'dim', roman => 'r_iii', function => 'mediant' }, |
73
|
|
|
|
|
|
|
{ chord => 'maj', roman => 'r_IV', function => 'subdominant' }, |
74
|
|
|
|
|
|
|
{ chord => 'min', roman => 'r_v', function => 'dominant' }, |
75
|
|
|
|
|
|
|
{ chord => 'min', roman => 'r_vi', function => 'submediant' }, |
76
|
|
|
|
|
|
|
{ chord => 'maj', roman => 'r_VII', function => 'subtonic' } |
77
|
|
|
|
|
|
|
], |
78
|
|
|
|
|
|
|
aeolian => [ |
79
|
|
|
|
|
|
|
{ chord => 'min', roman => 'r_i', function => 'tonic' }, |
80
|
|
|
|
|
|
|
{ chord => 'dim', roman => 'r_ii', function => 'supertonic' }, |
81
|
|
|
|
|
|
|
{ chord => 'maj', roman => 'r_III', function => 'mediant' }, |
82
|
|
|
|
|
|
|
{ chord => 'min', roman => 'r_iv', function => 'subdominant' }, |
83
|
|
|
|
|
|
|
{ chord => 'min', roman => 'r_v', function => 'dominant' }, |
84
|
|
|
|
|
|
|
{ chord => 'maj', roman => 'r_VI', function => 'submediant' }, |
85
|
|
|
|
|
|
|
{ chord => 'maj', roman => 'r_VII', function => 'subtonic' } |
86
|
|
|
|
|
|
|
], |
87
|
|
|
|
|
|
|
locrian => [ |
88
|
|
|
|
|
|
|
{ chord => 'dim', roman => 'r_i', function => 'tonic' }, |
89
|
|
|
|
|
|
|
{ chord => 'maj', roman => 'r_II', function => 'supertonic' }, |
90
|
|
|
|
|
|
|
{ chord => 'min', roman => 'r_iii', function => 'mediant' }, |
91
|
|
|
|
|
|
|
{ chord => 'min', roman => 'r_iv', function => 'subdominant' }, |
92
|
|
|
|
|
|
|
{ chord => 'maj', roman => 'r_V', function => 'dominant' }, |
93
|
|
|
|
|
|
|
{ chord => 'maj', roman => 'r_VI', function => 'submediant' }, |
94
|
|
|
|
|
|
|
{ chord => 'min', roman => 'r_vii', function => 'subtonic' } |
95
|
|
|
|
|
|
|
] |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
has _database => ( |
100
|
|
|
|
|
|
|
is => 'lazy', |
101
|
|
|
|
|
|
|
); |
102
|
|
|
|
|
|
|
sub _build__database { |
103
|
6
|
|
|
6
|
|
79
|
my ($self) = @_; |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
# consider every note |
106
|
6
|
|
|
|
|
54
|
my @chromatic = get_scale_notes('c', 'chromatic', 0, 'b'); |
107
|
6
|
|
|
|
|
1125
|
my $database = ''; |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
# build a prolog fact for each base note |
110
|
6
|
|
|
|
|
16
|
for my $base (@chromatic) { |
111
|
72
|
|
|
|
|
242
|
my ($mode_base) = map { lc } Music::ModalFunction::midi_format($base); |
|
72
|
|
|
|
|
1820
|
|
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
# consider each mode's properties |
114
|
72
|
|
|
|
|
142
|
for my $mode (sort keys %{ $self->_modes }) { |
|
72
|
|
|
|
|
1203
|
|
115
|
|
|
|
|
|
|
# get the 7 notes of the base note mode |
116
|
504
|
|
|
|
|
1851
|
my @notes = get_scale_notes($base, $mode); |
117
|
504
|
50
|
|
|
|
87625
|
warn "Basics: $base $mode [@notes]\n" if $self->verbose; |
118
|
|
|
|
|
|
|
|
119
|
504
|
|
|
|
|
759
|
my @pitches; # notes suitable for the prolog database |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
# convert the notes to flatted, lower-case |
122
|
504
|
|
|
|
|
829
|
for my $note (@notes) { |
123
|
3528
|
|
|
|
|
7996
|
my $n = Music::Note->new($note, 'isobase'); |
124
|
3528
|
100
|
|
|
|
100465
|
$n->en_eq('flat') if $note =~ /#/; |
125
|
3528
|
|
|
|
|
11166
|
push @pitches, map { lc } Music::ModalFunction::midi_format($n->format('isobase')); |
|
3528
|
|
|
|
|
142877
|
|
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
|
128
|
504
|
|
|
|
|
854
|
my $i = 0; # increment for each of 7 diatonic modes |
129
|
|
|
|
|
|
|
|
130
|
504
|
|
|
|
|
744
|
for my $pitch (@pitches) { |
131
|
|
|
|
|
|
|
# get the properties of the given mode |
132
|
3528
|
|
|
|
|
55001
|
my $chord = $self->_modes->{$mode}[$i]{chord}; |
133
|
3528
|
|
|
|
|
69918
|
my $function = $self->_modes->{$mode}[$i]{function}; |
134
|
3528
|
|
|
|
|
68701
|
my $roman = $self->_modes->{$mode}[$i]{roman}; |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
# append to the database of facts |
137
|
3528
|
|
|
|
|
26575
|
$database .= "chord_key($pitch, $chord, $mode_base, $mode, $function, $roman).\n"; |
138
|
|
|
|
|
|
|
|
139
|
3528
|
|
|
|
|
6556
|
$i++; |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
# append the prolog rules |
144
|
6
|
|
|
|
|
43
|
$database .= <<'RULES'; |
145
|
|
|
|
|
|
|
% Can a chord in one key function in a second? |
146
|
|
|
|
|
|
|
pivot_chord_keys(ChordNote, Chord, Key1Note, Key1, Key1Function, Key1Roman, Key2Note, Key2, Key2Function, Key2Roman) :- |
147
|
|
|
|
|
|
|
% bind the chord to the function of the first key |
148
|
|
|
|
|
|
|
chord_key(ChordNote, Chord, Key1Note, Key1, Key1Function, Key1Roman), |
149
|
|
|
|
|
|
|
% bind the chord to the function of the second key |
150
|
|
|
|
|
|
|
chord_key(ChordNote, Chord, Key2Note, Key2, Key2Function, Key2Roman), |
151
|
|
|
|
|
|
|
% the functions cannot be the same |
152
|
|
|
|
|
|
|
Key1Function \= Key2Function. |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
% TODO |
155
|
|
|
|
|
|
|
roman_key(Mode, ModeRoman, Key, KeyRoman) :- |
156
|
|
|
|
|
|
|
chord_key(_, _, _, Mode, ModeFunction, ModeRoman), |
157
|
|
|
|
|
|
|
chord_key(_, _, _, Key, KeyFunction, KeyRoman), |
158
|
|
|
|
|
|
|
ModeFunction \= KeyFunction. |
159
|
|
|
|
|
|
|
RULES |
160
|
6
|
50
|
|
|
|
29
|
warn "Database: $database\n" if $self->verbose; |
161
|
|
|
|
|
|
|
|
162
|
6
|
|
|
|
|
1739
|
return $database; |
163
|
|
|
|
|
|
|
} |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
has _prolog => ( |
166
|
|
|
|
|
|
|
is => 'lazy', |
167
|
|
|
|
|
|
|
); |
168
|
|
|
|
|
|
|
sub _build__prolog { |
169
|
5
|
|
|
5
|
|
92
|
my ($self) = @_; |
170
|
5
|
|
|
|
|
97
|
return AI::Prolog->new($self->_database); |
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
sub chord_key { |
175
|
2
|
|
|
2
|
1
|
3980
|
my ($self) = @_; |
176
|
2
|
50
|
|
|
|
60
|
my $query = sprintf 'chord_key(%s, %s, %s, %s, %s, %s).', |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
177
|
|
|
|
|
|
|
defined $self->chord_note ? $self->chord_note : 'ChordNote', |
178
|
|
|
|
|
|
|
defined $self->chord ? $self->chord : 'Chord', |
179
|
|
|
|
|
|
|
defined $self->key_note ? $self->key_note : 'KeyNote', |
180
|
|
|
|
|
|
|
defined $self->key ? $self->key : 'Key', |
181
|
|
|
|
|
|
|
defined $self->key_function ? $self->key_function : 'KeyFunction', |
182
|
|
|
|
|
|
|
defined $self->key_roman ? $self->key_roman : 'KeyRoman'; |
183
|
2
|
|
|
|
|
11
|
return $self->_querydb($query); |
184
|
|
|
|
|
|
|
} |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
sub pivot_chord_keys { |
188
|
2
|
|
|
2
|
1
|
4660
|
my ($self) = @_; |
189
|
2
|
50
|
|
|
|
149
|
my $query = sprintf 'pivot_chord_keys(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s).', |
|
|
50
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
190
|
|
|
|
|
|
|
defined $self->chord_note ? $self->chord_note : 'ChordNote', |
191
|
|
|
|
|
|
|
defined $self->chord ? $self->chord : 'Chord', |
192
|
|
|
|
|
|
|
defined $self->mode_note ? $self->mode_note : 'ModeNote', |
193
|
|
|
|
|
|
|
defined $self->mode ? $self->mode : 'Mode', |
194
|
|
|
|
|
|
|
defined $self->mode_function ? $self->mode_function : 'ModeFunction', |
195
|
|
|
|
|
|
|
defined $self->mode_roman ? $self->mode_roman : 'ModeRoman', |
196
|
|
|
|
|
|
|
defined $self->key_note ? $self->key_note : 'KeyNote', |
197
|
|
|
|
|
|
|
defined $self->key ? $self->key : 'Key', |
198
|
|
|
|
|
|
|
defined $self->key_function ? $self->key_function : 'KeyFunction', |
199
|
|
|
|
|
|
|
defined $self->key_roman ? $self->key_roman : 'KeyRoman'; |
200
|
2
|
|
|
|
|
11
|
return $self->_querydb($query); |
201
|
|
|
|
|
|
|
} |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
sub roman_key { |
205
|
1
|
|
|
1
|
1
|
476
|
my ($self) = @_; |
206
|
1
|
50
|
|
|
|
59
|
my $query = sprintf 'roman_key(%s, %s, %s, %s).', |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
207
|
|
|
|
|
|
|
defined $self->mode ? $self->mode : 'Mode', |
208
|
|
|
|
|
|
|
defined $self->mode_roman ? $self->mode_roman : 'ModeRoman', |
209
|
|
|
|
|
|
|
defined $self->key ? $self->key : 'Key', |
210
|
|
|
|
|
|
|
defined $self->key_roman ? $self->key_roman : 'KeyRoman'; |
211
|
1
|
|
|
|
|
6
|
return $self->_querydb($query); |
212
|
|
|
|
|
|
|
} |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
sub _querydb { |
215
|
5
|
|
|
5
|
|
17
|
my ($self, $query) = @_; |
216
|
|
|
|
|
|
|
|
217
|
5
|
50
|
|
|
|
24
|
warn "Query: $query\n" if $self->verbose; |
218
|
|
|
|
|
|
|
|
219
|
5
|
|
|
|
|
151
|
$self->_prolog->query($query); |
220
|
|
|
|
|
|
|
|
221
|
5
|
|
|
|
|
12488481
|
my @return; |
222
|
|
|
|
|
|
|
|
223
|
5
|
|
|
|
|
254
|
while (my $result = $self->_prolog->results) { |
224
|
71
|
|
|
|
|
2736352
|
push @return, $result; |
225
|
|
|
|
|
|
|
} |
226
|
|
|
|
|
|
|
|
227
|
5
|
|
|
|
|
484422
|
return \@return; |
228
|
|
|
|
|
|
|
} |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
1; |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
__END__ |