how to keep offsets in music21 sequences?

23 views Asked by At

this function is supposed to replace one note in a sequence with two notes of the same value but half durations:

def double_note(mystream):
    random_index = randint(0, (len(mystream) - 1))
    if len(mystream.getElementsByClass(note.Note)) > 0 and isinstance(mystream[random_index], note.Note):
        selected_note = mystream[random_index]
        # Create two new notes of half the duration
        new_note1 = note.Note(selected_note.pitch, quarterLength=selected_note.duration.quarterLength / 2)
        new_note2 = note.Note(selected_note.pitch, quarterLength=selected_note.duration.quarterLength / 2)
        # Adjust offsets of the new notes
        new_note1.offset = selected_note.offset
        new_note2.offset = selected_note.offset + new_note1.duration.quarterLength
        print(selected_note, selected_note.duration.quarterLength)
        print(new_note1.offset, new_note2.offset)
        # Replace the selected note in the sequence with the two new notes
        mystream.pop(random_index)
        mystream.insert(random_index, new_note1)
        mystream.insert(random_index + 1, new_note2)
    else:
         pass
    return mystream

but my two new notes will never land at the calculated offsets, but anywhere else. what am i doing wrong?

0

There are 0 answers