|
from configurations.read_configuration import midi_parameter_range, midi_is_discrete |
|
from data_generation.encoding import ParameterDescription |
|
|
|
|
|
def get_full_duration(midi): |
|
"""Uses "full_duration" strategy to generate random duration (see Readme).""" |
|
n = len(midi) |
|
(time_starting_point, pitch) = midi[n - 1] |
|
new_midi = [(time_starting_point, pitch, 0.5)] |
|
next_time_starting_point = time_starting_point |
|
for i in range(n - 1): |
|
(time_starting_point, pitch) = midi[n - 2 - i] |
|
duration = (next_time_starting_point - time_starting_point) * 0.9 |
|
new_midi.insert(0, (time_starting_point, pitch, duration)) |
|
next_time_starting_point = time_starting_point |
|
|
|
return new_midi |
|
|
|
|
|
def get_random_duration(midi): |
|
"""Uses "random_duration" strategy to generate random duration (see Readme).""" |
|
parameterDescription = ParameterDescription(name="duration", |
|
values=midi_parameter_range('duration'), |
|
discrete=midi_is_discrete('duration')) |
|
n = len(midi) |
|
new_midi = [] |
|
for i in range(n): |
|
(location, pitch) = midi[i] |
|
duration = float(parameterDescription.generate().value) |
|
new_midi.append((location, pitch, duration)) |
|
return new_midi |
|
|
|
|
|
def get_fixed_duration(midi): |
|
return [(location, pitch, 2.0) for (location, pitch) in midi] |
|
|
|
|
|
def get_limited_random_duration(midi): |
|
"""Uses "limited_random_duration" strategy to generate random duration (see Readme).""" |
|
parameterDescription = ParameterDescription(name="duration", |
|
values=midi_parameter_range('duration'), |
|
discrete=midi_is_discrete('duration')) |
|
n = len(midi) |
|
(time_starting_point, pitch) = midi[n - 1] |
|
duration = float(parameterDescription.generate().value) |
|
new_midi = [(time_starting_point, pitch, duration)] |
|
next_time_starting_point = time_starting_point |
|
for i in range(n - 1): |
|
(time_starting_point, pitch) = midi[n - 2 - i] |
|
max_duration = (next_time_starting_point - time_starting_point) * 0.9 |
|
duration = float(parameterDescription.generate().value) |
|
duration = min(duration, max_duration) |
|
new_midi.insert(0, (time_starting_point, pitch, duration)) |
|
next_time_starting_point = time_starting_point |
|
|
|
return new_midi |
|
|
|
|
|
class RandomDuration: |
|
"""Third component in the random midi pipeline responsible for random duration (keyboard hold time) generating""" |
|
|
|
def __call__(self, strategy: str, midi, *args, **kwargs): |
|
"""Choose required strategy to generate random duration for each note. |
|
|
|
Parameters |
|
---------- |
|
strategy: str |
|
Strategy names for random duration (see Readme). |
|
midi: List[(float, float)] |
|
Random rhythm and pitch from previous pipeline component. |
|
|
|
Returns |
|
------- |
|
midi: List[(float, float, float)] |
|
Original input list with duration assigned to each note onset. |
|
""" |
|
|
|
if strategy == 'random_duration': |
|
midi = get_random_duration(midi) |
|
elif strategy == 'limited_random_duration': |
|
midi = get_limited_random_duration(midi) |
|
elif strategy == 'fixed_duration': |
|
midi = get_fixed_duration(midi) |
|
else: |
|
midi = get_full_duration(midi) |
|
return midi |
|
|