-
Notifications
You must be signed in to change notification settings - Fork 5
/
prolog-music
142 lines (134 loc) · 6.66 KB
/
prolog-music
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Dumper::Compact qw(ddc);
use AI::Prolog;
use MIDI::Util qw(midi_format);
use Music::Note ();
use Music::Scales qw(get_scale_notes);
# we will loop over every note
my @chromatic = get_scale_notes('c', 'chromatic', 0, 'b');
#warn __PACKAGE__,' L',__LINE__,' ',,"@chromatic\n";exit;
# slurp the mode definition hashref
$/ = undef;
my $modes = eval <DATA>;
#warn __PACKAGE__,' L',__LINE__,' ',ddc($modes, {max_width=>128});exit;
# construct the database of key-chord relations
my $database = '';
for my $base (@chromatic) {
my ($mode_base) = map { lc } midi_format($base);
for my $mode (keys %$modes) {
my @pitches;
my @notes = get_scale_notes($base, $mode);
#warn __PACKAGE__,' L',__LINE__,' ',,"$base $mode @notes\n";
for my $note (@notes) {
my $n = Music::Note->new($note, 'isobase');
$n->en_eq('flat') if $note =~ /#/;
push @pitches, map { lc } midi_format($n->format('isobase'));
}
my $i = 0;
for my $pitch (@pitches) {
# chord_key(Base, Mode, Note, Chord, Function).
$database .= "chord_key($mode_base, $mode, $pitch, $modes->{$mode}[$i]{chord}, $modes->{$mode}[$i]{function}).\n";
$i++;
}
}
}
$database .=<<'RULE';
% Can a chord in one key function in a second?
pivot_chord_keys(ChordNote, Chord, Key1Note, Key1, Key1Function, Key2Note, Key2, Key2Function) :-
% bind the chord to the function of the first key
chord_key(Key1Note, Key1, ChordNote, Chord, Key1Function),
% bind the chord to the function of the second key
chord_key(Key2Note, Key2, ChordNote, Chord, Key2Function),
% the functions cannot be the same
Key1Function \= Key2Function.
RULE
#warn __PACKAGE__,' L',__LINE__,' ',,"$database\n";exit;
my $prolog = AI::Prolog->new($database);
# Query the database:
# What keys have a Dmaj chord as dominant?
$prolog->query('chord_key(d, ionian, KeyNote, Key, dominant).');
# What keys contain a Gmaj chord?
#$prolog->query('chord_key(g, ionian, KeyNote, Key, Function).');
# What key can modulate to, from Cmaj with Gmaj as pivot?
#$prolog->query('pivot_chord_keys(g, maj, c, ionian, Key1Function, Key2Note, Key2, Key2Function).');
# What key can modulate to Cmin through a pivot chord?
#$prolog->query('pivot_chord_keys(ChordNote, Chord, Key1Note, Key1, Key1Function, c, aeolian, Key2Function).');
# What keys have pivot chords that are dominant in the new key?
#$prolog->query('pivot_chord_keys(ChordNote, Chord, Key1Note, Key1, Key1Function, Key2Note, Key2, dominant).');
# What keys can be modulated to, from Cmaj, using Gmaj as pivot?
#$prolog->query('pivot_chord_keys(g, maj, c, Key1, Key1Function, Key2Note, Key2, Key2Function).');
# What keys can be modulated to, from Cmaj, using Gmaj as pivot?
#$prolog->query('pivot_chord_keys(g, maj, c, Key1, Key1Function, Key2Note, Key2, subdominant).');
# What is the function of Gmaj borrowed between Cmaj & Cmin?
#$prolog->query('pivot_chord_keys(g, maj, c, ionian, Key1Function, c, aeolian, Key2Function).');
while (my $result = $prolog->results) {
print ddc($result, {max_width=>128});
}
__DATA__
{
ionian => [
{ chord => 'maj', roman => 'r_I', function => 'tonic' },
{ chord => 'min', roman => 'r_ii', function => 'supertonic' },
{ chord => 'min', roman => 'r_iii', function => 'mediant' },
{ chord => 'maj', roman => 'r_IV', function => 'subdominant' },
{ chord => 'maj', roman => 'r_V', function => 'dominant' },
{ chord => 'min', roman => 'r_vi', function => 'submediant' },
{ chord => 'dim', roman => 'r_vii', function => 'leading_tone' }
],
dorian => [
{ chord => 'min', roman => 'r_i', function => 'tonic' },
{ chord => 'min', roman => 'r_ii', function => 'supertonic' },
{ chord => 'maj', roman => 'r_III', function => 'mediant' },
{ chord => 'maj', roman => 'r_IV', function => 'subdominant' },
{ chord => 'min', roman => 'r_v', function => 'dominant' },
{ chord => 'dim', roman => 'r_vi', function => 'submediant' },
{ chord => 'maj', roman => 'r_VII', function => 'subtonic' }
],
phrygian => [
{ chord => 'min', roman => 'r_i', function => 'tonic' },
{ chord => 'maj', roman => 'r_II', function => 'supertonic' },
{ chord => 'maj', roman => 'r_III', function => 'mediant' },
{ chord => 'min', roman => 'r_iv', function => 'subdominant' },
{ chord => 'dim', roman => 'r_v', function => 'dominant' },
{ chord => 'maj', roman => 'r_VI', function => 'submediant' },
{ chord => 'min', roman => 'r_vii', function => 'subtonic' }
],
lydian => [
{ chord => 'maj', roman => 'r_I', function => 'tonic' },
{ chord => 'maj', roman => 'r_II', function => 'supertonic' },
{ chord => 'min', roman => 'r_iii', function => 'mediant' },
{ chord => 'dim', roman => 'r_iv', function => 'subdominant' },
{ chord => 'maj', roman => 'r_V', function => 'dominant' },
{ chord => 'min', roman => 'r_vi', function => 'submediant' },
{ chord => 'min', roman => 'r_vii', function => 'leading_tone' }
],
mixolydian => [
{ chord => 'maj', roman => 'r_I', function => 'tonic' },
{ chord => 'min', roman => 'r_ii', function => 'supertonic' },
{ chord => 'dim', roman => 'r_iii', function => 'mediant' },
{ chord => 'maj', roman => 'r_IV', function => 'subdominant' },
{ chord => 'min', roman => 'r_v', function => 'dominant' },
{ chord => 'min', roman => 'r_vi', function => 'submediant' },
{ chord => 'maj', roman => 'r_VII', function => 'subtonic' }
],
aeolian => [
{ chord => 'min', roman => 'r_i', function => 'tonic' },
{ chord => 'dim', roman => 'r_ii', function => 'supertonic' },
{ chord => 'maj', roman => 'r_III', function => 'mediant' },
{ chord => 'min', roman => 'r_iv', function => 'subdominant' },
{ chord => 'min', roman => 'r_v', function => 'dominant' },
{ chord => 'maj', roman => 'r_VI', function => 'submediant' },
{ chord => 'maj', roman => 'r_VII', function => 'subtonic' }
],
locrian => [
{ chord => 'dim', roman => 'r_i', function => 'tonic' },
{ chord => 'maj', roman => 'r_II', function => 'supertonic' },
{ chord => 'min', roman => 'r_iii', function => 'mediant' },
{ chord => 'min', roman => 'r_iv', function => 'subdominant' },
{ chord => 'maj', roman => 'r_V', function => 'dominant' },
{ chord => 'maj', roman => 'r_VI', function => 'submediant' },
{ chord => 'min', roman => 'r_vii', function => 'subtonic' }
]
}