mutwo.core_converters

Convert data from and to mutwo.

Object

Documentation

mutwo.core_converters.ChrononToAttribute

Extract from a chronon an attribute.

mutwo.core_converters.dict

dict() -> new empty dictionary

mutwo.core_converters.MutwoParameterDictToKeywordArgument

Extract from a dict of mutwo parameters specific objects.

mutwo.core_converters.MutwoParameterDictToDuration

Extract from a dict of mutwo parameters the duration.

mutwo.core_converters.MutwoParameterDictToChronon

Convert a dict of mutwo parameters to a mutwo.core_events.Chronon

mutwo.core_converters.TempoConverter

Apply tempo on an Event.

mutwo.core_converters.EventToMetrizedEvent

Apply tempo of event on copy of itself

class ChrononToAttribute(attribute_name, exception_value)[source]

Bases: Converter

Extract from a chronon an attribute.

Parameters:
  • attribute_name (str) – The name of the attribute which is fetched from a mutwo.core_events.Chronon.

  • exception_value (Any) – This value is returned in case an AttributeError raises .

Public Methods:

convert(chronon_to_convert)

Extract from a mutwo.core_events.Chronon an attribute.

Inherited from Converter

convert(...)

Inherited from MutwoObject

copy()

Return a deep copy of mutwo object.


convert(chronon_to_convert)[source]

Extract from a mutwo.core_events.Chronon an attribute.

Parameters:

chronon_to_convert (mutwo.core_events.Chronon) – The mutwo.core_events.Chronon from which an attribute shall be extracted.

Return type:

Any

Example:

>>> from mutwo import core_converters
>>> from mutwo import core_events
>>> chronon = core_events.Chronon(duration=10.0)
>>> chronon_to_duration = core_converters.ChrononToAttribute(
...     'duration', 0
... )
>>> chronon_to_duration.convert(chronon)
DirectDuration(10.0)
>>> chronon_to_pasta = core_converters.ChrononToAttribute(
...     'pasta', 'spaghetti'
... )
>>> chronon_to_pasta.convert(chronon)
'spaghetti'
>>> chronon.pasta = 'tagliatelle'
>>> chronon_to_pasta.convert(chronon)
'tagliatelle'
class MutwoParameterDictToKeywordArgument(mutwo_parameter_to_search_name, keyword=None)[source]

Bases: Converter

Extract from a dict of mutwo parameters specific objects.

Parameters:
  • mutwo_parameter_to_search_name (str) – The parameter name which should be fetched from the MutwoParameterDict (if it exists).

  • keyword (Optional[str]) – The keyword string to return. If no argument is given it will use the same value as :param:`mutwo_parameter_to_search_name`.

Example:

>>> from mutwo import core_converters
>>> from mutwo import core_parameters
>>> mutwo_parameter_dict_to_keyword_argument = core_converters.MutwoParameterDictToKeywordArgument('duration')
>>> mutwo_parameter_dict_to_keyword_argument.convert(
...     {'duration': core_parameters.DirectDuration(1.0)}
... )
('duration', DirectDuration(1.0))

Public Methods:

convert(mutwo_parameter_dict_to_convert)

Inherited from Converter

convert(...)

Inherited from MutwoObject

copy()

Return a deep copy of mutwo object.


convert(mutwo_parameter_dict_to_convert)[source]
Parameters:

mutwo_parameter_dict_to_convert (dict[str, Any]) –

Return type:

Optional[tuple[str, Any]]

class MutwoParameterDictToDuration(duration_to_search_name=None, duration_keyword_name=None)[source]

Bases: MutwoParameterDictToKeywordArgument

Extract from a dict of mutwo parameters the duration.

Parameters:

Public Methods:

Inherited from MutwoParameterDictToKeywordArgument

convert(mutwo_parameter_dict_to_convert)

Inherited from Converter

convert(...)

Inherited from MutwoObject

copy()

Return a deep copy of mutwo object.


class MutwoParameterDictToChronon(mutwo_parameter_dict_to_keyword_argument_sequence=None, chronon_class=<class 'mutwo.core_events.basic.Chronon'>)[source]

Bases: Converter

Convert a dict of mutwo parameters to a mutwo.core_events.Chronon

Parameters:

Public Methods:

convert(mutwo_parameter_dict_to_convert)

Inherited from Converter

convert(...)

Inherited from MutwoObject

copy()

Return a deep copy of mutwo object.


convert(mutwo_parameter_dict_to_convert)[source]
Parameters:

mutwo_parameter_dict_to_convert (dict[str, Any]) –

Return type:

Chronon

class TempoConverter(tempo, apply_converter_on_events_tempo=True)[source]

Bases: EventConverter

Apply tempo on an Event.

Parameters:
  • tempo (Tempo) – The tempo that shall be applied on the mutwo events. This must be a Tempo object.

  • apply_converter_on_events_tempo (bool) – If set to True the converter adjusts the tempo attribute of each converted event. Default to True.

Example:

