Skip to content

Commit

Permalink
parameters/TempoPoint: Refactor 'tempo_in_beats_per_minute' to 'tempo'
Browse files Browse the repository at this point in the history
It's an inconsistency of the 'TempoPoint' class to explicitly name its
attribute with the unit of its attribute. In other words we don't have

    Duration.duration_in_beats
    Pitch.frequency_in_hertz
    PitchInterval.interval_in_cents

but

    Duration.duration
    Pitch.frequency
    PitchInterval.interval

Therefore it also shouldn't be

    Tempo.tempo_in_beats_per_minute

but

    Tempo.tempo

This patch therefore helps unifies the naming of mutwo which makes it
easier for anyone new coming to the library.
  • Loading branch information
levinericzimmermann committed Feb 26, 2024
1 parent f5b73ae commit b5bb382
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 39 deletions.
2 changes: 1 addition & 1 deletion mutwo/core_converters/tempos.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def _extract_beats_per_minute_and_reference_from_tempo_point(
self, tempo_point: TempoPoint
) -> tuple[core_constants.Real, core_constants.Real]:
try:
beats_per_minute = tempo_point.tempo_in_beats_per_minute # type: ignore
beats_per_minute = tempo_point.tempo # type: ignore
except AttributeError:
beats_per_minute = float(tempo_point) # type: ignore

