Lee Hampton
Lee Hampton

Reputation: 1109

Generating 8th note tuplets using music21

I'm using music21 to add a given sequence of notes to arbitrary tuplet divisions. For example, given "CEGBCAFD" in the time signature of 2/4, I want to create measures with quintuplets (e.g., 5 over 2) that go over that sequence.

Here's the python code I came up with to generate the tuplet:

    for i, note_name in enumerate(notes):
        # Create a note
        n = note.Note(note_name)

        # Set note duration to eighth and within a tuplet
        n.duration = duration.Duration('eighth')

        # Create tuplet if needed and add it to note's duration
        if tuplet_counter % rhythm == 0:
            tup = duration.Tuplet(numberNotesActual=rhythm, numberNotesNormal=2, type='quarter')
            tup.setDurationType('eighth')  # Set the tuplet's note type to eighth
            n.duration.appendTuplet(tup)

        # Add accent if the note is in the accents list
        if note_name in accents:
            n.articulations.append(articulations.Accent())

        #measure.makeBeams(inPlace=True)
        measure.append(n)
        tuplet_counter += 1

        # If we reach the rhythm count or the end of the notes, reset the counter and append the measure
        if tuplet_counter == rhythm or note_name == notes[-1]:
            part.append(measure)
            measure = stream.Measure()  # Create a new measure for the next group
            tuplet_counter = 0
            if (i + 1) < len(notes):  # If there are more notes, set the attributes for the new measure
                measure.append(key.Key(key_signature))
                measure.append(meter.TimeSignature(time_signature))

    # Add the part to the score
    s.append(part)

It does a fine job of creating 5 note tuplets, but the XML it generates makes them 16th notes for some reason, which creates a 10 over 2 feel rather than a 5 over 2 feel. How can I get it to give me 5 8th notes per measure in a single tuplet?

XML snippet:

      <note>
        <pitch>
          <step>C</step>
          <octave>4</octave>
        </pitch>
        <duration>2016</duration>
        <type>16th</type>
        <time-modification>
          <actual-notes>5</actual-notes>
          <normal-notes>4</normal-notes>
          <normal-type>16th</normal-type>
        </time-modification>
        <beam number="1">begin</beam>
    ```

Upvotes: 1

Views: 53

Answers (0)

Related Questions