Exemple #1
0
    def generate_interval(self) -> (Interval, Pitch, Pitch):
        # Generate first random pitch
        pitch_generator = PitchGenerator(scale=self._scale,
                                         lowest_pitch=self._lowest_pitch,
                                         highest_pitch=self._highest_pitch,
                                         possible_detune=0)
        pitch1 = pitch_generator.generate_pitch()

        # Generate second random pitch
        possible_pitches = list()
        for pitch in self._scale.get_pitches(from_pitch=self._lowest_pitch,
                                             to_pitch=self._highest_pitch):
            interval = Interval.from_pitches(
                from_pitch=Pitch.lower(pitch1, pitch),
                to_pitch=Pitch.higher(pitch1, pitch))
            if self._largest_interval.get_cents()\
                    >= interval.get_cents()\
                    >= self._smallest_interval.get_cents():
                possible_pitches.append(pitch)
        pitch2 = random.choice(possible_pitches)

        # Return interval and pitches
        output_pitch1 = Pitch.from_pitch(pitch=Pitch.lower(pitch1, pitch2),
                                         detune=random.uniform(
                                             -self._possible_detune / 2,
                                             self._possible_detune / 2))
        output_pitch2 = Pitch.from_pitch(pitch=Pitch.higher(pitch1, pitch2),
                                         detune=random.uniform(
                                             -self._possible_detune / 2,
                                             self._possible_detune / 2))
        output_interval = Interval.from_pitches(from_pitch=output_pitch1,
                                                to_pitch=output_pitch2)
        return output_interval, output_pitch1, output_pitch2
Exemple #2
0
    def generate_interval(self) -> (Interval, Pitch, Pitch):
        # Generate first random pitch
        pitch1 = Pitch.from_frequency(
            random.uniform(self._lowest_pitch.get_frequency(),
                           self._highest_pitch.get_frequency()))

        # Generate second random pitch
        output_interval = random.choice(self._scale.get_intervals())
        pitch2 = pitch1.add_interval(output_interval)
        if pitch2 == Pitch.higher(self._highest_pitch, pitch2):
            pitch2 = pitch1.copy(detune=-output_interval.get_cents())
        if pitch2 == Pitch.lower(self._lowest_pitch, pitch2):
            raise RuntimeError(
                '[MicrotonalIntervalGenerator:generate_interval()] ' +
                'Could not generate interval! ' +
                'Make sure that lowest_pitch is sufficiently lower than highest_pitch!'
            )

        # Return interval and pitches
        output_pitch1 = Pitch.from_pitch(pitch=Pitch.lower(pitch1, pitch2),
                                         detune=random.uniform(
                                             -self._possible_detune / 2,
                                             self._possible_detune / 2))
        output_pitch2 = Pitch.from_pitch(pitch=Pitch.higher(pitch1, pitch2),
                                         detune=random.uniform(
                                             -self._possible_detune / 2,
                                             self._possible_detune / 2))
        return output_interval, output_pitch1, output_pitch2