Expand Down
2 changes: 1 addition & 1 deletion mutwo/core_events/envelopes.py
Original file line number Diff line number Diff line change
Expand Up @@ -871,7 +871,7 @@ def parameter_to_value(
# successful, if not it will return 'parameter', because it
# will assume that we have a number based tempo point.
return float(
getattr(parameter, "absolute_tempo_in_beats_per_minute", parameter)
getattr(parameter, "absolute_tempo", parameter)
)

def apply_parameter_on_event(
Expand Down
22 changes: 11 additions & 11 deletions mutwo/core_parameters/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ class TempoPoint(Parameter):
"""Represent the active tempo at a specific moment in time.
If the user wants to define a `TempoPoint` class, the abstract
properties :attr:`tempo_or_tempo_range_in_beats_per_minute`
properties :attr:`tempo_or_tempo_range`
and `reference` have to be overridden.
"""

Expand All @@ -400,14 +400,14 @@ class TempoPoint(Parameter):
parser :func:`TempoPoint.from_any`."""

def __repr_content__(self) -> str:
return f"BPM={self.tempo_in_beats_per_minute},ref={self.reference}"
return f"BPM={self.tempo},ref={self.reference}"

def __str_content__(self) -> str:
return self.__repr_content__()

def __eq__(self, other: object) -> bool:
attribute_to_compare_tuple = (
"tempo_in_beats_per_minute",
"tempo",
"reference",
)
return core_utilities.test_if_objects_are_equal_by_parameter_tuple(
Expand All @@ -416,7 +416,7 @@ def __eq__(self, other: object) -> bool:

@property
@abc.abstractmethod
def tempo_or_tempo_range_in_beats_per_minute(
def tempo_or_tempo_range(
self,
) -> core_parameters.constants.TempoOrTempoRangeInBeatsPerMinute:
...
Expand All @@ -427,29 +427,29 @@ def reference(self) -> core_constants.Real:
...

@property
def tempo_in_beats_per_minute(
def tempo(
self,
) -> core_parameters.constants.TempoInBeatsPerMinute:
"""Get tempo in `beats per minute <https://en.wikipedia.org/wiki/Tempo#Measurement>`_
If :attr:`tempo_or_tempo_range_in_beats_per_minute` is a range
If :attr:`tempo_or_tempo_range` is a range
mutwo will return the minimal tempo.
"""

if isinstance(self.tempo_or_tempo_range_in_beats_per_minute, ranges.Range):
return self.tempo_or_tempo_range_in_beats_per_minute.start
if isinstance(self.tempo_or_tempo_range, ranges.Range):
return self.tempo_or_tempo_range.start
else:
return self.tempo_or_tempo_range_in_beats_per_minute
return self.tempo_or_tempo_range

@property
def absolute_tempo_in_beats_per_minute(self) -> float:
def absolute_tempo(self) -> float:
"""Get absolute tempo in `beats per minute <https://en.wikipedia.org/wiki/Tempo#Measurement>`_
The absolute tempo takes the :attr:`reference` of the :class:`TempoPoint`
into account.
"""

return self.tempo_in_beats_per_minute * self.reference
return self.tempo * self.reference

@classmethod
def from_any(cls: typing.Type[T], object: TempoPoint.Type) -> T:
Expand Down
24 changes: 10 additions & 14 deletions mutwo/core_parameters/tempos.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
class DirectTempoPoint(core_parameters.abc.TempoPoint):
"""Represent the active tempo at a specific moment in time.
:param tempo_or_tempo_range_in_beats_per_minute: Specify a tempo in
:param tempo_or_tempo_range: Specify a tempo in
`beats per minute <https://en.wikipedia.org/wiki/Tempo#Measurement>`_.
Tempo can also be a tempo range where the first value indicates a
minimal tempo and the second value the maximum tempo. If the user
Expand All @@ -55,13 +55,11 @@ class DirectTempoPoint(core_parameters.abc.TempoPoint):

def __init__(
self,
tempo_or_tempo_range_in_beats_per_minute: core_parameters.constants.TempoOrTempoRangeInBeatsPerMinute,
tempo_or_tempo_range: core_parameters.constants.TempoOrTempoRangeInBeatsPerMinute,
reference: core_constants.Real = 1,
textual_indication: typing.Optional[str] = None,
):
self.tempo_or_tempo_range_in_beats_per_minute = (
tempo_or_tempo_range_in_beats_per_minute
)
self.tempo_or_tempo_range = tempo_or_tempo_range
self.reference = reference
self.textual_indication = textual_indication

Expand All @@ -76,17 +74,15 @@ def reference(self, reference: core_constants.Real):
self._reference = float(reference)

@property
def tempo_or_tempo_range_in_beats_per_minute(
def tempo_or_tempo_range(
self,
) -> core_parameters.constants.TempoOrTempoRangeInBeatsPerMinute:
return self._tempo_or_tempo_range_in_beats_per_minute
return self._tempo_or_tempo_range

@tempo_or_tempo_range_in_beats_per_minute.setter
def tempo_or_tempo_range_in_beats_per_minute(
@tempo_or_tempo_range.setter
def tempo_or_tempo_range(
self,
tempo_or_tempo_range_in_beats_per_minute: core_parameters.constants.TempoOrTempoRangeInBeatsPerMinute,
tempo_or_tempo_range: core_parameters.constants.TempoOrTempoRangeInBeatsPerMinute,
):
self._tempo_or_tempo_range_in_beats_per_minute = (
tempo_or_tempo_range_in_beats_per_minute
)
return self._tempo_or_tempo_range_in_beats_per_minute
self._tempo_or_tempo_range = tempo_or_tempo_range
return self._tempo_or_tempo_range
4 changes: 2 additions & 2 deletions tests/converters/tempos_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def test_extract_beats_per_minute_and_reference_from_complete_tempo_point_object
self.converter._extract_beats_per_minute_and_reference_from_tempo_point(
tempo_point
),
(tempo_point.tempo_in_beats_per_minute, tempo_point.reference),
(tempo_point.tempo, tempo_point.reference),
)

def test_extract_beats_per_minute_and_reference_from_incomplete_tempo_point_object(
Expand All @@ -42,7 +42,7 @@ def test_extract_beats_per_minute_and_reference_from_incomplete_tempo_point_obje
self.converter._extract_beats_per_minute_and_reference_from_tempo_point(
tempo_point
),
(tempo_point.tempo_in_beats_per_minute, 1),
(tempo_point.tempo, 1),
)

def test_extract_beats_per_minute_and_reference_from_number(self):
Expand Down
20 changes: 10 additions & 10 deletions tests/parameters/tempos_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ def test_unequal_reference(self):
def test_unequal_tempo_point(self):
self.assertNotEqual(self.tempo_point0, self.tempo_point3)

def test_tempo_in_beats_per_minute(self):
self.assertEqual(self.tempo_point0.tempo_in_beats_per_minute, 60)
self.assertEqual(self.tempo_point2.tempo_in_beats_per_minute, 60)
self.assertEqual(self.tempo_point3.tempo_in_beats_per_minute, 62)
self.assertEqual(self.tempo_point5.tempo_in_beats_per_minute, 50)

def test_absolute_tempo_in_beats_per_minute(self):
self.assertEqual(self.tempo_point0.absolute_tempo_in_beats_per_minute, 60)
self.assertEqual(self.tempo_point2.absolute_tempo_in_beats_per_minute, 30)
self.assertEqual(self.tempo_point2.tempo_in_beats_per_minute, 60)
def test_tempo(self):
self.assertEqual(self.tempo_point0.tempo, 60)
self.assertEqual(self.tempo_point2.tempo, 60)
self.assertEqual(self.tempo_point3.tempo, 62)
self.assertEqual(self.tempo_point5.tempo, 50)

def test_absolute_tempo(self):
self.assertEqual(self.tempo_point0.absolute_tempo, 60)
self.assertEqual(self.tempo_point2.absolute_tempo, 30)
self.assertEqual(self.tempo_point2.tempo, 60)

def test_textual_indication(self):
self.assertEqual(self.tempo_point6.textual_indication, "adagio")

0 comments on commit b5bb382

Please sign in to comment.