-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
412 lines (348 loc) · 13.8 KB
/
main.py
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
from quicktions import Fraction as f
import numpy as np
import ranges
from mutwo import core_converters
from mutwo import clock_converters
from mutwo import clock_events
from mutwo import clock_interfaces
from mutwo import common_generators
from mutwo import core_events
from mutwo import diary_converters
from mutwo import project_converters
from mutwo import timeline_interfaces
import project
def make_clock(poem_index, poem_line, before_rest_duration=0) -> clock_interfaces.Clock:
print("make clock for", poem_line, "...")
scale = project.constants.PENTATONIC_SCALE_TUPLE[poem_index]
match poem_index % 4:
case 0:
part_count = 2
energy = 61
case 1:
part_count = 3
energy = 51
case 2:
part_count = 2
energy = 41
case 3:
part_count = 1
energy = 51
if not poem_line:
scale_position_tuple = tuple((0, 0) for _ in range(part_count * 4))
else:
markov_chain = scale_to_markov_chain(scale)
g = markov_chain.walk_deterministic(tuple(markov_chain.keys())[0])
scale_position_list = []
for _ in range(part_count):
scale_position_list.extend(next(g))
scale_position_tuple = tuple(scale_position_list)
root_pitch_tuple = tuple(
scale.scale_position_to_pitch(scale_position)
for scale_position in scale_position_tuple
)
modal_sequential_event = core_events.SequentialEvent(
[
clock_events.ModalEvent0(start_pitch, end_pitch, scale, energy=energy)
for start_pitch, end_pitch in zip(root_pitch_tuple, root_pitch_tuple[1:])
]
)
project.clocks.apply_clock_events(modal_sequential_event)
if part_count >= 3:
insert_modal_event(
modal_sequential_event,
scale,
energy=-4,
index=7,
duration=f(8, 16),
)
if part_count >= 2:
if part_count >= 3:
energy = -3
else:
energy = -4
insert_modal_event(
modal_sequential_event,
scale,
energy=energy,
index=3,
duration=f(8, 16),
)
for modal_event in modal_sequential_event:
modal_event.control_event = core_events.SimultaneousEvent(
[core_events.SequentialEvent([core_events.SimpleEvent(1)])]
)
main_clock_line = clock_converters.Modal0SequentialEventToClockLine(
(
diary_converters.Modal0SequentialEventToEventPlacementTuple(
orchestration=project.constants.ORCHESTRATION.get_subset("PCLOCK"),
add_mod1=True,
),
diary_converters.Modal0SequentialEventToEventPlacementTuple(
orchestration=project.constants.ORCHESTRATION.get_subset(
"GLOCKENSPIEL"
),
add_mod1=True,
),
diary_converters.Modal0SequentialEventToEventPlacementTuple(
orchestration=project.constants.ORCHESTRATION.get_subset("HARP"),
add_mod1=True,
),
diary_converters.Modal0SequentialEventToEventPlacementTuple(
orchestration=project.constants.ORCHESTRATION.get_subset("V"),
add_mod1=True,
),
diary_converters.Modal0SequentialEventToEventPlacementTuple(
orchestration=project.constants.ORCHESTRATION.get_subset("V", "HARP"),
add_mod1=False,
),
diary_converters.Modal0SequentialEventToEventPlacementTuple(
orchestration=project.constants.ORCHESTRATION.get_subset(
"GLOCKENSPIEL",
"V",
"HARP",
),
add_mod1=False,
),
)
).convert(modal_sequential_event)
# Fix overlaps
main_clock_line.resolve_conflicts(
[
TuningForkHitStrategy(),
TagCountStrategy(),
timeline_interfaces.AlternatingStrategy(),
],
is_conflict=is_conflict,
)
start_clock_line = (
_clock_rest(before_rest_duration) if before_rest_duration > 0 else None
)
end_clock_line = _clock_end(scale)
clock = clock_interfaces.Clock(main_clock_line, start_clock_line, end_clock_line)
return clock
def is_conflict(event_placement_0, event_placement_1):
tag_tuple0, tag_tuple1 = (
ep.tag_tuple for ep in (event_placement_0, event_placement_1)
)
# Avoid overlaps between bowed tuning forks and other percussion
# sounds (because the player need to hold the tuning fork with one
# hand).
if may_be_bowed_tuning_fork_conflict(event_placement_0, event_placement_1):
for n in get_overlapping_tuning_fork_note_tuple(
event_placement_0, event_placement_1
):
if (
hasattr(n, "notation_indicator_collection")
and n.notation_indicator_collection.duration_line.is_active
):
return True
# Standard logic
share_instruments = bool(set(tag_tuple0).intersection(set(tag_tuple1)))
return share_instruments
def may_be_bowed_tuning_fork_conflict(event_placement_0, event_placement_1):
tag_tuple0, tag_tuple1 = (
ep.tag_tuple for ep in (event_placement_0, event_placement_1)
)
return (
project.constants.ORCHESTRATION.PCLOCK.name in tag_tuple0
and project.constants.ORCHESTRATION.GLOCKENSPIEL.name in tag_tuple1
) or (
project.constants.ORCHESTRATION.PCLOCK.name in tag_tuple1
and project.constants.ORCHESTRATION.GLOCKENSPIEL.name in tag_tuple0
)
def get_overlapping_tuning_fork_note_tuple(event_placement_0, event_placement_1):
glockenspiel_event, glockenspiel_ep = get_glockenspiel_event(
event_placement_0, event_placement_1
)
adjusted_glockenspiel_event = glockenspiel_event.set(
"duration", glockenspiel_ep.duration, mutate=False
)
_, pclock_ep = get_pclock_event(event_placement_0, event_placement_1)
pclock_duration_range = pclock_ep.time_range
overlapping_tuning_fork_note_list = []
for sequential_event_copy, sequential_event in zip(
# We need the duration information from the copied event
# (because we only copied the event in order to scale to real
# duration), but want to have the original note (because this
# is the one which needs to be adjusted).
adjusted_glockenspiel_event,
glockenspiel_event,
):
(
absolute_time_tuple,
duration,
) = sequential_event_copy._absolute_time_tuple_and_duration
for start, end, n in zip(
absolute_time_tuple, absolute_time_tuple[1:] + (duration,), sequential_event
):
r_start = start + glockenspiel_ep.min_start
r_end = end + glockenspiel_ep.min_start
if r_start in pclock_duration_range or r_end in pclock_duration_range:
overlapping_tuning_fork_note_list.append(n)
return tuple(overlapping_tuning_fork_note_list)
def get_glockenspiel_event(event_placement_0, event_placement_1):
return get_tag_event(
event_placement_0,
event_placement_1,
project.constants.ORCHESTRATION.GLOCKENSPIEL.name,
)
def get_pclock_event(event_placement_0, event_placement_1):
return get_tag_event(
event_placement_0,
event_placement_1,
project.constants.ORCHESTRATION.PCLOCK.name,
)
def get_tag_event(event_placement_0, event_placement_1, tag):
tag_tuple0, tag_tuple1 = (
ep.tag_tuple for ep in (event_placement_0, event_placement_1)
)
if tag in tag_tuple0:
ep = event_placement_0
else:
ep = event_placement_1
return ep.event[tag], ep
def scale_to_markov_chain(scale):
key = tuple(p.ratio for p in scale.scale_family.interval_tuple)
try:
markov_chain = _scale_to_markov_chain[key]
except KeyError:
gatra_tuple = scale_to_gatra_tuple.convert(scale)
markov_chain = _scale_to_markov_chain[
key
] = gatra_tuple_to_markov_chain.convert(gatra_tuple)
markov_chain.make_deterministic_map()
return markov_chain
def insert_modal_event(
modal_sequential_event,
scale,
index: int = 3,
energy: float = 0,
duration: f = f(25, 16),
):
if not (len(modal_sequential_event) > index):
return
assert index > 0, "Can't insert at first position"
modal_sequential_event.insert(
index,
clock_events.ModalEvent0(
modal_sequential_event[index - 1].end_pitch,
modal_sequential_event[index - 1].end_pitch,
scale,
energy=energy,
clock_event=clock_events.ClockEvent(
[core_events.SequentialEvent([core_events.SimpleEvent(duration)])]
),
),
)
scale_to_gatra_tuple = project_converters.ScaleToGatraTuple()
gatra_tuple_to_markov_chain = project_converters.GatraTupleToMarkovChain()
_scale_to_markov_chain = {}
def _clock_rest(rest_duration):
return clock_interfaces.ClockLine(
clock_events.ClockEvent(
[core_events.SequentialEvent([core_events.SimpleEvent(rest_duration)])]
)
)
def _clock_end(scale):
modal_sequential_event = core_events.SequentialEvent(
[
clock_events.ModalEvent0(
scale.scale_index_to_pitch(0), scale.scale_index_to_pitch(0), scale
).set("is_end", True)
]
)
project.clocks.apply_clock_events(modal_sequential_event)
return clock_converters.Modal0SequentialEventToClockLine([]).convert(
modal_sequential_event
)
class TuningForkHitStrategy(timeline_interfaces.ConflictResolutionStrategy):
def __init__(self, fix_level: int = 5):
# fix_level = 10 => always fix
# fix_level = 0 => never fix
self.fix_level = fix_level
self.activity_level = common_generators.ActivityLevel()
self.split_activity_level = common_generators.ActivityLevel(1)
def resolve_conflict(self, timeline, conflict) -> bool:
event_placement_0, event_placement_1 = conflict.left, conflict.right
if may_be_bowed_tuning_fork_conflict(event_placement_0, event_placement_1):
# Special treatment if the tuning fork belongs to an event placement
# with more than one instruments:
# In this case, to keep the larger event placement consistent, we
# always drop the percussion one.
_, epg = get_glockenspiel_event(event_placement_0, event_placement_1)
if len(epg.event) > 1:
_, epc = get_pclock_event(event_placement_0, event_placement_1)
timeline.unregister(epc)
return True
# Don't fix always, because this would be a bit boring :)
if self.activity_level(self.fix_level):
self._solve(event_placement_0, event_placement_1)
return True
return False
def _solve(self, event_placement_0, event_placement_1):
for n in get_overlapping_tuning_fork_note_tuple(
event_placement_0, event_placement_1
):
try:
n.notation_indicator_collection.duration_line.is_active = False
except AttributeError:
pass
# Sometimes split some of the glockenspiel notes, which needed to
# be separated.
glockenspiel_event, _ = get_glockenspiel_event(
event_placement_0, event_placement_1
)
glockenspiel_event = glockenspiel_event[0] # seq event
for absolute_time, n in zip(
glockenspiel_event.absolute_time_tuple, glockenspiel_event
):
split = False
try:
split = (
n.notation_indicator_collection.duration_line.is_active == False
and n.pitch_list
)
except AttributeError:
pass
if split and self.split_activity_level(5):
glockenspiel_event.split_child_at(
absolute_time + (n.duration.duration * f(1, 2))
)
class TagCountStrategy(timeline_interfaces.TagCountStrategy):
def __init__(self, prefer_more_level: int = 8):
self.prefer_more_level = prefer_more_level
self.activity_level = common_generators.ActivityLevel()
self.tag_count_more = timeline_interfaces.TagCountStrategy()
self.tag_count_fewer = timeline_interfaces.TagCountStrategy(False)
def resolve_conflict(self, timeline, conflict) -> bool:
if self.activity_level(self.prefer_more_level):
return self.tag_count_more.resolve_conflict(timeline, conflict)
else:
return self.tag_count_fewer.resolve_conflict(timeline, conflict)
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(prog="project")
parser.add_argument("-i", "--illustration", action="store_true")
parser.add_argument("-n", "--notation", default="")
parser.add_argument("-s", "--sound", action="store_true")
parser.add_argument("-m", "--max-index", default=16)
args = parser.parse_args()
max_index = int(args.max_index)
if args.illustration:
project.render.illustration()
# import logging
# from mutwo import diary_converters
# diary_converters.configurations.LOGGING_LEVEL = logging.DEBUG
from mutwo import diary_interfaces
with diary_interfaces.open():
clock_list = []
for index, line in enumerate(project.constants.POEM.split("\n")):
if index < max_index:
clock_list.append(make_clock(index, line))
else:
break
clock_tuple = tuple(clock_list)
if args.notation:
project.render.notation(clock_tuple, args.notation)
if args.sound:
project.render.midi(clock_tuple)