User’s Guide, Chapter 46: Streams 3: Manipulation, inPlace, and deepcopy

Just getting started here…

Replace all consecutive notes of a Stream with intervals while maintaining the hierarchy

import copy

def intervalStream(inputStream):
    out = copy.deepcopy(inputStream)
    for n in out.recurse().getElementsByClass('Note'):
        nNext = n.next('GeneralNote')
        if nNext is None or not nNext.isNote:
            continue # Rests, Chords, etc.
        n.activeSite.replace(n, interval.Interval(n, nNext))
    for other in out.recurse().getElementsNotOfClass(['Interval', 'Stream']):
        other.activeSite.remove(other)  # get rid of everyhing that is not an Interval or Stream
    return out
b = corpus.parse('bwv66.6')
ints = intervalStream(b.measures(0, 4))
ints.show('text')
 {0.0} <music21.stream.Part Soprano>
     {0.0} <music21.stream.Measure 0 offset=0.0>
         {0.0} <music21.interval.Interval M-2>
         {0.5} <music21.interval.Interval M-2>
     {1.0} <music21.stream.Measure 1 offset=1.0>
         {0.0} <music21.interval.Interval M2>
         {1.0} <music21.interval.Interval M2>
         {2.0} <music21.interval.Interval m3>
         {3.0} <music21.interval.Interval m-3>
     {5.0} <music21.stream.Measure 2 offset=5.0>
         {0.0} <music21.interval.Interval M-2>
         {1.0} <music21.interval.Interval M-2>
         {2.0} <music21.interval.Interval M3>
         {3.0} <music21.interval.Interval M-3>
     {9.0} <music21.stream.Measure 3 offset=9.0>
         {0.0} <music21.interval.Interval M2>
         {0.5} <music21.interval.Interval m-3>
         {1.0} <music21.interval.Interval M-2>
         {2.0} <music21.interval.Interval m3>
         {3.0} <music21.interval.Interval M2>
     {13.0} <music21.stream.Measure 4 offset=13.0>
         {0.0} <music21.interval.Interval P1>
         {1.0} <music21.interval.Interval P-4>
         {2.0} <music21.interval.Interval M-2>
         {3.0} <music21.interval.Interval P4>
 {0.0} <music21.stream.Part Alto>
     {0.0} <music21.stream.Measure 0 offset=0.0>
         {0.0} <music21.interval.Interval M2>
     {1.0} <music21.stream.Measure 1 offset=1.0>
         {0.0} <music21.interval.Interval M-2>
         {1.0} <music21.interval.Interval P1>
         {2.0} <music21.interval.Interval P1>
         {3.0} <music21.interval.Interval P1>
     {5.0} <music21.stream.Measure 2 offset=5.0>
         {0.0} <music21.interval.Interval P4>
         {0.5} <music21.interval.Interval m-2>
         {1.0} <music21.interval.Interval M-3>
         {2.0} <music21.interval.Interval M3>
         {3.0} <music21.interval.Interval M-2>
     {9.0} <music21.stream.Measure 3 offset=9.0>
         {0.0} <music21.interval.Interval M2>
         {0.5} <music21.interval.Interval m-3>
         {1.0} <music21.interval.Interval M-3>
         {2.0} <music21.interval.Interval P4>
         {3.0} <music21.interval.Interval P1>
     {13.0} <music21.stream.Measure 4 offset=13.0>
         {0.0} <music21.interval.Interval M-2>
         {1.0} <music21.interval.Interval m-2>
         {2.0} <music21.interval.Interval M-2>
         {3.0} <music21.interval.Interval P1>
 {0.0} <music21.stream.Part Tenor>
     {0.0} <music21.stream.Measure 0 offset=0.0>
         {0.0} <music21.interval.Interval M2>
         {0.5} <music21.interval.Interval M2>
     {1.0} <music21.stream.Measure 1 offset=1.0>
         {0.0} <music21.interval.Interval M-2>
         {1.0} <music21.interval.Interval M-2>
         {2.0} <music21.interval.Interval M2>
         {3.0} <music21.interval.Interval M-2>
     {5.0} <music21.stream.Measure 2 offset=5.0>
         {0.0} <music21.interval.Interval P5>
         {0.5} <music21.interval.Interval P1>
         {1.0} <music21.interval.Interval M-2>
         {1.5} <music21.interval.Interval m-2>
         {2.0} <music21.interval.Interval P1>
         {3.0} <music21.interval.Interval P1>
     {9.0} <music21.stream.Measure 3 offset=9.0>
         {0.0} <music21.interval.Interval m2>
         {0.5} <music21.interval.Interval m-2>
         {1.0} <music21.interval.Interval M-2>
         {1.5} <music21.interval.Interval M-2>
         {2.0} <music21.interval.Interval M3>
         {3.0} <music21.interval.Interval M-2>
     {13.0} <music21.stream.Measure 4 offset=13.0>
         {0.0} <music21.interval.Interval P1>
         {1.0} <music21.interval.Interval P1>
         {2.0} <music21.interval.Interval M-2>
         {2.5} <music21.interval.Interval m-2>
         {3.0} <music21.interval.Interval M-2>
 {0.0} <music21.stream.Part Bass>
     {0.0} <music21.stream.Measure 0 offset=0.0>
         {0.0} <music21.interval.Interval m-2>
         {0.5} <music21.interval.Interval M-2>
     {1.0} <music21.stream.Measure 1 offset=1.0>
         {0.0} <music21.interval.Interval M2>
         {1.0} <music21.interval.Interval m2>
         {2.0} <music21.interval.Interval m-2>
         {3.0} <music21.interval.Interval m2>
     {5.0} <music21.stream.Measure 2 offset=5.0>
         {0.0} <music21.interval.Interval m-6>
         {0.5} <music21.interval.Interval m3>
         {1.0} <music21.interval.Interval P-5>
         {2.0} <music21.interval.Interval A5>
         {3.0} <music21.interval.Interval m2>
     {9.0} <music21.stream.Measure 3 offset=9.0>
         {0.0} <music21.interval.Interval P-5>
         {0.5} <music21.interval.Interval M2>
         {1.0} <music21.interval.Interval P-5>
         {2.0} <music21.interval.Interval P8>
         {3.0} <music21.interval.Interval M2>
     {13.0} <music21.stream.Measure 4 offset=13.0>
         {0.0} <music21.interval.Interval M-2>
         {0.5} <music21.interval.Interval M2>
         {1.0} <music21.interval.Interval m2>
         {1.5} <music21.interval.Interval M2>
         {2.0} <music21.interval.Interval P-8>
         {2.5} <music21.interval.Interval M2>
         {3.0} <music21.interval.Interval P4>