Timespan Demos

Work in progress… (This has also turned into a general sandbox of unstable things)

from music21 import *
luca = corpus.parse('luca/gloria').measures(1, 8)
tsCol = luca.asTimespans()
tsCol
 <TimespanTree {91} (0.0 to 24.0) <music21.stream.Score luca/gloria.xml>>
for v in tsCol.iterateVerticalities():
    print(v)
 <music21.tree.verticality.Verticality 0.0 {F3 F4 C5}>
 <music21.tree.verticality.Verticality 3.0 {F3 F4 D5}>
 <music21.tree.verticality.Verticality 3.5 {F3 C4 C5}>
 <music21.tree.verticality.Verticality 4.5 {F3 C4 A4}>
 <music21.tree.verticality.Verticality 5.0 {F3 C4 F4}>
 <music21.tree.verticality.Verticality 5.5 {F3 A3 F4}>
 <music21.tree.verticality.Verticality 6.0 {F3 F4}>
 <music21.tree.verticality.Verticality 6.5 {F3 F4 A4}>
 <music21.tree.verticality.Verticality 7.5 {F3 F4 C5}>
 <music21.tree.verticality.Verticality 8.5 {F3 F4 C5}>
 <music21.tree.verticality.Verticality 9.0 {C4 E4 G4}>
 <music21.tree.verticality.Verticality 10.5 {D4 F4 A4}>
 <music21.tree.verticality.Verticality 12.0 {C4 E4 G4}>
 <music21.tree.verticality.Verticality 15.0 {G3 G4 D5}>
 <music21.tree.verticality.Verticality 16.5 {A3 E4 C5}>
 <music21.tree.verticality.Verticality 17.5 {A3 E4 B4}>
 <music21.tree.verticality.Verticality 18.0 {D4 F4 A4}>
 <music21.tree.verticality.Verticality 18.5 {D4 F4 G4}>
 <music21.tree.verticality.Verticality 19.0 {D4 F4}>
 <music21.tree.verticality.Verticality 19.5 {C4 E4 G4}>
 <music21.tree.verticality.Verticality 20.0 {C4 F4 G4}>
 <music21.tree.verticality.Verticality 20.5 {C4 D4 G4}>
 <music21.tree.verticality.Verticality 21.0 {C4 E4}>
 <music21.tree.verticality.Verticality 21.5 {A3 E4}>
 <music21.tree.verticality.Verticality 22.0 {A3 C4 F4}>
 <music21.tree.verticality.Verticality 22.5 {B-3 D4 F4}>
 <music21.tree.verticality.Verticality 23.0 {G3 B-3 D4}>
 <music21.tree.verticality.Verticality 23.5 {B-3 D4 F4}>
v4 = list(tsCol.iterateVerticalities())[4]
v4
 <music21.tree.verticality.Verticality 5.0 {F3 C4 F4}>
v4.pitchSet
 {<music21.pitch.Pitch F3>, <music21.pitch.Pitch C4>, <music21.pitch.Pitch F4>}
f = sorted(v4.pitchSet)[0]
f
 <music21.pitch.Pitch F3>
v4.startTimespans
 (<PitchedTimespan (5.0 to 6.0) <music21.note.Note F>>,)
v4.stopTimespans
 (<PitchedTimespan (4.5 to 5.0) <music21.note.Note A>>,)
v4.overlapTimespans
 (<PitchedTimespan (3.0 to 6.0) <music21.note.Note F>>,
  <PitchedTimespan (4.5 to 5.5) <music21.note.Note C>>)
v35 = tsCol.getVerticalityAt(3.5)
v35
 <music21.tree.verticality.Verticality 3.5 {F3 C4 C5}>
v35.startTimespans
 (<PitchedTimespan (3.5 to 4.5) <music21.note.Note C>>,
  <PitchedTimespan (3.5 to 4.5) <music21.note.Note C>>)
v35.stopTimespans
 (<PitchedTimespan (3.0 to 3.5) <music21.note.Note D>>,
  <PitchedTimespan (3.0 to 3.5) <music21.note.Note F>>)
v35.overlapTimespans
 (<PitchedTimespan (3.0 to 6.0) <music21.note.Note F>>,)
elTsC = v35.startTimespans[0]
elTsC, elTsC.element
 (<PitchedTimespan (3.5 to 4.5) <music21.note.Note C>>, <music21.note.Note C>)

Test previousVerticality with Rest

v22 = tsCol.getVerticalityAt(22.0)
v22
 <music21.tree.verticality.Verticality 22.0 {A3 C4 F4}>
v22.startTimespans
 (<PitchedTimespan (22.0 to 22.5) <music21.note.Note C>>,
  <PitchedTimespan (22.0 to 23.0) <music21.note.Note F>>)
elTSfollowingRest = v22.startTimespans[1]
elTSfollowingRest
 <PitchedTimespan (22.0 to 23.0) <music21.note.Note F>>
v22.stopTimespans
 (<PitchedTimespan (21.0 to 22.0) <music21.note.Note E>>,
  <ElementTimespan (21.5 to 22.0) <music21.note.Rest eighth>>)
elTSfollowingRest.part
 <music21.stream.Part Cantus>
