-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathangry_man.pl
More file actions
219 lines (172 loc) · 6.98 KB
/
angry_man.pl
File metadata and controls
219 lines (172 loc) · 6.98 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
:- ensure_loaded(music).
% 12-Tone Rows
% A 12-tone row is a list of 12 notes where each tone only appears once.
tone_row_mk_12_unbound_notes(Notes) :-
Notes = [
note{name: a, accidental: natural, octave: _},
note{name: a, accidental: sharp, octave: _},
note{name: b, accidental: natural, octave: _},
note{name: c, accidental: natural, octave: _},
note{name: c, accidental: sharp, octave: _},
note{name: d, accidental: natural, octave: _},
note{name: d, accidental: sharp, octave: _},
note{name: e, accidental: natural, octave: _},
note{name: f, accidental: natural, octave: _},
note{name: f, accidental: sharp, octave: _},
note{name: g, accidental: natural, octave: _},
note{name: g, accidental: sharp, octave: _}
].
tone_row(Notes) :-
tone_row_mk_12_unbound_notes(TwelveUnboundNotes),
permutation(TwelveUnboundNotes, PermutedNotes),
Notes = PermutedNotes.
intervals_of_row([], []).
intervals_of_row([SingleNote], []).
intervals_of_row([FirstNote, SecondNote|Tail], [Interval|Intervals]) :-
interval(FirstNote, SecondNote, Interval),
intervals_of_row([SecondNote|Tail], Intervals).
unique_tones(Notes) :-
% unique_tones is true if no (name, accidental) collisions appear
maplist(\Note^NameAccidental^(
Note = note{name: Name, accidental: Accidental, octave: _},
NameAccidental = [Name, Accidental]
), Notes, NameAccidentals),
sort(NameAccidentals, SortedNameAccidentals),
length(NameAccidentals, Length),
length(SortedNameAccidentals, Length).
angry_man_interval_names([
minor_second,
major_second,
tritone,
minor_seventh,
major_seventh
]).
angry_man_intervals(Intervals) :-
angry_man_interval_names(AngryManIntervalNames),
maplist(\IntervalName^Interval^(
interval_name(Interval, IntervalName)
), AngryManIntervalNames, Intervals).
% Old implementation -- deprecated.
% angry_man_row(Notes) :-
% tone_row(Notes),
% unique_tones(Notes),
% intervals_of_row(Notes, RowIntervals),
% % Every RowIntervals entry is in angry_man_intervals
% angry_man_intervals(AngryManIntervals),
% maplist(\RowInterval^(
% member(RowInterval, AngryManIntervals)
% ), RowIntervals).
angry_man_next_note([], Note) :-
% Any note can be a starting point
is_guitar_playable(Note).
angry_man_next_note([FirstNote], SecondNote) :-
% Match any note that is an Angry Man interval away
angry_man_intervals(AngryManIntervals),
member(Interval, AngryManIntervals),
interval(FirstNote, SecondNote, Interval).
angry_man_next_note([FirstNote | Tail], NextNote) :-
is_guitar_playable(NextNote),
PrevNotes = [FirstNote | Tail],
flatten([PrevNotes | NextNote], AllNotes),
% Each tone is used only once
unique_tones(AllNotes),
% The previous note is one of the right distances away from this one
last(PrevNotes, LastPrevNote),
interval_octave_agnostic(LastPrevNote, NextNote, Interval),
angry_man_intervals(AngryManIntervals),
member(Interval, AngryManIntervals).
% Precomputed angry man successor pitch classes for fast lookup
:- dynamic angry_successor_pc/2.
precompute_angry_successors :-
retractall(angry_successor_pc(_, _)),
% Offsets from angry man intervals: m2(1), M2(2), tritone(6), m7(10=-2), M7(11=-1)
Offsets = [1, -1, 2, -2, 6],
forall(
between(0, 11, PC),
forall(
member(Offset, Offsets),
(
SuccPC is (PC + Offset + 12) mod 12,
(angry_successor_pc(PC, SuccPC) -> true ; assertz(angry_successor_pc(PC, SuccPC)))
)
)
).
:- precompute_angry_successors.
% Generate candidate next notes directly from interval constraints
angry_man_candidate(PrevNote, NextNote) :-
PrevNote = note{name: PrevName, accidental: PrevAcc, octave: _},
note_pitch_class(PrevName, PrevAcc, PrevPC),
angry_successor_pc(PrevPC, NextPC),
guitar_note_by_pc(NextPC, NextNote).
% Incremental angry man row generation.
% Builds the row left-to-right, checking constraints at each step:
% - Each note must be guitar-playable
% - Each consecutive pair must be an angry man interval apart (octave-agnostic)
% - Each tone (name+accidental) is used at most once
angry_man_row(Row) :-
angry_man_build(Row, none, []).
angry_man_build([], _, _).
angry_man_build([Note|Rest], none, UsedTones) :-
guitar_note(Note),
Note = note{name: Name, accidental: Acc, octave: _},
\+ member(Name-Acc, UsedTones),
angry_man_build(Rest, just(Note), [Name-Acc|UsedTones]).
angry_man_build([Note|Rest], just(Prev), UsedTones) :-
angry_man_candidate(Prev, Note),
Note = note{name: Name, accidental: Acc, octave: _},
\+ member(Name-Acc, UsedTones),
angry_man_build(Rest, just(Note), [Name-Acc|UsedTones]).
% Old implementation (replaced by incremental version above):
% angry_man_row([]).
% angry_man_row(Row) :-
% reverse(Row, ReversedRow),
% angry_man_row_helper(ReversedRow),
% unique_tones(Row).
% angry_man_row_helper([Note]) :-
% is_guitar_playable(Note).
% angry_man_row_helper([LastNote, PenultimateNote | Tail]) :-
% is_guitar_playable(LastNote),
% angry_man_next_note([PenultimateNote], LastNote),
% angry_man_row_helper([PenultimateNote | Tail]).
% Finding all valid angry man rows
the_angry_man_helper(Row, Length) :-
length(Row, Length),
angry_man_row(Row).
the_angry_man(ToneRows, Length) :-
findall(ToneRow, the_angry_man_helper(ToneRow, Length), ToneRows).
% Much faster (but incomplete?) experimental version.
%
% Regular Verify (len=4): inferences=43211
% Experimental Verify (len=4): inferences=2218
exp_helper(Elem, Acc, Result) :-
print(Acc), print("\n"),
angry_man_next_note(Acc, Elem),
append(Acc, [Elem], Result).
exp_angry_man_row(Row) :-
foldl(exp_helper, Row, [], _).
% Old versions / implementation scraps:
% the_angry_man_topleft(ToneRows, Length) :-
% the_angry_man(ToneRows, Length),
% maplist(\ToneRow^(
% nth0(0, ToneRow, FirstNote),
% FirstNote = note{name: e, accidental: natural, octave: 2}
% ), ToneRows).
% the_angry_man(ToneRows) :-
% findall(ToneRow, angry_man_row(ToneRow), ToneRows).
% maplist(\ToneRow^(
% length(ToneRow, 3)
% ), ToneRows).
% intervals_of_row([FirstNote, SecondNote|Tail], NextNote) :-
% interval(FirstNote, SecondNote, Interval),
% intervals_of_row([SecondNote|Tail], Intervals).
% intervals_of_row(RowHead, RowHeadIntervals),
% % Have not used this tone (note name + accidental) yet
% % use unique_tones between the row head, prev note, and this note
% flatten([RowHead, PrevNote, NextNote], AllNotes),
% unique_tones(AllNotes).
% % Have not used this interval yet
% \+ member(ThisInterval, RowHeadIntervals),
% % It is an angry man interval
% angry_man_intervals(AngryManIntervals),
% member(ThisInterval, AngryManIntervals),
% interval(PrevNote, NextNote, ThisInterval).