
    rh             	         S r SSKJr  SSKJrJrJr  SSKJrJ	r	J
r
  SSKrSSKJr  SSKrSSKrSSKJr  SSKrSSKrSSKrSSKrSSKJr  SSKrSSKrSS	KJr  SS
KJr  SSKJr  SSKJrJr  SSK J!r!  SSK"J#r#J$r$J%r%J&r&J'r'  SSKJ(r(  SSKJ)r)  SSKJ*r*  SSKJ+r+  SSKJ,r,  SSKJ-r-  SSKJ.r.  SSKJ/r/  SSKJ0r0  SSKJ1r1  SSKJ2r2  SSKJ3r3  SSKJ4r4  SSKJ5r5  SSKJ6r6  SSKJ7r7  SSKJ8r8  SS KJ9r9  SS!KJ:r:  SS"K;J<r<  SS#K;J=r=  SS$K;J>r>  SS%K;J?r?  SS&K;J@r@  SS'KAJBrBJCrCJDrD  \R                  (       a  SS(KJFrF  \-R                  " S)5      rH\.R                  rI\.R                  rJ\R                  " S*5      rL\4R                  S-  \NS+   -  rO\" S,/ S-Q5      rP " S. S/\Q5      rR\" S0/ S1Q5      rS " S2 S3\<R                  \R                  \$   5      rV " S4 S5\V5      rW " S6 S7\V5      rX " S8 S9\V5      rY " S: S;\Y5      rZ " S< S=\V5      r[ " S> S?\V5      r\ " S@ SA\V5      r] " SB SC\V5      r^ " SD SE\V5      r_ " SF SG\R                  5      ra\V\X\Y\\\]\W\^\_\S/	rb\cSH:X  a  SSKr\R                  " \a5        gg)Ia  
The :class:`~music21.stream.Stream` and its subclasses
(which are themselves subclasses of the :class:`~music21.base.Music21Object`)
are the fundamental containers of offset-positioned notation and
musical elements in music21. Common Stream subclasses, such
as the :class:`~music21.stream.Measure`, :class:`~music21.stream.Part`
and :class:`~music21.stream.Score` objects, are also in this module.
    )annotations)deque
namedtupleOrderedDict)
CollectionIterableSequenceN)Fraction)isclose)overload)base)bar)common)GatherSpannersOffsetSpecial)opFrac)
StreamType
M21ObjTypeChangedM21ObjTypeOffsetQLOffsetQLSpecial)clef)chord)defaults)
derivation)duration)environment)exceptions21)interval)
instrumentkey)metadata)meter)note)pitch)tie)repeat)sites)style)tempo)core)makeNotation)streamStatus)iterator)filters)GivenElementsBehaviorRecursionType
ShowNumberspannerstreamTRecursiveLyricListBestQuantizationMatch)remainingGaperrortickmatchsignedErrordivisorc                      \ rS rSrSrg)StreamDeprecationWarning\    N)__name__
__module____qualname____firstlineno____static_attributes__rC       M/home/james-whalen/.local/lib/python3.13/site-packages/music21/stream/base.pyrA   rA   \   s     	rI   rA   	OffsetMap)elementoffsetendTime
voiceIndexc                  x  ^  \ rS rSr% SrSrSrSrS\S'   \	R                  rS\S	'   \R                  r/ S
QrSSSSSSS.rS\S'    GS\R$                  S.   GSU 4S jjjjrS rGSS jrGSS jrGSU 4S jjrGSU 4S jjrGSS jrGSS jrGSS jr\GSS j5       r\GSS  j5       r\GSS! j5       r\    GSS" j5       r\    GSS# j5       r\    GS S$ j5       r    GS!S% jrGS"S& jrGS"S' jrGS#S( jr \!GS$S) j5       r"\"RF                  GS%S* j5       r"S+ r$S, r%GS&S- jr&S. r'\!GS'S/ j5       r(\(RF                  GS(S0 j5       r(\!GS)S1 j5       r)\)RF                  GS*S2 j5       r)\!GS+S3 j5       r*\*RF                  GS,S4 j5       r*\!GSS5 j5       r+\+RF                  GS-S6 j5       r+GS.S7 jr,GSGS/S8 jjr-GS0U 4S9 jjr.\/R`                  " S:S;S<5      GS1S= j5       r1GS2S> jr2GSS? jr3GS3S@ jr4SSSA. GS4SB jjr5GSGS5SC jjr6SD r7GS.SE jr8SF r9 GSSSG.   GS6U 4SH jjjjr:GSGS7SI jjr;SJ r<    GS8SK jr=GS2SL jr> GSSSSM.SN jjr?GS2SO jr@SP rAGS2SQ jrBGSSR jrC GS2   GS9SS jjrDSSST.         GS:SU jjrESSV.GS;SW jjrFSSSSSX. GS;SY jjrGSZSSSSS[. GSS\ jjrHSSS].GSS^ jjrISSS].GSS_ jjrJ    GS<SS`.         GS=Sa jjjrKGS>Sb jrL GSSSSc. GS?Sd jjjrM\    GS@Se j5       rN\    GSASf j5       rN\    GSBSg j5       rN    GSCSh jrNGSDSi jrOGSDSj jrPGSESk jrQ GSSSSSSSl. GSDSm jjjrR GSSSn.       GSFSo jjjrS GS     GSGSp jjrTGSSq jrU        GSHSr jrVSs\WR                  SSt. GSISu jjrYSsSSv. GSJSw jjrZSSSS\[" 5       Sx.Sy jr\ GSK   GSLSz jjr]S{ r^S| r_\!" \^\_S}S~9r`\!S 5       ra\!GSMS j5       rb\!GSNS j5       rc\cRF                  GSOS j5       rc\SSSS.           GSPS jj5       rd\SSSSS.           GSQS jj5       rdSSSSS.           GSRS jjrdGSSS jreSSS. GSTS jjrfSSSS.     GSUS jjrgSSSSS.S jrhSSSS. GSVS jjriSSSS. GSWS jjrj\kR                  " S5      4SS.S jjrmGSXS jrnS roS rpS rq  GSYS jrrGSZS jrsSSSSSS.S jrtS ruGSS jrv       GS[S jrw     GS\S jrxSSS\kR                  44S jrzSSSS.S jr{SSSSSSSSSSSSS.                       GS]S jjr|S r}SSSSSSSSSSSSSS.                 GS^S jjr~SS.S jr\SS.       GS_S jj5       r\SSS.       GS`S jj5       rSSS.       GSaS jjrGSbS jrGS2S jrS rGS2GS7S jjr\!S 5       r\SSSSS.   GScS jj5       r\SSSS.   GSdS jj5       rSSSSS.       GSeS jjrSS.   GSfS jjrS rGSgS jr\!S 5       rS r\!S 5       r\!S 5       rS rS r\!GShS j5       r\RF                  GSiS j5       rS rS r\!" \\SS~9rGSS jrS rGSS jr\!" \SS~9rGSjS jrGSkS jr\!" \\SS~9rS r\!S 5       r\!S 5       r\!S 5       r\!S 5       rGSgS jrSSSS. GSlS jjrSSSS.S jrSS.S jrSS.S jr     GSm         GSnS jjrGSgGSoS jjrSSSS.S jrSSS.S jr    GSpS jr    GSpS jrS rS rS rS rGSqS jr\!GSrS j5       r\!GSsS j5       r\!GStS j5       r\SSSSSSS.               GSuS jj5       r\SSSSSS.               GSvS jj5       r\SSSSSSSS.               GSwS jj5       rSSSSSSSS.               GSxS jjrS rGSyS jrGS2S jrGSzS jrS rS r\!GSqS j5       rS rGSqS jrS rS rS rGSGS  jrGSGS jrSSGS.GS jrSGS.GS{GS jjrSGS.GS jrGS rSSGS	.GS
 jr   GS|       GS}GS jjrGSSSGS.GS jjrGSgGS jrGSgGS jrGSgGS jr GS~GS jrGS rSS.GS jrGSrU =r$ (  Streamh   a  
This is the fundamental container for Music21Objects;
objects may be ordered and/or placed in time based on
offsets from the start of this container.

As a subclass of Music21Object, Streams have offsets,
priority, id, and groups.

Streams may be embedded within other Streams. As each
Stream can have its own offset, when Streams are
embedded the offset of an element is relatively only
to its parent Stream. The :meth:`~music21.stream.Stream.flatten`
and method provides access to a flat version of all
embedded Streams, with offsets relative to the
top-level Stream.

The Stream :attr:`~music21.stream.Stream.elements` attribute
returns the contents of the Stream as a list. Direct access
to, and manipulation of, the elements list is not recommended.
Instead, use the host of high-level methods available.

The Stream, like all Music21Objects, has a
:class:`music21.duration.Duration` that is usually the
"release" time of the chronologically last element in the Stream
(that is, the highest onset plus the duration of
any element in the Stream).
The duration, however, can be "unlinked" and explicitly
set independent of the Stream's contents.

The first element passed to the Stream is an optional single
Music21Object or a list, tuple, or other Stream of Music21Objects
which is used to populate the Stream by inserting each object at
its :attr:`~music21.base.Music21Object.offset`
property. One special case is when every such object, such as a newly created
one, has no offset. Then, so long as the entire list is not composed of
non-Measure Stream subclasses representing synchrony like Parts or Voices,
each element is appended, creating a sequence of elements in time,
rather than synchrony.

Other arguments and keywords are ignored, but are
allowed so that subclassing the Stream is easier.

>>> s1 = stream.Stream()
>>> s1.append(note.Note('C#4', type='half'))
>>> s1.append(note.Note('D5', type='quarter'))
>>> s1.duration.quarterLength
3.0
>>> for thisNote in s1.notes:
...     print(thisNote.octave)
...
4
5

This is a demonstration of creating a Stream with other elements,
including embedded Streams (in this case, :class:`music21.stream.Part`,
a Stream subclass):

>>> c1 = clef.TrebleClef()
>>> c1.offset = 0.0
>>> c1.priority = -1
>>> n1 = note.Note('E-6', type='eighth')
>>> n1.offset = 1.0
>>> p1 = stream.Part()
>>> p1.offset = 0.0
>>> p1.id = 'embeddedPart'
>>> p1.append(note.Rest())  # quarter rest
>>> s2 = stream.Stream([c1, n1, p1])
>>> s2.duration.quarterLength
1.5
>>> s2.show('text')
{0.0} <music21.clef.TrebleClef>
{0.0} <music21.stream.Part embeddedPart>
    {0.0} <music21.note.Rest quarter>
{1.0} <music21.note.Note E->

* New in v7: providing a single element now works:

>>> s = stream.Stream(meter.TimeSignature())
>>> s.first()
<music21.meter.TimeSignature 4/4>

Providing a list of objects or Measures or Scores (but not other Stream
subclasses such as Parts or Voices) positions sequentially, i.e. appends, if they
all have offset 0.0 currently:

>>> s2 = stream.Measure([note.Note(), note.Note(), bar.Barline()])
>>> s2.show('text')
{0.0} <music21.note.Note C>
{1.0} <music21.note.Note C>
{2.0} <music21.bar.Barline type=regular>

A list of measures will thus each be appended:

>>> m1 = stream.Measure(n1, number=1)
>>> m2 = stream.Measure(note.Rest(), number=2)
>>> s3 = stream.Part([m1, m2])
>>> s3.show('text')
{0.0} <music21.stream.Measure 1 offset=0.0>
    {1.0} <music21.note.Note E->
{1.5} <music21.stream.Measure 2 offset=1.5>
    {0.0} <music21.note.Rest quarter>

Here, every element is a Stream that's not a Measure (or Score), so it
will be inserted at 0.0, rather than appending:

>>> s4 = stream.Score([stream.PartStaff(n1), stream.PartStaff(note.Rest())])
>>> s4.show('text')
{0.0} <music21.stream.PartStaff 0x...>
    {1.0} <music21.note.Note E->
{0.0} <music21.stream.PartStaff 0x...>
    {0.0} <music21.note.Rest quarter>

Create nested streams in one fell swoop:

>>> s5 = stream.Score(stream.Part(stream.Measure(chord.Chord('C2 A2'))))
>>> s5.show('text')
{0.0} <music21.stream.Part 0x...>
    {0.0} <music21.stream.Measure 0 offset=0.0>
        {0.0} <music21.chord.Chord C2 A2>

This behavior can be modified by the `givenElementsBehavior` keyword to go against the norm
of 'OFFSETS':

>>> from music21.stream.enums import GivenElementsBehavior
>>> s6 = stream.Stream([note.Note('C'), note.Note('D')],
...                    givenElementsBehavior=GivenElementsBehavior.INSERT)
>>> s6.show('text')  # all notes at offset 0.0
{0.0} <music21.note.Note C>
{0.0} <music21.note.Note D>

>>> p1 = stream.Part(stream.Measure(note.Note('C')), id='p1')
>>> p2 = stream.Part(stream.Measure(note.Note('D')), id='p2')
>>> s7 = stream.Score([p1, p2],
...                   givenElementsBehavior=GivenElementsBehavior.APPEND)
>>> s7.show('text')  # parts following each other (not recommended)
{0.0} <music21.stream.Part p1>
    {0.0} <music21.stream.Measure 0 offset=0.0>
        {0.0} <music21.note.Note C>
{1.0} <music21.stream.Part p2>
    {0.0} <music21.stream.Measure 0 offset=0.0>
        {0.0} <music21.note.Note D>

For developers of subclasses, please note that because of how Streams
are copied, there cannot be
required parameters (i.e., without defaults) in initialization.
For instance, this would not
be allowed, because craziness and givenElements are required::

    class CrazyStream(Stream):
        def __init__(self, givenElements, craziness, **keywords):
            ...

* New in v7: smart appending
* New in v8: givenElementsBehavior keyword configures the smart appending.
TFizint | floatclassSortOrderr2   recursionType)appendinsert
storeAtEndinsertAndShiftrecurseflatnotespitches	transposeaugmentOrDiminishscaleOffsetsscaleDurationsaw  
            Class variable:

            RecursionType Enum of (ELEMENTS_FIRST (default), FLATTEN, ELEMENTS_ONLY)
            that decides whether the stream likely holds relevant
            contexts for the elements in it.

            Define this for a stream class, not an individual object.

            see :meth:`~music21.base.Music21Object.contextSites`
            zQ
            Boolean describing whether the Stream is sorted or not.
            z
            Boolean describing whether the Stream is automatically sorted by
            offset whenever necessary.
            z
            Boolean describing whether this Stream contains embedded
            sub-Streams or Stream subclasses (not flat).
            a  
            Boolean that says whether all system breaks in the piece are
            explicitly defined.  Only used on musicxml output (maps to the
            musicxml <supports attribute="new-system"> tag) and only if this is
            the outermost Stream being shown
            a  
            Boolean that says whether all page breaks in the piece are
            explicitly defined.  Only used on musicxml output (maps to the
            musicxml <supports attribute="new-page"> tag) and only if this is
            the outermost Stream being shown.
            )rT   isSortedautoSortisFlatdefinesExplicitSystemBreaksdefinesExplicitPageBreaksdict[str, str]	_DOC_ATTRN)givenElementsBehaviorc                 > [         TU ]  " S0 UD6  SU l        [        R                  " U 5      U l        S U l        SU l        SU l        SU l        SU l	        SU l
        Uc  g [        U[        R                  5      (       a-  [        R                  " [         [        R                     U/5      nSnU["        R$                  :X  a4   ['        S U 5       5      nU(       a  ['        S U 5       5      (       a  SnOU["        R*                  :X  a  SnOSnU HJ  nU R-                  U5        U(       a  U R/                  U5        M.  U R1                  UR2                  U5        ML     U R5                  5         g ! [(         a     Nf = f)NFTunknownc              3  >   #    U  H  oR                   S :H  v   M     g7f)        N)rM   .0es     rJ   	<genexpr>"Stream.__init__.<locals>.<genexpr>i  s      H-QS-s   c              3     #    U  H=  nUR                   =(       a%    UR                  R                  [        [        45      v   M?     g 7fN)isStreamclassSet
isdisjointMeasureScorerm   s     rJ   rp   rq   l  s7      ",* ZZKAJJ$9$97E:J$KK*s   AArC   )super__init___created_via_deprecated_flatr.   StreamStatus_unlinkedDurationrb   rd   re   _atSoundingPitch_mutable
isinstancer   Music21Objecttcastlistr1   OFFSETSallAttributeErrorINSERTcoreGuardBeforeAddElement
coreAppend
coreInsertrM   coreElementsChanged)selfgivenElementsrh   keywords
appendBoolro   	__class__s         rJ   rz   Stream.__init__=  se    	$8$ -2)(55d;!% ,1().& <E  mT%7%788FF4(:(:#;m_MM 
 $9$A$AA  H- HH
 c ",*", , , #
"&;&B&BBJJA**1-"!,  	  "% " s   E2 2
E?>E?c                    XL $ )z?
No two streams are ever equal unless they are the same Stream
rC   )r   others     rJ   __eq__Stream.__eq__~  s     }rI   c                    [        U 5      S-	  $ )N   idr   s    rJ   __hash__Stream.__hash__  s    $x1}rI   c                :   U R                   b  U R                   [        U 5      :w  a@  [        U R                   5      [        [        U 5      5      :w  a  [        U R                   5      $ [        U R                   [        5      (       a  [	        U R                   5      $ gg)N )r   strr   inthexr   s    rJ   _reprInternalStream._reprInternal  sf    77ww"T("s477|s2d8}'D477|#DGGS))477|#rI   c                   > U R                   SL a!  U R                  (       a  U R                  5         [        TU ]  " SXS.UD6$ )NFfmtfprC   )ra   rb   sortry   write)r   r   r   r   r   s       rJ   r   Stream.write  s5    ==E!dmmIIKw}88x88rI   c                   > U R                   SL a!  U R                  (       a  U R                  5         [        TU ]  " SXS.UD6$ )NFr   apprC   )ra   rb   r   ry   show)r   r   r   r   r   s       rJ   r   Stream.show  s5    ==E!dmmIIKw|9999rI   c                X    [        U R                  5      [        U R                  5      -   $ )a  
Get the total number of elements in the Stream.
This method does not recurse into and count elements in contained Streams.

>>> import copy
>>> a = stream.Stream()
>>> for x in range(4):
...     n = note.Note('G#')
...     n.offset = x * 3
...     a.insert(n)
>>> len(a)
4

>>> b = stream.Stream()
>>> for x in range(4):
...     b.insert(copy.deepcopy(a))  # append streams
>>> len(b)
4
>>> len(b.flatten())
16

Includes end elements:

>>> b.storeAtEnd(bar.Barline('double'))
>>> len(b)
5
)len	_elements_endElementsr   s    rJ   __len__Stream.__len__  s#    8 4>>"S):):%;;;rI   c                    U R                   (       a+  [        R                  " S[        R                  SS9  SU l         [
        R                  " [        R                  [           [        R                  " U 5      5      $ )z
The Stream iterator, used in all for
loops and similar iteration routines. This method returns the
specialized :class:`music21.stream.StreamIterator` class, which
adds necessary Stream-specific features.
z-.flat is deprecated.  Call .flatten() instead   )
stacklevelF)
r{   warningswarnr   Music21DeprecationWarningr   r   r/   StreamIteratorr   r   s    rJ   __iter__Stream.__iter__  s^     ,,MMI&@@%&( 16D-vvh--j9--d35 	5rI   c                "    U R                  5       $ )a\  
The Stream iterator, used in all for
loops and similar iteration routines. This method returns the
specialized :class:`music21.stream.StreamIterator` class, which
adds necessary Stream-specific features.

Generally you don't need this, just iterate over a stream, but it is necessary
to add custom filters to an iterative search before iterating.
)r   r   s    rJ   iterStream.iter  s     }}rI   c                    g rs   rC   r   ks     rJ   __getitem__Stream.__getitem__      rI   c                    g rs   rC   r   s     rJ   r   r     r   rI   c                    g rs   rC   r   s     rJ   r   r     r   rI   c                    g rs   rC   r   s     rJ   r   r         
 	rI   c                    g rs   rC   r   s     rJ   r   r     r   rI   c                    g rs   rC   r   s     rJ   r   r     r   rI   c                   U R                   (       d!  U R                  (       a  U R                  5         [        U[        5      (       aq  SnSUs=::  a  [        U R                  5      :  a  O  OU R                  U   nO U R                  U   nU R                  U5        [        R                  " [        U5      $ [        U[        5      (       ax  U R                  nUR                  b  UR                  S:  d  UR                  b%  UR                  S:  a  [!        U R                  5      n[        R                  " [        X1   5      $ [        U["        5      (       a  [%        U[&        R(                  5      (       a  U R+                  5       R-                  U5      $ [        R                  " ["        [           U5      nU R+                  5       R-                  U5      $ [.        R0                  " U5      (       a6  [3        S U 5       5      (       a  U R+                  5       R-                  U5      $ [        U[4        5      (       a7  U R+                  5       R7                  U5      nSU;   a  UR9                  5       $ U$ [;        S[#        U5       35      e! [         a)    [        SU S3S[        U R                  5       3-   5      ef = f)	aV  
Get a Music21Object from the Stream using a variety of keys or indices.

If an int is given, the Music21Object at the index is returned, as if it were a list
or tuple:

>>> c = note.Note('C')
>>> d = note.Note('D')
>>> e = note.Note('E')
>>> r1 = note.Rest()
>>> f = note.Note('F')
>>> g = note.Note('G')
>>> r2 = note.Rest()
>>> a = note.Note('A')
>>> s = stream.Stream([c, d, e, r1, f, g, r2, a])

>>> s[0]
<music21.note.Note C>
>>> s[-1]
<music21.note.Note A>

Out of range notes raise an IndexError:

>>> s[99]
Traceback (most recent call last):
IndexError: attempting to access index 99 while elements is of size 8

If a slice of indices is given, a list of elements is returned, as if the Stream
were a list or Tuple.

>>> subslice = s[2:5]
>>> subslice
[<music21.note.Note E>, <music21.note.Rest quarter>, <music21.note.Note F>]
>>> len(subslice)
3
>>> s[1].offset
1.0
>>> subslice[1].offset
3.0


If a class is given, then a :class:`~music21.stream.iterator.RecursiveIterator`
of elements matching the requested class is returned, similar
to `Stream().recurse().getElementsByClass()`.

>>> len(s)
8
>>> len(s[note.Rest])
2
>>> len(s[note.Note])
6

>>> for n in s[note.Note]:
...     print(n.name, end=' ')
C D E F G A

Note that this iterator is recursive: it will find elements inside of streams
within this stream:

>>> c_sharp = note.Note('C#')
>>> v = stream.Voice()
>>> v.insert(0, c_sharp)
>>> s.insert(0.5, v)
>>> len(s[note.Note])
7

When using a single Music21 class in this way, your type checker will
be able to infer that the only objects in any loop are in fact `note.Note`
objects, and catch programming errors before running.

Multiple classes can be provided, separated by commas. Any element matching
any of the requested classes will be matched.

>>> len(s[note.Note, note.Rest])
9

>>> for note_or_rest in s[note.Note, note.Rest]:
...     if isinstance(note_or_rest, note.Note):
...         print(note_or_rest.name, end=' ')
...     else:
...         print('Rest', end=' ')
C C# D E Rest F G Rest A

The actual object returned by `s[module.Class]` is a
:class:`~music21.stream.iterator.RecursiveIterator` and has all the functions
available on it:

>>> s[note.Note]
<...>

If no elements of the class are found, no error is raised in version 7:

>>> list(s[layout.StaffLayout])
[]


If the key is a string, it is treated as a `querySelector` as defined in
:meth:`~music21.stream.iterator.getElementsByQuerySelector`, namely that bare strings
are treated as class names, strings beginning with `#` are id-queries, and strings
beginning with `.` are group queries.

We can set some ids and groups for demonstrating.

>>> a.id = 'last_a'
>>> c.groups.append('ghost')
>>> e.groups.append('ghost')

'.ghost', because it begins with `.`, is treated as a class name and
returns a `RecursiveIterator`:

>>> for n in s['.ghost']:
...     print(n.name, end=' ')
C E

A query selector with a `#` returns the single element matching that
element or returns None if there is no match:

>>> s['#last_a']
<music21.note.Note A>

>>> s['#nothing'] is None
True

Any other query raises a TypeError:

>>> s[0.5]
Traceback (most recent call last):
TypeError: Streams can get items by int, slice, class, class iterable, or string query;
   got <class 'float'>

* Changed in v7:
  - out of range indexes now raise an IndexError, not StreamException
  - strings ('music21.note.Note', '#id', '.group') are now treated like a query selector.
  - slices with negative indices now supported
  - Unsupported types now raise TypeError
  - Class and Group searches now return a recursive `StreamIterator` rather than a `Stream`
  - Slice searches now return a list of elements rather than a `Stream`

* Changed in v8:
  - for strings: only fully-qualified names such as "music21.note.Note" or
  partially-qualified names such as "note.Note" are
  supported as class names.  Better to use a literal type or explicitly call
  .recurse().getElementsByClass to get the earlier behavior.  Old behavior
  still works until v9.  This is an attempt to unify __getitem__ behavior in
  StreamIterators and Streams.
  - allowed iterables of qualified class names, e.g. `[note.Note, note.Rest]`

Nr   zattempting to access index  zwhile elements is of size c              3  B   #    U  H  n[        U[        5      v   M     g 7frs   )r   type)rn   
maybe_types     rJ   rp   %Stream.__getitem__.<locals>.<genexpr>  s     )[YZ:*Z*F*FYZs   #zQStreams can get items by int, slice, class, class iterable, or string query; got )ra   rb   r   r   r   r   r   elements
IndexErrorcoreSelfActiveSiter   r   r   slicestartstopr   r   
issubclassr   r   rY   getElementsByClassr   
isIterabler   r   getElementsByQuerySelectorfirst	TypeError)r   r   r=   searchElementsm21TypequerySelectorIterators         rJ   r   r     s.   ~ }}IIKaEA+DNN++q) MM!,E ##E*66*e,,5!! 8<~~N#!9KPQPVPVYZPZ!%dmm!466*n&7884  !T//00||~88;;
 &&j!115||~88AAq!!c)[YZ)[&[&[<<>44Q773$(LLN$M$Ma$P!ax,2244,,7)
 	
Q " $5aS:6s4==7I6JKL s   >J 3Kc                .     U S   $ ! [          a     gf = f)a  
Return the first element of a Stream.  (Added for compatibility with StreamIterator)
Or None if the Stream is empty.

Unlike s.iter().first(), which is a significant performance gain, s.first() is the
same speed as s[0], except for not raising an IndexError.

>>> nC = note.Note('C4')
>>> nD = note.Note('D4')
>>> s = stream.Stream()
>>> s.append([nC, nD])
>>> s.first()
<music21.note.Note C>

>>> empty = stream.Stream()
>>> print(empty.first())
None

* New in v7.
r   Nr   r   s    rJ   r   Stream.first  s"    *	7N 		    
c                .     U S   $ ! [          a     gf = f)a  
Return the last element of a Stream.  (Added for compatibility with StreamIterator)
Or None if the Stream is empty.

s.last() is the same speed as s[-1], except for not raising an IndexError.

>>> nC = note.Note('C4')
>>> nD = note.Note('D4')
>>> s = stream.Stream()
>>> s.append([nC, nD])
>>> s.last()
<music21.note.Note D>

>>> empty = stream.Stream()
>>> print(empty.last())
None

* New in v7.
Nr   r   s    rJ   lastStream.last  s"    (	8O 		r   c                   ^ [        T5      U R                  ;   =(       dE    [        U4S jU R                   5       5      =(       d    [        U4S jU R                   5       5      $ )a  
Returns True if `el` is in the stream (compared with Identity) and False otherwise.

>>> nC = note.Note('C4')
>>> nD = note.Note('D4')
>>> s = stream.Stream()
>>> s.append(nC)
>>> nC in s
True
>>> nD in s
False

Note that we match on actual `id()` equality (`x is y`) and not on
`==` equality.

>>> nC2 = note.Note('C4')
>>> nC == nC2
True
>>> nC2 in s
False

To get the latter, compare on `.elements` which uses Python's
default `__contains__` for tuples.

>>> nC2 in s.elements
True
c              3  4   >#    U  H  oTL d  M	  S v   M     g7fTNrC   rn   sElels     rJ   rp   &Stream.__contains__.<locals>.<genexpr>)  s     C~tt~   	c              3  4   >#    U  H  oTL d  M	  S v   M     g7fr   rC   r   s     rJ   rp   r   *  s     F'82Itt'8r   )r   _offsetDictanyr   r   r   r   s    `rJ   __contains__Stream.__contains__
  sO    < 2$*** GCt~~CCGFt'8'8FF	HrI   c                b   SU R                   ;  d  U R                   S   cx  U R                  (       d!  U R                  (       a  U R                  5         [        R
                  " [        [           U R                  U R                  -   5      U R                   S'   [        U R                   S   5      $ )ag	  
.elements is a Tuple representing the elements contained in the Stream.

Directly getting, setting, and manipulating this Tuple is
reserved for advanced usage. Instead, use the
provided high-level methods.  The elements retrieved here may not
have this stream as an activeSite, therefore they might not be properly ordered.

In other words:  Don't use unless you really know what you're doing.
Treat a Stream like a list!

See how these are equivalent:

>>> m = stream.Measure([note.Note('F4'), note.Note('G4')])
>>> m.elements
(<music21.note.Note F>, <music21.note.Note G>)
>>> tuple(m)
(<music21.note.Note F>, <music21.note.Note G>)

When setting .elements, a list of Music21Objects can be provided, or a complete Stream.
If a complete Stream is provided, elements are extracted
from that Stream. This has the advantage of transferring
offset correctly and getting elements stored at the end.

>>> a = stream.Stream()
>>> a.repeatInsert(note.Note('C'), list(range(10)))
>>> b = stream.Stream()
>>> b.repeatInsert(note.Note('D'), list(range(10)))
>>> b.offset = 6
>>> c = stream.Stream()
>>> c.repeatInsert(note.Note('E'), list(range(10)))
>>> c.offset = 12
>>> b.insert(c)
>>> b.isFlat
False

>>> a.isFlat
True

Assigning from a Stream works well, and is actually much safer than assigning
from `.elements` of the other Stream, since the active sites may have changed
of that stream's elements in the meantime.

>>> a.elements = b
>>> a.isFlat
False

>>> len(a.recurse().notes) == len(b.recurse().notes) == 20
True

There is one good use for .elements as opposed to treating a Stream like a list,
and that is that `in` for Streams compares on object identity, i.e.,
id(a) == id(b) [this is for historical reasons], while since `.elements`
is a tuple.  Recall our measure with the notes F4 and G4 above.

>>> other_g = note.Note('G4')

This new G can't be found in m, because it is not physically in the Measure

>>> other_g in m
False

But it is *equal* to something in the Measure:

>>> other_g in m.elements
True

But again, this could be done simply with:

>>> other_g in tuple(m)
True

One reason to use `.elements` is to iterate quickly without setting
activeSite:

>>> n = note.Note()
>>> m1 = stream.Measure([n])
>>> m2 = stream.Measure([n])
>>> n.activeSite is m2
True
>>> for el in m1.elements:
...     pass
>>> n.activeSite is m2
True
>>> for el in m1:
...     pass
>>> n.activeSite is m1
True
r   )_cachera   rb   r   r   r   r   r   r   r   tupler   s    rJ   r   Stream.elements,  sy    x T[[(DKK
,C,K ==T]]		&'ffT*-=t~~PTPaPa?a&bDKK
#T[[,--rI   c           	        0 U l         [        U[        5      (       a  [        UR                  5      U l        U R                   HN  nU R                  X!R                  U5      SS9  UR                  R                  U 5        U R                  U5        MP     [        UR                  5      U l
        U R                   HN  nU R                  UUR                  USS9SS9  UR                  R                  U 5        U R                  U5        MP     Op[        U5      U l        / U l
        U R                   HI  nU R                  X"R                  SS9  UR                  R                  U 5        U R                  U5        MK     U R                  5         g)a  
Sets this stream's elements to the elements in another stream (just give
the stream, not the stream's .elements), or to a list of elements.

Safe:

newStream.elements = oldStream

Unsafe:

newStream.elements = oldStream.elements

Why?

The activeSites of some elements may have changed between retrieving
and setting (esp. if a lot else has happened in the meantime). Where
are we going to get the new stream's elements' offsets from? why
from their active sites! So don't do this!
T
addElementreturnSpecialN)r   r   rQ   r   r   coreSetElementOffsetelementOffsetr)   addr   r   rM   r   )r   valuero   s      rJ   r   r     sL   * SUeV$$7;EOO7LDN^^))!-@-@-CPT)UD!''* $ ;?u?Q?Q:RD&&))!&+&9&9!4&9&P15 * 7 D!''* ' "%[DN "D^^))!XX$)GD!''* $ 	  "rI   c                   U R                   U   nU R                  [        U5      	 UR                  R	                  U 5        SUl        X R                   U'   U R                  X"R                  SS9  U R                  U5        UR                  R                  U 5        [        U[        5      (       a  U R                  SS9  SU l        gU R                  5         g)a  
Insert an item at a currently filled index position,
as represented in the elements list.

>>> a = stream.Stream()
>>> a.repeatInsert(note.Note('C'), list(range(10)))
>>> b = stream.Stream()
>>> b.repeatInsert(note.Note('C'), list(range(10)))
>>> b.offset = 6
>>> c = stream.Stream()
>>> c.repeatInsert(note.Note('C'), list(range(10)))
>>> c.offset = 12
>>> b.insert(c)
>>> a.isFlat
True
>>> a[3] = b
>>> a.isFlat
False
NTr   FupdateIsFlat)r   r   r   r)   remove
activeSiter  rM   r   r  r   rQ   r   rc   )r   r   r  oldValues       rJ   __setitem__Stream.__setitem__  s    * >>!$R\*d#" "q!!%$!G& 	eV$$$$%$8DK $$&rI   c                >    U R                   U	 U R                  5         g)z
Delete element at an index position. Index positions are based
on positions in self.elements.

>>> a = stream.Stream()
>>> a.repeatInsert(note.Note('C'), list(range(10)))
>>> del a[0]
>>> len(a)
9
N)r   r   r   s     rJ   __delitem__Stream.__delitem__  s     NN1  "rI   c                   Ub  [        U[        5      (       d  [        S5      eU R                  SS9nU R                   H$  nUR                  U R                  U5      U5        M&     UR                   H$  nUR                  UR                  U5      U5        M&     U R                   H  nUR                  U5        M     UR                   H  nUR                  U5        M     U$ )a  
Add, or concatenate, two Streams.

Presently, this does not manipulate the offsets of the incoming elements
to actually be at the end of the Stream. This may be a problem that
makes this method not so useful?

>>> a = stream.Part()
>>> a.repeatInsert(note.Note('C'), [0, 1])
>>> b = stream.Stream()
>>> b.repeatInsert(note.Note('G'), [0, 1, 2])
>>> c = a + b
>>> c.pitches  # autoSort is True, thus a sorted version results
[<music21.pitch.Pitch C>,
 <music21.pitch.Pitch G>,
 <music21.pitch.Pitch C>,
 <music21.pitch.Pitch G>,
 <music21.pitch.Pitch G>]
>>> len(c.notes)
5

The autoSort of the first stream becomes the autoSort of the
destination.  The class of the first becomes the class of the destination.

>>> a.autoSort = False
>>> d = a + b
>>> [str(p) for p in d.pitches]
['C', 'C', 'G', 'G', 'G']
>>> d.__class__.__name__
'Part'

Works with Streams with Store at end, which does put both at the end.

>>> a.autoSort = True
>>> a.storeAtEnd(bar.Barline('final'))
>>> b.storeAtEnd(clef.TrebleClef())
>>> f = a + b
>>> f.show('text')
{0.0} <music21.note.Note C>
{0.0} <music21.note.Note G>
{1.0} <music21.note.Note C>
{1.0} <music21.note.Note G>
{2.0} <music21.note.Note G>
{3.0} <music21.bar.Barline type=final>
{3.0} <music21.clef.TrebleClef>
z-cannot concatenate a Stream with a non-Stream__add__derivationMethod)	r   rQ   r   
cloneEmptyr   rV   r  r   rW   )r   r   sro   s       rJ   r  Stream.__add__  s    ^ =
5& 9 9KLLOOYO7 AHHT''*A.  AHHU((+Q/ ! ""ALLO ###ALLO $ rI   c                L    U R                   (       a  gU R                  (       a  gg)a  
As a container class, Streams return True if they are non-empty
and False if they are empty:

>>> def testBool(s):
...    if s:
...        return True
...    else:
...        return False

>>> s = stream.Stream()
>>> testBool(s)
False
>>> s.append(note.Note())
>>> testBool(s)
True
>>> s.append(note.Note())
>>> testBool(s)
True

>>> s = stream.Stream()
>>> s.storeAtEnd(bar.Barline('final'))
>>> testBool(s)
True
TFr   r   r   s    rJ   __bool__Stream.__bool__9  s    4 >>rI   c                ~    U R                  [        R                  5      R                  S5      nUR	                  5       $ )a  
Finds or sets a :class:`~music21.clef.Clef` at offset 0.0 in the Stream
(generally a Measure):

>>> m = stream.Measure()
>>> m.number = 10
>>> m.clef = clef.TrebleClef()
>>> thisTrebleClef = m.clef
>>> thisTrebleClef.sign
'G'
>>> thisTrebleClef.getOffsetBySite(m)
0.0

Setting the clef for the measure a second time removes the previous clef
from the measure and replaces it with the new one:

>>> m.clef = clef.BassClef()
>>> m.clef.sign
'F'

And the TrebleClef is no longer in the measure:

>>> thisTrebleClef.getOffsetBySite(m)
Traceback (most recent call last):
music21.sites.SitesException: an entry for this object <music21.clef.TrebleClef> is not
      stored in stream <music21.stream.Measure 10 offset=0.0>

The `.clef` appears in a `.show()` or other call
just like any other element

>>> m.append(note.Note('D#', type='whole'))
>>> m.show('text')
{0.0} <music21.clef.BassClef>
{0.0} <music21.note.Note D#>
r   )r   r   ClefgetElementsByOffsetr   )r   clefLists     rJ   r   Stream.clefZ  s1    J **4995II!L~~rI   c                    U R                   nUb   U R                  U R                  U5      5      nUc  g U R                  SU5        g )Nrl   )r   popindexrV   )r   clefObjoldClefjunks       rJ   r   r"    sB     ))88DJJw/0D? C!rI   c                ~    U R                  [        R                  5      R                  S5      nUR	                  5       $ )a  
Gets or sets the timeSignature at offset 0.0 of the Stream (generally a Measure)

>>> m1 = stream.Measure(number=1)
>>> m1.timeSignature = meter.TimeSignature('2/4')
>>> m1.timeSignature.numerator, m1.timeSignature.denominator
(2, 4)
>>> m1.show('text')
{0.0} <music21.meter.TimeSignature 2/4>

Setting timeSignature to None removes any TimeSignature at offset 0.0:

>>> m1.timeSignature = None
>>> m1.elements
()


Only the time signature at offset 0 is found:

>>> m2 = stream.Measure(number=2)
>>> m2.insert(0.0, meter.TimeSignature('5/4'))
>>> m2.insert(2.0, meter.TimeSignature('7/4'))
>>> ts = m2.timeSignature
>>> ts.numerator, ts.denominator
(5, 4)

>>> m2.timeSignature = meter.TimeSignature('2/8')
>>> m2.timeSignature
<music21.meter.TimeSignature 2/8>

After setting a new `.timeSignature`, the old one is no longer in the Stream:

>>> ts in m2
False

This property is not recursive, so a Part will not have the time signature of
the measure within it:

>>> p = stream.Part()
>>> p.append(m2)
>>> p.timeSignature is None
True
r   )r   r$   TimeSignaturer   r   )r   tsLists     rJ   timeSignatureStream.timeSignature  s5    \ (()<)<=QQRST ||~rI   c                    U R                   nUb   U R                  U R                  U5      5      nUc  g U R                  SU5        g Nr   )r,  r$  r%  rV   )r   tsObjoldTimeSignaturer(  s       rJ   r,  r-    sD    --'88DJJ'789D= AurI   c                     [        U R                  5       R                  [        R                  5      R                  S5      5      $ ! [         a     gf = f)a  
Find or set a Key or KeySignature at offset 0.0 of a stream.

>>> a = stream.Measure()
>>> a.keySignature = key.KeySignature(-2)
>>> ks = a.keySignature
>>> ks.sharps
-2
>>> a.show('text')
{0.0} <music21.key.KeySignature of 2 flats>

A key.Key object can be used instead of key.KeySignature,
since the former derives from the latter.

>>> a.keySignature = key.Key('E', 'major')
>>> for k in a:
...     print(k.offset, repr(k))
0.0 <music21.key.Key of E major>

Notice that setting a new key signature replaces any previous ones:

>>> len(a.getElementsByClass(key.KeySignature))
1

`.keySignature` can be set to None:

>>> a.keySignature = None
>>> a.keySignature is None
True
r   N)nextr   r   r"   KeySignaturer   StopIterationr   s    rJ   keySignatureStream.keySignature  sH    @			66s7G7GH\\]^_`` 		s   AA 
AAc                    U R                   nUb   U R                  U R                  U5      5      nUc  gU R                  SU5        g)z_
>>> a = stream.Measure()
>>> a.keySignature = key.KeySignature(6)
>>> a.keySignature.sharps
6
Nr   )r6  r$  r%  rV   )r   keyObjoldKeyr(  s       rJ   r6  r7    sD     ""88DJJv./D> AvrI   c                    SSK Jn  XR                     nU H5  nUR                  U 5      S:  a    gUR                  c  M)  UR                  s  $    g)a  
Returns the number of staffLines for the Stream, as defined by
the first StaffLayout object found at offset 0 that defines staffLines

>>> m = stream.Measure()
>>> m.staffLines
5
>>> m.staffLines = 4
>>> m.staffLines
4
>>> m.show('text')
{0.0} <music21.layout.StaffLayout distance None, staffNumber None,
          staffSize None, staffLines 4>

>>> staffLayout = m.getElementsByClass(layout.StaffLayout).first()
>>> staffLayout.staffLines = 1
>>> m.staffLines
1

>>> p = stream.Part()
>>> p.insert(0, m)
>>> p.staffLines
1

>>> p2 = stream.Part()
>>> m0 = stream.Measure()
>>> m0.insert(0, note.Note(type='whole'))
>>> p2.append(m0)
>>> p2.append(m)
>>> p2.staffLines
5

OMIT_FROM_DOCS

Check that staffLayout is altered by staffLayout setter:

>>> m.staffLines = 2
>>> staffLayout.staffLines
2
r   layout   )music21r=  StaffLayoutgetOffsetInHierarchy
staffLines)r   r=  staffLayoutssls       rJ   rB  Stream.staffLines  sX    T 	#../ B&&t,q0  }}(}}$	 
 rI   c                    SSK Jn  U R                  5       R                  S5      R	                  UR
                  5      nUR                  5       nU(       d"  UR                  US9nU R                  SU5        g Xl        g )Nr   r<  rl   )rB  )	r?  r=  rY   r   r   r@  r   rV   rB  )r   newStaffLinesr=  rC  firstLayoutrD  s         rJ   rB  rE  =  sj    "WY  % 2 23	 	 #((*%+%7%7=%7%QBKKR %2"rI   c                    SU l         g)z
Remove all elements in a stream.

>>> m = stream.Measure(number=3)
>>> m.append(note.Note('C'))
>>> m.storeAtEnd(bar.Barline('final'))
>>> len(m)
2
>>> m.clear()
>>> len(m)
0

Does not remove any other attributes

>>> m.number
3
rC   N)r   r   s    rJ   clearStream.clearM  s    $ rI   c                    U R                  5       nX"R                  l        XR                  l        Ub  XR                  l        UR                  U 5        U$ )aq  
Create a Stream that is identical to this one except that the elements are empty
and set derivation.

>>> p = stream.Part()
>>> p.autoSort = False
>>> p.id = 'hi'
>>> p.insert(0, note.Note())
>>> q = p.cloneEmpty(derivationMethod='demo')
>>> q.autoSort
False
>>> q
<music21.stream.Part hi>
>>> q.derivation.origin is p
True
>>> q.derivation.method
'demo'
>>> len(q)
0
)r   r   clientoriginmethodmergeAttributes)r   r  	returnObjs      rJ   r  Stream.cloneEmptya  sN    * !% 0	&/#&*#'*:  '!!$'rI   c           	        > [         TU ]  U5        [        U[        5      (       d  gS H*  n[	        X5      (       d  M  [        X[        X5      5        M,     g)a  
Merge relevant attributes from the Other stream into this one.

>>> s = stream.Stream()
>>> s.append(note.Note())
>>> s.autoSort = False
>>> s.id = 'hi'
>>> s2 = stream.Stream()
>>> s2.mergeAttributes(s)
>>> s2.autoSort
False
>>> s2
<music21.stream.Stream hi>
>>> len(s2)
0
N)rb   ra   rd   re   r~   r   )ry   rP  r   rQ   hasattrsetattrgetattrr   r   attrr   s      rJ   rP  Stream.mergeAttributes~  sJ    " 	&%((RDu##GE$89RrI   zv9.3v11z5Use `el in stream` instead of `stream.hasElement(el)`c                
    X;   $ )z
DEPRECATED: just use `el in stream` instead of `stream.hasElement(el)`

Return True if an element, provided as an argument, is contained in
this Stream.
rC   )r   objs     rJ   
hasElementStream.hasElement  s     {rI   c                L    U R                    H  nXR                  ;   d  M    g   g)ag  
Given a single class name as string,
return True or False if an element with the
specified class is found.

Only a single class name can be given.

>>> s = stream.Stream()
>>> s.append(meter.TimeSignature('5/8'))
>>> s.append(note.Note('d-2'))
>>> s.insert(dynamics.Dynamic('fff'))
>>> s.hasElementOfClass(meter.TimeSignature)
True
>>> s.hasElementOfClass('Measure')
False

To be deprecated in v10 -- to be removed in v11, use:

>>> bool(s.getElementsByClass(meter.TimeSignature))
True
>>> bool(s.getElementsByClass(stream.Measure))
False

forceFlat does nothing, while getElementsByClass can be done on recurse()
TF)r   ru   )r   	className	forceFlatro   s       rJ   hasElementOfClassStream.hasElementOfClass  s$    6 AJJ&  rI   c                   Ub  [        U5      nOSnUR                   Hl  nUbE  UR                  UR                  5      (       a#  U R	                  UR                  U5      U5        MI  MK  U R	                  UR                  U5      U5        Mn     UR                   HL  nUb5  UR                  UR                  5      (       a  U R                  U5        M9  M;  U R                  U5        MN     U R                  5         g)aO  
Given another Stream, store references of each element
in the other Stream in this Stream. This does not make
copies of any elements, but simply stores all of them in this Stream.

Optionally, provide a list of classes to include with the `classFilter` list.

This method provides functionality like a shallow copy,
but manages locations properly, only copies elements,
and permits filtering by class type.

>>> s1 = stream.Stream()
>>> s2 = stream.Stream()
>>> n1 = note.Note('f#')
>>> n2 = note.Note('g')
>>> s1.append(n1)
>>> s1.append(n2)
>>> s2.mergeElements(s1)
>>> len(s2)
2
>>> s1[0] is s2[0]
True
>>> s1[1] is s2[1]
True

>>> viola = instrument.Viola()
>>> trumpet = instrument.Trumpet()
>>> s1.insert(0, viola)
>>> s1.insert(0, trumpet)
>>> s2.mergeElements(s1, classFilterList=('BrassInstrument',))
>>> len(s2)
3
>>> viola in s2
False
N)	setr   intersectionru   r   r  r   coreStoreAtEndr   )r   r   classFilterListclassFilterSetro   s        rJ   mergeElementsStream.mergeElements  s    H & 1N!NA*!..qzz::OOE$7$7$:A> ;  3 3A 6: !  ##A*!..qzz::''* ; ##A& $ 	  "rI   c                   U R                   (       d!  U R                  (       a  U R                  5         SU R                  ;   a,  U R                  S   b   U R                  S   [	        U5         $ 0 U R                  S'   [	        U5      nSnU R                   H!  nXAL a  X0R                  S   U'   Us  $ US-  nM#     U R                   H!  nXAL a  X0R                  S   U'   Us  $ US-  nM#     [        SU S35      e! [
         a     Nf = f)a  
Return the first matched index for
the specified object.

Raises a StreamException if the object cannot be found.

>>> s = stream.Stream()
>>> n1 = note.Note('G')
>>> n2 = note.Note('A')

>>> s.insert(0.0, n1)
>>> s.insert(5.0, n2)
>>> len(s)
2
>>> s.index(n1)
0
>>> s.index(n2)
1

Note that this is done via Object identity, so another identical
G won't be found in the stream.

>>> n3 = note.Note('G')
>>> s.index(n3)
Traceback (most recent call last):
music21.exceptions21.StreamException: cannot find object (<music21.note.Note G>) in Stream

To find the index of something equal to the object in the stream, cast the
stream to a tuple or list first:

>>> tuple(s).index(n3)
0

r%  r      zcannot find object (z) in Stream)	ra   rb   r   r   r   KeyErrorr   r   StreamException)r   r   objIdcountro   s        rJ   r%  Stream.index  s   F }}IIKdkk!dkk'&:&F{{7+BrF33 $&DKK 2Aw.3G$U+QJE	  
 ""Aw.3G$U+QJE	 #
  4RDDEE'  s   C< <
D	D	)shiftOffsetsrY   c          	        U R                   SL a  [        S5      eUSL a  USL a  [        S5      e[        R                  " U5      (       d:  [
        R                  (       a!  [        U[        R                  5      (       d   eU/nO[        U[        5      (       ac  [        U5      S:  aT  [
        R                  (       a!  [        U[        R                  5      (       a   e [        [        XR                  S95      nOA[
        R                  (       a!  [        U[        R                  5      (       a   e[        U5      nSn[#        U5       GH  u  pg U R%                  U5      nSn[        U R.                  5      nSnX:  a  U R.                  R1                  U5      nOU R2                  R1                  X-
  5      nSnUb^  USL a  U R                  U5      n U R4                  [7        U5      	 U R;                  SS9  UR                  R-                  U 5        S
Ul        USL d  M  USL d  M  UR>                  R@                  nX-   nUS-   [        U5      :  a  U R                  XFS-      5      nOU R>                  R@                  nX_-  nUS:w  d  GMF  U RC                  UUSSSS9 H)  nU R                  U5      nU RE                  UUU-
  5        M+     GM     U R;                  SS9  g
! [        R                    a    [        U5      n GNf = f! [         a  n	[        U[        R                  5      (       d  ['        U S[)        U5       35      U	eUSL aZ  U R+                  SS	9 HG  n
 U
R%                  U5      nU
R-                  U5          O#! [        [        R                   4 a     ME  f = f    S
n	A	GMj  S
n	A	ff = f! [8         a     GNf = f)at  
Remove an object from this Stream. Additionally, this Stream is
removed from the object's sites in :class:`~music21.sites.Sites`.

If a list of objects is passed, they will all be removed.
If shiftOffsets is True, then offsets will be
corrected after object removal. It is more efficient to pass
a list of objects than to call remove on
each object individually if shiftOffsets is True.

>>> import copy
>>> s = stream.Stream()
>>> n1 = note.Note('g')
>>> n2 = note.Note('g#')

Copies of an object are not the same as the object

>>> n3 = copy.deepcopy(n2)
>>> s.insert(10, n1)
>>> s.insert(5, n2)
>>> s.remove(n1)
>>> len(s)
1
>>> s.insert(20, n3)
>>> s.remove(n3)
>>> [e for e in s] == [n2]
True

No error is raised if the target is not found.

>>> s.remove(n3)

>>> s2 = stream.Stream()
>>> c = clef.TrebleClef()
>>> n1, n2, n3, n4 = note.Note('a'), note.Note('b'), note.Note('c'), note.Note('d')
>>> n5, n6, n7, n8 = note.Note('e'), note.Note('f'), note.Note('g'), note.Note('a')
>>> s2.insert(0.0, c)
>>> s2.append([n1, n2, n3, n4, n5, n6, n7, n8])
>>> s2.remove(n1, shiftOffsets=True)
>>> s2.show('text')
{0.0} <music21.clef.TrebleClef>
{0.0} <music21.note.Note B>
{1.0} <music21.note.Note C>
{2.0} <music21.note.Note D>
{3.0} <music21.note.Note E>
{4.0} <music21.note.Note F>
{5.0} <music21.note.Note G>
{6.0} <music21.note.Note A>

>>> s2.remove([n3, n6, n4], shiftOffsets=True)
>>> s2.show('text')
{0.0} <music21.clef.TrebleClef>
{0.0} <music21.note.Note B>
{1.0} <music21.note.Note E>
{2.0} <music21.note.Note G>
{3.0} <music21.note.Note A>

With the recurse=True parameter, we can remove elements deeply nested.
However, shiftOffsets
does not work with recurse=True yet.

>>> p1 = stream.Part()
>>> m1 = stream.Measure(number=1)
>>> c = clef.BassClef()
>>> m1.insert(0, c)
>>> m1.append(note.Note(type='whole'))
>>> p1.append(m1)
>>> m2 = stream.Measure(number=2)
>>> n2 = note.Note('D', type='half')
>>> m2.append(n2)
>>> n3 = note.Note(type='half')
>>> m2.append(n3)
>>> p1.append(m2)
>>> p1.show('text')
{0.0} <music21.stream.Measure 1 offset=0.0>
    {0.0} <music21.clef.BassClef>
    {0.0} <music21.note.Note C>
{4.0} <music21.stream.Measure 2 offset=4.0>
    {0.0} <music21.note.Note D>
    {2.0} <music21.note.Note C>

Without recurse=True:

>>> p1.remove(n2)
>>> p1.show('text')
{0.0} <music21.stream.Measure 1 offset=0.0>
    {0.0} <music21.clef.BassClef>
    {0.0} <music21.note.Note C>
{4.0} <music21.stream.Measure 2 offset=4.0>
    {0.0} <music21.note.Note D>
    {2.0} <music21.note.Note C>

With recurse=True:

>>> p1.remove(n2, recurse=True)
>>> p1.show('text')
{0.0} <music21.stream.Measure 1 offset=0.0>
    {0.0} <music21.clef.BassClef>
    {0.0} <music21.note.Note C>
{4.0} <music21.stream.Measure 2 offset=4.0>
    {2.0} <music21.note.Note C>

With recurse=True and a list to remove:

>>> p1.remove([c, n3], recurse=True)
>>> p1.show('text')
{0.0} <music21.stream.Measure 1 offset=0.0>
    {0.0} <music21.note.Note C>
{4.0} <music21.stream.Measure 2 offset=4.0>
<BLANKLINE>


Can also remove elements stored at end:

>>> streamWithBarline = stream.Stream(note.Note())
>>> barline = bar.Barline('final')
>>> streamWithBarline.storeAtEnd(barline)
>>> barline in streamWithBarline
True
>>> streamWithBarline.remove(barline)
>>> barline in streamWithBarline
False

* Changed in v5.3: firstMatchOnly removed -- impossible to have element
  in stream twice.  recurse and shiftOffsets changed to keywordOnly arguments
Fz&Cannot remove from an immutable streamTzDCannot do both shiftOffsets and recurse search at the same time, yetrm  r!   rl   z is not a Music21Object; got streamsOnlyNclearIsSortedincludeEndBoundarymustFinishInSpanmustBeginInSpan)#r   ImmutableStreamExceptionro  r   
isListLiker   TYPE_CHECKINGr   r   r   r	   r   r   sortedr  r)   SitesException	enumerater%  r   r   rY   r  r   r$  r   r   r   rn  r   r  r   quarterLengthr   r  )r   targetOrListrs  rY   
targetListshiftDuritargetindexInStreamser  matchedEndElementbaseElementCountmatchOffsetr=   matchDurationshiftedRegionStartshiftedRegionEndro   r  s                       rJ   r  Stream.removeJ  s   J ==E!*+STT
 4GtO!VX X   ..!,0B0BCCCC&Jh//C4E4I%lD4F4FGGGG0!&;M;M"NO

 %lD4F4FGGGGl+J":.IA $

6 2" !&"4>>2K/**=9))--m.NO$(! 4'"&"4"4U";K((E3 ((u(=""4(#' t#(9U(B % < <%0%@"ES_,'+'9'9*U:K'L$'+}}'B'B$)s?!556H6FINGLFJ	 6 L )-(:(:1(=11!]X5MNLi /| 	  u 5Q '' 0!,/
0 # !&$*<*<==#vh.KDQWL>$Z[accd?!\\d\;%,-GGFOMHHV,! /1E1EF %$% < @   sa    K& LN=&L	L	
N:AN5'"N	N5N)	%N5(N)	)N55N:=
O
Oc                   [        U R                  5      nUcG  U R                  (       a  U R                  R                  5       nOYU R                  R                  5       nO>X:  a  U R                  R                  U5      nOU R                  R                  X-
  5      nU R	                  SS9   U R
                  [        U5      	 UR                  R                  U 5        SUl
        U$ ! [         a     N0f = f)a  
Return and remove the object found at the
user-specified index value. Index values are
those found in `elements` and are not necessary offset order.

>>> a = stream.Stream()
>>> for i in range(12):  # notes C, C#, etc. to B
...     a.append(note.Note(i))
>>> a.pop(0)
<music21.note.Note C>
>>> len(a)
11

If nothing is given, then it pops the last thing from the stream.

>>> a.pop()
<music21.note.Note B>
>>> len(a)
10
NFrw  )r   r   r   r$  r   r   r   rn  r)   r  r  )r   r%  eLenposts       rJ   r$  
Stream.pop-  s    * 4>>"=  ((,,.~~))+\>>%%e,D$$((6D  u 5	  D* 	

$  		s   .C( (
C54C5c                   / / S.nU H(  nUR                   nX$S      R                  US   5        M*     U Hn  n[        X5      nX%   n[        U5       HM  nUR	                  U5      n	 U R
                  [        U	5      	 U	R                  R                  U 5        SU	l
        MO     Mp     U R                  SS9  g! [         a     NFf = f)z6
helper method to remove different kinds of elements.
r  iterSectionsectionIndexNFrw  )activeInformationrU   rV  reversedr$  r   r   rn  r)   r  r  r   )
r   streamIteratorpopDict	unused_elaisectionsectionListpopListpopIndexremoveElements
             rJ   _removeIterationStream._removeIterationY  s     !##% (I11B}%&--b.@A ( G!$0K&G$W- + 9((M):;
 ##**40+/( .   	  u 5   s   (B88
CCc                d    U R                  5       R                  U5      nU R                  U5        g)a  
Remove all elements from the Stream
based on one or more classes given
in a list.

>>> s = stream.Stream()
>>> s.append(meter.TimeSignature('4/4'))
>>> s.repeatAppend(note.Note('C'), 8)
>>> len(s)
9
>>> s.removeByClass('GeneralNote')
>>> len(s)
1
>>> len(s.notes)
0

Test that removing from end elements works.

>>> s = stream.Measure()
>>> s.append(meter.TimeSignature('4/4'))
>>> s.repeatAppend(note.Note('C'), 4)
>>> s.rightBarline = bar.Barline('final')
>>> len(s)
6
>>> s.removeByClass('Barline')
>>> len(s)
5
N)r   r   r  r   rh  elFilters      rJ   removeByClassStream.removeByClassx  s(    : 99;11/Bh'rI   c                b    U R                  5       R                  U5      nU R                  U5      $ )a  
Remove all elements not of the specified
class or subclass in the Stream in place.

>>> s = stream.Stream()
>>> s.append(meter.TimeSignature('4/4'))
>>> s.repeatAppend(note.Note('C'), 8)
>>> len(s)
9
>>> s.removeByNotOfClass(meter.TimeSignature)
>>> len(s)
1
>>> len(s.notes)
0
)r   getElementsNotOfClassr  r  s      rJ   removeByNotOfClassStream.removeByNotOfClass  s+      99;44_E$$X..rI   ignoreAttributesc                 > 1 SknUc  UnOX#-  n[         T	U ]  XS9n0 nXTl        / Ul        / Ul        XDR
                  l        U R                   H]  nU R                  U5      nUR                  (       d  [        R                  " Xa5      nOUR                  U5      nUR                  XxSS9  M_     U R                   H(  nUR                  [        R                  " Xa5      5        M*     UR                  5         U$ )N>   r   r   r   r  T
ignoreSort)ry   _deepcopySubclassabler   r   r   r.   rM  r  rt   copydeepcopyr   rg  r   )
r   memor  defaultIgnoreSetnewnewOffsetDictro   rM   
newElementr   s
            rJ   r  Stream._deepcopySubclassable  s    
 #//B  '77` PR' #& A ''*F::!]]13

 44T:

 NN6$N?3  8 ""A t}}Q56 # 	!
rI   c                l    U R                  U5      nUR                  (       a  U R                  U5        U$ )z*
Deepcopy the stream from copy.deepcopy()
)r  r    _replaceSpannerBundleForDeepcopy)r   r  r  s      rJ   __deepcopy__Stream.__deepcopy__  s.     ((.==11#6
rI   c                l   UR                   nU(       d  g UR                  SS9 H  nSUR                  ;   a  M  UR                  R                  S:w  a  M1  UR                  R
                  nUc  ML  UR                  R                  5       (       d  Mm  UR                  XC5        UR                  SS9  M     g )NFincludeSelfzmusic21.spanner.Spannerr  )excludeStorageStreams)
spannerBundlerY   ru   r   rO  rN  r)   hasSpannerSitereplaceSpannedElementpurgeOrphans)r   r  newSpannerBundlero   rN  s        rJ   r  'Stream._replaceSpannerBundleForDeepcopy  s     ,, /A(AJJ6||""n4\\((F~||**,, !66vA
 U;) 0rI   c                F    U R                  UU5        U R                  SS9  g)aM  
Sets the Offset for an element that is already in a given stream.

Set up a note in two different streams at two different offsets:

>>> n = note.Note('B-4')
>>> s = stream.Stream(id='Stream1')
>>> s.insert(10, n)
>>> n.offset
10.0
>>> n.activeSite.id
'Stream1'

>>> s2 = stream.Stream(id='Stream2')
>>> s2.insert(30, n)
>>> n.activeSite.id
'Stream2'

Now change the note's offset in Stream1:

>>> s.setElementOffset(n, 20.0)

This call has the effect of switching the `activeSite` of `n` to `s`.

>>> n.activeSite.id
'Stream1'
>>> n.offset
20.0
>>> n.getOffsetBySite(s)
20.0

If the element is not in the Stream, raises a StreamException:

>>> n2 = note.Note('D')
>>> s.setElementOffset(n2, 30.0)
Traceback (most recent call last):
music21.exceptions21.StreamException: Cannot set the offset for element
    <music21.note.Note D>, not in Stream <music21.stream.Stream Stream1>.

* Changed in v5.5: also sets .activeSite for the element
* Changed in v6.7: also runs coreElementsChanged()
* Changed in v7: addElement is removed;
  see :meth:`~music21.stream.core.StreamCoreMixin.coreSetElementOffset`
Fr	  N)r  r   )r   rL   rM   s      rJ   setElementOffsetStream.setElementOffset  s+    b 	!!'"(	$ 	  e 4rI   c                    U R                   [        U5         S   n[        U[        5      (       a  USL a  U[        ;   a   [        X5      $ U$ ! [         aS    U R                    H  nU R                   U   u  p5XL d  M     Mf     [        R                  " S[        U5      S SU  35      ef = f! [         a    [        R                  " SSU 3-   5      ef = f)a>  
Return the offset as an opFrac (float or Fraction) from the offsetMap.
highly optimized for speed.

>>> m = stream.Measure(number=1)
>>> m.append(note.Note('C'))
>>> d = note.Note('D')
>>> m.append(d)
>>> m.elementOffset(d)
1.0

If returnSpecial is True then returns like OffsetSpecial.AT_END are allowed.

>>> b = bar.Barline()
>>> m.storeAtEnd(b)
>>> m.elementOffset(b)
2.0
>>> m.elementOffset(b, returnSpecial=True)
<OffsetSpecial.AT_END>

Unlike element.getOffsetBySite(self), this method will NOT follow derivation chains
and in fact will raise a sites.SitesException

>>> import copy
>>> p = stream.Part(id='sPart')
>>> p.insert(20, m)
>>> m.getOffsetBySite(p)
20.0
>>> p.elementOffset(m)
20.0

>>> mCopy = copy.deepcopy(m)
>>> mCopy.number = 10
>>> mCopy.derivation
<Derivation of <music21.stream.Measure 10 offset=0.0> from
    <music21.stream.Measure 1 offset=20.0> via '__deepcopy__'>
>>> mCopy.getOffsetBySite(p)
20.0
>>> p.elementOffset(mCopy)
Traceback (most recent call last):
music21.sites.SitesException: an entry for this object 0x... is not stored in
    stream <music21.stream.Part sPart>

Performance note: because it will not follow derivation chains, and does
not need to unwrap a weakref, this method
should usually be about 3x faster than element.getOffsetBySite(self) --
currently 600ns instead of 1.5 microseconds.
r   zan entry for this object 0xxz is not stored in stream Fz3attempted to retrieve a bound offset with a string z!attribute that is not supported: )
r   r   rn  r   r  r   r   r   rV  r   )r   rL   r  o	idElementreturnedElements         rJ   r  Stream.elementOffsetR  s    b	b  G-a0A a-5"8Q-=O?t'' H)  		b!--	%)%5%5i%@"-  . ))1"W+a@YZ^Y_`b b		b " ?))I9!=>? ??s#   A 
B/ ,B,?B,)B,/'Cr  setActiveSitec               V   Ub  UnUnO{UcX  [        U[        5      (       aC  SnU[        U5      :  a1  X   nXS-      nU R                  XVUS9  US-  nU[        U5      :  a  M1  gUn UR                  nUR                  U5      n [        U5      nUn	U R                  U	5        U R                  UU	UUS9n
SnU	R                  (       a  SnU R                  US9  USL a  Xl        gg! [         a    [        SU< S3S-   5      ef = f! [        [        4 a    [        S	U< S
35      ef = f)aJ  
Inserts an item(s) at the given offset(s).

If `ignoreSort` is True then the inserting does not
change whether the Stream is sorted or not (much faster if you're
going to be inserting dozens
of items that don't change the sort status)

The `setActiveSite` parameter should nearly always be True; only for
advanced Stream manipulation would you not change
the activeSite after inserting an element.

Has three forms: in the two argument form, inserts an
element at the given offset:

>>> st1 = stream.Stream()
>>> st1.insert(32, note.Note('B-'))
>>> st1.highestOffset
32.0

In the single argument form with an object, inserts the element at its
stored offset:

>>> n1 = note.Note('C#')
>>> n1.offset = 30.0
>>> st1 = stream.Stream()
>>> st1.insert(n1)
>>> st2 = stream.Stream()
>>> st2.insert(40.0, n1)
>>> n1.getOffsetBySite(st1)
30.0

In single argument form with a list, the list should
contain pairs that alternate
offsets and items; the method then, obviously, inserts the items
at the specified offsets.

Note: This functionality will be deprecated in v9 and replaced
with a list of tuples of [(offset, element), (offset, element)]
and removed in v10

>>> n1 = note.Note('G')
>>> n2 = note.Note('F#')
>>> st3 = stream.Stream()
>>> st3.insert([1.0, n1, 2.0, n2])
>>> n1.getOffsetBySite(st3)
1.0
>>> n2.getOffsetBySite(st3)
2.0
>>> len(st3)
2

Raises an error if offset is not a number:

>>> stream.Stream().insert('l', note.Note('B'))
Traceback (most recent call last):
music21.exceptions21.StreamException: Offset 'l' must be a number.

Also raises an error if the object is not a music21 object (or a list of them)

>>> stream.Stream().insert(3.3, 'hello')
Traceback (most recent call last):
music21.exceptions21.StreamException: The object you tried to add
    to the Stream, 'hello', is not a Music21Object.
    Use an ElementWrapper object if this is what you intend.

The error message is slightly different in the one-element form:

>>> stream.Stream().insert('hello')
Traceback (most recent call last):
music21.exceptions21.StreamException: Cannot insert item 'hello' to
    stream -- is it a music21 object?
Nr   rm  r     zCannot insert item z to stream z-- is it a music21 object?zOffset z must be a number.r  FTr	  )r   r   r   rV   r  getOffsetBySiter   ro  float
ValueErrorr   r   r   rt   r   ra   )r   offsetOrItemOrList
itemOrNoner  r  rM   itemr  r  rL   storeSortedr
  s               rJ   rV   Stream.insert  s   h !'FDJ/A4$H$HAc,--+.)a%0FZ@Q c,--  &DF!__
--j9	J6]F  	&&w/ oof&-1;4A & C L  l ;'M 1 " F%(;D8;&O(D'E F FF I& 	J!GF:5G"HII	Js   'C& D &D!D(c           
        [        U R                  UXR                  -   SSSS9R                  5      nSn[	        U5      S:X  Ga  / n/ nUS   nUn[        U[        R                  5      (       ao  [        U[        R                  5      (       a  UR                  /nU/nO?[        U[        R                  5      (       a   [        UR                  5      n[        U5      n[        U[        R                  5      (       a  [        U[        R                  5      (       a  UR                  UR                  /nX/nOb[        U[        R                  5      (       a3  UR                  /[        UR                  5      -   nU/[        U5      -   nOUR                  /nU/n[        U[        R                  5      (       a  [        U[        R                  5      (       a3  [        UR                  5      UR                  /-   n[        U5      U/-   nO[        U[        R                  5      (       aC  [        UR                  5      [        UR                  5      -   n[        U5      [        U5      -   nO [        UR                  5      n[        U5      n[	        U5      S:  d  USL a  [        R                  " U5      n	O>[	        U5      S:X  a  [        R                  " US   5      n	O[        R                  " 5       n	UR                  U	l        UR                  U	l        UR                  U	l        [!        US5      (       a@  UR"                   H0  n
U
R$                  S;  d  M  U	R'                  U
R$                  5        M2     [!        U	S	5      (       a"  [!        US	5      (       a  UR(                  U	l        [!        U	S
5      (       a"  [!        US
5      (       a  UR*                  U	l        [        U	[        R                  5      (       a)  [-        U	5       H  u  pX{   nUR*                  Ul        M     O[	        U5      S:  a  [/        S5      eUn	Ub  U R1                  U5        U R3                  XSSS9  g)aZ  
Insert a Note or Chord into an offset position in this Stream.
If there is another Note or Chord in this position,
create a new Note or Chord that combines the pitches of the
inserted chord. If there is a Rest in this position,
the Rest is replaced by the Note or Chord. The duration of the
previously-found chord will remain the same in the new Chord.

>>> n1 = note.Note('D4')
>>> n1.duration.quarterLength = 2.0
>>> r1 = note.Rest()
>>> r1.duration.quarterLength = 2.0
>>> c1 = chord.Chord(['C4', 'E4'])
>>> s = stream.Stream()
>>> s.append(n1)
>>> s.append(r1)
>>> s.append(c1)
>>> s.show('text')
{0.0} <music21.note.Note D>
{2.0} <music21.note.Rest half>
{4.0} <music21.chord.Chord C4 E4>

Save the original Streams for later

>>> import copy
>>> s2 = copy.deepcopy(s)
>>> s3 = copy.deepcopy(s)
>>> s4 = copy.deepcopy(s)

Notice that the duration of the inserted element is not taken into
consideration and the original element is not broken up,
as it would be in chordify().  But Chords and Notes are created:

>>> for i in [0.0, 2.0, 4.0]:
...     s.insertIntoNoteOrChord(i, note.Note('F#4'))
>>> s.show('text')
{0.0} <music21.chord.Chord D4 F#4>
{2.0} <music21.note.Note F#>
{4.0} <music21.chord.Chord C4 E4 F#4>

If chordsOnly is set to True then no notes are returned, only chords, but
untouched notes are left alone:

>>> s2.insert(5.0, note.Note('E##4'))
>>> for i in [0.0, 2.0, 4.0]:
...     s2.insertIntoNoteOrChord(i, note.Note('F#4'), chordsOnly=True)
>>> s2.show('text')
{0.0} <music21.chord.Chord D4 F#4>
{2.0} <music21.chord.Chord F#4>
{4.0} <music21.chord.Chord C4 E4 F#4>
{5.0} <music21.note.Note E##>

A chord inserted on top of a note always changes the note into a chord:

>>> s2.insertIntoNoteOrChord(5.0, chord.Chord('F#4 G-4'))
>>> s2.show('text')
{0.0} <music21.chord.Chord D4 F#4>
{2.0} <music21.chord.Chord F#4>
{4.0} <music21.chord.Chord C4 E4 F#4>
{5.0} <music21.chord.Chord E##4 F#4 G-4>

Chords can also be inserted into rests:

>>> s3.getElementsByOffset(2.0).first()
<music21.note.Rest half>
>>> s3.insertIntoNoteOrChord(2.0, chord.Chord('C4 E4 G#4'))
>>> s3.show('text')
{0.0} <music21.note.Note D>
{2.0} <music21.chord.Chord C4 E4 G#4>
{4.0} <music21.chord.Chord C4 E4>

Despite the variable name, a rest could be inserted into a noteOrChord.
It does nothing to existing notes or chords, and just adds a new rest
afterwards.

>>> s4.show('text', addEndTimes=True)
{0.0 - 2.0} <music21.note.Note D>
{2.0 - 4.0} <music21.note.Rest half>
{4.0 - 5.0} <music21.chord.Chord C4 E4>

>>> for i in [0.0, 4.0, 6.0]:  # skipping 2.0 for now
...     r = note.Rest(type='quarter')
...     s4.insertIntoNoteOrChord(i, r)
>>> r2 = note.Rest(type='quarter')
>>> s4.insertIntoNoteOrChord(2.0, r)
>>> s4.show('text', addEndTimes=True)
{0.0 - 2.0} <music21.note.Note D>
{2.0 - 4.0} <music21.note.Rest half>
{4.0 - 5.0} <music21.chord.Chord C4 E4>
{6.0 - 7.0} <music21.note.Rest quarter>

Notice that (1) the original duration and not the new duration is used, unless
there is no element at that place, and (2) if an element is put into a place where
no existing element was found, then it will be found in the new Stream, but if it
is placed on top of an existing element, the original element or a new copy will remain:

>>> r in s4
True
>>> r2 in s4
False

If a Stream has more than one note, chord, or rest at that position,
currently an error is raised.  This may change later:

>>> s5 = stream.Stream()
>>> s5.insert(0, note.Note('C##4'))
>>> s5.insert(0, note.Note('E--4'))
>>> s5.insertIntoNoteOrChord(0, note.Note('D4'))
Traceback (most recent call last):
music21.exceptions21.StreamException: more than one element found at the specified offset
FTry  Nrm  r   lyrics)r   NstemDirectionnoteheadFillz3more than one element found at the specified offsetr  )r   r   r  notesAndRestsr   r   r%   RestNoter&   r   Chordr\   expressionsarticulationsr   rT  r  textaddLyricr  r  r  ro  r  rV   )r   rM   noteOrChord
chordsOnlytargetsremoveTargetr\   
componentsr  finalTargetlyr  n	nPreviouss                 rJ   insertIntoNoteOrChordStream.insertIntoNoteOrChord!	  sy   d $$222#(!& $ %  m
 w<1GJQZF!L&$)),,k49955*001G"-JU[[99";#6#67G!%k!2J&$)),,k49955%||[->->?G"(!6JU[[99%||ntK4G4G/HHG"(D,=!=J%||nG"(J&%++..k49955"6>>2k6G6G5HHG!%f!=JU[[99"6>>2T+:M:M5NNG!%f[0A!AJ"6>>2G!%fJ7|a:#5#kk'2W""ii
3"iik&,&8&8K#(.(<(<K%#)??K vx(( --Bwwj0#,,RWW5 ( {O449Y9Y,2,@,@){N338W8W+1+>+>( +u{{33%k2DA *I%.%;%;AN 3 \A!"WXX%K#KK%FENrI   c                L   U R                   n[        R                  " U5      (       d  U/nSnU R                  (       a  U R                  S   nOSnSnU GH  nU R	                  U5        UR
                  (       a  SnU R                  XbSS9  UR                  R                  U 5        U R                  U5        U R                  R                  U5        UR                  R                  S:w  a  X&R                  R                  -  nUbQ  UR                  R                  (       d6  UR                  UR                  :  d  UR                  UR                  :  a  SnUnGM
     U(       a  SnOU R                  nU R!                  US9  Xpl        U R#                  [%        U5      5        g)a  
Add a Music21Object (including another Stream) to the end of the current Stream.

If given a list, will append each element in order after the previous one.

The "end" of the stream is determined by the `highestTime` property
(that is the latest "release" of an object, or directly after the last
element ends).

Runs fast for multiple addition and will preserve isSorted if True

>>> a = stream.Stream()
>>> notes = []
>>> for x in range(3):
...     n = note.Note('G#')
...     n.duration.quarterLength = 3
...     notes.append(n)
>>> a.append(notes[0])
>>> a.highestOffset, a.highestTime
(0.0, 3.0)
>>> a.append(notes[1])
>>> a.highestOffset, a.highestTime
(3.0, 6.0)
>>> a.append(notes[2])
>>> a.highestOffset, a.highestTime
(6.0, 9.0)
>>> notes2 = []

Notes' naive offsets will
change when they are added to a stream.

>>> for x in range(3):
...     n = note.Note('A-')
...     n.duration.quarterLength = 3
...     n.offset = 0
...     notes2.append(n)
>>> a.append(notes2)  # add em all again
>>> a.highestOffset, a.highestTime
(15.0, 18.0)
>>> a.isSequence()
True

Adding a note that already has an offset set does nothing different
from above! That is, it is still added to the end of the Stream:

>>> n3 = note.Note('B-')
>>> n3.offset = 1
>>> n3.duration.quarterLength = 3
>>> a.append(n3)
>>> a.highestOffset, a.highestTime
(18.0, 21.0)
>>> n3.getOffsetBySite(a)
18.0

Prior to v5.7 there was a bug where appending a `Clef` after a `KeySignature`
or a `Measure` after a `KeySignature`, etc. would not cause sorting to be re-run.
This bug is now fixed.

>>> s = stream.Stream()
>>> s.append([meter.TimeSignature('4/4'),
...           clef.TrebleClef()])
>>> s.elements[0]
<music21.clef.TrebleClef>
>>> s.show('text')
{0.0} <music21.clef.TrebleClef>
{0.0} <music21.meter.TimeSignature 4/4>

>>> s.append(metadata.Metadata(composer='Cage'))
>>> s.show('text')
{0.0} <music21.metadata.Metadata object at 0x11ca356a0>
{0.0} <music21.clef.TrebleClef>
{0.0} <music21.meter.TimeSignature 4/4>
Fr   NTr   r   r	  )highestTimer   r~  r   r   rt   r  r)   r  r   rU   r   r  priorityrS   ra   r   _setHighestTimer   )r   othersr  rx  lastElementr
  ro   r  s           rJ   rU   Stream.append	  s]   V &&  ((XF>>..,KKA**1-zz# %%a%FGGKK##A&NN!!!$zz''1, zz777&{/C/C/Q/QJJ!5!55++k.H.HH$(MK+ 0 K--K 	  l ;#VK01rI   c                   [        U[        5      (       a  U H  nU R                  X2S9  M     gUnUnU R                  U5        UR                  R
                  S:w  a  [        S5      eU R                  U5        U R                  SS9  g)aq  
Inserts an item or items at the end of the Stream,
stored in the special box (called _endElements).

This method is useful for putting things such as
right bar lines or courtesy clefs that should always
be at the end of a Stream no matter what else is appended
to it.

As sorting is done only by priority and class,
it cannot avoid setting isSorted to False.

>>> s = stream.Stream()
>>> b = bar.Repeat()
>>> s.storeAtEnd(b)
>>> b in s
True
>>> s.elementOffset(b)
0.0
>>> s.elementOffset(b, returnSpecial=True)
<OffsetSpecial.AT_END>

Only elements of zero duration can be stored.  Otherwise, a
`StreamException` is raised.
r  Nr   zTcannot insert an object with a non-zero Duration into the highest time elements listFr	  )	r   r   rW   r   r   r  ro  rg  r   )r   
itemOrListr  r  rL   s        rJ   rW   Stream.storeAtEnd`
  s    4 j$''"< # D&&w/ ))Q.! #S T T 	G$  e 4rI   c                B   Ub!  UnUR                   R                  nUnUnXT-   nOUc|  [        U[        5      (       ag  UnSnSnSn	U	[	        U5      :  aO  X   n
XS-      nUR                   R                  n[        XzU-   5      nUb  X:  a  U
nU	S-  n	U	[	        U5      :  a  MO  O3UnUR                   R                  nUR                  U-   nUR                  nSnSnU R                   H.  nU R                  U5      n
X-
  nUS:  a  M   Ub  X:  d  M*  UnUnM0     Ub  U R                  U5      nX-
  nOSnUS::  a  ODU R                   H4  nU R                  U5      n
X-
  nUS:  d  M   U R                  XU-   5        M6     U R                  5         U R                  X5        g)a  
Insert an item at a specified or native offset,
and shift any elements found in the Stream to start at
the end of the added elements.

This presently does not shift elements that have durations
that extend into the lowest insert position.

>>> st1 = stream.Stream()
>>> st1.insertAndShift(32, note.Note('B'))
>>> st1.highestOffset
32.0
>>> st1.insertAndShift(32, note.Note('C'))
>>> st1.highestOffset
33.0
>>> st1.show('text', addEndTimes=True)
{32.0 - 33.0} <music21.note.Note C>
{33.0 - 34.0} <music21.note.Note B>

Let's insert an item at the beginning, note that
since the C and B are not affected, they do not shift.

>>> st1.insertAndShift(0, note.Note('D'))
>>> st1.show('text', addEndTimes=True)
{0.0 - 1.0} <music21.note.Note D>
{32.0 - 33.0} <music21.note.Note C>
{33.0 - 34.0} <music21.note.Note B>

But if we insert something again at the beginning of the stream,
everything after the first shifted note begins shifting, so the
C and the B shift even though there is a gap there.  Normally
there's no gaps in a stream, so this will not be a factor:

>>> st1.insertAndShift(0, note.Note('E'))
>>> st1.show('text', addEndTimes=True)
{0.0 - 1.0} <music21.note.Note E>
{1.0 - 2.0} <music21.note.Note D>
{33.0 - 34.0} <music21.note.Note C>
{34.0 - 35.0} <music21.note.Note B>

In the single argument form with an object, inserts the element at its stored offset:

>>> n1 = note.Note('C#')
>>> n1.offset = 30.0
>>> n2 = note.Note('D#')
>>> n2.offset = 30.0
>>> st1 = stream.Stream()
>>> st1.insertAndShift(n1)
>>> st1.insertAndShift(n2)  # will shift offset of n1
>>> n1.getOffsetBySite(st1)
31.0
>>> n2.getOffsetBySite(st1)
30.0
>>> st1.show('text', addEndTimes=True)
{30.0 - 31.0} <music21.note.Note D#>
{31.0 - 32.0} <music21.note.Note C#>

>>> st2 = stream.Stream()
>>> st2.insertAndShift(40.0, n1)
>>> st2.insertAndShift(40.0, n2)
>>> n1.getOffsetBySite(st2)
41.0

In single argument form with a list, the list should contain pairs that alternate
offsets and items; the method then, obviously, inserts the items
at the specified offsets:

>>> n1 = note.Note('G-')
>>> n2 = note.Note('F-')
>>> st3 = stream.Stream()
>>> st3.insertAndShift([1.0, n1, 2.0, n2])
>>> n1.getOffsetBySite(st3)
1.0
>>> n2.getOffsetBySite(st3)
2.0
>>> len(st3)
2
>>> st3.show('text', addEndTimes=True)
{1.0 - 2.0} <music21.note.Note G->
{2.0 - 3.0} <music21.note.Note F->

N.B. -- using this method on a list assumes that you'll be inserting
contiguous objects; you can't shift things that are separated, as this
following FAILED example shows.

>>> n1 = note.Note('G', type='half')
>>> st4 = stream.Stream()
>>> st4.repeatAppend(n1, 3)
>>> st4.insertAndShift([2.0, note.Note('e'), 4.0, note.Note('f')])
>>> st4.show('text')
{0.0} <music21.note.Note G>
{2.0} <music21.note.Note E>
{4.0} <music21.note.Note F>
{5.0} <music21.note.Note G>
{7.0} <music21.note.Note G>

As an FYI, there is no removeAndShift() function, so the opposite of
insertAndShift(el) is remove(el, shiftOffsets=True).
Nrl   r   rm  r  )r   r  r   r   r   maxrM   r   r  r  r   rV   )r   r  r  insertObjectqLrM   lowestOffsetInserthighestTimeInsert
insertListr  r  ro   lowestElementToShift	lowestGapgaplowestOffsetToShiftshiftPoss                    rJ   rX   Stream.insertAndShift
  s   J !%L&&44B'F!' &J/A4$H$H+J #!%Ac*o%M1u%ZZ--$'(9r6$B!%-1G)*&Q c*o% .L&&44B , 3 3b 8!-!4!4  $	A""1%A (CQw CO	'($    +"&"4"45I"J(>HHq=
 ^^&&q) ,#: --aX> $  	  "&3rI   c                    U(       d  U R                  5       nOU R                  5       nU H"  nUR                  c  M  XR                  l        M$     g)a  
Sets the .derivation.method for each element in the Stream
if it has a .derivation object.

>>> import copy
>>> s = converter.parse('tinyNotation: 2/4 c2 d e f')
>>> s2 = copy.deepcopy(s)
>>> s2.recurse().notes[-1].derivation
<Derivation of <music21.note.Note F> from <music21.note.Note F> via '__deepcopy__'>
>>> s2.setDerivationMethod('exampleCopy', recurse=True)
>>> s2.recurse().notes[-1].derivation
<Derivation of <music21.note.Note F> from <music21.note.Note F> via 'exampleCopy'>

Without recurse:

>>> s = converter.parse('tinyNotation: 2/4 c2 d e f')
>>> s2 = copy.deepcopy(s)
>>> s2.setDerivationMethod('exampleCopy')
>>> s2.recurse().notes[-1].derivation
<Derivation of <music21.note.Note F> from <music21.note.Note F> via '__deepcopy__'>
N)r   rY   r   rO  )r   r  rY   sIterr   s        rJ   setDerivationMethodStream.setDerivationMethodM  s;    0 IIKELLNEB}}('7$ rI   rY   
allDerivedc                 ^^^^ U 4UUUU4S jjn U R                  T5      n[        T SU  35      e! [         a     Of = f U R                  T5      nOK! [         a>    T(       a4  U R                  TSS9nUb  UR                  TTTS9  U" 5         Ub  U" US9   gf = f[	        U R
                  5      nXh:  aZ  U R
                  U   mTU R
                  U'   U R                  TU R                  T5      SS	9  TR                  R                  U 5        O\U R                  Xh-
     mTU R                  Xh-
  '   U R                  T[        R                  SS	9  TR                  R                  U 5        TR                  R                  U 5        STl        [        T5      U R                   ;   a  U R                   [        T5      	 Sn	TR"                  (       a  Sn	U R%                  U	S
9  U" 5         g)a  
Given a `target` object, replace it with
the supplied `replacement` object.

Does nothing if target cannot be found. Raises StreamException if replacement
is already in the stream.

If `allDerived` is True (as it is by default), all sites (stream) that
this stream derives from and also
have a reference for the replacement will be similarly changed.
This is useful for altering both a flat and nested representation.

>>> cSharp = note.Note('C#4')
>>> s = stream.Stream()
>>> s.insert(0, cSharp)
>>> dFlat = note.Note('D-4')
>>> s.replace(cSharp, dFlat)
>>> s.show('t')
{0.0} <music21.note.Note D->

If allDerived is True then all streams that this stream comes from get changed
(but not non-derived streams)

>>> otherStream = stream.Stream()
>>> otherStream.insert(0, dFlat)
>>> f = note.Note('F4')
>>> sf = s.flatten()
>>> sf is not s
True
>>> sf.replace(dFlat, f, allDerived=True)
>>> sf[0] is f
True
>>> s[0] is f
True
>>> otherStream[0] is dFlat
True

Note that it does not work the other way: if we made the replacement on `s`
then `sf`, the flattened representation, would not be changed, since `s`
does not derive from `sf` but vice-versa.

With `recurse=True`, a stream can replace an element that is
further down in the hierarchy.  First let's set up a
nested score:

>>> s = stream.Score()
>>> p = stream.Part(id='part1')
>>> s.append(p)
>>> m = stream.Measure()
>>> p.append(m)
>>> cSharp = note.Note('C#4')
>>> m.append(cSharp)
>>> s.show('text')
{0.0} <music21.stream.Part part1>
    {0.0} <music21.stream.Measure 0 offset=0.0>
        {0.0} <music21.note.Note C#>

Now make a deep-nested replacement

>>> dFlat = note.Note('D-4')
>>> s.replace(cSharp, dFlat, recurse=True)
>>> s.show('text')
{0.0} <music21.stream.Part part1>
    {0.0} <music21.stream.Measure 0 offset=0.0>
        {0.0} <music21.note.Note D->

* Changed by v5: `allTargetSites` renamed to `allDerived` -- only
  searches in derivation chain.
* Changed in v5.3: `firstMatchOnly` removed -- impossible to have element
  in stream twice.  `recurse` and `shiftOffsets` changed to keywordOnly arguments
* Changed in v6: recurse works
* Changed in v7: raises `StreamException` if replacement is already in the stream.
c           	        > T(       d  g U R                   R                  5        H>  nUR                  SSS9 H'  nUTR                  ;   d  M  UR	                  TTTSS9  M)     M@     g )NTrv  r  Fr  )r   chainrY   r)   replace)	startSitederivedSitesubsiter  rY   replacementr  s      rJ   replaceDerived&Stream.replace.<locals>.replaceDerived  sf    (3399;*22tQU2VG&,,.(30738 ( :  W  <rI   z already in Fr  N)r  )r  Tr   r	  )r%  ro  containerInHierarchyr  r   r   r  r  r)   r  r   r   AT_ENDr  r  r   r   rt   r   )
r   r  r  rY   r  r  r  	containerr  r
  s
    ````     rJ   r  Stream.replacen  s   ^ &* 		: 		:	F

;'A
 "[MdV"DEE	  		
	

6"A 	 55fE5R	(%%fkj%Q ("Y7	 4>>"8^^A&F +DNN1%%k43E3Ef3MZ^%_!!$' &&qx0F*5Dah' %%k=3G3GTX%Y!!$'D! f:)))  F,L  l ;s!   3 
A A A ABBrY   c                   SS jnU" U 5        U(       a   U R                  SSSS9 H  nU" U5        M     [        R                  " U 45      $ )a  
Overrides base method :meth:`~music21.base.Music21Object.splitAtDurations`
so that once each element in the stream having a complex duration is split
into similar, shorter elements representing each duration component,
the original element is actually replaced in the stream where it was found
with those new elements.

Returns a 1-tuple containing itself, for consistency with the superclass method.

>>> s = stream.Stream()
>>> s.insert(note.Note(quarterLength=5.0))
>>> post = s.splitAtDurations()
>>> post
(<music21.stream.Stream 0x10955ceb0>,)
>>> [n.duration for n in s]
[<music21.duration.Duration 4.0>, <music21.duration.Duration 1.0>]

Unless `recurse=True`, notes in substreams will not be found.

>>> s2 = stream.Score()
>>> p = stream.Part([note.Note(quarterLength=5)])
>>> s2.append(p)
>>> s2.splitAtDurations()
(<music21.stream.Score 0x10d12f100>,)
>>> [n.duration for n in s2.recurse().notes]
[<music21.duration.Duration 5.0>]
>>> s2.splitAtDurations(recurse=True)
(<music21.stream.Score 0x10d12f100>,)
>>> [n.duration for n in s2.recurse().notes]
[<music21.duration.Duration 4.0>, <music21.duration.Duration 1.0>]

`recurse=True` should not be necessary to find elements in streams
without substreams, such as a loose Voice:

>>> v = stream.Voice([note.Note(quarterLength=5.5)], id=1)
>>> v.splitAtDurations()
(<music21.stream.Voice 1>,)
>>> [n.duration for n in v.notes]
[<music21.duration.Duration 4.0>, <music21.duration.Duration 1.5>]

But a Voice in a Measure (most common) will not be found without `recurse`:

>>> m = stream.Measure()
>>> v2 = stream.Voice([note.Note(quarterLength=5.25)])
>>> m.insert(v2)
>>> m.splitAtDurations()
(<music21.stream.Measure 0 offset=0.0>,)
>>> [n.duration for n in m.recurse().notes]
[<music21.duration.Duration 5.25>]

For any spanner containing the element being removed, the first or last of the
replacing components replaces the removed element
(according to whether it was first or last in the spanner.)

>>> s3 = stream.Stream()
>>> n1 = note.Note(quarterLength=5)
>>> n2 = note.Note(quarterLength=5)
>>> s3.append([n1, n2])
>>> s3.insert(0, spanner.Slur([n1, n2]))
>>> post = s3.splitAtDurations()
>>> s3.spanners.first().getFirst() is n1
False
>>> s3.spanners.first().getFirst().duration
<music21.duration.Duration 4.0>
>>> s3.spanners.first().getLast().duration
<music21.duration.Duration 1.0>

Does not act on rests where `.fullMeasure` is True or 'always',
nor when `.fullMeasure` is 'auto' and the duration equals the `.barDuration`.
This is because full measure rests are usually represented
as a single whole rest regardless of their duration.

>>> r = note.Rest(quarterLength=5.0)
>>> r.fullMeasure = 'auto'
>>> v = stream.Voice(r)
>>> m = stream.Measure(v)
>>> result = m.splitAtDurations(recurse=True)
>>> list(result[0][note.Rest])
[<music21.note.Rest 5ql>]

Here is a rest that doesn't fill the measure:

>>> m.insert(0, meter.TimeSignature('6/4'))
>>> result = m.splitAtDurations(recurse=True)
>>> list(result[0][note.Rest])
[<music21.note.Rest whole>, <music21.note.Rest quarter>]

But by calling it a full-measure rest, we won't try to split it:

>>> r2 = note.Rest(quarterLength=5.0)
>>> r2.fullMeasure = True
>>> m2 = stream.Measure(r2)
>>> m2.insert(0, meter.TimeSignature('6/4'))
>>> result = m2.splitAtDurations()
>>> list(result[0][note.Rest])
[<music21.note.Rest 5ql>]
c                $   U R                  / SQ5       GH  nUR                  R                  S:w  a  M   [        U[        R
                  5      (       a  UR                  S;   a  MQ  [        U[        R
                  5      (       a  UR                  S:X  a  [        U [        5      (       a  UR                  U R                  :X  a  M  SU R                  ;   aS  U R                  (       aB  U R                  R                  (       a'  UR                  U R                  R                  :X  a  GM  UR                  nUR                  5       nU R                  XS   5        X#S   R                  -  nUSS   H"  nU R!                  X$5        X$R                  -  nM$     UR#                  5        HS  nUR%                  5       UL a  UR'                  XS   5        UR)                  5       UL d  M?  UR'                  XS   5        MU     S	U R*                  l        GM     g )
N)rQ   VariantSpannercomplex)TalwaysautoVoicer   rm  r   F)r  r   r   r   r%   r  fullMeasurerw   barDurationclassesr  	isMeasurerM   splitAtDurationsr  r  rV   getSpannerSitesgetFirstr  getLastr.   beams)r"  
complexObjinsertPointobjList
subsequentsps         rJ   processContainer1Stream.splitAtDurations.<locals>.processContainer\  s   '==>^_
&&++y8j$))449O9OSc9cj$))449O9OSY9Y"9g66!+!4!4	8M8M!M !Y%6%66'22'22<<(11Y5I5I5U5UU (//$557!!*aj9qz777")!"+J$$[=#;#;;K #.
 %446B{{}
200QZHzz|z100R[I	 7 05	&&,C `rI   FTr  rv  restoreActiveSites)r"  rQ   )rY   r   _SplitTuple)r   rY   r;  innerStreams       rJ   r1  Stream.splitAtDurations  sT    F"	5J 	#|| %4D  ,  R - R ((rI   )retainOriginaddTiesdisplayTiedAccidentalssearchContextc          	        / n[        U5      nU(       a  U nOU R                  S5      nU R                  5       nUR                  (       a  UR	                  USS9n	U	(       a  [
        R                  " U	S   5      Ul        U(       a&  UR                  [        R                  5      n
U
b  U
/nO([        UR                  [        R                  5      5      nU(       a  [
        R                  " US   5      Ul        UR                  [        R                  5      nUb  [
        R                  " U5      Ul        XR                  :  a  [         R"                  " Xx/5      $ UR%                  UUR                  SSSSS9n/ n/ nU H<  nUR'                  U5      U:  a  UR)                  U5        M+  UR)                  U5        M>     U H=  nXR'                  U5      -
  nUR+                  USUUS9u  nnUR-                  SU5        M?     U H8  nUR-                  UR/                  U5      U-
  U5        UR1                  U5        M:     [         R"                  " Xx/5      $ )z
This method overrides the method on Music21Object to provide
similar functionality for Streams.

Most arguments are passed to Music21Object.splitAtQuarterLength.

* Changed in v7: all but quarterLength are keyword only
splitAtQuarterLengthFrE  returnDefaultr   T)rz  r{  includeElementsThatEndAtStartr|  rB  rC  rD  )r   coreCopyAsDerivationr   r0  getTimeSignaturesr  r  r6  getContextByClassr"   r4  r   r   r   r  r  r   r?  r   r  rU   rG  rV   r  r  )r   r  rB  rC  rD  rE  keySignaturessLeftsRighttimeSignatures	ksContextendClefr  targetSplit
targetMover  qlSplitunused_eLefteRights                      rJ   rG  Stream.splitAtQuarterLength  s1    13}-E--.DEE! ??"44+# 5 N &*mmN14E&F#!33C4D4DE	(%.KM $U%=%=c>N>N%O P&*mmM!4D&E#--dii8G""mmG4,,,##UO44 ++#"*/! , 
 
F""6*]:""6*!!&)  "F
 $&9&9&&AAG#)#>#>!'=	 $? $? L& MM!V$ "  !FMM&007-GPLL  ! 00rI   r   prefixSpaces	addBreaks	addIndentaddEndTimesuseMixedNumeralsc                 ^ ^^   SUU U4S jjn/ nSnT  H{  n	U(       a  SU-  n
OSn
[        U	[        5      (       a=  UR                  U" X5      5        UR                  U	R                  X-   UUTTS95        Md  UR                  U" X5      5        M}     U(       a  SR	                  U5      nU$ SR	                  U5      nU$ )	a  
Used by .show('text') to display a stream's contents with offsets.

>>> s1 = stream.Stream()
>>> s2 = stream.Stream()
>>> s3 = stream.Stream()
>>> n1 = note.Note()
>>> s3.append(n1)
>>> s2.append(s3)
>>> s1.append(s2)
>>> post = s1.recurseRepr(addBreaks=False, addIndent=False)
>>> post
'{0.0} <music21.stream.Stream ...> / {0.0} <...> / {0.0} <music21.note.Note C>'

Made public in v7.  Always calls on self.
c                  > U R                  T5      nT(       a  [        R                  " U5      nO[        R                  " U5      nTSL a  US-   U-   S-   [	        U 5      -   $ X R
                  R                  -   nT(       a  [        R                  " U5      nO[        R                  " U5      nUS-   U-   S-   U-   S-   [	        U 5      -   $ )NF{z} z - )r  r   mixedNumeralstrTrimFloatreprr   r  )	
in_element	in_indentoffGetoffqlqlStrr_  r   r`  s	         rJ   singleElement)Stream.recurseRepr.<locals>.singleElement  s      //5F))&1))&1e# 3,t3d:6FFF11???#"//3E"//3E 3,u4u<tCd:FVVVrI   r   r   r   r[  
z / returnr   )r   rQ   rU   recurseReprjoin)r   r\  r]  r^  r_  r`  rm  msginsertSpacesrL   indentmsgStrs   `   ``      rJ   rr  Stream.recurseRepr  s    .	W"	W 	W$ G|+'6**

=9:

''\5P2;2;4?9I	 ( K 

=9:! $ YYs^F  ZZ_FrI   r_  r`  c               "    U R                  UUS9$ )z
Return a text representation. This method can be overridden by
subclasses to provide alternative text representations.

This is used by .show('text')
ry  rr  )r   r_  r`  r   s       rJ   	_reprTextStream._reprText+  s#     K1A   C 	CrI   c               &    U R                  UUSSS9$ )z
Return a text representation without line breaks.
This method can be overridden by subclasses to
provide alternative text representations.
F)r_  r`  r]  r^  r{  )r   r_  r`  s      rJ   _reprTextLineStream._reprTextLine5  s'     K1A*/*/   1 	1rI   )returnInNotebookc                   SSK Jn  UR                  " U U4UUUS.UD6nU(       d  [        R                  " 5       (       d  U$ UR                  5         g)a|  
Given a method and keyword configuration arguments, create and display a plot.

Note: plot() requires the Python package matplotlib to be installed.

For details on arguments this function takes, see
:ref:`User's Guide, Chapter 22: Graphing <usersGuide_22_graphing>`.

>>> s = corpus.parse('demos/two-parts.xml') #_DOCS_HIDE
>>> thePlot = s.plot('pianoroll', doneAction=None) #_DOCS_HIDE
>>> #_DOCS_SHOW s = corpus.parse('bach/bwv57.8')
>>> #_DOCS_SHOW thePlot = s.plot('pianoroll')

.. image:: images/HorizontalBarPitchSpaceOffset.*
    :width: 600

By default, a plot is returned in normal Python environments, but not
in Jupyter notebook/JupyterLab/Google Colab.  The
keyword `returnInNotebook` if True returns a plot no matter what.

Changed in v9: Changed default for return in notebook, and added
  `returnInNotebook` keyword based on changes to recent Jupyter and
  similar releases.
r   )graph)xValueyValuezValueN)r?  r  
plotStreamr   runningInNotebookrun)	r   
plotFormatr  r  r  r  r   r  outs	            rJ   plotStream.plotC  s\    B 	"
 
 
 6#;#;#=#=J	rI   c                4    SSK Jn  UR                  " X40 UD6$ )a<  
Runs a particular analytical method on the contents of the
stream to find its ambitus (range) or key.

*  ambitus -- runs :class:`~music21.analysis.discrete.Ambitus`
*  key -- runs :class:`~music21.analysis.discrete.KrumhanslSchmuckler`

Some of these methods can take additional arguments.  For details on
these arguments, see
:func:`~music21.analysis.discrete.analyzeStream`.

Example:

>>> s = corpus.parse('bach/bwv66.6')
>>> s.analyze('ambitus')
<music21.interval.Interval m21>
>>> s.analyze('key')
<music21.key.Key of f# minor>

Example: music21 allows you to automatically run an
analysis to get the key of a piece or excerpt not
based on the key signature but instead on the
frequency with which some notes are used as opposed
to others (first described by Carol Krumhansl).  For
instance, a piece with mostly Cs and Gs, some Fs,
and Ds, but fewer G#s, C#s, etc. is more likely to
be in the key of C major than in D-flat major
(or A minor, etc.).  You can easily get this analysis
from a stream by running:

>>> myStream = corpus.parse('luca/gloria')
>>> analyzedKey = myStream.analyze('key')
>>> analyzedKey
<music21.key.Key of F major>

analyzedKey is a :class:`~music21.key.Key`
object with a few extra parameters.
correlationCoefficient shows how well this key fits the
profile of a piece in that key:

>>> analyzedKey.correlationCoefficient
0.86715...

`alternateInterpretations` is a list of the other
possible interpretations sorted from most likely to least:

>>> analyzedKey.alternateInterpretations
[<music21.key.Key of d minor>,
 <music21.key.Key of C major>,
 <music21.key.Key of g minor>,
 ...]

Each of these can be examined in turn to see its correlation coefficient:

>>> analyzedKey.alternateInterpretations[1].correlationCoefficient
0.788528...
>>> analyzedKey.alternateInterpretations[22].correlationCoefficient
-0.86728...
r   )discrete)music21.analysisr  analyzeStream)r   rO  r   r  s       rJ   analyzeStream.analyzet  s    z 	.%%d?h??rI   )rY   r  c               $   U(       d  U R                  5       OU R                  5       nUb%  UR                  [        R                  " U5      5      nU(       + Ul        U H/  nXR                  ;  d  M  UR                  R                  U5        M1     g)a  
Add the group to the groups attribute of all elements.
if `classFilter` is set then only those elements whose objects
belong to a certain class (or for Streams which are themselves of
a certain class) are set.

>>> a = stream.Stream()
>>> a.repeatAppend(note.Note('A-'), 30)
>>> a.repeatAppend(note.Rest(), 30)
>>> a.addGroupForElements('flute')
>>> a[0].groups
['flute']
>>> a.addGroupForElements('quietTime', note.Rest)
>>> a[0].groups
['flute']
>>> a[50].groups
['flute', 'quietTime']
>>> a[1].groups.append('quietTime')  # set one note to it
>>> a[1].step = 'B'
>>> b = a.getElementsByGroup('quietTime')
>>> len(b)
31
>>> c = b.getElementsByClass(note.Note)
>>> len(c)
1
>>> c[0].name
'B-'

If recurse is True then all sub-elements will get the group:

>>> s = converter.parse('tinyNotation: 4/4 c4 d e f g a b- b')
>>> s.addGroupForElements('scaleNote', 'Note')
>>> s.recurse().notes[3].groups
[]
>>> s.addGroupForElements('scaleNote', 'Note', recurse=True)
>>> s.recurse().notes[3].groups
['scaleNote']

No group will be added more than once:

>>> s.addGroupForElements('scaleNote', 'Note', recurse=True)
>>> s.recurse().notes[3].groups
['scaleNote']

* New in v6.7.1: recurse
N)r   rY   	addFilterr0   ClassFilterr>  groupsrU   )r   groupclassFilterrY   r  	sIteratorr   s          rJ   addGroupForElementsStream.addGroupForElements  sl    h (/DIIKDLLN	"!++G,?,?,LMI+8'8	$BII%		  ' rI   c                    g rs   rC   r   rh  s     rJ   r   Stream.getElementsByClass       	rI   c                    g rs   rC   r  s     rJ   r   r    r  rI   c                    g rs   rC   r  s     rJ   r   r    r  rI   c                @    U R                  5       R                  U5      $ )a  
Return a StreamIterator that will iterate over Elements that match one
or more classes in the `classFilterList`. A single class
can also be used for the `classFilterList` parameter instead of a List.

>>> a = stream.Score()
>>> a.repeatInsert(note.Rest(), list(range(10)))
>>> for x in range(4):
...     n = note.Note('G#')
...     n.offset = x * 3
...     a.insert(n)
>>> found = a.getElementsByClass(note.Note)
>>> found
<music21.stream.iterator.StreamIterator for Score:0x118d20710 @:0>

>>> len(found)
4
>>> found[0].pitch.accidental.name
'sharp'

>>> foundStream = found.stream()
>>> isinstance(foundStream, stream.Score)
True


Notice that we do not find elements that are in
sub-streams of the main Stream.  We'll add 15 more rests
in a sub-stream, and they won't be found:

>>> b = stream.Stream()
>>> b.repeatInsert(note.Rest(), list(range(15)))
>>> a.insert(b)
>>> found = a.getElementsByClass(note.Rest)
>>> len(found)
10

To find them either (1) use `.flatten()` to get at everything:

>>> found = a.flatten().getElementsByClass(note.Rest)
>>> len(found)
25

Or, (2) recurse over the main stream and call .getElementsByClass
on each one.  Notice that the first subStream is actually the outermost
Stream:

>>> totalFound = 0
>>> for subStream in a.recurse(streamsOnly=True, includeSelf=True):
...     found = subStream.getElementsByClass(note.Rest)
...     totalFound += len(found)
>>> totalFound
25

The class name of the Stream created is usually the same as the original:

>>> found = a.getElementsByClass(note.Note).stream()
>>> found.__class__.__name__
'Score'

An exception is if `returnStreamSubClass` is False, which makes the method
return a generic Stream:

>>> found = a.getElementsByClass(note.Rest).stream(returnStreamSubClass=False)
>>> found.__class__.__name__
'Stream'

Make a list from a StreamIterator:

>>> foundList = list(a.recurse().getElementsByClass(note.Rest))
>>> len(foundList)
25
)r   r   r  s     rJ   r   r  
  s    b yy{--o>>rI   c                >    U R                  5       R                  USS9$ )a  
Return a list of all Elements that do not
match the one or more classes in the `classFilterList`.

In lieu of a list, a single class can be used as the `classFilterList` parameter.

>>> a = stream.Stream()
>>> a.repeatInsert(note.Rest(), list(range(10)))
>>> for x in range(4):
...     n = note.Note('G#')
...     n.offset = x * 3
...     a.insert(n)
>>> found = a.getElementsNotOfClass(note.Note)
>>> len(found)
10

>>> b = stream.Stream()
>>> b.repeatInsert(note.Rest(), list(range(15)))
>>> a.insert(b)

Here, it gets elements from within a stream
this probably should not do this, as it is one layer lower

>>> found = a.flatten().getElementsNotOfClass(note.Rest)
>>> len(found)
4
>>> found = a.flatten().getElementsNotOfClass(note.Note)
>>> len(found)
25
FreturnClone)r   r  r  s     rJ   r  Stream.getElementsNotOfClass]  s     > yy{00e0TTrI   c                >    U R                  5       R                  USS9$ )aN  
>>> n1 = note.Note('C')
>>> n1.groups.append('trombone')
>>> n2 = note.Note('D')
>>> n2.groups.append('trombone')
>>> n2.groups.append('tuba')
>>> n3 = note.Note('E')
>>> n3.groups.append('tuba')
>>> s1 = stream.Stream()
>>> s1.append(n1)
>>> s1.append(n2)
>>> s1.append(n3)
>>> tboneSubStream = s1.getElementsByGroup('trombone')
>>> for thisNote in tboneSubStream:
...     print(thisNote.name)
C
D
>>> tubaSubStream = s1.getElementsByGroup('tuba')
>>> for thisNote in tubaSubStream:
...     print(thisNote.name)
D
E

OMIT_FROM_DOCS
# TODO: group comparisons are not YET case insensitive.
Fr  )r   getElementsByGroup)r   groupFilterLists     rJ   r  Stream.getElementsByGroup~  s     6 yy{--o5-QQrI   c                    U R                  5       R                  [        R                  " U5      5      nU H  nUs  $    g)a  
Returns the first encountered element for a given id. Return None
if no match. Note: this uses the id attribute stored on elements,
which may not be the same as id(e).

>>> a = stream.Stream()
>>> ew = note.Note()
>>> a.insert(0, ew)
>>> a[0].id = 'green'
>>> None == a.getElementById(3)
True
>>> a.getElementById('green').id
'green'
>>> a.getElementById('Green').id  # case does not matter
'green'

Getting an element by getElementById changes its activeSite

>>> b = stream.Stream()
>>> b.append(ew)
>>> ew.activeSite is b
True
>>> ew2 = a.getElementById('green')
>>> ew2 is ew
True
>>> ew2.activeSite is a
True
>>> ew.activeSite is a
True

* Changed in v7: remove classFilter.
N)r   r  r0   IdFilter)r   	elementIdr  ro   s       rJ   getElementByIdStream.getElementById  s8    B IIK))'*:*:9*EF	AH rI   )rz  r{  r|  rJ  	classListc          	     r    U R                  5       R                  UUUUUUS9nUb  UR                  U5      nU$ )a  
Returns a StreamIterator containing all Music21Objects that
are found at a certain offset or within a certain
offset time range (given the `offsetStart` and (optional) `offsetEnd` values).

There are several attributes that govern how this range is
determined:


If `mustFinishInSpan` is True then an event that begins
between offsetStart and offsetEnd but which ends after offsetEnd
will not be included.  The default is False.


For instance, a half note at offset 2.0 will be found in
getElementsByOffset(1.5, 2.5) or getElementsByOffset(1.5, 2.5,
mustFinishInSpan=False) but not by getElementsByOffset(1.5, 2.5,
mustFinishInSpan=True).

The `includeEndBoundary` option determines if an element
begun just at the offsetEnd should be included.  For instance,
the half note at offset 2.0 above would be found by
getElementsByOffset(0, 2.0) or by getElementsByOffset(0, 2.0,
includeEndBoundary=True) but not by getElementsByOffset(0, 2.0,
includeEndBoundary=False).

Setting includeEndBoundary to False at the same time as
mustFinishInSpan is set to True is probably NOT what you want to do
unless you want to find things like clefs at the end of the region
to display as courtesy clefs.

The `mustBeginInSpan` option determines whether notes or other
objects that do not begin in the region but are still sounding
at the beginning of the region are excluded.  The default is
True -- that is, these notes will not be included.
For instance the half note at offset 2.0 from above would not be found by
getElementsByOffset(3.0, 3.5) or getElementsByOffset(3.0, 3.5,
mustBeginInSpan=True) but it would be found by
getElementsByOffset(3.0, 3.5, mustBeginInSpan=False)

Setting includeElementsThatEndAtStart to False is useful for zeroLength
searches that set mustBeginInSpan == False to not catch notes that were
playing before the search but that end just before the end of the search type.
This setting is *ignored* for zero-length searches.
See the code for allPlayingWhileSounding for a demonstration.

This chart and the examples below demonstrate the various
features of getElementsByOffset.  It is one of the most complex
methods of music21 but also one of the most powerful, so it
is worth learning at least the basics.

    .. image:: images/getElementsByOffset.*
        :width: 600


>>> st1 = stream.Stream()
>>> n0 = note.Note('C')
>>> n0.duration.type = 'half'
>>> n0.offset = 0
>>> st1.insert(n0)
>>> n2 = note.Note('D')
>>> n2.duration.type = 'half'
>>> n2.offset = 2
>>> st1.insert(n2)
>>> out1 = st1.getElementsByOffset(2)
>>> len(out1)
1
>>> out1[0].step
'D'

>>> out2 = st1.getElementsByOffset(1, 3)
>>> len(out2)
1
>>> out2[0].step
'D'
>>> out3 = st1.getElementsByOffset(1, 3, mustFinishInSpan=True)
>>> len(out3)
0
>>> out4 = st1.getElementsByOffset(1, 2)
>>> len(out4)
1
>>> out4[0].step
'D'
>>> out5 = st1.getElementsByOffset(1, 2, includeEndBoundary=False)
>>> len(out5)
0
>>> out6 = st1.getElementsByOffset(1, 2, includeEndBoundary=False, mustBeginInSpan=False)
>>> len(out6)
1
>>> out6[0].step
'C'
>>> out7 = st1.getElementsByOffset(1, 3, mustBeginInSpan=False)
>>> len(out7)
2
>>> [el.step for el in out7]
['C', 'D']


Note, that elements that end at the start offset are included if mustBeginInSpan is False

>>> out8 = st1.getElementsByOffset(2, 4, mustBeginInSpan=False)
>>> len(out8)
2
>>> [el.step for el in out8]
['C', 'D']

To change this behavior set includeElementsThatEndAtStart=False

>>> out9 = st1.getElementsByOffset(2, 4,
...                     mustBeginInSpan=False, includeElementsThatEndAtStart=False)
>>> len(out9)
1
>>> [el.step for el in out9]
['D']


Note how zeroLengthSearches implicitly set includeElementsThatEndAtStart=False.
These two are the same:

>>> out1 = st1.getElementsByOffset(2, mustBeginInSpan=False)
>>> out2 = st1.getElementsByOffset(2, 2, mustBeginInSpan=False)
>>> len(out1) == len(out2) == 1
True
>>> out1[0] is out2[0] is n2
True

But this is different:

>>> out3 = st1.getElementsByOffset(2, 2.1, mustBeginInSpan=False)
>>> len(out3)
2
>>> out3[0] is n0
True

Explicitly setting includeElementsThatEndAtStart=False does not get the
first note:

>>> out4 = st1.getElementsByOffset(2, 2.1, mustBeginInSpan=False,
...                                includeElementsThatEndAtStart=False)
>>> len(out4)
1
>>> out4[0] is n2
True



Testing multiple zero-length elements with mustBeginInSpan:

>>> tc = clef.TrebleClef()
>>> ts = meter.TimeSignature('4/4')
>>> ks = key.KeySignature(2)
>>> s = stream.Stream()
>>> s.insert(0.0, tc)
>>> s.insert(0.0, ts)
>>> s.insert(0.0, ks)
>>> len(s.getElementsByOffset(0.0, mustBeginInSpan=True))
3
>>> len(s.getElementsByOffset(0.0, mustBeginInSpan=False))
3

OMIT_FROM_DOCS

>>> a = stream.Stream()
>>> n = note.Note('G')
>>> n.quarterLength = 0.5
>>> a.repeatInsert(n, list(range(8)))
>>> b = stream.Stream()
>>> b.repeatInsert(a, [0, 3, 6])
>>> c = b.getElementsByOffset(2, 6.9)
>>> len(c)
2
>>> c = b.flatten().getElementsByOffset(2, 6.9)
>>> len(c)
10


Same test as above, but with floats

>>> out1 = st1.getElementsByOffset(2.0)
>>> len(out1)
1
>>> out1[0].step
'D'
>>> out2 = st1.getElementsByOffset(1.0, 3.0)
>>> len(out2)
1
>>> out2[0].step
'D'
>>> out3 = st1.getElementsByOffset(1.0, 3.0, mustFinishInSpan=True)
>>> len(out3)
0
>>> out3b = st1.getElementsByOffset(0.0, 3.001, mustFinishInSpan=True)
>>> len(out3b)
1
>>> out3b[0].step
'C'
>>> out3b = st1.getElementsByOffset(1.0, 3.001,
...                                 mustFinishInSpan=True, mustBeginInSpan=False)
>>> len(out3b)
1
>>> out3b[0].step
'C'


>>> out4 = st1.getElementsByOffset(1.0, 2.0)
>>> len(out4)
1
>>> out4[0].step
'D'
>>> out5 = st1.getElementsByOffset(1.0, 2.0, includeEndBoundary=False)
>>> len(out5)
0
>>> out6 = st1.getElementsByOffset(1.0, 2.0,
...                                includeEndBoundary=False, mustBeginInSpan=False)
>>> len(out6)
1
>>> out6[0].step
'C'
>>> out7 = st1.getElementsByOffset(1.0, 3.0, mustBeginInSpan=False)
>>> len(out7)
2
>>> [el.step for el in out7]
['C', 'D']

* Changed in v5.5: all arguments changing behavior are keyword only.
)offsetStart	offsetEndrz  r{  r|  rJ  )r   r   r   )	r   r  r  rz  r{  r|  rJ  r  r  s	            rJ   r   Stream.getElementsByOffset  sQ    Z IIK33#1-+*G 4 I	  !44Y?IrI   _beforeNotAtc                 ^  / n[        U5      nUnT R                  5       nU(       a  UR                  U5      nSUl        U Hn  n[        UT R	                  U5      -
  5      nUS:  d  US:X  a  U(       a  T R
                  (       a    O,MH  X:X  a  UR                  X45        Ma  X:  d  Mh  X4/nUnMp     U(       a'  [        UU 4S jS9n	T R                  U	S   5        U	S   $ g)au  
Given an offset, find the element at this offset,
or with the offset less than and nearest to.

Return one element or None if no elements are at or preceded by this
offset.

If the `classList` parameter is used, it should be a
list of class names or strings, and only objects that
are instances of
these classes or subclasses of these classes will be returned.

>>> stream1 = stream.Stream()
>>> x = note.Note('D4')
>>> x.id = 'x'
>>> y = note.Note('E4')
>>> y.id = 'y'
>>> z = note.Rest()
>>> z.id = 'z'

>>> stream1.insert(20, x)
>>> stream1.insert(10, y)
>>> stream1.insert( 0, z)

>>> b = stream1.getElementAtOrBefore(21)
>>> b.offset, b.id
(20.0, 'x')

>>> b = stream1.getElementAtOrBefore(19)
>>> b.offset, b.id
(10.0, 'y')

>>> b = stream1.getElementAtOrBefore(0)
>>> b.offset, b.id
(0.0, 'z')
>>> b = stream1.getElementAtOrBefore(0.1)
>>> b.offset, b.id
(0.0, 'z')


You can give a list of acceptable classes to return, and non-matching
elements will be ignored

>>> c = stream1.getElementAtOrBefore(100, [clef.TrebleClef, note.Rest])
>>> c.offset, c.id
(0.0, 'z')

Getting an object via getElementAtOrBefore sets the activeSite
for that object to the Stream, and thus sets its offset

>>> stream2 = stream.Stream()
>>> stream2.insert(100.5, x)
>>> x.offset
100.5
>>> d = stream1.getElementAtOrBefore(20)
>>> d is x
True
>>> x.activeSite is stream1
True
>>> x.offset
20.0

If no element is before the offset, returns None

>>> s = stream.Stream()
>>> s.insert(10, note.Note('E'))
>>> print(s.getElementAtOrBefore(9))
None

The sort order of returned items is the reverse
of the normal sort order, so that, for instance,
if there's a clef and a note at offset 20,
getting the object before offset 21 will give
you the note, and not the clef, since clefs
sort before notes:

>>> clef1 = clef.BassClef()
>>> stream1.insert(20, clef1)
>>> e = stream1.getElementAtOrBefore(21)
>>> e
<music21.note.Note D>
Fr   c                <   > SU S   -  U S   R                  T5      4$ )Nr   r   rm  	sortTupler  r   s    rJ   <lambda>-Stream.getElementAtOrBefore.<locals>.<lambda>:  s    R!A$Y!t@T4UrI   r!   rm  N)	r   r   r   r>  r  ra   rU   r  r   )
r   rM   r  r  
candidatesnearestTrailSpanr  ro   spanrL   s
   `         rJ   getElementAtOrBeforeStream.getElementAtOrBefore  s    L 
!&>!IIK	!44Y?I',	$ A#FT-?-?-B$BCD axDAI,==)!!4),(#i[
#'   **UVG##GAJ/1:rI   c                "    U R                  XSS9$ )a  
Get element before (and not at) a provided offset.

If the `classList` parameter is used, it should be a
list of class names or strings, and only objects that
are instances of
these classes or subclasses of these classes will be returned.

>>> stream1 = stream.Stream()
>>> x = note.Note('D4')
>>> x.id = 'x'
>>> y = note.Note('E4')
>>> y.id = 'y'
>>> z = note.Rest()
>>> z.id = 'z'
>>> stream1.insert(20, x)
>>> stream1.insert(10, y)
>>> stream1.insert( 0, z)

>>> b = stream1.getElementBeforeOffset(21)
>>> b.offset, b.id
(20.0, 'x')
>>> b = stream1.getElementBeforeOffset(20)
>>> b.offset, b.id
(10.0, 'y')

>>> b = stream1.getElementBeforeOffset(10)
>>> b.offset, b.id
(0.0, 'z')

>>> b = stream1.getElementBeforeOffset(0)
>>> b is None
True
>>> b = stream1.getElementBeforeOffset(0.1)
>>> b.offset, b.id
(0.0, 'z')

>>> w = note.Note('F4')
>>> w.id = 'w'
>>> stream1.insert( 0, w)

This should get w because it was inserted last.

>>> b = stream1.getElementBeforeOffset(0.1)
>>> b.offset, b.id
(0.0, 'w')

But if we give it a lower priority than z then z will appear first.

>>> w.priority = z.priority - 1
>>> b = stream1.getElementBeforeOffset(0.1)
>>> b.offset, b.id
(0.0, 'z')
Tr  )r  )r   rM   r  s      rJ   getElementBeforeOffsetStream.getElementBeforeOffset?  s    D (((NNrI   c                z   Ub  [        U5      nOSnU R                  U5      nU R                  nUc-  U[        U5      S-
  :X  a  gXTS-      nU R	                  U5        U$ [        US-   [        U5      5       HA  nUb$  UR                  XW   R                  5      (       d  M*  XW   nU R	                  U5        Us  $    g)a  
Given an element, get the next element.  If classList is specified,
check to make sure that the element is an instance of the class list

>>> st1 = stream.Stream()
>>> n1 = note.Note('C4')
>>> n2 = note.Note('D4')
>>> r3 = note.Rest()
>>> st1.append([n1, n2, r3])
>>> t2 = st1.getElementAfterElement(n1)
>>> t2 is n2
True
>>> t3 = st1.getElementAfterElement(t2)
>>> t3 is r3
True
>>> t4 = st1.getElementAfterElement(t3)
>>> t4

>>> t5 = st1.getElementAfterElement(n1, [note.Rest])
>>> t5
<music21.note.Rest quarter>
>>> t5 is r3
True
>>> t6 = st1.getElementAfterElement(n1, [note.Rest, note.Note])
>>> t6 is n2
True

>>> t7 = st1.getElementAfterElement(r3)
>>> t7 is None
True


If the element is not in the stream, it will raise a StreamException:

>>> st1.getElementAfterElement(note.Note('C#'))
Traceback (most recent call last):
music21.exceptions21.StreamException:
    cannot find object (<music21.note.Note C#>) in Stream

Nrm  )re  r%  r   r   r   rangerf  ru   )r   rL   r  ru   elPosr   ro   r  s           rJ   getElementAfterElementStream.getElementAfterElement  s    R  9~HH
 

7# ==H))QY'''*519c(m4$(=(=hk>R>R(S(S A++A.H	 5rI   c                  SS jnU R                  [        5      nU(       ar  [        U[        5      (       a%  [        U[        [        R
                  45      (       d  [        S5      e[        R                  " [        [           [        XQU 5      5      $ U" U5      nS nS n[        U[        5      (       a,  [        R                  " U5      u  pU	(       a  U	n[        U5      n[        U[        5      (       a,  [        R                  " U5      u  p*U
(       a  U
n[        U5      nUbm  [        [        XS-   5      5      nU(       a$  U Vs/ s H  oR                  U;   d  M  UPM     nnO[!        U5       VVs/ s H  u  pUS-   U;   d  M  UPM     nnnOUU(       a$  U Vs/ s H  oR                  U:  d  M  UPM     nnO*[!        U5       VVs/ s H  u  pUS-   U:  d  M  UPM     nnnUbw  Un/ nU Hm  nUR                  U:w  a  UR#                  U5        M&  UR$                  (       d  UR#                  U5        MJ  UR$                  U:  d  M\  UR#                  U5        Mo     Ubw  Un/ nU Hm  nUR                  U:w  a  UR#                  U5        M&  UR$                  (       d  UR#                  U5        MJ  UR$                  U::  d  M\  UR#                  U5        Mo     U$ s  snf s  snnf s  snf s  snnf )Nc                    U  H!  n [        UR                  5      nUS:w  d  M!    g   g! [         a    [        SU 35      ef = f)zi
Many people create streams where every number is zero.
This will check for that as quickly as possible.
z)found problematic measure for numbering: r   TF)r   numberr  ro  )measureIteratormmNumbers      rJ   hasMeasureNumberInformationKStream._getMeasureNumberListByStartEnd.<locals>.hasMeasureNumberInformation  sZ    
 %[!!((mG a< %  " [),UVWUX*YZZ[s	   )AzFnumberStart and numberEnd must be integers with indicesNotNumbers=Truerm  )r  z iterator.StreamIterator[Measure]rq  bool)r   rw   r   r   typesNoneTyper  r   r   r   r   r   getNumFromStrre  r  r  r  rU   numberSuffix)r   numberStart	numberEndindicesNotNumbersr  mStreamIterhasUniqueMeasureNumbersstartSuffix	endSuffixstartSuffixTempendSuffixTempmatchingMeasureNumbersr  matchesr  
oldMatchess                   rJ   _getMeasureNumberListByStartEnd&Stream._getMeasureNumberListByStartEnd  s   	 9=8O8OPW8X k3//z)cSXSaSaMb7c7c \  66$w-ki.P)QRR"=k"J
 	k3''+1+?+?+L(K-k*Ki%%'-';';I'F$I)	II  %({M)J%K"&&1XkXXAW5W1kX)2;)? D)?#$q5,B#B )? D '&1MkXX5L1kM)2;)? =)?'(1u'; )? = " JG88{*NN1%NN1%^^{2NN1%     JG88y(NN1%NN1%^^y0NN1%   A YD N=s0   KK5K"K"K(4K(K-K-r  r*  
Instrumentr4  collectgatherSpannersr  c                  [         R                  " [        [           U R	                  SS95      nU nU R                  UUUS9nU(       d  Sn	Sn
OUS   n	U	R                  U5      n
U Hz  nU	b3  U	R                  5       R                  U5      R                  S5      (       a  M;  U	R                  U5      nUc  MQ  U	b  U	R                  S-
  Ul        UR                  SU5        M|     U H(  nUR                  U5      U
-
  nUR                  X5        M*     UR                  5         U(       a,  U[        R                  L nUR!                  UU R"                  S9  U$ )	a  
Get a region of Measures based on a start and end Measure number
where the boundary numbers are both included.

That is, a request for measures 4 through 10 will return 7 Measures, numbers 4 through 10.

Additionally, any number of associated classes can be gathered from the context
and put into the measure.  By default, we collect the Clef, TimeSignature, KeySignature,
and Instrument so that there is enough context to perform.  (See getContextByClass()
and .previous() for definitions of the context)

While all elements in the source are the original elements in the extracted region,
new Measure objects are created and returned.

>>> bachIn = corpus.parse('bach/bwv66.6')
>>> bachExcerpt = bachIn.parts[0].measures(1, 3)
>>> len(bachExcerpt.getElementsByClass(stream.Measure))
3

Because bwv66.6 has a pickup measure, and we requested to start at measure 1,
this is NOT true:

>>> firstExcerptMeasure = bachExcerpt.getElementsByClass(stream.Measure).first()
>>> firstBachMeasure = bachIn.parts[0].getElementsByClass(stream.Measure).first()
>>> firstExcerptMeasure is firstBachMeasure
False
>>> firstBachMeasure.number
0
>>> firstExcerptMeasure.number
1

To get all measures from the beginning, go ahead and always request measure 0 to x,
there will be no error if there is not a pickup measure.

>>> bachExcerpt = bachIn.parts[0].measures(0, 3)
>>> excerptNote = bachExcerpt.getElementsByClass(stream.Measure).first().notes.first()
>>> originalNote = bachIn.parts[0].recurse().notes[0]
>>> excerptNote is originalNote
True

if `indicesNotNumbers` is True, then it ignores defined measureNumbers and
uses 0-indexed measure objects and half-open range.  For instance, if you have a piece
that goes "m1, m2, m3, m4, ..." (like a standard piece without pickups, then
`.measures(1, 3, indicesNotNumbers=True)` would return measures 2 and 3, because
it is interpreted as the slice from object with index 1, which is measure 2 (m1 has
an index of 0) up to but NOT including the object with index 3, which is measure 4.
IndicesNotNumbers is like a Python-slice.

>>> bachExcerpt2 = bachIn.parts[0].measures(0, 2, indicesNotNumbers=True)
>>> for m in bachExcerpt2.getElementsByClass(stream.Measure):
...     print(m)
...     print(m.number)
<music21.stream.Measure 0 offset=0.0>
0
<music21.stream.Measure 1 offset=1.0>
1

If `numberEnd=None` then it is interpreted as the last measure of the stream:

>>> bachExcerpt3 = bachIn.parts[0].measures(7, None)
>>> for m in bachExcerpt3.getElementsByClass(stream.Measure):
...     print(m)
<music21.stream.Measure 7 offset=0.0>
<music21.stream.Measure 8 offset=4.0>
<music21.stream.Measure 9 offset=8.0>

Note that the offsets in the new stream are shifted so that the first measure
in the excerpt begins at 0.0

The measure elements are the same objects as the original:

>>> lastExcerptMeasure = bachExcerpt3.getElementsByClass(stream.Measure).last()
>>> lastOriginalMeasure = bachIn.parts[0].getElementsByClass(stream.Measure).last()
>>> lastExcerptMeasure is lastOriginalMeasure
True

At the beginning of the Stream returned, before the measures will be some additional
objects so that the context is properly preserved:

>>> for thing in bachExcerpt3:
...     print(thing)
P1: Soprano: Instrument 1
<music21.clef.TrebleClef>
f# minor
<music21.meter.TimeSignature 4/4>
<music21.stream.Measure 7 offset=0.0>
<music21.stream.Measure 8 offset=4.0>
<music21.stream.Measure 9 offset=8.0>

Collecting gets the most recent element in the context of the stream:

>>> bachIn.parts[0].insert(10, key.Key('D-'))
>>> bachExcerpt4 = bachIn.parts[0].measures(7, None)
>>> for thing in bachExcerpt4:
...     print(thing)
P1: Soprano: Instrument 1
<music21.clef.TrebleClef>
D- major
...

What is collected is determined by the "collect" iterable.  To collect nothing
send an empty list:

>>> bachExcerpt5 = bachIn.parts[0].measures(8, None, collect=[])
>>> for thing in bachExcerpt5:
...     print(thing)
<music21.stream.Measure 8 offset=0.0>
<music21.stream.Measure 9 offset=4.0>

If a stream has measure suffixes, then Streams having that suffix or no suffix
are returned.

>>> p = stream.Part()
>>> mSuffix3 = stream.Measure(number=3)
>>> mSuffix4 = stream.Measure(number=4)
>>> mSuffix4a = stream.Measure(number=4)
>>> mSuffix4a.numberSuffix = 'a'
>>> mSuffix4b = stream.Measure(number=4)
>>> mSuffix4b.numberSuffix = 'b'
>>> mSuffix5 = stream.Measure(number=5)
>>> mSuffix5a = stream.Measure(number=5)
>>> mSuffix5a.numberSuffix = 'a'
>>> mSuffix6 = stream.Measure(number=6)
>>> p.append([mSuffix3, mSuffix4, mSuffix4a, mSuffix4b, mSuffix5, mSuffix5a, mSuffix6])
>>> suffixExcerpt = p.measures('4b', 6)
>>> suffixExcerpt.show('text')
{0.0} <music21.stream.Measure 4 offset=0.0>
<BLANKLINE>
{0.0} <music21.stream.Measure 4b offset=0.0>
<BLANKLINE>
{0.0} <music21.stream.Measure 5 offset=0.0>
<BLANKLINE>
{0.0} <music21.stream.Measure 5a offset=0.0>
<BLANKLINE>
{0.0} <music21.stream.Measure 6 offset=0.0>
<BLANKLINE>
>>> suffixExcerpt2 = p.measures(3, '4a')
>>> suffixExcerpt2.show('text')
{0.0} <music21.stream.Measure 3 offset=0.0>
<BLANKLINE>
{0.0} <music21.stream.Measure 4 offset=0.0>
<BLANKLINE>
{0.0} <music21.stream.Measure 4a offset=0.0>
<BLANKLINE>

GatherSpanners can change the output:

>>> from music21.common.enums import GatherSpanners
>>> beachIn = corpus.parse('beach')
>>> beachExcerpt = beachIn.measures(3, 4, gatherSpanners=GatherSpanners.ALL)
>>> len(beachExcerpt.spannerBundle)
8
>>> len(beachIn.spannerBundle)
94

* Changed in v7: does not create measures automatically.
* Changed in v7: If `gatherSpanners` is True or GatherSpanners.ALL (default),
  then just the spanners pertaining to the requested measure region
  are provided, rather than the entire bundle from the source.

OMIT_FROM_DOCS

Ensure that layout.StaffGroup objects are present:

>>> for sp in beachExcerpt.spannerBundle.getByClass('StaffGroup'):
...    print(sp)
<music21.layout.StaffGroup <music21.stream.PartStaff P5-Staff1><... P5-Staff2>>
<music21.layout.StaffGroup <music21.stream.Part Soprano I><...Alto II>>

This is in OMIT
measuresr  r  Nrl   r   rm  )requireAllPresentconstrainingSpannerBundle)r   r   rQ   rw   r  r  r  rY   r   r   rN  r  r   r   r   COMPLETE_ONLYcoreGatherMissingSpannersr  )r   r  r  r  r  r  rQ  srcObjr  startMeasurestartOffsetr`  foundr  mOffsetr  s                   rJ   r  Stream.measures9  sb   l FF6'?DOOZO,XY	66/ 7 
 LK"1:L&66v>K I$#++-@@K__`abb !229=E +%1%:%:Q%>EN $$Q. ! A''/+=G  , 
 	%%'!/>3O3O!O//"3*.*<*< 0  rI   r  r  c                   [        U[        5      (       d  US:  a  SnUnUnU(       a  US-  nUS:X  a  SnU R                  UUUS9nU(       a  US   nU R                  U5        U$ g)ad  
Given a measure number, return a single
:class:`~music21.stream.Measure` object if the Measure number exists, otherwise return None.

This method is distinguished from :meth:`~music21.stream.Stream.measures`
in that this method returns a single Measure object, not a Stream containing
one or more Measure objects.

>>> a = corpus.parse('bach/bwv324.xml')
>>> a.parts[0].measure(3)
<music21.stream.Measure 3 offset=8.0>

See :meth:`~music21.stream.Stream.measures` for an explanation of collect and
indicesNotNumbers

To get the last measure of a piece, use -1 as a measureNumber -- this will turn
on indicesNotNumbers if it is off:

>>> a.parts[0].measure(-1)
<music21.stream.Measure 9 offset=38.0>

Getting a non-existent measure will return None:

>>> print(a.parts[0].measure(99))
None

OMIT_FROM_DOCS

>>> sf = a.parts[0].flatten()
>>> sf.measure(2) is None
True
r   Trm  r   Nr  )r   r   r  r   )r   measureNumberr  r  startMeasureNumberendMeasureNumbermatchingMeasuresr  s           rJ   measureStream.measure!  s    J ----!2C $*(!!R'#' ??/ @ 

  #A##A&HrI   fillWithRestsremoveClassesretainVoices	removeAllexemptFromRemovec          	     .  ^^^ U R                  SS9mUc  1 SknO8USL a  [        5       nSnO&[        R                  " U5      (       a  [        U5      nSSS.mUUU4S jnU  GH  nU R	                  USS9nUR
                  (       aj  U(       d  S	UR                  ;  aS  U" 5         UR                  TUUUUS
9n	U[        R                  :w  a  TR                  X5        OTR                  U	5        M  Sn
USL a  Sn
O<UR                  R                  U5      (       a  Sn
OU(       d  S	UR                  ;   a  Sn
U(       a"  UR                  R                  U5      (       a  Sn
U
(       ag  T(       a]  UR                  R                  (       a?  XR                  R                  -   nTS   c  UTS'   UTS'   GMR  UTS   :  a  UTS'   GMc  GMf  GMi  GMl  U" 5         [         R"                  " U5      nU[        R                  :w  a  TR                  X5        GM  TR                  U5        GM     [%        U R'                  [(        5      TR'                  [(        5      SS9 H   u  pTR*                  R-                  X5        M"     U" 5         TR/                  5         T$ )a  
Return a new Stream based on this one, but without the notes and other elements
but keeping instruments, clefs, keys, etc.

Classes to remove are specified in `removeClasses`.

If this Stream contains measures, return a new Stream
with new Measures populated with the same characteristics of those found in this Stream.

>>> b = corpus.parse('bwv66.6')
>>> sopr = b.parts[0]
>>> soprEmpty = sopr.template()
>>> soprEmpty.show('text')
{0.0} <music21.instrument.Instrument 'P1: Soprano: Instrument 1'>
{0.0} <music21.stream.Measure 0 offset=0.0>
    {0.0} <music21.clef.TrebleClef>
    {0.0} <music21.tempo.MetronomeMark Quarter=96 (playback only)>
    {0.0} <music21.key.Key of f# minor>
    {0.0} <music21.meter.TimeSignature 4/4>
    {0.0} <music21.note.Rest quarter>
{1.0} <music21.stream.Measure 1 offset=1.0>
    {0.0} <music21.note.Rest whole>
{5.0} <music21.stream.Measure 2 offset=5.0>
    {0.0} <music21.note.Rest whole>
{9.0} <music21.stream.Measure 3 offset=9.0>
    {0.0} <music21.layout.SystemLayout>
    {0.0} <music21.note.Rest whole>
{13.0} <music21.stream.Measure 4 offset=13.0>
...


Really make empty with `fillWithRests=False`

>>> alto = b.parts[1]
>>> altoEmpty = alto.template(fillWithRests=False)
>>> altoEmpty.show('text')
{0.0} <music21.instrument.Instrument 'P2: Alto: Instrument 2'>
{0.0} <music21.stream.Measure 0 offset=0.0>
    {0.0} <music21.clef.TrebleClef>
    {0.0} <music21.tempo.MetronomeMark Quarter=96 (playback only)>
    {0.0} <music21.key.Key of f# minor>
    {0.0} <music21.meter.TimeSignature 4/4>
{1.0} <music21.stream.Measure 1 offset=1.0>
<BLANKLINE>
{5.0} <music21.stream.Measure 2 offset=5.0>
<BLANKLINE>
{9.0} <music21.stream.Measure 3 offset=9.0>
    {0.0} <music21.layout.SystemLayout>
...


`removeClasses` can be a list or set of classes to remove.  By default it is
['GeneralNote', 'Dynamic', 'Expression']

>>> tenor = b.parts[2]
>>> tenorNoClefsSignatures = tenor.template(fillWithRests=False,
...       removeClasses=['Clef', 'KeySignature', 'TimeSignature', 'Instrument'])
>>> tenorNoClefsSignatures.show('text')
{0.0} <music21.stream.Measure 0 offset=0.0>
    {0.0} <music21.tempo.MetronomeMark Quarter=96 (playback only)>
    {0.0} <music21.note.Note A>
    {0.5} <music21.note.Note B>
{1.0} <music21.stream.Measure 1 offset=1.0>
    {0.0} <music21.note.Note C#>
    {1.0} <music21.note.Note B>
    {2.0} <music21.note.Note A>
    {3.0} <music21.note.Note B>
{5.0} <music21.stream.Measure 2 offset=5.0>
...

Setting removeAll to True removes everything that is not a Stream:
(this was removeClasses=True; in v9.9 both removeClasses=True or removeAll=True
is allowed; in v10 only removeAll=True will be allowed).

>>> bass = b.parts[3]
>>> bassEmpty = bass.template(fillWithRests=False, removeAll=True)
>>> bassEmpty.show('text')
{0.0} <music21.stream.Measure 0 offset=0.0>
<BLANKLINE>
{1.0} <music21.stream.Measure 1 offset=1.0>
<BLANKLINE>
{5.0} <music21.stream.Measure 2 offset=5.0>
<BLANKLINE>
{9.0} <music21.stream.Measure 3 offset=9.0>
<BLANKLINE>
{13.0} <music21.stream.Measure 4 offset=13.0>
<BLANKLINE>
...

`exemptFromRemove` should be a set of classes (or class strings) that
are exempted from removal by removeAll or removeClasses

>>> bass = b.parts[3]
>>> bassEmpty = bass.template(fillWithRests=False, removeAll=True,
...                           exemptFromRemove={meter.TimeSignature})
>>> bassEmpty[meter.TimeSignature].first()
<music21.meter.TimeSignature 4/4>
>>> print(bassEmpty[clef.Clef].first())
None

Here is an example of exerpts of what calling template() with
default values on the whole score looks like.

>>> b.template().show('text')
{0.0} <music21.metadata.Metadata object at 0x106151940>
{0.0} <music21.stream.Part Soprano>
    {0.0} <music21.instrument.Instrument 'P1: Soprano: Instrument 1'>
    {0.0} <music21.stream.Measure 0 offset=0.0>
        {0.0} <music21.clef.TrebleClef>
        {0.0} <music21.tempo.MetronomeMark Quarter=96 (playback only)>
        {0.0} <music21.key.Key of f# minor>
        {0.0} <music21.meter.TimeSignature 4/4>
        {0.0} <music21.note.Rest quarter>
    {1.0} <music21.stream.Measure 1 offset=1.0>
        {0.0} <music21.note.Rest whole>
        ...
    {33.0} <music21.stream.Measure 9 offset=33.0>
        {0.0} <music21.note.Rest dotted-half>
        {3.0} <music21.bar.Barline type=final>
{0.0} <music21.stream.Part Alto>
    {0.0} <music21.instrument.Instrument 'P2: Alto: Instrument 2'>
    {0.0} <music21.stream.Measure 0 offset=0.0>
        {0.0} <music21.clef.TrebleClef>
        {0.0} <music21.tempo.MetronomeMark Quarter=96 (playback only)>
        {0.0} <music21.key.Key of f# minor>
        {0.0} <music21.meter.TimeSignature 4/4>
        {0.0} <music21.note.Rest quarter>
    {1.0} <music21.stream.Measure 1 offset=1.0>
        {0.0} <music21.note.Rest whole>
    ...
    {33.0} <music21.stream.Measure 9 offset=33.0>
        {0.0} <music21.note.Rest dotted-half>
        {3.0} <music21.bar.Barline type=final>
{0.0} <music21.layout.StaffGroup ...>


If `retainVoices` is False (default True) then Voice streams are treated
differently from all other Streams and are removed.  All elements in the
voice are removed even if they do not match the `classList`:

>>> p = stream.Part(id='part0')
>>> m1 = stream.Measure(number=1)
>>> v1 = stream.Voice(id='voice1')
>>> v1.insert(0, note.Note('E', quarterLength=4.0))
>>> v2 = stream.Voice(id='voice2')
>>> v2.insert(0, note.Note('G', quarterLength=2.0))
>>> m1.insert(0, v1)
>>> m1.insert(0, v2)
>>> m2 = stream.Measure(number=2)
>>> m2.insert(0, note.Note('D', quarterLength=4.0))
>>> p.append([m1, m2])
>>> pt = p.template(retainVoices=False)
>>> pt.show('text')
{0.0} <music21.stream.Measure 1 offset=0.0>
    {0.0} <music21.note.Rest whole>
{4.0} <music21.stream.Measure 2 offset=4.0>
    {0.0} <music21.note.Rest whole>
>>> pt[0][0].quarterLength
4.0

Developer note -- if you just want a copy of a Score with new
Part and Measure objects, but you don't care that the notes, etc.
inside are the same objects as the original (i.e., you do not
plan to manipulate them, or you want the manipulations to
return to the original objects), using .template() is several
times faster than a deepcopy of the stream (about 4x faster
on bwv66.6)

* Changed in v7: all arguments are keyword only.
* New in v9.9: added exemptFromRemove
* Note: in v10
templater  N>   Dynamic
ExpressionGeneralNoteT)rM   rN   c                    > T(       d  g TS   c  g TS   TS   -
  n [         R                  " U S9nTR                  TS   U5        S TS'   S TS'   g )NrM   rN   r  )r%   r  r   )restQLrestObjr  r  restInfos     rJ   optionalAddRest(Stream.template.<locals>.optionalAddRest  s`     !)i(8H+==Fiif5GNN8H-w7!%HX"&HYrI   r  r,  r  FrM   rN   )strict)r  re  r   r   r  rt   r/  r  r   r!  r   rg  ru   rf  r   r  r  r  zipr   rw   r  r  r   )r   r  r  r  r  r  r  r   elOffsetoutElskip_elementrN   elNewoldMnewMr  r  s    `             @@rJ   r  Stream.template[  sT   h oozo: DMd"EMI}--.M"t4
	' B))"D)AH {{

1J!-2?1=.75E	 $ G
 }333NN83 &&u- !LD #))-88#!g&<#BKK$<$<=M$N$N$ !R[[%>%>&)B)BBG)1-5*.5+ 8I#66.5+ 7 &?=  !b)}333NN83&&u-_ f ##G,""7+
JD
 33D?
 	!
rI   c                   0 n[         U;   d  SU;   aI  U R                  [         5       H0  nU R                  U5      nXB;  a  / X$'   X$   R                  U5        M2     U Hw  nU[         S4;   a  M  U R                  U5       HQ  nUR	                  [         5      nUc  M  UnUR
                  nXB;  a  / X$'   X2U   ;  d  M>  X$   R                  U5        MS     My     [        [        UR                  5       S S95      nU$ )a  
If this Stream contains Measures, returns an OrderedDict
whose keys are the offsets of the start of each measure
and whose values are a list of references
to the :class:`~music21.stream.Measure` objects that start
at that offset.

Even in normal music there may be more than
one Measure starting at each offset because each
:class:`~music21.stream.Part` might define its own Measure.
However, you are unlikely to encounter such things unless you
run Score.flatten(retainContainers=True).

The offsets are always measured relative to the
calling Stream (self).

You can specify a `classFilterList` argument as a list of classes
to find instead of Measures.  But the default will of course
find Measure objects.

Example 1: This Bach chorale is in 4/4 without a pickup, so
as expected, measures are found every 4 offsets, until the
weird recitation in m. 7 which in our edition lasts 10 beats
and thus causes a gap in measureOffsetMap from 24.0 to 34.0.

.. image:: images/streamMeasureOffsetMapBWV324.*
    :width: 572


>>> chorale = corpus.parse('bach/bwv324.xml')
>>> alto = chorale.parts['#alto']
>>> altoMeasures = alto.measureOffsetMap()
>>> altoMeasures
OrderedDict([(0.0, [<music21.stream.Measure 1 offset=0.0>]),
             (4.0, [<music21.stream.Measure 2 offset=4.0>]),
             (8.0, [<music21.stream.Measure 3 offset=8.0>]),
             ...
             (38.0, [<music21.stream.Measure 9 offset=38.0>])])
>>> list(altoMeasures.keys())
[0.0, 4.0, 8.0, 12.0, 16.0, 20.0, 24.0, 34.0, 38.0]

altoMeasures is a dictionary of the measures
that are found in the alto part, so we can get
the measure beginning on offset 4.0 (measure 2)
and display it (though it's the only measure
found at offset 4.0, there might be others as
in example 2, so we need to call altoMeasures[4.0][0]
to get this measure.):

>>> altoMeasures[4.0]
[<music21.stream.Measure 2 offset=4.0>]
>>> altoMeasures[4.0][0].show('text')
{0.0} <music21.note.Note D>
{1.0} <music21.note.Note D#>
{2.0} <music21.note.Note E>
{3.0} <music21.note.Note F#>

Example 2: How to get all the measures from all parts (not the
most efficient way, but it works!):

>>> mom = chorale.measureOffsetMap()
>>> mom
OrderedDict([(0.0, [<music21.stream.Measure 1 offset=0.0>,
                    <music21.stream.Measure 1 offset=0.0>,
                    <music21.stream.Measure 1 offset=0.0>,
                    <music21.stream.Measure 1 offset=0.0>]),
              (4.0, [<music21.stream.Measure 2 offset=4.0>,
                     ...])])
>>> for measure_obj in mom[8.0]:
...     print(measure_obj, measure_obj.getContextByClass(stream.Part).id)
<music21.stream.Measure 3 offset=8.0> Soprano
<music21.stream.Measure 3 offset=8.0> Alto
<music21.stream.Measure 3 offset=8.0> Tenor
<music21.stream.Measure 3 offset=8.0> Bass

Changed in v9: classFilterList must be a list or tuple of strings or Music21Objects

OMIT_FROM_DOCS

see important examples in testMeasureOffsetMap() and
testMeasureOffsetMapPostTie()
rw   c                    U S   $ r/  rC   r  s    rJ   r  )Stream.measureOffsetMap.<locals>.<lambda>  	    qQRtrI   r!   )	rw   r   r  rU   rN  rM   r   r  items)	r   rh  	offsetMapr  rM   r`  ro   maybe_morderedOffsetMaps	            rJ   measureOffsetMapStream.measureOffsetMape  s   r :<	 o%o)E,,W5++A.*(*I%!((+ 6 )IWi00,,Y7 --g6? *(*I%f--%,,Q/% 8 ). 'vioo.?^'TUrI   c                b   U R                  5       (       a;  / nU R                  S5       H"  nUR                  UR                  5       5        M$     U$ U R	                  5       (       a-  U R                  [
        5      R                  5       R                  $ [        U S5      (       a  U R                  $ g NrQ   rightBarline)	hasPartLikeStreamsr   rU   _getFinalBarlinehasMeasuresrw   r   r)  rT  )r   r  ps      rJ   r+  Stream._getFinalBarline  s    ""$$D,,X6A..01 7K !!..w7<<>KKK~..(((rI   c                   U R                  5       (       ac  [        R                  " U5      (       d  U/n[        U R	                  S5      5       H&  u  p#X[        U5      -     nUR                  U5        M(     g U R                  5       (       a(  XR	                  [        5      R                  5       l
        g [        U S5      (       a  Xl
        g g r(  )r*  r   r~  r  r   r   _setFinalBarliner,  rw   r   r)  rT  )r   r  r  r-  bls        rJ   r0  Stream._setFinalBarline  s    ""$$$$U++!$"9"9("CDs5z>*""2&	 E
  CH##G,113@T>** % +rI   aM  
        Get or set the final barline of this Stream's Measures,
        if and only if there are Measures defined as elements in this Stream.
        This method will not create Measures if none exist.

        >>> p = stream.Part()
        >>> m1 = stream.Measure()
        >>> m1.rightBarline = bar.Barline('double')
        >>> p.append(m1)
        >>> p.finalBarline
        <music21.bar.Barline type=double>
        >>> m2 = stream.Measure()
        >>> m2.rightBarline = bar.Barline('final')
        >>> p.append(m2)
        >>> p.finalBarline
        <music21.bar.Barline type=final>

        This property also works on Scores that contain one or more Parts.
        In that case a list of barlines can be used to set the final barline.

        >>> s = corpus.parse('bwv66.6')
        >>> s.finalBarline
        [<music21.bar.Barline type=final>,
         <music21.bar.Barline type=final>,
         <music21.bar.Barline type=final>,
         <music21.bar.Barline type=final>]

        >>> s.finalBarline = 'none'
        >>> s.finalBarline
        [<music21.bar.Barline type=none>,
         <music21.bar.Barline type=none>,
         <music21.bar.Barline type=none>,
         <music21.bar.Barline type=none>]


        Getting or setting a final barline on a Measure (or another Stream
        with a rightBarline attribute) is the same as getting or setting the rightBarline.

        >>> m = stream.Measure()
        >>> m.finalBarline is None
        True
        >>> m.finalBarline = 'final'
        >>> m.finalBarline
        <music21.bar.Barline type=final>
        >>> m.rightBarline
        <music21.bar.Barline type=final>

        Getting on a generic Stream, Voice, or Opus always returns a barline of None,
        and setting on a generic Stream, Voice, or Opus always returns None:

        >>> s = stream.Stream()
        >>> s.finalBarline is None
        True
        >>> s.finalBarline = 'final'
        >>> s.finalBarline is None
        True

        * Changed in v6.3: does not raise an exception if queried or set on a measure-less stream.
          Previously raised a StreamException
        docc                $    U R                  S5      $ )z
Return all :class:`~music21.stream.Voices` objects
in an iterator

>>> s = stream.Stream()
>>> s.insert(0, stream.Voice())
>>> s.insert(0, stream.Voice())
>>> len(s.voices)
2
r,  r   r   s    rJ   voicesStream.voicesD  s     &&w//rI   c                D    SSK Jn  U R                  UR                  5      $ )a   
Return all :class:`~music21.spanner.Spanner` objects
(things such as Slurs, long trills, or anything that
connects many objects)
in an Iterator

>>> s = stream.Stream()
>>> s.insert(0, spanner.Slur())
>>> s.insert(0, spanner.Slur())
>>> len(s.spanners)
2
r   r4   )r?  r5   r   r(  )r   r5   s     rJ   spannersStream.spannersR  s     	$&&w77rI   c                    U R                   $ )aj  
Get or set the atSoundingPitch status, that is whether the
score is at concert pitch or may have transposing instruments
that will not sound as notated.

Valid values are True, False, and 'unknown'.

Note that setting "atSoundingPitch" does not actually transpose the notes. See
`toSoundingPitch()` for that information.

>>> s = stream.Stream()
>>> s.atSoundingPitch = True
>>> s.atSoundingPitch = False
>>> s.atSoundingPitch = 'unknown'
>>> s.atSoundingPitch
'unknown'
>>> s.atSoundingPitch = 'junk'
Traceback (most recent call last):
music21.exceptions21.StreamException: not a valid at sounding pitch value: junk
)r~   r   s    rJ   atSoundingPitchStream.atSoundingPitchf  s    , $$$rI   c                8    US;   a  Xl         g [        SU 35      e)N)TFrj   z%not a valid at sounding pitch value: )r~   ro  r   r  s     rJ   r=  r>  ~  s$    ,,$)!!$I%"QRRrI   )reversetransposeKeySignaturepreserveAccidentalDisplayc                   g rs   rC   r   rA  rB  rC  inPlaces        rJ   _transposeByInstrumentStream._transposeByInstrument       	rI   )rA  rB  rC  rF  c                   g rs   rC   rE  s        rJ   rG  rH    rI  rI   c          	        U(       d  [         R                  " U 5      nOU nUR                  SS9n0 nU H  nUR                  R                  Xx'   M     UR                  Ul        UR                  SSS9nU(       a0  [        R                  [        R                  [        R                  4n	O [        R                  [        R                  4n	U R                  5       n
U H  nUR                  c  M  UR                  nXR                  -   nUR                  5       R!                  UUSSSS9R#                  5       nUR                  nU(       a  UR%                  5       nU(       a9  U
(       a2  [&        R(                  " U5         UR+                  USU	S9  SSS5        M  UR+                  USU	S9  M     UR-                  5        H  u  pXR                  l        M     U$ ! , (       d  f       GM  = f)	a8  
Transpose the Stream according to each instrument's transposition.

If reverse is False, the transposition will happen in the direction
opposite of what is specified by the Instrument. for instance,
for changing a concert score to a transposed score or for
extracting parts.

TODO: Fill out -- expose publicly?
Tr$  r  FrF  Nry  )rF  rh  )r  r  getInstrumentsr   r  extendDurationr%   r  r   r  r"   r4  haveAccidentalsBeenMadetranspositionrM   flattenr   r6   rA  r-   saveAccidentalDisplayStatusr]   r!  )r   rA  rB  rC  rF  rQ  instrument_streaminstrument_mapinstrh  displayStatusesAreSetr   endfocustransoriginal_qls                   rJ   rG  rH    s   $ d+II%44T4B@B%D#'==#>#>N  &
 &/%7%7" .<<\SX<Y !#yy%++s7G7GHO#yy%++6O&*&B&B&D%D!!)KKE,,,C%--/CC#(!& $ D &
 '-fh  &&E(-B!==eDOOE4OY ED t_U' &, "0!5!5!7D*5MM' "8  EDs   ?G
G+	c                   U R                   nU R                   S:X  aU  U R                  5        H@  nUR                  nUR                  (       d  M"  UR                   S:w  d  M4  UR                   n  O   gU R	                  SSS9 Ha  nUR                   S:X  a  M  UR                   SL a  USL a  UR                  SS9  M:  UR                   SL d  MK  USL d  MR  UR                  SS9  Mc     U$ )a  
`atSoundingPitch` might be True, False, or 'unknown'. Given that
setting the property does not automatically synchronize the corresponding
property on contained or containing streams, any time a method relying on the
value of `atSoundingPitch` such as :meth:`toSoundingPitch` visits a stream,
it will need to resolve 'unknown' values or even possibly conflicting values.

If this stream's `.atSoundingPitch` is 'unknown', this helper method searches
this stream's sites until a True or False
value for `.atSoundingPitch` is found, since it is possible a user only manipulated
the value on the top-level stream.

Then, contained streams are searched to verify that they do not contain
conflicting values (i.e. .atSoundingPitch = True when the container has
.atSoundingPitch = False). Conflicting values are resolved by converting
the inner streams to written or sounding pitch as necessary to match this
stream's value.
rj   TFr  rL  )r=  contextSitessitert   rY   toSoundingPitchtoWrittenPitch)r   at_soundingcontextTupler]  	substreams        rJ   _treatAtSoundingPitchStream._treatAtSoundingPitch  s    & **9, $ 1 1 3 $((===T%9%9Y%F"&"6"6K !4 !$EJI((I5((E1kT6I))$)7**d2{e7K(((6 K rI   )rC  rF  c                  SSK Jn  U(       d  U R                  S5      nOU nUR                  5       (       d  SUR                  ;   a>  UR                  [        5       H  nUR                  SUS9  M     SUl        U(       d  U$ S$ UR                  5       nUSL a/  UR                  SUSS	9  UR                  SSS
9 H
  nSUl        M     XCR                      H  nUR                  5         M     U(       d  U$ g)a  
If not at sounding pitch, transpose all Pitch
elements to sounding pitch. The atSoundingPitch property
is used to determine if transposition is necessary.

Affected by the presence of Instruments and by Ottava spanners

>>> sc = stream.Score()
>>> p = stream.Part(id='barisax')
>>> p.append(instrument.BaritoneSaxophone())
>>> m = stream.Measure(number=1)
>>> m.append(note.Note('A4'))
>>> p.append(m)
>>> sc.append(p)
>>> sc.atSoundingPitch = False

>>> scSounding = sc.toSoundingPitch()
>>> scSounding.show('text')
{0.0} <music21.stream.Part barisax>
    {0.0} <music21.instrument.BaritoneSaxophone 'Baritone Saxophone'>
    {0.0} <music21.stream.Measure 1 offset=0.0>
        {0.0} <music21.note.Note C>

>>> scSounding.atSoundingPitch
True
>>> scSounding.parts[0].atSoundingPitch
True
>>> scSounding.recurse().notes[0].nameWithOctave
'C3'

If 'atSoundingPitch' is unknown for this Stream and all of its parent Streams
then no transposition will take place, and atSoundingPitch will remain unknown
(this used to raise an exception):

>>> s = stream.Score()
>>> p = stream.Part(id='partEmpty')
>>> s.insert(0.0, p)
>>> p.toSoundingPitch()
<music21.stream.Part partEmpty>
>>> s.atSoundingPitch = False
>>> sp = p.toSoundingPitch()
>>> sp
<music21.stream.Part partEmpty>
>>> sp.atSoundingPitch
'unknown'
>>> sp.derivation.origin is p
True

* Changed in v2.0.10: inPlace is False
* Changed in v5: returns None if inPlace=True
* Changed in v9: no transposition instead of exception if atSoundingPitch is 'unknown'
r   r4   r^  OpusT)rF  rC  NFrA  rC  rF  r  )r?  r5   rL  r*  ru   r   rQ   r^  r=  rc  rG  rY   OttavaperformTransposition)	r   rC  rF  r5   rQ  partLiker`  r"  ottavas	            rJ   r^  Stream.toSoundingPitch  s	   v 	$112CDII''))Vy7I7I-I%88@(( .G )  A )-I%$+955557%,,*C - 
 '..4T.R	,0	) S  /F'') 0  rI   )ottavasToSoundingrC  rF  c                  SSK Jn  U(       d  U R                  S5      nOU nUR                  5       (       d  SUR                  ;   a]  UR                  S5       H@  n[        R                  (       a  [        U[        5      (       d   eUR                  SUUS9  MB     SUl        ODUR                  5       nUSL a/  UR                  SUSS	9  UR                  SSS
9 H
  nSUl        M     U(       a&  XTR                      H  n	U	R!                  5         M     O%XTR                      H  n	U	R#                  5         M     U(       d  U$ g)a  
If not at written pitch, transpose all Pitch elements to
written pitch. The atSoundingPitch property is used to
determine if transposition is necessary.  Note that if
ottavasToSounding is True, any notes/chords within
an Ottava will _then_ be transposed to sounding Pitch (this
is useful for the MusicXML writer, since MusicXML likes
all pitches to be written pitches, except for those in
ottavas, which should be transposed to written (by instrument)
and then transposed to sounding (by ottava).

>>> sc = stream.Score()
>>> p = stream.Part(id='baritoneSax')
>>> p.append(instrument.BaritoneSaxophone())
>>> m = stream.Measure(number=1)
>>> m.append(note.Note('C3'))
>>> p.append(m)
>>> sc.append(p)
>>> sc.atSoundingPitch = True
>>> scWritten = sc.toWrittenPitch()
>>> scWritten.show('text')
{0.0} <music21.stream.Part baritoneSax>
    {0.0} <music21.instrument.BaritoneSaxophone 'Baritone Saxophone'>
    {0.0} <music21.stream.Measure 1 offset=0.0>
        {0.0} <music21.note.Note A>
>>> scWritten.atSoundingPitch
False
>>> scWritten.parts[0].atSoundingPitch
False
>>> scWritten.recurse().notes[0].nameWithOctave
'A4'

* Changed in v3: `inPlace` defaults to `False`
* Changed in v5 returns `None` if `inPlace=True`
* Changed in v9: no transposition instead of exception if atSoundingPitch is 'unknown'
r   r4   r_  rf  rQ   T)rF  rm  rC  Frg  r  N)r?  r5   rL  r*  r/  r   r   r  r   rQ   r_  r=  rc  rG  rY   rh  ri  undoTransposition)
r   rm  rC  rF  r5   rQ  rj  r`  r"  rk  s
             rJ   r_  Stream.toWrittenPitchi  s<   V 	$112BCII''))Vy7H7H-H%88B??%h7777'' &7.G ( 	 C ).I%#99;Kd"00 .G  1 
 "+!2!2tQU!2!VI05I- "W #NN3++- 4 $NN3((* 4  rI   )rE  rI  rY   sortByCreationTimec                  U(       a"  U [         R                     R                  5       nO-U R                  [         R                  5      R                  5       nSUR                  l        U(       d+  U(       a$  U R                  SUS9nUb  UR                  U5        U(       ae  U(       a  US   R                  S:  aK  [         R                  " [        R                  < S[        R                  < 35      nUR                  SU5        U$ )aj  
Collect all :class:`~music21.meter.TimeSignature` objects in this stream.
If no TimeSignature objects are defined, get a default (4/4 or whatever
is defined in the defaults.py file).

>>> s = stream.Part(id='changingMeter')
>>> s.repeatInsert(note.Note('C#'), list(range(11)))

>>> threeFour = meter.TimeSignature('3/4')
>>> s.insert(0.0, threeFour)
>>> twoTwo = meter.TimeSignature('2/2')
>>> s.insert(3.0, twoTwo)
>>> tsStream = s.getTimeSignatures()
>>> tsStream.derivation.method
'getTimeSignatures'

>>> tsStream
<music21.stream.Part changingMeter>
>>> tsStream.show('text')
{0.0} <music21.meter.TimeSignature 3/4>
{3.0} <music21.meter.TimeSignature 2/2>

The contents of the time signature stream are the original, not copies
of the original:

>>> tsStream[0] is threeFour
True

Many time signatures are found within measures, so this method will find
them also and place them at the appropriate point within the overall Stream.

N.B. if there are different time signatures for different parts, this method
will not distinguish which parts use which time signatures.

>>> sm = s.makeMeasures()
>>> sm.show('text')
{0.0} <music21.stream.Measure 1 offset=0.0>
    {0.0} <music21.clef.TrebleClef>
    {0.0} <music21.meter.TimeSignature 3/4>
    {0.0} <music21.note.Note C#>
    {1.0} <music21.note.Note C#>
    {2.0} <music21.note.Note C#>
{3.0} <music21.stream.Measure 2 offset=3.0>
    {0.0} <music21.meter.TimeSignature 2/2>
    {0.0} <music21.note.Note C#>
    {1.0} <music21.note.Note C#>
    {2.0} <music21.note.Note C#>
    {3.0} <music21.note.Note C#>
{7.0} <music21.stream.Measure 3 offset=7.0>
    {0.0} <music21.note.Note C#>
    {1.0} <music21.note.Note C#>
    {2.0} <music21.note.Note C#>
    {3.0} <music21.note.Note C#>
    {4.0} <music21.bar.Barline type=final>

>>> tsStream2 = sm.getTimeSignatures()
>>> tsStream2.show('text')
{0.0} <music21.meter.TimeSignature 3/4>
{3.0} <music21.meter.TimeSignature 2/2>

If you do not want this recursion, set recurse=False

>>> len(sm.getTimeSignatures(recurse=False, returnDefault=False))
0

We set returnDefault=False here, because otherwise a default time signature
of 4/4 is returned:

>>> sm.getTimeSignatures(recurse=False)[0]
<music21.meter.TimeSignature 4/4>

Note that a measure without any time signature can still find a context TimeSignature
with this method so long as searchContext is True (as by default):

>>> m3 = sm.measure(3)
>>> m3.show('text')
{0.0} <music21.note.Note C#>
{1.0} <music21.note.Note C#>
{2.0} <music21.note.Note C#>
{3.0} <music21.note.Note C#>
{4.0} <music21.bar.Barline type=final>

>>> m3.getTimeSignatures()[0]
<music21.meter.TimeSignature 2/2>

The oldest context for the measure will be used unless sortByCreationTime is False, in which
case the typical order of context searching will be used.

>>> p2 = stream.Part()
>>> p2.insert(0, meter.TimeSignature('1/1'))
>>> p2.append(m3)
>>> m3.getTimeSignatures()[0]
<music21.meter.TimeSignature 2/2>

If searchContext is False then the default will be returned (which is somewhat
acceptable here, since there are 4 quarter notes in the measure) but not generally correct:

>>> m3.getTimeSignatures(searchContext=False)[0]
<music21.meter.TimeSignature 4/4>


* Changed in v8: time signatures within recursed streams are found by default.
  Added recurse. Removed option for recurse=False and still getting the
  first time signature in the first measure.  This was wholly inconsistent.
rM  r*  )rq  r   rl   /)r$   r*  r6   r   r   rO  rN  rU   rM   r   meterNumeratormeterDenominatorBeatTyperV   )r   rE  rI  rY   rq  r  r\  tss           rJ   rM  Stream.getTimeSignatures  s    ` ++,335D**5+>+>?FFHD!4 ((M_(`C C  47>>C/((H4K4K4<4U4U*W XAr"rI   searchActiveSiterI  rY   c                  SnU(       d  U R                  5       nOU R                  5       n[        R                  " [        [
        R                     UR                  [
        R                  5      R                  5       5      nU(       a  UR                  5       nOmU(       af  U R                  bY  U R                  R                  (       a>  U R                  U La/  U R                  R                  USS9nUb  UR                  SU5        U(       a7  Uc4  SSKJn  U" 5       n[        R                   Ul        UR                  SU5        U$ )a   
Search this stream (and, by default, its subStreams) or activeSite streams for
:class:`~music21.instrument.Instrument` objects, and return a new stream
containing them.

>>> m1 = stream.Measure([meter.TimeSignature('4/4'),
...                      instrument.Clarinet(),
...                      note.Note('C5', type='whole')])
>>> m2 = stream.Measure([instrument.BassClarinet(),
...                      note.Note('C3', type='whole')])
>>> p = stream.Part([m1, m2])
>>> instruments = p.getInstruments()
>>> instruments
<music21.stream.Part 0x112ac26e0>

>>> instruments.show('text')
{0.0} <music21.instrument.Clarinet 'Clarinet'>
{4.0} <music21.instrument.BassClarinet 'Bass clarinet'>

If there are no instruments, returns a Stream containing a single default `Instrument`,
unless returnDefault is False.

>>> p = stream.Part()
>>> m = stream.Measure([note.Note()])
>>> p.insert(0, m)
>>> instrumentStream = p.getInstruments(returnDefault=True)
>>> defaultInst = instrumentStream.first()
>>> defaultInst
<music21.instrument.Instrument ': '>

Insert an instrument into the Part (not the Measure):

>>> p.insert(0, instrument.Koto())

Searching the measure will find this instrument only if the measure's activeSite is
searched, as it is by default:

>>> searchedActiveSite = p.measure(1).getInstruments()
>>> searchedActiveSite.first()
<music21.instrument.Koto 'Koto'>

>>> searchedNaive = p.measure(1).getInstruments(searchActiveSite=False, returnDefault=False)
>>> len(searchedNaive)
0

* Changed in v8: recurse is True by default.
NF)ry  rI  r   r  )r   rY   r   r   rQ   r    r  r   r6   r   r  rt   getInstrumentrV   music21.instrumentr   partName)r   ry  rI  rY   instObjr  r  r  s           rJ   rM  Stream.getInstrumentsE  s    h /3IIKELLNEvvfZ223..z/D/DELLN jjlGOO/00 OO47 #oo;;)9&+ < -G *Aw/ W_5 lG  (00GKK7# rI   c               D    U R                  UUUS9nUR                  5       $ )aG  
Return the first Instrument found in this Stream, or None.

>>> s = stream.Score()
>>> p1 = stream.Part()
>>> p1.insert(instrument.Violin())
>>> m1p1 = stream.Measure()
>>> m1p1.append(note.Note('g'))
>>> p1.append(m1p1)

>>> p2 = stream.Part()
>>> p2.insert(instrument.Viola())
>>> m1p2 = stream.Measure()
>>> m1p2.append(note.Note('f#'))
>>> p2.append(m1p2)

>>> s.insert(0, p1)
>>> s.insert(0, p2)
>>> p1.getInstrument(returnDefault=False).instrumentName
'Violin'
>>> p2.getInstrument(returnDefault=False).instrumentName
'Viola'

* Changed in v7: added `recurse` (default False)
rx  )rM  r   )r   ry  rI  rY   r  s        rJ   r|  Stream.getInstrument  s1    < /3.A.A-' /B /

 zz|rI   C4rL  c               T   U(       a  U nOU R                  S5      nUR                  5       R                  [        R                  5      nSnU(       d  [        R
                  " S5      nO[        U5      S:X  a  US   nOSnSnUR                  R                  nU[        R                      H  nSU-  UR                  R                  -
  UR                  l        U(       a5  UR                  UR                  R                  5      UR                  l        OSUR                  [        R                  5      n	U	R                  UR                  R                  5      UR                  l        UR                  R                  c  M  SUR                  R                  l        M     U(       d  U$ g)	a  
inverts a stream diatonically around the given note (by default, middle C)

For pieces where the key signature
does not change throughout the piece it is MUCH faster than
for pieces where the key signature changes.

Here in this test, we put Ciconia's Quod Jactatur (a single voice
piece that should have a canon solution: see trecento.quodJactatur)
into 3 flats (instead of its original 1 flat) in measure 1, but
into 5 sharps in measure 2 and then invert around F4, creating
a new piece.

>>> qj = corpus.parse('ciconia/quod_jactatur').parts[0].measures(1, 2)
>>> qj.id = 'measureExcerpt'

>>> qj.show('text')
{0.0} <music21.instrument.Piano 'P1: MusicXML Part: Grand Piano'>
{0.0} <music21.stream.Measure 1 offset=0.0>
    {0.0} <music21.layout.SystemLayout>
    {0.0} <music21.clef.Treble8vbClef>
    {0.0} <music21.tempo.MetronomeMark Quarter=120 (playback only)>
    {0.0} <music21.key.Key of F major>
    {0.0} <music21.meter.TimeSignature 2/4>
    {0.0} <music21.note.Note C>
    {1.5} <music21.note.Note D>
{2.0} <music21.stream.Measure 2 offset=2.0>
    {0.0} <music21.note.Note E>
    {0.5} <music21.note.Note D>
    {1.0} <music21.note.Note C>
    {1.5} <music21.note.Note D>

>>> qjFlat = qj.flatten()
>>> k1 = qjFlat.getElementsByClass(key.KeySignature).first()
>>> k3flats = key.KeySignature(-3)
>>> qjFlat.replace(k1, k3flats, allDerived=True)
>>> qj.getElementsByClass(stream.Measure)[1].insert(0, key.KeySignature(5))

>>> qj2 = qj.invertDiatonic(note.Note('F4'), inPlace=False)
>>> qj2.show('text')
{0.0} <music21.instrument.Piano 'P1: MusicXML Part: Grand Piano'>
{0.0} <music21.stream.Measure 1 offset=0.0>
    {0.0} <music21.layout.SystemLayout>
    {0.0} <music21.clef.Treble8vbClef>
    {0.0} <music21.tempo.MetronomeMark Quarter=120 (playback only)>
    {0.0} <music21.key.KeySignature of 3 flats>
    {0.0} <music21.meter.TimeSignature 2/4>
    {0.0} <music21.note.Note B->
    {1.5} <music21.note.Note A->
{2.0} <music21.stream.Measure 2 offset=2.0>
    {0.0} <music21.key.KeySignature of 5 sharps>
    {0.0} <music21.note.Note G#>
    {0.5} <music21.note.Note A#>
    {1.0} <music21.note.Note B>
    {1.5} <music21.note.Note A#>

* Changed in v5: inPlace is False by default.
invertDiatonicTCrm  r   NFr  )rL  rY   r   r"   r4  Keyr   r&   diatonicNoteNumr%   NotRestaccidentalByStepstep
accidentalrN  displayStatus)
r   inversionNoterF  returnStreamkeySigSearchquickSearchourKeyinversionDNNr  ksActives
             rJ   r  Stream.invertDiatonic  s?   v L445EFL#++-@@AQAQRWWS\F!#!!_FFK$**::dll+A'(<'7177;R;R&RAGG#%+%<%<QWW\\%J"..s/?/?@%-%>%>qww||%L"ww!!-37""0 ,  rI   c           	     V   U R                    H  nUb  U R                  U5      U:  a  M  Ub  U R                  U5      U:  a  M7  Ub"  UR                  R                  U5      (       a  M\  U R	                  U[        U R                  U5      U-   5      5        M     U R                  5         g)a  
Add the given offset value to every offset of
the objects found in the Stream. Objects that are
specifically placed at the end of the Stream via
.storeAtEnd() (such as right barlines) are
not affected.

If startOffset is given then elements before
that offset will not be shifted.  If endOffset is given
then all elements at or after this offset will be not be shifted.

>>> a = stream.Stream()
>>> a.repeatInsert(note.Note('C'), list(range(10)))
>>> a.shiftElements(30)
>>> a.lowestOffset
30.0
>>> a.shiftElements(-10)
>>> a.lowestOffset
20.0

Use shiftElements to move elements after a change in
duration:

>>> st2 = stream.Stream()
>>> st2.insert(0, note.Note('D4', type='whole'))
>>> st2.repeatInsert(note.Note('C4'), list(range(4, 8)))
>>> st2.show('text')
{0.0} <music21.note.Note D>
{4.0} <music21.note.Note C>
{5.0} <music21.note.Note C>
{6.0} <music21.note.Note C>
{7.0} <music21.note.Note C>

Now make the first note a dotted whole note and shift the rest by two quarters:

>>> firstNote = st2[0]
>>> firstNoteOldQL = firstNote.quarterLength
>>> firstNote.duration.dots = 1
>>> firstNoteNewQL = firstNote.quarterLength
>>> shiftAmount = firstNoteNewQL - firstNoteOldQL
>>> shiftAmount
2.0

>>> st2.shiftElements(shiftAmount, startOffset=4.0)
>>> st2.show('text')
{0.0} <music21.note.Note D>
{6.0} <music21.note.Note C>
{7.0} <music21.note.Note C>
{8.0} <music21.note.Note C>
{9.0} <music21.note.Note C>

A class filter list may be given.  It must be an iterable.

>>> st2.insert(7.5, key.Key('F'))
>>> st2.shiftElements(2/3, startOffset=6.0, endOffset=8.0,
...                   classFilterList=[note.Note])
>>> st2.show('text')
{0.0} <music21.note.Note D>
{6.6667} <music21.note.Note C>
{7.5} <music21.key.Key of F major>
{7.6667} <music21.note.Note C>
{8.0} <music21.note.Note C>
{9.0} <music21.note.Note C>
N)r   r  ru   rv   r  r   r   )r   rM   r  	endOffsetrh  ro   s         rJ   shiftElementsStream.shiftElements#  s    D A&4+=+=a+@;+N$););A)>))K*qzz/D/D_/U/U%%a0B0B10E0N)OP   	  "rI   c                H    U R                  U R                  5        SU l        g)a^  
Transfer the offset of this stream to all
internal elements; then set
the offset of this stream to zero.

>>> a = stream.Stream()
>>> a.repeatInsert(note.Note('C'), list(range(10)))
>>> a.offset = 30
>>> a.transferOffsetToElements()
>>> a.lowestOffset
30.0
>>> a.offset
0.0
>>> a.offset = 20
>>> a.transferOffsetToElements()
>>> a.lowestOffset
50.0
rl   N)r  rM   r   s    rJ   transferOffsetToElementsStream.transferOffsetToElementsr  s    & 	4;;'rI   c                     UR                   nUn[        U5       H(  nU R	                  [
        R                  " U5      5        M*     g! [         a    [        S5      ef = f)a  
Given an object and a number, run append that many times on
a deepcopy of the object.
numberOfTimes should of course be a positive integer.

>>> a = stream.Stream()
>>> n = note.Note('D--')
>>> n.duration.type = 'whole'
>>> a.repeatAppend(n, 10)
>>> a.show('text')
{0.0} <music21.note.Note D-->
{4.0} <music21.note.Note D-->
{8.0} <music21.note.Note D-->
{12.0} <music21.note.Note D-->
...
{36.0} <music21.note.Note D-->

>>> a.duration.quarterLength
40.0
>>> a[9].offset
36.0
Tto put a non Music21Object in a stream, create a music21.ElementWrapper for the itemN)rt   r   ro  r  rU   r  r  )r   r  numberOfTimesunusedrL   unused_is         rJ   repeatAppendStream.repeatAppend  sd    .	T]]FG m,HKKg./ -  	T! #S T T	Ts   A Ac                &   [         R                  " U5      (       d  [        SU 35      e UR                  nUnU H*  n[
        R                  " U5      nU R                  XV5        M,     U R                  5         g! [         a    [        S5      ef = f)a  
Given an object, create a deep copy of each object at
each position specified by the offset list:

>>> a = stream.Stream()
>>> n = note.Note('G-')
>>> n.quarterLength = 1

>>> a.repeatInsert(n, [0, 2, 3, 4, 4.5, 5, 6, 7, 8, 9, 10, 11, 12])
>>> len(a)
13
>>> a[10].offset
10.0
z)must provide an iterable of offsets, not r  N)	r   r   ro  rt   r   r  r  r   r   )r   r  offsetsr  rL   rM   elementCopys          rJ   repeatInsertStream.repeatInsert  s       ))!$MgY"WXX	T]]FG F--0KOOF0  	  "  	T " #S T T	Ts   A: :Bc                r   U R                  S5      nSnSnSn	[        U R                  5       HP  u  pXL d  UR                  UR                  :X  d  M%  U
nU R	                  U5      nXR
                  R                  -   n	MR     Uc  [        S5      eU R                   H<  nU R	                  U5      nX-
  Us=::  a
  X-   :  d  M'  O  M+  UR                  X5        M>     U R                   H<  nU R	                  U5      nX-
  Us=::  a
  X-   :  d  M'  O  M+  UR                  U5        M>     UR                  5         U$ )aB  
Extracts elements around the given element within (before)
quarter notes and (after) quarter notes (default 4), and
returns a new Stream.

>>> qn = note.Note(type='quarter')
>>> qtrStream = stream.Stream()
>>> qtrStream.repeatInsert(qn, [0, 1, 2, 3, 4, 5])
>>> hn = note.Note(type='half')
>>> hn.name = 'B-'
>>> qtrStream.append(hn)
>>> qtrStream.repeatInsert(qn, [8, 9, 10, 11])
>>> hnStream = qtrStream.extractContext(hn, 1.0, 1.0)
>>> hnStream.show('text')
{5.0} <music21.note.Note C>
{6.0} <music21.note.Note B->
{8.0} <music21.note.Note C>

* Changed in v7: forceOutputClass removed.

OMIT_FROM_DOCS
TODO: maxBefore -- maximum number of elements to return before; etc.
TODO: use .template to get new Stream

NOTE: RENAME: this probably should be renamed, as we use Context in a special way.
Perhaps better is extractNeighbors?

extractContextNr   z(Could not find the element in the stream)r  r  r   r   r  r   r  ro  r   r   r   rg  r   )r   searchElementbeforeafter	maxBeforemaxAfterdisplayr  foundOffsetfoundEndr  bro   r  s                 rJ   r  Stream.extractContext  s%   < //"23dmm,DA!QTT]-=-=%="003&)A)AA	 -
 =!"LMM A""1%A#q;8+;;;""1(  
 ""A""1%A#q;8+;;;&&q) # 	##%rI   c                r   U R                   R                  5       nU(       a  [        5       nOU Vs1 s H  n[        US   5      iM     nnU(       a  [        5       nO8U Vs1 s H+  n[        US   US   R                  R
                  -   5      iM-     nn[        UR                  U5      5      $ s  snf s  snf )a  
Get a list of all offsets and endtimes
of notes and rests in this stream.

Helper method for makeChords and Chordify
run on .flatten().notesAndRests


>>> s = stream.Score()
>>> p1 = stream.Part()
>>> p1.insert(4, note.Note('C#'))
>>> p1.insert(5.3, note.Rest())
>>> p2 = stream.Part()
>>> p2.insert(2.12, note.Note('D-', type='half'))
>>> p2.insert(5.5, note.Rest())
>>> s.insert(0, p1)
>>> s.insert(0, p2)

We will get a mix of float and fractions.Fraction() objects

>>> [str(o) for o in s.flatten()._uniqueOffsetsAndEndTimes()]
['53/25', '4.0', '103/25', '5.0', '53/10', '5.5', '63/10', '6.5']

Limit what is returned:

>>> [str(o) for o in s.flatten()._uniqueOffsetsAndEndTimes(offsetsOnly=True)]
['53/25', '4.0', '53/10', '5.5']
>>> [str(o) for o in s.flatten()._uniqueOffsetsAndEndTimes(endTimesOnly=True)]
['103/25', '5.0', '63/10', '6.5']

And this is useless :-)

>>> s.flatten()._uniqueOffsetsAndEndTimes(offsetsOnly=True, endTimesOnly=True)
[]

r   rm  )r   valuesre  r   r   r  r  union)r   offsetsOnlyendTimesOnlyoffsetDictValuesr  vendTimess          rJ   _uniqueOffsetsAndEndTimes Stream._uniqueOffsetsAndEndTimes  s    J  ++224eG-=>-=vad|-=G>uH &67%5 qtadmm&A&AAB%5  7gmmH-.. ?
7s   B/!2B4)rC  addPartIdAsGroupremoveRedundantPitchesr^  copyPitchesc               (  ^^^^^^ UUUUU4S jnU4S jmS mU(       as  U R                  5       (       a<  U R                  S5      R                  5       R                  SL a  U R	                  SS9nO$U R                  SL a  U R	                  SS9nOU nOU nU R                  5       (       a$  UR                  [
        5      R                  5       nOUnUR                  SSSS9n	U	R                  5       (       ac  U	R                  [        5      n
[        U
5       H>  u  pUR                  US	S
S9nUb
  U" X5        M#  [        R                  SU SU 35        M@     OU" X5        U	R                   H?  nUR                  c  M  UR                  R                  S:w  d  M.  SUR                  l        MA     [#        US5      (       aP  UR$                  bC  UR                  5       S
L a0  U	R'                  S[(        R*                  " UR$                  5      5        U	$ )a(  
Create a chordal reduction of polyphonic music, where each
change to a new pitch results in a new chord. If a Score or
Part of Measures is provided, a Stream of Measures will be
returned. If a flat Stream of notes, or a Score of such
Streams is provided, no Measures will be returned.

If using chordify with chord symbols, ensure that the ChordSymbol objects
have durations (by default, the duration of a ChordSymbol object is 0, unlike
a Chord object). If Harmony objects are not provided a duration, they
will not be included in the chordified output pitches but may appear as chord symbols
in notation on the score. To realize the chord symbol durations on a score, call
:meth:`music21.harmony.realizeChordSymbolDurations` and pass in the score.

This functionality works by splitting all Durations in
all parts, or if there are multiple parts by all unique offsets. All
simultaneous durations are then gathered into single chords.

If `addPartIdAsGroup` is True, all elements found in the
Stream will have their source Part id added to the
element's pitches' Group.  These groups names are useful
for partially "de-chordifying" the output.  If the element chordifies to
a :class:`~music21.chord.Chord` object, then the group will be found in each
:class:`~music21.pitch.Pitch` element's .groups in Chord.pitches.  If the
element chordifies to a single :class:`~music21.note.Note` then .pitch.groups
will hold the group name.

The `addTies` parameter currently does not work for pitches in Chords.

If `toSoundingPitch` is True, all parts that define one or
more transpositions will be transposed to sounding pitch before chordification.
True by default.

>>> s = stream.Score()
>>> p1 = stream.Part()
>>> p1.id = 'part1'
>>> p1.insert(4, note.Note('C#4'))
>>> p1.insert(5.3, note.Rest())
>>> p2 = stream.Part()
>>> p2.id = 'part2'
>>> p2.insert(2.12, note.Note('D-4', type='half'))
>>> p2.insert(5.5, note.Rest())
>>> s.insert(0, p1)
>>> s.insert(0, p2)
>>> s.show('text', addEndTimes=True)
{0.0 - 6.3} <music21.stream.Part part1>
    {4.0 - 5.0} <music21.note.Note C#>
    {5.3 - 6.3} <music21.note.Rest quarter>
{0.0 - 6.5} <music21.stream.Part part2>
    {2.12 - 4.12} <music21.note.Note D->
    {5.5 - 6.5} <music21.note.Rest quarter>

>>> cc = s.chordify()

>>> cc[3]
<music21.chord.Chord C#4>
>>> cc[3].duration.quarterLength
Fraction(22, 25)

>>> cc.show('text', addEndTimes=True)
{0.0 - 2.12} <music21.note.Rest 53/25ql>
{2.12 - 4.0} <music21.chord.Chord D-4>
{4.0 - 4.12} <music21.chord.Chord C#4 D-4>
{4.12 - 5.0} <music21.chord.Chord C#4>
{5.0 - 6.5} <music21.note.Rest dotted-quarter>

Here's how addPartIdAsGroup works:

>>> cc2 = s.chordify(addPartIdAsGroup=True)
>>> cSharpDFlatChord = cc2[2]
>>> for p in cSharpDFlatChord.pitches:
...     (str(p), p.groups)
('C#4', ['part1'])
('D-4', ['part2'])

>>> s = stream.Stream()
>>> p1 = stream.Part()
>>> p1.insert(0, harmony.ChordSymbol('Cm', quarterLength=4.0))
>>> p1.insert(2, note.Note('C2'))
>>> p1.insert(4, harmony.ChordSymbol('D', quarterLength=4.0))
>>> p1.insert(7, note.Note('A2'))
>>> s.insert(0, p1)
>>> s.chordify().show('text')
{0.0} <music21.chord.Chord C3 E-3 G3>
{2.0} <music21.chord.Chord C2 C3 E-3 G3>
{3.0} <music21.chord.Chord C3 E-3 G3>
{4.0} <music21.chord.Chord D3 F#3 A3>
{7.0} <music21.chord.Chord A2 D3 F#3 A3>

Note that :class:`~music21.harmony.ChordSymbol` objects can also be chordified:

>>> s = stream.Stream()
>>> p2 = stream.Part()
>>> p1 = stream.Part()
>>> p2.insert(0, harmony.ChordSymbol('Cm', quarterLength=4.0))
>>> p1.insert(2, note.Note('C2'))
>>> p2.insert(4, harmony.ChordSymbol('D', quarterLength=4.0))
>>> p1.insert(7, note.Note('A2'))
>>> s.insert(0, p1)
>>> s.insert(0, p2)
>>> s.chordify().show('text')
{0.0} <music21.chord.Chord C3 E-3 G3>
{2.0} <music21.chord.Chord C2 C3 E-3 G3>
{3.0} <music21.chord.Chord C3 E-3 G3>
{4.0} <music21.chord.Chord D3 F#3 A3>
{7.0} <music21.chord.Chord A2 D3 F#3 A3>

If addPartIdAsGroup is True, and there are redundant pitches,
ensure that the merged pitch has both groups

>>> s = stream.Score()
>>> p0 = stream.Part(id='p0')
>>> p0.insert(0, note.Note('C4'))
>>> p1 = stream.Part(id='p1')
>>> p1.insert(0, note.Note('C4'))
>>> s.insert(0, p0)
>>> s.insert(0, p1)
>>> s1 = s.chordify(addPartIdAsGroup=True)
>>> c = s1.recurse().notes[0]
>>> c
<music21.chord.Chord C4>
>>> c.pitches[0].groups
['p0', 'p1']

With copyPitches = False, then the original pitches are retained, which
together with removeRedundantPitches=False can be a powerful tool for
working back to the original score:

>>> n00 = note.Note('C4')
>>> n01 = note.Note('E4')
>>> n10 = note.Note('E4', type='half')
>>> p0 = stream.Part(id='p0')
>>> p1 = stream.Part(id='p1')
>>> p0.append([n00, n01])
>>> p1.append(n10)
>>> s = stream.Score()
>>> s.insert(0, p0)
>>> s.insert(0, p1)
>>> ss = s.chordify(removeRedundantPitches=False, copyPitches=False, addPartIdAsGroup=True)
>>> ss.show('text')
{0.0} <music21.chord.Chord C4 E4>
{1.0} <music21.chord.Chord E4 E4>

>>> c1 = ss.recurse().notes[1]
>>> for p in c1.pitches:
...     if 'p0' in p.groups:
...         p.step = 'G'  # make a complete triad
>>> n01
<music21.note.Note G>

* Changed in v5:
  - Runs a little faster for small scores and run a TON faster for big scores
  running in O(n) time not O(n^2)
  - no longer supported: displayTiedAccidentals=False,
* Changed in v6.3: Added copyPitches

OMIT_FROM_DOCS

Test that chordifying works on a single stream.

>>> f2 = stream.Score()
>>> f2.insert(0, metadata.Metadata())
>>> f2.insert(0, note.Note('C4'))
>>> f2.insert(0, note.Note('D#4'))
>>> c = f2.chordify()
>>> cn = c.notes
>>> cn[0].pitches
(<music21.pitch.Pitch C4>, <music21.pitch.Pitch D#4>)
c           	       > UR                  [        R                  4S9nUR                  5       nSU;  a  SU-   n[	        X3SS 5       H  u  pE[        XESS9(       a  M  UR                  U5      nXT-
  nUS:  a*  [        R                  SS	U < S
U< S3-   SU SU 3-   5        UR                  UT
T	TTS9nU R                  [        U5      U5        M     U R                  5         T" U 5        g)z>
streamToChordify is either a Measure or a Score=MeasureSlice
r  r   )r   rm  NgHz>)abs_tolz(Something is wrong with the verticality z
in stream z: r   zits endTime z is less than its offset )rC  r  r  r  )asTimespansr%   r  allTimePointsr  r   getVerticalityAtenvironLocalr   makeElementr   r   r   )templateInnerstreamToChordifytimespanTreer  rM   rN   vertr  chordOrRestr  rC  consolidateRestsr  r  s            rJ   chordifyOneMeasure+Stream.chordify.<locals>.chordifyOneMeasure  s    ,774CSCSBU7VL(668M% $} 4#&}AB6G#H6D9#44V< ' 0 1$ %%B&}&7r$CD(	1J6(ST #..}7>@PF\;F	 / 1 ((E' $I( --/]+rI   c                   > / n[        U R                  5       H?  n[        U[        R                  5      (       d  T" X5        / nM.  UR                  U5        MA     T" X5        g rs   )r   r  r   r%   r  rU   )r  consecutiveRestsr   removeConsecutiveRestss      rJ   r  )Stream.chordify.<locals>.consolidateRests  sU    !=667!"dii00*=K')$$++B/ 8 #=CrI   c                   [        U5      S:  a  g [        S U 5       5      nU R                  US   5      nU H  nU R                  U5        M     [        R
                  " 5       nX%R                  l        U R                  X55        g )Nr  c              3  L   #    U  H  oR                   R                  v   M     g 7frs   )r   r  )rn   rs     rJ   rp   BStream.chordify.<locals>.removeConsecutiveRests.<locals>.<genexpr>&  s     SBRQ

 8 8BRs   "$r   )	r   sumr  r  r%   r  r   r  rV   )r  r  totalDurationr  r  rNews         rJ   r  /Stream.chordify.<locals>.removeConsecutiveRests#  su    #$q(SBRSSM'556Fq6IJK%$$Q' &99;D*7MM'  3rI   rQ   FrL  )r  )r  r  r  rC   Tr  NzMalformed Part object, z, at measure index z	even-tiedr#   r   )r*  r   r   r=  r^  rQ   r  r,  rw   r  r  r  r   r\   r  displayTyper  rT  r#   rV   r  r  )r   rC  r  r  r^  r  r  workObjtemplateStreamr  r  r  templateMeasuremeasurePartr-  r  r  s    ``` `         @@rJ   chordifyStream.chordifyF  s   f	, 	,@	D		4 ''))00:@@BRRV[[..u.=%%...u.=G""$$$77?EEGN$N!**9I8= + ? !!&99'BO&/&@"%ooatoT*&D %%(?yH[\][^&_` 'A x1 !!A||'ALL,D,D,S-1* " GZ(($$0..0D8OOAt}}W-=-=>?rI   c                
   U R                  SS9nU R                  SS9nUb   U R                  U5      R                  5       nOU nU HT  nU" U5      (       a#  UR                  UR	                  U5      U5        M3  UR                  UR	                  U5      U5        MV     UR
                   H4  nU" U5      (       a  UR                  U5        M#  UR                  U5        M6     UR                  5         UR                  5         X44$ )a  
Given a stream, get all objects of type classObj and divide them into
two new streams depending on the results of fx.
Fx should be a lambda or other function on elements.
All elements where fx returns "True" go in the first stream.
All other elements are put in the second stream.

If classObj is None then all elements are returned.  ClassObj
can also be a list of classes.

In this example, we will create 50 notes from midi note 30 (two
octaves and a tritone below middle C) to midi note 80 (an octave
and a minor sixth above middle C) and add them to a Stream.
We then create a lambda function to split between those notes
below middle C (midi note 60) and those above
(google "lambda functions in Python" for more information on
what these powerful tools are).


>>> stream1 = stream.Stream()
>>> for x in range(30, 81):
...     n = note.Note()
...     n.pitch.midi = x
...     stream1.append(n)
>>> fx = lambda n: n.pitch.midi < 60
>>> b, c = stream1.splitByClass(note.Note, fx)

Stream b now contains all the notes below middle C,
that is, 30 notes, beginning with F#1 and ending with B3
while Stream c has the 21 notes from C4 to A-5:

>>> len(b)
30
>>> (b[0].nameWithOctave, b[-1].nameWithOctave)
('F#1', 'B3')
>>> len(c)
21
>>> (c[0].nameWithOctave, c[-1].nameWithOctave)
('C4', 'G#5')
splitByClassr  )r  r   r6   r   r  r   rg  r   )r   classObjfxar  r  ro   s          rJ   r  Stream.splitByClass]  s    T OO^O<OO^O<++H5<<>EEA!uuU003Q7U003Q7	 
 ##A!uu  #   # $ 	
	trI   c                `   Uc  U n/ nUR                  5       (       a|  / n[        UR                  5       H&  u  pEUR                  UR	                  5       U45        M(     UR                  S5      nU(       a"  UR                  SUR                  5       S45        OUS4/nU H  u  pxUR                   Hu  n	[        U	[        R                  5      (       a  M$  U	R                  R                  n
UR                  U	5      n[        X-   5      n[!        XX5      nUR                  U5        Mw     M     U$ )a  
Returns a list where each element is a NamedTuple
consisting of the 'offset' of each element in a stream, the
'endTime' (that is, the offset plus the duration) and the
'element' itself.  Also contains a 'voiceIndex' entry which
contains the voice number of the element, or None if there
are no voices.

>>> n1 = note.Note(type='quarter')
>>> c1 = clef.AltoClef()
>>> n2 = note.Note(type='half')
>>> s1 = stream.Stream()
>>> s1.append([n1, c1, n2])
>>> om = s1.offsetMap()
>>> om[2].offset
1.0
>>> om[2].endTime
3.0
>>> om[2].element is n2
True
>>> om[2].voiceIndex


Needed for makeMeasures and a few other places.

The Stream source of elements is self by default,
unless a `srcObj` is provided.  (this will be removed in v.8)


>>> s = stream.Stream()
>>> s.repeatAppend(note.Note(), 8)
>>> for om in s.offsetMap():
...     om
OffsetMap(element=<music21.note.Note C>, offset=0.0, endTime=1.0, voiceIndex=None)
OffsetMap(element=<music21.note.Note C>, offset=1.0, endTime=2.0, voiceIndex=None)
OffsetMap(element=<music21.note.Note C>, offset=2.0, endTime=3.0, voiceIndex=None)
OffsetMap(element=<music21.note.Note C>, offset=3.0, endTime=4.0, voiceIndex=None)
OffsetMap(element=<music21.note.Note C>, offset=4.0, endTime=5.0, voiceIndex=None)
OffsetMap(element=<music21.note.Note C>, offset=5.0, endTime=6.0, voiceIndex=None)
OffsetMap(element=<music21.note.Note C>, offset=6.0, endTime=7.0, voiceIndex=None)
OffsetMap(element=<music21.note.Note C>, offset=7.0, endTime=8.0, voiceIndex=None)
Nr,  r   )	hasVoicesr  r7  rU   rQ  r  rV   r6   r   r   r   Barliner   r  r  r   rK   )r   r  r"  r  r  r  elsNotOfVoicer  rO   ro   durrM   rN   thisOffsetMaps                 rJ   r"  Stream.offsetMap  s   V >F	F!&--0qyy{A./ 1"88AMa-"6"6"8$!?@tn%F!'E__a--jj..,,Q/ . !*!W I  / % "( rI   c                8    [         R                  " U UUUUUUUS9$ )z
Return a new stream (or if inPlace=True change in place) this
Stream so that it has internal measures.

For more details, see :py:func:`~music21.stream.makeNotation.makeMeasures`.
)meterStreamrefStreamOrTimeRangerE  innerBarlinefinalBarlinebestClefrF  )r-   makeMeasures)r   r  r  rE  r  r  r  rF  s           rJ   r  Stream.makeMeasures  s/      ((#!5'%%	
 		
rI   c           	     4    [         R                  " U UUUUUS9$ )zf
Calls :py:func:`~music21.stream.makeNotation.makeRests`.

* Changed in v7, inPlace=False by default.
)r  fillGapstimeRangeFromBarDurationrF  	hideRests)r-   	makeRests)r   r  r  r  rF  r  s         rJ   r  Stream.makeRests  s)     %%!5%=
 	
rI   c                2    [         R                  " U UUUUS9$ )z
Calls :py:func:`~music21.stream.makeNotation.makeTies`.

* Changed in v4: inPlace=False by default.
* New in v.7: `classFilterList`.
)r  rF  rD  rh  )r-   makeTies)r   r  rF  rD  rh  s        rJ   r  Stream.makeTies  s&     $$##9+
 	
rI   rF  setStemDirectionsfailOnNoTimeSignaturec               0    [         R                  " U UUUS9$ )a;  
Return a new Stream, or modify the Stream in place, with beams applied to all
notes.

See :py:func:`~music21.stream.makeNotation.makeBeams`.

* New in v6.7: setStemDirections.
* New in v7: failOnNoTimeSignature raises StreamException if no TimeSignature
  exists in the stream context from which to make measures.
r  )r-   	makeBeams)r   rF  r  r  s       rJ   r  Stream.makeBeams,  s#     %%/"7	
 	
rI   )	pitchPastpitchPastMeasureotherSimultaneousPitchesuseKeySignaturealteredPitchessearchKeySignatureByContextcautionaryPitchClasscautionaryAllrF  overrideStatuscautionaryNotImmediateRepeattiePitchSetc               j   U	(       d  U R                  S5      nOU nUc  / nUc  / nUc  / n/ n[        U[        R                  5      (       a  UR                  nOiUSL ad  SnU(       a&  U R                  [        R                  5      nUb  U/nOU R                  [        R                  5      nU(       a  US   R                  nX^-  nUR                  5       R                  nUc
  [        5       nSnU GH  nUR                  b@  UR                  R                  (       a%  Ub  UR                  ULa  USS n/ nUR                  n[        U[        R                  5      (       a  UR                  R                  U;   a  SnOSnUR                  R!                  UUUUUU
UUS9  UR#                  UR                  5        UR%                  5         UR&                  b?  UR&                  R(                  S:w  a%  UR+                  UR                  R                  5        [,        R.                  " UUUUUUU
US9  GMD  [        U[0        R2                  5      (       Ga+  [        5       n[5        U5       H  nUR                  nUR                  U;   a  SnOSnUR6                   Vs/ s H  nUULd  M
  UPM     nnUR!                  UUUUUUU
UUS	9	  UR&                  b5  UR&                  R(                  S:w  a  UR+                  UR                  5        [,        R.                  " UUUUUUU
US9  M     UR%                  5         U H  nUR+                  U5        M     UUR6                  -  n[,        R.                  " UUUUUUU
US9  GM  UR%                  5         GM     SUR8                  l        U	(       d  U$ gs  snf )
a	  
A method to set and provide accidentals given various conditions and contexts.

`pitchPast` is a list of pitches preceding this pitch in this measure.

`pitchPastMeasure` is a list of pitches preceding this pitch but in a previous measure.

`otherSimultaneousPitches` is a list of other pitches in this simultaneity, for use
when `cautionaryPitchClass` is True.

If `useKeySignature` is True, a :class:`~music21.key.KeySignature` will be searched
for in this Stream or this Stream's defined contexts. An alternative KeySignature
can be supplied with this object and used for temporary pitch processing.

If `alteredPitches` is a list of modified pitches (Pitches with Accidentals) that
can be directly supplied to Accidental processing. These are the same values obtained
from a :class:`music21.key.KeySignature` object using the
:attr:`~music21.key.KeySignature.alteredPitches` property.

If `cautionaryPitchClass` is True, comparisons to past accidentals are made regardless
of register. That is, if a past sharp is found two octaves above a present natural,
a natural sign is still displayed.

If `cautionaryAll` is True, all accidentals are shown.

If `overrideStatus` is True, this method will ignore any current `displayStatus` setting
found on the Accidental. By default this does not happen. If `displayStatus` is set to
None, the Accidental's `displayStatus` is set.

If `cautionaryNotImmediateRepeat` is True, cautionary accidentals will be displayed for
an altered pitch even if that pitch had already been displayed as altered.

If `tiePitchSet` is not None it should be a set of `.nameWithOctave` strings
to determine whether following accidentals should be shown because the last
note of the same pitch had a start or continue tie.

If `searchKeySignatureByContext` is True then keySignatures from the context of the
stream will be used if none found.

The :meth:`~music21.pitch.Pitch.updateAccidentalDisplay` method is used to determine if
an accidental is necessary.

This will assume that the complete Stream is the context of evaluation. For smaller context
ranges, call this on Measure objects.

If `inPlace` is True, this is done in-place; if `inPlace` is False,
this returns a modified deep copy.

* Changed in v6: does not return anything if inPlace is True.
* Changed in v7: default inPlace is False
* Changed in v8: altered unisons/octaves in Chords now supply clarifying naturals.

All arguments are keyword only.
makeAccidentalsNTr   F)r
  r  r  r  r  r  r  lastNoteWasTiedr   )r
  r  r  r  r  r  r  )	r
  r  r  r  r  r  r  r  r  )rL  r   r"   r4  r  rN  r   rY   r  re  r  r0  r%   r  r&   nameWithOctaveupdateAccidentalDisplayrU   rJ  r'   r   r  r-   makeOrnamentalAccidentalsr   r  r   r\   r.   accidentals)r   r
  r  r  r  r  r  r  r  rF  r  r  r  rQ  addAlteredPitchesksIterksnoteIteratorlast_measurero   r  seenPitchNamesr  r-  r   pNames                             rJ   r  Stream.makeAccidentals>  s   L 112CDII I#!!N/1os'7'788 / > >$ 	 
 +++C,<,<=> TF001A1AB %+1I$<$<!+ !((*88
 %K%)A||'ALL,B,B+L0P (1|$ "I ||!TYY''77))[8&*O&+O//'%5#1)="/#11M$3 0 5   )!!#55$v)=OOAGG$:$:; 66'%5#1)="/#11MO Au{{++ "%aAA'';6*.*/CD99/_9%PU]^P^9,/_--"+)91I'5-A&3'55Q(7 . 	9 uu(QUUZZ6-A&**1+;+;< !::"+)9'5-A&3'55QS1 !D !!#+EOOE* , QYY&	 66'%5#1)="/#11MO !!#C F .2	* c 0`s   	N0)N0c                .    U R                   R                  $ )a.  
If Accidentals.displayStatus is None for all
contained pitches, it as assumed that accidentals
have not been set for display and/or makeAccidentals
has not been run. If any Accidental has displayStatus
other than None, this method returns True, regardless
of if makeAccidentals has actually been run.
)r.   r  r   s    rJ   rO  Stream.haveAccidentalsBeenMade  s       ,,,rI   )r  r  rF  r  r
  r  r  r  r  r  r  r  r  c                  U(       a  U nOU R                  S5      nUR                  U(       + S9  UR                  5       (       dO  UR                  SSS9  UR	                  UUSUS9  UR                  5       (       d  [        S[        U 5       S35      eUR                  R                  (       d  [        R                  " UUUUUU	U
UUUS9
  [        R                  " XSS	9  UR                  [        5       H/  n[        R                  " USSS
9  [        R                  " USSS9  M1     UR                  R                   (       d   [        R"                  " USS9  UR                  [        5       H5  nUR                  R.                  (       a  M   [        R0                  " USS9  M7     U(       d  U$ g! [$        R&                   a)  n[(        R*                  " [-        U5      5         SnANSnAff = f)a  
This method calls a sequence of Stream methods on this Stream to prepare
notation, including creating voices for overlapped regions, Measures
if necessary, creating ties, beams, accidentals, and tuplet brackets.

If `inPlace` is True, this is done in-place.
if `inPlace` is False, this returns a modified deep copy.

The following additional parameters are documented on
:meth:`~music21.stream.base.makeAccidentals`::

    pitchPast
    pitchPastMeasure
    useKeySignature
    alteredPitches
    cautionaryPitchClass
    cautionaryAll
    overrideStatus
    cautionaryNotImmediateRepeat
    tiePitchSet


>>> s = stream.Stream()
>>> n = note.Note('g')
>>> n.quarterLength = 1.5
>>> s.repeatAppend(n, 10)
>>> sMeasures = s.makeNotation()
>>> len(sMeasures.getElementsByClass(stream.Measure))
4
>>> sMeasures.getElementsByClass(stream.Measure).last().rightBarline.type
'final'

* Changed in v7: `inPlace=True` returns `None`.
r-   )rV   TrF  r  r  r  rF  r  z!no measures found in stream with z	 elements)	r
  r  r  r  r  r  r  r  r  )r  rF  rY   rC  rY   
onlyIfTiedrL  N)rL  r  r,  
makeVoicesr  ro  r   r.   r  r-   makeAccidentalsInMeasureStreamr  r   rw   splitElementsToCompleteTupletsconsolidateCompletedTupletsr5  r  r$   MeterExceptionr   r   r   tupletsmakeTupletBrackets)r   r  r  rF  r  r
  r  r  r  r  r  r  r  r  r  r  mes                    rJ   r-   Stream.makeNotation*  s   h L44^DL 	..; 	/ 	

 ''))##D4#@ %%'%9!	 & #  ++--%7D	{)LN N ((4477#!1 /-%9+--I'
) 	lTR009A774QUV44QQUV : ((..'&&|TB 009A>>)))//4@ :   '' 'c"g&&'s   F2 2G/G**G/c                  U(       d  U R                  S5      nOU nUR                  R                  n[        UR	                  U5      5      n[        XUSS 5       H8  u  pgUR                  U5      UR                  U5      -
  nXR                  l        M:     U(       a)  UUR                  US   5      -
  US   R                  l        U(       d  U$ g)ad  
Given a Stream and an object class name, go through the Stream
and find each instance of the desired object. The time between
adjacent objects is then assigned to the duration of each object.
The last duration of the last object is assigned to extend to the
end of the Stream.

If `inPlace` is True, this is done in-place; if `inPlace` is
False, this returns a modified deep copy.

>>> stream1 = stream.Stream()
>>> n = note.Note(type='quarter')
>>> n.duration.quarterLength
1.0
>>> stream1.repeatInsert(n, [0, 10, 20, 30, 40])

>>> dyn = dynamics.Dynamic('ff')
>>> stream1.insert(15, dyn)
>>> stream1[-1].offset  # offset of last element
40.0
>>> stream1.duration.quarterLength  # total duration
41.0
>>> len(stream1)
6

>>> stream2 = stream1.flatten().extendDuration(note.GeneralNote, inPlace=False)
>>> len(stream2)
6
>>> stream2[0].duration.quarterLength
10.0

The Dynamic does not affect the second note:

>>> stream2[1].offset
10.0
>>> stream2[1].duration.quarterLength
10.0

>>> stream2[-1].duration.quarterLength  # or extend to end of stream
1.0
>>> stream2.duration.quarterLength
41.0
>>> stream2[-1].offset
40.0
rN  rm  Nr   )rL  r   r  r   r   r  r  )	r   objClassrF  rQ  	qLenTotalr   rL   nextElementr  s	            rJ   rN  Stream.extendDuration  s    ^ 112BCII&&44		44X>?$'12,$? G**;7):Q:QRY:ZZD-1* %@
 3<5>5L5LXVX\5Z4[HRL!!/ rI   )matchByPitchc                   g rs   rC   r   rF  r:  s      rJ   	stripTiesStream.stripTies       rI   rF  r:  c                   U $ rs   rC   r<  s      rJ   r=  r>    s	     rI   c               4  ^^^^ U(       d  U R                  S5      nOU nSUR                  l        UR                  5       (       a6  UR	                  [
        5       H  nUR                  STS9  M     U(       d  U$ gUR                  5       (       a-  UR                   H  nUR                  STS9  M     U(       d  U$ gUR                  5       nUR                  R                  S 5      R                  5       n/ m/ nSUUUU4S jjn	SS jn
[        [        U5      5       GH  nSnX{   nUS	:  a  US
-
  mUT   mOSmSm[        US5      (       aN  UR                   bA  UR                   R"                  S:X  a'  Tb  TT;  a  U/mOTT;   a  TR%                  U5        SnO[        US5      (       a  UR                   b  UR                   R"                  S:X  a  T(       d  TR%                  U5        SnOT(       a)  U	" U5      nU(       a  TR%                  U5        SnOzU/mSnOtU
" U5      (       ac  ['        T[(        R*                  5      (       a0  [        TR,                  5      [        UR,                  5      :w  a  U/mOTR%                  U5        SnO/ mSnUc  U	" U5      nU(       d  GM  TR%                  U5        [        T5      S:  a  / mGM  S	nTS
S  H%  nXU   R.                  -  nUR%                  U5        M'     US	:X  a  [1        S5      e[2        R4                  " [(        R6                  UTS	      5      nUR.                  nUR8                  R:                  (       d  SUR8                  l        [=        UU-   5      Ul        SUl        UR?                  5         UR@                   H/  nTS
S  H#  nUU   U;   d  M  URC                  UU   U5        M%     M1     / mGM     URE                  5         U H  nX{   nURG                  USS9  M     U(       d  U$ g)a  
Find all notes that are tied; remove all tied notes,
then make the first of the tied notes have a duration
equal to that of all tied constituents. Lastly,
remove the formerly-tied notes.

This method can be used on Stream and Stream subclasses.
When used on a stream containing Part-like substreams, as with many scores,
:class:`~music21.stream.Part`, :class:`~music21.stream.Measure`, and other
Stream subclasses are retained.

`inPlace` controls whether the input stream is modified or whether a deep copy
is made. (New in v7, to conform to the rest of music21, `inPlace=True` returns `None`.)

Presently, this only works if tied notes are sequential in the same voice; ultimately
this will need to look at .to and .from attributes (if they exist)

>>> a = stream.Stream()
>>> n = note.Note()
>>> n.quarterLength = 6
>>> a.append(n)
>>> m = a.makeMeasures()
>>> m.makeTies(inPlace=True)
>>> len(m.flatten().notes)
2

>>> m = m.stripTies()
>>> len(m.flatten().notes)
1

In cases where notes are manipulated after initial tie creation,
some chord members might lack ties. This will not prevent merging the tied notes
if all the pitches match, and `matchByPitch=True` (default):

>>> c1 = chord.Chord('C4 E4')
>>> c1.tie = tie.Tie('start')

>>> c2 = chord.Chord('C4 E4')
>>> c2.tie = tie.Tie('stop')

>>> m = stream.Measure()
>>> m.append([c1, c2])

>>> c1.add(note.Note('G4'))
>>> c2.add(note.Note('G4'))

>>> c2.notes[-1].tie is None
True

>>> strippedPitchMatching = m.stripTies()
>>> len(strippedPitchMatching.flatten().notes)
1

This can be prevented with `matchByPitch=False`, in which case every note,
including each chord member, must have "stop" and/or "continue" tie types,
which was not the case above:

>>> strippedMixedTieTypes = m.stripTies(matchByPitch=False)
>>> len(strippedMixedTieTypes.flatten().notes)
2

>>> c2.notes[0].tie = tie.Tie('stop')
>>> c2.notes[1].tie = tie.Tie('stop')
>>> c2.notes[2].tie = tie.Tie('stop')
>>> strippedUniformTieTypes = m.stripTies(matchByPitch=False)
>>> len(strippedUniformTieTypes.flatten().notes)
1

Notice the matching happens even after altering the pitches:

>>> c3 = c2.transpose(6)
>>> otherM = stream.Measure([c1, c3])
>>> strippedTransposed = otherM.stripTies(matchByPitch=False)
>>> len(strippedTransposed.flatten().notes)
1

When `matchByPitch` is True (as it is by default) the following
behavior defined regarding chords with a tie type "continue":

>>> c1.notes[0].tie = tie.Tie('continue')
>>> c1.notes[1].tie = tie.Tie('start')
>>> c1.notes[2].tie = tie.Tie('start')

Continue is accepted here as an ersatz-start:

>>> stripped1 = m.stripTies(matchByPitch=True)
>>> len(stripped1.flatten().notes)
1

But prepend an element so that it's considered as a tie continuation:

>>> c0 = chord.Chord('C4 E4 G4')
>>> c0.tie = tie.Tie('start')
>>> m2 = stream.Measure()
>>> m2.append([c0, c1, c2])

Now the mixed tie types on c1 will only be connected to c2
on the permissive option (`matchByPitch=True`):

>>> stripped2 = m2.stripTies(matchByPitch=True)
>>> stripped2.elements
(<music21.chord.Chord C4 E4 G4>,)

>>> stripped3 = m2.stripTies(matchByPitch=False)
>>> stripped3.elements
(<music21.chord.Chord C4 E4 G4>,
 <music21.chord.Chord C4 E4 G4>,
 <music21.chord.Chord C4 E4 G4>)

Now correct the tie types on c1 and try the strict option:

>>> c1.notes[0].tie = tie.Tie('continue')
>>> c1.notes[1].tie = tie.Tie('continue')
>>> c1.notes[2].tie = tie.Tie('continue')
>>> stripped4 = m2.stripTies(matchByPitch=False)
>>> stripped4.elements
(<music21.chord.Chord C4 E4 G4>,)

Now replace the first element with just a single C4 note.
The following chords will be merged with each other, but not with the single
note, even on `matchByPitch=False`.
(`matchByPitch=False` is permissive about pitch but strict about cardinality.)

>>> newC = note.Note('C4')
>>> newC.tie = tie.Tie('start')
>>> m2.replace(c0, newC)
>>> stripped5 = m2.stripTies(matchByPitch=False)
>>> stripped5.elements
(<music21.note.Note C>, <music21.chord.Chord C4 E4 G4>)

* Changed in v7: `matchByPitch` defaults True
r=  FTr@  Nc                     U R                   S:  $ r/  r  )r   	_iterators     rJ   r  "Stream.stripTies.<locals>.<lambda>  s    ""2"2Q"6rI   c                *  > [        U S5      (       aG  [        U [        R                  5      (       d(  U R                  b  U R                  R
                  S:X  a  gT(       d  [        U [        R                  5      (       a  [        T[        R                  5      (       a  SU R                   Vs/ s H  oR                  PM     sn;  ab  U R                   Vs1 s H  oR                  R
                  iM     snS1:X  a-  [        TR                  5      [        U R                  5      :X  a  gT(       d  gTb  TT;   a{  [        TS5      (       aj  [        U S5      (       aY  [        T[        R                  5      (       d:  [        U [        R                  5      (       d  TR                  U R                  :X  a  gTb  [        U [        R                  5      (       d  TT;   a  [        TS5      (       a  [        U S5      (       a  [        TR                  5      [        U R                  5      :w  a  g[        TR                  U R                  5       H7  u  p#UR                  UR                  :w  d  UR                  U5      (       a  M7    g   ggs  snf s  snf )z:
updateEndMatch based on nList, iLast, matchByPitch, etc.
r'   Nr   TFr&   r\   )rT  r   r   r  r'   r   r[   r   r\   r&   r%   r  r  r  isEnharmonic)nInnerinner_ppLastpInneriLastr:  nLastposConnecteds       rJ   updateEndMatch(Stream.stripTies.<locals>.updateEndMatch  s    &&&vu{{;;

.

61 !"65;;77"5%++66$MW[[$MM9?Fg))F6(REMM*c&...AA " !-w//00 'uekk::&vu{{;;v||3!&vtyy99-y11	22u}}%V^^)<< %(%GME zzV[[08J8J68R8R$	 &H
 S %NFs   .JJc                   U R                   c  gU R                   R                  S:w  a  g[        U [        R                  5      (       a>  U R
                   H.  nUR                   c    gUR                   R                  S:w  d  M.    g   g)NFcontinueT)r'   r   r   r   r  r[   )nrinnerNs     rJ   allTiesAreContinue,Stream.stripTies.<locals>.allTiesAreContinue  sg    vv~vv{{j( "ekk** hhFzz)$zz*4$	 '
 rI   r   rm  r'   r   rR  r  z(aggregated ties have a zero duration sumr$  rq  r  )rS  note.NotRestrq  r  )$rL  r.   r5  r*  r   rQ   r=  r  r7  rQ  r  r  r6   r  r   rT  r'   r   rU   r   r%   r  r\   r  ro  r   r   r  r   linkedr   informSitesr:  r  rA  r  )r   rF  r:  rQ  r-  r  fnotes_and_rests	posDeleterO  rU  r  endMatchr  tempEndMatchdurSumqchanging_noteqLenr:  r%  nTargetrL  rM  rN  s     `                   @@@rJ   r=  r>    s   X 11+>II (-	$'')) 11&9D|D :     %%D|D &    &'oo&?&?6'

&( 	 	=	 =	~	 s?+,AH"A1uA'. 5!!)

g-=E$=$%3L l* ''*  !U##)

j0# ''*$H! $2!#4L#$++A.#( )*s#('** "%66C<NRUVWV_V_R`<` )*s$++A.$H $&L$H )!, x##A&|$q( $&L %ab)Aa0>>>F$$Q' * Q;)*TUU !"t'7'7VW9Y Z$22$--4448M**1.4TF].C+ %)! ))+ **B!-ab!1*51R744 / 6 - "2 %  "c -h 	A &(G Wd3   rI   c                  ^ U4S jnU R                  5       R                  R                  5       n[        U5       GH  u  pV/ n[	        U[
        R                  5      (       a  U/nO-[	        U[        R                  5      (       a  [        U5      nOMX  U" XEUR                  U5      UR                  R                  -   5      n[        R                  " Xx5       H  u  p/ n[	        U
[
        R                  5      (       a  U
/nO*[	        U
[        R                  5      (       a  [        U
5      nU H  n[        UR                   U5      [        U	R                   U5      :X  d  M3  U	R"                  c  ["        R$                  " S5      U	l        O+U	R"                  R&                  S:X  a  SU	R"                  l        ["        R$                  " S5      Ul          M     M     GM     g)aY  
Connect any adjacent pitch space values that are the
same with a Tie. Adjacent pitches can be Chords, Notes, or Voices.

If `ignoreRests` is True, rests that occur between events will not be
considered in matching pitches.

The `pitchAttr` determines the pitch attribute that is
used for comparison. Any valid pitch attribute name can be used.
c                  > U[        U 5      S-
  :X  a  / $ T(       aw  [        US-   [        U R                  5      5       HQ  nU R                  U   n[        U[        R
                  5      (       d  M3  U R                  U   R                  U 5      n  O   U R                  U5      n/ nU H5  n[        U[        R
                  5      (       d  M$  UR                  U5        M7     U$ )Nrm  )	r   r  r   r   r%   r  r  r   rU   )		srcStreamcurrentIndextargetOffsetjr   r=   r  matchElignoreRestss	           rJ   _getNextElements+Stream.extendTies.<locals>._getNextElements  s    s9~11 	|a/Y5H5H1IJA",,Q/B!"dll33'0':':1'='M'M%(' K 11,?ED gt||44KK( ! KrI   Nr   r   rR  )rQ  r[   r6   r  r   r%   r  r   r  r   r  r   r  	itertoolsproductrV  r&   r'   Tier   )r   rl  	pitchAttrrm  srcFlatr  ro   pSrcconnectionsr-  r  mSrcra  s    `           rJ   
extendTiesStream.extendTies~  sY   	6 ,,.&&--/g&DAD!TYY''sAu{{++Aw*7+,+<+<W+E

H`H`+`bK "))$< a++3D5;;//7DAqww	2gaggy6QQ55=$'GGG$4AEUUZZ61)3AEEJ #  = 'rI   c                   ^  T R                   (       d  T R                  (       d  U(       aS  T R                  R                  U 4S jS9  T R                  R                  U 4S jS9  T R                  SSSS9  ST l         gg)ay  
Sort this Stream in place by offset, then priority, then
standard class sort order (e.g., Clefs before KeySignatures before
TimeSignatures).

Note that Streams automatically sort themselves unless
autoSort is set to False (as in the example below)

If `force` is True, a sort will be attempted regardless of any other parameters.


>>> n1 = note.Note('A')
>>> n2 = note.Note('B')
>>> s = stream.Stream()
>>> s.autoSort = False
>>> s.insert(100, n2)
>>> s.insert(0, n1)  # now a has a lower offset by higher index
>>> [n.name for n in s]
['B', 'A']
>>> s[0].name
'B'
>>> s.sort()
>>> s[0].name
'A'
>>> [n.name for n in s]
['A', 'B']
c                &   > U R                  T5      $ rs   r  r  s    rJ   r  Stream.sort.<locals>.<lambda>  s    akk$.?rI   r!   c                &   > U R                  T5      $ rs   r  r  s    rJ   r  r{    s    T1BrI   F)r
  rx  	keepIndexTN)ra   r   r   r   r   r   )r   forces   ` rJ   r   Stream.sort  sn    < $--ENN$?@""'B"C $$"# % 
 !DM 5:rI   c                   U R                   R                  S5      nUb  U$ [        R                  " U R                  5      n[        R                  " U R                  5      n[        R                  " U 5      nX$l        X4l        X#-    HN  nUR                  XPR                  U5      SS9  UR                  R                  U5        UR                  U5        MP     UR                  5         X@R                   S'   U$ )a  
(TL;DR: you probably do not need to call this method unless you have turned `.autoSort` to
off.)

Returns a new Stream where all the elements are sorted according to offset time, then
priority, then classSortOrder (so that, for instance, a Clef at offset 0 appears before
a Note at offset 0).

If this Stream is not flat, then only the elements directly in the stream itself are sorted.
To sort all, run myStream.flatten().sorted().

For instance, here is an unsorted Stream:

>>> s = stream.Stream()
>>> s.autoSort = False  # if True, sorting is automatic
>>> s.insert(1, note.Note('D'))
>>> s.insert(0, note.Note('C'))
>>> s.show('text')
{1.0} <music21.note.Note D>
{0.0} <music21.note.Note C>


But a sorted version of the Stream puts the C first:

>>> s.sorted().show('text')
{0.0} <music21.note.Note C>
{1.0} <music21.note.Note D>

While the original stream remains unsorted:

>>> s.show('text')
{1.0} <music21.note.Note D>
{0.0} <music21.note.Note C>


OMIT_FROM_DOCS

>>> s = stream.Stream()
>>> s.autoSort = False
>>> s.repeatInsert(note.Note('C#'), [0, 2, 4])
>>> s.repeatInsert(note.Note('D-'), [1, 3, 5])
>>> s.isSorted
False
>>> g = ''
>>> for myElement in s:
...    g += '%s: %s; ' % (myElement.offset, myElement.name)
>>> g
'0.0: C#; 2.0: C#; 4.0: C#; 1.0: D-; 3.0: D-; 5.0: D-; '
>>> y = s.sorted()
>>> y.isSorted
True
>>> g = ''
>>> for myElement in y:
...    g += '%s: %s; ' % (myElement.offset, myElement.name)
>>> g
'0.0: C#; 1.0: D-; 2.0: C#; 3.0: D-; 4.0: C#; 5.0: D-; '
>>> farRight = note.Note('E')
>>> farRight.priority = 5
>>> farRight.offset = 2.0
>>> y.insert(farRight)
>>> g = ''
>>> for myElement in y:
...    g += '%s: %s; ' % (myElement.offset, myElement.name)
>>> g
'0.0: C#; 1.0: D-; 2.0: C#; 3.0: D-; 4.0: C#; 5.0: D-; 2.0: E; '
>>> z = y.sorted()
>>> g = ''
>>> for myElement in z:
...    g += '%s: %s; ' % (myElement.offset, myElement.name)
>>> g
'0.0: C#; 1.0: D-; 2.0: C#; 2.0: E; 3.0: D-; 4.0: C#; 5.0: D-; '
>>> z[2].name, z[3].name
('C#', 'E')

* Changed in v7: made into a method, not a property.
r  Tr   )r   getr  r   r   r  r  r)   r  r   r   )r   cache_sortedshallowElementsshallowEndElementsr  ro   s         rJ   r  Stream.sorted  s    \ {{x0#))DNN3!YYt'8'89IIdO &+ 5A""1&8&8&;"MGGKKN  #	 6 	
 !HrI   c                \   U(       a  SnOSnU R                   R                  U5      nUb  U$ [        R                  " U 5      nUR                  [        U5      :w  aW  UR                  n[	        U[
        5      (       a  U[        R                  :  a  [        U5      n[        U5      S-   U-   nXdl        [        R                  " U5      nXl        X'l        Xtl
        0 Ul         0 Ul        / Ul        / Ul        UR#                  5         [$        R&                  " U SSSS9nU H<  n	U	R(                  (       a	  U(       d  M  UR+                  UR-                  5       U	SS9  M>     U(       d  SUl        U R0                  SL a  UR3                  5         OUR#                  5         X@R                   U'   U$ )ar  
A very important method that returns a new Stream
that has all sub-containers "flattened" within it,
that is, it returns a new Stream where no elements nest within
other elements.

Here is a simple example of the usefulness of .flatten().  We
will create a Score with two Parts in it, each with two Notes:

>>> sc = stream.Score()
>>> p1 = stream.Part()
>>> p1.id = 'part1'
>>> n1 = note.Note('C4')
>>> n2 = note.Note('D4')
>>> p1.append(n1)
>>> p1.append(n2)

>>> p2 = stream.Part()
>>> p2.id = 'part2'
>>> n3 = note.Note('E4')
>>> n4 = note.Note('F4')
>>> p2.append(n3)
>>> p2.append(n4)

>>> sc.insert(0, p1)
>>> sc.insert(0, p2)

When we look at sc, we will see only the two parts:

>>> sc.elements
(<music21.stream.Part part1>, <music21.stream.Part part2>)

We can get at the notes by using the indices of the
stream to get the parts and then looking at the .elements
there:

>>> sc[0].elements
(<music21.note.Note C>, <music21.note.Note D>)

>>> sc.getElementById('part2').elements
(<music21.note.Note E>, <music21.note.Note F>)

If, however, we want to get all the notes, storing their
offsets related to the beginning of the containing stream,
one way
is via calling .flatten() on sc and looking at the elements
there:

>>> sc.flatten().elements
(<music21.note.Note C>, <music21.note.Note E>,
 <music21.note.Note D>, <music21.note.Note F>)

Flattening a stream is a great way to get at all the notes in
a larger piece.  For instance if we load a four-part
Bach chorale into music21 from the integrated corpus, it
will appear at first that there are no notes in the piece:

>>> bwv66 = corpus.parse('bach/bwv66.6')
>>> len(bwv66.notes)
0

This is because all the notes in the piece lie within :class:`music21.stream.Measure`
objects and those measures lie within :class:`music21.stream.Part`
objects.  It'd be a pain to navigate all the way through all those
objects just to count notes.  Fortunately we can get a Stream of
all the notes in the piece with .flatten().notes and then use the
length of that Stream to count notes:

>>> bwv66flat = bwv66.flatten()
>>> len(bwv66flat.notes)
165

When, as is commonly the case, we want to find all the notes,
but do not care to have offsets related to the origin of the stream,
then `.recurse()` is a more efficient way of working:

>>> len(bwv66.recurse().notes)
165

If `retainContainers=True` then a "semiFlat" version of the stream
is returned where Streams are also included in the output stream.

In general, you will not need to use this because `.recurse()` is
more efficient and does not lead to problems of the same
object appearing in the hierarchy more than once.

>>> n1 = note.Note('C5')
>>> m1 = stream.Measure([n1], number='1a')
>>> p1 = stream.Part([m1])
>>> p1.id = 'part1'

>>> n2 = note.Note('D5')
>>> m2 = stream.Measure([n2], number='1b')
>>> p2 = stream.Part([m2])
>>> p2.id = 'part2'

>>> sc = stream.Score([p1, p2])

`sf` will be the "semi-flattened" version of the score.

>>> sf = sc.flatten(retainContainers=True)
>>> sf.elements
(<music21.stream.Part part1>,
 <music21.stream.Measure 1a offset=0.0>,
 <music21.stream.Part part2>,
 <music21.stream.Measure 1b offset=0.0>,
 <music21.note.Note C>,
 <music21.note.Note D>)
>>> sf[0]
<music21.stream.Part part1>

Notice that these all return the same object:

>>> sf[0][0][0]
<music21.note.Note C>
>>> sf[1][0]
<music21.note.Note C>
>>> sf[4]
<music21.note.Note C>

Unless it is important to get iterate in order from
front of score to back of the score, you are generally better off using recurse
instead of `.flatten(retainContainers=True)`, with `.getOffsetInHierarchy()`
to figure out where in the score each element lies.

For instance, this is how we can iterate using recurse():

>>> for el in sc.recurse():
...     print(el)
<music21.stream.Part part1>
<music21.stream.Measure 1a offset=0.0>
<music21.note.Note C>
<music21.stream.Part part2>
<music21.stream.Measure 1b offset=0.0>
<music21.note.Note D>

If you look back to our simple example of four notes above,
you can see that the E (the first note in part2) comes before the D
(the second note of part1).  This is because the flat stream
is automatically sorted like all streams are by default.  The
next example shows how to change this behavior.

>>> s = stream.Stream()
>>> s.autoSort = False
>>> s.repeatInsert(note.Note('C#'), [0, 2, 4])
>>> s.repeatInsert(note.Note('D-'), [1, 3, 5])
>>> s.isSorted
False

>>> g = ''
>>> for myElement in s:
...    g += '%s: %s; ' % (myElement.offset, myElement.name)
...

>>> g
'0.0: C#; 2.0: C#; 4.0: C#; 1.0: D-; 3.0: D-; 5.0: D-; '

>>> y = s.sorted()
>>> y.isSorted
True

>>> g = ''
>>> for myElement in y:
...    g += '%s: %s; ' % (myElement.offset, myElement.name)
...

>>> g
'0.0: C#; 1.0: D-; 2.0: C#; 3.0: D-; 4.0: C#; 5.0: D-; '

>>> q = stream.Stream()
>>> for i in range(5):
...     p = stream.Stream()
...     p.repeatInsert(base.Music21Object(), [0, 1, 2, 3, 4])
...     q.insert(i * 10, p)
...

>>> len(q)
5

>>> qf = q.flatten()
>>> len(qf)
25
>>> qf[24].offset
44.0

Note that combining `.flatten(retainContainers=True)` with pure `.flatten()`
can lead to unstable Streams where the same object appears more than once,
in violation of a `music21` lookup rule.

>>> sc.flatten(retainContainers=True).flatten().elements
(<music21.note.Note C>,
 <music21.note.Note C>,
 <music21.note.Note C>,
 <music21.note.Note D>,
 <music21.note.Note D>,
 <music21.note.Note D>)

OMIT_FROM_DOCS

>>> r = stream.Stream()
>>> for j in range(5):
...   q = stream.Stream()
...   for i in range(5):
...      p = stream.Stream()
...      p.repeatInsert(base.Music21Object(), [0, 1, 2, 3, 4])
...      q.insert(i * 10, p)
...   r.insert(j * 100, q)

>>> len(r)
5

>>> len(r.flatten())
125

>>> r.flatten()[124].offset
444.0
semiFlatrZ   _FT)r>  r  ignoreSortingr  )r   r  r  r   r   r   r   #minIdNumberToConsiderMemoryLocationr   r   r   
DerivationrN  rO  r   r   r   r   r/   RecursiveIteratorrt   r   currentHierarchyOffsetrc   rb   r   )
r   retainContainersrO  cached_versionsNewsOldIdnewIdsNew_derivationriro   s
             rJ   rQ  Stream.flattenX  su   x FF0%!!
 yy77bhWWF&#&&6H4`4`+`VK#%.EG$//5!%!')   "5=5O5O$	6
 Azz"2OOB557+0  2   DK==D IIK$$&"FrI   c                2    U R                  SS9nSUl        U$ )z
Deprecated: use `.flatten()` instead

A property that returns the same flattened representation as `.flatten()`
as of music21 v7.

See :meth:`~music21.stream.base.Stream.flatten()` for documentation.
F)r  T)rQ  r{   )r   
flatStreams     rJ   rZ   Stream.flatq  s"     \\5\9
26
/rI   rC   )rv  r>  r  r  c                   [         R                  " [        R                  [           [        R                  " U 5      5      $ rs   )r   r   r/   r  r   r   rv  r>  r  r  s        rJ   rY   Stream.recurse  s,     vvh00<h>X>XY]>^__rI   )r>  r  r  c                   [         R                  " [        R                  [           [        R                  " U 5      R                  [        5      5      $ rs   )r   r   r/   r  rQ   r   r  s        rJ   rY   r    s=     vvh008006II&QS 	SrI   c                   [         R                  " U UUUS9nU(       a  UR                  U5      n[        R                  (       a3  U(       a,  [        R
                  " [         R                  [           U5      $ U$ )a  
`.recurse()` is a fundamental method of music21 for getting into
elements contained in a Score, Part, or Measure, where elements such as
notes are contained in sub-Stream elements.

Returns an iterator that iterates over a list of Music21Objects
contained in the Stream, starting with self's elements (unless
includeSelf=True in which case, it starts with the element itself),
and whenever finding a Stream subclass in self,
that Stream subclass's elements.

Here's an example. Let's create a simple score.

>>> s = stream.Score(id='mainScore')
>>> p0 = stream.Part(id='part0')
>>> p1 = stream.Part(id='part1')

>>> m01 = stream.Measure(number=1)
>>> m01.append(note.Note('C', type='whole'))
>>> m02 = stream.Measure(number=2)
>>> m02.append(note.Note('D', type='whole'))
>>> m11 = stream.Measure(number=1)
>>> m11.append(note.Note('E', type='whole'))
>>> m12 = stream.Measure(number=2)
>>> m12.append(note.Note('F', type='whole'))

>>> p0.append([m01, m02])
>>> p1.append([m11, m12])

>>> s.insert(0, p0)
>>> s.insert(0, p1)
>>> s.show('text')
{0.0} <music21.stream.Part part0>
    {0.0} <music21.stream.Measure 1 offset=0.0>
        {0.0} <music21.note.Note C>
    {4.0} <music21.stream.Measure 2 offset=4.0>
        {0.0} <music21.note.Note D>
{0.0} <music21.stream.Part part1>
    {0.0} <music21.stream.Measure 1 offset=0.0>
        {0.0} <music21.note.Note E>
    {4.0} <music21.stream.Measure 2 offset=4.0>
        {0.0} <music21.note.Note F>

Now we could assign the `.recurse()` method to something,
but that won't have much effect:

>>> sRecurse = s.recurse()
>>> sRecurse
<music21.stream.iterator.RecursiveIterator for Score:mainScore @:0>

So, that's not how we use `.recurse()`.  Instead, use it in a `for` loop:

>>> for el in s.recurse():
...     tup = (el, el.offset, el.activeSite)
...     print(tup)
(<music21.stream.Part part0>, 0.0, <music21.stream.Score mainScore>)
(<music21.stream.Measure 1 offset=0.0>, 0.0, <music21.stream.Part part0>)
(<music21.note.Note C>, 0.0, <music21.stream.Measure 1 offset=0.0>)
(<music21.stream.Measure 2 offset=4.0>, 4.0, <music21.stream.Part part0>)
(<music21.note.Note D>, 0.0, <music21.stream.Measure 2 offset=4.0>)
(<music21.stream.Part part1>, 0.0, <music21.stream.Score mainScore>)
(<music21.stream.Measure 1 offset=0.0>, 0.0, <music21.stream.Part part1>)
(<music21.note.Note E>, 0.0, <music21.stream.Measure 1 offset=0.0>)
(<music21.stream.Measure 2 offset=4.0>, 4.0, <music21.stream.Part part1>)
(<music21.note.Note F>, 0.0, <music21.stream.Measure 2 offset=4.0>)

If we specify `includeSelf=True` then the original stream is also iterated:

>>> for el in s.recurse(includeSelf=True):
...     tup = (el, el.offset, el.activeSite)
...     print(tup)
(<music21.stream.Score mainScore>, 0.0, None)
(<music21.stream.Part part0>, 0.0, <music21.stream.Score mainScore>)
(<music21.stream.Measure 1 offset=0.0>, 0.0, <music21.stream.Part part0>)
(<music21.note.Note C>, 0.0, <music21.stream.Measure 1 offset=0.0>)
...

Notice that like calling `.show('text')`, the offsets are relative to their containers.

Compare the difference between putting `.recurse().notes` and `.flatten().notes`:

>>> for el in s.recurse().notes:
...     tup = (el, el.offset, el.activeSite)
...     print(tup)
(<music21.note.Note C>, 0.0, <music21.stream.Measure 1 offset=0.0>)
(<music21.note.Note D>, 0.0, <music21.stream.Measure 2 offset=4.0>)
(<music21.note.Note E>, 0.0, <music21.stream.Measure 1 offset=0.0>)
(<music21.note.Note F>, 0.0, <music21.stream.Measure 2 offset=4.0>)

>>> for el in s.flatten().notes:
...     tup = (el, el.offset, el.activeSite)
...     print(tup)
(<music21.note.Note C>, 0.0, <music21.stream.Score mainScore_flat>)
(<music21.note.Note E>, 0.0, <music21.stream.Score mainScore_flat>)
(<music21.note.Note D>, 4.0, <music21.stream.Score mainScore_flat>)
(<music21.note.Note F>, 4.0, <music21.stream.Score mainScore_flat>)

If you don't need correct offsets or activeSites, set `restoreActiveSites` to `False`.
Then the last offset/activeSite will be used.  It's a bit of a speedup, but leads to some
bad code, so use it only in highly optimized situations.

We'll also test using multiple classes here. The Stream given is the same as the
s.flatten().notes stream.

>>> for el in s.recurse(classFilter=('Note', 'Rest'), restoreActiveSites=False):
...     tup = (el, el.offset, el.activeSite)
...     print(tup)
(<music21.note.Note C>, 0.0, <music21.stream.Score mainScore_flat>)
(<music21.note.Note D>, 4.0, <music21.stream.Score mainScore_flat>)
(<music21.note.Note E>, 0.0, <music21.stream.Score mainScore_flat>)
(<music21.note.Note F>, 4.0, <music21.stream.Score mainScore_flat>)

So, this is pretty unreliable so don't use it unless the tiny speedup is worth it.

The other two attributes are pretty self-explanatory: `streamsOnly` will put only Streams
in, while `includeSelf` will add the initial stream from recursion.  If the inclusion or
exclusion of `self` is important to you, put it in explicitly.

>>> for el in s.recurse(includeSelf=False, streamsOnly=True):
...     tup = (el, el.offset, el.activeSite)
...     print(tup)
(<music21.stream.Part part0>, 0.0, <music21.stream.Score mainScore>)
(<music21.stream.Measure 1 offset=0.0>, 0.0, <music21.stream.Part part0>)
(<music21.stream.Measure 2 offset=4.0>, 4.0, <music21.stream.Part part0>)
(<music21.stream.Part part1>, 0.0, <music21.stream.Score mainScore>)
(<music21.stream.Measure 1 offset=0.0>, 0.0, <music21.stream.Part part1>)
(<music21.stream.Measure 2 offset=4.0>, 4.0, <music21.stream.Part part1>)

.. warning::

    Remember that like all iterators, it is dangerous to alter
    the components of the Stream being iterated over during iteration.
    if you need to edit while recursing, list(s.recurse()) is safer.

* Changed in v5.5: All attributes are keyword only.
* Changed in v8: removed parameter `skipSelf`.  Use `includeSelf` instead.
)rv  r>  r  )r/   r  r   r   r  r   rQ   )r   rv  r>  r  r  r  s         rJ   rY   r    s`    ` 6>5O5O#1#	6
 &&{3B??{66(44V<bAA	rI   r  c                   UR                   nU R                  SSSS9 H$  nXC;   d  M
  U(       a  UR                  U5        Us  $    g)a  
Returns the container in a hierarchy that this element belongs to.

For instance, assume a Note (n) is in a Measure (m1) which is in a Part, in a Score (s1),
and the Note is also in another hierarchy (say, a chordified version of a Score, s2).
if s1.containerInHierarchy(n) is called, it will return m1,
the Measure that contains the note.

Unless `setActiveSite` is False, n's activeSite will be set to m1, and
its `.offset` will be the offset in `m1`.

>>> s1 = stream.Score(id='s1')
>>> p1 = stream.Part()
>>> m1 = stream.Measure(id='m1')
>>> n = note.Note('D')
>>> m1.append(n)
>>> p1.append(m1)
>>> s1.insert(0, p1)
>>> s2 = stream.Stream(id='s2')
>>> s2.append(n)
>>> n.activeSite.id
's2'
>>> s1.containerInHierarchy(n).id
'm1'
>>> n.activeSite.id
'm1'
>>> n.activeSite = s2
>>> s1.containerInHierarchy(n, setActiveSite=False).id
'm1'
>>> n.activeSite.id
's2'

If the element cannot be found in the hierarchy then None is returned.

>>> s3 = stream.Stream()
>>> s3.containerInHierarchy(n) is None
True
TFr=  N)r)   rY   r   )r   r   r  elSitesr  s        rJ   r   Stream.containerInHierarchy/   sI    X (($DUZ[A| ((,	 \
 rI   c                    U R                   SLa  U R                  5         U R                  SSS9 H-  nUR                  (       d  M  UR                  5         SUl         M/     SU l         g)z
Clean this Stream: for self and all elements, purge all dead locations
and remove all non-contained sites. Further, restore all active sites.
FTr  N)r   r   rY   rt   )r   ro   s     rJ   makeImmutableStream.makeImmutablec   sU    
 ==%IIK$EBA
 zzz"
 C rI   c                    SU l         U(       a%  U R                  SS9 H  nUR                  SS9  M     U R                  5         g )NTru  Fr$  )r   rY   makeMutabler   )r   rY   ro   s      rJ   r  Stream.makeMutablet   s=    \\d\3e, 4 	  "rI   c                   SU R                   ;   a  U R                   S   b  OU R                  (       d  SU R                   S'   OU R                  SL a.  U R                  S   nU R                  U5      U R                   S'   O]SnU R                   H   nU R                  U5      nUb  XB:  d  M  UnM"     Ub  [	        U5      U R                   S'   OSU R                   S'   U R                   S   $ )al  
Get start time of element with the highest offset in the Stream.
Note the difference between this property and highestTime
which gets the end time of the highestOffset

>>> stream1 = stream.Stream()
>>> for offset in [0, 4, 8]:
...     n = note.Note('G#', type='whole')
...     stream1.insert(offset, n)
>>> stream1.highestOffset
8.0
>>> stream1.highestTime
12.0
HighestOffsetNrl   Tr   )r   r   ra   r  r  )r   eLasthighestOffsetSoFarro   candidateOffsets        rJ   highestOffsetStream.highestOffset   s    " dkk)dkk/.J.V+.DKK(]]d"NN2&E+/+=+=e+DDKK(!%^^"&"4"4Q"7%-1U)8& $
 "-/45G/HO,/3O,{{?++rI   c                     XR                   S'   g)z
For internal use only.
HighestTimeN)r   r@  s     rJ   r  Stream._setHighestTime   s     &+M"rI   c                d   SU R                   ;   a  U R                   S   b  OU R                  (       d  SU R                   S'   gSnU R                   H6  nU R                  U5      UR                  R                  -   n[        X5      nM8     [        U5      U R                   S'   U R                   S   $ )a  
Returns the maximum of all Element offsets plus their Duration
in quarter lengths. This value usually represents the last
"release" in the Stream.

Stream.duration is normally equal to the highestTime
expressed as a Duration object, but it can be set separately
for advanced operations.

Example: Insert a dotted half note at position 0 and see where
it cuts off:

>>> p1 = stream.Stream()
>>> p1.highestTime
0.0

>>> n = note.Note('A-')
>>> n.quarterLength = 3
>>> p1.insert(0, n)
>>> p1.highestTime
3.0

Now insert in the same stream, the dotted half note
at positions 1, 2, 3, 4 and see when the final note cuts off:

>>> p1.repeatInsert(n, [1, 2, 3, 4])
>>> p1.highestTime
7.0

Another example.

>>> n = note.Note('C#')
>>> n.quarterLength = 3

>>> q = stream.Stream()
>>> for i in [20, 0, 10, 30, 40]:
...    p = stream.Stream()
...    p.repeatInsert(n, [0, 1, 2, 3, 4])
...    q.insert(i, p)  # insert out of order
>>> len(q.flatten())
25
>>> q.highestTime  # this works b/c the component Stream has a duration
47.0
>>> r = q.flatten()

* Changed in v6.5: highestTime can return a Fraction.

OMIT_FROM_DOCS

Make sure that the cache really is empty
>>> 'HighestTime' in r._cache
False
>>> r.highestTime  # 44 + 3
47.0

Can highestTime be a fraction?

>>> n = note.Note('D')
>>> n.duration.quarterLength = 1/3
>>> n.duration.quarterLength
Fraction(1, 3)

>>> s = stream.Stream()
>>> s.append(note.Note('C'))
>>> s.append(n)
>>> s.highestTime
Fraction(4, 3)
r  rl   )r   r   r  r   r  r  r   )r   highestTimeSoFarro   r  s       rJ   r  Stream.highestTime   s    T DKK'DKK,F,R),DKK&" ^^#'#5#5a#8%&ZZ%=%=$>#&'7#I  $ *00@)ADKK&{{=))rI   c                   SU R                   ;   a  U R                   S   b  OU R                  (       d  SU R                   S'   O}U R                  SL a.  U R                  S   nU R                  U5      U R                   S'   O@SnU R                   H   nU R                  U5      nUb  XB:  d  M  UnM"     X R                   S'   U R                   S   $ )a  
Get the start time of the Element with the lowest offset in the Stream.

>>> stream1 = stream.Stream()
>>> for x in range(3, 5):
...     n = note.Note('G#')
...     stream1.insert(x, n)
...
>>> stream1.lowestOffset
3.0


If the Stream is empty, then the lowest offset is 0.0:


>>> stream2 = stream.Stream()
>>> stream2.lowestOffset
0.0



>>> p = stream.Stream()
>>> p.repeatInsert(note.Note('D5'), [0, 1, 2, 3, 4])
>>> q = stream.Stream()
>>> q.repeatInsert(p, list(range(0, 50, 10)))
>>> len(q.flatten())
25
>>> q.lowestOffset
0.0
>>> r = stream.Stream()
>>> r.repeatInsert(q, list(range(97, 500, 100)))
>>> len(r.flatten())
125
>>> r.lowestOffset
97.0
lowestOffsetLowestOffsetNrl   Tr   )r   r   ra   r  )r   eFirstminOffsetSoFarro   r  s        rJ   r  Stream.lowestOffset!  s    L T[[(T[[-H-T*-DKK']]d"^^A&F*.*<*<V*DDKK'!N^^"&"4"4Q"7!)_-M%4N $ +9KK' {{>**rI   c                   U R                   b  U R                   $ SU R                  ;   a  U R                  S   b  U R                  S   $ [        R                  " U R                  S9U R                  S'   U R                  S   $ )zb
Gets the duration of the whole stream (generally the highestTime, but can be set
independently).
Durationr  )r}   r   r   r  r  r   s    rJ   _getDurationStream._getDurationD!  su    
 !!-)))4;;&4;;z+B+N;;z** '/&7&7dFVFV&WDKK
#;;z**rI   c                    [        U[        R                  5      (       a  Xl        gUc  SU l        g[	        SU 35      e)a  
Set the total duration of the Stream independently of the highestTime
of the stream.  Useful to define the scope of the stream as independent
of its constituted elements.

If set to None, then the default behavior of computing automatically from
highestTime is reestablished.
Nz$this must be a Duration object, not )r   r   r  r}   ro  )r   durationObjs     rJ   _setDurationStream._setDurationS!  s>     k8#4#455%0" %)D"!$H"VWWrI   c                "    U R                  5       $ )a=  
Returns the total duration of the Stream, from the beginning of the
stream until the end of the final element.
May be set independently by supplying a Duration object.

>>> a = stream.Stream()
>>> q = note.Note(type='quarter')
>>> a.repeatInsert(q, [0, 1, 2, 3])
>>> a.highestOffset
3.0
>>> a.highestTime
4.0
>>> a.duration
<music21.duration.Duration 4.0>
>>> a.duration.quarterLength
4.0

Advanced usage: override the duration from what is set:

>>> newDuration = duration.Duration('half')
>>> newDuration.quarterLength
2.0

>>> a.duration = newDuration
>>> a.duration.quarterLength
2.0

Restore normal behavior by setting duration to None:

>>> a.duration = None
>>> a.duration
<music21.duration.Duration 4.0>

Note that the highestTime for the stream is the same
whether duration is overridden or not:

>>> a.highestTime
4.0
)r  r   s    rJ   r   Stream.durationc!  s    R   ""rI   c                &    U R                  U5        g rs   )r  r@  s     rJ   r   r  !  s    % rI   c                    g rs   rC   r@  s     rJ   _setSecondsStream._setSeconds!  s    rI   c                   SnU R                  [        R                  5      n/ nU(       d  SnO<U H6  nU R                  U5      nUR	                  5       nUR                  XV/5        M8     [        U5       H  u  nu  pVUS:X  a  US:  a  Sn  O   U(       aH  U R                  S5      nUc  U R                  S:w  a  [        S5      $ gUR	                  5       nSU//U-   nSn[        U5       Hv  u  nu  pV[        U5      S:X  a   XR                  U R                  5      -  n  U$ XVpU[        U5      S-
  :  a
  X7S-      u  pOU R                  nXR                  X-
  5      -  nMx     U$ )NFTr   rl   TempoIndicationnanrm  )r   r+   r  r  getSoundingMetronomeMarkrU   r  rN  r  r  r   durationToSeconds)r   getTempoFromContexttiStreamoffsetMetronomeMarkPairstir  mmr  secoStartmmStartoEndunused_mmEnds                rJ   _getSecondsStream._getSeconds!  s   # **5+@+@A#% "&&&r*002(//8	  $$<=JAwAv!c'&*# >
 ''(9:Bz##s* <' ,,.B),b	{5M'M$#$<=JAw+,1++D,<,<== 
  G3/0144%=!e%D"l'',,T];;C > 
rI   ag  
        Get or set the duration of this Stream in seconds, assuming that
        this object contains a :class:`~music21.tempo.MetronomeMark` or
        :class:`~music21.tempo.MetricModulation`.

        >>> s = corpus.parse('bwv66.6')  # piece without a tempo
        >>> sFlat = s.flatten()
        >>> t = tempo.MetronomeMark('adagio')
        >>> sFlat.insert(0, t)
        >>> sFlat.seconds
        38.57142857...
        >>> tFast = tempo.MetronomeMark('allegro')
        >>> sFlat.replace(t, tFast)
        >>> sFlat.seconds
        16.363...

        Setting seconds on streams is not supported.  Ideally it would instead
        scale all elements to fit, but this is a long way off.

        If a stream does not have a tempo-indication in it then the property
        returns 0.0 if an empty Stream (or self.highestTime is 0.0) or 'nan'
        if there are non-zero duration objects in the stream:

        >>> s = stream.Stream()
        >>> s.seconds
        0.0
        >>> s.insert(0, clef.TrebleClef())
        >>> s.seconds
        0.0
        >>> s.append(note.Note(type='half'))
        >>> s.seconds
        nan
        >>> import math
        >>> math.isnan(s.seconds)
        True

        * Changed in v6.3: return nan rather than raising an exception.  Do not
          attempt to change seconds on a stream, as it did not do what you would expect.
        c                   Uc  U nUR                  5       nUR                  [        R                  5      n/ nUR                  nUR
                  nU(       d)  [        R                  " SS9nUR                  XeU45        U$ [        U5      S:X  a~  US   R                  U5      nUS   R                  5       n	X:  a<  [        R                  " SS9nUR                  XhU45        UR                  XU	45        U$ UR                  XeU	45         U$ / n
U H4  nUR                  U5      nU
R                  XR                  5       /5        M6     U
S   S   U:  aT  [        R                  " SS9nUR                  XjS   S   U45        UR                  U
S   S   U
S   S   U
S   S   45        O&UR                  U
S   S   U
S   S   U
S   S   45        [        U
5       Hh  u  nu  pUS:X  a  M  U[        U
5      S-
  :X  a   UR                  X   S   UX   S   45        MB  UR                  X   S   XS-      S   X   S   45        Mj     U$ )a  
Return a list of offset start, offset end,
MetronomeMark triples for all TempoIndication objects found
in this Stream or a Stream provided by srcObj.

If no MetronomeMarks are found, or an initial region does not
have a MetronomeMark, a mark of quarter equal to 120 is provided as default.

Note that if other TempoIndication objects are defined,
they will be converted to MetronomeMarks and returned here


>>> s = stream.Stream()
>>> s.repeatAppend(note.Note(), 8)
>>> s.insert([6, tempo.MetronomeMark(number=240)])
>>> s.metronomeMarkBoundaries()
[(0.0, 6.0, <music21.tempo.MetronomeMark animato Quarter=120>),
 (6.0, 8.0, <music21.tempo.MetronomeMark Quarter=240>)]
x   r  rm  r   )rQ  r   r+   r  r  r  MetronomeMarkrU   r   r  r  r  )r   r  rs  r  mmBoundariesr  r  	mmDefaultr  r  offsetPairsr  r  s                rJ   metronomeMarkBoundariesStream.metronomeMarkBoundaries!  s   ( >F .."--e.C.CD ))++ ++37II FGV S ]a++G4A!557B!//s;	##\i$@A##QR$89D A ##\$CD@ ; K&&w/""A'B'B'D#EF 
 1~a </!//s;	##\q>!3D%.%0 1##[^A%6Aq8I%0^A%6%8 9 ##[^A%6Aq8I%0^A%6%8 9 (4
7A6#k*Q.. ''):K)4):)< = !''):KA<Nq<Q)4):)< = 5 rI   c                    SnUnSnU HA  u  pxn	Xus=::  a  U:  a#  O  O X8:  a  UnOUnXIR                  Xe-
  5      -  nOM7  Xc:X  a    U$ UnMC     U$ )zf
Given MetronomeMark boundaries, for any pair of offsets,
determine the realized duration in seconds.
rl   N)r  )
r   r  r  r  totalSecondsactiveStart	activeEndr  ro   r  s
             rJ   _accumulatedSecondsStream._accumulatedSeconds?"  sv     	$HA"#!#8 $I !I  4 4Y5L MM   ( %  rI   c                x   Uc  U nU R                  US9nUR                  n/ nUR                  5       (       aB  / n[        UR                  5       H&  u  pgUR                  UR                  5       U45        M(     OUS4/nU H  u  pU H  n
[        U
[        R                  5      (       a  M$  U
R                  R                  n[        U
R                  U5      S5      n0 nUR                  X#U5      US'   UR                  X,X-   5      US'   US   US   -   US'   XS'   XS'   UR                  U5        M     M     U$ )	aM  
Return a list of dictionaries for all elements in this Stream,
where each dictionary defines the real-time characteristics of
the stored events. This will attempt to find
all :class:`~music21.tempo.TempoIndication` subclasses and use these
values to realize tempi. If no initial tempo is found,
a tempo of 120 BPM will be provided.
N)r     offsetSecondsdurationSecondsendTimeSecondsrL   rO   )r  r  r  r  r7  rU   rQ  r   r   r  r   r  roundr  r  )r   r  r  r  
secondsMapr  r  r  r  rO   ro   r  rM   secondsDicts                 rJ   _getSecondsMapStream._getSecondsMapZ"  sW    >F3363B **
F!&--0qyy{A./ 1 tn%F "(Ea--jj..q007;
 !/5/I/I 08O,171K1K &,28-.1<_1M3>?P3Q2R,-)*I&,6L)!!+.%  "(( rI   ab  
        Returns a list where each element is a dictionary
        consisting of the 'offsetSeconds' in seconds of each element in a Stream,
        the 'duration' in seconds, the 'endTimeSeconds' in seconds (that is, the offset
        plus the duration), and the 'element' itself. Also contains a 'voiceIndex' entry which
        contains the voice number of the element, or None if there
        are no voices.


        >>> mm1 = tempo.MetronomeMark(number=120)
        >>> n1 = note.Note(type='quarter')
        >>> c1 = clef.AltoClef()
        >>> n2 = note.Note(type='half')
        >>> s1 = stream.Stream()
        >>> s1.append([mm1, n1, c1, n2])
        >>> om = s1.secondsMap
        >>> om[3]['offsetSeconds']
        0.5
        >>> om[3]['endTimeSeconds']
        1.5
        >>> om[3]['element'] is n2
        True
        >>> om[3]['voiceIndex']
    c                    U R                  [        R                  5      nUR                  S5      nUR	                  5       $ )>
>>> a = stream.Stream()
>>> a.metadata = metadata.Metadata()
r   )r   r#   Metadatar   r   )r   mdLists     rJ   _getMetadataStream._getMetadata"  s6    
 (():):;++A.||~rI   c                    U R                  5       nUb   U R                  U R                  U5      5      nUb3  [        U[        R
                  5      (       a  U R                  SU5        ggg)r  Nr   )r  r$  r%  r   r#   r  rV   )r   metadataObjoldMetadatar(  s       rJ   _setMetadataStream._setMetadata"  s^    
 '')"88DJJ{34D"z+x?P?P'Q'QKK;' (R"rI   a`  
        Get or set the :class:`~music21.metadata.Metadata` object
        found at the beginning (offset 0) of this Stream.

        >>> s = stream.Stream()
        >>> s.metadata = metadata.Metadata()
        >>> s.metadata.composer = 'frank'
        >>> s.metadata.composer
        'frank'

        May also return None if nothing is there.
        c                8    U R                  U R                  5      $ rs   )r  r  r   s    rJ   _getMeasureOffsetStream._getMeasureOffset"  s     ##DOO44rI   c                    g)z!
beat returns None for a Stream.
NrC   r   s    rJ   beatStream.beat"  r?  rI   c                    g)zh
unlike other Music21Objects, streams always have beatStr (beat string) of None

May change to '' soon.
NrC   r   s    rJ   beatStrStream.beatStr"  r?  rI   c                    g)zG
unlike other Music21Objects, streams always have beatDuration of None
NrC   r   s    rJ   beatDurationStream.beatDuration"       rI   c                    g)zG
unlike other Music21Objects, streams always have beatStrength of None
NrC   r   s    rJ   beatStrengthStream.beatStrength"  r	  rI   c                   U nUR                  5       (       d  UR                  5       (       aL  SnU H1  nUR                  (       d  M  UR                  5       (       d  M-  SnUn  O   U(       d  [        S5      eO UR                  5       (       d  [        S5      eUR	                  US/S9nUc  [        S5      eUR
                  nUc  UR                  [        R                  5      nUc  [        S	5      e UR                  XR                  -
  5      nUR                  n	UR                  n
U
S:X  a  Sn
U
c  U(       a  U	S:X  a  UR                  UR                  S/S9nU(       a  UR                  [        R                  5      nU(       d  [        S5      eUR                  UR                  R                   :X  a  UR"                  nOUR                  UR                  5      S-
  nX-   U4$ UR                  UR                  R                   UR$                  R                   -
  5      S-
  nX-   U4$ X4$ !   [        S
5      e= f)a  
Returns a two-element tuple of the beat and the Measure object (or the first one
if there are several at the same offset; unlikely but possible) for a given
offset from the start of this Stream (that contains measures).

Recursively searches for measures.  Note that this method assumes that all parts
have measures of consistent length.  If that's not the case, this
method can be called on the relevant part.

This algorithm should work even for weird time signatures such as 2+3+2/8.


>>> bach = corpus.parse('bach/bwv1.6')
>>> bach.parts[0].measure(2).getContextByClass(meter.TimeSignature)
<music21.meter.TimeSignature 4/4>
>>> returnTuples = []
>>> for offset in [0.0, 1.0, 2.0, 5.0, 5.5]:
...     returnTuples.append(bach.beatAndMeasureFromOffset(offset))
>>> returnTuples
[(4.0, <music21.stream.Measure 0 offset=0.0>),
 (1.0, <music21.stream.Measure 1 offset=1.0>),
 (2.0, <music21.stream.Measure 1 offset=1.0>),
 (1.0, <music21.stream.Measure 2 offset=5.0>),
 (1.5, <music21.stream.Measure 2 offset=5.0>)]

To get just the measureNumber and beat, use a transformation like this:
>>> [(beat, measureObj.number) for beat, measureObj in returnTuples]
[(4.0, 0), (1.0, 1), (2.0, 1), (1.0, 2), (1.5, 2)]

Adapted from contributed code by Dmitri Tymoczko.  With thanks to DT.
FTz3beatAndMeasureFromOffset: could not find any parts!z6beatAndMeasureFromOffset: could not find any measures!rw   r  Nz4beatAndMeasureFromOffset: no measure at that offset.zIbeatAndMeasureFromOffset: could not find a time signature for that place.z?beatAndMeasureFromOffset: offset is beyond the end of the piecer   r   znbeatAndMeasureFromOffset: partial measure found, but could not find a time signature for the preceding measurerm  )r,  r*  rt   ro  r  r,  rN  r$   r*  getBeatProportionrM   r  r  r  r  r.  r  	beatCountr   )r   searchOffsetfixZerosmyStream	foundPart	subStreammyMeasts1myBeatfoundMeasureNumber	numSuffixprevMeasts2padBeatss                 rJ   beatAndMeasureFromOffsetStream.beatAndMeasureFromOffset"  sO   @ ##%%**,,!	!)I$--  ,,..$(	#, "* !)*_`` !  ++--)*bcc ..|	{.S>!"XYY"";**5+>+>?C;![] ]	S**<--+GHF $]] ''	?I X2D2I66v}}QZP[6\H001D1DE)Z[ [ ''3??+H+HH"}}H"44X5I5IJQNH)844 00OO11FOO4Q4QQSUVW)622##S	S!QS Ss   5H2 2H?)rF  rY   rh  c              `   U(       d  U R                  S5      nOU n[        U[        [        45      (       a  [        R
                  " U5      nO[        U[        R                  5      (       a  [        R
                  " US9nO|[        U[        R                  5      (       a  [        R
                  " US9nOH[        R                  (       a1  [        U[        R                  [        R
                  45      (       d   eUnUSL a  UR                  5       nOUR                  5       nU(       a%  UR                  [        R                  " U5      5      nU H  nUR                   (       a  M  [#        US5      (       d  M)  [        U[        R                  5      (       ax  [        U[$        R&                  5      (       dW  [#        US5      (       aD  UR)                  [$        R&                  5      n	UR*                   H  n
UR-                  XSS9  M     M  M  M  UR/                  USS9  M     U(       d  U$ g)aI  
Transpose all specified classes in the
Stream by the
user-provided value. If the value is an integer, the
transposition is treated in half steps. If the value is
a string, any Interval string specification can be
provided.

returns a new Stream by default, but if the
optional "inPlace" key is set to True then
it modifies pitches in place.

TODO: for generic interval set accidental by key signature.

>>> aInterval = interval.Interval('d5')

>>> aStream = corpus.parse('bach/bwv324.xml')
>>> part = aStream.parts[0]
>>> [str(p) for p in aStream.parts[0].pitches[:10]]
['B4', 'D5', 'B4', 'B4', 'B4', 'B4', 'C5', 'B4', 'A4', 'A4']

>>> bStream = aStream.parts[0].flatten().transpose('d5')
>>> [str(p) for p in bStream.pitches[:10]]
['F5', 'A-5', 'F5', 'F5', 'F5', 'F5', 'G-5', 'F5', 'E-5', 'E-5']

Test that aStream hasn't been changed:

>>> [str(p) for p in aStream.parts[0].pitches[:10]]
['B4', 'D5', 'B4', 'B4', 'B4', 'B4', 'C5', 'B4', 'A4', 'A4']

>>> cStream = bStream.flatten().transpose('a4')
>>> [str(p) for p in cStream.pitches[:10]]
['B5', 'D6', 'B5', 'B5', 'B5', 'B5', 'C6', 'B5', 'A5', 'A5']

>>> cStream.flatten().transpose(aInterval, inPlace=True)
>>> [str(p) for p in cStream.pitches[:10]]
['F6', 'A-6', 'F6', 'F6', 'F6', 'F6', 'G-6', 'F6', 'E-6', 'E-6']

* Changed in v8: first value is position only, all other values are keyword only
r]   )	chromatic)diatonicTr\   rL  N)rL  r   r   r   r   IntervalChromaticIntervalDiatonicIntervalr   r  GenericIntervalrY   r   r  r0   r  rt   rT  r"   r4  rN  r\   transposePitchKeyAwarer]   )r   r  rF  rY   rh  r  intvr  ro   r   r-  s              rJ   r]   Stream.transposed#  s   f ,,[9DD ec3Z(($$U+Dx99::$$u5Dx8899$$e4D!%(*B*BHDUDU)VWWWWD d?I		I!++G,?,?,PQIAzzq+&&dH$<$<==%a)9)9::wq)?T?T//0@0@A!"A 77d7K "+ @U: KKdK3  KrI   lowest
anchorZeroanchorZeroRecurserF  c               
   US:  d  [        S5      eU(       d  U R                  S5      nOU nUS;   a  [        UR                  5      nO>US;   a  [        UR                  5      nO"US;   a  [        SS5      nO[        SU S	35      eUR
                   HT  nUR                  U5      U-
  U-  nX-  nUR                  Xx5        UR                  (       d  MB  UR                  UUUS
S9  MV     UR                  5         U(       d  U$ g)a  
Scale all offsets by a multiplication factor given
in `amountToScale`. Durations are not altered.

To augment or diminish a Stream,
see the :meth:`~music21.stream.Stream.augmentOrDiminish` method.

The `anchorZero` parameter determines if and/or where
the zero offset is established for the set of
offsets in this Stream before processing.
Offsets are shifted to make either the lower
or upper values the new zero; then offsets are scaled;
then the shifts are removed. Accepted values are None
(no offset shifting), "lowest", or "highest".

The `anchorZeroRecurse` parameter determines the
anchorZero for all embedded Streams, and Streams
embedded within those Streams. If the lowest offset
in an embedded Stream is non-zero, setting this value
to None will allow the space between the start of that
Stream and the first element to be scaled. If the
lowest offset in an embedded Stream is non-zero,
setting this value to 'lowest' will not alter the
space between the start of that Stream and the first
element to be scaled.

To shift all the elements in a Stream, see the
:meth:`~music21.stream.Stream.shiftElements` method.

* Changed in v5: inPlace is default False, and anchorZero, anchorZeroRecurse
  and inPlace are keyword only arguments.
r   'amountToScale must be greater than zeror_   )r)  )highestrs   rm  zan anchorZero value of z is not acceptedTr*  N)ro  rL  r
   r  r  r   r  r  rt   r_   r   )	r   amountToScaler+  r,  rF  rQ  offsetShiftro   r  s	            rJ   r_   Stream.scaleOffsets#  s   R q !"KLL11.AII #"9#9#9:K;&"9#:#:;K6!"1a.K!$;J<GW"XYY$$A ((+k9]JAA **10
 zzz}*;1B'+  -! %* 	%%' rI   c                   US:  d  [        S5      eU(       d  U R                  S5      nOU nUR                  5       R                  S5       H#  nUR                  R                  U5      Ul        M%     USLa  U$ g)a  
Scale all durations by a provided scalar. Offsets are not modified.

To augment or diminish a Stream, see the
:meth:`~music21.stream.Stream.augmentOrDiminish` method.

We do not retain durations in any circumstance;
if inPlace=False, two deepcopies of each duration are done.
r   r.  r`   rQ   TN)ro  rL  rY   r  r   r^   )r   r0  rF  rQ  ro   s        rJ   r`   Stream.scaleDurations $  s{     q !"KLL112BCII""$::8DA55mDAJ E $ rI   c                   US:  d  [        S5      eU(       d  U R                  S5      nOU nUR                  USSSS9  UR                  USS9  U$ )	aQ  
Given a number greater than zero,
multiplies the current quarterLength of the
duration of each element by this number
as well as their offset and returns a new Stream.
Or if `inPlace` is
set to True, modifies the durations of each
element within the stream.


A number of 0.5 will halve the durations and relative
offset positions; a number of 2 will double the
durations and relative offset positions.



Note that the default for inPlace is the opposite
of what it is for augmentOrDiminish on a Duration.
This is done purposely to reflect the most common
usage.



>>> s = stream.Stream()
>>> n = note.Note()
>>> s.repeatAppend(n, 10)
>>> s.highestOffset, s.highestTime
(9.0, 10.0)
>>> s1 = s.augmentOrDiminish(2)
>>> s1.highestOffset, s1.highestTime
(18.0, 20.0)
>>> s1 = s.augmentOrDiminish(0.5)
>>> s1.highestOffset, s1.highestTime
(4.5, 5.0)
r   r.  r^   r)  NT)r0  r+  r,  rF  )r0  rF  )ro  rL  r_   r`   )r   r0  rF  rQ  s       rJ   r^   Stream.augmentOrDiminish8$  sl    H q !"KLL112EFII 	]x15t 	 	E  }d K rI   c                  ^^ T(       d  [         R                  m  S SS jjm        SUU4S jjnUSL a  U R                  S5      nOU nU/nUSL a  [        UR	                  SSS95      nU GHp  n	U	R
                  n
/ n[        U	R                  5       GH$  u  pU(       a  U	R                  U5      nSnUS:  a  S	nS	U-  nT" [        U5      T5      nUR                  U-  nU	R                  X5        [        US
5      (       a.  UR                  S:w  a  UR                  U-  UR                  l        U(       d  M  UR                   R"                  n[%        US5      n['        U[(        R*                  5      (       + =(       d    UR                   R,                  nU(       ae  U
(       a^  U" XS-   WS9u  nnUb:  Ub7  [/        UR                  UR0                  -
  5      nT" [        U5      TUU5      nO'T" [        U5      TU5      nOT" [        U5      TU5      nUR                  S:X  a3  ['        U[(        R2                  5      (       a  UR5                  U5        GM  UR                  UR                   l        [        US
5      (       d  GM  UR                  S:w  d  GM	  UR                  UR                  l        GM'     U	R9                  SS9  U	R;                  U5        GMs     USL a  U$ g)a  
Quantize time values in this Stream by snapping offsets
and/or durations to the nearest multiple of a quarter length value
given as one or more divisors of 1 quarter length. The quantized
value found closest to a divisor multiple will be used.

The `quarterLengthDivisors` provides a flexible way to provide quantization
settings. For example, (2,) will snap all events to eighth note grid.
(4, 3) will snap events to sixteenth notes and eighth note triplets,
whichever is closer. (4, 6) will snap events to sixteenth notes and
sixteenth note triplets.  If quarterLengthDivisors is not specified then
defaults.quantizationQuarterLengthDivisors is used.  The default is (4, 3).

`processOffsets` determines whether the Offsets are quantized.

`processDurations` determines whether the Durations are quantized.

Both are set to True by default.  Setting both to False does nothing to the Stream.

if `inPlace` is True, then the quantization is done on the Stream itself.  If False
(default) then a new quantized Stream of the same class is returned.

If `recurse` is True, then all substreams are also quantized.
If False (default), then only the highest level of the Stream is quantized.

* Changed in v7:
   - `recurse` defaults False
   - look-ahead approach to choosing divisors to avoid gaps when processing durations

>>> n = note.Note()
>>> n.quarterLength = 0.49
>>> s = stream.Stream()
>>> s.repeatInsert(n, [0.1, 0.49, 0.9])
>>> nShort = note.Note()
>>> nShort.quarterLength = 0.26
>>> s.repeatInsert(nShort, [1.49, 1.76])

>>> s.quantize((4,), processOffsets=True, processDurations=True, inPlace=True)
>>> [e.offset for e in s]
[0.0, 0.5, 1.0, 1.5, 1.75]
>>> [e.duration.quarterLength for e in s]
[0.5, 0.5, 0.5, 0.25, 0.25]

The error in quantization is set in the editorial attribute for the note in
two places `.offsetQuantizationError` and `.quarterLengthQuantizationError`:

>>> [e.editorial.offsetQuantizationError for e in s.notes]
[0.1, -0.01, -0.1, -0.01, 0.01]
>>> [e.editorial.quarterLengthQuantizationError for e in s.notes]
[-0.01, -0.01, -0.01, 0.01, 0.01]

With default quarterLengthDivisors:

>>> s = stream.Stream()
>>> s.repeatInsert(n, [0.1, 0.49, 0.9])
>>> nShort = note.Note()
>>> nShort.quarterLength = 0.26
>>> s.repeatInsert(nShort, [1.49, 1.76])
>>> quantized = s.quantize(processOffsets=True, processDurations=True, inPlace=False)
>>> [e.offset for e in quantized]
[0.0, 0.5, 1.0, 1.5, 1.75]
>>> [e.duration.quarterLength for e in quantized]
[0.5, 0.5, 0.5, 0.25, 0.25]

Set `recurse=True` to quantize elements in substreams such as parts, measures, voices:

>>> myPart = converter.parse('tinynotation: c32 d32 e32 f32')
>>> myPart.quantize(inPlace=True)
>>> [e.offset for e in myPart.measure(1).notes]  # no change!
[0.0, 0.125, 0.25, 0.375]

>>> myPart.quantize(inPlace=True, recurse=True)
>>> [e.offset for e in myPart.measure(1).notes]
[0.0, 0.0, 0.25, Fraction(1, 3)]

* New in v7: if both `processDurations` and `processOffsets` are True, then
  the next note's quantized offset is taken into account when quantizing the
  duration of the current note. This is to prevent unnecessary gaps from applying
  different quantization units to adjacent notes:

>>> s2 = stream.Stream()
>>> nOddLength = note.Note(quarterLength=0.385)
>>> s2.repeatInsert(nOddLength, [0, 0.5, 1, 1.5])
>>> s2.show('t', addEndTimes=True)
    {0.0 - 0.385} <music21.note.Note C>
    {0.5 - 0.885} <music21.note.Note C>
    {1.0 - 1.385} <music21.note.Note C>
    {1.5 - 1.885} <music21.note.Note C>

Before v.7, this would have yielded four triplet-eighths (separated by 1/6 QL rests):

>>> s2.quantize(processOffsets=True, processDurations=True, inPlace=True)
>>> s2.show('text', addEndTimes=True)
    {0.0 - 0.5} <music21.note.Note C>
    {0.5 - 1.0} <music21.note.Note C>
    {1.0 - 1.5} <music21.note.Note C>
    {1.5 - 1.8333} <music21.note.Note C>

OMIT_FROM_DOCS

Test changing defaults, running, and changing back:

>>> dd = defaults.quantizationQuarterLengthDivisors
>>> defaults.quantizationQuarterLengthDivisors = (3,)

>>> u = s.quantize(processOffsets=True, processDurations=True, inPlace=False)
>>> [e.offset for e in u]
[0.0, Fraction(1, 3), 1.0, Fraction(4, 3), Fraction(5, 3)]
>>> [e.duration.quarterLength for e in u]
[Fraction(1, 3), Fraction(1, 3), Fraction(1, 3), Fraction(1, 3), Fraction(1, 3)]

Original unchanged because inPlace=False:

>>> [e.offset for e in s]
[Fraction(1, 10), Fraction(49, 100), Fraction(9, 10), Fraction(149, 100), Fraction(44, 25)]

>>> defaults.quantizationQuarterLengthDivisors = dd
>>> v = s.quantize(processOffsets=True, processDurations=True, inPlace=False)
>>> [e.offset for e in v]
[0.0, 0.5, 1.0, 1.5, 1.75]
>>> [e.duration.quarterLength for e in v]
[0.5, 0.5, 0.5, 0.25, 0.25]
Tc                (   / nU H~  nSU-  n[         R                  " X5      u  pxn	U(       d!  US:X  a  Un[        X-
  S5      n	[        U	5      nX6-  S:X  a  Sn
O[	        X7-
  S5      n
UR                  [        XXgX5      5        M     [        U5      nU$ )Nrm  rl      r   )r   nearestMultipler  absr  rU   r9   min)r  divisorszeroAllowed	gapToFillr  divr<   r=   r;   signedErrorInnerr:   bestMatchTuples               rJ   	bestMatch"Stream.quantize.<locals>.bestMatch$  s     24E3w171G1G1U.."u| E',V^Q'?$ 01E#q(#&L#&y'8##>L)$T:JQR    !ZN!!rI   c                   > U R                   US   H;  nU R                  U5      nT" [        U5      T5      nUR                  U:  d  M8  X54s  $    g)NNN)r   r  r  r=   )	useStream
startIndexr  next_elnext_offsetlook_ahead_resultrC  quarterLengthDivisorss         rJ   findNextElementNotCoincident5Stream.quantize.<locals>.findNextElementNotCoincident%  s[    
 %..z{;'55g>$-eK.@BW$X!$**[8"55	 <
 rI   Fquantizer  rm  r   r   	editorial)rG  rH  r  Nr	  )Trl   )rq  r9   )rG  rQ   rH  r   r  r   rq  z>tuple[base.Music21Object | None, BestQuantizationMatch | None])r   !quantizationQuarterLengthDivisorsrL  r   rY   ra   r  r   r  r  r=   r  rT  r>   rP  offsetQuantizationErrorr   r  r  r   r%   r  isGracer   rM   r  rU   quarterLengthQuantizationErrorr   r  )r   rL  processOffsetsprocessDurationsrF  rY   rM  r  
useStreamsrG  originallySortedrests_lacking_durationsr  ro   r  signo_matchTuplerk  r>  next_elementrK  r?  d_matchTuplerC  s    `                     @rJ   rO  Stream.quantizek$  s   H %$,$N$N! 		"
 #	"4
	
	
	 "
	 H	
	 
	 e44Z@LL"^
d?l22tQU2VWJ#I
  )1179#!)"5"56!!//2AD1u!F#,U1X7L#ML$**T1A2218q+..<3K3Kq3P>J>V>VY]>];##11BRB&0DLL&A"A"WQZZEWEWK%*:8*3ASTV 8&7 (38I8U(./@/F/F/Q(RI+4 %b	+@+y,ZL ,5U2Y@UWb+cL'0r<QS^'_#))Q.:a3K3K/66q93?3E3E

0"1k22|7O7OST7TIUIaIaAKKFC 7J ))u)=45] $` e rI   c                   U R                  5       (       d  [        S5      e[        R                  " U 5      nUR	                  5       nU R                  [        5       HL  nSUR                  ;  d  M  [        R                  " U5      nUR                  U R                  U5      U5        MN     U(       a  UR                  nUR                  5        Hl  nUR                  R                  5       (       d  M$  UR                   R"                  nUc  M?  UR                   R$                  S:X  d  M[  UR'                  Xt5        Mn     U$ )a}  
Expand this Stream with repeats. Nested repeats
given with :class:`~music21.bar.Repeat` objects, or
repeats and sections designated with
:class:`~music21.repeat.RepeatExpression` objects, are all expanded.

This method always returns a new Stream, with
deepcopies of all contained elements at all levels.

Uses the :class:`~music21.repeat.Expander` object in the `repeat` module.
z?cannot process repeats on Stream that does not contain measuresRepeatBracketr  )r,  ro  r(   Expanderprocessr  rw   r/  r  r  rV   r  r  rY   r)   r  r   rN  rO  r  )r   copySpannersexr  ro   eNewr  rN  s           rJ   expandRepeatsStream.expandRepeatsW%  s     !!!Q  __T"zz| ++G4Aaii/}}Q'D..q148 5  !..M \\^77))++\\00F* ! 3 3~ E%;;FF $ rI   r  rC  rF  c                  U(       d  U R                  S5      nOU nUR                  5       (       a?  UR                  [        5       H  nUR	                  UX#SS9  M     UR                  5         U$ UR                  5       (       a?  UR                  [        5       H  nUR	                  UX#SS9  M     UR                  5         U$ [        R                  " U5      (       d  U/nUb  XPR                  U5         /nOUR                  nU GHE  n	[        [        U5      5      U	R                  :  a  M(  [        [        U5      5      U	R                  :X  dO  / n
Sn U
R                  X[!        U5      -     5        US-  n[        [        U
5      5      nXR                  :  a  OMJ  Un
[        [        U
5      5      U	R                  :X  d&  [#        S[        U
5      < SU	R                  < 35      eU	R%                  XS	9nU	R'                  U5      nUR)                  U	5        U H+  nUR+                  X5        [        XR                  -   5      nM-     GMH     UR                  5         U(       d  U$ g)
a  
Slice all :class:`~music21.duration.Duration` objects on all Notes and Rests
of this Stream.
Duration are sliced according to values provided in `quarterLengthList` list.
If the sum of these values is less than the Duration, the values are accumulated
in a loop to try to fill the Duration. If a match cannot be found, an
Exception is raised.

If `target` is None, the entire Stream is processed. Otherwise, only the element
specified is manipulated.

sliceByQuarterLengthsTrh  Nr   rm  z5cannot map quarterLength list into element Duration: z, rC  )rL  r,  r   rw   rj  r   r*  Partr   r~  r%  r  r   r  r  rU   r   ro  splitByQuarterLengthsr  r  r   )r   quarterLengthListr  rC  rF  rQ  r  r-  
eToProcessro   	qlProcessr  sumQLr  oInsertre  s                   rJ   rj  Stream.sliceByQuarterLengths%  sR    112IJII  ""11':''(9/5PT ( V ; ))+''))11$7''(9/5PT ( V 8 ))+  !233!2 3 $JJv$678J"00JAc+,-?C 123qF	$$)c2C.D*DEGFA"3y>2E/  .	
 #i.)Q__<%I9: : **9*FD''	2GQ$$W3 +=+=!=> = D 	%%' rI   rC  rF  c                  U(       d  U R                  S5      nOU nUR                  5       (       a.  UR                  [        5       H  nUR	                  USS9  M     U$ [        5       nUR                   H0  nUR                  U;  d  M  UR                  UR                  5        M2     [        R                  " U5      nUR                  U/SUSS9  U(       d  U$ g)z
Slice all :class:`~music21.duration.Duration` objects on all Notes and Rests of this Stream.
Duration are sliced according to the approximate GCD found in all durations.
sliceByGreatestDivisorTrt  Nrn  r  rC  rF  )rL  r,  r   rw   rv  re  r  r  r  r   approximateGCDrj  )r   rC  rF  rQ  r  uniqueQuarterLengthsro   r?   s           rJ   rv  Stream.sliceByGreatestDivisor%  s     112JKII  ""11':(($(G ;"u((A&::$((9 ) ''(<= 	''7)/3Wd 	( 	T  rI   c           	        U(       d  U R                  S5      nOU nUR                  5       (       aS  UR                  [        5       H8  nU Vs/ s H  oUR	                  U5      -
  PM     n	nUR                  U	USUS9  M:     U$ UR                  5       (       aS  UR                  [        5       H8  n
U Vs/ s H  oU
R	                  U5      -
  PM     n	nU
R                  U	USUS9  M:     U$ UR                  5       nU Vs/ s H  n[        U5      PM     nnU H  nUu  pnnUb  [        U5      [        U5      :w  a  M&  / n[        U5      n[        U5      nU H&  nXs=:  a  U:  d  M  O  M  UR                  U5        M(     U(       d  Ms  UnUnU H1  nUU-
  nUR                  USUUS9u  nnUR                  UU5        UnM3     M     UR                  5         USL a  U$ gs  snf s  snf s  snf )aS  
Given a list of quarter lengths, slice and optionally tie all
Music21Objects crossing these points.

>>> s = stream.Stream()
>>> n = note.Note()
>>> n.duration.type = 'whole'
>>> s.append(n)
>>> post = s.sliceAtOffsets([1, 2, 3], inPlace=True)
>>> [(e.offset, e.quarterLength) for e in s]
[(0.0, 1.0), (1.0, 1.0), (2.0, 1.0), (3.0, 1.0)]
sliceAtOffsetsT)
offsetListrC  rF  rD  NrK  F)rL  r,  r   rw   r  r|  r*  rQ   r"  r   r   rU   rG  r   r   )r   r}  r  rC  rF  rD  rQ  r  r  offsetListLocalr-  r"  obro   r  r  unused_voiceCount	cutPointseNext
oStartNextoCutunused_eCompletes                         rJ   r|  Stream.sliceAtOffsets%  s(   * 112BCII  ""11': NX"XZq'8'8'C#CZ"X  O)0)-8N ! P	 ; ''))11&9MW"XZq'8'8'C#CZ"X  O)0)-8N ! P :  '')	)34AfQi
4B13.At.!ber&z&9IF^F$<D$$$$$Q'   y #
"Az>D.3.H.H%) '/E	 /I /+$e ((E2!"J #' @ 	%%'e o #Y #Y 5s   G#5G( G-c                "   U(       d  U R                  S5      nOU nUR                  5       (       a0  UR                  [        5       H  nUR	                  UUSUS9  M     U$ UR                  5       (       a0  UR                  [        5       H  nUR	                  UUSUS9  M     U$ UR                  SS9nU(       d  [        S5      e[        U5      S:  a  [        S5      eUS   R                  5       n	UR                  U	UUSUS9  U(       d  U$ g	)
z
Slice all elements in the Stream that have a Duration at
the offsets determined to be the beat from the local TimeSignature.

* Changed in v7: return None if inPlace is True
sliceByBeatT)r  rC  rF  rD  )rI  zno time signature was foundrm  z6not yet implemented: slice by changing time signaturesr   N)rL  r,  r   rw   r  r*  rl  rM  ro  r   getBeatOffsetsr|  )
r   r  rC  rF  rD  rQ  r  r-  tsStreamr}  s
             rJ   r  Stream.sliceByBeatX&  s.    11-@II  ""11':V&-&*5K  M ;
 ''))11$7V&-&*5K  M 8
  ..T.B!"?@@x=1!"Z[[a[//1
  (.)0)-8N	 	! 	P  rI   c                    SU R                   ;  d  U R                   S   c<  SnU R                   H  n[        U[        5      (       d  M  Sn  O   XR                   S'   U R                   S   $ )a  
Return a boolean value showing if this Stream contains Measures.

>>> p = stream.Part()
>>> p.repeatAppend(note.Note(), 8)
>>> p.hasMeasures()
False
>>> p.makeMeasures(inPlace=True)
>>> len(p.getElementsByClass(stream.Measure))
2
>>> p.hasMeasures()
True

Only returns True if the immediate Stream has measures, not if there are nested measures:

>>> sc = stream.Score()
>>> sc.append(p)
>>> sc.hasMeasures()
False
r,  FT)r   r   r   rw   r   r  r\  s      rJ   r,  Stream.hasMeasures&  sc    * +t{{=/I/QD~~c7++D	 &
 *.KK&{{=))rI   c                    SU R                   ;  d  U R                   S   c7  SnU R                   H  nSUR                  ;   d  M  Sn  O   XR                   S'   U R                   S   $ )z?
Return a boolean value showing if this Stream contains Voices
r  Fr,  T)r   r   r/  r  s      rJ   r  Stream.hasVoices&  sb     dkk)T[[-E-MD~~ckk)D	 &
 (,KK${{;''rI   c                   SU R                   ;  d  U R                   S   c  SnU R                  (       d  U R                  [        5       H~  n[	        U[
        5      (       a  Sn  Of[	        U[        [        45      (       a  Sn  OGUR                  S:w  a  Sn  O3UR                  [        5      (       d  UR                  (       d  M|  SnM     XR                   S'   U R                   S   $ )a-  
Return a boolean value showing if this Stream contains any Parts,
or Part-like sub-Streams.

Part-like sub-streams are Streams that contain Measures or Notes.
And where no sub-stream begins at an offset besides zero.

>>> s = stream.Score()
>>> s.hasPartLikeStreams()
False
>>> p1 = stream.Part()
>>> p1.repeatAppend(note.Note(), 8)
>>> s.insert(0, p1)
>>> s.hasPartLikeStreams()
True

A stream that has a measure in it is not a part-like stream.

>>> s = stream.Score()
>>> m1 = stream.Measure()
>>> m1.repeatAppend(note.Note(), 4)
>>> s.append(m1)
>>> s.hasPartLikeStreams()
False


A stream with a single generic Stream substream at the beginning has part-like Streams:

>>> s = stream.Score()
>>> m1 = stream.Stream()
>>> m1.repeatAppend(note.Note(), 4)
>>> s.append(m1)
>>> s.hasPartLikeStreams()
True


Adding another though makes it not part-like.

>>> m2 = stream.Stream()
>>> m2.repeatAppend(note.Note(), 4)
>>> s.append(m2)
>>> s.hasPartLikeStreams()
False

Flat objects do not have part-like Streams:

>>> sf = s.flatten()
>>> sf.hasPartLikeStreams()
False
r*  FTrl   )
r   rc   r   rQ   r   rl  rw   r,  rM   r  )r   	multiPartr\  s      rJ   r*  Stream.hasPartLikeStreams&  s    f  t{{2dkkBV6W6_I;;226:C!#t,,$(	#C'5)9::$)	s*$)	 0099 ...$(	% ;& 1:KK,-{{/00rI   c                X    U R                    H  nUR                  5       (       a  M    g   g)z
Return true if this Stream only employs twelve-tone equal-tempered pitch values.


>>> s = stream.Stream()
>>> s.append(note.Note('G#4'))
>>> s.isTwelveTone()
True
>>> s.append(note.Note('G~4'))
>>> s.isTwelveTone()
False
FT)r\   isTwelveTone)r   r-  s     rJ   r  Stream.isTwelveTone'  s'     A>>##  rI   c                d  ^ S m[        U [        [        45      (       a  gU R                  (       a  g[        U [        5      (       a  U R                  5       (       a  g g[        U [        5      (       a  [        U4S jU R                   5       5      $ U R                  5       (       a  T" U 5      $ g)a%  
Return True if, given the context of this Stream or Stream subclass,
contains what appears to be well-formed notation. This often means
the formation of Measures, or a Score that contains Part with Measures.


>>> s = corpus.parse('bwv66.6')
>>> s.isWellFormedNotation()
True
>>> s.parts[0].isWellFormedNotation()
True
>>> s.parts[0].getElementsByClass(stream.Measure).first().isWellFormedNotation()
True

>>> s2 = stream.Score()
>>> m = stream.Measure()
>>> s2.append(m)
>>> s2.isWellFormedNotation()
False

>>> o = stream.Opus([s])
>>> o.isWellFormedNotation()
True
>>> o2 = stream.Opus([s2])
>>> o2.isWellFormedNotation()
False

Only Measures and Voices are allowed to contain notes and rests directly:

>>> m.isWellFormedNotation()
True
>>> s2.append(note.Rest())
>>> s2.isWellFormedNotation()
False
c                L    [        S U R                  [        5       5       5      $ )Nc              3  @   #    U  H  oR                  5       v   M     g 7frs   )r,  )rn   r  s     rJ   rp   QStream.isWellFormedNotation.<locals>.allSubstreamsHaveMeasures.<locals>.<genexpr>E'  s     V0U1}}0Us   )r   r   rQ   )
testStreams    rJ   allSubstreamsHaveMeasures>Stream.isWellFormedNotation.<locals>.allSubstreamsHaveMeasuresD'  s    V
0M0Mf0UVVVrI   TFc              3  4   >#    U  H  nT" U5      v   M     g 7frs   rC   )rn   r  r  s     rJ   rp   .Stream.isWellFormedNotation.<locals>.<genexpr>Q'  s     I[033[s   )
r   rw   r,  r  rl  r,  rf  r   scoresr*  )r   r  s    @rJ   isWellFormedNotationStream.isWellFormedNotation'  s    J	W dWe,--d##!! "  d##IT[[III$$&&,T22rI   c                R    U R                  [        R                  5      nSUl        U$ )a(  
The notesAndRests property of a Stream returns a `StreamIterator`
that consists only of the :class:`~music21.note.GeneralNote` objects found in
the stream.  The new Stream will contain
mostly notes and rests (including
:class:`~music21.note.Note`,
:class:`~music21.chord.Chord`,
:class:`~music21.note.Rest`) but also their subclasses, such as
`Harmony` objects (`ChordSymbols`, `FiguredBass`), etc.


>>> s1 = stream.Stream()
>>> k1 = key.KeySignature(0)  # key of C
>>> n1 = note.Note('B')
>>> r1 = note.Rest()
>>> c1 = chord.Chord(['A', 'B-'])
>>> s1.append([k1, n1, r1, c1])
>>> s1.show('text')
{0.0} <music21.key.KeySignature of no sharps or flats>
{0.0} <music21.note.Note B>
{1.0} <music21.note.Rest quarter>
{2.0} <music21.chord.Chord A B->

`.notesAndRests` removes the `KeySignature` object but keeps the `Rest`.

>>> notes1 = s1.notesAndRests.stream()
>>> notes1.show('text')
{0.0} <music21.note.Note B>
{1.0} <music21.note.Rest quarter>
{2.0} <music21.chord.Chord A B->

The same caveats about `Stream` classes and `.flatten()` in `.notes` apply here.
r  )r   r%   r  overrideDerivationr   r  s     rJ   r  Stream.notesAndRestsX'  s.    H ##D$4$45 	 +:'rI   c                R    U R                  [        R                  5      nSUl        U$ )a  
The `.notes` property of a Stream returns an iterator
that consists only of the notes (that is,
:class:`~music21.note.Note`,
:class:`~music21.chord.Chord`, etc.) found
in the stream. This excludes :class:`~music21.note.Rest` objects.

>>> p1 = stream.Part()
>>> k1 = key.KeySignature(0)  # key of C
>>> n1 = note.Note('B')
>>> r1 = note.Rest()
>>> c1 = chord.Chord(['A', 'B-'])
>>> p1.append([k1, n1, r1, c1])
>>> p1.show('text')
{0.0} <music21.key.KeySignature of no sharps or flats>
{0.0} <music21.note.Note B>
{1.0} <music21.note.Rest quarter>
{2.0} <music21.chord.Chord A B->

>>> noteStream = p1.notes.stream()
>>> noteStream.show('text')
{0.0} <music21.note.Note B>
{2.0} <music21.chord.Chord A B->

Notice that `.notes` returns a :class:`~music21.stream.iterator.StreamIterator` object

>>> p1.notes
<music21.stream.iterator.StreamIterator for Part:0x105b56128 @:0>

Let's add a measure to `p1`:

>>> m1 = stream.Measure()
>>> n2 = note.Note('D')
>>> m1.insert(0, n2)
>>> p1.append(m1)

Now note that `n2` is *not* found in `p1.notes`

>>> p1.notes.stream().show('text')
{0.0} <music21.note.Note B>
{2.0} <music21.chord.Chord A B->

We need to call `p1.flatten().notes` to find it:

>>> p1.flatten().notes.stream().show('text')
{0.0} <music21.note.Note B>
{2.0} <music21.chord.Chord A B->
{3.0} <music21.note.Note D>

(Technical note: All elements of class NotRest are being found
right now.  This will eventually change to also filter out
Unpitched objects, so that all elements returned by
`.notes` have a `.pitches` attribute.
r[   )r   r%   r  r  r  s     rJ   r[   Stream.notes'  s+    p ?C>U>UVZVbVb>c*1'rI   c                   / nU R                    Ho  n[        U[        R                  5      (       a  M$  [        U[        R
                  [        45      (       d  MK  UR                  [        UR                  5      5        Mq     U$ )aM  
Returns all :class:`~music21.pitch.Pitch` objects found in any
element in the Stream as a Python List. Elements such as
Streams, and Chords will have their Pitch objects accumulated as
well. For that reason, a flat representation is not required.

Note that this does not include any ornamental pitches implied
by any ornaments on those Notes and Chords.  To get those, use
the makeNotation.ornamentalPitches(s) method.

Pitch objects are returned in a List, not a Stream.  This usage
differs from the .notes property, but makes sense since Pitch
objects usually have by default a Duration of zero. This is an important difference
between them and :class:`music21.note.Note` objects.

key.Key objects are subclasses of Scales, which DO have a .pitches list, but
they are specifically exempt from looking for pitches, since that is unlikely
what someone wants here.

N.B., TODO: This may turn to an Iterator soon.

>>> a = corpus.parse('bach/bwv324.xml')
>>> partOnePitches = a.parts[0].pitches
>>> len(partOnePitches)
25
>>> partOnePitches[0]
<music21.pitch.Pitch B4>
>>> [str(p) for p in partOnePitches[0:10]]
['B4', 'D5', 'B4', 'B4', 'B4', 'B4', 'C5', 'B4', 'A4', 'A4']


Note that the pitches returned above are
objects, not text:

>>> partOnePitches[0].octave
4

Since all elements with a `.pitches` list are returned and streams themselves have
`.pitches` properties (the docs you are reading now), pitches from embedded streams
are also returned.  Flattening a stream is not necessary.  Whether this is a
feature or a bug is in the eye of the beholder.

>>> len(a.pitches)
104

Chords get their pitches found as well:

>>> c = chord.Chord(['C4', 'E4', 'G4'])
>>> n = note.Note('F#4')
>>> m = stream.Measure()
>>> m.append(n)
>>> m.append(c)
>>> m.pitches
[<music21.pitch.Pitch F#4>, <music21.pitch.Pitch C4>,
 <music21.pitch.Pitch E4>, <music21.pitch.Pitch G4>]
)
r   r   r"   r  r%   r  rQ   extendr   r\   )r   r  ro   s      rJ   r\   Stream.pitches'  sa    t A!SWW%% A 0 0&9::DO,  rI   )	skipRests
skipChordsskipUnisonsskipOctavesskipGapsgetOverlapsc                   / $ rs   rC   	r   r  r  r  r  r  r  noNoner   s	            rJ   findConsecutiveNotesStream.findConsecutiveNotes(  	     	rI   )r  r  r  r  r  c                   / $ rs   rC   r  s	            rJ   r  r  (  r  rI   )r  r  r  r  r  r  r  c                   / $ rs   rC   r  s	            rJ   r  r  !(  r  rI   c               :   U R                   SL a!  U R                  (       a  U R                  5         / n	Sn
SnSnSnSnUSL a  SnU R                  SSS9 GH  nUR                  U:  a>  UR                  [        R                  5      (       a  USL a  U	R                  S5        SnSnSn
SnUR                  [        R                  5      (       a  UR                  nUR                  [        R                  5       GH  nUSL a/  USL a*  UR                  U:  a  U(       d  U	R                  S5        Sn[        U[        R                  5      (       a  USL dd  [        U5      S:w  dU  UR                  R                  US   R                  :w  d.  USL a'  UR                  R                  US   R                  :w  d  M  USL a  UR                  U:  a  M  U	R                  U5        UR                  U:  a  M  UR                  n
[!        U
UR"                  R$                  -   5      nSnUR&                  nGM9  [        U[(        R*                  5      (       Ga  [        UR&                  5      S:  a  USL a*  USL a"  U(       d  U	R                  S5        SnSnGM  GM  GM  USL a@  [        U5      [        UR&                  5      :X  a  S	 UR&                   5       S
 U 5       :X  d{  USL d  UR                  U:  ac  U	R                  U5        UR                  U:  a  GM  UR                  n
[!        U
UR"                  R$                  -   5      nUR&                  nSnGM^  GMa  GMd  USL aD  [        U[        R,                  5      (       a%  USL a   USL a  U	R                  S5        SnSnGM  GM  USL d  GM  [        U[        R,                  5      (       d  GM  [!        UR                  UR"                  R$                  -   5      nGM     GM     USL a  U	R/                  5         U	$ )a  
Returns a list of consecutive *pitched* Notes in a Stream.

A single "None" is placed in the list
at any point there is a discontinuity (such as if there is a
rest between two pitches), unless the `noNone` parameter is True.

How to determine consecutive pitches is a little tricky and there are many options:

* The `skipUnisons` parameter uses the midi-note value (.ps) to determine unisons,
  so enharmonic transitions (F# -> Gb) are
  also skipped if `skipUnisons` is true.  We believe that this is the most common usage.
* However, because
  of this, you cannot completely be sure that the
  x.findConsecutiveNotes() - x.findConsecutiveNotes(skipUnisons=True)
  will give you the number of P1s in the piece, because there could be
  d2's in there as well.

See test.TestStream.testFindConsecutiveNotes() for usage details.

This example is adapted from the tutorials/Examples page.

>>> s = converter.parse("tinynotation: 4/4 f8 d'8~ d'8 d'8~ d'4 b'-8 a'-8 a-8")
>>> m = s.measure(1)
>>> m.findConsecutiveNotes(skipUnisons=True, skipOctaves=True,
...                        skipRests=True, noNone=True)
[<music21.note.Note F>, <music21.note.Note D>,
 <music21.note.Note B->, <music21.note.Note A->]

>>> m.findConsecutiveNotes(skipUnisons=False,
...                        skipRests=True, noNone=True)
[<music21.note.Note F>, <music21.note.Note D>,
 <music21.note.Note D>, <music21.note.Note D>, <music21.note.Note D>,
 <music21.note.Note B->, <music21.note.Note A->]

* Changed in v7:

  * now finds notes in Voices without requiring `getOverlaps=True`
    and iterates over Parts rather than flattening.

  * If `noNone=False`, inserts `None`
    when backing up to scan a subsequent voice or part.

* Changed in v8: all parameters are keyword only.


OMIT_FROM_DOCS

N.B. for chords, currently, only the first pitch is tested for unison.
this is a bug TODO: FIX

(\*\*keywords is there so that other methods that pass along dicts to
findConsecutiveNotes don't have to remove
their own args; this method is used in melodicIntervals.)

this is omitted -- add docs above.
Frl   rC   Tr  Nrm  r   c              3  8   #    U  H  oR                   v   M     g 7frs   psrn   r-  s     rJ   rp   .Stream.findConsecutiveNotes.<locals>.<genexpr>(  s     $=9aTT9   c              3  8   #    U  H  oR                   v   M     g 7frs   r  r  s     rJ   rp   r  (  s     A\P[1$$P[r  )ra   rb   r   rY   rM   r   r%   r  rU   r  r   r  r   r&   
pitchClassr  r   r   r  r\   r   r  r  r$  )r   r  r  r  r  r  r  r  r   
returnList	lastStartlastEndlastContainerEndlastWasNonelastPitchesr"  ro   s                    rJ   r  r  0(  s   R ==E!dmmIIK.0
!	%(/1$K$DII  #33!44T5E5EFF%!!$'" IG
 ++D,<,<==#,#8#8  11$2B2BC5($-HHw.!"))$/&*a++'50!+.!3gg00KN4M4MM*e3$%GGJJ+a.2C2C$C "e+70B %%a(xx')  !I$Y1I1I%IJG"'K"#))K  5;;//C		NQ4F!T)&%/&--d3*.K*,K 9?/
  +d2$'$4AII$F$=199$=A\P[A\$\*d2ahh'6I"))!,88g-$$%HH	"(QZZ5M5M)M"N&'ii&+ 7J %]  5($Q		22%."))$/&*&( ' $&:a+C+C$QXX

0H0H%HIGq D% JX $NNrI   c                   U R                   " S0 UD6n[        U5      S:  a  U R                  SS9$ U R                  SS9n[        X"SS 5       GH  u  pEUb  Uc  M  UR                  (       a  UR                  (       d  M2  [
        R                  UR                  ;   a  UR                  S   nOUn[
        R                  UR                  ;   a  UR                  S   nOUn[        R                  " Xg5      n[        UR                  UR                  -   5      Ul        [        R                  " [        UR                  UR                  -
  5      5      Ul        UR!                  U5        GM     U$ )aE  
Returns a Stream of :class:`~music21.interval.Interval` objects
between Notes (and by default, Chords) that follow each other in a stream.
the offset of the Interval is the offset of the beginning of the interval
(if two notes are adjacent, then this offset is equal to the offset of
the second note, but if skipRests is set to True or there is a gap
in the Stream, then these two numbers
will be different).

See :meth:`~music21.stream.Stream.findConsecutiveNotes` in this class for
a discussion of what is meant by default for "consecutive notes", and
which keywords such as skipChords, skipRests, skipUnisons, etc. can be
used to change that behavior.

The interval between a Note and a Chord (or between two chords) is the
interval to the first pitch of the Chord (pitches[0]) which is usually the lowest.
For more complex interval calculations,
run :meth:`~music21.stream.Stream.findConsecutiveNotes` and then calculate
your own intervals directly.

Returns an empty Stream if there are not at least two elements found by
findConsecutiveNotes.

>>> s1 = converter.parse("tinynotation: 3/4 c4 d' r b b'", makeNotation=False)
>>> #_DOCS_SHOW s1.show()

.. image:: images/streamMelodicIntervals1.*
    :width: 246

>>> intervalStream1 = s1.melodicIntervals()
>>> intervalStream1.show('text')
{1.0} <music21.interval.Interval M9>
{4.0} <music21.interval.Interval P8>

>>> M9 = intervalStream1[0]
>>> M9.noteStart.nameWithOctave, M9.noteEnd.nameWithOctave
('C4', 'D5')

Using the skip attributes from :meth:`~music21.stream.Stream.findConsecutiveNotes`,
we can alter which intervals are reported:

>>> intervalStream2 = s1.melodicIntervals(skipRests=True, skipOctaves=True)
>>> intervalStream2.show('text')
{1.0} <music21.interval.Interval M9>
{2.0} <music21.interval.Interval m-3>

>>> m3 = intervalStream2[1]
>>> m3.directedNiceName
'Descending Minor Third'
r  melodicIntervalsr  rm  Nr   rC   )r  r   r  r  r\   r   r  ru   r[   r   r"  r   rM   r  r   r  rV   )	r   skipKeywordsr  r  thisNotenextNote	noteStartnoteEndreturnIntervals	            rJ   r  Stream.melodicIntervals(  s:   f ..>>
z?Q??4F?GG8JK"%jQR."AH8#3$$)9)9{{h///$NN1-	$	{{h///"..+"%..yBN$*8??X=S=S+S$TN!&.&7&7."7"7799 ':N#/) #B, rI   c                    / nU HN  nUR                   R                  nUR                  [        UR                  U-   5      4nUR	                  U5        MP     U$ )a!  
Given a flat stream, create a list of the start and end
times (as a tuple pair) of all elements in the Stream.

>>> a = stream.Stream()
>>> a.repeatInsert(note.Note(type='half'), [0, 1, 2, 3, 4])
>>> a._getDurSpan(a.flatten())
[(0.0, 2.0), (1.0, 3.0), (2.0, 4.0), (3.0, 5.0), (4.0, 6.0)]
)r   r  rM   r   rU   )r   r  r  ro   r  durSpans         rJ   _getDurSpanStream._getDurSpan%)  sP     A****Cxx3!78GKK   rI   c                    X/nUR                  5         SnU(       a  US   S   US   S   ::  a  SnU$ US   S   US   S   :  a  SnU$ )a  
Compare two durSpans and find overlaps; optionally,
include coincident boundaries. a and b are sorted to permit any ordering.

If an element ends at 3.0 and another starts at 3.0, this may or may not
be considered an overlap. The includeCoincidentEnds parameter determines
this behaviour, where ending and starting 3.0 being a type of overlap
is set by the includeEndBoundary being True.


>>> sc = stream.Stream()
>>> sc._durSpanOverlap((0, 5), (4, 12), False)
True
>>> sc._durSpanOverlap((0, 10), (11, 12), False)
False
>>> sc._durSpanOverlap((11, 12), (0, 10), False)
False
>>> sc._durSpanOverlap((0, 3), (3, 6), False)
False
>>> sc._durSpanOverlap((0, 3), (3, 6), True)
True
Frm  r   T)r   )r   r  r  rz  durSpansr  s         rJ   _durSpanOverlapStream._durSpanOverlap8)  sd    . 6{1~!Q/  {1~A.rI   c                   U R                  5       nUR                  SL a  UR                  5       nU R                  U5      n[        R
                  " [        [        [        [        4      [        [        U5      5      5      nUR                  5         [        [        U5      5       Vs/ s H  n/ PM     nn[        U5       Hz  u  pg[        US-   [        U5      5       HY  nX8   n	U R                  US   U	S   5      (       a4  XWS      R                  U	S   5        XYS      R                  US   5        MX    Mx     M|     U H  n
U
R                  5         M     U$ s  snf )a  
Find any elements in an elementsSorted list that have
durations that cause overlaps.

Returns a list of lists that has elements with overlaps,
all index values that match are included in that list.

See testOverlaps, in unit tests, for examples.

Used in getOverlaps inside makeVoices.
Frm  r   )rQ  ra   r  r  r   r   r   r   r   r   r  r   r  r   r  rU   )r   r  durSpanSorteddurSpanSortedIndexr  
overlapMapr  srcrj  dstlss              rJ   _findLayeringStream._findLayering])  s@    \\^
%'#**,J((4 :;sH}%&=)*:
 	! 49]9K3L&M3Lar3L
&M 23FA1q5#&8"9:(+''AA771v&--c!f51v&--c!f5 ; 4 BGGI  'Ns   (Ec                d  ^
^ U R                  5       nUR                  SL a  UR                  5       n[        U5      [        U5      :w  a  [	        S5      e0 n[        X5       H  u  nmU(       d  M  TR                  nSnU H^  nX'   m
SnU H%  n	[        U
4S jX9    5       5      (       d  M!  SnU	n  O   Uc  UnU(       d  MB  Xc;  a  / X6'   X6   R                  T
5        M`     U H"  n	[        U4S jX9    5       5      (       d  M!    M     Uc  UnXc;  a  / X6'   X6   R                  T5        M     U$ )z
Given a stream of flat elements and a map of equal length with lists of
index values that meet a given condition (overlap or simultaneities),
organize into a dictionary by the relevant or first offset
Fz1layeringMap must be the same length as flatStreamNTc              3  4   >#    U  H  oTL d  M	  S v   M     g7fr   rC   )rn   ro   
elementObjs     rJ   rp   .Stream._consolidateLayering.<locals>.<genexpr>)  s     DAO44r   c              3  4   >#    U  H  oTL d  M	  S v   M     g7fr   rC   )rn   ro   srcElementObjs     rJ   rp   r  )  s     CW]0BttWr   )	rQ  ra   r  r   ro  r  rM   r   rU   )r   layeringMapr  r  indices	srcOffset	dstOffsetrj  storer   r  r  s             @@rJ   _consolidateLayeringStream._consolidateLayering)  sA    \\^
%'#**,J{s:.!"UVV&)+&B"G]%,,II']
 A DDDD %$%	  $ )I5 ,*,O**:6' , CTWCCC  $ )I(&(DO&&}5O 'CP rI   c                \   SU R                   ;   a2  U R                   S   b"  U R                   S   SL a  gU R                   S   $ U R                  SS9nSnU  H  nU R                  USS9nU[        R                  :X  a    OxXB:  aB  [
        R                  " 5       n[        XB-
  5      nXeR                  l	        UR                  X%SS9  UR                  R                  n[        [        X$U-   5      5      nM     UR                  5         U(       d  SU R                   S'   gXR                   S'   U$ )	a,  
Returns either (1) a Stream containing empty Music21Objects
whose offsets and durations
are the length of gaps in the Stream
or (2) None if there are no gaps.

N.B. there may be gaps in the flattened representation of the stream
but not in the unflattened.  Hence why "isSequence" calls self.flatten().isGapless

>>> s = stream.Stream()
>>> s.insert(1.0, note.Note('E', type='half'))
>>> s.insert(5.0, note.Note('F', type='whole'))
>>> s.storeAtEnd(bar.Barline('final'))
>>> gapStream = s.findGaps()
>>> gapStream.show('text', addEndTimes=True)
{0.0 - 1.0} <music21.note.Rest quarter>
{3.0 - 5.0} <music21.note.Rest half>

Returns None if not gaps:

>>> s2 = stream.Stream()
>>> s2.append(note.Note('G'))
>>> s2.findGaps() is None
True

* Changed in v7: gapStream is filled with rests instead of Music21Objects
findGapsNFr  rl   Tr  r  )r   r  r  r   r!  r%   r  r   r   r  rV   r  r   )r   	gapStreamhighestCurrentEndTimero   eOffset
gapElementgapQuarterLengtheDurs           rJ   r  Stream.findGaps)  s   : $Z)@)L{{:&%/ ;;z**OOZO@	 #A(($(?G-....!YY[
#)'*I#J 4D##1  !6t T::++D$*3/DPTn+U$V!  	&+DKK
#&/KK
#rI   c                    SU R                   ;   a  U R                   S   b  U R                   S   $ U R                  5       c  SU R                   S'   gSU R                   S'   g)a@  
Returns True if there are no gaps between the lowest offset and the highest time.
Otherwise returns False

>>> s = stream.Stream()
>>> s.append(note.Note('C'))
>>> s.append(note.Note('D'))
>>> s.isGapless
True
>>> s.insert(10.0, note.Note('E'))
>>> s.isGapless
False

OMIT_FROM_DOCS

Test cache:

>>> s.isGapless
False
	isGaplessTF)r   r  r   s    rJ   r  Stream.isGapless *  s[    , $++%$++k*B*N;;{++}}&+/K(+0K(rI   c                D    U R                  5       nU R                  U5      $ )aM  
Find any elements that overlap. Overlapping might include elements
that have zero-length duration simultaneous.

This method returns a dictionary, where keys
are the start time of the first overlap and
value are a list of all objects included in
that overlap group.

This example demonstrates that end-joining overlaps do not count.

>>> a = stream.Stream()
>>> for x in range(4):
...     n = note.Note('G#')
...     n.duration = duration.Duration('quarter')
...     n.offset = x * 1
...     a.insert(n)
...
>>> d = a.getOverlaps()
>>> len(d)
0

Notes starting at the same time overlap:

>>> a = stream.Stream()
>>> for x in [0, 0, 0, 0, 13, 13, 13]:
...     n = note.Note('G#')
...     n.duration = duration.Duration('half')
...     n.offset = x
...     a.insert(n)
...
>>> d = a.getOverlaps()
>>> len(d[0])
4
>>> len(d[13])
3
>>> a = stream.Stream()
>>> for x in [0, 0, 0, 0, 3, 3, 3]:
...     n = note.Note('G#')
...     n.duration = duration.Duration('whole')
...     n.offset = x
...     a.insert(n)
...

Default is to not include coincident boundaries

>>> d = a.getOverlaps()
>>> len(d[0])
7

)r  r  )r   r  s     rJ   r  Stream.getOverlaps *  s&    h '')
 ((44rI   c                T    U R                  5       nSnU H  nU(       d  M  Sn  U$    U$ )a  
A stream is a sequence if it has no overlaps.

>>> a = stream.Stream()
>>> for x in [0, 0, 0, 0, 3, 3, 3]:
...     n = note.Note('G#')
...     n.duration = duration.Duration('whole')
...     n.offset = x * 1
...     a.insert(n)
...
>>> a.isSequence()
False

OMIT_FROM_DOCS

TODO: check that co-incident boundaries are properly handled

>>> a = stream.Stream()
>>> for x in [0, 4, 8.0]:
...     n = note.Note('G#')
...     n.duration = duration.Duration('whole')
...     a.append(n)
...
>>> a.isSequence()
True
TF)r  )r   r  r  	indexLists       rJ   
isSequenceStream.isSequenceY*  s:    6 '')
#Iy	 $ rI   c                b   [         R                  " U 5      n[         R                  " U5      n0 nU H  nU R                  US   5      nSXF'   M     U H*  nUS   R                  U5      nX;   d  M  XH==   S-  ss'   M,     / n	[	        U5       H  n
XJ   S:  d  M  U	R                  U
5        M      U	$ )a  
returns an ordered list of offsets where elements are started (attacked)
at the same time in both self and stream2.

In this example, we create one stream of Qtr, Half, Qtr, and one of Half, Qtr, Qtr.
There are simultaneous attacks at offset 0.0 (the beginning) and at offset 3.0,
but not at 1.0 or 2.0:


>>> st1 = stream.Stream()
>>> st2 = stream.Stream()
>>> st1.append([note.Note(type='quarter'),
...             note.Note(type='half'),
...             note.Note(type='quarter')])
>>> st2.append([note.Note(type='half'),
...             note.Note(type='quarter'),
...             note.Note(type='quarter')])
>>> print(st1.simultaneousAttacks(st2))
[0.0, 3.0]
r   rm  r  )r/   OffsetIteratorr  r  r  rU   )r   stream2stream1Offsetsstream2Offsets	returnKeythisList
thisOffsetthatList
thatOffsetr  r  s              rJ   simultaneousAttacksStream.simultaneousAttacks*  s    * "006!009	&H++HQK8J$%I! ' 'H!!44W=J&%*% '
 
!),K%*!!+. - rI   c                   U R                  [        R                  5       H  nSUR                  l        UR                  U R                  U5      SSS9nUR                  [        R                  5       H9  nSn [        R                  " X$5      nSUl	        XRR                  l        Uc  M8    M     M     g! [        R                   a     N%f = f)ax  
For each element in self, creates an interval.Interval object in the element's
editorial that is the interval between it and the element in cmpStream that
is sounding at the moment the element in srcStream is attacked.

Remember that if you are comparing two streams with measures, etc.,
you'll need to flatten each stream as follows:

>>> #_DOCS_SHOW stream1.flatten().attachIntervalsBetweenStreams(stream2.flatten())

Example usage:

>>> s1 = converter.parse('tinynotation: 7/4 C4 d8 e f# g A2 d2', makeNotation=False)
>>> s2 = converter.parse('tinynotation: 7/4 g4 e8 d c4   a2 r2', makeNotation=False)
>>> s1.attachIntervalsBetweenStreams(s2)
>>> for n in s1.notes:
...     if n.editorial.harmonicInterval is None:
...         print('None')  # Wil print if other voice had a rest.
...     else:
...         print(n.editorial.harmonicInterval.directedName)
P12
M2
M-2
A-4
P-5
P8
None
NF)r|  r{  harmonic)r   r%   r  rP  harmonicIntervalr   r  r   r"  intervalTyper   Music21Exception)r   	cmpStreamr  	simultEls
simultNote	interval1s         rJ   attachIntervalsBetweenStreams$Stream.attachIntervalsBetweenStreams*  s    @ ((3A+/AKK(!55d6H6H6KFKGL 6 NI (::499E
 	 ( 1 1! @I-7I*3<KK0 ( F 4 $44 s   9-B55CCc                X   U R                   R                  5       nUS   nSnUb  Ubk  [        U[        R                  5      (       aL  [        U[        R                  5      (       a-  [
        R                  " X25      nSUl        XBR                  l	        UnUR                  5       nUb  M  gg)a  
For each element in self, creates an interval.Interval object in the element's
editorial that is the interval between it and the previous element in the stream. Thus,
the first element will have a value of None.

DEPRECATED sometime soon.  A replacement to come presently.

>>> s1 = converter.parse('tinyNotation: 7/4 C4 d8 e f# g A2 d2', makeNotation=False)
>>> s1.attachMelodicIntervals()
>>> for n in s1.notes:
...     if n.editorial.melodicInterval is None:
...         print('None')
...     else:
...         print(n.editorial.melodicInterval.directedName)
None
M9
M2
M2
m2
m-7
P4

>>> s = stream.Stream()
>>> s.append(note.Note('C'))
>>> s.append(note.Note('D'))
>>> s.append(note.Rest(quarterLength=4.0))
>>> s.append(note.Note('D'))
>>> s.attachMelodicIntervals()
>>> for n in s.notes:
...     if n.editorial.melodicInterval is None:
...         print('None')  # Will print if other voice had a rest.
...     else:
...         print(n.editorial.melodicInterval.directedName)
None
M2
P1
r   Nmelodic)r[   r6   r   r%   r  r   r"  r  rP  melodicIntervalr3  )r   r[   currentObjectpreviousObjectr  s        rJ   attachMelodicIntervalsStream.attachMelodicIntervals*  s    N 

!!#a'*"=$))<<">499=="*"3"3N"R/8,:I''7*N)..0M 'rI   c                    Ub  UR                  U5      nOUR                  nU R                  USS9nU(       d  g[        U5      S:X  a  US   $ U H!  n[	        XQR
                  5      (       d  M  Us  $    US   $ )a  
Given an element (from another Stream) returns the single element
in this Stream that is sounding while the given element starts.

If there are multiple elements sounding at the moment it is
attacked, the method returns the first element of the same class
as this element, if any. If no element
is of the same class, then the first element encountered is
returned. For more complex usages, use allPlayingWhileSounding.

Returns None if no elements fit the bill.

The optional elStream is the stream in which el is found.
If provided, el's offset
in that Stream is used.  Otherwise, the current offset in
el is used.  It is just
in case you are paranoid that el.offset might not be what
you want, because of some fancy manipulation of
el.activeSite

>>> n1 = note.Note('G#')
>>> n2 = note.Note('D#')
>>> s1 = stream.Stream()
>>> s1.insert(20.0, n1)
>>> s1.insert(21.0, n2)

>>> n3 = note.Note('C#')
>>> s2 = stream.Stream()
>>> s2.insert(20.0, n3)
>>> s1.playingWhenAttacked(n3)
<music21.note.Note G#>

>>> n3.setOffsetBySite(s2, 20.5)
>>> s1.playingWhenAttacked(n3)
<music21.note.Note G#>

>>> n3.setOffsetBySite(s2, 21.0)
>>> n3.offset
21.0
>>> s1.playingWhenAttacked(n3)
<music21.note.Note D#>

If there is more than one item at the same time in the other stream
then the first item matching the same class is returned, even if
another element has a closer offset.

>>> n3.setOffsetBySite(s2, 20.5)
>>> s1.insert(20.5, clef.BassClef())
>>> s1.playingWhenAttacked(n3)
<music21.note.Note G#>
>>> fc = clef.FClef()  # superclass of BassClef
>>> s2.insert(20.5, fc)
>>> s1.playingWhenAttacked(fc)
<music21.clef.BassClef>

But since clefs have zero duration, moving the FClef ever so slightly
will find the note instead

>>> fc.setOffsetBySite(s2, 20.6)
>>> s1.playingWhenAttacked(fc)
<music21.note.Note G#>


Optionally, specify the site to get the offset from:

>>> n3.setOffsetBySite(s2, 21.0)
>>> n3.setOffsetBySite(None, 100)
>>> n3.activeSite = None
>>> s1.playingWhenAttacked(n3) is None
True
>>> s1.playingWhenAttacked(n3, s2).name
'D#'
NFr|  rm  r   )r  rM   r   r   r   r   )r   r   elStreamr  otherElementsthisEls         rJ   playingWhenAttackedStream.playingWhenAttacked+  s    T ))(3HyyH0050Q1$ ##'fll33!M ( !##rI   c                V   Ub  UR                  U5      nOUR                  nX1R                  -   nXC:w  a"  U R                  UUSSSS9R	                  5       nOU R                  USS9R	                  5       nX5l        UR                  Ul        U H  nUR                  U-
  Ul        M     U$ )a2  
Returns a new Stream of elements in this stream that sound
at the same time as `el`, an element presumably in another Stream.

The offset of this new Stream is set to el's offset, while the
offset of elements within the Stream are adjusted relative to
their position with respect to the start of el.  Thus, a note
that is sounding already when el begins would have a negative
offset.  The duration of otherStream is forced
to be the length of el -- thus a note sustained after el ends
may have a release time beyond that of the duration of the Stream.

As above, elStream is an optional Stream to look up el's offset in.  Use
this to work on an element in another part.

The method always returns a Stream, but it might be an empty Stream.

OMIT_FROM_DOCS
TODO: write: requireClass:
Takes as an optional parameter "requireClass".  If this parameter
is boolean True then only elements
of the same class as el are added to the new Stream.  If requireClass
is list, it is used like
classList in elsewhere in stream to provide a list of classes that the
el must be a part of.

F)r|  rz  rJ  r!  )r  rM   r  r   r6   )r   r   r"  r  elEndr#  r$  s          rJ   allPlayingWhileSoundingStream.allPlayingWhileSoundingt+  s    8 ))(3HyyH+++ 44 %#(.3 5 5
 6<VX  !44XEJ 5 LLRFH   (&(&6&6##F"MMH4FM $ rI   r'  c               ,   U(       d  U R                  S5      nOU nUR                  (       d  UR                  5         UR                  R	                  5       R                  5       n[        UR                  5        Vs/ s H  n[        U5      PM     snS/-   5      nUS:X  a
  U(       d  U$ g/ n[        U5       H  nUR                  [        5       5        M     UR                   HQ  n	U	R                  U5      n
U H&  nUR                  U
::  d  M  UR                  X5          O   UR                  U	5        MS     U H  nU(       d  M  UR                  SU5        M      U(       a  UR!                  SSSS9  UR#                  S5        U(       d  U$ gs  snf )a  
If this Stream has overlapping Notes or Chords, this method will isolate
all overlaps in unique Voices, and place those Voices in the Stream.

>>> s = stream.Stream()
>>> s.insert(0, note.Note('C4', quarterLength=4))
>>> s.repeatInsert(note.Note('b-4', quarterLength=0.5), [x * 0.5 for x in list(range(8))])
>>> s.makeVoices(inPlace=True)
>>> len(s.voices)
2
>>> [n.pitch for n in s.voices[0].notes]
[<music21.pitch.Pitch C4>]
>>> [str(n.pitch) for n in s.voices[1].notes]
['B-4', 'B-4', 'B-4', 'B-4', 'B-4', 'B-4', 'B-4', 'B-4']

* Changed in v7: if `fillGaps=True` and called on an incomplete measure,
  makes trailing rests in voices. This scenario occurs when parsing MIDI.
r,  rm  Nr   T)r  rF  r  r  )rL  ra   r   r[   r6   r  r  r  r   r  rU   r,  r  r  rV   r  r  r  )r   rF  r  rQ  olDictr  maxVoiceCountr7  dummyro   r  r  s               rJ   r,  Stream.makeVoices+  sj   , 11,?II!!NN'')557 V]]_E_ESZ_EKLA   =)EMM%'" *
 A!!),A ==A%HHQN 
 Q !  Aq  A&  (,9=   "
 	' Q Fs   <F	countByIdc                   Sn/ nU R                  5       (       a~  U R                  [        5       Hd  nUR                  n[	        U5      nU(       d  [        Xb5      nM.  U H0  nUR                  U;  d  M  UR                  UR                  5        M2     Mf     ORU R                  5       (       a;  U R                  nU(       d  [	        U5      nOU Vs/ s H  owR                  PM     nnOSn[        U[	        U5      5      nU(       d  U$ X#4$ s  snf )a  
Returns the maximum number of voices in a part.  Used by voicesToParts.
Minimum returned is 1.  If `countById` is True, returns a tuple of
(maxVoiceCount, voiceIdList)

>>> import copy

>>> p0 = stream.Part()
>>> p0._maxVoiceCount()
1
>>> v0 = stream.Voice(id='v0')
>>> p0.insert(0, v0)
>>> p0._maxVoiceCount()
1
>>> v1 = stream.Voice(id='v1')
>>> p0.insert(0, v1)
>>> p0._maxVoiceCount()
2

>>> p1 = stream.Part()
>>> m1 = stream.Measure(number=1)
>>> p1.insert(0, m1)
>>> p1._maxVoiceCount()
1
>>> m1.insert(0, v0)
>>> p1._maxVoiceCount()
1
>>> m1.insert(0, v1)
>>> p1._maxVoiceCount()
2
>>> m2 = stream.Measure(number=2)
>>> p1.append(m2)
>>> p1._maxVoiceCount()
2
>>> v01 = copy.copy(v0)
>>> v11 = stream.Voice(id='v11')
>>> m2.insert(0, v01)
>>> m2.insert(0, v11)
>>> p1._maxVoiceCount()
2
>>> v2 = stream.Voice(id='v2')
>>> m2.insert(0, v2)
>>> p1._maxVoiceCount()
3

If `countById` is True then different voice ids create different parts.

>>> mvc, vIds = p1._maxVoiceCount(countById=True)
>>> mvc
4
>>> vIds
['v0', 'v1', 'v11', 'v2']

>>> v01.id = 'v01'
>>> mvc, vIds = p1._maxVoiceCount(countById=True)
>>> mvc
5
>>> vIds
['v0', 'v1', 'v01', 'v11', 'v2']
rm  )	r,  r   rw   r7  r   r  r   rU   r  )	r   r1  
voiceCountvoiceIdsr  mVoicesmVCountr  r7  s	            rJ   _maxVoiceCountStream._maxVoiceCount+  s    z 
,,W5((g, !$W!9J$44x/$OOADD1 % 6 ^^[[F [
*01&QDD&1JS]3
'' 2s   DseparateByIdc                 ^ ^^^ [        5       nT R                  5       (       aH  T R                  [        5       H-  nUR	                  TS9nU H  nUR                  SU5        M     M/     U$ T R                  TS9nT(       d  Um/ nOUu  mn0 m[        T5       H  n[        5       nUR                  SU5        T(       d0  [        T R                  5      S-   [        U5      -   Ul
        UTU'   MV  Xx   n	[        T R                  5      S-   [        U	5      -   Ul
        UTU	'   M     UUU U4S jn
UR                   H  nUR                  T SS9  M     T R                  5       (       Ga  T R                  [        5       H  nUR                  5       (       a
  U
" U5        M"  [        5       nUR!                  U5        UR                  U5        UR                  S   R                  T R#                  U5      U5        [        S	T5       H\  n[        5       nUR!                  U5        UR                  US
S9  UR                  U   R                  T R#                  U5      U5        M^     M     OpT R                  5       (       a=  [%        T R&                  5       H#  u  pUR                  U   R                  U5        M%     OUR                  S   R                  T 5        UR                   HU  nUR                  5       (       d  M  [(        R*                  " USS9UR                  [        5      R-                  5       l        MW     U$ )a  
If this Stream defines one or more voices,
extract each into a Part, returning a Score.

If this Stream has no voices, return the Stream as a Part within a Score.

>>> c = corpus.parse('demos/two-voices')
>>> c.show('t')
{0.0} <music21.text.TextBox 'Music21 Fr...'>
{0.0} <music21.text.TextBox 'Music21'>
{0.0} <music21.metadata.Metadata object at 0x109ce1630>
{0.0} <music21.stream.Part Piano>
    {0.0} <music21.instrument.Instrument 'P1: Piano: '>
    {0.0} <music21.stream.Measure 1 offset=0.0>
        {0.0} <music21.layout.PageLayout>
        {0.0} <music21.layout.SystemLayout>
        ...
        {0.0} <music21.clef.BassClef>
        {0.0} <music21.key.Key of D major>
        {0.0} <music21.meter.TimeSignature 4/4>
        {0.0} <music21.stream.Voice 3>
            {0.0} <music21.note.Note E>
            ...
            {3.0} <music21.note.Rest quarter>
        {0.0} <music21.stream.Voice 4>
            {0.0} <music21.note.Note F#>
            ...
            {3.5} <music21.note.Note B>
    {4.0} <music21.stream.Measure 2 offset=4.0>
        {0.0} <music21.stream.Voice 3>
            {0.0} <music21.note.Note E>
            ...
            {3.0} <music21.note.Rest quarter>
        {0.0} <music21.stream.Voice 4>
            {0.0} <music21.note.Note E>
            ...
            {3.5} <music21.note.Note A>
    {8.0} <music21.stream.Measure 3 offset=8.0>
        {0.0} <music21.note.Rest whole>
        {4.0} <music21.bar.Barline type=final>
{0.0} <music21.layout.ScoreLayout>

>>> ce = c.voicesToParts()
>>> ce.show('t')
{0.0} <music21.stream.Part Piano-v0>
    {0.0} <music21.instrument.Instrument 'P1: Piano: '>
    {0.0} <music21.stream.Measure 1 offset=0.0>
        {0.0} <music21.clef.TrebleClef>
        {0.0} <music21.key.Key of D major>
        {0.0} <music21.meter.TimeSignature 4/4>
        {0.0} <music21.note.Note E>
        ...
        {3.0} <music21.note.Rest quarter>
    {4.0} <music21.stream.Measure 2 offset=4.0>
        {0.0} <music21.note.Note E>
        ...
        {3.0} <music21.note.Rest quarter>
    {8.0} <music21.stream.Measure 3 offset=8.0>
        {0.0} <music21.note.Rest whole>
        {4.0} <music21.bar.Barline type=final>
{0.0} <music21.stream.Part Piano-v1>
    {0.0} <music21.instrument.Instrument 'P1: Piano: '>
    {0.0} <music21.stream.Measure 1 offset=0.0>
        {0.0} <music21.clef.BassClef>
        {0.0} <music21.key.Key of D major>
        ...
        {3.5} <music21.note.Note B>
    {4.0} <music21.stream.Measure 2 offset=4.0>
        {0.0} <music21.note.Note E>
        ...
        {3.5} <music21.note.Note A>
    {8.0} <music21.stream.Measure 3 offset=8.0>
        {0.0} <music21.bar.Barline type=final>
<BLANKLINE>

If `separateById` is True then all voices with the same id
will be connected to the same Part, regardless of order
they appear in the measure.

Compare the previous output:

>>> p0pitches = ce.parts[0].pitches
>>> p1pitches = ce.parts[1].pitches
>>> ' '.join([p.nameWithOctave for p in p0pitches])
'E4 D#4 D#4 E4 F#4 E4 B3 B3 E4 E4'
>>> ' '.join([p.nameWithOctave for p in p1pitches])
'F#2 F#3 E3 E2 D#2 D#3 B2 B3 E2 E3 D3 D2 C#2 C#3 A2 A3'

Swap voice ids in first measure:

>>> m0 = c.parts[0].getElementsByClass(stream.Measure).first()
>>> m0.voices[0].id, m0.voices[1].id
('3', '4')
>>> m0.voices[0].id = '4'
>>> m0.voices[1].id = '3'

Now run voicesToParts with `separateById=True`

>>> ce = c.voicesToParts(separateById=True)
>>> p0pitches = ce.parts[0].pitches
>>> p1pitches = ce.parts[1].pitches
>>> ' '.join([p.nameWithOctave for p in p0pitches])
'E4 D#4 D#4 E4 F#4 E2 E3 D3 D2 C#2 C#3 A2 A3'
>>> ' '.join([p.nameWithOctave for p in p1pitches])
'F#2 F#3 E3 E2 D#2 D#3 B2 B3 E4 B3 B3 E4 E4'

Note that the second and subsequent measure's pitches were changed
not the first, because separateById aligns the voices according to
order first encountered, not by sorting the Ids.
r9  r   r0  z-v-c                  > [        5       nUR                  U 5        UR                  U SS9  Sn[        5       n[	        U R
                  5       H|  u  p$[        R                  " U5      nUR                  U5        UR                  nT(       d  TU   nOUR                  U5        TU   nUR                  TR                  U 5      U5        M~     T(       dQ  [        US-   T
5       H=  nTU   nUR                  TR                  U 5      [        R                  " U5      5        M?     gT HD  n	X;   a  M
  TU	   nUR                  TR                  U 5      [        R                  " U5      5        MF     g)zY
This is the main routine for dealing with the most common
and most difficult voice set.
r  r*  r  r4  rh  r   rm  N)rw   rP  rj  re  r  r7  r  r  r   r  rV   r  r  )mInnermActivevIndexInnerseenIdsThisMeasurevInner	mNewInnervIdrK  
emptyIndexvoiceIdInner	partCountpartDictr   r:  s             rJ   doOneMeasureWithVoices4Stream.voicesToParts.<locals>.doOneMeasureWithVoices,  sI   
 iG##F+ !!! "  K!$'0'?# !MM'2	''/ii#%k2F&**3/%c]Fd008)D (@"  "'a"CJ%j1FMM$"4"4V"<dmmG>TU #D %-L#9 %l3FMM$"4"4V"<dmmG>TU	 %-rI   r{  r?  rm  )r  r*  r4  Tr$  )rx   r*  r   rQ   voicesToPartsrV   r7  r  rl  r   r   partsrj  r,  rw   r  rP  r  r  r7  r   r  r   )r   r:  r  r-  sSubpSub	mvcReturnr4  r  voiceIdrK  r  mNewmEmptyvIndexr  rI  rJ  s   ``              @@rJ   rM  Stream.voicesToPartsR,  s   ` G
 ""$$,,V4LA DHHQ% ! 5 H '','?	!IH"+Ix
 y!AAHHQN477|d*SV3"+477|c)CL8$%! "/	V /	Vd AOOD/OB  ,,W5;;==*1- #9D((+&&q)GGAJ%%d&8&8&;TB"1i0!(..q1,,Q AM, N
))$*<*<Q*?H 1 6( ^^&t{{3	--a0 4 GGAJ$$T* A }}=A]]1VZ=[$$W-335:  rI   c                "    U R                  5       $ )a  
Create a multiple Part representation from a single polyphonic Part.

Currently just runs :meth:`~music21.stream.Stream.voicesToParts`
but that will change as part explosion develops, and this
method will use our best available quick method for part
extraction.
)rM  r   s    rJ   explodeStream.explodeB-  s     !!##rI   )r~  rF  c               @   U R                   (       d  gU(       d  [        R                  " U 5      nOU n/ n/ nUR                    H.  nU(       d  UR                  U5        M  UR                  U5        M0     U H  nUR	                  U5        M     [        U5      S:X  d  U(       aq  U H[  nUR                  U5      nUR                   H&  nUR                  XxR                  U5      -   U5        M(     UR	                  U5        M]     UR                  5         U(       d  U$ g)al  
If this Stream defines one or more internal voices, do the following:

* If there is more than one voice, and a voice has no elements,
  remove that voice.
* If there is only one voice left that has elements, place those
  elements in the parent Stream.
* If `force` is True, even if there is more than one Voice left,
  all voices will be flattened.

This leaves a stream where all voices appear when another appears in
the same measure.

More demonstrations of `recurse=True`:

>>> s = stream.Stream(note.Note())
>>> s.insert(0, note.Note())
>>> s.insert(0, note.Note())
>>> s.makeVoices(inPlace=True)
>>> len(s.voices)
3

>>> s.remove(s.voices[1].notes[0], recurse=True)
>>> s.remove(s.voices[2].notes[0], recurse=True)
>>> voicesFlattened = s.flattenUnnecessaryVoices()
>>> len(voicesFlattened.voices)
0

* Changed in v5: inPlace is default False and a keyword only arg.
Nrm  )
r7  r  r  rU   r  r   r  r   r   r   )	r   r~  rF  rQ  r  rQ  r  shiftOffsetro   s	            rJ   flattenUnnecessaryVoicesStream.flattenUnnecessaryVoicesM-  s    > {{d+II !!Aa q!	 " AQ  w<1//	:A((7H7H7K)KQO $   #  ))+rI   c                   0 nSn      SS jnU  GHE  nUSL ax  [        U[        5      (       ac  UnUR                   HO  n	USL a9  U	R                  b  U	R                  R                  S:X  a  U" XU5        US-  nM?  MA  U" XU5        US-  nMQ     M  USL aO  [        U[
        5      (       a:  UR                  USUS9n
U
 H!  nX;  a  / XK'   XK   R                  X   5        M#     M  [        U[        R                  5      (       aQ  USL a;  UR                  b  UR                  R                  S:X  a  U" XtU5        US-  nGM1  GM4  U" XtU5        US-  nGME  GMH     U$ )a	  
Returns a dict of lists of lyric objects (with the keys being
the lyric numbers) found in self. Each list will have an element for each
note in the stream (which may be a note.Lyric() or None).
By default, this method automatically
recurses through measures, but not other container streams.


>>> s = converter.parse('tinynotation: 4/4 a4 b c d   e f g a', makeNotation=False)
>>> someLyrics = ['this', 'is', 'a', 'list', 'of', 'eight', 'lyric', 'words']
>>> for n, lyric in zip(s.notes, someLyrics):
...     n.lyric = lyric


>>> s.lyrics()
{1: [<music21.note.Lyric number=1 syllabic=single text='this'>, ...,
     <music21.note.Lyric number=1 syllabic=single text='words'>]}

>>> s.notes[3].lyric = None
>>> s.lyrics()[1]
[<music21.note.Lyric number=1 syllabic=single text='this'>, ..., None, ...,
 <music21.note.Lyric number=1 syllabic=single text='words'>]

If ignoreBarlines is True, it will behave as if the elements in measures are all
in a flattened stream (note that this is not stream.flatten()
as it does not copy the elements)
together without measure containers. This means that even if recurse is
False, lyrics() will still essentially recurse through measures.

>>> s.makeMeasures(inPlace=True)
>>> s.lyrics()[1]
[<music21.note.Lyric number=1 syllabic=single text='this'>, ..., None, ...,
 <music21.note.Lyric number=1 syllabic=single text='words'>]

>>> list(s.lyrics(ignoreBarlines=False).keys())
[]

If recurse is True, this method will recurse through all container streams and
build a nested list structure mirroring the hierarchy of the stream.
Note that if ignoreBarlines is True, measure structure will not be reflected
in the hierarchy, although if ignoreBarlines is False, it will.

Note that streams which do not contain any instance of a lyric number will not
appear anywhere in the final list (not as a [] or otherwise).

>>> scr = stream.Score(s)

>>> scr.lyrics(ignoreBarlines=False, recurse=True)[1]
[[[<music21.note.Lyric number=1 syllabic=single text='this'>, <...'is'>, <...'a'>, None],
  [<...'of'>, <...'eight'>, <...'lyric'>, <...'words'>]]]

Notice that the measures are nested in the part which is nested in the score.

>>> scr.lyrics(ignoreBarlines=True, recurse=True)[1]
[[<music21.note.Lyric number=1 syllabic=single text='this'>, <...'is'>, <...'a'>, None,
  <...'of'>, <...'eight'>, <...'lyric'>, <...'words'>]]

Notice that this time, the measure structure is ignored.

>>> list(scr.lyrics(ignoreBarlines=True, recurse=False).keys())
[]

r   c                   U R                   (       d  U H  nX   R                  S 5        M     g / nU R                    Hs  nUR                  U;  a(  [        U5       Vs/ s H  nS PM     snXR                  '   XR                     R                  U5        UR                  UR                  5        Mu     U H  nX4;  d  M
  X   R                  S 5        M     g s  snf rs   )r  rU   r  r  )r  inner_returnListsnumNonesToAppendr   addLyricNumsr  r.  s          rJ   appendLyricsFromNote+Stream.lyrics.<locals>.appendLyricsFromNote-  s    
 88*A%(//5 +Lhh99$55FKL\F]3^F]UDF]3^%ii0!)),33B7##BII.	 
 '(%(//5 ' 4_s   CTr   rm  )ignoreBarlinesrY   skipTies)r  rX  r`  #dict[int, list[RecursiveLyricList]]ra  r   )
r   rw   r[   r'   r   rQ   r  rU   r%   r  )r   re  rY   rf  returnListsnumNotesrc  ro   r  r  sublistsr   s               rJ   r  Stream.lyrics-  sh   L <>
	6	6B	6 "	6, A%*Q*@*@A4'55=AEEJJ',A0J$MH ,QXF A ! DZ6%:%:88>4Zb8c!A+)+N))(+6 " At||,,t#uu}

g(=,QXF A(BMH C F rI   )matchBySpanrF  c                  SSK Jn  U(       d  U R                  S5      nOU n/ n/ nUR                  UR                  5       Hn  nUb  XR
                  ;  a  M  UR                  n	U	S:X  a  UR                  U5        M<  U	S:X  a  UR                  U5        MU  U	S:X  d  M]  UR                  X5        Mp     / n
/ n/ nU HJ  nUR                  X5      u  pnUR                  U5        U
R                  U5        UR                  U5        ML     UR                  USSS	9  / nU HR  nUR                  UR                  -
  nUR                  U5      UR                  -   nUR                  US
U-  U/45        MT     UR                  USSS	9  U H8  nUR                  X5      u  pUR                  U5        U
R                  U5        M:     UR!                  X5        UR#                  5         U(       d  U$ g)a  
For any :class:`~music21.variant.Variant` objects defined in this Stream
(or selected by matching the `group` parameter),
replace elements defined in the Variant with those in the calling Stream.
Elements replaced will be gathered into a new Variant
given the group 'default'. If a variant is activated with
.replacementDuration different from its length, the appropriate elements
in the stream will have their offsets shifted, and measure numbering
will be fixed. If matchBySpan is True, variants with lengthType
'replacement' will replace all the elements in the
replacement region of comparable class. If matchBySpan is False,
elements will be swapped in when a match is found between an element
in the variant and an element in the replacement region of the string.

>>> sStr   = 'd4 e4 f4 g4   a2 b-4 a4    g4 a8 g8 f4 e4    d2 a2              '
>>> v1Str  = '              a2. b-8 a8 '
>>> v2Str1 = '                                             d4 f4 a2 '
>>> v2Str2 = '                                                      d4 f4 AA2 '

>>> sStr += "d4 e4 f4 g4    a2 b-4 a4    g4 a8 b-8 c'4 c4    f1"

>>> s = converter.parse('tinynotation: 4/4 ' + sStr, makeNotation=False)
>>> s.makeMeasures(inPlace=True)  # maybe not necessary?
>>> v1stream = converter.parse('tinynotation: 4/4 ' + v1Str, makeNotation=False)
>>> v2stream1 = converter.parse('tinynotation: 4/4 ' + v2Str1, makeNotation=False)
>>> v2stream2 = converter.parse('tinynotation: 4/4 ' + v2Str2, makeNotation=False)


>>> v1 = variant.Variant()
>>> v1measure = stream.Measure()
>>> v1.insert(0.0, v1measure)
>>> for e in v1stream.notesAndRests:
...    v1measure.insert(e.offset, e)

>>> v2 = variant.Variant()
>>> v2.replacementDuration = 4.0
>>> v2measure1 = stream.Measure()
>>> v2measure2 = stream.Measure()
>>> v2.insert(0.0, v2measure1)
>>> v2.insert(4.0, v2measure2)
>>> for e in v2stream1.notesAndRests:
...    v2measure1.insert(e.offset, e)
>>> for e in v2stream2.notesAndRests:
...    v2measure2.insert(e.offset, e)

>>> v3 = variant.Variant()
>>> v3.replacementDuration = 4.0
>>> v1.groups = ['docVariants']
>>> v2.groups = ['docVariants']
>>> v3.groups = ['docVariants']

>>> s.insert(4.0, v1)   # replacement variant
>>> s.insert(12.0, v2)  # insertion variant (2 bars replace 1 bar)
>>> s.insert(20.0, v3)  # deletion variant (0 bars replace 1 bar)
>>> s.show('text')
{0.0} <music21.stream.Measure 1 offset=0.0>
    {0.0} <music21.clef.TrebleClef>
    {0.0} <music21.meter.TimeSignature 4/4>
    {0.0} <music21.note.Note D>
    {1.0} <music21.note.Note E>
    {2.0} <music21.note.Note F>
    {3.0} <music21.note.Note G>
{4.0} <music21.variant.Variant object of length 4.0>
{4.0} <music21.stream.Measure 2 offset=4.0>
    {0.0} <music21.note.Note A>
    {2.0} <music21.note.Note B->
    {3.0} <music21.note.Note A>
{8.0} <music21.stream.Measure 3 offset=8.0>
    {0.0} <music21.note.Note G>
    {1.0} <music21.note.Note A>
    {1.5} <music21.note.Note G>
    {2.0} <music21.note.Note F>
    {3.0} <music21.note.Note E>
{12.0} <music21.variant.Variant object of length 8.0>
{12.0} <music21.stream.Measure 4 offset=12.0>
    {0.0} <music21.note.Note D>
    {2.0} <music21.note.Note A>
{16.0} <music21.stream.Measure 5 offset=16.0>
    {0.0} <music21.note.Note D>
    {1.0} <music21.note.Note E>
    {2.0} <music21.note.Note F>
    {3.0} <music21.note.Note G>
{20.0} <music21.variant.Variant object of length 0.0>
{20.0} <music21.stream.Measure 6 offset=20.0>
    {0.0} <music21.note.Note A>
    {2.0} <music21.note.Note B->
    {3.0} <music21.note.Note A>
{24.0} <music21.stream.Measure 7 offset=24.0>
    {0.0} <music21.note.Note G>
    {1.0} <music21.note.Note A>
    {1.5} <music21.note.Note B->
    {2.0} <music21.note.Note C>
    {3.0} <music21.note.Note C>
{28.0} <music21.stream.Measure 8 offset=28.0>
    {0.0} <music21.note.Note F>
    {4.0} <music21.bar.Barline type=final>

>>> docVariant = s.activateVariants('docVariants')

>>> #_DOCS_SHOW s.show()

.. image:: images/stream_activateVariants1.*
    :width: 600

>>> #_DOCS_SHOW docVariant.show()

.. image:: images/stream_activateVariants2.*
    :width: 600

>>> docVariant.show('text')
{0.0} <music21.stream.Measure 1 offset=0.0>
    {0.0} <music21.clef.TrebleClef>
    {0.0} <music21.meter.TimeSignature 4/4>
    {0.0} <music21.note.Note D>
    {1.0} <music21.note.Note E>
    {2.0} <music21.note.Note F>
    {3.0} <music21.note.Note G>
{4.0} <music21.variant.Variant object of length 4.0>
{4.0} <music21.stream.Measure 2 offset=4.0>
    {0.0} <music21.note.Note A>
    {3.0} <music21.note.Note B->
    {3.5} <music21.note.Note A>
{8.0} <music21.stream.Measure 3 offset=8.0>
    {0.0} <music21.note.Note G>
    {1.0} <music21.note.Note A>
    {1.5} <music21.note.Note G>
    {2.0} <music21.note.Note F>
    {3.0} <music21.note.Note E>
{12.0} <music21.variant.Variant object of length 4.0>
{12.0} <music21.stream.Measure 4 offset=12.0>
    {0.0} <music21.note.Note D>
    {1.0} <music21.note.Note F>
    {2.0} <music21.note.Note A>
{16.0} <music21.stream.Measure 5 offset=16.0>
    {0.0} <music21.note.Note D>
    {1.0} <music21.note.Note F>
    {2.0} <music21.note.Note A>
{20.0} <music21.stream.Measure 6 offset=20.0>
    {0.0} <music21.note.Note D>
    {1.0} <music21.note.Note E>
    {2.0} <music21.note.Note F>
    {3.0} <music21.note.Note G>
{24.0} <music21.variant.Variant object of length 4.0>
{24.0} <music21.stream.Measure 7 offset=24.0>
    {0.0} <music21.note.Note G>
    {1.0} <music21.note.Note A>
    {1.5} <music21.note.Note B->
    {2.0} <music21.note.Note C>
    {3.0} <music21.note.Note C>
{28.0} <music21.stream.Measure 8 offset=28.0>
    {0.0} <music21.note.Note F>
    {4.0} <music21.bar.Barline type=final>

After a variant group has been activated, the regions it replaced are
stored as variants with the group 'default'.
It should be noted that this means .activateVariants should rarely if
ever be used on a stream which is returned
by activateVariants because the group information is lost.

>>> defaultVariant = docVariant.activateVariants('default')
>>> #_DOCS_SHOW defaultVariant.show()

.. image:: images/stream_activateVariants3.*
    :width: 600

>>> defaultVariant.show('text')
{0.0} <music21.stream.Measure 1 offset=0.0>
    {0.0} <music21.clef.TrebleClef>
    {0.0} <music21.meter.TimeSignature 4/4>
    {0.0} <music21.note.Note D>
    {1.0} <music21.note.Note E>
    {2.0} <music21.note.Note F>
    {3.0} <music21.note.Note G>
{4.0} <music21.variant.Variant object of length 4.0>
{4.0} <music21.stream.Measure 2 offset=4.0>
    {0.0} <music21.note.Note A>
    {2.0} <music21.note.Note B->
    {3.0} <music21.note.Note A>
{8.0} <music21.stream.Measure 3 offset=8.0>
    {0.0} <music21.note.Note G>
    {1.0} <music21.note.Note A>
    {1.5} <music21.note.Note G>
    {2.0} <music21.note.Note F>
    {3.0} <music21.note.Note E>
{12.0} <music21.variant.Variant object of length 8.0>
{12.0} <music21.stream.Measure 4 offset=12.0>
    {0.0} <music21.note.Note D>
    {2.0} <music21.note.Note A>
{16.0} <music21.stream.Measure 5 offset=16.0>
    {0.0} <music21.note.Note D>
    {1.0} <music21.note.Note E>
    {2.0} <music21.note.Note F>
    {3.0} <music21.note.Note G>
{20.0} <music21.variant.Variant object of length 0.0>
{20.0} <music21.stream.Measure 6 offset=20.0>
    {0.0} <music21.note.Note A>
    {2.0} <music21.note.Note B->
    {3.0} <music21.note.Note A>
{24.0} <music21.stream.Measure 7 offset=24.0>
    {0.0} <music21.note.Note G>
    {1.0} <music21.note.Note A>
    {1.5} <music21.note.Note B->
    {2.0} <music21.note.Note C>
    {3.0} <music21.note.Note C>
{28.0} <music21.stream.Measure 8 offset=28.0>
    {0.0} <music21.note.Note F>
    {4.0} <music21.bar.Barline type=final>
r   variantactivateVariantsN
elongationdeletionr  T)isRemoverF  r   F)r?  ro  rL  r   r'  r  
lengthTyperU   _insertReplacementVariant_insertDeletionVariantr  _removeOrExpandGapsreplacementDurationcontainedHighestTimer  _insertInsertionVariant_fixMeasureNumbersr   )r   r  rl  rF  ro  rQ  elongationVariantsdeletionVariantsr  rt  deletedMeasuresinsertedMeasuresdeletedRegionsForRemovaldeletedRegionvDeletedMeasuresvInsertedMeasuresTupleinsertionRegionsForExpansionlengthDifferenceinsertionStarts                      rJ   rp  Stream.activateVariants.  s   b 	$112DEII   --goo>A U((%:J \)"))!,z) ''*},33AC ?$   $& !A11!A].D$++M:""#34 ##$:; " 	%%&>W[%\ (*$#A 44q7M7MM..y9A<Q<QQN(//FVAVYZX[0\]	 $ 	%%&BU\`%a#A221B###$:;
 ""#34 $ 	$$_G 	%%'rI   c                
   SSK Jn  UR                  5       nS/Ul        U R	                  U5      nU(       d  SnUR
                   H  nXWR                  UR                  5      -   nU R                  U5      R                  UR                  S   5      n	U	(       d  MV  US-  nU	S   n
UR                  U
5        U R                  U
5        U R                  X5        [        U
SS5      (       d  M  U
R                  Ul        M     US:  a#  U R                  U5        U R                  XT5        gg[!        5       n/ nSnUR#                  U 5      n	U	 Hk  nU R	                  U5      U-
  nUR                  X5        U R                  U5        [%        U[&        5      (       d  MP  UR                  UR                  5        Mm     UR
                   H  nXWR                  UR                  5      -   nU R                  X5        [%        U[&        5      (       d  MH  U(       a#  UR)                  5       Ul        UR                  nMr  SUl        UR                  U5        M     U R                  U5        U R                  XT5        [+        U5      X44$ )a	  
Helper function for activateVariants. Activates variants which are the same size there the
region they replace.

>>> v = variant.Variant()
>>> variantDataM1 = [('b', 'eighth'), ('c', 'eighth'),
...                  ('a', 'quarter'), ('a', 'quarter'),('b', 'quarter')]
>>> variantDataM2 = [('c', 'quarter'), ('d', 'quarter'), ('e', 'quarter'), ('e', 'quarter')]
>>> variantData = [variantDataM1, variantDataM2]
>>> for d in variantData:
...    m = stream.Measure()
...    for pitchName,durType in d:
...        n = note.Note(pitchName)
...        n.duration.type = durType
...        m.append(n)
...    v.append(m)
>>> v.groups = ['paris']
>>> v.replacementDuration = 8.0

>>> s = stream.Stream()
>>> streamDataM1 = [('a', 'quarter'), ('b', 'quarter'), ('a', 'quarter'), ('g', 'quarter')]
>>> streamDataM2 = [('b', 'eighth'), ('c', 'quarter'), ('a', 'eighth'),
...                 ('a', 'quarter'), ('b', 'quarter')]
>>> streamDataM3 = [('c', 'quarter'), ('b', 'quarter'), ('a', 'quarter'), ('a', 'quarter')]
>>> streamDataM4 = [('c', 'quarter'), ('b', 'quarter'), ('a', 'quarter'), ('a', 'quarter')]
>>> streamData = [streamDataM1, streamDataM2, streamDataM3, streamDataM4]
>>> for d in streamData:
...    m = stream.Measure()
...    for pitchName,durType in d:
...        n = note.Note(pitchName)
...        n.duration.type = durType
...        m.append(n)
...    s.append(m)
>>> s.insert(4.0, v)

>>> deletedMeasures, insertedMeasuresTuple = s._insertReplacementVariant(v)
>>> deletedMeasures
[]
>>> insertedMeasuresTuple
(0, [])
>>> s.show('text')
{0.0} <music21.stream.Measure 0 offset=0.0>
    {0.0} <music21.note.Note A>
    {1.0} <music21.note.Note B>
    {2.0} <music21.note.Note A>
    {3.0} <music21.note.Note G>
{4.0} <music21.variant.Variant object of length 8.0>
{4.0} <music21.stream.Measure 0 offset=4.0>
    {0.0} <music21.note.Note B>
    {0.5} <music21.note.Note C>
    {1.0} <music21.note.Note A>
    {2.0} <music21.note.Note A>
    {3.0} <music21.note.Note B>
{8.0} <music21.stream.Measure 0 offset=8.0>
    {0.0} <music21.note.Note C>
    {1.0} <music21.note.Note D>
    {2.0} <music21.note.Note E>
    {3.0} <music21.note.Note E>
{12.0} <music21.stream.Measure 0 offset=12.0>
    {0.0} <music21.note.Note C>
    {1.0} <music21.note.Note B>
    {2.0} <music21.note.Note A>
    {3.0} <music21.note.Note A>
r   rn  defaultrm  r0  FN)r?  ro  r'  r  r  r   r  containedSiter   r   r/  rU   r  rV   rV  r  r   replacedElementsr   rw   popleftr   )r   r  rl  ro  removedvStarttargetsMatchedro   	oInStreamr  targetToReplacer~  r  highestNumber
oInVariants                  rJ   ru   Stream._insertReplacementVariant@/  s)   D 	$//####A&NZZ"%6%6q%GG	229=PPQRQZQZ[\Q]^7"a'N&-ajO NN?3KK0KK	-UCC#2#9#9'  * !AF,	 " $gO! M((.G !//2V;
z- Aa))#**1884  ZZ"%6%6q%GG	I)a))&#2#:#:#< )* $% )//2  " KKNKK( )"57 7rI   c                   SSK Jn  [        5       nUR                  UR                  -
  nUR                  5       nS/Ul        UR                  Ul        U R                  U5      nXqR                  -   nUR                  U 5      n	U	 Hi  n
[        U
[        5      (       a  UR                  U
R                  5        U R                  U
5      U-
  nUR                  X5        U R                  U
5        Mk     Sn/ nUR                   H  n
[        U
[        5      (       aA  U(       a"  UR!                  5       U
l        U
R                  nOSU
l        UR                  U
5        XzR#                  UR$                  5      -   nU R                  X5        M     U R                  U5        U R                  Xv5        X/ 4['        U5      X44$ )a
  
Helper function for activateVariants used for inserting variants that are shorter than
the region they replace. Inserts elements in the variant and deletes elements in the
replaced region but does not close gaps.

Returns a tuple describing the region where elements were removed, the
gap is left behind to be dealt with by _removeOrExpandGaps.
Tuple is of form (startOffset, lengthOfDeletedRegion, []). The empty list is
expected by _removeOrExpandGaps
and describes the list of elements which should be exempted from shifting
for a particular gap. In the
case of deletion, no elements need be exempted.

>>> v = variant.Variant()
>>> variantDataM1 = [('b', 'eighth'), ('c', 'eighth'), ('a', 'quarter'),
...                  ('a', 'quarter'),('b', 'quarter')]
>>> variantDataM2 = [('c', 'quarter'), ('d', 'quarter'),
...                  ('e', 'quarter'), ('e', 'quarter')]
>>> variantData = [variantDataM1, variantDataM2]
>>> for d in variantData:
...    m = stream.Measure()
...    for pitchName,durType in d:
...        n = note.Note(pitchName)
...        n.duration.type = durType
...        m.append(n)
...    v.append(m)
>>> v.groups = ['paris']
>>> v.replacementDuration = 12.0

>>> s = stream.Stream()
>>> streamDataM1 = [('a', 'quarter'), ('b', 'quarter'), ('a', 'quarter'), ('g', 'quarter')]
>>> streamDataM2 = [('b', 'eighth'), ('c', 'quarter'), ('a', 'eighth'),
...                 ('a', 'quarter'), ('b', 'quarter')]
>>> streamDataM3 = [('c', 'quarter'), ('b', 'quarter'), ('a', 'quarter'), ('a', 'quarter')]
>>> streamDataM4 = [('c', 'quarter'), ('b', 'quarter'), ('a', 'quarter'), ('a', 'quarter')]
>>> streamData = [streamDataM1, streamDataM2, streamDataM3, streamDataM4]
>>> for d in streamData:
...    m = stream.Measure()
...    for pitchName,durType in d:
...        n = note.Note(pitchName)
...        n.duration.type = durType
...        m.append(n)
...    s.append(m)
>>> s.insert(4.0, v)


>>> deletedRegion, deletedMeasures, insertedMeasuresTuple = s._insertDeletionVariant(v)
>>> deletedRegion
(12.0, 4.0, [])
>>> deletedMeasures
[0]
>>> insertedMeasuresTuple
(0, [])
>>> s.show('text')
{0.0} <music21.stream.Measure 0 offset=0.0>
    {0.0} <music21.note.Note A>
    {1.0} <music21.note.Note B>
    {2.0} <music21.note.Note A>
    {3.0} <music21.note.Note G>
{4.0} <music21.variant.Variant object of length 12.0>
{4.0} <music21.stream.Measure 0 offset=4.0>
    {0.0} <music21.note.Note B>
    {0.5} <music21.note.Note C>
    {1.0} <music21.note.Note A>
    {2.0} <music21.note.Note A>
    {3.0} <music21.note.Note B>
{8.0} <music21.stream.Measure 0 offset=8.0>
    {0.0} <music21.note.Note C>
    {1.0} <music21.note.Note D>
    {2.0} <music21.note.Note E>
    {3.0} <music21.note.Note E>

r   rn  r  N)r?  ro  r   rx  ry  r'  r  r  r  r   rw   rU   r  rV   r  r   r  r  r  r   )r   r  rl  ro  r~  r  r  r  deletionStartr  ro   r  r  r  r  s                  rJ   rv  Stream._insertDeletionVariant/  s   V 	$'0013I3II//##&'&<&<###A&!7!77$$T* A!W%%&&qxx0++A.7JNN:)KKN  A!W%% #.668AH %&HHM AH %++A.!2!21??!CCIKK	%! & 	AF$
 b1!-
 	
rI   c                   SSK Jn  [        5       nUR                  5       nS/Ul        UR
                  Ul        U R                  U5      nUR                  U 5      nU Hi  n[        U[        5      (       a  UR                  UR                  5        U R                  U5      U-
  n	UR                  X5        U R                  U5        Mk     Sn
/ nUR                   H  n[        U[        5      (       aA  U(       a"  UR!                  5       Ul        UR                  n
OSUl        UR                  U5        XhR#                  UR$                  5      -   nU R                  X5        M     U
ca  U R'                  SU R                  U5      SSSS9R)                  [        5      nSn
U H$  nU
b  UR                  U
:  d  M  UR                  n
M&     U R                  U5        U R                  Xe5        X4U4$ )	a
  
Helper function for activateVariants. _removeOrExpandGaps must be called on the
expanded regions before this function,
or it will not work properly.


>>> v = variant.Variant()
>>> variantDataM1 = [('b', 'eighth'), ('c', 'eighth'), ('a', 'quarter'),
...                  ('a', 'quarter'),('b', 'quarter')]
>>> variantDataM2 = [('c', 'quarter'), ('d', 'quarter'),
...                  ('e', 'quarter'), ('e', 'quarter')]
>>> variantData = [variantDataM1, variantDataM2]
>>> for d in variantData:
...    m = stream.Measure()
...    for pitchName,durType in d:
...        n = note.Note(pitchName)
...        n.duration.type = durType
...        m.append(n)
...    v.append(m)
>>> v.groups = ['paris']
>>> v.replacementDuration = 4.0

>>> s = stream.Stream()
>>> streamDataM1 = [('a', 'quarter'), ('b', 'quarter'), ('a', 'quarter'), ('g', 'quarter')]
>>> streamDataM2 = [('b', 'eighth'), ('c', 'quarter'), ('a', 'eighth'),
...                 ('a', 'quarter'), ('b', 'quarter')]
>>> streamDataM3 = [('c', 'quarter'), ('b', 'quarter'), ('a', 'quarter'), ('a', 'quarter')]
>>> streamDataM4 = [('c', 'quarter'), ('b', 'quarter'), ('a', 'quarter'), ('a', 'quarter')]
>>> streamData = [streamDataM1, streamDataM2, streamDataM3, streamDataM4]
>>> for d in streamData:
...    m = stream.Measure()
...    for pitchName,durType in d:
...        n = note.Note(pitchName)
...        n.duration.type = durType
...        m.append(n)
...    s.append(m)
>>> s.insert(4.0, v)

>>> insertionRegionsForExpansion = [(8.0, 4.0, [v])]
>>> s._removeOrExpandGaps(insertionRegionsForExpansion, isRemove=False, inPlace=True)

>>> insertedMeasuresTuple, deletedMeasures = s._insertInsertionVariant(v)
>>> measurePrior, insertedMeasures = insertedMeasuresTuple
>>> measurePrior
0
>>> len(insertedMeasures)
1

>>> s.show('text')
{0.0} <music21.stream.Measure 0 offset=0.0>
    {0.0} <music21.note.Note A>
    {1.0} <music21.note.Note B>
    {2.0} <music21.note.Note A>
    {3.0} <music21.note.Note G>
{4.0} <music21.variant.Variant object of length 4.0>
{4.0} <music21.stream.Measure 0 offset=4.0>
    {0.0} <music21.note.Note B>
    {0.5} <music21.note.Note C>
    {1.0} <music21.note.Note A>
    {2.0} <music21.note.Note A>
    {3.0} <music21.note.Note B>
{8.0} <music21.stream.Measure 0 offset=8.0>
    {0.0} <music21.note.Note C>
    {1.0} <music21.note.Note D>
    {2.0} <music21.note.Note E>
    {3.0} <music21.note.Note E>
{12.0} <music21.stream.Measure 0 offset=12.0>
    {0.0} <music21.note.Note C>
    {1.0} <music21.note.Note B>
    {2.0} <music21.note.Note A>
    {3.0} <music21.note.Note A>
{16.0} <music21.stream.Measure 0 offset=16.0>
    {0.0} <music21.note.Note C>
    {1.0} <music21.note.Note B>
    {2.0} <music21.note.Note A>
    {3.0} <music21.note.Note A>

r   rn  r  Nrl   TFry  )r?  ro  r   r'  r  ry  rx  r  r  r   rw   rU   r  rV   r  r   r  r  r  r   r   )r   r  rl  ro  r~  r  r  r  ro   r  highestMeasurer  r  measuresToCheckr  s                  rJ   rz  Stream._insertInsertionVariantZ0  s   ` 	$'//##&'&<&<###A& $$T* A!W%%&&qxx0++A.7JNN:)KKN  A!W%% #.668AH%&XXN  !AH$++A. !2!21??!CCIKK	% " ! #66s7;7I7I!7LJNHMGK	 7 9
 :L9KG9T  N$!)QXX-F%&XXN %
 	AF$1?BBrI   c           	        USL a  U nO[         R                  " U 5      nUR                  R                  nUSL a  Sn[	        US S9n[        U5       H  u  pU	u  pn[        S U 5       5      nUS-   [        U5      :  a  XxS-      S   nSnOUnSnXk-   nUR                  X-   UUSSS	9 H]  n[        U5      U;   a  M  U(       d  UR                  R                  U;   a  M7  UR                  U5      nUR                  UUU-
  5        M_     M     GOSn0 n[	        US
 S9n[        U5       H<  u  pU	u  pnUS-   [        U5      :  a  XxS-      S   nSnOUnSnUnXk-   nXnUUU4UU
'   M>     [	        US S9 H  nUU   u  pnpn[        S U 5       5      nUR                  UUUSSS	9 H  n[        U5      U;   d!  U(       dB  UR                  R                  U;   a(  UR                  U5      nUR                  UUU-   5        M[  UR                  U5      nUR                  UUU-   5        M     M     UR                  5         USL a  gU$ )a  
Helper for activateVariants. Takes a list of tuples in the form
(startOffset, duration, [list, of, exempt, objects]). If isRemove is True,
gaps with duration will be closed at each startOffset.
Exempt objects are useful for gap-expansion with variants. The gap must push all objects
that occur after the insertion ahead, but the variant object
itself should not be moved except by other gaps. This is poorly written
and should be re-written, but it is difficult to describe.

>>> s = stream.Stream()
>>> s.insert(5.0, note.Note('a'))
>>> s.insert(10.0, note.Note('b'))
>>> s.insert(11.0, note.Note('c'))
>>> s.insert(12.0, note.Note('d'))
>>> s.insert(13.0, note.Note('e'))
>>> s.insert(20.0, note.Note('f'))
>>> n = note.Note('g')
>>> s.insert(15.0, n)

>>> sGapsRemoved = s._removeOrExpandGaps(
...             [(0.0, 5.0, []), (6.0, 4.0, []), (14.0, 6.0, [n])], isRemove=True)
>>> sGapsRemoved.show('text')
{0.0} <music21.note.Note A>
{1.0} <music21.note.Note B>
{2.0} <music21.note.Note C>
{3.0} <music21.note.Note D>
{4.0} <music21.note.Note E>
{5.0} <music21.note.Note F>
{15.0} <music21.note.Note G>

>>> sGapsExpanded = s._removeOrExpandGaps(
...            [(0.0, 5.0, []), (11.0, 5.0, []), (14.0, 1.0, [n])], isRemove=False)
>>> sGapsExpanded.show('text')
{10.0} <music21.note.Note A>
{15.0} <music21.note.Note B>
{21.0} <music21.note.Note C>
{22.0} <music21.note.Note D>
{23.0} <music21.note.Note E>
{25.0} <music21.note.Note G>
{31.0} <music21.note.Note F>
Trl   c                    U S   $ r/  rC   r  s    rJ   r  ,Stream._removeOrExpandGaps.<locals>.<lambda>1  	    6RS9rI   r!   c              3  8   #    U  H  n[        U5      v   M     g 7frs   r   rm   s     rJ   rp   -Stream._removeOrExpandGaps.<locals>.<genexpr>1       %C]bee]r  rm  r   Fry  c                    U S   $ r/  rC   r  s    rJ   r  r  81  r  rI   c                    SU -  $ Nr   rC   )rj  s    rJ   r  r  H1  s    R#XrI   c              3  8   #    U  H  n[        U5      v   M     g 7frs   r   rm   s     rJ   rp   r  K1  r  r  N)r  r  r   r  r  r  re  r   r   r   r   originIdr  r  r   )r   listOffsetDurExemptionrs  rF  rQ  returnObjDurationr  
listSortedr  durTupler  durationAmountexemptObjectsexemptObjectSetr  
includeEndro   r  
shiftsDictexemptShiftrM   s                        rJ   rw  Stream._removeOrExpandGaps0  s   V d?Id+I%..<< tH 6<TUJ(4=E:]"%%C]%C"Cq53z?* *q5 1! 4I!&J 1I!%J#4"66{7S7@JTHMGK	 7 MA !u/ "q||'<'<'O $%$5$5i$@M221mh6NOM  52 HJ 6<TUJ(4=E:]q53z?* *q5 1! 4I!&J 1I!%J&#4+3
+8++G
;'  5 !1EFNXY_N`KZ"%%C]%C"C"66v7@JTHMGK	 7 MA 10 'ALL,A,A_,T()(9(9)(D!66q-+:UV $%$5$5i$@M221mh6NOM	 G, 	%%'d?rI   c                \  ^ UR                  U5        UnU(       d  gU4S jmUR                  TS9  U R                  [        5      R	                  5       n/ nSn0 n0 nU H  n	[        U	[        5      (       ac  U	u  pU(       d  M%  U[        U5      -  nU
S-   nU H0  nUR                  U5        UR                  U5        Xl
        US-  nM2     XgU
S-   '   XhU'   M{  US-  nXgU	S-   '   XhU	S-   '   M     Sn[        US S9 HM  nX   nU H>  nUb  UR                  U:  d  M  UR                  U:  d  M*  UR                  U-   Ul
        M@     UnMO     Sn[        US S9 HM  nX   nU H>  nUb  UR                  U:  d  M  UR                  U:  d  M*  UR                  U-   Ul
        M@     UnMO     g)a  
Corrects the measures numbers of a string of measures given a list of measure numbers
that have been deleted and a
list of tuples (highest measure number below insertion, number of inserted measures).

>>> s = converter.parse('tinynotation: 4/4 d4 e4 f4 g4   a2 b-4 a4    g4 a8 g8 f4 e4  g1')
>>> s[-1].offset = 20.0
>>> s.show('text')
{0.0} <music21.stream.Measure 1 offset=0.0>
    {0.0} <music21.clef.TrebleClef>
    {0.0} <music21.meter.TimeSignature 4/4>
    {0.0} <music21.note.Note D>
    {1.0} <music21.note.Note E>
    {2.0} <music21.note.Note F>
    {3.0} <music21.note.Note G>
{4.0} <music21.stream.Measure 2 offset=4.0>
    {0.0} <music21.note.Note A>
    {2.0} <music21.note.Note B->
    {3.0} <music21.note.Note A>
{8.0} <music21.stream.Measure 3 offset=8.0>
    {0.0} <music21.note.Note G>
    {1.0} <music21.note.Note A>
    {1.5} <music21.note.Note G>
    {2.0} <music21.note.Note F>
    {3.0} <music21.note.Note E>
{20.0} <music21.stream.Measure 4 offset=20.0>
    {0.0} <music21.note.Note G>
    {4.0} <music21.bar.Barline type=final>
>>> s.remove(s.measure(2))
>>> s.show('text')
{0.0} <music21.stream.Measure 1 offset=0.0>
    {0.0} <music21.clef.TrebleClef>
    ...
{8.0} <music21.stream.Measure 3 offset=8.0>
    {0.0} <music21.note.Note G>
    ...
{20.0} <music21.stream.Measure 4 offset=20.0>
    {0.0} <music21.note.Note G>
    {4.0} <music21.bar.Barline type=final>
>>> deletedMeasures = [2]
>>> m1 = stream.Measure()
>>> m1.repeatAppend(note.Note('e'),4)
>>> s.insert(12.0, m1)
>>> m2 = stream.Measure()
>>> m2.repeatAppend(note.Note('f'),4)
>>> s.insert(16.0, m2)
>>> s.show('text')
{0.0} <music21.stream.Measure 1 offset=0.0>
    {0.0} <music21.clef.TrebleClef>
    ...
{8.0} <music21.stream.Measure 3 offset=8.0>
    {0.0} <music21.note.Note G>
    ...
{12.0} <music21.stream.Measure 0 offset=12.0>
    {0.0} <music21.note.Note E>
    {1.0} <music21.note.Note E>
    {2.0} <music21.note.Note E>
    {3.0} <music21.note.Note E>
{16.0} <music21.stream.Measure 0 offset=16.0>
    {0.0} <music21.note.Note F>
    {1.0} <music21.note.Note F>
    {2.0} <music21.note.Note F>
    {3.0} <music21.note.Note F>
{20.0} <music21.stream.Measure 4 offset=20.0>
    {0.0} <music21.note.Note G>
    {4.0} <music21.bar.Barline type=final>
>>> insertedMeasures = [(3, [m1, m2])]

>>> s._fixMeasureNumbers(deletedMeasures, insertedMeasures)
>>> s.show('text')
{0.0} <music21.stream.Measure 1 offset=0.0>
    {0.0} <music21.clef.TrebleClef>
    {0.0} <music21.meter.TimeSignature 4/4>
    ...
{8.0} <music21.stream.Measure 2 offset=8.0>
    {0.0} <music21.note.Note G>
    ...
{12.0} <music21.stream.Measure 3 offset=12.0>
    {0.0} <music21.note.Note E>
    ...
{16.0} <music21.stream.Measure 4 offset=16.0>
    {0.0} <music21.note.Note F>
    ...
{20.0} <music21.stream.Measure 5 offset=20.0>
    {0.0} <music21.note.Note G>
    {4.0} <music21.bar.Barline type=final>
>>> fixedNumbers = []
>>> for m in s.getElementsByClass(stream.Measure):
...    fixedNumbers.append( m.number )
>>> fixedNumbers
[1, 2, 3, 4, 5]

Nc                P   > [        U [        5      (       a  T" U S   5      $ U c  gU $ )Nr   i)r   r   )numOrNumTuplemeasureNumberSortRoutines    rJ   r  ;Stream._fixMeasureNumbers.<locals>.measureNumberSortRoutine1  s/    -///a0@AA&$$rI   r!   r   rm  c                    SU -  $ r  rC   r  s    rJ   r  +Stream._fixMeasureNumbers.<locals>.<lambda>1      b1frI   c                    SU -  $ r  rC   r  s    rJ   r  r  2  r  rI   )r  r   r   rw   r6   r   r   r   r  rU   r  r  )r   r~  r  allMeasuresoldMeasuresnewMeasurescumulativeNumberShiftoldCorrectionsnewCorrectionsr  measurePriorextendedMeasuresnextMeasurer  previousBoundaryr   shiftr  s                    @rJ   r{  Stream._fixMeasureNumberse1  s   ~ 	/0%	% 	56--g6==? ! )M-//1>.'%-=)>>%*Q.)A&&q)&&q)*H1$K	 *
 4I|a/0.C{+%*%4I}q014I}q01# ),  ,<=A"%E #+qxx:J/Jxx1}#$88e#3 !  ! >  ,<=A"%E #+qxx:J/Jxx1}#$88e#3 !  ! >rI   c                  SSK Jn  X;  a  UR                  SU SU  35      eUSL a  U nUnOWU R                  S5      nU R                  R                  5       R                  U5      nUR                  5       R                  U   nU GH  n[        R                  " U5      n	Sn
U	R                   GHp  nUR                  nSU;   a8  UR                  nX;  d  UR                  S	;   a  U	R                  U5        GOSn
GOS
U;   a  UR                  R                   nUR#                  U	5      nU	R                  U5        [$        R&                  " 5       nSUR(                  l        UUR                  l        U	R-                  UU5        OSU;   a  UR                  R                   nUR.                   H  nUR                  U5        M     [$        R&                  " 5       nUUR                  l        SUR(                  l        UR-                  SU5        SUR(                  l        GMs     U	R1                  USSS9  U
(       d  GM  UR-                  SU	5        GM     U(       a  gU$ )a,  
Takes a part within the score and a list of variant groups within that part.
Puts the variant object
in a part surrounded by hidden rests to mimic the appearance of an ossia despite limited
musicXML support for ossia staves. Note that this will ignore variants with .lengthType
'elongation' and 'deletion' as there is no good way to represent ossia staves like those
by this method.

>>> sPartStr = 'd4 e4 f4 g4   a2 b-4 a4    g4 a8 g8 f4 e4    d2 a2 '
>>> v1Str =    '              a2. b-8 a8 '
>>> v2Str =    '                                             d4 f4 a2 '

>>> sPartStr += "d4 e4 f4 g4    a2 b-4 a4    g4 a8 b-8 c'4 c4    f1"

>>> sPartStream = converter.parse('tinynotation: 4/4 ' + sPartStr)
>>> sPartStream.makeMeasures(inPlace=True)  # maybe not necessary?
>>> v1stream = converter.parse('tinynotation: 4/4 ' + v1Str)
>>> v2stream = converter.parse('tinynotation: 4/4 ' + v2Str)

>>> v1 = variant.Variant()
>>> v1measure = stream.Measure()
>>> v1.insert(0.0, v1measure)
>>> for e in v1stream.notesAndRests:
...    v1measure.insert(e.offset, e)

>>> v2 = variant.Variant()
>>> v2measure = stream.Measure()
>>> v2.insert(0.0, v2measure)
>>> for e in v2stream.notesAndRests:
...    v2measure.insert(e.offset, e)

>>> v3 = variant.Variant()
>>> v2.replacementDuration = 4.0
>>> v3.replacementDuration = 4.0
>>> v1.groups = ['variant1']
>>> v2.groups = ['variant2']
>>> v3.groups = ['variant3']

>>> sPart = stream.Part()
>>> for e in sPartStream:
...    sPart.insert(e.offset, e)

>>> sPart.insert(4.0, v1)
>>> sPart.insert(12.0, v2)
>>> sPart.insert(20.0, v3)  # This is a deletion variant and will be skipped
>>> s = stream.Score()
>>> s.insert(0.0, sPart)
>>> streamWithOssia = s.showVariantAsOssialikePart(sPart,
...          ['variant1', 'variant2', 'variant3'], inPlace=False)
>>> #_DOCS_SHOW streamWithOssia.show()

r   rn  zCould not find z in TshowVariantAsOssialikePartFr'  )rq  rr  r  rw   rl   )rF  rl  N)r?  ro  VariantExceptionrL  rN  r6   r%  r   r  r  r   r/  r  rt  r  r   r  r  r%   r  r*   hideObjectOnPrintrV   r  rp  )r   containedPartvariantGroupsrF  ro  rQ  
returnPartcontainedPartIndexvariantGroupnewPartexpressedVariantsExistro   eClasseselementGroupsnQuarterLengthnOffsetr  measureDurationr  s                      rJ   r  !Stream.showVariantAsOssialikePart	2  s   l 	$ %**_]O4PTv+VWWd?I&J112NOI!%!2!2!4!:!:=!I")//0BCJ *LmmJ/G%*"%%99($%HHM): ||/IIq)15."h.%&ZZ%=%=N//8GNN1%		A04AGG-/=AJJ,NN7A.(*&'jj&>&>O__ -		A/>AJJ,04AGG-HHS!$,0)5 &8 $$\4T$R%%  g.C *F rI   )r~   r{   r   r   r   r   r}   rb   re   rd   r   rc   ra   rM   r)  r.   rs   )r   z?t.Union[None, base.Music21Object, Sequence[base.Music21Object]]rh   r1   )rq  r   rp  rF  )rq  #iterator.StreamIterator[M21ObjType])r   r   rq  &iterator.RecursiveIterator[M21ObjType])r   r   rq  r   )r   r   rq  zlist[M21ObjType])r   type[ChangedM21ObjType]rq  z-iterator.RecursiveIterator[ChangedM21ObjType])r   r   rq  r  )r   zCollection[type]rq  r  )r   zCt.Union[str, int, slice, type[ChangedM21ObjType], Collection[type]]rq  z|t.Union[iterator.RecursiveIterator[M21ObjType], iterator.RecursiveIterator[ChangedM21ObjType], M21ObjType, list[M21ObjType]])rq  zM21ObjType | None)r   base.Music21Objectrq  r  )rq  ztuple[M21ObjType, ...])r  z%Stream | Iterable[base.Music21Object])r   r   r   z'Stream'rq  r   )rq  clef.Clef | None)r&  r  )rq  meter.TimeSignature | None)r0  r  )rq  key.KeySignature | None)r9  r  )rG  r   )rq  None)r   r   r  
str | Nonerq  r   )r   r  )r\  r  rq  r  F)r   r  rq  r   )r  z1base.Music21Object | Sequence[base.Music21Object])r%  z
int | Nonerq  r  )r   r   r  zdict[int, t.Any] | Nonerq  r   )r   r   rq  r   )rL   r  rM   z&int | float | Fraction | OffsetSpecial)r  r   rq  r  
r  r  r  r  rY   r  r  r  rq  r  )rq  zbase._SplitTuple)NNNN)
r  r  r  r  r  r  r  r  r  r  )rO  r   )r  r   )rh  zstr | Iterable[str]rq  r  )rh  r  rq  z*iterator.StreamIterator[ChangedM21ObjType])rh  z!Iterable[type[ChangedM21ObjType]]rq  r  )rh  zWt.Union[str, Iterable[str], type[ChangedM21ObjType], Iterable[type[ChangedM21ObjType]]]rq  zXt.Union[iterator.StreamIterator[M21ObjType], iterator.StreamIterator[ChangedM21ObjType]])rq  ziterator.StreamIterator)rq  base.Music21Object | None)rM   r   r  It.Union[str, Iterable[str], type[M21ObjType], type, Iterable[type], None]r  r  rq  r  )rM   r   r  r  rq  r  )r  	int | strr  r  r  r  rq  zlist[Measure])rq  zStream[Measure])rq  zMeasure | None)rw   rh  z5list[t.Type] | list[str] | tuple[t.Type] | tuple[str]rq  z,OrderedDict[float | Fraction, list[Measure]])rq  z(iterator.StreamIterator[spanner.Spanner])rq  bool | t.Literal['unknown'])r  r  )r   r   rA  r  rB  r  rC  r  rF  t.Literal[True]rq  r  )r   r   rA  r  rB  r  rC  r  rF  t.Literal[False]rq  r   )r   r   rA  r  rB  r  rC  r  rF  r  rq  StreamType | None)rq  z
bool | str)rC  r  )rm  r  rC  r  rF  r  )rq  zStream[instrument.Instrument])rq  zinstrument.Instrument | None)NNN)      @r  NN)FF)NNFNfinalFF)NFFFF)r
  list[pitch.Pitch] | Noner  r  r  r  r  bool | key.KeySignaturer  r  r  r  r  r  r  r  rF  r  r  r  r  r  r  set[str] | None)r   r   r
  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  )r   r   rF  r  r:  r  rq  r  )r   r   rF  r  r:  r  rq  r   )r   r   rF  r  r:  r  rq  r  )Fr  )rv  r  rq  r  )rv  r  rq  z"iterator.RecursiveIterator[Stream])rv  r  r>  r  r  r   rq  zSt.Union[iterator.RecursiveIterator[M21ObjType], iterator.RecursiveIterator[Stream]])r   r  rq  zStream | NoneT)rq  'music21.duration.Duration')r  r  )rq  metadata.Metadata | None)r  r  rq  r  )r  z+str | int | 'music21.interval.IntervalBase')rC   TTFF)
rL  zIterable[int]rU  r  rV  r  rF  r  rY   r  )r   r   rc  r  rq  r   )NTFFrW  )rq  z)iterator.StreamIterator[note.GeneralNote])rq  z%iterator.StreamIterator[note.NotRest])rq  zlist[pitch.Pitch])r  r  r  r  r  r  r  r  r  r  r  r  r  r  rq  zlist[note.NotRest])r  r  r  r  r  r  r  r  r  r  r  r  r  r  rq  zlist[note.Note])r  r  r  r  r  r  r  r  r  r  r  r  r  r  rq  zlist[note.NotRest | None])r  r  r  r  r  r  r  r  r  r  r  r  r  r  rq  zGt.Union[list[note.NotRest | None], list[note.NotRest], list[note.Note]])rq  zlist[tuple[OffsetQL, OffsetQL]])rq  zlist[list[int]])rq  zint | tuple[int, list[str]])TFF)re  r  rY   r  rf  r  rq  rg  TF)rD   rE   rF   rG   __doc__rt   r0  rS   __annotations__r2   ELEMENTS_FIRSTrT   r*   StreamStyle_styleClass
_DOC_ORDERrg   r1   r   rz   r   r   r   r   r   r   r   r   r   r   r   r   r   propertyr   setterr  r  r  r  r   r,  r6  rB  rJ  r  rP  r   
deprecatedr]  rb  rj  r%  r  r$  r  r  r  r  r  r  r  r  rV   r  rU   rW   rX   r  r  r1  rG  rr  r|  r  r  r  r  r   r  r  r  r   r  r  r  r  r   ALLr  r  	frozensetr  r%  r+  r0  r  r7  r:  r=  rG  rc  r^  r_  rM  rM  r|  r%   r  r  r  r  r  r  r  r  r  r  r"  r  r  r  r  r  r  rO  r-   rN  r=  rw  r   r  rQ  rZ   rY   r   r  r  r  r  r  r  r  r  r   r  r  secondsr  r  r  r  r  r  r#   r  r  r  r  r  r  r]   r_   r`   r^   rO  rf  rj  rv  r|  r  r,  r  r*  r  r  r  r[   r\   r  r  r  r  r  r  r  r  r  r   r  r  r  r%  r)  r,  r7  rM  rX  r\  r  rp  ru  rv  rz  rw  r{  r  rH   __classcell__r   s   @rJ   rQ   rQ   h   s   Zz HI #NI##0#?#?M=?##KIJ
(&;(!I~ (X IM?#
 AV@]@]?#!F?#
 )>?# ?#B	9 9: :<<5"       " 
7   
0   
0 W
1W
3W
r42 HD a. a.F __+# +#Z('T#AFB &  & P 
[[
" 
" 1 1f 	 	 " "H  " 3 3j 3 3( :: :4 vu '1 22@G#R<FB "	a6La6F* *X6>(@/* =AB 04B$9B $.	B BH	 	<@55#55 155nJ\ A(  !A(FDOLw2r-5dv4z %*8.18.28J !&#'I*I/I 	I
 !I
 -1IV +0 O) O)h +/%)49+/[1 5E[1@ "#"" %%*A 03AF (-u C C ,15 	1 	1 %) $ $ $	. ',.!.. . 	.  $.b?@L )-:( %**.:(#&:(@ ,= C 
 ,C J 
 ,M C 
Q?-Q?!TQ?fUBR:$R v
  &*v 
!vD D #DD
D D 
!D` BOBO
BO 
!BOjARZZ Z
  Z 
ZB H%))e 
eV P"'	8 -9	8x  $#" "++HX LX} H}  
4} ~ && ,.> ;E ;Lz 0 0 8 8& % %. S S  &**/   $	
 $( ! 
   &**/$)   $	
 $( " 
  &**/EE E  $	E
 $(E E 
EN'X +0	\ $(\B #(*/Q  Q $(	Q
 Qj )-(,"&-1	FT )-%)#	Z )F	Z| (,$(#	# )C	#J ,099T? X u X zM#^2&0P$#L ?B046v0/j #Un>@HX !
: "!&
. "(-"&"2"2!4	
( $)DX] 
* -137;?1515,1%)#$-1%)^ *^ 1	^
 #9^ /^ /^ &*^ #^ ^ ^ ^ '+^ #^@
- "&*."#9=@D>B>B26+0,1:>26z  !7z  (>z  '<z  &<z  ,0z  %)z  &*z  48z  #0z x 38 @D 
 "	 ! 	
 
   %*!	 " 	
 
  !	BB B 	B
 
BHFT)!XbHW Wr    16#' `-`
 &L` `  $( S,S
 &HS S %*+/%' [![ %)[ #	[
&Q[B 	22
 
2h"# ", ",H+ ^* ^*@ 6+ 6+p+X  (# (#T __! !/b {K &6 &GPOb6.b . / 	J8( lH"5
        i$f f6fP 9A'+URh 8= 0 ;@ 1j 02#!%j ,j  j  	j 
 j  j X, ,b BF&*EQf 15e "N $Wt   !+0	4r*@(K1Z$6r & &P 9 9v A AJ   ',!!!  %	
       
    !!!  $	
       
     !!!#(  	
     ! 
! "   !!!b b 	b
 b b b b b
bHNb&#J*X6p=~  >75r!N'h0d21hX$t2n %*D JX +0 Y( Y(v -2 n`	$ 16u @P  $	EE E 	E
 
-ERb$ b bH	R7lB
HKC\ 49||b!H SX o orI   rQ   c                  4    \ rS rSrSr\R                  rSrSr	g)r,  i}2  a  
A Stream subclass for declaring that all the music in the
stream belongs to a certain "voice" for analysis or display
purposes.

Note that both Finale's Layers and Voices as concepts are
considered Voices here.

Voices have a sort order of 1 greater than time signatures
r>  rC   N)
rD   rE   rF   rG   r  r2   r  rT   rS   rH   rC   rI   rJ   r,  r,  }2  s    	 "00MNrI   r,  c            
        ^  \ rS rSr% Sr\R                  rSrS/r	SSSSS	S
SSSS.	r
S\S'   SS.S$U 4S jjjrS rS rU 4S jr S%S jrS&S jrS'S jr\S 5       rS rS rS r\" \\SS9rS  rS! r\" \\S"S9rS#rU =r$ )(rw   i2  a  
A representation of a Measure organized as a Stream.

All properties of a Measure that are Music21 objects are found as part of
the Stream's elements.

Measure number can be explicitly set with the `number` keyword:

>>> m4 = stream.Measure(number=4)
>>> m4
<music21.stream.Measure 4 offset=0.0>
>>> m4.number
4

If passed a single integer as an argument, assumes that this int
is the measure number.

>>> m5 = stream.Measure(5)
>>> m5
<music21.stream.Measure 5 offset=0.0>

Number can also be a string if there is a suffix:

>>> m4 = stream.Measure(number='4a')
>>> m4
<music21.stream.Measure 4a offset=0.0>
>>> m4.numberSuffix
'a'

Though they have all the features of general streams,
Measures have specific attributes that allow for setting their number
and numberSuffix, keep track of whether they have a different clef or
key or timeSignature than previous measures, allow for padding (and pickups),
and can be found as a "measure slice" within a score and parts.
Tr   zh
            Boolean describing if the TimeSignature
            is different than the previous Measure.zFBoolean describing if the Clef is different than the previous Measure.zJBoolean describing if KeySignature is different than the previous Measure.zu
            A number representing the displayed or shown
            Measure number as presented in a written Score.aP  
            If a Measure number has a string annotation, such as "a" or similar,
            this string is stored here. Note that in MusicXML, such
            suffixes often appear as
            prefixes to measure numbers.  In music21 (like most measure
            numbering systems), these
            numbers appear as suffixes.zG
            Enum describing if the measure number should be displayed.z
            A suggestion for layout width, though most rendering systems do not support
            this designation. Use :class:`~music21.layout.SystemLayout`
            objects instead.ao  
            defines empty space at the front of the measure for purposes of determining
            beat, etc for pickup/anacrusis bars.  In 4/4, a
            measure with a one-beat pickup
            note will have a `paddingLeft` of 3.0.
            (The name comes from the CSS graphical term
            for the amount of padding on the left side of a region.)a  
            defines empty space at the end of the measure for purposes of determining
            whether or not a measure is filled.
            In 4/4, a piece beginning a one-beat pickup
            note will often have a final measure of three beats, instead of four.
            The final
            measure should have a `paddingRight` of 1.0.
            (The name comes from the CSS graphical term
            for the amount of padding on the right side of a region.))	timeSignatureIsNew	clefIsNewkeyIsNewr  r  
showNumberlayoutWidthpaddingLeftpaddingRightrf   rg   r   r  c                 > [        U5      S:X  a%  [        US   [        5      (       a  US:X  a  US   nSn[        TU ]  " U0 UD6  SU l        SU l        SU l        SU l        SU l	        SU l
        S U l        [        U[        5      (       a6  [        R                  " U5      u  pE[        U5      U l        U(       a  XPl        OXl        [         R"                  U l        S U l        g )Nrm  r   rC   Frl   )r   r   r   ry   rz   r  r	  r
  filledr  r  r  r   r   r  r  r3   DEFAULTr  r  )r   r  argsr   realNumsuffixr   s         rJ   rz   Measure.__init__2  s    t9>ja#666Q;!WFD$+(+ #( &)&) fc""$226:OGg,DK$*! K$,,  rI   c                    U R                   (       a"  [        U R                  5      U R                   -   $ [        U R                  5      $ )a  
Return the measure `.number` with the `.numberSuffix` as a string.

>>> m = stream.Measure()
>>> m.number = 4
>>> m.numberSuffix = 'A'
>>> m.measureNumberWithSuffix()
'4A'

Test that it works as musicxml

>>> xml = musicxml.m21ToXml.GeneralObjectExporter().parse(m)
>>> print(xml.decode('utf-8'))
<?xml version="1.0"...?>
...
<part id="...">
    <!--========================= Measure 4 ==========================-->
    <measure implicit="no" number="4A">
...

Test round tripping:

>>> s2 = converter.parseData(xml)
>>> print(s2[stream.Measure].first().measureNumberWithSuffix())
4A

Note that we use print here because in parsing the data will become a unicode string.
)r  r   r  r   s    rJ   measureNumberWithSuffixMeasure.measureNumberWithSuffix3  s6    : t{{#d&7&777t{{##rI   c                B    U R                  5       SU R                   3-   $ )Nz offset=)r  rM   r   s    rJ   r   Measure._reprInternal/3  s!    ++-(4;;-0HHHrI   c           	        > [         TU ]  U5        S H*  n[        X5      (       d  M  [        X[	        X5      5        M,     g)a  
Given another Measure, configure all non-element attributes of this
Measure with the attributes of the other Measure. No elements
will be changed or copied.

This method is necessary because Measures, unlike some Streams,
have attributes independent of any stored elements.

Overrides base.Music21Object.mergeAttributes

>>> m1 = stream.Measure()
>>> m1.id = 'MyMeasure'
>>> m1.clefIsNew = True
>>> m1.number = 2
>>> m1.numberSuffix = 'b'
>>> m1.layoutWidth = 200

>>> m2 = stream.Measure()
>>> m2.mergeAttributes(m1)
>>> m2.layoutWidth
200
>>> m2.id
'MyMeasure'
>>> m2
<music21.stream.Measure 2b offset=0.0>

Try with not another Measure:

>>> m3 = stream.Stream()
>>> m3.id = 'hello'
>>> m2.mergeAttributes(m3)
>>> m2.id
'hello'
>>> m2.layoutWidth
200
)	r  r	  r
  r  r  r  r  r  r  Nry   rP  rT  rU  rV  rW  s      rJ   rP  Measure.mergeAttributes33  s;    L 	&]Du##GE$89]rI   c                |   U(       d  [         R                  " U 5      nOU nUR                  5       nS H  nXT;   d  M
  XE	 M     UR                  " SSSS.UD6  UR                  cD  UR	                  SSS9nUR	                  SSS9nU(       a  US   nO U R                  5       nXl        [        R                  " USSS9  [        R                  " USSS	9  UR                  SS
9  U/UR                  Q H  n	[        R                  " U	SS
9  M     U(       d  U$ g! [        [        R                  4 a    US   n Nf = f)a  
This method calls a sequence of Stream methods on this
:class:`~music21.stream.Measure` to prepare notation.

If `inPlace` is True, this is done in-place; if
`inPlace` is False, this returns a modified deep copy.

>>> m = stream.Measure()
>>> n1 = note.Note('g#')
>>> n2 = note.Note('g')
>>> m.append([n1, n2])
>>> m.makeNotation(inPlace=True)
>>> m.notes[1].pitch.accidental
<music21.pitch.Accidental natural>
)r  r  r  T)r  rF  NFrH  r   r)  r*  rL  rC   )r  r  r  r,  rM  bestTimeSignaturero  r$   r0  r-   r.  r/  r  r7  r2  )
r   rF  subroutineKeywordsr  srkCopy
illegalKeycontextMetersdefaultMetersrv  m_or_vs
             rJ   r-   Measure.makeNotationa3  sM   4 d#AA$))+MJ$' N 	
TdDTGT ??" //d>C 0 EM//e>B 0 DM"1%*//1B !O33AtTR00DTR	D!n188nF++FDA % H ()=)=> *&q)B*s   D D;:D;c                d    Uc  U R                   n[        U R                  UR                  -  5      $ )au  
Return a floating point value greater than 0 showing the proportion
of the bar duration that is filled based on the highest time of
all elements. 0.0 is empty, 1.0 is filled; 1.5 specifies of an
overflow of half.

Bar duration refers to the duration of the Measure as suggested by
the `TimeSignature`. This value cannot be determined without a `TimeSignature`.

An already-obtained Duration object can be supplied with the `barDuration`
optional argument.

>>> import copy
>>> m = stream.Measure()
>>> m.timeSignature = meter.TimeSignature('3/4')
>>> n = note.Note()
>>> n.quarterLength = 1
>>> m.append(copy.deepcopy(n))
>>> m.barDurationProportion()
Fraction(1, 3)
>>> m.append(copy.deepcopy(n))
>>> m.barDurationProportion()
Fraction(2, 3)
>>> m.append(copy.deepcopy(n))
>>> m.barDurationProportion()
1.0
>>> m.append(copy.deepcopy(n))
>>> m.barDurationProportion()
Fraction(4, 3)
)r.  r   r  r  )r   r.  s     rJ   barDurationProportionMeasure.barDurationProportion3  s2    @ **Kd&&)B)BBCCrI   c                V   U(       a^  / nU R                    H5  n[        U[        R                  5      (       d    OUR	                  U5        M7     U(       a  U R                  USS9  U R                  nU R                  US9nUS:  a#  SU-
  n[        UR                  U-  5      U l
        gg)af  
Given an incompletely filled Measure, adjust the `paddingLeft` value to
represent contained events as shifted to fill the right-most duration of the bar.

Calling this method will overwrite any previously set `paddingLeft` value,
based on the current TimeSignature-derived `barDuration` attribute.

>>> m = stream.Measure()
>>> m.timeSignature = meter.TimeSignature('3/4')
>>> n = note.Note()
>>> n.quarterLength = 1.0
>>> m.append(n)
>>> m.padAsAnacrusis()
>>> m.paddingLeft
2.0

>>> m.timeSignature = meter.TimeSignature('5/4')
>>> m.padAsAnacrusis()
>>> m.paddingLeft
4.0


Empty space at the beginning of the measure will not be taken in account:

>>> m = stream.Measure()
>>> m.timeSignature = meter.TimeSignature('3/4')
>>> n = note.Note(type='quarter')
>>> m.insert(2.0, n)
>>> m.padAsAnacrusis()
>>> m.paddingLeft
0.0

If useInitialRests is True, then rests at the beginning of the measure
are removed.  This is especially useful for formats that don't give a
way to specify a pickup measure (such as tinynotation) or software
that generates incorrect opening measures.  So, to fix the problem before,
put a rest at the beginning and call useInitialRests:

>>> r = note.Rest(type='half')
>>> m.insert(0, r)
>>> m.padAsAnacrusis(useInitialRests=True)
>>> m.paddingLeft
2.0

And the rest is gone!

>>> m.show('text')
{0.0} <music21.meter.TimeSignature 3/4>
{0.0} <music21.note.Note C>


Only initial rests count for useInitialRests:

>>> m = stream.Measure()
>>> m.timeSignature = meter.TimeSignature('3/4')
>>> m.append(note.Rest(type='eighth'))
>>> m.append(note.Rest(type='eighth'))
>>> m.append(note.Note('C4', type='quarter'))
>>> m.append(note.Rest(type='eighth'))
>>> m.append(note.Note('D4', type='eighth'))
>>> m.show('text')
{0.0} <music21.meter.TimeSignature 3/4>
{0.0} <music21.note.Rest eighth>
{0.5} <music21.note.Rest eighth>
{1.0} <music21.note.Note C>
{2.0} <music21.note.Rest eighth>
{2.5} <music21.note.Note D>
>>> m.padAsAnacrusis(useInitialRests=True)
>>> m.paddingLeft
1.0
>>> m.show('text')
{0.0} <music21.meter.TimeSignature 3/4>
{0.0} <music21.note.Note C>
{1.0} <music21.note.Rest eighth>
{1.5} <music21.note.Note D>
T)rs  )r.  rm  N)r  r   r%   r  rU   r  r.  r(  r   r  r  )r   useGapsuseInitialRests
removeListgnr.  
proportionproportionShifts           rJ   padAsAnacrusisMeasure.padAsAnacrusis3  s    Z J((!"dii00!!"% ) JT: &&//K/H
>*nO%k&?&?/&QRD rI   c                L   U R                   b  U R                   nUR                  $ U R                  SSSS9nU(       d   U R                  5       nUR                  $ US   nUR                  $ ! [        R                   a#    [
        R                  " U R                  5      s $ f = f)a  
Return the bar duration, or the Duration specified by the TimeSignature,
regardless of what elements are found in this Measure or the highest time.
TimeSignature is found first within the Measure,
or within a context based search.

To get the duration of the total length of elements, just use the
`.duration` property.

Here we create a 3/4 measure and "over-stuff" it with five quarter notes.
`barDuration` still gives a duration of 3.0, or a dotted quarter note,
while `.duration` gives a whole note tied to a quarter.


>>> m = stream.Measure()
>>> m.timeSignature = meter.TimeSignature('3/4')
>>> m.barDuration
<music21.duration.Duration 3.0>
>>> m.repeatAppend(note.Note(type='quarter'), 5)
>>> m.barDuration
<music21.duration.Duration 3.0>
>>> m.duration
<music21.duration.Duration 5.0>

The objects returned by `barDuration` and `duration` are
full :class:`~music21.duration.Duration`
objects, will all the relevant properties:

>>> m.barDuration.fullName
'Dotted Half'
>>> m.duration.fullName
'Whole tied to Quarter (5 total QL)'
TF)rE  rI  rq  r   )	r,  rM  r  r   r  r   r  r  r.  )r   rv  r  s      rJ   r.  Measure.barDuration94  s    J )##B ~~ --D<AAE . GH ?//1B ~~ a[~~ $44 ?#,,T-=-=>>?s   A, ,4B#"B#c                .    [         R                  " U 5      $ )a  
Given a Measure with elements in it,
get a TimeSignature that contains all elements.
Calls meter.bestTimeSignature(self)

Note: this does not yet accommodate triplets.


We create a simple stream that should be in 3/4


>>> s = converter.parse('C4 D4 E8 F8', format='tinyNotation', makeNotation=False)
>>> m = stream.Measure()
>>> for el in s:
...     m.insert(el.offset, el)

But there is no TimeSignature!

>>> m.show('text')
{0.0} <music21.note.Note C>
{1.0} <music21.note.Note D>
{2.0} <music21.note.Note E>
{2.5} <music21.note.Note F>

So, we get the best Time Signature and put it in the Stream.

>>> ts = m.bestTimeSignature()
>>> ts
<music21.meter.TimeSignature 3/4>
>>> m.timeSignature = ts
>>> m.show('text')
{0.0} <music21.meter.TimeSignature 3/4>
{0.0} <music21.note.Note C>
{1.0} <music21.note.Note D>
{2.0} <music21.note.Note E>
{2.5} <music21.note.Note F>

For further details about complex time signatures, etc.
see `meter.bestTimeSignature()`

)r$   r  r   s    rJ   r  Measure.bestTimeSignaturet4  s    T &&t,,rI   c                    / nU R                    HL  n[        U[        R                  5      (       d  M$  U R	                  U5      S:X  d  M;  UR                  U5          O   U(       d  g US   $ )Nrl   r   )r   r   r   r  r  rU   r   barListro   s      rJ   _getLeftBarlineMeasure._getLeftBarline4  s\     A!S[[))%%a(C/NN1%	  
 1:rI   c                "   Sn[        U[        5      (       a  [        R                  " U5      nSUl        OUc  SnOSUl        U R                  5       nUb   U R                  U R                  U5      5      nU(       a  U R                  SU5        g g )NTleftFr   )	r   r   r   r  locationr:  r$  r%  rV   )r   
barlineObjrV   oldLeftBarliner(  s        rJ   _setLeftBarlineMeasure._setLeftBarline4  s    j#&&Z0J"(JF"(J--/%88DJJ~67D KK:& rI   a>  
        Get or set the left barline, or the Barline object
        found at offset zero of the Measure.  Can be set either with a string
        representing barline style or a bar.Barline() object or None.
        Note that not all bars have
        barline objects here -- regular barlines don't need them.
        r3  c                    / nU R                    H5  n[        U[        R                  5      (       d  M$  UR	                  U5          O   U(       d  g US   $ r/  )r   r   r   r  rU   r8  s      rJ   _getRightBarlineMeasure._getRightBarline4  sK     ""A!S[[))q! #
 1:rI   c                   Sn[        U[        5      (       a  [        R                  " U5      nSUl        OUc  SnOSUl        Ub6  [        U[        R
                  5      (       a  UR                  S;   a  SUl        U R                  5       nUb   U R                  U R                  U5      5      nU(       a  U R                  U5        g g )NTrightF)r   NrW  )r   r   r   r  r>  Repeat	directionrD  r$  r%  rW   )r   r?  rV   oldRightBarliner(  s        rJ   _setRightBarlineMeasure._setRightBarline4  s    j#&&Z0J")JF")J !jSZZ&H&H##6',
$//1&88DJJ78DOOJ' rI   a  
        Get or set the right barline, or the Barline object
        found at the offset equal to the bar duration.

        >>> b = bar.Barline('final')
        >>> m = stream.Measure()
        >>> print(m.rightBarline)
        None
        >>> m.rightBarline = b
        >>> m.rightBarline.type
        'final'


        A string can also be used instead:

        >>> c = converter.parse('tinynotation: 3/8 C8 D E F G A B4.')
        >>> c.measure(1).rightBarline = 'light-light'
        >>> c.measure(3).rightBarline = 'light-heavy'
        >>> #_DOCS_SHOW c.show()

        .. image:: images/stream_barline_demo.*
            :width: 211

        OMIT_FROM_DOCS

        .measure currently isn't the same as the
        original measure.

        )
r	  r  r
  r  r  r  r  r  r  r  )r  r  r  rs   r  )rD   rE   rF   rG   r  r2   r  rT   r0  r  rg   r  rz   r  r   rP  r-   r(  r1  r  r.  r  r:  rA  leftBarlinerD  rK  r)  rH   r  r  s   @rJ   rw   rw   2  s    "F "00MI J7 ^`?+J HI;&!I~ &P 12 #  # Z $DI+:^ #FP"DHgX 4 4t*-X'& ?* K(6 ,,!LrI   rw   c                     ^  \ rS rSrSr\R                  rU 4S jrS r	S r
\" \	\
SS9rS rS	 r\" \\S
S9rSSSSSSSS.S jrU 4S jrSrU =r$ )rl  i5  a?  
A Stream subclass for designating music that is considered a single part.

When put into a Score object, Part objects are all collected in the `Score.parts`
call.  Otherwise, they mostly work like generic Streams.

Generally the hierarchy goes: Score > Part > Measure > Voice, but you are not
required to stick to this.

Part groupings (piano braces, etc.) are found in the :ref:`moduleLayout` module
in the :class:`~music21.layout.StaffGroup` Spanner object.

OMIT_FROM_DOCS
Check that this is True and works for everything before suggesting that it works!

May be enclosed in a staff (for instance, 2nd and 3rd trombone
on a single staff), may enclose staves (piano treble and piano bass),
or may not enclose or be enclosed by a staff (in which case, it
assumes that this part fits on one staff and shares it with no other
part
c                B   > [         TU ]  " U0 UD6  S U l        S U l        g rs   )ry   rz   	_partName_partAbbreviation)r   r  r   r   s      rJ   rz   Part.__init__05  s$    $+(+!%rI   c                
   U R                   b  U R                   $ SU R                  ;   a  U R                  S   $ S nU [        R                      H#  nUR                  nUc  UR
                  nUc  M#    O   XR                  S'   U$ )NrP  )rP  r   r    r  r~  instrumentNamer   pnro   s      rJ   _getPartNamePart._getPartName55  s    >>%>>!DKK';;{++B*//0ZZ:))B> 1 (*KK$IrI   c                    Xl         g rs   )rP  r   newNames     rJ   _setPartNamePart._setPartNameE5  s     rI   a  
        Gets or sets a string representing the name of this part
        as a whole (not counting instrument changes, etc.).

        It can be set explicitly (or set on parsing) or it
        can take its name from the first :class:`~music21.instrument.Instrument` object
        encountered in the stream (or within a substream),
        first checking its .partName, then checking its .instrumentName

        Can also return None.

        >>> p = stream.Part()
        >>> p.partName is None
        True
        >>> cl = instrument.Clarinet()
        >>> p.insert(0, cl)
        >>> p.partName
        'Clarinet'
        >>> p.remove(cl)
        >>> p.partName is None
        True
        >>> p.insert(0, instrument.Flute())
        >>> p.partName
        'Flute'
        >>> p.partName = 'Reed 1'
        >>> p.partName
        'Reed 1'

        Note that changing an instrument's .partName or .instrumentName while it
        is already in the Stream will not automatically update this unless
        .coreElementsChanged() is called or this Stream's elements are otherwise altered.
        This is because the value is cached so that O(n) searches through the Stream
        do not need to be done every time.
    r3  c                
   U R                   b  U R                   $ SU R                  ;   a  U R                  S   $ S nU [        R                      H#  nUR                  nUc  UR
                  nUc  M#    O   XR                  S'   U$ )NrQ  )rQ  r   r    r  partAbbreviationinstrumentAbbreviationrU  s      rJ   _getPartAbbreviationPart._getPartAbbreviationk5  s    !!-))) DKK/;;233B*//0'':11B> 1 02KK+,IrI   c                    Xl         g rs   )rQ  rZ  s     rJ   _setPartAbbreviationPart._setPartAbbreviation{5  s    !(rI   a  
        Gets or sets a string representing the abbreviated name of this part
        as a whole (not counting instrument changes, etc.).

        It can be set explicitly (or set on parsing) or it
        can take its name from the first :class:`~music21.instrument.Instrument` object
        encountered in the stream (or within a substream),
        first checking its .partAbbreviation, then checking its .instrumentAbbreviation

        Can also return None.

        >>> p = stream.Part()
        >>> p.partAbbreviation is None
        True
        >>> cl = instrument.Clarinet()
        >>> p.insert(0, cl)
        >>> p.partAbbreviation
        'Cl'
        >>> p.remove(cl)
        >>> p.partAbbreviation is None
        True
        >>> p.insert(0, instrument.Flute())
        >>> p.partAbbreviation
        'Fl'
        >>> p.partAbbreviation = 'Rd 1'
        >>> p.partAbbreviation
        'Rd 1'

        Note that changing an instrument's .partAbbreviation or .instrumentAbbreviation while it
        is already in the Stream will not automatically update this unless
        .coreElementsChanged() is called or this Stream's elements are otherwise altered.
        This is because the value is cached so that O(n) searches through the Stream
        do not need to be done every time.
    NTF)r  r  r  rF  r  r  r  c          
         U(       d  U R                  S5      nOU nUR                  [        5      n	[        R                  " U	UUUUUUS9  U(       d  U$ g)a  
This overridden method of Stream.makeAccidentals
walks measures to arrive at desired values for keyword arguments
`tiePitchSet` and `pitchPastMeasure` when calling `makeAccidentals()`
on each Measure.

1. Ties across barlines are detected so that accidentals are not
unnecessarily reiterated. (`tiePitchSet`)

2. Pitches appearing on the same step in an immediately preceding measure,
if foreign to the key signature of that previous measure,
are printed with cautionary accidentals in the subsequent measure.
(`pitchPastMeasure`)

Most of the logic has been factored out to
:meth:`~music21.stream.makeNotation.makeAccidentalsInMeasureStream`,
which is called after managing the `inPlace` keyword and finding
measures to iterate.

* Changed in v7: `inPlace` defaults False
r  )r  r  r  r  r  r  N)rL  r   rw   r-   r-  )
r   r  r  r  rF  r  r  r  rQ  measureStreams
             rJ   r  Part.makeAccidentals5  s_    @ 112CDII!44W=33)!5'))E#	
 rI   c           	        > [         TU ]  U5        S H*  n[        X5      (       d  M  [        X[	        X5      5        M,     g)z
Merge relevant attributes from the Other part
into this one. Key attributes of difference: partName and partAbbreviation.

TODO: doc test
)rP  rQ  Nr  rW  s      rJ   rP  Part.mergeAttributes5  s6     	&6Du##GE$89 7rI   )rQ  rP  )rD   rE   rF   rG   r  r2   FLATTENrT   rz   rW  r\  r  r~  ra  rd  r_  r  rP  rH   r  r  s   @rJ   rl  rl  5  s    * "))M
&
 ! l !9 !	HF )   46J !Q !	L !%)2h: :rI   rl  c                      \ rS rSrSrSrg)	PartStaffi5  z
A Part subclass for designating music that is
represented on a single staff but may only be one
of many staves for a single part.
rC   NrD   rE   rF   rG   r  rH   rC   rI   rJ   rm  rm  5  s    rI   rm  c                       \ rS rSrSrSrSrSrg)Systemi5  aO  
Totally optional and used only in OMR and Capella: a designation that all the
music in this Stream belongs in a single system.

The system object has two attributes, systemNumber (which number is it)
and systemNumbering which says at what point the numbering of
systems resets.  It can be either "Score" (default), "Opus", or "Page".
r   rx   rC   N)rD   rE   rF   rG   r  systemNumbersystemNumberingrH   rC   rI   rJ   rp  rp  5  s     LOrI   rp  c                    ^  \ rS rSrSr\R                  r\SS j5       r	S\
R                  S4S jr\R                  \R                   \R$                  \R(                  4\
R                  S4S jrSSS	 jjr S   SS
 jjr\SS.       SS jj5       r\SSS.       SS jj5       rSSS.       SS jjr   S SS jjrS r    SU 4S jjrSrU =r$ )rx   i
6  a  
A Stream subclass for handling music with more than one Part.

Almost totally optional (the largest containing Stream in a piece could be
a generic Stream, or a Part, or a Staff).  And Scores can be
embedded in other Scores (in fact, our original thought was to call
this class a Fragment because of this possibility of continuous
embedding; though it's probably better to embed a Score in an Opus),
but we figure that many people will like calling the largest
container a Score and that this will become a standard.
c                >    U R                  [        5      nSUl        U$ )aQ  
Return all :class:`~music21.stream.Part` objects in a :class:`~music21.stream.Score`.

It filters out all other things that might be in a Score object, such as Metadata
returning just the Parts.

>>> s = corpus.parse('bach/bwv66.6')
>>> s.parts
<music21.stream.iterator.StreamIterator for Score:bach/bwv66.6.mxl @:0>
>>> len(s.parts)
4
rN  )r   rl  r  )r   partIterators     rJ   rN  Score.parts6  s"     7;6M6Md6S*1'rI   r  Fc           	     v   U R                  5       nUR                  U 5        U R                   H(  nUR                  UUUUUS9nUR	                  SU5        M*     U(       a'  U R
                  n	U	 H  n
UR	                  SU
5        M     XfR                  l        XR                  l        SUR                  l	        U$ )a  
This method overrides the :meth:`~music21.stream.Stream.measures`
method on Stream. This creates a new Score stream that has the same measure
range for all Parts.

The `collect` argument is a list of classes that will be collected; see
Stream.measures()

>>> s = corpus.parse('bwv66.6')
>>> post = s.measures(3, 5)  # range is inclusive, i.e., [3, 5]
>>> len(post.parts)
4
>>> len(post.parts[0].getElementsByClass(stream.Measure))
3
>>> len(post.parts[1].getElementsByClass(stream.Measure))
3
r  r   r  )
r   rP  rN  r  rV   r:  r   rM  rN  rO  )r   r  r  r  r  r  r  r-  measuredPartspStreamr:  s              rJ   r  Score.measures*6  s    0 ~~T"A::k&/.55C8I	 & KL
 KK<(  }}HAr"  "&!%!+rI   c           	        US:  a  SnUnUnU(       a  US-  nUS:X  a  SnU R                  5       nUR                  U 5        U R                  [        5       H(  nUR	                  UUUUUS9n	UR                  SU	5        M*     U(       a'  U R                  n
U
 H  nUR                  SU5        M     XwR                  l        XR                  l	        SUR                  l
        U$ )a	  
Given a measure number (or measure index, if indicesNotNumbers is True)
return another Score object which contains multiple parts but each of which has only a
single :class:`~music21.stream.Measure` object if the
Measure number exists, otherwise returns a score with parts that are empty.

This method overrides the :meth:`~music21.stream.Stream.measure` method on Stream to
allow for finding a single "measure slice" within parts:

>>> bachIn = corpus.parse('bach/bwv324.xml')
>>> excerpt = bachIn.measure(2)
>>> excerpt
<music21.stream.Score bach/bwv324.mxl>
>>> len(excerpt.parts)
4
>>> excerpt.parts[0].show('text')
{0.0} <music21.instrument.Instrument 'P1: Soprano: '>
{0.0} <music21.clef.TrebleClef>
{0.0} <music21.key.Key of e minor>
{0.0} <music21.meter.TimeSignature 4/4>
{0.0} <music21.stream.Measure 2 offset=0.0>
    {0.0} <music21.note.Note B>
    {1.0} <music21.note.Note B>
    {2.0} <music21.note.Note B>
    {3.0} <music21.note.Note B>

Note that the parts created have all the meta-information outside the measure
unless this information appears in the measure itself at the beginning:

>>> bachIn.measure(1).parts[0].show('text')
{0.0} <music21.instrument.Instrument 'P1: Soprano: '>
{0.0} <music21.stream.Measure 1 offset=0.0>
    {0.0} <music21.clef.TrebleClef>
    {0.0} <music21.key.Key of e minor>
    {0.0} <music21.meter.TimeSignature 4/4>
    {0.0} <music21.layout.SystemLayout>
    {0.0} <music21.note.Note B>
    {2.0} <music21.note.Note D>

This way the original measure objects can be returned without being altered.

The final measure slice of the piece can be obtained with index -1.  Example:
quickly get the last chord of the piece, without needing to run .chordify()
on the whole piece:

>>> excerpt = bachIn.measure(-1)
>>> excerptChords = excerpt.chordify()
>>> excerptChords.show('text')
{0.0} <music21.instrument.Instrument 'P1: Soprano: '>
{0.0} <music21.clef.TrebleClef>
{0.0} <music21.key.Key of e minor>
{0.0} <music21.meter.TimeSignature 4/4>
{0.0} <music21.stream.Measure 9 offset=0.0>
    {0.0} <music21.chord.Chord E2 G3 B3 E4>
    {4.0} <music21.bar.Barline type=final>

>>> lastChord = excerptChords[chord.Chord].last()
>>> lastChord
<music21.chord.Chord E2 G3 B3 E4>

Note that we still do a .getElementsByClass(chord.Chord) since many pieces end
with nothing but a rest.
r   Trm  r   Nr  r  )r   rP  r   rl  r  rV   r:  r   rM  rN  rO  )r   r  r  r  r  r  r  r  r-  mStreamry  r:  s               rJ   r  Score.measure[6  s    H 1 $*(!!R'#' ~~T"((.Ajj!3!1)00>3D	 ! FG
 KK7# / }}HAr"  "&!%!*rI   Tc                   U R                  SS9nUR                  U 5        U R                  5       R                  [        5       H:  n[
        R                  " U5      nUR                  U R                  U5      U5        M<     U R                  [        5       H"  nUR                  SUR                  SS95        M$     UR                  nUR                  SS9 Hl  nUR                  R                  5       (       d  M$  UR                  R                   nUc  M?  UR                  R"                  S:X  d  M[  UR%                  Xs5        Mn     U$ )a  
Expand all repeats, as well as all repeat indications
given by text expressions such as D.C. al Segno.

This method always returns a new Stream, with deepcopies
of all contained elements at all level.

Note that copySpanners is ignored here, as they are always copied.
rf  r  r   F)rc  r  r  )r  rP  r   r  rl  r  r  rV   r  r   rf  r  rY   r)   r  r   rN  rO  r  )r   rc  r  ro   re  r-  r  rN  s           rJ   rf  Score.expandRepeats6  s    @T" 2248A==#DKK**1-t4 9 ((.AKK1???>? /
 ** %0Aww%%'',,%!,,*=*=*O!77B 1 rI   c                b   U R                  5       R                  nU(       d  [        R                  X5      $ 0 nU HN  nUR                  U5      nU H4  nXc;  a  / X6'   XV    H   nXsU   ;  d  M  X6   R	                  U5        M"     M6     MP     [        [        UR                  5       S S95      nU$ )a  
This Score method overrides the
:meth:`~music21.stream.Stream.measureOffsetMap` method of Stream.
This creates a map based on all contained Parts in this Score.
Measures found in multiple Parts with the same offset will be
appended to the same list.

If no parts are found in the score, then the normal
:meth:`~music21.stream.Stream.measureOffsetMap` routine is called.

This method is smart and does not assume that all Parts
have measures with identical offsets.
c                    U S   $ r/  rC   r  s    rJ   r  (Score.measureOffsetMap.<locals>.<lambda>7  r   rI   r!   )r   rN  rQ   r%  rU   r   r  r!  )	r   rh  rN  r"  r-  
mapPartialr   r  r$  s	            rJ   r%  Score.measureOffsetMap6  s    " 		!!**4AA9;	A++O<J%#%IL#A!,!++A. '    'vioo.?^'TUrI   rk  c                   g rs   rC   r   rC  rF  s      rJ   rv  Score.sliceByGreatestDivisor7       	rI   rt  c                   g rs   rC   r  s      rJ   rv  r  7  r  rI   c          	     <   U(       d  U R                  S5      nOU nUR                  R                  5       nUc  [        S5      eUR	                  [
        5      n[        U5      nUS:X  a  Sn[        U5       GH  n/ nUR	                  [        5       Hs  n	U	R                  5       (       a  U	R	                  [
        5      U   n
OU	n
U
R                   H0  nUR                  U;  d  M  UR                  UR                  5        M2     Mu     [        R                  " U5      nUR	                  [        5       HF  n	U	R                  5       (       a  U	R	                  [
        5      U   n
OU	n
U
R                  U/SUSS9  MH     GM
     AUR!                  5         U(       d  U$ g)z
Slice all duration of all part by the minimum duration
that can be summed to each concurrent duration.

Overrides method defined on Stream.
rv  Nz+Cannot sliceByGreatestDivisor without partsr   rm  Trw  )rL  rN  r   r   r   rw   r   r  rl  r,  r  r  rU   r   rx  rj  r   )r   rC  rF  rQ  	firstPartr|  mCountr  ry  r-  m_or_pro   r?   s                rJ   rv  r  7  s{    112JKI I OO))+	IJJ..w7WQ;F vA#% 11$7==??11':1=FF  --A.BB,33AOOD . 8 ++,@AG 11$7 ==??11':1=FF,,y485<59 - ; 8) > %%' rI   c           	     4   SSK Jn  / n/ n[        U[        5      (       a  Un[	        U R
                  5       HR  u  pX-  S:X  a  / nUR                  U	5        OUR                  U	5        X-  US-
  :X  d  M?  UR                  U5        / nMT     U(       a  UR                  U5        O[        R                  " U5      (       a  [        R                  " [        [        [        -     U5      nU Hq  n
/ n[        U
[        5      (       d  UR                  U R
                  U
   5        O'U
 H!  nUR                  U R
                  U   5        M#     UR                  U5        Ms     O[        SU 35      eU R                  SS9nU R                  Ul        U GH  n[        U5      S:X  a  U(       d  UR!                  SUS   5        M1  [#        5       n[	        U5       GH  u  pUR%                  5       (       a  SnOSn[	        U	R'                  [(        5      5       GH+  u  nnU(       d,  [)        5       nUR+                  U5        UR-                  US	S
9  OUR'                  [(        5      U   n[/        5       nUUl        UR'                  [2        R4                  UR6                  /5       H\  nU(       a1  [        U[2        R8                  5      (       a  US-  S:X  a  SOSUl        UR!                  UR=                  U5      U5        M^     UR!                  SU5        U(       a  GM
  UR!                  UR=                  U	5      U5        GM.     U	R>                   H$  nUR!                  UR=                  U	5      U5        M&     GM     UR!                  SU5        SnGM     U$ )aW  
Given a multi-part :class:`~music21.stream.Score`,
return a new Score that combines parts into voices.

The `voiceAllocation` parameter sets the maximum number
of voices per Part.

The `permitOneVoicePerPart` parameter, if True, will encode a
single voice inside a single Part, rather than leaving it as
a single Part alone, with no internal voices.


>>> s = corpus.parse('bwv66.6')
>>> len(s.flatten().notes)
165
>>> post = s.partsToVoices(voiceAllocation=4)
>>> len(post.parts)
1
>>> len(post.parts.first().getElementsByClass(stream.Measure).first().voices)
4
>>> len(post.flatten().notes)
165

r   r4   rm  z"incorrect voiceAllocation format: partsToVoicesr  TFr>  r?  r  updownN) r?  r5   r   r   r  rN  rU   r   r   r   r   r   ro  r  r#   r   rV   rl  r,  r   rw   rP  rj  r,  r   r%   r  r(  r  r  r  r:  )r   voiceAllocationpermitOneVoicePerPartsetStemsr5   subbundlevoicesPerPartpIndexr-  r  partIdr  pActiver,  mIndexr  rA  r  ro   r:  s                        rJ   r  Score.partsToVoicesZ7  s   : 	$os+++M&tzz2	)Q.CJJqMJJqM)]Q->>MM#&C 3 c"//ffT$s(^_EO(!%..JJtzz%01"'

4::f#56 #(c" ) "$FFW"XYY OO_O=]]
 C3x1}%:CF#fG&s^	&&(("&K"'K!*1+?+?+H!IIFA '"))  //2--a BP- Q #*"<"<W"Ef"M A!AD1143C3CW__2UV#
1dii(@(@6<qjAod6AO!2!21!5q9 W NN1a( ';q'8'8';WEK "JP **BNN2#5#5a#8"= %_ ,d HHQ Gw x rI   c                *    SnSnU R                  UUS9$ )z
Reduce a polyphonic work into two staves.

Currently, this is just a synonym for `partsToVoices` with
`voiceAllocation = 2`, and `permitOneVoicePerPart = False`,
but someday this will have better methods for finding identical
parts, etc.
r  F)r  r  )r  )r   r  r  s      rJ   implodeScore.implode7  s-      %!!+"7 " 
 	
rI   c           	     J  > U(       a  U nOU R                  S5      nUR                  5         U R                  5       (       a@  UR                  S5       H  nUR                  " SUUSUS.UD6  M     UR                  5         O[        [        U]  " SUUSUS.UD6  U(       a  gU$ )a@  
This method overrides the makeNotation method on Stream,
such that a Score object with one or more Parts or Streams
that may not contain well-formed notation may be transformed
and replaced by well-formed notation.

If `inPlace` is True, this is done in-place;
if `inPlace` is False, this returns a modified deep copy.
r-   rQ   Tr(  NrC   )rL  r  r*  r   r-   r   ry   rx   )	r   r  r  rF  r  r   r  r  r   s	           rJ   r-   Score.makeNotation7  s    $ L44^DL..0 ""$$!44X> 5;4H'+(05 "4	5 ? ,,.%3 JI]<@=EJ 7I	J rI   rC   )rq  ziterator.StreamIterator[Part]r  )r   rx   rc  r  rq  rx   r  r  )r   rx   rC  r  rF  r  rq  r  )r   rx   rC  r  rF  r  rq  rx   )r   rx   rC  r  rF  r  rq  zScore | None)r  FT)r  zint | list[list | int])NNFF)rD   rE   rF   rG   r  r2   ELEMENTS_ONLYrT   r  rN  r   r  r  r   r  r$   r*  r    r  r"   r4  r  rf  r%  r   rv  r  r  r-   rH   r  r  s   @rJ   rx   rx   
6  s   
 "//M ( Q . 2 2#(.f E$7$79N9NPSP`P`a-11"'	dL!J LX  H   
4  F    !	
 
   $)	  "	
 
  	?? ? 	?
 
?D =>,1#@'9@D
& !4  4 rI   rx   c                  l    \ rS rSrSr\R                  rS rS r	S r
\S 5       rS rSS	 jrSS
 jrSrg)rf  i$8  z
A Stream subclass for handling multi-work music encodings.
Many ABC files, for example, define multiple works or parts within a single file.

Opus objects can contain multiple Score objects, or even other Opus objects!
c                    / nU R                  [        5       H(  nUR                  UR                  R                  5        M*     U$ )z
Return a list of all numbers defined in this Opus.

>>> o = corpus.parse('josquin/oVenusBant')
>>> o.getNumbers()
['1', '2', '3']
)r   rx   rU   r#   r  )r   r  r  s      rJ   
getNumbersOpus.getNumbers/8  s9     ((/AKK

))* 0rI   c                    U R                  [        5       H,  nUR                  R                  US5      u  p4U(       d  M*  Us  $    g)aX  
Get Score objects from this Stream by number.
Performs title search using the
:meth:`~music21.metadata.Metadata.search` method,
and returns the first result.

>>> o = corpus.parse('josquin/oVenusBant')
>>> o.getNumbers()
['1', '2', '3']
>>> s = o.getScoreByNumber(2)
>>> s.metadata.title
'O Venus bant'
>>> s.metadata.alternativeTitle
'Tenor'
r  Nr   rx   r#   search)r   	opusMatchr  r=   unused_fields        rJ   getScoreByNumberOpus.getScoreByNumber<8  s<    " ((/A"#**"3"3Ix"HEu 0rI   c                    U R                  [        5       H,  nUR                  R                  US5      u  p4U(       d  M*  Us  $    g)a  
Get Score objects from this Stream by a title.
Performs title search using the :meth:`~music21.metadata.Metadata.search` method,
and returns the first result.

>>> o = corpus.parse('essenFolksong/erk5')
>>> s = o.getScoreByTitle('Vrienden, kommt alle gaere')
>>> s.metadata.title
'Vrienden, kommt alle gaere'

Regular expressions work fine

>>> s = o.getScoreByTitle('(.*)kommt(.*)')
>>> s.metadata.title
'Vrienden, kommt alle gaere'
titleNr  )r   
titleMatchr  r=   r  s        rJ   getScoreByTitleOpus.getScoreByTitleS8  s<    $ ((/A"#**"3"3J"HEu 0rI   c                $    U R                  S5      $ )zB
Return all :class:`~music21.stream.Score` objects
in an iterator
rx   r6  r   s    rJ   r  Opus.scoresj8  s     &&w//rI   c                   [        5       n[        R                  " 5       nU R                   H  nUR                  R                  5       R                  5       nUR                  SU5        UR                  nUc  MN  UR                  b  UR                  c  UR                  Ul        UR                  c  M  UR                  b  M  UR                  Ul	        M     UR                  SU5        U$ )aI  
Some Opus objects represent numerous scores
that are individual parts of the same work.
This method will treat each contained Score as a Part,
merging and returning a single Score with merged Metadata.

>>> o = corpus.parse('josquin/milleRegrets')
>>> s = o.mergeScores()
>>> s.metadata.title
'Mille regrets'
>>> len(s.parts)
4
r   )
rx   r#   r  r  rN  r   r-   rV   r  composer)r   r  mdNewr  r-  mds         rJ   mergeScoresOpus.mergeScoresr8  s     w!!#A,,.AKK1B~ 88'EKK,?"$((EK;;*u~~/E%'[[EN  	AurI   Nc                J   U R                   (       d  gUb  SU;   a  [        R                  " XU40 UD6$ [        R                  " 5       (       a  [        R                  " XU40 UD6$ SnUc>  Uc  S[
        S   -   nO[        R                  " U5      u  pe[
        R                  USS9nSn[        U[        5      (       a  [        R                  " U5      nUR                  nUR                  nSR                  UR                  5      n	/ n
[         R"                  " [         R$                  " ['        U R                   5      5      5      n[)        U R                   5       H  u  p[         R"                  " [         R$                  " US-   5      5      nX-
  nS	U-  nUS
-   U-   [        US-   5      -   U	-   nUU-  nUR                  " SUUS.UD6n[
        R+                  SU SU 35        U
R-                  U5        M     U(       a  [.        R0                  " U5        U
(       a  U
S   $ S$ )a  
Displays an object in a format provided by the `fmt` argument or, if not
provided, the format set in the user's Environment.

This method overrides the behavior specified in
:class:`~music21.base.Music21Object` for all formats besides explicit
lily.x calls.

Individual files are written for each score; returns the last file written.

>>> sc1 = stream.Score()
>>> sc2 = stream.Score()
>>> o = stream.Opus()
>>> o.append([sc1, sc2])

#_DOCS_SHOW >>> o.write()
#_DOCS_SHOW PosixPath('/some/temp/path-2.xml')
NlilyF.writeFormat)r  returnPathlibTr  0r<  rm  r   z
Component z written to r   rC   )r  rQ   r   r   r  r  
findFormatgetTempFiler   r   pathlibPathparentstemrs  suffixesmathceillog10r   r  
printDebugrU   osr  )r   r   r   r   deleter  unused_formatfpParentfpStemfpSuffixr  placesRequiredr  r  placesConsumedzeroesNeededzeroes	scoreNamefpToUse
fpReturneds                       rJ   r   
Opus.write8  s   & {{?v}<<2:::%%''<<2::::{|M::(.(9(9#(>%))u)MBFb#b!B9988BKK(4::c$++.>#?@dkk*DA!YYtzz!a%'89N):L<'Fv-AE
:XEI*GASWAAJ##j<
|$LMKK
# + IIbMtBx)T)rI   c                    Ub  SU;   a  [         R                  " XU40 UD6$ [        R                  " 5       (       a  [         R                  " XU40 UD6$ U R                   H  nUR                  " SXS.UD6  M     g)z
Show an Opus file.

This method overrides the behavior specified in
:class:`~music21.base.Music21Object` for all
formats besides explicit lily.x calls. or when running under Jupyter notebook.
Nr  r   rC   )rQ   r   r   r  r  )r   r   r   r   r  s        rJ   r   	Opus.show8  sl     ?v};;t#:::%%'';;t#:::[[43484 !rI   rC   rF  )rD   rE   rF   rG   r  r2   r  rT   r  r  r  r  r  r  r   r   rH   rC   rI   rJ   rf  rf  $8  sH     "//M.. 0 0!H;*z5rI   rf  c                  ~   ^  \ rS rSrSrSSS.SU 4S jjjjrS rS rSS	 jrS
SS.         SU 4S jjjr	Sr
U =r$ )SpannerStoragei8  a  
For advanced use. This Stream subclass is only used
inside a Spanner object to provide object storage
of connected elements (things the Spanner spans).

This subclass name can be used to search in an
object's .sites and find any and all
locations that are SpannerStorage objects.

A `client` keyword argument must be provided by the Spanner in creation.

>>> stream.SpannerStorage(client=spanner.Slur())
<music21.stream.SpannerStorage for music21.spanner.Slur>

* Changed in v8: spannerParent is renamed client.
N)rM  c               h   > Uc  SSK Jn  UR                  " 5       nX l        [        TU ]  " U40 UD6  g )Nr   r4   )r?  r5   r(  rM  ry   rz   )r   r   rM  r   r5   r   s        rJ   rz   SpannerStorage.__init__8  s.    >'__&F'-3(3rI   c                d    [        U R                  5      nSUR                   SUR                   3$ )Nzfor r  )r   rM  rE   rF   )r   tcs     rJ   r   SpannerStorage._reprInternal9  s,    $++bmm_Aboo%677rI   c                    g)z(
Never set activeSite to spannerStorage
NrC   r   s     rJ   r   !SpannerStorage.coreSelfActiveSite9  s     	rI   Tc                    [        S5      e)Nz#SpannerStorage cannot store at end.)ro  )r   rL   r  s      rJ   rg  SpannerStorage.coreStoreAtEnd9  s    CDDrI   Fr  c               P   > X ;   a  U R                  U5        g[        TU ]	  XX4S9  g)z
Overrides :meth:`~music21.stream.Stream.replace` in order to check first
whether `replacement` already exists in `self`. If so, delete `target` from
`self` and return; otherwise call the superclass method.

* New in v7.
Nr  )r  ry   r  )r   r  r  rY   r  r   s        rJ   r  SpannerStorage.replace9  s*     KKWTrI   rs   )rM  zspanner.Spanner | Noner  r  )rD   rE   rF   rG   r  rz   r   r   rg  r  rH   r  r  s   @rJ   r  r  8  sp     4T 4 48E !&#'U*U/U 	U
 !U
 -1U UrI   r  c                      \ rS rSrSrSrg)VariantStoragei*9  a  
For advanced use. This Stream subclass is only
used inside a Variant object to provide object
storage of connected elements (things the Variant
defines).

This subclass name can be used to search in an
object's .sites and find any and all
locations that are VariantStorage objects.  It also
ensures that they are pickled properly as Streams on a non-Stream
object.

* Changed in v8: variantParent is removed.  Never used.
rC   Nrn  rC   rI   rJ   r  r  *9  s    rI   r  c                      \ rS rSrSrS rSrg)Testi<9  z3
Note: most Stream tests are found in stream.tests
c                2    SSK Jn  U" U [        5       5        g )Nr   )testCopyAll)music21.test.commonTestr  globals)r   r  s     rJ   testCopyAndDeepcopyTest.testCopyAndDeepcopy@9  s    7D')$rI   rC   N)rD   rE   rF   rG   r  r  rH   rC   rI   rJ   r  r  <9  s    %rI   r  __main__)er  
__future__r   collectionsr   r   r   collections.abcr   r   r	   r  	fractionsr
   ro  r  r   r  r  r  typingr   r   unittestr   r?  r   r   r   music21.common.enumsr   r   music21.common.numberToolsr   music21.common.typesr   r   r   r   r   r   r   r   r   r   r   r   r   r    r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   music21.streamr,   r-   r.   r/   r0   music21.stream.enumsr1   r2   r3   r  r5   Environmentr  ro  r}  TypeVarr7   Lyricr   r8   r9   UserWarningrA   rK   
StreamCoreGenericrQ   r,  rw   rl  rm  rp  rx   rf  r  r  TestCaser  r  rD   mainTestrC   rI   rJ   <module>r     s   # 6 6 : :      	          > -                       ' ' # " Q Q ?? &&x0..'@@ IIcNZZ_T*>%?? "H 
	{ 	 {$RS	PHT__aii
3 PHjPF "D
f D
NK:6 K:Z 6
V 
W F W t}56 }5J>UV >UBV $%8 % gtUD%ni9
 zT rI   