Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions music21/stream/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ def asTimespans(self, *, flatten=True, classList=None):
highly optimized data structure for searching through elements and
offsets.

>>> score = tree.makeExampleScore()
>>> score = tree.examples.makeExampleScore()
>>> scoreTree = score.asTimespans()
>>> print(scoreTree)
<TimespanTree {20} (0.0 to 8.0) <music21.stream.Score exampleScore>>
Expand Down Expand Up @@ -519,17 +519,17 @@ def asTree(self, *, flatten=False, classList=None, useTimespans=False, groupOffs

See tree.fromStream.asTree() for more details.

>>> score = tree.makeExampleScore()
>>> score = tree.examples.makeExampleScore()
>>> scoreTree = score.asTree(flatten=True)
>>> scoreTree
<ElementTree {20} (0.0 <0.-25...> to 8.0) <music21.stream.Score exampleScore>>
'''
if t.TYPE_CHECKING:
assert isinstance(self, Stream)
hashedAttributes = hash((tuple(classList or ()),
flatten,
useTimespans,
groupOffsets))
flatten,
useTimespans,
groupOffsets))
cacheKey = 'elementTree' + str(hashedAttributes)
if cacheKey not in self._cache or self._cache[cacheKey] is None:
hashedElementTree = tree.fromStream.asTree(self,
Expand Down
71 changes: 3 additions & 68 deletions music21/tree/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
__all__ = [
'analysis',
'core',
'examples',
'fromStream',
'makeExampleScore',
'node',
Expand All @@ -35,6 +36,8 @@

from music21.tree import analysis
from music21.tree import core
from music21.tree import examples
from music21.tree.examples import makeExampleScore
from music21.tree import fromStream
from music21.tree import node
from music21.tree import spans
Expand All @@ -43,74 +46,6 @@
from music21.tree import verticality


# -----------------------------------------------------------------------------

# TODO: Test with scores with Voices: cpebach/h186

def makeExampleScore():
r'''
Makes example score for use in stream-to-tree conversion docs.

>>> sc = tree.makeExampleScore()
>>> sc.show('text')
{0.0} <music21.stream.Part ...>
{0.0} <music21.instrument.Instrument 'PartA: : '>
{0.0} <music21.stream.Measure 1 offset=0.0>
{0.0} <music21.clef.BassClef>
{0.0} <music21.meter.TimeSignature 2/4>
{0.0} <music21.note.Note C>
{1.0} <music21.note.Note D>
{2.0} <music21.stream.Measure 2 offset=2.0>
{0.0} <music21.note.Note E>
{1.0} <music21.note.Note F>
{4.0} <music21.stream.Measure 3 offset=4.0>
{0.0} <music21.note.Note G>
{1.0} <music21.note.Note A>
{6.0} <music21.stream.Measure 4 offset=6.0>
{0.0} <music21.note.Note B>
{1.0} <music21.note.Note C>
{2.0} <music21.bar.Barline type=final>
{0.0} <music21.stream.Part ...>
{0.0} <music21.instrument.Instrument 'PartB: : '>
{0.0} <music21.stream.Measure 1 offset=0.0>
{0.0} <music21.clef.BassClef>
{0.0} <music21.meter.TimeSignature 2/4>
{0.0} <music21.note.Note C#>
{2.0} <music21.stream.Measure 2 offset=2.0>
{0.0} <music21.note.Note G#>
{4.0} <music21.stream.Measure 3 offset=4.0>
{0.0} <music21.note.Note E#>
{6.0} <music21.stream.Measure 4 offset=6.0>
{0.0} <music21.note.Note D#>
{2.0} <music21.bar.Barline type=final>
'''
from music21 import converter
from music21 import stream
streamA = converter.parse('tinynotation: 2/4 C4 D E F G A B C')
streamB = converter.parse('tinynotation: 2/4 C#2 G# E# D#')
streamA.makeMeasures(inPlace=True)
streamB.makeMeasures(inPlace=True)
partA = stream.Part()
for x in streamA:
partA.append(x)
instrumentA = partA.getInstrument()
instrumentA.partId = 'PartA'
partA.insert(0, instrumentA)
partB = stream.Part()
for x in streamB:
partB.append(x)
instrumentB = partB.getInstrument()
instrumentB.partId = 'PartB'
partB.insert(0, instrumentB)
score = stream.Score(id='exampleScore')
score.insert(0, partA)
score.insert(0, partB)
return score


# -----------------------------------------------------------------------------


if __name__ == '__main__':
import music21
music21.mainTest()
16 changes: 8 additions & 8 deletions music21/tree/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class AVLNode(common.SlottedObjectMixin):

This attribute is used to help balance the AVL tree.

>>> score = tree.makeExampleScore()
>>> score = tree.examples.makeExampleScore()
>>> scoreTree = tree.fromStream.asTimespans(score, flatten=True,
... classList=(note.Note, chord.Chord))
>>> print(scoreTree.debug())
Expand Down Expand Up @@ -111,7 +111,7 @@ class AVLNode(common.SlottedObjectMixin):

This property is used to help balance the AVL tree.

>>> score = tree.makeExampleScore()
>>> score = tree.examples.makeExampleScore()
>>> scoreTree = tree.fromStream.asTimespans(score, flatten=True,
... classList=(note.Note, chord.Chord))
>>> print(scoreTree.debug())
Expand Down Expand Up @@ -149,7 +149,7 @@ class AVLNode(common.SlottedObjectMixin):
The position of this node -- this is often the same as the offset of
the node in a containing score, but does not need to be. It could be the .sortTuple

>>> score = tree.makeExampleScore()
>>> score = tree.examples.makeExampleScore()
>>> scoreTree = tree.fromStream.asTimespans(score, flatten=True,
... classList=(note.Note, chord.Chord))
>>> print(scoreTree.rootNode.debug())
Expand All @@ -176,7 +176,7 @@ class AVLNode(common.SlottedObjectMixin):

After setting the left child you need to do a node update. with node.update()

>>> score = tree.makeExampleScore()
>>> score = tree.examples.makeExampleScore()
>>> scoreTree = tree.fromStream.asTimespans(score, flatten=True,
... classList=(note.Note, chord.Chord))
>>> print(scoreTree.rootNode.debug())
Expand All @@ -199,7 +199,7 @@ class AVLNode(common.SlottedObjectMixin):

After setting the right child you need to do a node update. with node.update()

>>> score = tree.makeExampleScore()
>>> score = tree.examples.makeExampleScore()
>>> scoreTree = tree.fromStream.asTimespans(score, flatten=True,
... classList=(note.Note, chord.Chord))
>>> print(scoreTree.rootNode.debug())
Expand Down Expand Up @@ -275,7 +275,7 @@ def debug(self):
'''
Get a debug of the Node:

>>> score = tree.makeExampleScore()
>>> score = tree.examples.makeExampleScore()
>>> scoreTree = tree.fromStream.asTimespans(score, flatten=True,
... classList=(note.Note, chord.Chord))
>>> rn = scoreTree.rootNode
Expand All @@ -297,7 +297,7 @@ def _getDebugPieces(self):

Called recursively

>>> score = tree.makeExampleScore()
>>> score = tree.examples.makeExampleScore()
>>> scoreTree = tree.fromStream.asTimespans(score, flatten=True,
... classList=(note.Note, chord.Chord))
>>> rn = scoreTree.rootNode
Expand Down Expand Up @@ -338,7 +338,7 @@ def update(self):

We create a score with everything correct.

>>> score = tree.makeExampleScore()
>>> score = tree.examples.makeExampleScore()
>>> scoreTree = tree.fromStream.asTimespans(score, flatten=True,
... classList=(note.Note, chord.Chord))
>>> n = scoreTree.rootNode
Expand Down
67 changes: 67 additions & 0 deletions music21/tree/examples.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# -----------------------------------------------------------------------------

from music21 import converter
from music21 import stream

# TODO: Test with scores with Voices: cpebach/h186

def makeExampleScore():
r'''
Makes example score for use in stream-to-tree conversion docs.

>>> sc = tree.examples.makeExampleScore()
>>> sc.show('text')
{0.0} <music21.stream.Part ...>
{0.0} <music21.instrument.Instrument 'PartA: : '>
{0.0} <music21.stream.Measure 1 offset=0.0>
{0.0} <music21.clef.BassClef>
{0.0} <music21.meter.TimeSignature 2/4>
{0.0} <music21.note.Note C>
{1.0} <music21.note.Note D>
{2.0} <music21.stream.Measure 2 offset=2.0>
{0.0} <music21.note.Note E>
{1.0} <music21.note.Note F>
{4.0} <music21.stream.Measure 3 offset=4.0>
{0.0} <music21.note.Note G>
{1.0} <music21.note.Note A>
{6.0} <music21.stream.Measure 4 offset=6.0>
{0.0} <music21.note.Note B>
{1.0} <music21.note.Note C>
{2.0} <music21.bar.Barline type=final>
{0.0} <music21.stream.Part ...>
{0.0} <music21.instrument.Instrument 'PartB: : '>
{0.0} <music21.stream.Measure 1 offset=0.0>
{0.0} <music21.clef.BassClef>
{0.0} <music21.meter.TimeSignature 2/4>
{0.0} <music21.note.Note C#>
{2.0} <music21.stream.Measure 2 offset=2.0>
{0.0} <music21.note.Note G#>
{4.0} <music21.stream.Measure 3 offset=4.0>
{0.0} <music21.note.Note E#>
{6.0} <music21.stream.Measure 4 offset=6.0>
{0.0} <music21.note.Note D#>
{2.0} <music21.bar.Barline type=final>
'''
streamA = converter.parse('tinynotation: 2/4 C4 D E F G A B C')
streamB = converter.parse('tinynotation: 2/4 C#2 G# E# D#')
streamA.makeMeasures(inPlace=True)
streamB.makeMeasures(inPlace=True)
partA = stream.Part()
for x in streamA:
partA.append(x)
instrumentA = partA.getInstrument()
instrumentA.partId = 'PartA'
partA.insert(0, instrumentA)
partB = stream.Part()
for x in streamB:
partB.append(x)
instrumentB = partB.getInstrument()
instrumentB.partId = 'PartB'
partB.insert(0, instrumentB)
score = stream.Score(id='exampleScore')
score.insert(0, partA)
score.insert(0, partB)
return score


# -----------------------------------------------------------------------------
6 changes: 3 additions & 3 deletions music21/tree/fromStream.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def listOfTreesByClass(
This is used internally by `streamToTimespanTree`.


>>> score = tree.makeExampleScore()
>>> score = tree.examples.makeExampleScore()

Get everything in the score

Expand Down Expand Up @@ -168,7 +168,7 @@ def asTree(

Use Stream.asTree() generally since that caches the ElementTree.

>>> score = tree.makeExampleScore()
>>> score = tree.examples.makeExampleScore()
>>> elementTree = tree.fromStream.asTree(score)
>>> elementTree
<ElementTree {2} (0.0 <0.-20...> to 8.0) <music21.stream.Score exampleScore>>
Expand Down Expand Up @@ -401,7 +401,7 @@ def testFastPopulate(self):
self.assertIs(fastI, slowI)

def testAutoSortExample(self):
from music21.tree import makeExampleScore
from music21.tree.examples import makeExampleScore
sc = makeExampleScore()
sc.sort()
scTree = asTree(sc)
Expand Down
4 changes: 2 additions & 2 deletions music21/tree/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ class OffsetNode(ElementNode):

Here's an example of what it means and does:

>>> score = tree.makeExampleScore()
>>> score = tree.examples.makeExampleScore()
>>> sf = score.flatten()
>>> sf.show('text', addEndTimes=True)
{0.0 - 0.0} <music21.instrument.Instrument 'PartA: : '>
Expand Down Expand Up @@ -400,7 +400,7 @@ class OffsetNode(ElementNode):
The contents of the node at this point. Usually a list of ElementTimespans
or PitchedTimespans.

>>> score = tree.makeExampleScore()
>>> score = tree.examples.makeExampleScore()
>>> scoreTree = tree.fromStream.asTimespans(score, flatten=True,
... classList=(note.Note, chord.Chord))
>>> print(scoreTree.rootNode.debug())
Expand Down
Loading