Skip to content

Commit

Permalink
events: Drop 'RelativeEnvelope'
Browse files Browse the repository at this point in the history
This event has been added in 9f601a8 in
order to support glissandi (introduced few commits later in df609dc).
With recent patch 05a919c that aims to
unify the way how continuous/dynamic parameters are treated, we would
also no longer need 'RelativeEnvelope' for glissandi (once 'mutwo.music'
has been adjusted to 'mutwo.core >= 2.0.0'), because glissandi would be
described by absolute points. Now, even if we later on re-introduce
generic relative parameters, we may not necessarily need a
'RelativeEnvelope' class as this only provides the small '.resolve'
method that perhaps could also be re-written in a better way.
  • Loading branch information
levinericzimmermann committed Apr 8, 2024
1 parent a0c0f83 commit 2c73955
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 71 deletions.
54 changes: 1 addition & 53 deletions mutwo/core_events/envelopes.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from mutwo import core_utilities


__all__ = ("Envelope", "RelativeEnvelope")
__all__ = ("Envelope",)

T = typing.TypeVar("T", bound=core_events.abc.Event)

Expand Down Expand Up @@ -745,55 +745,3 @@ def add(s, value):
add(s, v)

return segment_tuple


class RelativeEnvelope(Envelope, typing.Generic[T]):
"""Envelope with relative durations and values / parameters.
:param *args: Argument of :class:`Envelope`.
:param base_parameter_and_relative_parameter_to_absolute_parameter: A function",
which runs when the :func:`resolve` is called. It expects the base parameter",
and the relative parameter (which is extracted from the envelope events)",
and should return an absolute parameter.",
:param **kwargs: Keyword arguments of :class:`Envelope`.
The :class:`RelativeEnvelope` adds the :func:`resolve` method to the base
class :class:`Envelope`.",
"""

def __init__(
self,
*args,
base_parameter_and_relative_parameter_to_absolute_parameter: typing.Callable[
[typing.Any, typing.Any],
typing.Any,
],
**kwargs,
):
self.base_parameter_and_relative_parameter_to_absolute_parameter = (
base_parameter_and_relative_parameter_to_absolute_parameter
)
super().__init__(*args, **kwargs)

def resolve(
self,
duration: "core_parameters.abc.Duration.Type",
base_parameter: typing.Any,
resolve_envelope_class: type[Envelope] = Envelope,
) -> Envelope:
dur = core_parameters.abc.Duration.from_any(duration)
plist = []
try:
fact = dur / self.duration
except ZeroDivisionError:
fact = core_parameters.DirectDuration(0)
for abst, e in zip(self.absolute_time_tuple, self):
rel_param = self.event_to_parameter(e)
new_param = (
self.base_parameter_and_relative_parameter_to_absolute_parameter(
base_parameter, rel_param
)
)
p = (abst * fact, new_param, self.event_to_curve_shape(e))
plist.append(p)
return resolve_envelope_class(plist)
18 changes: 0 additions & 18 deletions tests/events/envelopes_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,23 +352,5 @@ def test_extend_until(self):
)


class RelativeEnvelopeTest(unittest.TestCase):
def setUp(cls):
cls.envelope = core_events.RelativeEnvelope(
[
[0, 0],
[5, 5],
[10, 10],
],
base_parameter_and_relative_parameter_to_absolute_parameter=lambda base_parameter, relative_parameter: base_parameter
+ relative_parameter,
)

def test_resolve(self):
resolved_envelope = self.envelope.resolve(duration=1, base_parameter=100)
self.assertEqual(resolved_envelope.duration, core_parameters.DirectDuration(1))
self.assertEqual(resolved_envelope.value_tuple, (100, 105, 110))


if __name__ == "__main__":
unittest.main()

0 comments on commit 2c73955

Please sign in to comment.