music21.tree.trees¶
Tools for grouping elements, timespans, and especially pitched elements into kinds of searchable tree organized by start and stop offsets and other positions.
ElementTree¶
-
class
music21.tree.trees.ElementTree(elements=None, source=None)¶ A data structure for efficiently storing a score: flat or recursed or normal.
This data structure has no connection to the XML ElementTree.
This data structure stores ElementNodes: objects which implement both a position and endTime property. It provides fast lookups of such objects.
>>> et = tree.trees.ElementTree() >>> et <ElementTree {0} (-inf to inf)>
>>> s = stream.Stream() >>> for i in range(100): ... n = note.Note() ... n.duration.quarterLength = 2.0 ... s.insert(i * 2, n)
>>> for n in s: ... et.insert(n) >>> et <ElementTree {100} (0.0 <0.20...> to 200.0)> >>> et.rootNode <ElementNode: Start:126.0 <0.20...> Indices:(l:0 *63* r:100) Payload:<music21.note.Note C>>
>>> n2 = s[-1]
These operations are very fast...
>>> et.index(n2, n2.sortTuple()) 99
Get a position after a certain position:
>>> st = s[40].sortTuple() >>> st SortTuple(atEnd=0, offset=80.0, priority=0, classSortOrder=20, isNotGrace=1, insertIndex=...) >>> st2 = et.getPositionAfter(st) >>> st2.shortRepr() '82.0 <0.20...>' >>> st2.offset 82.0
>>> st3 = et.getPositionAfter(5.0) >>> st3.offset 6.0 >>> et.getPositionAfter(4.0).offset 6.0
ElementTree bases
ElementTree read-only properties
-
ElementTree.endTime¶ Gets the latest stop position in this element-tree.
This is cast as a property so that it can be used like a TimeSpan in a TimeSpanTree
>>> score = corpus.parse('bwv66.6') >>> tsTree = score.asTree() >>> tsTree.endTime 36.0
Returns infinity if no elements exist:
>>> et = tree.trees.ElementTree() >>> et.endTime inf
ElementTree read/write properties
-
ElementTree.source¶ the original stream. (stored as a weakref but returned unwrapped)
>>> example = tree.makeExampleScore() >>> eTree = example.asTree() >>> eTree.source is example True
>>> s = stream.Stream() >>> eTree.source = s >>> eTree.source is s True
ElementTree methods
-
ElementTree.getNodeByIndex(i)¶ Get a node whose element is at a particular index (not position). Works with slices too
See __getitem__ for caveats about speed...
>>> score = tree.makeExampleScore() >>> scoreTree = score.asTree(flatten=True) >>> scoreTree <ElementTree {20} (0.0 <0.-25...> to 8.0) <music21.stream.Score exampleScore>>
>>> scoreTree.getNodeByIndex(0) <ElementNode: Start:0.0 <0.-25...> Indices:(l:0 *0* r:2) Payload:<music21.instrument.Instrument PartA: : >>
>>> scoreTree.getNodeByIndex(-1) <ElementNode: Start:End <0.-5...> Indices:(l:19 *19* r:20) Payload:<music21.bar.Barline style=final>>
>>> scoreTree.getNodeByIndex(slice(2, 5)) [<ElementNode: Start:0.0 <0.0...> Indices:(l:0 *2* r:4) Payload:<music21.clef.BassClef>>, <ElementNode: Start:0.0 <0.0...> Indices:(l:3 *3* r:4) Payload:<music21.clef.BassClef>>, <ElementNode: Start:0.0 <0.4...> Indices:(l:0 *4* r:8) Payload:<music21.meter.TimeSignature 2/4>>]
>>> scoreTree.getNodeByIndex(slice(-6, -3)) [<ElementNode: Start:5.0 <0.20...> Indices:(l:9 *14* r:20) Payload:<music21.note.Note A>>, <ElementNode: Start:6.0 <0.20...> Indices:(l:15 *15* r:17) Payload:<music21.note.Note B>>, <ElementNode: Start:6.0 <0.20...> Indices:(l:16 *16* r:17) Payload:<music21.note.Note D#>>]
>>> scoreTree.getNodeByIndex(slice(-100, -200)) []
-
ElementTree.getPositionFromElementUnsafe(el)¶ A quick but dirty method for getting the likely position (or offset) of an element within the elementTree from the element itself. Such as calling
el.getOffsetBySite(tree.source) or something like that.
Pulled out for subclassing
-
ElementTree.highestPosition()¶ Gets the latest position in this tree.
Keep as a property, because a similar property exists on streams.
>>> score = corpus.parse('bwv66.6') >>> tsTree = score.asTimespans(classList=(note.Note,)) >>> tsTree.highestPosition() 35.0
-
ElementTree.index(element, position=None)¶ Gets index of element in tree. position could be none.
If the element is in the original score, then it should be very fast (O(log n))
>>> score = tree.makeExampleScore() >>> scoreFlat = score.flat >>> n = scoreFlat.notes[-1]
>>> flatTree = scoreFlat.asTree() >>> flatTree.index(n) 17
If it’s not in the original stream, then it should be slower than doing it on a stream (O (n log n)).
>>> scoreTree = score.asTree(flatten=True) >>> n = score.flat.notes[-1] >>> scoreTree.index(n) 17
And if it’s nowhere at all, you get a ValueError!
>>> scoreTree.index(note.Note("F-")) Traceback (most recent call last): ValueError: <music21.note.Note F-> not in Tree at position SortTuple(atEnd=0, offset=0.0, priority=0, ...).
-
ElementTree.insert(positionsOrElements, elements=None)¶ Inserts elements or Timespans into this tree.
>>> n = note.Note() >>> ot = tree.trees.OffsetTree() >>> ot <OffsetTree {0} (-inf to inf)> >>> ot.insert(10.0, n) >>> ot <OffsetTree {1} (10.0 to 11.0)>
>>> n2 = note.Note('D') >>> n2.offset = 20 >>> n3 = note.Note('E') >>> n3.offset = 5 >>> ot.insert([n2, n3]) >>> ot <OffsetTree {3} (5.0 to 21.0)>
-
ElementTree.lowestPosition()¶ Gets the earliest position in this tree.
>>> score = tree.makeExampleScore() >>> elTree = score.asTree() >>> elTree.lowestPosition().shortRepr() '0.0 <0.-20...>'
>>> tsTree = score.asTimespans() >>> tsTree.lowestPosition() 0.0
-
ElementTree.populateFromSortedList(listOfTuples)¶ This method assumes that the current tree is empty (or will be wiped) and that listOfTuples is a non-empty list where the first element is a unique position to insert, and the second is the complete payload for that node, and that the positions are strictly increasing in order.
This is about an order of magnitude faster (3ms vs 21ms for 1000 items; 31 vs. 30ms for 10,000 items) than running createNodeAtPosition() for each element in a list if it is already sorted. Thus it should be used when converting a Stream where .isSorted is True into a tree.
If any of the conditions is not true, expect to get a dangerously badly sorted tree that will be useless.
>>> bFlat = corpus.parse('bwv66.6').flat >>> bFlat.isSorted True
>>> listOfTuples = [(e.sortTuple(bFlat), e) for e in bFlat] >>> listOfTuples[10] (SortTuple(atEnd=0, offset=0.0, priority=0, ...), <music21.key.KeySignature of 3 sharps, mode minor>)
>>> t = tree.trees.ElementTree() >>> t.rootNode is None True >>> t.populateFromSortedList(listOfTuples) >>> t.rootNode <ElementNode: Start:15.0 <0.20...> Indices:(l:0 *97* r:195) Payload:<music21.note.Note D#>>
>>> n = t.rootNode >>> while n is not None: ... print(n) ... n = n.leftChild <ElementNode: Start:15.0 <0.20...> Indices:(l:0 *97* r:195) Payload:<music21.note.Note D#>> <ElementNode: Start:6.0 <0.20...> Indices:(l:0 *48* r:97) Payload:<music21.note.Note B>> <ElementNode: Start:0.5 <0.20...> Indices:(l:0 *24* r:48) Payload:<music21.note.Note G#>> <ElementNode: Start:0.0 <0.2...> Indices:(l:0 *12* r:24) Payload:<music21.key.KeySignature of 3 sharps, mode minor>> <ElementNode: Start:0.0 <0.0...> Indices:(l:0 *6* r:12) Payload:<music21.clef.TrebleClef>> <ElementNode: Start:0.0 <0.-25...> Indices:(l:0 *3* r:6) Payload:<music21.instrument.Instrument P3: Tenor: Instrument 3>> <ElementNode: Start:0.0 <0.-25...> Indices:(l:0 *1* r:3) Payload:<music21.instrument.Instrument P1: Soprano: Instrument 1>> <ElementNode: Start:0.0 <0.-30...> Indices:(l:0 *0* r:1) Payload:<music21.metadata.Metadata object at 0x104adbdd8>>
>>> n = t.rootNode >>> while n is not None: ... print(n) ... n = n.rightChild <ElementNode: Start:15.0 <0.20...> Indices:(l:0 *97* r:195) Payload:<music21.note.Note D#>> <ElementNode: Start:25.0 <0.20...> Indices:(l:98 *146* r:195) Payload:<music21.note.Note F#>> <ElementNode: Start:32.0 <0.20...> Indices:(l:147 *171* r:195) Payload:<music21.note.Note F#>> <ElementNode: Start:34.0 <0.20...> Indices:(l:172 *183* r:195) Payload:<music21.note.Note D>> <ElementNode: Start:35.0 <0.20...> Indices:(l:184 *189* r:195) Payload:<music21.note.Note A#>> <ElementNode: Start:36.0 <0.-5...> Indices:(l:190 *192* r:195) Payload:<music21.bar.Barline style=final>> <ElementNode: Start:36.0 <0.-5...> Indices:(l:193 *194* r:195) Payload:<music21.bar.Barline style=final>>
Methods inherited from AVLTree:
OffsetTree¶
-
class
music21.tree.trees.OffsetTree(elements=None, source=None)¶ A tree representation where positions are offsets in the score and each node has a payload which is a list of elements at that offset (unsorted by sort order).
OffsetTree bases
OffsetTree read-only properties
Read-only properties inherited from ElementTree:
OffsetTree read/write properties
Read/write properties inherited from ElementTree:
OffsetTree methods
-
OffsetTree.allOffsets()¶ Gets all unique offsets of all timespans in this offset-tree.
>>> score = corpus.parse('bwv66.6') >>> tsTree = score.asTimespans() >>> for offset in tsTree.allOffsets()[:10]: ... offset ... 0.0 0.5 1.0 2.0 3.0 4.0 5.0 5.5 6.0 6.5
-
OffsetTree.allTimePoints()¶ Gets all unique offsets (both starting and stopping) of all elements/timespans in this offset-tree.
>>> score = corpus.parse('bwv66.6') >>> scoreTree = score.asTimespans() >>> for offset in scoreTree.allTimePoints()[:10]: ... offset ... 0.0 0.5 1.0 2.0 3.0 4.0 5.0 5.5 6.0 6.5
-
OffsetTree.append(el)¶ Add an element to the end, making certain speed savings.
-
OffsetTree.copy()¶ Creates a new tree with the same payload as this tree.
SLOW!
This is analogous to dict.copy().
>>> score = tree.makeExampleScore() >>> scoreTree = score.asTimespans() >>> newTree = scoreTree.copy() >>> newTree <TimespanTree {20} (0.0 to 8.0)>
>>> scoreTree[16] <PitchedTimespan (6.0 to 8.0) <music21.note.Note D#>> >>> newTree[16] <PitchedTimespan (6.0 to 8.0) <music21.note.Note D#>>
>>> scoreTree[16] is newTree[16] True
-
OffsetTree.elementsOverlappingOffset(offset)¶ Finds elements or timespans in this ElementTree which overlap offset.
>>> score = corpus.parse('bwv66.6') >>> scoreTree = score.asTimespans() >>> for el in scoreTree.elementsOverlappingOffset(0.5): ... el ... <PitchedTimespan (0.0 to 1.0) <music21.note.Note E>>
-
OffsetTree.elementsStartingAt(position)¶ Finds elements or timespans in this tree which start at position.
>>> score = corpus.parse('bwv66.6') >>> scoreTree = score.asTimespans() >>> for timespan in scoreTree.elementsStartingAt(0.5): ... timespan ... <PitchedTimespan (0.5 to 1.0) <music21.note.Note B>> <PitchedTimespan (0.5 to 1.0) <music21.note.Note B>> <PitchedTimespan (0.5 to 1.0) <music21.note.Note G#>>
-
OffsetTree.elementsStoppingAt(offset)¶ Finds elements or timespans in this offset-tree which stop at offset.
>>> score = corpus.parse('bwv66.6') >>> scoreTree = score.asTimespans() >>> for timespan in scoreTree.elementsStoppingAt(0.5): ... timespan ... <PitchedTimespan (0.0 to 0.5) <music21.note.Note C#>> <PitchedTimespan (0.0 to 0.5) <music21.note.Note A>> <PitchedTimespan (0.0 to 0.5) <music21.note.Note A>>
-
OffsetTree.getPositionFromElementUnsafe(el)¶ A quick but dirty method for getting the likely position (or offset) of an element within the elementTree from the element itself. Such as calling
el.getOffsetBySite(tree.source) or something like that.
Pulled out for subclassing
-
OffsetTree.index(element, offset=None)¶ Gets index of an element or Timespan in tree.
Since Timespans do not have .sites, there is only one offset to deal with...
>>> tsList = [(0,2), (0,9), (1,1), (2,3), (3,4), (4,9), (5,6), (5,8), (6,8), (7,7)] >>> ts = [tree.spans.Timespan(x, y) for x, y in tsList] >>> tsTree = tree.trees.TimespanTree() >>> tsTree.insert(ts)
>>> for timespan in ts: ... print("%r %d" % (timespan, tsTree.index(timespan))) ... <Timespan 0.0 2.0> 0 <Timespan 0.0 9.0> 1 <Timespan 1.0 1.0> 2 <Timespan 2.0 3.0> 3 <Timespan 3.0 4.0> 4 <Timespan 4.0 9.0> 5 <Timespan 5.0 6.0> 6 <Timespan 5.0 8.0> 7 <Timespan 6.0 8.0> 8 <Timespan 7.0 7.0> 9
>>> tsTree.index(tree.spans.Timespan(-100, 100)) Traceback (most recent call last): ValueError: <Timespan -100.0 100.0> not in Tree at offset -100.0.
-
OffsetTree.removeElements(elements, offsets=None, runUpdate=True)¶ Removes elements which can be Music21Objects or Timespans (a single one or a list) from this Tree.
Much safer (for non-timespans) if a list of offsets is used but it is optional
If runUpdate is False then the tree will be left with incorrect indices and endTimes; but it can speed up operations where an element is going to be removed and then immediately replaced: i.e., where the position of an element has changed
Methods inherited from ElementTree:
Methods inherited from AVLTree:
TimespanTree¶
-
class
music21.tree.trees.TimespanTree(elements=None, source=None)¶ A data structure for efficiently slicing a score for pitches.
While you can construct an TimespanTree by hand, inserting timespans one at a time, the common use-case is to construct the offset-tree from an entire score at once:
>>> bach = corpus.parse('bwv66.6') >>> scoreTree = tree.fromStream.convert(bach, flatten=True, ... classList=(note.Note, chord.Chord)) >>> print(scoreTree.getVerticalityAt(17.0)) <Verticality 17.0 {F#3 C#4 A4}>
All offsets are assumed to be relative to the score’s source if flatten is True
Example: How many moments in Bach are consonant and how many are dissonant:
>>> totalConsonances = 0 >>> totalDissonances = 0 >>> for v in scoreTree.iterateVerticalities(): ... if v.toChord().isConsonant(): ... totalConsonances += 1 ... else: ... totalDissonances += 1 >>> (totalConsonances, totalDissonances) (34, 17)
So 1/3 of the vertical moments in Bach are dissonant! But is this an accurate perception? Let’s sum up the total consonant duration vs. dissonant duration.
Do it again pairwise to figure out the length (actually this won’t include the last element)
>>> totalConsonanceDuration = 0 >>> totalDissonanceDuration = 0 >>> iterator = scoreTree.iterateVerticalitiesNwise(n=2) >>> for verticality1, verticality2 in iterator: ... offset1 = verticality1.offset ... offset2 = verticality2.offset ... quarterLength = offset2 - offset1 ... if verticality1.toChord().isConsonant(): ... totalConsonanceDuration += quarterLength ... else: ... totalDissonanceDuration += quarterLength >>> (totalConsonanceDuration, totalDissonanceDuration) (25.5, 9.5)
Remove neighbor tones from the Bach chorale. (It’s actually quite viscous in its pruning...)
Here in Alto, measure 7, there’s a neighbor tone E#.
>>> bach.parts['Alto'].measure(7).show('text') {0.0} <music21.note.Note F#> {0.5} <music21.note.Note E#> {1.0} <music21.note.Note F#> {1.5} <music21.note.Note F#> {2.0} <music21.note.Note C#>
We’ll get rid of it and a lot of other neighbor tones.
>>> for verticalities in scoreTree.iterateVerticalitiesNwise(n=3): ... horizontalities = scoreTree.unwrapVerticalities(verticalities) ... for unused_part, horizontality in horizontalities.items(): ... if horizontality.hasNeighborTone: ... merged = horizontality[0].new( ... endTime=horizontality[2].endTime, ... ) # merged is a new PitchedTimespan ... scoreTree.removeTimespan(horizontality[0]) ... scoreTree.removeTimespan(horizontality[1]) ... scoreTree.removeTimespan(horizontality[2]) ... scoreTree.insert(merged)
>>> newBach = tree.toStream.partwise( ... scoreTree, ... templateStream=bach, ... ) >>> newBach.parts['Alto'].measure(7).show('text') {0.0} <music21.chord.Chord F#4> {1.5} <music21.chord.Chord F#3> {2.0} <music21.chord.Chord C#4>
The second F# is an octave lower, so it wouldn’t get merged even if adjacent notes were fused together (which they’re not).
Note
TimespanTree is an implementation of an extended AVL tree. AVL trees are a type of binary tree, like Red-Black trees. AVL trees are very efficient at insertion when the objects being inserted are already sorted - which is usually the case with data extracted from a score. TimespanTree is an extended AVL tree because each node in the tree keeps track of not just the start offsets of PitchedTimespans stored at that node, but also the earliest and latest stop offset of all PitchedTimespans stores at both that node and all nodes which are children of that node. This lets us quickly located PitchedTimespans which overlap offsets or which are contained within ranges of offsets. This also means that the contents of a TimespanTree are always sorted.
TimespanTree bases
TimespanTree read-only properties
Read-only properties inherited from ElementTree:
TimespanTree read/write properties
-
TimespanTree.element¶ defined so a TimespanTree can be used like an PitchedTimespan
TODO: Look at subclassing or at least deriving from a common base...
Read/write properties inherited from ElementTree:
TimespanTree methods
-
TimespanTree.allParts()¶
-
TimespanTree.findNextPitchedTimespanInSameStreamByClass(pitchedTimespan, classList=None)¶ Finds next element timespan in the same stream class as PitchedTimespan.
Default classList is (stream.Part, )
>>> score = corpus.parse('bwv66.6') >>> scoreTree = score.asTimespans(classList=(note.Note,)) >>> timespan = scoreTree[0] >>> timespan <PitchedTimespan (0.0 to 0.5) <music21.note.Note C#>>
>>> timespan.part <music21.stream.Part Soprano>
>>> timespan = scoreTree.findNextPitchedTimespanInSameStreamByClass(timespan) >>> timespan <PitchedTimespan (0.5 to 1.0) <music21.note.Note B>>
>>> timespan.part <music21.stream.Part Soprano>
>>> timespan = scoreTree.findNextPitchedTimespanInSameStreamByClass(timespan) >>> timespan <PitchedTimespan (1.0 to 2.0) <music21.note.Note A>>
>>> timespan.part <music21.stream.Part Soprano>
-
TimespanTree.findPreviousPitchedTimespanInSameStreamByClass(pitchedTimespan, classList=None)¶ Finds next element timespan in the same Part/Measure, etc. (specify in classList) as the pitchedTimespan.
>>> score = corpus.parse('bwv66.6') >>> scoreTree = score.asTimespans(classList=(note.Note,)) >>> timespan = scoreTree[-1] >>> timespan <PitchedTimespan (35.0 to 36.0) <music21.note.Note F#>>
>>> timespan.part <music21.stream.Part Bass>
>>> timespan = scoreTree.findPreviousPitchedTimespanInSameStreamByClass(timespan) >>> timespan <PitchedTimespan (34.0 to 35.0) <music21.note.Note B>>
>>> timespan.part <music21.stream.Part Bass>
>>> timespan = scoreTree.findPreviousPitchedTimespanInSameStreamByClass(timespan) >>> timespan <PitchedTimespan (33.0 to 34.0) <music21.note.Note D>>
>>> timespan.part <music21.stream.Part Bass>
-
TimespanTree.getVerticalityAt(offset)¶ Gets the verticality in this offset-tree which starts at offset.
>>> bach = corpus.parse('bwv66.6') >>> scoreTree = bach.asTimespans() >>> scoreTree.getVerticalityAt(2.5) <Verticality 2.5 {G#3 B3 E4 B4}>
Verticalities outside the range still return a Verticality, but it might be empty...
>>> scoreTree.getVerticalityAt(2000) <Verticality 2000 {}>
Test that it still works if the tree is empty...
>>> scoreTree = bach.asTimespans(classList=(instrument.Tuba,)) >>> scoreTree <TimespanTree {0} (-inf to inf) <music21.stream.Score ...>> >>> scoreTree.getVerticalityAt(5.0) <Verticality 5.0 {}>
Returns a verticality.Verticality object.
-
TimespanTree.getVerticalityAtOrBefore(offset)¶ Gets the verticality in this offset-tree which starts at offset.
If the found verticality has no start timespans, the function returns the next previous verticality with start timespans.
>>> score = corpus.parse('bwv66.6') >>> scoreTree = score.asTimespans() >>> scoreTree.getVerticalityAtOrBefore(0.125) <Verticality 0.0 {A3 E4 C#5}>
>>> scoreTree.getVerticalityAtOrBefore(0.) <Verticality 0.0 {A3 E4 C#5}>
-
TimespanTree.iterateConsonanceBoundedVerticalities()¶ Iterates consonant-bounded verticality subsequences in this offset-tree.
>>> score = corpus.parse('bwv66.6') >>> scoreTree = score.asTimespans() >>> for subsequence in scoreTree.iterateConsonanceBoundedVerticalities(): ... print('Subequence:') ... for verticality in subsequence: ... print('\t[{}] {}: {} [{}]'.format( ... verticality.measureNumber, ... verticality, ... verticality.isConsonant, ... verticality.beatStrength, ... )) ... Subequence: [2] <Verticality 6.0 {E3 E4 G#4 B4}>: True [0.25] [2] <Verticality 6.5 {E3 D4 G#4 B4}>: False [0.125] [2] <Verticality 7.0 {A2 C#4 E4 A4}>: True [0.5] Subequence: [3] <Verticality 9.0 {F#3 C#4 F#4 A4}>: True [1.0] [3] <Verticality 9.5 {B2 D4 G#4 B4}>: False [0.125] [3] <Verticality 10.0 {C#3 C#4 E#4 G#4}>: True [0.25] Subequence: [3] <Verticality 10.0 {C#3 C#4 E#4 G#4}>: True [0.25] [3] <Verticality 10.5 {C#3 B3 E#4 G#4}>: False [0.125] [3] <Verticality 11.0 {F#2 A3 C#4 F#4}>: True [0.5] Subequence: [3] <Verticality 12.0 {F#3 C#4 F#4 A4}>: True [0.25] [4] <Verticality 13.0 {G#3 B3 F#4 B4}>: False [1.0] [4] <Verticality 13.5 {F#3 B3 F#4 B4}>: False [0.125] [4] <Verticality 14.0 {G#3 B3 E4 B4}>: True [0.25] Subequence: [4] <Verticality 14.0 {G#3 B3 E4 B4}>: True [0.25] [4] <Verticality 14.5 {A3 B3 E4 B4}>: False [0.125] [4] <Verticality 15.0 {B3 D#4 F#4}>: True [0.5] Subequence: [4] <Verticality 15.0 {B3 D#4 F#4}>: True [0.5] [4] <Verticality 15.5 {B2 A3 D#4 F#4}>: False [0.125] [4] <Verticality 16.0 {C#3 G#3 C#4 E4}>: True [0.25] Subequence: [5] <Verticality 17.5 {F#3 D4 F#4 A4}>: True [0.125] [5] <Verticality 18.0 {G#3 C#4 E4 B4}>: False [0.25] [5] <Verticality 18.5 {G#3 B3 E4 B4}>: True [0.125] Subequence: [6] <Verticality 24.0 {F#3 C#4 F#4 A4}>: True [0.25] [7] <Verticality 25.0 {B2 D4 F#4 G#4}>: False [1.0] [7] <Verticality 25.5 {C#3 C#4 E#4 G#4}>: True [0.125] Subequence: [7] <Verticality 25.5 {C#3 C#4 E#4 G#4}>: True [0.125] [7] <Verticality 26.0 {D3 C#4 F#4}>: False [0.25] [7] <Verticality 26.5 {D3 F#3 B3 F#4}>: True [0.125] Subequence: [8] <Verticality 29.0 {A#2 F#3 C#4 F#4}>: True [1.0] [8] <Verticality 29.5 {A#2 F#3 D4 F#4}>: False [0.125] [8] <Verticality 30.0 {A#2 C#4 E4 F#4}>: False [0.25] [8] <Verticality 31.0 {B2 C#4 E4 F#4}>: False [0.5] [8] <Verticality 32.0 {C#3 B3 D4 F#4}>: False [0.25] [8] <Verticality 32.5 {C#3 A#3 C#4 F#4}>: False [0.125] [9] <Verticality 33.0 {D3 B3 F#4}>: True [1.0] Subequence: [9] <Verticality 33.0 {D3 B3 F#4}>: True [1.0] [9] <Verticality 33.5 {D3 B3 C#4 F#4}>: False [0.125] [9] <Verticality 34.0 {B2 B3 D4 F#4}>: True [0.25] Subequence: [9] <Verticality 34.0 {B2 B3 D4 F#4}>: True [0.25] [9] <Verticality 34.5 {B2 B3 D4 E#4}>: False [0.125] [9] <Verticality 35.0 {F#3 A#3 C#4 F#4}>: True [0.5]
-
TimespanTree.iterateVerticalities(reverse=False)¶ Iterates all vertical moments in this offset-tree.
Note
The offset-tree can be mutated while its verticalities are iterated over. Each verticality holds a reference back to the offset-tree and will ask for the start-offset after (or before) its own start offset in order to determine the next verticality to yield. If you mutate the tree by adding or deleting timespans, the next verticality will reflect those changes.
>>> score = corpus.parse('bwv66.6') >>> scoreTree = score.asTimespans(classList=(note.Note,)) >>> iterator = scoreTree.iterateVerticalities() >>> for _ in range(10): ... next(iterator) ... <Verticality 0.0 {A3 E4 C#5}> <Verticality 0.5 {G#3 B3 E4 B4}> <Verticality 1.0 {F#3 C#4 F#4 A4}> <Verticality 2.0 {G#3 B3 E4 B4}> <Verticality 3.0 {A3 E4 C#5}> <Verticality 4.0 {G#3 B3 E4 E5}> <Verticality 5.0 {A3 E4 C#5}> <Verticality 5.5 {C#3 E4 A4 C#5}> <Verticality 6.0 {E3 E4 G#4 B4}> <Verticality 6.5 {E3 D4 G#4 B4}>
Verticalities can also be iterated in reverse:
>>> iterator = scoreTree.iterateVerticalities(reverse=True) >>> for _ in range(10): ... next(iterator) ... <Verticality 35.0 {F#3 A#3 C#4 F#4}> <Verticality 34.5 {B2 B3 D4 E#4}> <Verticality 34.0 {B2 B3 D4 F#4}> <Verticality 33.5 {D3 B3 C#4 F#4}> <Verticality 33.0 {D3 B3 F#4}> <Verticality 32.5 {C#3 A#3 C#4 F#4}> <Verticality 32.0 {C#3 B3 D4 F#4}> <Verticality 31.0 {B2 C#4 E4 F#4}> <Verticality 30.0 {A#2 C#4 E4 F#4}> <Verticality 29.5 {A#2 F#3 D4 F#4}>
-
TimespanTree.iterateVerticalitiesNwise(n=3, reverse=False)¶ Iterates verticalities in groups of length n.
Note
The offset-tree can be mutated while its verticalities are iterated over. Each verticality holds a reference back to the offset-tree and will ask for the start-offset after (or before) its own start offset in order to determine the next verticality to yield. If you mutate the tree by adding or deleting timespans, the next verticality will reflect those changes.
>>> score = corpus.parse('bwv66.6') >>> scoreTree = score.asTimespans(classList=(note.Note,)) >>> iterator = scoreTree.iterateVerticalitiesNwise(n=2) >>> for _ in range(4): ... print(next(iterator)) ... <VerticalitySequence: [ <Verticality 0.0 {A3 E4 C#5}>, <Verticality 0.5 {G#3 B3 E4 B4}> ]> <VerticalitySequence: [ <Verticality 0.5 {G#3 B3 E4 B4}>, <Verticality 1.0 {F#3 C#4 F#4 A4}> ]> <VerticalitySequence: [ <Verticality 1.0 {F#3 C#4 F#4 A4}>, <Verticality 2.0 {G#3 B3 E4 B4}> ]> <VerticalitySequence: [ <Verticality 2.0 {G#3 B3 E4 B4}>, <Verticality 3.0 {A3 E4 C#5}> ]>
Grouped verticalities can also be iterated in reverse:
>>> iterator = scoreTree.iterateVerticalitiesNwise(n=2, reverse=True) >>> for _ in range(4): ... print(next(iterator)) ... <VerticalitySequence: [ <Verticality 34.5 {B2 B3 D4 E#4}>, <Verticality 35.0 {F#3 A#3 C#4 F#4}> ]> <VerticalitySequence: [ <Verticality 34.0 {B2 B3 D4 F#4}>, <Verticality 34.5 {B2 B3 D4 E#4}> ]> <VerticalitySequence: [ <Verticality 33.5 {D3 B3 C#4 F#4}>, <Verticality 34.0 {B2 B3 D4 F#4}> ]> <VerticalitySequence: [ <Verticality 33.0 {D3 B3 F#4}>, <Verticality 33.5 {D3 B3 C#4 F#4}> ]>
-
TimespanTree.maximumOverlap()¶ The maximum number of timespans overlapping at any given moment in this timespan collection.
>>> score = corpus.parse('bwv66.6') >>> scoreTree = score.asTimespans(classList=(note.Note,)) >>> scoreTree.maximumOverlap() 4
Returns None if there is no verticality here.
-
TimespanTree.offset()¶ this is just for mimicking elements as streams.
-
TimespanTree.removeTimespan(elements, offsets=None, runUpdate=True)¶ this will eventually be different from above...
-
TimespanTree.removeTimespanList(elements, offsets=None, runUpdate=True)¶ this will eventually be different from above...
-
TimespanTree.splitAt(offsets)¶ Splits all timespans in this offset-tree at offsets, operating in place.
>>> score = corpus.parse('bwv66.6') >>> scoreTree = score.asTimespans() >>> scoreTree.elementsStartingAt(0.1) ()
>>> for timespan in scoreTree.elementsOverlappingOffset(0.1): ... print("%r, %s" % (timespan, timespan.part.id)) ... <PitchedTimespan (0.0 to 0.5) <music21.note.Note C#>>, Soprano <PitchedTimespan (0.0 to 0.5) <music21.note.Note A>>, Tenor <PitchedTimespan (0.0 to 0.5) <music21.note.Note A>>, Bass <PitchedTimespan (0.0 to 1.0) <music21.note.Note E>>, Alto
>>> scoreTree.splitAt(0.1) >>> for timespan in scoreTree.elementsStartingAt(0.1): ... print("%r, %s" % (timespan, timespan.part.id)) ... <PitchedTimespan (0.1 to 0.5) <music21.note.Note C#>>, Soprano <PitchedTimespan (0.1 to 1.0) <music21.note.Note E>>, Alto <PitchedTimespan (0.1 to 0.5) <music21.note.Note A>>, Tenor <PitchedTimespan (0.1 to 0.5) <music21.note.Note A>>, Bass
>>> scoreTree.elementsOverlappingOffset(0.1) ()
-
TimespanTree.toPartwiseTimespanTrees()¶ Returns a dictionary of TimespanTrees where each entry is indexed by a Part object (TODO: Don’t use mutable objects as hash keys!) and each key is a TimeSpan tree containing only element timespans belonging to that part.
Used by reduceChords. May disappear.
-
static
TimespanTree.unwrapVerticalities(verticalities)¶ Unwraps a sequence of Verticality objects into a dictionary of Part:Horizontality key/value pairs.
>>> score = corpus.parse('bwv66.6') >>> scoreTree = score.asTimespans(classList=(note.Note,)) >>> iterator = scoreTree.iterateVerticalitiesNwise() >>> verticalities = next(iterator) >>> unwrapped = scoreTree.unwrapVerticalities(verticalities) >>> for part in sorted(unwrapped, key=lambda x: x.partName): ... print(part) ... horizontality = unwrapped[part] ... for timespan in horizontality: ... print('\t%r' % timespan) ... <music21.stream.Part Alto> <PitchedTimespan (0.0 to 1.0) <music21.note.Note E>> <PitchedTimespan (1.0 to 2.0) <music21.note.Note F#>> <music21.stream.Part Bass> <PitchedTimespan (0.0 to 0.5) <music21.note.Note A>> <PitchedTimespan (0.5 to 1.0) <music21.note.Note G#>> <PitchedTimespan (1.0 to 2.0) <music21.note.Note F#>> <music21.stream.Part Soprano> <PitchedTimespan (0.0 to 0.5) <music21.note.Note C#>> <PitchedTimespan (0.5 to 1.0) <music21.note.Note B>> <PitchedTimespan (1.0 to 2.0) <music21.note.Note A>> <music21.stream.Part Tenor> <PitchedTimespan (0.0 to 0.5) <music21.note.Note A>> <PitchedTimespan (0.5 to 1.0) <music21.note.Note B>> <PitchedTimespan (1.0 to 2.0) <music21.note.Note C#>>
Methods inherited from OffsetTree:
Methods inherited from ElementTree:
Methods inherited from AVLTree: