File size: 3,765 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
88
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