def duration_changes(individual: Individual): notes = individual.get_flattened_notes() durations = list(map(lambda x: x.duration.duration_value, notes)) counter = 0 for i in range(1, len(durations)): d_1 = durations[i - 1] d_2 = durations[i] if d_1 / d_2 >= 4.0: counter += 1 return - (counter / (len(notes) - 1))
def duration_patterns(individual: Individual): notes = individual.get_flattened_notes() durations = list(map(lambda x: x.duration.duration_name, notes)) pattern_counter = find_patterns(durations) fitness = 0.0 for k, v in pattern_counter.items(): divider = len(notes) - k + 1 fitness += v / divider if fitness == 0.0: return 0.0 return fitness / len(pattern_counter.items())
def intervallic_patterns(individual: Individual): notes: [Note] = individual.get_flattened_notes() notes = list(filter(lambda x: x.pitch != 'REST', notes)) intervals = [] for i in range(1, len(notes)): n1 = notes[i - 1].to_music21_note() n2 = notes[i].to_music21_note() n1_scalestep = get_scale_position(n1.nameWithOctave) n2_scalestep = get_scale_position(n2.nameWithOctave) scalestep_interval = n2_scalestep - n1_scalestep intervals.append(scalestep_interval) pattern_length_counter = find_patterns(intervals) fitness = 0.0 for k, v in pattern_length_counter.items(): # Maximum number of k length patterns in piece divider = len(notes) - k + 1 fitness += v / divider if fitness == 0.0: return 0.0 return fitness / len(pattern_length_counter.items())