def note_rhythm_zip(melody, note_sequence, rhythm_sequence, time_signature, interval=0.25): melody_sequence = mimic_melody(note_sequence, melody) melody_sequence = [ Note(elem.nameWithOctave, quarterLength=interval) if type(elem) is Note else Rest(quarterLength=interval) for elem in melody_sequence for i in range(0, int(elem.quarterLength / interval)) ] new_melody_sequence = [] elem = None bar_length = 0.0 # Handle notes in the melody due to bars and time signature def add_to_melody_sequence(new_melody_sequence, elem, bar_length): if type(elem) not in [Note, Rest]: pass elif bar_length + elem.quarterLength >= time_signature: extra = bar_length + elem.quarterLength - time_signature elem.quarterLength = time_signature - bar_length if elem.quarterLength > 0.0: new_melody_sequence += [elem] bar_length = extra # The possible extra note elem = Note(elem.nameWithOctave) if type(elem) is Note else Rest() elem.quarterLength = extra if elem.quarterLength > 0.0: new_melody_sequence += [elem] else: new_melody_sequence += [elem] bar_length += elem.quarterLength return (new_melody_sequence, elem, bar_length) for index, rhythm in enumerate(rhythm_sequence): if rhythm == 'Hold' and type(elem) is Note: elem.quarterLength += interval elif rhythm == 'Note': new_melody_sequence, elem, bar_length = add_to_melody_sequence( new_melody_sequence, elem, bar_length) elem = melody_sequence[index] elem.quarterLength = interval elif rhythm == 'Rest' and type(elem) is Rest: elem.quarterLength += interval elif rhythm == 'Rest' or rhythm == 'Hold': new_melody_sequence, elem, bar_length = add_to_melody_sequence( new_melody_sequence, elem, bar_length) elem = Rest() elem.quarterLength = interval new_melody_sequence, elem, bar_length = add_to_melody_sequence( new_melody_sequence, elem, bar_length) return new_melody_sequence