>>> from mutwo import core_converters
>>> from mutwo import core_parameters
>>> tempo = core_parameters.FlexTempo(
...     [[0, core_parameters.DirectTempo(60)], [3, 60], [3, 30], [5, 50]],
... )
>>> c = core_converters.TempoConverter(tempo)

Public Methods:

convert(event_to_convert)

Apply tempo curve of the converter on copy of 'event_to_convert'.

Inherited from Converter

convert(...)

Inherited from MutwoObject

copy()

Return a deep copy of mutwo object.


convert(event_to_convert)[source]

Apply tempo curve of the converter on copy of ‘event_to_convert’.

Parameters:

event_to_convert (Event) – The event to convert. Can be any object that inherits from mutwo.core_events.abc.Event. If the event that shall be converted is longer than the tempo curve of the TempoConverter, then the last tempo of the curve is hold.

Returns:

A new Event which duration has been adapted by the tempo curve of the TempoConverter.

Return type:

Event

Example:

>>> from mutwo import core_converters
>>> from mutwo import core_events
>>> from mutwo import core_parameters
>>> tempo = core_parameters.FlexTempo(
...     [[0, core_parameters.DirectTempo(60)], [3, 60], [3, 30], [5, 50]],
... )
>>> my_tempo_converter = core_converters.TempoConverter(tempo)
>>> my_events = core_events.Consecution([core_events.Chronon(d) for d in (3, 2, 5)])
>>> my_tempo_converter.convert(my_events)
Consecution([Chronon(duration=DirectDuration(3.0)), Chronon(duration=DirectDuration(3.2)), Chronon(duration=DirectDuration(6.0))])
class EventToMetrizedEvent(skip_level_count=None, maxima_depth_count=None)[source]

Bases: SymmetricalEventConverter

Apply tempo of event on copy of itself

Public Methods:

convert(event_to_convert)

Apply tempo of event on copy of itself

Inherited from Converter

convert(...)

Inherited from MutwoObject

copy()

Return a deep copy of mutwo object.


Parameters:
  • skip_level_count (Optional[int]) –

  • maxima_depth_count (Optional[int]) –

convert(event_to_convert)[source]

Apply tempo of event on copy of itself

Parameters:

event_to_convert (Event) –

Return type:

Event

mutwo.core_converters.abc

Defining the public API for any converter class.

class Converter[source]

Bases: MutwoObject, ABC

Abstract base class for all Converter classes.

Converter classes are defined as classes that convert data between two different encodings. Their only public method (besides initialisation) should be a convert method. The first argument of the convert method should be the data to convert.

abstract convert(event_or_parameter_or_file_to_convert, *args, **kwargs)[source]
Parameters:

event_or_parameter_or_file_to_convert (Any) –

Return type:

Any

class EventConverter[source]

Bases: Converter

Abstract base class for Converter which handle mutwo events.

This class helps building new classes which convert mutwo events with few general private methods (and without adding any new public method). Converting mutwo event often involves the same pattern: due to the nested structure of an Event, the converter has to iterate through the different layers until it reaches leaves (any class that inherits from mutwo.core_events.Chronon). This common iteration process and the different time treatment between mutwo.core_events.Consecution and mutwo.core_events.Concurrence are implemented in EventConverter. For writing a new EventConverter class, one only has to override the abstract method _convert_chronon() and the abstract method convert() (where one will perhaps call _convert_event().).

Example:

The following example defines a dummy class for demonstrating how to use EventConverter.

>>> from mutwo import core_converters
>>> class DurationPrintConverter(core_converters.abc.EventConverter):
...     def _convert_chronon(self, event_to_convert, absolute_time):
...         return "{}: {}".format(absolute_time, event_to_convert.duration),
...     def convert(self, event_to_convert):
...         data_per_event = self._convert_event(event_to_convert, 0)
...         [print(data) for data in data_per_event]
>>> # now test with random event
>>> import random
>>> from mutwo import core_events
>>> random.seed(100)
>>> random_event = core_events.Concurrence(
...     [
...        core_events.Consecution(
...             [
...                core_events.Chronon(random.uniform(0.5, 2))
...                 for _ in range(random.randint(2, 5))
...             ]
...         )
...         for _ in range(random.randint(1, 3))
...     ]
... )
>>> DurationPrintConverter().convert(random_event)
D(0.0): D(1.1823905068)
D(1.1823905068): D(1.6561757085)
D(2.8385662153): D(1.5582698404)
D(4.3968360557): D(1.5979384595)
D(5.9947745152): D(1.1502716523)
class SymmetricalEventConverter[source]

Bases: EventConverter

Abstract base class for Converter which handle mutwo core_events.

This converter is a more specified version of the EventConverter. It helps for building converters which aim to return mutwo core_events.

mutwo.core_converters.configurations

Configure mutwo.core_converters

DEFAULT_DURATION_KEYWORD_NAME = 'duration'

Default value for duration_keyword_name parameter in mutwo.core_converters.MutwoParameterDictToDuration

DEFAULT_DURATION_TO_SEARCH_NAME = 'duration'

Default value for duration_to_search_name parameter in mutwo.core_converters.MutwoParameterDictToDuration