def _calculate_metricity_of_melody( time_signature: tuple, melody: old.Melody ) -> float: duration = melody.duration fitness_per_beat = {} rising = fractions.Fraction(1, 8) position = 0 for _ in range( int( math.ceil( duration / (time_signature[0].numerator / time_signature[0].denominator) ) ) ): for fitness in time_signature[1]: fitness_per_beat.update({position: fitness}) position += rising metricity = 0 for tone in melody.convert2absolute(): if tone.pitch: try: metricity += fitness_per_beat[tone.delay] except KeyError: metricity -= 0.01 return metricity
def mk_envelope( voice: old.Melody, shadow_time: float, duration: float, average_volume: float, min_volume: float, max_volume: float, make_envelope: bool, anticipation_time: float, overlaying_time: float, ) -> interpolations.InterpolationLine: volume_difference = max_volume - min_volume max_volume_of_melody = max(voice.volume) def detect_volume(tone_volume: float) -> float: percentage = tone_volume / max_volume_of_melody return (percentage * volume_difference) + min_volume if make_envelope: events = tuple( (float(tone.delay) + anticipation_time, detect_volume(tone.volume)) for tone in voice.convert2absolute() ) envelope = interpolations.ShadowInterpolationLine( average_volume, shadow_time, events, duration ) else: envelope = interpolations.InterpolationLine( ( interpolations.FloatInterpolationEvent(duration, max_volume), interpolations.FloatInterpolationEvent(0, max_volume), ) ) return envelope