Skip to content

Commit

Permalink
TempoConverter: ↑ performance (cache envelopes)
Browse files Browse the repository at this point in the history
If multiple sequential events have simple events with the same timing
structure we often repeat the same efforts of creating tempo envelopes
by cutting the original envelope. We can reduce the work load by using
a cache.
  • Loading branch information
levinericzimmermann committed Mar 17, 2023
1 parent 805fa7f commit 3e48e66
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions mutwo/core_converters/tempos.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ def __init__(
self._apply_converter_on_events_tempo_envelope = (
apply_converter_on_events_tempo_envelope
)
self._start_and_end_to_tempo_converter_dict = {}

# ###################################################################### #
# static methods #
Expand Down Expand Up @@ -166,6 +167,21 @@ def _tempo_envelope_to_beat_length_in_seconds_envelope(
# private methods #
# ###################################################################### #

def _start_and_end_to_tempo_converter(self, start, end):
key = (start.duration, end.duration)
try:
t = self._start_and_end_to_tempo_converter_dict[key]
except KeyError:
t = self._start_and_end_to_tempo_converter_dict[key] = TempoConverter(
self._tempo_envelope.cut_out(
start,
end,
mutate=False,
),
apply_converter_on_events_tempo_envelope=False,
)
return t

def _convert_simple_event(
self,
simple_event: core_events.SimpleEvent,
Expand All @@ -186,14 +202,14 @@ def _convert_event(
depth: int = 0,
) -> core_events.abc.ComplexEvent[core_events.abc.Event]:
if self._apply_converter_on_events_tempo_envelope:
event_to_convert.tempo_envelope = TempoConverter(
self._tempo_envelope.cut_out(
absolute_entry_delay,
absolute_entry_delay + event_to_convert.duration,
mutate=False,
),
apply_converter_on_events_tempo_envelope=False,
).convert(event_to_convert.tempo_envelope)
start, end = (
absolute_entry_delay,
absolute_entry_delay + event_to_convert.duration,
)
local_tempo_converter = self._start_and_end_to_tempo_converter(start, end)
event_to_convert.tempo_envelope = local_tempo_converter(
event_to_convert.tempo_envelope
)
return super()._convert_event(event_to_convert, absolute_entry_delay, depth)

# ###################################################################### #
Expand Down

0 comments on commit 3e48e66

Please sign in to comment.