Search code examples
pythonmusic21

music21: How to append a chord to a stream simultaneously?


I have a stream object with notes (pitch and duration). I want to add chords to -for example - the first note of each 4 times. But I want them to sound at the same time.

The problem is that the only related stuff I found was how to append a chord to a stream but sequentially.

So... Any suggestions?


Solution

  • If you want to add additional pitches into existing notes, use the stream.Stream.insertIntoNoteOrChord method:

    http://web.mit.edu/music21/doc/moduleReference/moduleStream.html#music21.stream.Stream.insertIntoNoteOrChord

    For instance:

    s = stream.Stream()
    n = note.Note('C4') # qtr note default
    s.append(n)
    
    c = chord.Chord('E4 G4') # qtr
    s.insertIntoNoteOrChord(0.0, c)
    s.show('t')
    {0.0} <music21.chord.Chord C4 E4 G4>
    

    If you need to do something more complex, then I suggest just inserting all of the notes and chords to wherever you want them to be, and then running .chordify() on the Stream to make everything work.

    A third option is to use different stream.Voice() objects for the different layers.