Tools for grouping notes and chords into a searchable tree organized by start and stop offsets.
This is a lower-level tool that for now at least normal music21 users won’t need to worry about.
Makes an element from a verticality and quarterLength.
Recurses through inputStream, constructs ElementTimespans for each non-stream pitched element found, and returns all constructed ElementTimespans.
Recurses through a score and constructs a timespan collection.
>>> score = corpus.parse('bwv66.6')
>>> tree = stream.timespans.streamToTimespanCollection(score)
>>> for x in tree[:5]:
... x
...
<ElementTimespan 0.0:0.5 <music21.note.Note C#>>
<ElementTimespan 0.0:0.5 <music21.note.Note A>>
<ElementTimespan 0.0:0.5 <music21.note.Note A>>
<ElementTimespan 0.0:1.0 <music21.note.Note E>>
<ElementTimespan 0.5:1.0 <music21.note.Note B>>
A datastructure for efficiently slicing a score.
This datastructure stores timespans: objects which implement both a startOffset and stopOffset property. It provides fast lookups of such objects and can quickly locate vertical overlaps.
While you can construct an offset-tree 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')
>>> tree = stream.timespans.TimespanCollection(bach)
>>> print tree.getVerticalityAt(17.0)
<Verticality 17.0 {F#3 C#4 A4}>
All offsets are assumed to be relative to the score’s origin.
Example: How many moments in Bach are consonant and how many are dissonant:
>>> totalCons = 0
>>> totalDiss = 0
>>> for v in tree.iterateVerticalities():
... if v.toChord().isConsonant():
... totalCons += 1
... else:
... totalDiss += 1
>>> (totalCons, totalDiss)
(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)
>>> durCons = 0
>>> durDiss = 0
>>> for v1, v2 in tree.iterateVerticalitiesNwise(n=2):
... vDurationQL = v2.startOffset - v1.startOffset
... if v1.toChord().isConsonant():
... durCons += vDurationQL
... else:
... durDiss += vDurationQL
>>> (durCons, durDiss)
(25.5, 9.5)
Remove neighbor tones from the Bach chorale:
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 tree.iterateVerticalitiesNwise(n=3):
... horizontalities = tree.unwrapVerticalities(verticalities)
... for unused_part, horizontality in horizontalities.items():
... if horizontality.hasNeighborTone:
... merged = horizontality[0].new(
... stopOffset=horizontality[2].stopOffset,
... ) # merged is a new ElementTimeSpan
... tree.remove(horizontality[0])
... tree.remove(horizontality[1])
... tree.remove(horizontality[2])
... tree.insert(merged)
>>> newBach = tree.toPartwiseScore()
>>> newBach.parts[1].measure(7).show('text')
{0.0} <music21.note.Note F#>
{1.5} <music21.note.Note F#>
{2.0} <music21.note.Note C#>
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
TimespanCollection 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. TimespanCollection is an extended AVL tree because each node in the tree keeps track of not just the start offsets of ElementTimespans stored at that node, but also the earliest and latest stop offset of all ElementTimespans stores at both that node and all nodes which are children of that node. This lets us quickly located ElementTimespans which overlap offsets or which are contained within ranges.
TODO: newBach.parts[‘Alto’].measure(7).show(‘text’) should work. KeyError: ‘provided key (Alto) does not match any id or group’
TODO: Doc examples for all functions, including privates.
TimespanCollection read-only properties
Gets all unique offsets (both starting and stopping) of all timespans in this offset-tree.
>>> score = corpus.parse('bwv66.6')
>>> tree = stream.timespans.TimespanCollection(score)
>>> for offset in tree.allOffsets[:10]:
... offset
...
0.0
0.5
1.0
2.0
3.0
4.0
5.0
5.5
6.0
6.5
Gets all unique start offsets of all timespans in this offset-tree.
>>> score = corpus.parse('bwv66.6')
>>> tree = stream.timespans.TimespanCollection(score)
>>> for offset in tree.allStartOffsets[:10]:
... offset
...
0.0
0.5
1.0
2.0
3.0
4.0
5.0
5.5
6.0
6.5
Gets all unique stop offsets of all timespans in this offset-tree.
>>> score = corpus.parse('bwv66.6')
>>> tree = stream.timespans.TimespanCollection(score)
>>> for offset in tree.allStopOffsets[:10]:
... offset
...
0.5
1.0
2.0
4.0
5.5
6.0
7.0
8.0
9.5
10.5
Gets the earlies start offset in this offset-tree.
>>> score = corpus.parse('bwv66.6')
>>> tree = stream.timespans.TimespanCollection(score)
>>> tree.earliestStartOffset
0.0
Gets the earliest stop offset in this offset-tree.
>>> score = corpus.parse('bwv66.6')
>>> tree = stream.timespans.TimespanCollection(score)
>>> tree.earliestStopOffset
0.5
Gets the lateset start offset in this offset-tree.
>>> score = corpus.parse('bwv66.6')
>>> tree = stream.timespans.TimespanCollection(score)
>>> tree.latestStartOffset
35.0
Gets the latest stop offset in this offset-tree.
>>> score = corpus.parse('bwv66.6')
>>> tree = stream.timespans.TimespanCollection(score)
>>> tree.latestStopOffset
36.0
TimespanCollection methods
Creates a new offset-tree with the same timespans as this offset-tree.
This is analogous to dict.copy().
>>> score = corpus.parse('bwv66.6')
>>> tree = stream.timespans.TimespanCollection(score)
>>> newTree = tree.copy()
Finds timespans in this offset-tree which overlap offset.
>>> score = corpus.parse('bwv66.6')
>>> tree = stream.timespans.TimespanCollection(score)
>>> for timespan in tree.findTimespansOverlapping(0.5):
... timespan
...
<ElementTimespan 0.0:1.0 <music21.note.Note E>>
Finds timespans in this offset-tree which start at offset.
>>> score = corpus.parse('bwv66.6')
>>> tree = stream.timespans.TimespanCollection(score)
>>> for timespan in tree.findTimespansStartingAt(0.5):
... timespan
...
<ElementTimespan 0.5:1.0 <music21.note.Note B>>
<ElementTimespan 0.5:1.0 <music21.note.Note B>>
<ElementTimespan 0.5:1.0 <music21.note.Note G#>>
Finds timespans in this offset-tree which stop at offset.
>>> score = corpus.parse('bwv66.6')
>>> tree = stream.timespans.TimespanCollection(score)
>>> for timespan in tree.findTimespansStoppingAt(0.5):
... timespan
...
<ElementTimespan 0.0:0.5 <music21.note.Note C#>>
<ElementTimespan 0.0:0.5 <music21.note.Note A>>
<ElementTimespan 0.0:0.5 <music21.note.Note A>>
Gets start offset after offset.
>>> score = corpus.parse('bwv66.6')
>>> tree = stream.timespans.TimespanCollection(score)
>>> tree.getStartOffsetAfter(0.5)
1.0
>>> tree.getStartOffsetAfter(35) is None
True
Returns none if no succeeding offset exists.
Gets the start offset immediately preceding offset in this offset-tree.
>>> score = corpus.parse('bwv66.6')
>>> tree = stream.timespans.TimespanCollection(score)
>>> tree.getStartOffsetBefore(100)
35.0
>>> tree.getStartOffsetBefore(0) is None
True
Return none if no preceding offset exists.
Gets the verticality in this offset-tree which starts at offset.
>>> score = corpus.parse('bwv66.6')
>>> tree = stream.timespans.TimespanCollection(score)
>>> tree.getVerticalityAt(2.5)
<Verticality 2.5 {G#3 B3 E4 B4}>
Return verticality.
Gets index of timespan in tree.
>>> timespans = [
... stream.timespans.Timespan(0, 2),
... stream.timespans.Timespan(0, 9),
... stream.timespans.Timespan(1, 1),
... stream.timespans.Timespan(2, 3),
... stream.timespans.Timespan(3, 4),
... stream.timespans.Timespan(4, 9),
... stream.timespans.Timespan(5, 6),
... stream.timespans.Timespan(5, 8),
... stream.timespans.Timespan(6, 8),
... stream.timespans.Timespan(7, 7),
... ]
>>> tree = stream.timespans.TimespanCollection()
>>> tree.insert(timespans)
>>> for timespan in timespans:
... print timespan, tree.index(timespan)
...
<Timespan 0 2> 0
<Timespan 0 9> 1
<Timespan 1 1> 2
<Timespan 2 3> 3
<Timespan 3 4> 4
<Timespan 4 9> 5
<Timespan 5 6> 6
<Timespan 5 8> 7
<Timespan 6 8> 8
<Timespan 7 7> 9
>>> tree.index(stream.timespans.Timespan(-100, 100))
Traceback (most recent call last):
ValueError: <Timespan -100 100> not in timespan collection.
Inserts timespans into this offset-tree.
TODO: remove asserts...
Iterates consonant-bounded verticality subsequences in this offset-tree.
>>> score = corpus.parse('bwv66.6')
>>> tree = stream.timespans.TimespanCollection(score)
>>> for subsequence in tree.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]
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')
>>> tree = stream.timespans.TimespanCollection(score)
>>> iterator = tree.iterateVerticalities()
>>> for _ in range(10):
... iterator.next()
...
<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 = tree.iterateVerticalities(reverse=True)
>>> for _ in range(10):
... iterator.next()
...
<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}>
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')
>>> tree = stream.timespans.TimespanCollection(score)
>>> iterator = tree.iterateVerticalitiesNwise(n=2)
>>> for _ in range(4):
... print iterator.next()
...
<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 = tree.iterateVerticalitiesNwise(n=2, reverse=True)
>>> for _ in range(4):
... print iterator.next()
...
<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}>
]>
TODO: remove assert
Removes timespans from this offset-tree.
TODO: remove assert
Splits all timespans in this offset-tree at offsets, operating in place.
>>> score = corpus.parse('bwv66.6')
>>> tree = stream.timespans.TimespanCollection(score)
>>> tree.findTimespansStartingAt(0.1)
()
>>> for timespan in tree.findTimespansOverlapping(0.1):
... timespan
...
<ElementTimespan 0.0:0.5 <music21.note.Note C#>>
<ElementTimespan 0.0:0.5 <music21.note.Note A>>
<ElementTimespan 0.0:0.5 <music21.note.Note A>>
<ElementTimespan 0.0:1.0 <music21.note.Note E>>
>>> tree.splitAt(0.1)
>>> for timespan in tree.findTimespansStartingAt(0.1):
... timespan
...
<ElementTimespan 0.1:0.5 <music21.note.Note C#>>
<ElementTimespan 0.1:0.5 <music21.note.Note A>>
<ElementTimespan 0.1:0.5 <music21.note.Note A>>
<ElementTimespan 0.1:1.0 <music21.note.Note E>>
>>> tree.findTimespansOverlapping(0.1)
()
Creates a score from the ElementTimespan objects stored in this offset-tree.
A “template” score may be used to provide measure and time-signature information.
>>> score = corpus.parse('bwv66.6')
>>> tree = stream.timespans.TimespanCollection(score)
>>> chordifiedScore = tree.toChordifiedScore()
>>> chordifiedScore.show('text')
{0.0} <music21.stream.Measure 0 offset=0.0>
{0.0} <music21.clef.TrebleClef>
{0.0} <music21.key.KeySignature of 3 sharps, mode minor>
{0.0} <music21.meter.TimeSignature 4/4>
{0.0} <music21.chord.Chord A3 E4 C#5>
{0.5} <music21.chord.Chord G#3 B3 E4 B4>
{1.0} <music21.stream.Measure 1 offset=1.0>
{0.0} <music21.chord.Chord F#3 C#4 F#4 A4>
{1.0} <music21.chord.Chord G#3 B3 E4 B4>
{2.0} <music21.chord.Chord A3 E4 C#5>
{3.0} <music21.chord.Chord G#3 B3 E4 E5>
{5.0} <music21.stream.Measure 2 offset=5.0>
{0.0} <music21.chord.Chord A3 E4 C#5>
{0.5} <music21.chord.Chord C#3 E4 A4 C#5>
{1.0} <music21.chord.Chord E3 E4 G#4 B4>
{1.5} <music21.chord.Chord E3 D4 G#4 B4>
{2.0} <music21.chord.Chord A2 C#4 E4 A4>
{3.0} <music21.chord.Chord E#3 C#4 G#4 C#5>
{9.0} <music21.stream.Measure 3 offset=9.0>
{0.0} <music21.layout.SystemLayout>
{0.0} <music21.chord.Chord F#3 C#4 F#4 A4>
{0.5} <music21.chord.Chord B2 D4 G#4 B4>
{1.0} <music21.chord.Chord C#3 C#4 E#4 G#4>
{1.5} <music21.chord.Chord C#3 B3 E#4 G#4>
{2.0} <music21.chord.Chord F#2 A3 C#4 F#4>
{3.0} <music21.chord.Chord F#3 C#4 F#4 A4>
...
TODO: remove assert
Unwraps a sequence of Verticality objects into a dictionary of Part:Horizontality key/value pairs.
>>> score = corpus.parse('bwv66.6')
>>> tree = stream.timespans.TimespanCollection(score)
>>> iterator = tree.iterateVerticalitiesNwise()
>>> verticalities = iterator.next()
>>> unwrapped = tree.unwrapVerticalities(verticalities)
>>> for part in sorted(unwrapped,
... key=lambda x: x.getInstrument().partName,
... ):
... print part
... horizontality = unwrapped[part]
... for timespan in horizontality:
... print '\t', timespan
...
<music21.stream.Part Alto>
<ElementTimespan 0.0:1.0 <music21.note.Note E>>
<ElementTimespan 1.0:2.0 <music21.note.Note F#>>
<music21.stream.Part Bass>
<ElementTimespan 0.0:0.5 <music21.note.Note A>>
<ElementTimespan 0.5:1.0 <music21.note.Note G#>>
<ElementTimespan 1.0:2.0 <music21.note.Note F#>>
<music21.stream.Part Soprano>
<ElementTimespan 0.0:0.5 <music21.note.Note C#>>
<ElementTimespan 0.5:1.0 <music21.note.Note B>>
<ElementTimespan 1.0:2.0 <music21.note.Note A>>
<music21.stream.Part Tenor>
<ElementTimespan 0.0:0.5 <music21.note.Note A>>
<ElementTimespan 0.5:1.0 <music21.note.Note B>>
<ElementTimespan 1.0:2.0 <music21.note.Note C#>>
A collection of information about elements that are sounding at a given offset or just finished at that offset or are continuing from before, etc..
Create a timespan-stream from a score:
>>> score = corpus.parse('bwv66.6')
>>> tree = stream.timespans.TimespanCollection(score)
Find the verticality at offset 6.5, or beat 2.5 of measure 2 (there’s a one beat pickup)
>>> verticality = tree.getVerticalityAt(6.5)
>>> verticality
<Verticality 6.5 {E3 D4 G#4 B4}>
The representation of a verticality gives the pitches from lowest to highest (in sounding notes).
A verticality knows its startOffset, but because elements might end at different times, it doesn’t know its stopOffset
>>> verticality.startOffset
6.5
>>> verticality.stopOffset
Traceback (most recent call last):
AttributeError: 'Verticality' object has no attribute 'stopOffset'
However, we can find when the next verticality starts by looking at the nextVerticality
>>> verticality.nextVerticality
<Verticality 7.0 {A2 C#4 E4 A4}>
>>> verticality.nextVerticality.startOffset
7.0
Or more simply:
>>> verticality.nextStartOffset
7.0
(There is also a previousVerticality, but not a previousStartOffset)
What we just demonstrated is actually very powerful: a Verticality keeps a record of exactly where it is in the timespanStream – scores can be recreated with this information.
Getting back to the task at hand, we can find all the elementTimespans (and from there the elements) that start at exactly 6.5. There’s one, it’s a passing tone D in the tenor and it lastes from offset 6.5 to offset 7.0, with respect to the beginning of the score, not to the beginning of the measure. That is to say, it’s an eighth note
>>> verticality.startTimespans
(<ElementTimespan 6.5:7.0 <music21.note.Note D>>,)
And we can get all the elementTimespans that were already sounding at the moment (that is to say, the non-passing tones):
>>> verticality.overlapTimespans
(<ElementTimespan 6.0:7.0 <music21.note.Note B>>,
<ElementTimespan 6.0:7.0 <music21.note.Note G#>>,
<ElementTimespan 6.0:7.0 <music21.note.Note E>>)
And we can get all the things that stop right at this moment. It’s the E in the tenor preceding the passing tone D:
>>> verticality.stopTimespans
(<ElementTimespan 6.0:6.5 <music21.note.Note E>>,)
Verticality read-only properties
Gets the bass timespan in this verticality.
This is CURRENTLY the lowest PART not the lowest note necessarily.
>>> score = corpus.parse('bwv66.6')
>>> tree = stream.timespans.TimespanCollection(score)
>>> verticality = tree.getVerticalityAt(1.0)
>>> verticality
<Verticality 1.0 {F#3 C#4 F#4 A4}>
>>> verticality.bassTimespan
<ElementTimespan 1.0:2.0 <music21.note.Note F#>>
Gets the beat strength of a verticality.
>>> score = corpus.parse('bwv66.6')
>>> tree = stream.timespans.TimespanCollection(score)
>>> verticality = tree.getVerticalityAt(1.0)
>>> verticality.beatStrength
1.0
Counts the number of things sounding at this moment
Is true when the pitch set of a verticality is consonant.
>>> score = corpus.parse('bwv66.6')
>>> tree = stream.timespans.TimespanCollection(score)
>>> verticalities = list(tree.iterateVerticalities())
>>> for verticality in verticalities[:10]:
... print verticality, verticality.isConsonant
...
<Verticality 0.0 {A3 E4 C#5}> True
<Verticality 0.5 {G#3 B3 E4 B4}> True
<Verticality 1.0 {F#3 C#4 F#4 A4}> True
<Verticality 2.0 {G#3 B3 E4 B4}> True
<Verticality 3.0 {A3 E4 C#5}> True
<Verticality 4.0 {G#3 B3 E4 E5}> True
<Verticality 5.0 {A3 E4 C#5}> True
<Verticality 5.5 {C#3 E4 A4 C#5}> True
<Verticality 6.0 {E3 E4 G#4 B4}> True
<Verticality 6.5 {E3 D4 G#4 B4}> False
Gets the measure number of the verticality’s starting elements.
>>> score = corpus.parse('bwv66.6')
>>> tree = stream.timespans.TimespanCollection(score)
>>> verticality = tree.getVerticalityAt(7.0)
>>> verticality.measureNumber
2
Gets the next start-offset in the verticality’s offset-tree.
>>> score = corpus.parse('bwv66.6')
>>> tree = stream.timespans.TimespanCollection(score)
>>> verticality = tree.getVerticalityAt(1.0)
>>> verticality.nextStartOffset
2.0
Gets the next verticality after a verticality.
>>> score = corpus.parse('bwv66.6')
>>> tree = stream.timespans.TimespanCollection(score)
>>> verticality = tree.getVerticalityAt(1.0)
>>> print verticality
<Verticality 1.0 {F#3 C#4 F#4 A4}>
>>> nextVerticality = verticality.nextVerticality
>>> print nextVerticality
<Verticality 2.0 {G#3 B3 E4 B4}>
Verticality objects created by an offset-tree hold a reference back to that offset-tree. This means that they determine their next or previous verticality dynamically based on the state of the offset-tree only when asked. Because of this, it is safe to mutate the offset-tree by inserting or removing timespans while iterating over it.
>>> tree.remove(nextVerticality.startTimespans)
>>> verticality.nextVerticality
<Verticality 3.0 {A3 E4 C#5}>
Gets timespans overlapping the start offset of a verticality.
>>> score = corpus.parse('bwv66.6')
>>> tree = stream.timespans.TimespanCollection(score)
>>> verticality = tree.getVerticalityAt(0.5)
>>> verticality.overlapTimespans
(<ElementTimespan 0.0:1.0 <music21.note.Note E>>,)
Gets the pitch-class set of all elements in a verticality.
>>> score = corpus.parse('bwv66.6')
>>> tree = stream.timespans.TimespanCollection(score)
>>> verticality = tree.getVerticalityAt(1.0)
>>> for pitchClass in sorted(verticality.pitchClassSet):
... pitchClass
...
<music21.pitch.Pitch C#>
<music21.pitch.Pitch F#>
<music21.pitch.Pitch A>
Gets the pitch set of all elements in a verticality.
>>> score = corpus.parse('bwv66.6')
>>> tree = stream.timespans.TimespanCollection(score)
>>> verticality = tree.getVerticalityAt(1.0)
>>> for pitch in sorted(verticality.pitchSet):
... pitch
...
<music21.pitch.Pitch F#3>
<music21.pitch.Pitch C#4>
<music21.pitch.Pitch F#4>
<music21.pitch.Pitch A4>
Gets the previous verticality before a verticality.
>>> score = corpus.parse('bwv66.6')
>>> tree = stream.timespans.TimespanCollection(score)
>>> verticality = tree.getVerticalityAt(1.0)
>>> print verticality
<Verticality 1.0 {F#3 C#4 F#4 A4}>
>>> previousVerticality = verticality.previousVerticality
>>> print previousVerticality
<Verticality 0.5 {G#3 B3 E4 B4}>
Verticality objects created by an offset-tree hold a reference back to that offset-tree. This means that they determine their next or previous verticality dynamically based on the state of the offset-tree only when asked. Because of this, it is safe to mutate the offset-tree by inserting or removing timespans while iterating over it.
>>> tree.remove(previousVerticality.startTimespans)
>>> verticality.previousVerticality
<Verticality 0.0 {A3 E4 C#5}>
Gets the start offset of a verticality.
>>> score = corpus.parse('bwv66.6')
>>> tree = stream.timespans.TimespanCollection(score)
>>> verticality = tree.getVerticalityAt(1.0)
>>> verticality.startOffset
1.0
Gets the timespans starting at a verticality’s start offset.
>>> score = corpus.parse('bwv66.6')
>>> tree = stream.timespans.TimespanCollection(score)
>>> verticality = tree.getVerticalityAt(1.0)
>>> for timespan in verticality.startTimespans:
... timespan
...
<ElementTimespan 1.0:2.0 <music21.note.Note A>>
<ElementTimespan 1.0:2.0 <music21.note.Note F#>>
<ElementTimespan 1.0:2.0 <music21.note.Note C#>>
<ElementTimespan 1.0:2.0 <music21.note.Note F#>>
Gets the timespans stopping at a verticality’s start offset.
>>> score = corpus.parse('bwv66.6')
>>> tree = stream.timespans.TimespanCollection(score)
>>> verticality = tree.getVerticalityAt(1.0)
>>> for timespan in verticality.stopTimespans:
... timespan
...
<ElementTimespan 0.0:1.0 <music21.note.Note E>>
<ElementTimespan 0.5:1.0 <music21.note.Note B>>
<ElementTimespan 0.5:1.0 <music21.note.Note B>>
<ElementTimespan 0.5:1.0 <music21.note.Note G#>>
Verticality methods
creates a chord object from the verticality
A horizontality of consecutive elementTimespan objects.
Horizontality read-only properties
Is true if the horizontality contains a neighbor tone.
Is true if the horizontality contains a passing tone.
A span of time anchored to an element in a score. The span of time may be the same length as the element in the score. It may be shorter (a “slice” of an element) or it may be longer (in the case of a timespan that is anchored to a single element but extends over rests or other notes following a note)
ElementTimespans give information about an element (such as a Note). It knows its absolute position with respect to the element passed into TimespanCollection. It contains information about what measure it’s in, what part it’s in, etc.
Example, getting a passing tone from a known location from a Bach chorale.
First we create an Offset tree:
>>> score = corpus.parse('bwv66.6')
>>> tree = stream.timespans.TimespanCollection(score)
>>> tree
<music21.stream.timespans.TimespanCollection object at 0x...>
Then get the verticality from offset 6.5, which is beat two-and-a-half of measure 2 (the piece is in 4/4 with a quarter-note pickup)
>>> verticality = tree.getVerticalityAt(6.5)
>>> verticality
<Verticality 6.5 {E3 D4 G#4 B4}>
There are four elementTimespans in the verticality – each representing a note. The notes are arranged from lowest to highest.
We can find all the elementTimespans that start exactly at 6.5. There’s one.
>>> verticality.startTimespans
(<ElementTimespan 6.5:7.0 <music21.note.Note D>>,)
>>> elementTimespan = verticality.startTimespans[0]
>>> elementTimespan
<ElementTimespan 6.5:7.0 <music21.note.Note D>>
What can we do with a elementTimespan? We can get its Part object or the Part object name
>>> elementTimespan.part
<music21.stream.Part Tenor>
>>> elementTimespan.partName
u'Tenor'
Find out what measure it’s in:
>>> elementTimespan.measureNumber
2
>>> elementTimespan.measureStartOffset
5.0
The position in the measure is given by subtracting that from the .startOffset:
>>> elementTimespan.startOffset - elementTimespan.measureStartOffset
1.5
>>> elementTimespan.beatStrength
0.125
>>> elementTimespan.element
<music21.note.Note D>
These are not dynamic, so changing the Score object does not change the measureNumber, beatStrength, etc.
ElementTimespan read-only properties
The elementTimespan’s element’s beat-strength.
This may be overriden during instantiation by passing in a custom beat-strength. That can be useful when you are generating new elementTimespans based on old ones, and want to maintain pitch information from the old elementTimespan but change the start offset to reflect that of another timespan.
The elementTimespan’s element.
>>> score = corpus.parse('bwv66.6')
>>> tree = stream.timespans.TimespanCollection(score)
>>> verticality = tree.getVerticalityAt(1.0)
>>> elementTimespan = verticality.startTimespans[0]
>>> elementTimespan.element
<music21.note.Note A>
The measure number of the measure containing the element.
>>> score = corpus.parse('bwv66.6')
>>> tree = stream.timespans.TimespanCollection(score)
>>> verticality = tree.getVerticalityAt(1.0)
>>> elementTimespan = verticality.startTimespans[0]
>>> elementTimespan.measureNumber
1
The Stream hierarchy above the elementTimespan’s element.
>>> score = corpus.parse('bwv66.6')
>>> tree = stream.timespans.TimespanCollection(score)
>>> verticality = tree.getVerticalityAt(1.0)
>>> elementTimespan = verticality.startTimespans[0]
>>> for x in elementTimespan.parentage:
... x
...
<music21.stream.Measure 1 offset=1.0>
<music21.stream.Part Soprano>
<music21.stream.Score ...>
The part name of the part containing the elementTimespan’s element.
>>> score = corpus.parse('bwv66.6')
>>> tree = stream.timespans.TimespanCollection(score)
>>> verticality = tree.getVerticalityAt(1.0)
>>> elementTimespan = verticality.startTimespans[0]
>>> elementTimespan.partName
u'Soprano'
Gets the pitches of the element wrapped by this elementTimespan.
This treats notes as chords.
The start offset of the elementTimespan’s element, relative to its containing score.
>>> score = corpus.parse('bwv66.6')
>>> tree = stream.timespans.TimespanCollection(score)
>>> verticality = tree.getVerticalityAt(1.0)
>>> elementTimespan = verticality.startTimespans[0]
>>> elementTimespan.startOffset
1.0
The stop offset of the elementTimespan’s element, relative to its containing score.
>>> score = corpus.parse('bwv66.6')
>>> tree = stream.timespans.TimespanCollection(score)
>>> verticality = tree.getVerticalityAt(1.0)
>>> elementTimespan = verticality.startTimespans[0]
>>> elementTimespan.stopOffset
2.0
ElementTimespan methods
Split elementTimespan at offset.
>>> score = corpus.parse('bwv66.6')
>>> tree = stream.timespans.TimespanCollection(score)
>>> verticality = tree.getVerticalityAt(0)
>>> timespan = verticality.startTimespans[0]
>>> timespan
<ElementTimespan 0.0:0.5 <music21.note.Note C#>>
>>> for shard in timespan.splitAt(0.25):
... shard
...
<ElementTimespan 0.0:0.25 <music21.note.Note C#>>
<ElementTimespan 0.25:0.5 <music21.note.Note C#>>
>>> timespan.splitAt(1000)
(<ElementTimespan 0.0:0.5 <music21.note.Note C#>>,)
A segment of verticalities.
VerticalitySequence methods