mutwo.core_converters§
Convert data from and to mutwo.
Object |
Documentation |
---|---|
Extract from a simple event an attribute. |
|
Extract from a dict of mutwo parameters specific objects. |
|
Extract from a dict of mutwo parameters the duration. |
|
Convert a dict of mutwo parameters to a |
|
Helper to simplify standardisation of syntactic sugar. |
|
|
Convert a |
Apply tempo curves on mutwo events |
|
Apply tempo envelope of event on itself |
- class SimpleEventToAttribute(attribute_name, exception_value)[source]§
Extract from a simple event an attribute.
- Parameters:
attribute_name (str) – The name of the attribute which is fetched from a
mutwo.core_events.SimpleEvent
.exception_value (Any) – This value is returned in case an AttributeError raises .
Public Methods:
__init__
(attribute_name, exception_value)convert
(simple_event_to_convert)Extract from a
mutwo.core_events.SimpleEvent
an attribute.Inherited from
Converter
convert
(simple_event_to_convert)Extract from a
mutwo.core_events.SimpleEvent
an attribute.__call__
(*args, **kwargs)Call self as a function.
Private Data Attributes:
_abc_impl
Inherited from
Converter
_abc_impl
Inherited from
ABC
_abc_impl
- convert(simple_event_to_convert)[source]§
Extract from a
mutwo.core_events.SimpleEvent
an attribute.- Parameters:
simple_event_to_convert (mutwo.core_events.SimpleEvent) – The
mutwo.core_events.SimpleEvent
from which an attribute shall be extracted.- Return type:
Any
Example:
>>> from mutwo import core_converters >>> from mutwo import core_events >>> simple_event = core_events.SimpleEvent(duration=10) >>> simple_event_to_duration = core_converters.SimpleEventToAttribute( 'duration', 0 ) >>> simple_event_to_duration.convert(simple_event) 10 >>> simple_event_to_pasta = core_converters.SimpleEventToAttribute( 'pasta', 'spaghetti' ) >>> simple_event_to_pasta.convert(simple_event) 'spaghetti' >>> simple_event.pasta = 'tagliatelle' >>> simple_event_to_pasta.convert(simple_event) 'tagliatelle'
- class MutwoParameterDictToKeywordArgument(mutwo_parameter_to_search_name, keyword=None)[source]§
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 music_parameters >>> mutwo_parameter_dict_to_keyword_argument = core_converters.MutwoParameterDictToKeywordArgument('pitch') >>> mutwo_parameter_dict_to_keyword_argument.convert( {'pitch': music_parameters.WesternPitch('c')} ) ('pitch', music_parameters.WesternPitch(c4))
Public Methods:
__init__
(mutwo_parameter_to_search_name[, ...])convert
(mutwo_parameter_dict_to_convert)Inherited from
Converter
convert
(mutwo_parameter_dict_to_convert)__call__
(*args, **kwargs)Call self as a function.
Private Data Attributes:
_abc_impl
Inherited from
Converter
_abc_impl
Inherited from
ABC
_abc_impl
- class MutwoParameterDictToDuration(duration_to_search_name=None, duration_keyword_name=None)[source]§
Extract from a dict of mutwo parameters the duration.
- Parameters:
duration_to_search_name (Optional[str]) – The name of the duration which shall be searched for in the
MutwoParameterDict
. If None the value of the global constantsmutwo.core_converters.configurations.DEFAULT_DURATION_TO_SEARCH_NAME
will be used. Default to None.duration_keyword_name (typing.Optional[str]
mutwo.core_converters.configurations.DEFAULT_DURATION_KEYWORD_NAME
.) – The name of the duration keyword for the event. If None the value of the global constantsmutwo.core_converters.configurations.DEFAULT_DURATION_KEYWORD_NAME
will be used. Default to None.
Public Methods:
__init__
([duration_to_search_name, ...])Inherited from
MutwoParameterDictToKeywordArgument
__init__
([duration_to_search_name, ...])convert
(mutwo_parameter_dict_to_convert)Inherited from
Converter
convert
(mutwo_parameter_dict_to_convert)__call__
(*args, **kwargs)Call self as a function.
Private Data Attributes:
_abc_impl
Inherited from
MutwoParameterDictToKeywordArgument
_abc_impl
Inherited from
Converter
_abc_impl
Inherited from
ABC
_abc_impl
- convert(mutwo_parameter_dict_to_convert)§
- Parameters:
mutwo_parameter_dict_to_convert (dict[str, Any]) –
- Return type:
Optional[tuple[str, Any]]
- class MutwoParameterDictToSimpleEvent(mutwo_parameter_dict_to_keyword_argument_sequence=None, simple_event_class=<class 'mutwo.core_events.basic.SimpleEvent'>)[source]§
Convert a dict of mutwo parameters to a
mutwo.core_events.SimpleEvent
- Parameters:
mutwo_parameter_dict_to_keyword_argument_sequence (Optional[Sequence[MutwoParameterDictToKeywordArgument]]) – A sequence of
MutwoParameterDictToKeywordArgument
. If set to None a sequence withMutwoParameterDictToDuration
will be created. Default to None.simple_event_class (Type[core_events.SimpleEvent]) – Default to
mutwo.core_events.SimpleEvent
.
Public Methods:
__init__
([...])convert
(mutwo_parameter_dict_to_convert)Inherited from
Converter
convert
(mutwo_parameter_dict_to_convert)__call__
(*args, **kwargs)Call self as a function.
Private Data Attributes:
_abc_impl
Inherited from
Converter
_abc_impl
Inherited from
ABC
_abc_impl
- class UnknownObjectToObject(type_tuple_and_callable_tuple)[source]§
Helper to simplify standardisation of syntactic sugar.
- Parameters:
type_tuple_to_callable_dict – Define which types are converted by which methods.
type_tuple_and_callable_tuple (tuple[tuple[Type, ...], Callable]) –
Example:
>>> from mutwo impot core_converters >>> anything_to_string = core_converters.UnknownObjectToObject[str]( >>> ( >>> ((float, int, list), str), >>> ((tuple,), lambda t: str(len(t))), >>> ([], lambda _: "..."), >>> ) >>> ) >>> anything_to_string.convert(100) "100" >>> anything_to_string.convert(7.32) "7.32" >>> anything_to_string.convert((1, 2, 3)) "3" >>> anything_to_string.convert(b'') "..."
Public Methods:
__init__
(type_tuple_and_callable_tuple)convert
(unknown_object_to_convert)Inherited from
Converter
convert
(unknown_object_to_convert)__call__
(*args, **kwargs)Call self as a function.
Inherited from
Generic
__class_getitem__
(params)__init_subclass__
(*args, **kwargs)This method is called when a class is subclassed.
Private Data Attributes:
_abc_impl
Inherited from
Converter
_abc_impl
Inherited from
ABC
_abc_impl
Inherited from
Generic
_is_protocol
- class TempoConverter(tempo_envelope, apply_converter_on_events_tempo_envelope=True)[source]§
Apply tempo curves on mutwo events
- Parameters:
tempo_envelope (TempoEnvelope) – The tempo curve that shall be applied on the mutwo events. This is expected to be a
core_events.TempoEnvelope
which values are filled with numbers that will be interpreted as BPM [beats per minute]) or withmutwo.core_parameters.TempoPoint
objects.apply_converter_on_events_tempo_envelope (bool) – If set to True the converter will also adjust the
tempo_envelope
attribute of each converted event. Default to True.
Example:
>>> from mutwo import core_converters >>> from mutwo import core_events >>> from mutwo import core_parameters >>> tempo_envelope = core_events.Envelope( >>> [[0, tempos.TempoPoint(60)], [3, 60], [3, 30], [5, 50]], >>> ) >>> my_tempo_converter = core_converters.TempoConverter(tempo_envelope)
Public Methods:
__init__
(tempo_envelope[, ...])convert
(event_to_convert)Apply tempo curve of the converter to the entered event.
Inherited from
Converter
convert
(event_to_convert)Apply tempo curve of the converter to the entered event.
__call__
(*args, **kwargs)Call self as a function.
Private Data Attributes:
_tempo_point_to_beat_length_in_seconds
(...)Converts a
TempoPoint
to beat-length-in-seconds._abc_impl
Inherited from
EventConverter
_abc_impl
Inherited from
Converter
_abc_impl
Inherited from
ABC
_abc_impl
- convert(event_to_convert)[source]§
Apply tempo curve of the converter to the entered event.
The method doesn’t change the original event, but returns a copied version with different values for its duration attributes depending on the tempo curve.
- 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 theTempoConverter
, then the last tempo of the curve will be hold.- Returns:
A new
Event
object which duration property has been adapted by the tempo curve of theTempoConverter
.- Return type:
Example:
>>> from mutwo import core_converters >>> from mutwo import core_events >>> from mutwo import core_parameters >>> tempo_envelope = core_events.Envelope( >>> [[0, tempos.TempoPoint(60)], [3, 60], [3, 30], [5, 50]], >>> ) >>> my_tempo_converter = core_converters.TempoConverter(tempo_envelope) >>> my_events = core_events.SequentialEvent([core_events.SimpleEvent(d) for d in (3, 2, 5)]) >>> my_tempo_converter.convert(my_events) SequentialEvent([SimpleEvent(duration = 3.0), SimpleEvent(duration = 1.5), SimpleEvent(duration = 2.5)])
- class EventToMetrizedEvent(skip_level_count=None, maxima_depth_count=None)[source]§
Apply tempo envelope of event on itself
Public Methods:
__init__
([skip_level_count, maxima_depth_count])convert
(event_to_convert)Apply tempo envelope of event on itself
Inherited from
Converter
convert
(event_to_convert)Apply tempo envelope of event on itself
__call__
(*args, **kwargs)Call self as a function.
Private Data Attributes:
_abc_impl
Inherited from
SymmetricalEventConverter
_abc_impl
Inherited from
EventConverter
_abc_impl
Inherited from
Converter
_abc_impl
Inherited from
ABC
_abc_impl
- Parameters:
skip_level_count (Optional[int]) –
maxima_depth_count (Optional[int]) –
mutwo.core_converters.abc§
Defining the public API for any converter class.
- class Converter[source]§
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.
- class EventConverter[source]§
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.SimpleEvent
). This common iteration process and the different time treatment betweenmutwo.core_events.SequentialEvent
andmutwo.core_events.SimultaneousEvent
are implemented inEventConverter
. For writing a new EventConverter class, one only has to override the abstract method_convert_simple_event()
and the abstract methodconvert()
(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_simple_event(self, event_to_convert, absolute_entry_delay): >>> return "{}: {}".format(absolute_entry_delay, 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.SimultaneousEvent( >>> [ >>> core_events.SequentialEvent( >>> [ >>> core_events.SimpleEvent(random.uniform(0.5, 2)) >>> for _ in range(random.randint(2, 5)) >>> ] >>> ) >>> for _ in range(random.randint(1, 3)) >>> ] >>> ) >>> DurationPrintConverter().convert(random_event) 0: 1.182390506771032 1.182390506771032: 1.6561757084885333 2.8385662152595654: 1.558269840401042 4.396836055660607: 1.5979384595498836 5.994774515210491: 1.1502716523431056
- abstract convert(event_or_parameter_or_file_to_convert, *args, **kwargs)§
- Parameters:
event_or_parameter_or_file_to_convert (Any) –
- Return type:
Any
- class SymmetricalEventConverter[source]§
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.- abstract convert(event_or_parameter_or_file_to_convert, *args, **kwargs)§
- Parameters:
event_or_parameter_or_file_to_convert (Any) –
- Return type:
Any
mutwo.core_converters.configurations§
Configure mutwo.core_converters
- DEFAULT_DURATION_KEYWORD_NAME = 'duration'§
Default value for
duration_keyword_name
parameter inmutwo.core_converters.MutwoParameterDictToDuration
- DEFAULT_DURATION_TO_SEARCH_NAME = 'duration'§
Default value for
duration_to_search_name
parameter inmutwo.core_converters.MutwoParameterDictToDuration