File size: 3,387 Bytes
d1d6816
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import numpy as np

from configurations.read_configuration import get_conf_sample_rate, get_conf_stft_hyperparameter,\
    midi_parameter_range, get_conf_time_resolution, get_conf_max_n_notes
from melody_synth.random_duration import RandomDuration
from melody_synth.random_pitch import RandomPitch
from melody_synth.random_rhythm import RandomRhythm


class RandomMidi:
    """Pipeline generating random midi"""

    def __init__(self):
        self.randomRhythm = RandomRhythm()
        self.randomPitch = RandomPitch()
        self.randomDuration = RandomDuration()
        self.max_n_notes = get_conf_max_n_notes()

    def __call__(self, strategy=None, *args, **kwargs):
        """Assembles the pipeline based on given strategies and return random midi.

        Parameters
        ----------
        strategy: Dict[str, str]
                    Strategies names for random rhythm, pitch and duration generation (see Readme).

        Returns
        -------
        encode, midi: List[int], List[(float, float, float)]
                        encode -- Midi's label as a list of 0s and 1s
                        midi -- A list of (onset, pitch, duration) tuples, each tuple refers to a note
        """

        if strategy is None:
            strategy = {"rhythm_strategy": "non-test",
                        "pitch_strategy": "random_major",
                        "duration_strategy": "limited_random",
                        }

        midi = self.randomRhythm(strategy["rhythm_strategy"])
        midi = self.randomPitch(strategy["pitch_strategy"], midi)
        midi = self.randomDuration(strategy["duration_strategy"], midi)

        return self.get_encode(midi), midi

    def get_encode(self, midi):
        """Generate labels for midi

        Parameters
        ----------
        midi: List[(onset, pitch, duration)]
                A list of (onset, pitch, duration) tuples, each tuple refers to a note

        Returns
        -------
        encode: List[int]
                Midi's label as a list of 0s and 1s

        Encoding method
        -------
        One-hot Encoding for each note. Stack all note labels to form midi label.
        """
        duration_range = midi_parameter_range("duration")
        pitch_range = midi_parameter_range("pitch")
        time_resolution = get_conf_time_resolution()

        pixel_duration = get_conf_stft_hyperparameter()["frame_step"] / get_conf_sample_rate()
        single_note_encode_length = (time_resolution + len(pitch_range) + len(duration_range))
        encode_length = single_note_encode_length * self.max_n_notes
        encode = []
        for i in range(len(midi)):
            (location, pitch, duration) = midi[i]

            location_index = int(float(location) / pixel_duration)
            if location_index >= time_resolution:
                break
            pitch_index = pitch - pitch_range[0]
            duration_index = np.argmin(np.abs(np.array(duration_range) - duration))

            single_note_encode = np.zeros(single_note_encode_length)
            single_note_encode[location_index] = 1
            single_note_encode[time_resolution + pitch_index] = 1
            single_note_encode[time_resolution + len(pitch_range) + duration_index] = 1
            encode = np.hstack([encode, single_note_encode])

        return np.hstack([encode, np.zeros(encode_length)])[:encode_length]