VAE_sound / melody_synth /random_pitch.py
WeixuanYuan's picture
Upload 35 files
d1d6816
import numpy as np
from configurations.read_configuration import midi_parameter_range
class RandomPitch:
"""Second component in the random midi pipeline responsible for random rhythm (note onsets) generating"""
def __init__(self):
self.pitch_range = midi_parameter_range("pitch")
self.major = [0, 2, 4, 5, 7, 9, 11]
self.minor = [0, 2, 3, 5, 7, 8, 10]
def __call__(self, strategy: str, onsets, *args, **kwargs):
"""Choose required strategy to generate random pitch for each note.
Parameters
----------
strategy: str
Strategy names for random pitches (see Readme).
onsets: List[float]
Random rhythm from previous pipeline component.
Returns
-------
midi: List[(float, float)]
Original input list with pitches assigned to each note onset.
"""
if strategy == 'random_major':
return self.get_random_major(onsets)
elif strategy == 'random_minor':
return self.get_random_minor(onsets)
elif strategy == 'fixed_pitch':
return self.get_fixed_pitch(onsets)
elif strategy == 'fixed_pitch1':
return self.get_fixed_pitch1(onsets)
elif strategy == 'fixed_pitch2':
return self.get_fixed_pitch2(onsets)
elif strategy == 'fixed_pitch3':
return self.get_fixed_pitch3(onsets)
elif strategy == 'fixed_pitch4':
return self.get_fixed_pitch4(onsets)
else:
return self.get_random_pitch(onsets)
def get_random_major(self, midi):
"""Uses "random_major" strategy to generate random pitches (see Readme)."""
random_scale = np.random.randint(0, 12)
scale = [one for one in self.pitch_range if (one - random_scale) % 12 in self.major]
midi = [(duration, scale[np.random.randint(0, len(scale))]) for duration in midi]
# midi[0] = (midi[0][0], random_scale + self.pitch_range[-1])
midi[len(midi) - 1] = (midi[len(midi) - 1][0], random_scale + self.pitch_range[0])
return midi
def get_random_pitch(self, midi):
"""Uses "free_pitch" strategy to generate random pitches (see Readme)."""
return [(duration, np.random.randint(self.pitch_range[0], self.pitch_range[-1])) for duration in midi]
def get_fixed_pitch(self, midi):
"""Uses "free_pitch" strategy to generate random pitches (see Readme)."""
return [(duration, 48) for duration in midi]
def get_fixed_pitch1(self, midi):
"""Uses "free_pitch" strategy to generate random pitches (see Readme)."""
return [(duration, 55) for duration in midi]
def get_fixed_pitch2(self, midi):
"""Uses "free_pitch" strategy to generate random pitches (see Readme)."""
return [(duration, 62) for duration in midi]
def get_fixed_pitch3(self, midi):
"""Uses "free_pitch" strategy to generate random pitches (see Readme)."""
return [(duration, 69) for duration in midi]
def get_fixed_pitch4(self, midi):
"""Uses "free_pitch" strategy to generate random pitches (see Readme)."""
return [(duration, 76) for duration in midi]
def get_random_minor(self, midi):
"""Uses "random_minor" strategy to generate random pitches (see Readme)."""
random_scale = np.random.randint(0, 12)
scale = [one for one in self.pitch_range if (one - random_scale) % 12 in self.minor]
midi = [(duration, scale[np.random.randint(0, len(scale))]) for duration in midi]
# midi[0] = (midi[0][0], random_scale + self.pitch_range[-1])
midi[len(midi) - 1] = (midi[len(midi) - 1][0], random_scale + self.pitch_range[0])
return midi