score = tree.makeExampleScore()
score.show()
../_images/devTest_timespans_28_0.png
tsColSmall = score.asTimespans()
for v in tsColSmall.iterateVerticalities():
    if v:
        print(v)
        print(v.getAllVoiceLeadingQuartets())
 <music21.tree.verticality.Verticality 0.0 {C3 C#3}>
---------------------------------------------------------------------------

TimespanTreeException                     Traceback (most recent call last)

Input In [30], in <cell line: 1>()
      2 if v:
      3     print(v)
----> 4     print(v.getAllVoiceLeadingQuartets())


File ~/git/music21base/music21/tree/verticality.py:988, in Verticality.getAllVoiceLeadingQuartets(self, includeRests, includeOblique, includeNoMotion, returnObjects, partPairNumbers)
    928 '''
    929 >>> c = corpus.parse('luca/gloria').measures(1, 8)
    930 >>> tsCol = tree.fromStream.asTimespans(c, flatten=True,
   (...)
    985 Changed in v8: all parameters are keyword only.
    986 '''
    987 from music21.voiceLeading import VoiceLeadingQuartet
--> 988 pairedMotionList = self.getPairedMotion(includeRests=includeRests,
    989                                         includeOblique=includeOblique)
    990 allQuartets = itertools.combinations(pairedMotionList, 2)
    991 filteredList = []


File ~/git/music21base/music21/tree/verticality.py:1089, in Verticality.getPairedMotion(self, includeRests, includeOblique)
   1086 allPairedMotions = []
   1088 for startingTs in startTss:
-> 1089     previousTs = self.timespanTree.findPreviousPitchedTimespanInSameStreamByClass(
   1090         startingTs)
   1091     if previousTs is None:
   1092         continue  # first not in piece in this part...


File ~/git/music21base/music21/tree/timespanTree.py:317, in TimespanTree.findPreviousPitchedTimespanInSameStreamByClass(self, pitchedTimespan, classList)
    315 if not isinstance(pitchedTimespan, spans.PitchedTimespan):
    316     message = f'PitchedTimespan {pitchedTimespan!r}, must be an PitchedTimespan'
--> 317     raise TimespanTreeException(message)
    318 verticality = self.getVerticalityAt(pitchedTimespan.offset)
    319 while verticality is not None:


TimespanTreeException: PitchedTimespan <ElementTimespan (0.0 to 0.0) <music21.instrument.Instrument 'PartA: : '>>, must be an PitchedTimespan
lucaAll = corpus.parse('luca/gloria')
tsCol = lucaAll.asTimespans()
for v in tsCol.iterateVerticalities():
    vlqs = v.getAllVoiceLeadingQuartets()
---------------------------------------------------------------------------

TimespanTreeException                     Traceback (most recent call last)

/var/folders/mk/qf43gd_s5f30rzzbt7l7l01h0000gn/T/ipykernel_7366/2650510182.py in <module>
      1 for v in tsCol.iterateVerticalities():
----> 2     vlqs = v.getAllVoiceLeadingQuartets()


~/git/music21base/music21/tree/verticality.py in getAllVoiceLeadingQuartets(self, includeRests, includeOblique, includeNoMotion, returnObjects, partPairNumbers)
    975         '''
    976         from music21.voiceLeading import VoiceLeadingQuartet
--> 977         pairedMotionList = self.getPairedMotion(includeRests=includeRests,
    978                                                 includeOblique=includeOblique)
    979         allQuartets = itertools.combinations(pairedMotionList, 2)


~/git/music21base/music21/tree/verticality.py in getPairedMotion(self, includeRests, includeOblique)
   1076
   1077         for startingTs in startTss:
-> 1078             previousTs = self.timespanTree.findPreviousPitchedTimespanInSameStreamByClass(
   1079                 startingTs)
   1080             if previousTs is None:


~/git/music21base/music21/tree/timespanTree.py in findPreviousPitchedTimespanInSameStreamByClass(self, pitchedTimespan, classList)
    302         if not isinstance(pitchedTimespan, spans.PitchedTimespan):
    303             message = f'PitchedTimespan {pitchedTimespan!r}, must be an PitchedTimespan'
--> 304             raise TimespanTreeException(message)
    305         verticality = self.getVerticalityAt(pitchedTimespan.offset)
    306         while verticality is not None:


TimespanTreeException: PitchedTimespan <ElementTimespan (0.0 to 0.0) <music21.text.TextBox '1'>>, must be an PitchedTimespan
bach = corpus.parse('bwv66.6')
meterCol = lucaAll.parts[0].asTimespans(classList=(meter.TimeSignature,))
meterCol
 <TimespanTree {4} (0.0 to 275.0) <music21.stream.Part Cantus>>
print(meterCol)
 <TimespanTree {4} (0.0 to 275.0) <music21.stream.Part Cantus>>
     <ElementTimespan (0.0 to 0.0) <music21.meter.TimeSignature 6/8>>
     <ElementTimespan (81.0 to 81.0) <music21.meter.TimeSignature 2/4>>
     <ElementTimespan (185.0 to 185.0) <music21.meter.TimeSignature 6/8>>
     <ElementTimespan (275.0 to 275.0) <music21.meter.TimeSignature 3/4>>