
    3j-                   d   U d dl mZ d dlZd dlZd dlZd dlZd dlmZ d dlZd dl	Z
d dlZd dlmc mZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d d	lmZ d d
lmZ d dlmZ d dlmZ d dlmZmZ d dlmZmZ d dlm Z  d dlm!Z! d dlm"Z" d dlm#Z# d dlm$Z$ d dlm%Z% d dlm&Z& d dlm'Z' d dlm(Z( d dlm)Z) d dlm*Z* d dl+m,Z,m-Z- d dlm.Z. d dlm/Z/ d dlm0Z0 d dlm1Z1 d dlm2Z2 d d lm3Z3 d d!lm4Z4 d d"lm5Z5 d d#lm6Z6 d d$lm7Z7 d d%lm8Z8 d d&l9m:Z: d d'l9m;Z; d d(l<m=Z= d d)l>m?Z?m@Z@ e;j                  ZBe;j                  ZDe
j                  r d d*lmFZF d d+lGmHZH eIeJeKeFj                     f   ZM e!j                  d,      ZOd ZP eKe)j                  j                  e)j                  j                  z   e)j                  j                  z         ZUd-eVd.<   dAd/ZWdBd0ZXdCd1ZYd2 ZZdDdd3d4Z[dEd5Z\ G d6 d7      Z] G d8 d9      Z^ G d: d;e]      Z_ G d< d=e]      Z` G d> d?e=e]      Zaebd@k(  rd dlZ ej                          yy)F    )annotationsN)isclose)articulations)bar)beam)chord)clef)common)defaultsduration)dynamics)OffsetSpecialOrnamentDelay)opFracnearestMultiple)	editorial)environment)exceptions21)expressions)harmony)
instrument)intervalkey)layout)metadata)meter)MIDIPercussionExceptionPercussionMapper)note)
percussion)pitch)repeat)spanner)stream)style)	tablature)tempo)text)tie)
xmlObjects)helpers)SoundTagMixin)MusicXMLImportExceptionMusicXMLWarning)base)OffsetQLzmusicxml.xmlToM21z	list[str]_recognizableKeysc                P    | y | j                         } | j                  dd      }|S )N
 )stripreplace)badStrgoodStrs     F/DATA/.local/lib/python3.12/site-packages/music21/musicxml/xmlToM21.py_cleanr<   Z   s*    ~\\^FnnT3'GN    c                f    | y	 | j                   }|y|j                         S # t        $ r Y yw xY w)ak  
    Returns the `mxObj.text.strip()` from an Element (or None)
    taking into account that `.text` might be None, or the
    Element might be undefined.

    Replacement for the older textStripValid()

    >>> from xml.etree.ElementTree import Element
    >>> e = Element('an-element')
    >>> musicxml.xmlToM21.strippedText(e)
    ''
    >>> e.text = '    '
    >>> musicxml.xmlToM21.strippedText(e)
    ''
    >>> e.text = '  hello  '
    >>> musicxml.xmlToM21.strippedText(e)
    'hello'

    >>> musicxml.xmlToM21.strippedText(None)
    ''
    >>> musicxml.xmlToM21.strippedText(440.0)
    ''

    New in v9.
     )r*   r7   AttributeError)mxObjtxts     r;   strippedTextrC   c   s@    4 }jj;yy{ s   $ $ 	00c                ^    | t         j                  vr| dk(  ry| dk(  ryt        d|        | S )ai  
    Utility function to convert a MusicXML duration type to a music21 duration type.

    Changes 'long' to 'longa' and deals with a Guitar Pro 5.2 bug in MusicXML
    export, that exports a 32nd note with the type '32th'.

    >>> musicxml.xmlToM21.musicXMLTypeToType('long')
    'longa'
    >>> musicxml.xmlToM21.musicXMLTypeToType('32th')
    '32nd'
    >>> musicxml.xmlToM21.musicXMLTypeToType('quarter')
    'quarter'
    >>> musicxml.xmlToM21.musicXMLTypeToType(None)
    Traceback (most recent call last):
    music21.musicxml.xmlObjects.MusicXMLImportException:
        found unknown MusicXML type: None
    longlonga32th32ndzfound unknown MusicXML type: )r   typeToDurationr/   )values    r;   musicXMLTypeToTyperK      s>    & H+++F?f_),I%*QRRr=   c                x    | y	 t        |       }|t        |      k(  rt        |      }|S # t        $ r | cY S w xY w)z
    Convert a string to float or int if possible.

    >>> _f = musicxml.xmlToM21._floatOrIntStr
    >>> _f('20.3')
    20.3
    >>> _f('20.0')
    20
    >>> _f(None) is None
    True
    >>> _f('hi')
    'hi'
    N)floatint
ValueError)strObjvals     r;   _floatOrIntStrrR      sG     ~Fm#c(?c(C
 s   %+ 99	transformc                   |j                  |      }|y|j                  }|dv ry| ||      }|t        j                  |      }t	        | ||       y)aJ  
    If xmlEl has at least one element of tag==tag with some text. If
    it does, set the attribute either with the same name (with "foo-bar" changed to
    "fooBar") or with attributeName to the text contents.

    Pass a function or lambda function as `transform` to transform the value before setting it

    >>> from xml.etree.ElementTree import Element, SubElement

    This is essentially `<accidental><alter>-2</alter></accidental>`:

    >>> e = Element('accidental')
    >>> a = SubElement(e, 'alter')
    >>> a.text = '-2'

    >>> seta = musicxml.xmlToM21._setAttributeFromTagText
    >>> acc = pitch.Accidental()

    Transform the alter text to a float.

    >>> seta(acc, e, 'alter', transform=float)
    >>> acc.alter
    -2.0

    >>> e2 = Element('score-partwise')
    >>> a2 = SubElement(e2, 'movement-title')
    >>> a2.text = 'Trout'
    >>> md = metadata.Metadata()
    >>> seta(md, e2, 'movement-title', 'movementName')
    >>> md.movementName
    'Trout'

    set a different attribute

    >>> seta(md, e2, 'movement-title', 'composer')
    >>> md.composer
    'Trout'
    NNr?   )findr*   r
   hyphenToCamelCasesetattr)m21ElxmlEltagattributeNamerT   matchElrJ   s          r;   _setAttributeFromTagTextr_      s`    N jjoGLLE
% 005E=%(r=   c                r    |j                  |      }|y |j                  }|dv ry | j                  ||       y )NrV   )rW   r*   add)m21mdr[   r\   mdUniqueNamer^   rJ   s         r;   _addMetadataItemFromTagTextrd      s9    jjoGLLE
	IIlE"r=   c                      e Zd ZdZdddddddZdd
Zd Zd Zd Zd Z	d Z
d Zd Zd Zd Zd ZddZddZddZddZddZy	)XMLParserBasezk
    contains functions that could be called
    at multiple levels of parsing (Score, Part, Measure).
    z
half-sharpzone-and-a-half-sharpz	half-flatzone-and-a-half-flatzdouble-flatzdouble-sharp)zquarter-sharpzthree-quarters-sharpzquarter-flatzthree-quarters-flatz	flat-flatzsharp-sharpNc                   t        |t        j                        r|}nd}t        j                  |      s|f}|
d |D        }nt        j                  |      s|f}t        ||      D ]y  \  }}|j                  |      }||t        j                  v rt        j                  |      }	 |dur|durt        j                  |      }||j                  }t        |||       { y# t        t        f$ r Y /w xY w)a  
        Takes an mxObject, a music21Object, and a list/tuple of musicXML names and
        a list/tuple of m21Names, and assigns each of the mxObject's attributes
        that fits this style name to the corresponding style object's m21Name attribute.

        >>> from xml.etree.ElementTree import fromstring as EL
        >>> XP = musicxml.xmlToM21.XMLParserBase()
        >>> mxObj = EL('<a x="20.1" y="10.0" z="yes" />')
        >>> m21Obj = base.Music21Object()
        >>> musicXMLNames = ('w', 'x', 'y', 'z')
        >>> m21Names = ('justify', 'absoluteX', 'absoluteY', 'hideObjectOnPrint')

        >>> XP.setStyleAttributes(mxObj, m21Obj, musicXMLNames, m21Names)

        `.justify` requires a TextStyle object.

        >>> m21Obj.style.justify
        Traceback (most recent call last):
        AttributeError: 'Style' object has no attribute 'justify'

        >>> m21Obj.style.absoluteX
        20.1
        >>> m21Obj.style.absoluteY
        10
        >>> m21Obj.style.hideObjectOnPrint
        True
        Nc              3  F   K   | ]  }t        j                  |        y wN)r
   rX   ).0xs     r;   	<genexpr>z3XMLParserBase.setStyleAttributes.<locals>.<genexpr>9  s     K]003]s   !TF)
isinstancer'   Styler
   
isIterablezipgetr,   STYLE_ATTRIBUTES_YES_NO_TO_BOOLyesNoToBooleannumToIntOrFloatrO   	TypeErrorrY   )	selfmxObject	m21ObjectmusicXMLNamesm21NamesstObjxmlNamem21NamemxValues	            r;   setStyleAttributesz XMLParserBase.setStyleAttributes  s    : i-EE  /*,MK]KH""8, {H #M8 <GWll7+G*DDD$33G<$&7%+?$44W=G
 }!E7G,# != 	* s   *C%%C76C7c                X    d}d}| j                  ||||       | j                  ||       y)a|  
        sets the justification, print-style-align group, and
        text-decoration, text-rotation,
        letter-spacing, line-height, lang, text-direction, and
        enclosure, on an
        m21Object, which must have style.TextStyle as its Style class,
        and then calls setPrintStyleAlign

        conforms to attr-group %text-formatting in the MusicXML DTD
        )justifyztext-decorationztext-rotationzletter-spacingzline-heightlangztext-direction	enclosure)r   textDecorationtextRotationletterSpacing
lineHeightlanguagetextDirectionr   N)r   setPrintStyleAlignrv   rw   rx   ry   rz   s        r;   setTextFormattingzXMLParserBase.setTextFormattingP  s8    OL 	)]HM)4r=   c                z    d}t        |d      r|j                  d      }|||_        | j                  |||       y)z
        Sets four additional elements for line elements, conforms to entity
        %line-shape, %line-type, %dashed-formatting (dash-length and space-length)
        )
line-shape	line-typezdash-lengthzspace-lengthlineTyper   N)hasattrrq   r   r   )rv   rw   rx   ry   
mxLineTypes        r;   setLineStylezXMLParserBase.setLineStylec  sB    
 S9j)!k2J%%/	")]Cr=   c                    |j                  d      dk7  ryt        |d      rd|j                  _        y	 d|_        y# t        $ r Y yw xY w)zY
        convert 'print-object="no"' to m21Object.style.hideObjectOnPrint = True
        print-objectnoNr'   T)rq   r   r'   hideObjectOnPrintr@   rv   rw   rx   s      r;   setPrintObjectzXMLParserBase.setPrintObjectq  sM     <<'4/9g&04IOO-.2	+! s   = 	A	A	c                P    | j                  ||       | j                  ||dd       y)z
        runs setPrintStyle and then sets horizontalAlign and verticalAlign, on an
        m21Object, which must have style.TextStyle as its Style class.

        conforms to attr-group %print-style-align in the MusicXML DTD
        )valignhalign)alignVerticalalignHorizontalN)setPrintStyler   r   s      r;   r   z XMLParserBase.setPrintStyleAlign  s.     	8Y/ ) 4 D	Fr=   c                p    | j                  ||       | j                  ||       | j                  ||       y)z
        get position, font, and color information from the mxObject
        into the m21Object, which must have style.TextStyle as its Style class.

        conforms to attr-group %print-style in the MusicXML DTD
        N)setPositionsetFontsetColorr   s      r;   r   zXMLParserBase.setPrintStyle  s0     	9-Xy)h	*r=   c                ,    | j                  ||dd       y)zE
        Sets m21Object.style.color to be the same as color.
        colorNr   r   s      r;   r   zXMLParserBase.setColor  s     	)WgFr=   c                4    d}d}| j                  ||||       y)a  
        sets font-family, font-style, font-size, and font-weight as
        fontFamily (list), fontStyle, fontSize and fontWeight from
        an object into a TextStyle object

        conforms to attr-group %font in the MusicXML DTD

        >>> from xml.etree.ElementTree import fromstring as EL
        >>> XP = musicxml.xmlToM21.XMLParserBase()
        >>> mxObj = EL('<text font-family="Courier,monospaced" font-style="italic" '
        ...            'font-size="24" font-weight="bold" />')

        >>> te = expressions.TextExpression('hi!')
        >>> XP.setFont(mxObj, te)
        >>> te.style.fontFamily
        ['Courier', 'monospaced']
        >>> te.style.fontStyle
        'italic'
        >>> te.style.fontSize
        24
        >>> te.style.fontWeight
        'bold'
        )zfont-familyz
font-stylez	font-sizezfont-weight)
fontFamily	fontStylefontSize
fontWeightNr   r   s        r;   r   zXMLParserBase.setFont  s#    0 RH)]HMr=   c                4    d}d}| j                  ||||       y)z
        get positioning information for an object from
        default-x, default-y, relative-x, relative-y into
        the .style attribute's absoluteX, relativeX, etc. attributes'

        conforms to attr-group %position in the MusicXML DTD
        )z	default-xz	default-yz
relative-xz
relative-y)	absoluteX	absoluteY	relativeX	relativeYNr   r   s        r;   r   zXMLParserBase.setPosition  s#     OG)]HMr=   c                v    |j                  d      }|yt        |d      r||_        y||j                  _        y)z
        Sets the placement for objects that have a .placement attribute
        (most but not all spanners) and sets the `style.placement` for those
        that don't.
        	placementN)rq   r   r   r'   )rv   rw   rx   r   s       r;   setPlacementzXMLParserBase.setPlacement  s9     LL-	9k*"+I(1IOO%r=   c                   |j                  d      }|j                  d      }||yt        j                         }|*|j                  |_        d|_        | j                  ||       |.|j                  |_        |j                  d      }|dk(  rd|_        |j                  r&|j                  j                  j                  |       y|j                  j                  j                  |       y)ag  
        Set editorial information from an mxObj

        >>> from xml.etree.ElementTree import fromstring as EL
        >>> XP = musicxml.xmlToM21.XMLParserBase()
        >>> mxObj = EL('<a/>')
        >>> n = note.Note('C#4')

        Most common case:

        >>> XP.setEditorial(mxObj, n)
        >>> n.hasEditorialInformation
        False

        >>> mxObj = EL('<note><footnote>Sharp is conjectural</footnote>'
        ...            '<level reference="yes">2</level></note>')
        >>> XP.setEditorial(mxObj, n)
        >>> n.hasEditorialInformation
        True
        >>> len(n.editorial.footnotes)
        1
        >>> fn = n.editorial.footnotes[0]
        >>> fn
        <music21.editorial.Comment 'Sharp is conjectu...'>
        >>> fn.isFootnote
        True
        >>> fn.levelInformation
        '2'
        >>> fn.isReference
        True

        If no <footnote> tag exists, the editorial information will be found in
        comments:

        >>> mxObj = EL('<note><level reference="no">ed</level></note>')
        >>> n = note.Note('C#4')
        >>> XP.setEditorial(mxObj, n)
        >>> len(n.editorial.footnotes)
        0
        >>> len(n.editorial.comments)
        1
        >>> com = n.editorial.comments[0]
        >>> com.isReference
        False
        >>> com.text is None
        True
        >>> com.levelInformation
        'ed'
        footnotelevelNT	referenceyes)rW   r   Commentr*   
isFootnoter   levelInformationrq   isReference	footnotesappendcomments)rv   rA   m21Obj
mxFootnotemxLevelcreferenceAttributes          r;   setEditorialzXMLParserBase.setEditorial  s    f ZZ
+
**W%'/!__AFAL"":q1!(A!([!9!U* $
 <<&&--a0%%,,Q/r=   c                    |t        j                         }n|}t        } |||ddt        j                          |||dt
               |D ]&  }|j                  dk(  s| j                  ||        n ||S y)a;  
        Given an mxPrint object, set object data for
        the print section of a layout.PageLayout object

        >>> from xml.etree.ElementTree import fromstring as EL
        >>> MP = musicxml.xmlToM21.MeasureParser()

        >>> mxPrint = EL(
        ...    '<print new-page="yes" page-number="5">'
        ...    '    <page-layout><page-height>4000</page-height>'
        ...    '        <page-margins><left-margin>20</left-margin>'
        ...    '                 <right-margin>30.25</right-margin></page-margins>'
        ...    '</page-layout></print>'
        ... )

        >>> pl = MP.xmlPrintToPageLayout(mxPrint)
        >>> pl.isNew
        True
        >>> pl.rightMargin
        30.25
        >>> pl.leftMargin
        20
        >>> pl.pageNumber
        5
        >>> pl.pageHeight
        4000
        Nnew-pageisNewrS   page-numberpage-layoutinputM21)r   
PageLayoutsetAttributeFromAttributer,   rs   rN   r\   xmlPageLayoutToPageLayout)rv   mxPrintr   
pageLayoutsetbrk   s         r;   xmlPrintToPageLayoutz"XMLParserBase.xmlPrintToPageLayout'  s    : **,J!J(Z*gAZAZ[Z-3?Auu%..q:.F 
  r=   c                    |t        j                         }n|}t        } |||dt                |||dt               |j	                  d      }|dD ]  } ||||dz   t                ||S y)z
        get a PageLayout object from an mxPageLayout

        Called out from mxPrintToPageLayout because it
        is also used in the <defaults> tag
        Nzpage-heightrS   z
page-widthzpage-marginstopbottomleftright-margin)r   r   r_   rR   rW   )rv   mxPageLayoutr   r   setamxPageMargins	directions          r;   r   z'XMLParserBase.xmlPageLayoutToPageLayoutU  s     **,J!J'Z}OZ|~N %)).9$?	Z	I0E-/ @  r=   c                    |t        j                         }n|}t        } |||ddt        j                         |j                  d      }|| j                  ||       ||S y)a  
        Given an mxPrint object, set object data

        >>> from xml.etree.ElementTree import fromstring as EL
        >>> MP = musicxml.xmlToM21.MeasureParser()

        >>> mxPrint = EL(
        ...    '<print new-system="yes">'
        ...    '    <system-layout><system-distance>55</system-distance>'
        ...    '        <system-margins><left-margin>20</left-margin>'
        ...    '                 <right-margin>30.25</right-margin></system-margins>'
        ...    '</system-layout></print>'
        ... )
        >>> sl = MP.xmlPrintToSystemLayout(mxPrint)
        >>> sl.isNew
        True
        >>> sl.rightMargin
        30.25
        >>> sl.leftMargin
        20
        >>> sl.distance
        55
        N
new-systemr   system-layoutr   )r   SystemLayoutr   r,   rs   rW   xmlSystemLayoutToSystemLayout)rv   r   r   systemLayoutr   mxSystemLayouts         r;   xmlPrintToSystemLayoutz$XMLParserBase.xmlPrintToSystemLayoutp  sr    2 !..0L#L(\7L':;T;TU !o6%..~.U r=   c                    |t        j                         }n|}t        }|j                  d      }|dD ]  } ||||dz   t                 |||ddt                |||dd	t               ||S y)
z
        get a SystemLayout object from an <system-layout> element

        Called out from xmlPrintToSystemLayout because it
        is also used in the <defaults> tag
        Nzsystem-marginsr   r   rS   zsystem-distancedistanceztop-system-distancetopDistance)r   r   r_   rW   rR   )rv   r   r   r   r   mxSystemMarginsr   s          r;   r   z+XMLParserBase.xmlSystemLayoutToSystemLayout  s     !..0L#L' )--.>?&?	\?I	4I-/ @ 	\>+<j%	'\>+@-%	'
  r=   c                (   |t        j                         }n|}t        } |||ddt               |j	                  d      }|t        |      }||_        t        | d      r-t        | d      r!|xs d| j                  f}|| j                  |<   ||S y)	z
        get a StaffLayout object from an <staff-layout> tag

        In music21, the <staff-layout> and <staff-details> are
        intertwined in a StaffLayout object.
        Nzstaff-distancer   rS   numberstaffLayoutObjectsoffsetMeasureNote   )
r   StaffLayoutr_   rR   rq   rN   staffNumberr   r   r   )rv   mxStaffLayoutr   staffLayoutr   r   staffLayoutKeys          r;   xmlStaffLayoutToStaffLayoutz)XMLParserBase.xmlStaffLayoutToStaffLayout  s      ,,.K"K'[-z^	E $''1"k*K&1K#4-.74AT3U*/a$2H2HIN6AD##N3 r=   ri   )__name__
__module____qualname____doc__mxAccidentalNameToM21r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r=   r;   rf   rf     s}     /;5K-84I*7,:;-z5&DF	+GN8
N2M0^,\6( T >r=   rf   c                      e Zd ZdZd Zd Zy)	PartGroupz
    Small helper class for keeping track of part-groups from XML since they
    are converted to StaffGroup spanners much later.
    c                ~    || _         g | _        |j                  d      }|t        |      }|| _        y d}|| _        y )Nr   r   )mxPartGrouppartGroupIdsrq   rN   r   )rv   r   r   s      r;   __init__zPartGroup.__init__  sF    &*[F  Fr=   c                :    | j                   j                  |       y)z8
        Add a partGroupId to self.partGroupIds
        N)r   r   )rv   partGroupIds     r;   ra   zPartGroup.add  s     	  -r=   N)r   r   r   r   r  ra   r   r=   r;   r   r     s    
.r=   r   c                  
    e Zd ZdZ fdZd Zd Zd ZddZd Z	d Z
d	 Zdd
Zd Zd Zd Z	 	 d	 	 	 	 	 ddZ	 	 	 	 	 	 ddZ ej&                  ddd      	 d	 	 	 	 	 dd       Zedd       ZddZ	 d	 	 	 ddZd Z xZS ) MusicXMLImporterzR
    Object for importing .xml, .mxl, .musicxml, MusicXML files into music21.
    c                @   t         |           d | _        d | _        d | _        t        j                         | _        d| _        d| _        | j
                  j                  | _	        i | _
        i | _        g | _        g | _        t        j                  | _        d| _        y )NF)superr  xmlTextxmlFilenamexmlRootr&   ScoredefinesExplicitSystemBreaksdefinesExplicitPageBreaksspannerBundlemxScorePartDictm21PartObjectsByIdpartGroupListpartsr   musicxmlVersionmusicXmlVersionapplyFinaleWorkarounds)rv   	__class__s    r;   r  zMusicXMLImporter.__init__  s    lln+0().&![[66!"$
'77
 ',#r=   c                <    | j                  |       | j                  S )zt
        main program: opens a file given by filename and returns a complete
        music21 Score from it.
        )readFiler&   )rv   filenames     r;   scoreFromFilezMusicXMLImporter.scoreFromFile  s     	h{{r=   c                "   t        j                  |      }|j                         | _        | j                  j                  dk7  r&t        dd| j                  j                   dz         | j                  | j                  | j                         y )Nscore-partwise3Cannot parse MusicXML files not in score-partwise. Root tag was '')ETparsegetrootr
  r\   r/   xmlRootToScorer&   )rv   r  etrees      r;   r  zMusicXMLImporter.readFile  sw    "}}<<//)*_.<T\\=M=M<Na,P+Q R RDLL$++6r=   c                P   t        | j                  t              r | j                  j                  d      | _        t	        j
                  | j                        }	 t        j                  |      }|j                         | _	        | j                  j                  dk7  r&t        dd| j                  j                   dz         | j                  | j                  | j                         y # t        j                  $ r' t        j                  | j                        | _	        Y w xY w)Nzutf-8r  r  r  r  )rm   r  bytesdecodeioStringIOr   r!  r"  r
  
ParseErrorXMLr\   r/   r#  r&   )rv   sior$  s      r;   parseXMLTextzMusicXMLImporter.parseXMLText  s    dllE*<<..w7DLkk$,,'	0 HHSME ==?DL
 <<//)*_.<T\\=M=M<Na,P+Q R RDLL$++6 }} 	066$,,/DL	0s   *C+ +7D%$D%c                   |t        j                         }n|}|j                  d      }||| _        | j	                  |      }|j                  d|       |j                  d      }|#| j                  |      }|j                  d|       |j                  d      D ]%  }| j                  |      }	|j                  d|	       ' | j                  |       |j                  d      D ]  }
|
j                  d      }|&t        | j                  j                               d   }	 | j                  |   }| j#                  |
|      }|a|j                  d
|       || j$                  |<    | j'                          | j(                  j+                  t,        j.                        D ]	  }d|_         g }| j(                  j3                  d      D ]/  }| j                   j                  d|       |j5                  |       1 |D ]  }| j(                  j7                  |        |j9                          | j:                  |_        | j<                  |_        |j>                  D ]$  }
| j:                  |
_        | j<                  |
_        & |jA                          ||S y# t        $ r t        j!                  d| d	z          Y w xY w)z:
        parse an xml file into a Score() object.
        Nversionr   r   creditpartidz$Cannot find info for part with name z, skipping the part        T)!r&   r  rq   r  xmlMetadata
coreInsertrW   xmlDefaultsToScoreLayoutfindallxmlCreditToTextBoxparsePartListlistr  keysKeyErrorenvironLocal
printDebugxmlPartToPartr  
partGroupsr  
getByClassr   ArpeggioMarkSpannercompleteStatusgetByCompleteStatusr   removecoreElementsChangedr  r  r  sort)rv   mxScorer   s	mxVersionmd
mxDefaultsscoreLayoutmxCreditr0  ppartIdmxScorePartr1  sprms                   r;   r#  zMusicXMLImporter.xmlRootToScore/  s    AAKK	*	 #,D g&	Q\\*-
!77
CKLLK(1H,,X6FLLF# 2 	7#(AUU4[F~d22779:1="226: %%a5DS$'26''/! )$ 	 $$//0O0OPB $B Q $$88>BKK""1b)IIbM ? B%%b)  	
(,(H(H%&*&D&D#A,0,L,LA)*.*H*HA'  	
H E  ''*Nvh(W*?)@ As   %J$KKc                t    t        |||       }|j                          |j                  du r|j                  S y)z[
        Given a <part> object and the <score-part> object, parse a complete part.
        )rQ  parentTN)
PartParserr!  appendToScoreAfterParser&   )rv   mxPartrQ  parsers       r;   r?  zMusicXMLImporter.xmlPartToPartu  s6     FDI))T1== r=   c                N   |j                  d      }|yg }|D ]	  }|j                  dk(  r9|j                  d      }|| j                  |<   |D ]  }|j	                  |        L|j                  dk(  s\|j                  d      dk(  r8t        |      }| j                  j                  |       |j                  |       |j                  d      dk(  s|j                  d	      }|t        |      }nd
}g }|D ]#  }|j                  |k7  s|j                  |       % |} y)z
        Parses the <part-list> tag and adds
        <score-part> entries into self.mxScorePartDict[partId]
        and adds them to any open <part-group> entries,
        stored as PartGroup objects in self.partGroupList

        z	part-listNz
score-partr2  z
part-grouptypestartstopr   r   )
rW   r\   rq   r  ra   r   r  r   rN   r   )	rv   rH  
mxPartListopenPartGroupspartListElementrP  pgr   opgTemps	            r;   r9  zMusicXMLImporter.parsePartList  s    \\+.
)O""l2(,,T2/>$$V,(BFF6N ) $$4"&&v.'9"?3B&&--b1"))"-$((0F:,00:F)!$V!" G,99.#NN2. - &-N+  *r=   c                   t        j                         }d|j                  _        d|j                  _        |j                  d      }|d}nt        |      }||_        g }|j                  d      D ],  }|j                   dvs|j                  |j                          . |s	d|_
        |S dj                  |      |_
        |j                  d      }| j                  ||       |j                  d      |j                  _        |S )	a  
        Convert a MusicXML credit to a music21 TextBox

        >>> from xml.etree.ElementTree import fromstring as EL
        >>> credit = EL('<credit page="2"><credit-words>Testing</credit-words></credit>')

        >>> MI = musicxml.xmlToM21.MusicXMLImporter()
        >>> tb = MI.xmlCreditToTextBox(credit)
        >>> tb.page
        2
        >>> tb.content
        'Testing'

        OMIT_FROM_DOCS

        Capella generates empty credit-words.  Make sure they do not crash.

        >>> credit = EL('<credit><credit-words/></credit>')
        >>> tb = MI.xmlCreditToTextBox(credit)
        >>> tb
        <music21.text.TextBox ''>
        Npager   zcredit-wordsrV   r?   r5   r   )r*   TextBoxr'   r   r   rq   rN   rd  r7  r   contentjoinrW   r   r   )rv   rN  tbpageNumrf  cwcw1s          r;   r8  z#MusicXMLImporter.xmlCreditToTextBox  s    0 \\^ $( !%,,v&?G'lG"">2Bwwj(rww' 3 BJIYYw'
mmN+R(779- 	r=   c                   |t        j                         }n|}t        }|j                  d      }|" |||ddt                |||ddt               |j                  d      }|| j                  |      |_        |j                  d      }|| j                  |      |_        |j                  d	      D ].  }| j                  |      }	|j                  j                  |	       0 | j                  |       |S )
zc
        Convert a <defaults> tag to a :class:`~music21.layout.ScoreLayout`
        object
        scalingmillimetersscalingMillimetersrS   tenthsscalingTenthsr   r   staff-layout)r   ScoreLayoutr_   rW   rR   r   r   r   r   r7  r   staffLayoutListr   styleFromXmlDefaults)
rv   rL  r   rM  r   	mxScalingr   r   r   r   s
             r;   r6  z)MusicXMLImporter.xmlDefaultsToScoreLayout  s    
  ,,.K"K'OOI.	 i8L)+i?n] "}5#%)%C%CL%QK"#9%'+'I'I.'YK$'//?M::=IK''..{; @ 	!!*-r=   c                   |j                  d      }|| j                  |       |j                  d      }|At        j                         }| j	                  ||       || j
                  j                  _        |j                  d      }|At        j                         }| j	                  ||       || j
                  j                  _        |j                  d      D ]l  }t        j                         }| j	                  ||       |j                  d      }||f}| j
                  j                  j                  j                  |       n |j                  d      D ]  }	d}
|	j                  d      }|	j                  j                         D ]  \  }}|j                  d	      s|}
 n ||
f}| j
                  j                  j                  j                  |        y)
a  
        Set the appearance and font information from mxDefault
        <appearance>, <music-font>, <word-font>, <lyric-font> (multiple),
        and <lyric-language> tags.

        Here the demo does not include the <appearance> tag since that is
        documented in `xmlAppearanceToStyle`

        >>> from xml.etree.ElementTree import fromstring as EL
        >>> defaults = EL(
        ...    '<defaults>'
        ...    '<music-font font-family="Maestro, Opus" font-weight="bold" />'
        ...    '<word-font font-family="Garamond" font-style="italic" />'
        ...    '<lyric-font name="verse" font-size="12" />'
        ...    '<lyric-font name="chorus" font-size="14" />'
        ...    '<lyric-language name="verse" xml:lang="fr" />'
        ...    '<lyric-language name="chorus" xml:lang="en" />'
        ...    '</defaults>'
        ... )

        >>> MI = musicxml.xmlToM21.MusicXMLImporter()
        >>> MI.styleFromXmlDefaults(defaults)
        >>> st = MI.stream.style
        >>> st.musicFont
        <music21.style.TextStyle object at 0x10535c0f0>
        >>> st.musicFont.fontFamily
        ['Maestro', 'Opus']
        >>> st.musicFont.fontWeight
        'bold'
        >>> st.wordFont.fontFamily
        ['Garamond']
        >>> st.wordFont.fontStyle
        'italic'
        >>> len(st.lyricFonts)
        2
        >>> st.lyricFonts[0]
        ('verse', <music21.style.TextStyle object at 0x10535d438>)
        >>> st.lyricFonts[0][1].fontSize
        12
        >>> st.lyricLanguages
        [('verse', 'fr'), ('chorus', 'en')]
        
appearanceNz
music-fontz	word-fontz
lyric-fontnamezlyric-languageenz}lang)rW   xmlAppearanceToStyler'   	TextStyler   r&   	musicFontwordFontr7  rq   
lyricFontsr   attribitemsendswithlyricLanguages)rv   rL  mxAppearancemxMusicFontst
mxWordFontmxLyricFont	lyricName
styleTuplemxLyricLanguagelyricLanguageaKeyrJ   
lyricTuples                 r;   ru  z%MusicXMLImporter.styleFromXmlDefaults  s   X "|4#%%l3 ool3""BLLb)*,DKK'__[1
!"BLLR()+DKK&%--l;K"BLLb)#/I#RJKK((//
; <  *112BCO M'++F3I.55;;=e==)$)M	  >
 #M1JKK,,33J?  Dr=   c                   |j                  d      D ]e  }|j                  d      }t        j                  |j                        }||f}| j
                  j                  j                  j                  |       g |j                  d      D ]e  }|j                  d      }t        j                  |j                        }||f}	| j
                  j                  j                  j                  |	       g |j                  d      D ]e  }
|
j                  d      }t        j                  |
j                        }||f}| j
                  j                  j                  j                  |       g |j                  d      D ]R  }|j                  d      }|j                  }||f}| j
                  j                  j                  j                  |       T y)a  
        Parse the appearance tag for information about line widths and note sizes

        >>> from xml.etree.ElementTree import fromstring as EL
        >>> appear = EL(
        ...    '<appearance>'
        ...    '<line-width type="beam">5</line-width>'
        ...    '<line-width type="ledger">1.5625</line-width>'
        ...    '<note-size type="grace">60</note-size>'
        ...    '<distance type="hyphen">0.5</distance>'
        ...    '<other-appearance type="sharps">dotted</other-appearance>'
        ...    '</appearance>'
        ... )

        >>> MI = musicxml.xmlToM21.MusicXMLImporter()
        >>> MI.xmlAppearanceToStyle(appear)
        >>> st = MI.stream.style

        >>> st.lineWidths
        [('beam', 5), ('ledger', 1.5625)]

        >>> st.noteSizes
        [('grace', 60)]

        >>> st.distances
        [('hyphen', 0.5)]

        >>> st.otherAppearances
        [('sharps', 'dotted')]
        z
line-widthr[  z	note-sizer   zother-appearanceN)r7  rq   r
   rt   r*   r&   r'   
lineWidthsr   	noteSizes	distancesotherAppearances)rv   r  mxLineWidthlineWidthTypelineWidthValuelineWidthInfo
mxNoteSizenoteSizeTypenoteSizeValuenoteSizeInfo
mxDistancedistanceTypedistanceValuedistanceInfomxOther	otherType
otherValue	otherInfos                     r;   r{  z%MusicXMLImporter.xmlAppearanceToStyleK  s   @ (//=K'OOF3M#33K4D4DEN*N;MKK((//>	 > '..{;J%>>&1L"22:??CM(-8LKK''..|<	 < '..z:J%>>&1L"22:??CM(-8LKK''..|<	 ; $++,>?GF+I J"J/IKK..55i@	 @r=   c           
        t         }| j                  D ]  }t        j                         }|j                  D ]!  }	 |j                  | j                  |          # |j                  } |||dd	        |||d
d       |j                  d      }	|	0 |||dd       | j                  |	|       | j                  |	|       nd|_         |||dd       | j!                  ||       d|_        | j$                  j'                  |        y# t        $ rz}d}t        | j                        D ]7  }|j                  |dz         s|j                  | j                  |          d}9 |st        d| d| j                  d      Y d}~Pd}~ww xY w)zD
        set StaffGroup objects from the <part-group> tags.
        F-StaffTz9Cannot find part in m21PartObjectsById dictionary by Id: z 
   Full Dict:
   r6   Nz
group-namery  zgroup-abbreviationabbreviationzgroup-symbolsymbolbracezgroup-barlinebarTogether)r_   r  r   
StaffGroupr   addSpannedElementsr  r<  sorted
startswithr/   r   rW   r   r   r  r   rC  r  r   )
rv   r   pgObj
staffGrouprP  kefoundOne
partIdTestr   mxGroupSymbols
             r;   r@  zMusicXMLImporter.partGroups  s    (''E**,J,,W11$2I2I&2QR -  ++K[,?[*>O',,^<M(ZnhG  
;mZ8$+
![/=I k:6(,J%%%j1G (   
W$H&,T-D-D&E
%00(1BC&99$:Q:QR\:]^'+H 'F
 $5  "t#89P9P8SSTVW W $
Ws   D	F.F A FFc                d   || j                   }|t        j                         }n|}t        }|j	                  d      }|! |||dd        |||dd        |||dd        |||d	d
        |||dd       |d   |d   k(  rd|d<   |j	                  d      }|| j                  ||       ||S y)z
        Converts part of the root element into a metadata object

        Supported: work-title, work-number, opus, movement-number,
        movement-title, identification
        Nworkz
work-titletitlezwork-numberr   opus
opusNumberzmovement-numbermovementNumberzmovement-titlemovementNameidentification)r
  r   Metadatard   rW   addIdentificationToMetadata)rv   elr   rK  add_mr  r  s          r;   r4  zMusicXMLImporter.xmlMetadata  s     :B""$BB+ wwv"dL'2"dM84"dFL1b"')9:b"&7 g;"^,,BwK!12%,,^R@Ir=   c                   |j                  d      D ]]  }| j                  |      }|j                  |j                        r|j	                  |j                  |       L|j	                  d|       _ |j                  d      D ]&  }| j                  |      }|j	                  d|        n |j                  d      }|| j                  ||       |j                  d      }|q|j                  d      D ]\  }|j                  d	      }	|	|j                  }
|
d
}
| j                  |	      r|j	                  |	|
       K|j                  |	|
       ^ yy)a  
        Given an <identification> tag, , containing <creator> tags, <rights> tags, and
        <miscellaneous> tags etc., add this information to the
        :class:`~music21.metadata.Metadata` object passed in.

        (Replaces identificationToMetadata which is now deprecated.)

        Not supported: source, relation

        Only the first <rights> tag is supported

        Encoding only parses "supports" and that only has
        new-system (definesExplicitSystemBreaks) and
        new-page (definesExplicitPageBreaks)
        creatorotherContributorrights	copyrightencodingNmiscellaneouszmiscellaneous-fieldry  r?   )r7  creatorToContributorisContributorUniqueNamerolera   rightsToCopyrightrW   processEncodingrq   r*   isRecognizableMetadataKey	addCustom)rv   r  rK  r  r   r  r  r  mxMiscFieldmiscFieldNamemiscFieldValues              r;   r  z,MusicXMLImporter.addIdentificationToMetadata  sK   ( &--i8G))'2A))!&&1qvvq! )1- 9 %,,X6F&&v.AFF;" 7
 "&&z2  2. '++O<$,445JK + 7 (!,!1!1!)%'N11-@FF=.9 LL?  L %r=   zuse addIdentificationToMetadatav10v11c                b    ||}nt        j                         }| j                  ||       ||S y)zu
        Deprecated -- use addIdentificationToMetadata instead and always
        pass in a metadata object.
        N)r   r  r  )rv   r  r   rK  s       r;   identificationToMetadataz)MusicXMLImporter.identificationToMetadata  s;     B""$B((<Ir=   c                    | t         v S )a  
        Returns bool on whether `miscFieldName` is a one of the names
        that is among the list of names we might see in <miscellaneous>,
        that this parser will interpret as supported metadata keys.
        Currently, this is all the uniqueName keys (e.g. 'dateCreated'),
        the 'namespace:name' keys (e.g. 'dcterms:created'),
        and the pre-v8 music21 workIds (e.g. 'date').

        >>> MI = musicxml.xmlToM21.MusicXMLImporter()
        >>> MI.isRecognizableMetadataKey('dateCreated')
        True
        >>> MI.isRecognizableMetadataKey('dcterms:created')
        True
        >>> MI.isRecognizableMetadataKey('dateDestroyed')
        False
        )r3   )r  s    r;   r  z*MusicXMLImporter.isRecognizableMetadataKey0  s    $  111r=   c                p   d}d}|j                  d      D ],  }t        |      x}s|sd|v rd}d}|j                  d|       . |rd| _        |j                  d      D ]U  }|j	                  d      }|j	                  d      }	|	|j	                  d	      }	||	fd
k(  rd| _        G||	fdk(  sOd| _        W y)aM  
        Process all information in the <encoding> element and put it into the
        Metadata object passed in as `md`.

        Currently only processes 'software' and these `supports` attributes:

            * new-system = Metadata.definesExplicitSystemBreaks
            * new-page = Metadata.definesExplicitPageBreaks
        FsoftwareFinaleTsupports	attributerJ   Nr[  )r   r   )r   r   )r7  rC   ra   r  rq   r  r  )
rv   r  rK  foundOneSoftwareTagfinaleIsFirstr  softwareTextr  attrrJ   s
             r;   r  z MusicXMLImporter.processEncodingD  s      %*# ((4H+H55|5*</(,&*#z<0 5 *.D' ((4H <<,DLL)E} V, e} 55370"5515.! 5r=   c                    |t        j                         }n|}|j                  d      }|||_        |j                  }||j                         |_        ||S y)a  
        Given a <creator> tag, fill the necessary parameters of a Contributor.

        >>> from xml.etree.ElementTree import fromstring as EL
        >>> creator = EL('<creator type="composer">Beethoven, Ludwig van</creator>')

        >>> MI = musicxml.xmlToM21.MusicXMLImporter()
        >>> c = MI.creatorToContributor(creator)
        >>> c
        <music21.metadata.primitives.Contributor composer:Beethoven, Ludwig van>
        >>> c.role
        'composer'
        >>> c.name
        'Beethoven, Ludwig van'

        Pass in a Contributor object and set it:

        >>> c2 = metadata.Contributor()
        >>> MI.creatorToContributor(creator, c2)
        >>> c2.role
        'composer'
        Nr[  )r   Contributorrq   r  r*   r7   ry  )rv   r  r   r   creatorTypecreatorTexts         r;   r  z%MusicXMLImporter.creatorToContributorr  si    4 $$&AAkk&)"
 !AFll" &&(AFH r=   c                    |j                   }||j                         }t        j                  |      }|j	                  d      }|||_        |S )a   
        Given a <rights> tag, fill the necessary parameters of a
        :class:`~music21.metadata.primitives.Copyright` object.

        >>> from xml.etree.ElementTree import fromstring as EL
        >>> rights = EL('<rights type="owner">CC-SA-BY</rights>')

        >>> MI = musicxml.xmlToM21.MusicXMLImporter()
        >>> c = MI.rightsToCopyright(rights)
        >>> c
        <music21.metadata.primitives.Copyright CC-SA-BY>
        >>> c.role
        'owner'
        >>> str(c)
        'CC-SA-BY'
        r[  )r*   r7   r   	Copyrightrq   r  )rv   r  rtr   copyrightTypes        r;   r  z"MusicXMLImporter.rightsToCopyright  sM    $ [[>Br"

6*$"AFr=   ri   NN)r  ET.Element | Noner   metadata.Metadata | Nonereturnr  )r  
ET.ElementrK  metadata.Metadatar  None)r  r  r   r  r  r  )r  strr  bool)r  r  rK  r  r  r  )r  r  r   z&metadata.primitives.Contributor | None)r   r   r   r   r  r  r  r-  r#  r?  r9  r8  r6  ru  r{  r@  r4  r  r
   
deprecatedr  staticmethodr  r  r  r  __classcell__r  s   @r;   r  r    s#   ,.77$DL	!-F7rBL@\6Ap(2Z #+/33 )3 
 	3l6@"6@ 6@ 
	6@p V8%G ,0" ) 
 	 H& 2 2&,6` OS+&0+'K+Zr=   r  c                       e Zd ZdZ	 	 	 d	 	 	 	 	 d fdZddZ	 	 	 	 ddZ	 	 	 	 	 	 ddZd ZdddZ	e
	 	 	 	 	 	 dd       Zd	 Zdd
ZddZ	 	 	 	 	 	 ddZddZddZddZd dZd dZd!dZ xZS )"rV  zj
    parser to work with a single <part> tag.

    called out for multiprocessing potential in future
    c                d   t         |           || _        || _        |P|j	                  d      | _        | j
                  5|3t        |j                  j                               d   | _        nd| _        ||n	t               | _
        | j                  j                  | _        t        j                         | _        | j                  W| j                  j                  d      D ]9  }t        |      }|st!        |      dkD  s t        j"                         | _         n d| _        g | _        d | _        d| _        d| _        i | _        d gd	z  | _        d| _        d| _        d | _        d| _        d | _        d | _        d| _        d | _         tB        jD                  | _#        d| _$        d | _%        y )
Nr2  r   r?   zmeasure/attributes/stavesr   TFr3     )&r  r  rX  rQ  rq   rP  r:  r  r;  r  rU  r  r&   Partr7  rC   rN   	PartStaffatSoundingPitchstaffReferenceListlastTimeSignaturelastMeasureWasShortlastMeasureOffset	lastClefsactiveTuplets	maxStaveslastMeasureNumberlastNumberSuffixmultiMeasureRestsToCaptureactiveMultiMeasureRestSpanneractiveInstrumentfirstMeasureParsedactiveAttributesr   divisionsPerQuarterlastDivisionsrW  lastMeasureParser)rv   rX  rQ  rU  mxStaves
stavesTextr  s         r;   r  zPartParser.__init__  s    	& **T*DK{{"v'9"6#9#9#>#>#@A!DDK & 2f8H8J![[66#);;=;;" KK//0KL)(3
#j/A"5"("2"2"4DK	 M  $ =?;?#( !$ 57:>!!"*.*+'LP*<@"' $"*">">'+$59r=   c                   | j                          | j                          | j                  | j                  _        g }| j                  j                  d      D ]I  }t        |t        j                        s| j                  j                  d|       |j                  |       K |D ]  }| j                  j                  |        | j                  j                          g }| j                  dkD  r| j                         }n`| j                  T| j                  j!                  | j                         | j                  j"                  j                  | j                         | j%                  ||       y)z1
        Run the parser on a single part
        Tr   r   N)parseXmlScorePartparseMeasuresr  r&   r  rD  rm   r%   Ottavar5  r   rE  rF  r   separateOutPartStavesrP  addGroupForElementsgroups _fillAndInsertOttavasInPartStaff)rv   completedSpannersrR  
partStavess       r;   r!  zPartParser.parse  s    	 &*&:&:# 46$$88>Bb'..1&&q"-$$R(	 ? $B%%b) $ 	'')-/
>>A335J[[$KK++DKK8KK%%dkk2--.?Lr=   c                   |D ]  }t        |t        j                        sd }|r!| j                  |j	                         |      }n| j
                  }|R|j                  d|       |j                          |j                  |        y )Nr   )	rm   r%   r  _findFirstPartStaffContaininggetFirstr&   r5  rF  fill)rv   spannersr  rR  spannerParts        r;   r  z+PartParser._fillAndInsertOttavasInPartStaff   sw     Bb'..1,0K"@@PZ["kk&&&q"-//1$ r=   c                F    |y |D ]  }|j                  |d      |c S  y )NFsetActiveSite)containerInHierarchy)rv   objr  	partStaffs       r;   r  z(PartParser._findFirstPartStaffContaining6  s7    
 ;#I--c-GS   $
 r=   c                H   | j                   }| j                  }t        } |||dt               |j	                  d      }|'|j                  d      }|dk(  rd|j                  _         |||dt               |j	                  d      }|'|j                  d      }|dk(  rd|j                  _        | j                         }|j                         |j                         |_        || _        |j                  |_        |j                  |_        |j                  d|       y)	a  
        The <score-part> tag contains a lot of information about the
        Part itself.  It was found in the <part-list> in the ScoreParser but
        was not parsed and instead passed into the PartParser as .mxScorePart.

        Sets the stream.partName, stream.partAbbreviation, self.activeInstrument,
        and inserts an instrument at the beginning of the stream.

        The instrumentObj being configured comes from self.getDefaultInstrument.
        z	part-namerS   Nr   r   Fzpart-abbreviationr3  )r&   rQ  r_   r<   rW   rq   r'   printPartNameprintPartAbbreviationgetDefaultInstrumentbestNamer2  r  partNamepartAbbreviationr5  )rv   r1  rQ  r   
mxPartNameprintObjectmxPartAbbreviationinstrumentObjs           r;   r  zPartParser.parseXmlScorePartE  s    {{&&' 	T;v> %%k2
!$..8Kd"+0

(
 	T; 3vF(--.AB),00@Kd"38

0 113!!#/#,,.DG -%.. - > >]+r=   c                <   || j                   }|t        d      d }t        }|j                  d      }d}||j                  d      }|j                  d      }t	        |      x}r#t               }		 |	j                   ||            }n5t	        |      x}r(	 t        j                   ||            } |||d	|
       |t        j                          }|j                  d      }|at#        |t        j                        sG| j%                  ||      }|j&                  |j&                  k(  st#        |t        j(                        r|}| j*                  |_        | j*                  %|j,                  j/                  | j*                         | j0                  j2                  |_        | j0                  j4                  |_        |* |||dt6        
        |||dt6        
        |||d       |S # t        $ rL}
t        j                  t        |
      d       t        j                         } ||      |_        Y d}
~
d}
~
ww xY w# t        j                  $ r?}t        j                  t        |      d       t        j                          }Y d}~d}~ww xY w)al  
        Get a default instrument from the mxScorePart tag.

        >>> scorePart = (
        ...     '<score-part id="P4"><part-name>Bass</part-name>'
        ...     '<part-abbreviation>B.</part-abbreviation>'
        ...     '<score-instrument id="P4-I4">'
        ...     '    <instrument-name>Instrument 4</instrument-name>'
        ...     '</score-instrument>'
        ...     '<midi-instrument id="P4-I4">'
        ...     '   <midi-channel>4</midi-channel>'
        ...     '<midi-program>1</midi-program>'
        ...     '</midi-instrument>'
        ...     '</score-part>')
        >>> from xml.etree.ElementTree import fromstring as EL
        >>> pp = musicxml.xmlToM21.PartParser()

        >>> mxScorePart = EL(scorePart)
        >>> i = pp.getDefaultInstrument(mxScorePart)
        >>> i
        <music21.instrument.Instrument ': Instrument 4'>
        >>> i.instrumentName
        'Instrument 4'

        Non-default transpositions captured as of v7.3:

        >>> scorePart = (
        ...     '<score-part id="P5"><part-name>C Trumpet</part-name>'
        ...     '<part-abbreviation>C Tpt.</part-abbreviation>'
        ...     '<score-instrument id="P5-I5">'
        ...     '    <instrument-name>C Trumpet</instrument-name>'
        ...     '</score-instrument>'
        ...     '<midi-instrument id="P5-I5">'
        ...     '   <midi-channel>2</midi-channel>'
        ...     '<midi-program>57</midi-program>'
        ...     '</midi-instrument>'
        ...     '</score-part>')
        >>> from xml.etree.ElementTree import fromstring as EL
        >>> pp = musicxml.xmlToM21.PartParser()

        >>> mxScorePart = EL(scorePart)
        >>> i = pp.getDefaultInstrument(mxScorePart)
        >>> i
        <music21.instrument.Trumpet ': C Trumpet'>
        >>> i.instrumentName
        'C Trumpet'
        >>> i.transposition
        <music21.interval.Interval P1>
        Nz/score-part must be defined before calling this.c                0    t        |       dz
  }|dk(  rd}|S )Nr   r   )rN   )mcadjusteds     r;   _adjustMidiDataz8PartParser.getDefaultInstrument.<locals>._adjustMidiData  s     2w{H2~Or=   zmidi-instrumentzmidi-programzmidi-unpitched   
stacklevelzmidi-channelrS   zscore-instrumentinstrument-namezinstrument-abbreviationzinstrument-sound)rQ  r/   r_   rW   rC   r    midiPitchToInstrumentr   warningswarnr0   r   UnpitchedPercussionpercMapPitchinstrumentFromMidiProgramInstrumentException
Instrumentrm   reclassifyInstrumentFromNamemidiProgramPianorP  r  r   r&   r(  r)  r<   )rv   rQ  r3  r   mxMIDIInstrumentimxMidiProgrammxMidiUnpitchedmidiUnpitchedTextpmmpemidiProgramTextiemxScoreInstrumentinst_from_names                  r;   r&  zPartParser.getDefaultInstrumentw  sg   f **K)A 	 ( '++,=>(,',11.AM.334DEO$0$AA A%'H00AR1STA %1$???0"<<__=]^A
 Q(.OT9%%'A (,,-?@(Az?]?]1^!>>qBSTN ))Q]]:jJL\L\>]";;;;"HHOODKK([[))
![[99 (%'8FK%'@FS%'9:
 Y / HMM/#"61E"668A%45F%GANN	H "55 0MM/""5!D"--/A0s1   4G1 I	 1	I:AII	J4JJc                    |j                  d      }t        |      x}r)| j                  }	 t        j                  |      } || _        | S # t        j
                  $ r t        j                         } Y 2w xY w)Nr7  )rW   rC   midiChannelr   
fromStringr>  r?  )rD  rL  mxInstrumentNameinstrumentNameTextprevious_midi_channels        r;   r@  z'PartParser.reclassifyInstrumentFromName  s{    
 -112CD!-.>!???$%MM!,))*<= 2AM 11 ,))+,s   A
 
'A43A4c                    | j                   }| j                  j                  d      D ]  }| j                  |        | j	                          |j                          y)zI
        Parse each <measure> tag using self.xmlMeasureToMeasure
        measureN)r&   rX  iterfindxmlMeasureToMeasure&removeFinaleIncorrectEndingForwardRestrF  )rv   r1  	mxMeasures      r;   r  zPartParser.parseMeasures	  sN     {{--i8I$$Y/ 9 	335  "r=   c                   | j                   }|yd| _         |j                  y|j                  ry|j                  }|j                  t        j
                     j                         |u r!||j                  j                  |d       yyy)a#  
        If Finale generated the file AND it ended with an incomplete
        measure (like 4/4 beginning with a quarter pickup and ending
        with a 3-beat measure) then the file might have ended with a
        `<forward>` tag, which Finale used to create hidden rests.

        If this forward tag is at the end of the piece, then it
        will create rests that "complete" the measure in an incorrect way
        If voices are not involved (e.g., NOT bwv66.6) then we should
        remove this forward tag.

        * New in v7.
        NT)recurse)r
  lastForwardTagCreatedByFinale	useVoicesr&   r!   GeneralNotelastrE  )rv   lmpendingForwardRests      r;   rX  z1PartParser.removeFinaleIncorrectEndingForwardRest  s     $$;!%,,4==,/,M,MJJt''(--/3DD%1JJ/> 2 Er=   c           	     T   g dt        dg      }| j                         }g }t               	 	 	 	 	 	 dfd}| j                  j	                  t        j
                        }t        |      D ]  \  }}dd }|dk7  r|j                  d       t        j                  t        j                  | j                  j                  |d|            }	| j                   d	| }
|
|	_        |	j                  |
d
       |	j                  j                  |
       |j                  |	       |	| j                   j"                  |
<   t               }| j$                  D ]5  }| j'                  ||      }|D ]  }|j)                  t        |              7 t+        ||	j	                  t        j
                              D ]T  \  }} ||||       t+        |j,                  |j,                        D ]  \  }} ||||        |j/                  dd       V  | j                   j                  }t1        j2                  || j                  j4                  d      }d|j6                  _        |j;                  d|       |D ]  }|j;                  d|        |j=                          d| _        |S )z
        Take a `Part` with multiple staves and make them a set of `PartStaff` objects.

        There must be more than one staff to do this.
        )ClefDynamic
Expressionr^  KeySignaturer   TempoIndicationTimeSignatureRepeatBracketc                   | j                        }d|_        |D ]  }t        |      }||v r|	v rt        j                  |      }n|}	j                  |       | j                  |d      }|t        j                  k7  r%t        |t              rJ |j                  ||       |j                  |        |j                          y )NFT)returnSpecial)getElementsByClassrestoreActiveSitesr2  copydeepcopyra   elementOffsetr   AT_ENDrm   r5  coreStoreAtEndrF  )
sourcetargetomitTheseElementIdselementIterator
sourceElemidSource
targetElemsourceOffsetSTAFF_SPECIFIC_CLASSESappendedElementIdss
           r;   copy_into_partStaffz=PartParser.separateOutPartStaves.<locals>.copy_into_partStaffM  s     %778NOO16O.-
j>2211!%z!:J!+J&**84%33Jd3S=#7#77),FFF%%lJ?))*5 .  &&(r=   Nr   SpannerF)removeClassesfillWithRestsexemptFromRemover  r  T)forceinPlacer  )ry  r  )rs  stream.Streamrt  r  ru  zset[int]r  r  ) 	frozenset_getUniqueStaffKeyssetr&   rl  Measure	enumerater   tcastr  templaterP  r2  r  r  rU  r  r  _getStaffExcludera   rp   voicesflattenUnnecessaryVoicesr   r  r(  r'   r   r5  rF  rW  )rv   EXEMPT_FROM_REMOVEuniqueStaffKeysr  r}  sourceMeasureIterator
staffIndexstaffKeyr  newPartStaffpartStaffIdelementsIdsNotToGoInThisStaffstaffReferenceexcludeOneMeasurer  sourceMeasurecopyMeasuresourceVoice	copyVoicescorer  r"  r{  r|  s                         @@r;   r  z PartParser.separateOutPartStaves1  s   	"
 '
 &*%=%=%?-/
'*u	)(5	)5=	)BF	). !% > >v~~ N$-o$> J215MQ$$Y/66  $$=386H % JL "[[Mz:K)LO,,[,N&&{3l+:FDKK**;769e) #'"9"9 %)$9$9"%! ,B155bf= , #: /2%//?/*{ $M;@]^.1-2F2FHZHZ.[*K'Y@]^ /\445$4O/? %?P ""&&z8L8LU\]
-1
*J'#IQ	* $!!#',$ r=   c                ^    |t         k(  rg S g }|D ]  }|t         k(  r||k(  r|||   z  } |S )a  
        Given a staff reference dictionary (for a single measure),
        remove and combine in a list all elements that
        are NOT part of the given targetKey. Thus, return a list of all entries to remove.
        It keeps those elements under the staff key None (common to all) and
        those under given key. This then is the list of all elements that should be deleted.

        If targetKey is NO_STAFF_ASSIGNED (0) then returns an empty list
        )NO_STAFF_ASSIGNED)rv   r  	targetKeypostks        r;   r  zPartParser._getStaffExclude  sM     ))IA%%iN1%%D   r=   c                    g }| j                   D ])  }|D ]"  }|t        k7  s||vs|j                  |       $ + |j                          |S )z
        Given a list of staffReference dictionaries,
        collect and return a list of all unique keys except NO_STAFF_ASSIGNED (0)
        )r  r  r   rG  )rv   r  r  r  s       r;   r  zPartParser._getUniqueStaffKeys  sM    
 "55N#))atmKKN $ 6 			r=   c                   t        ||       }	 |j                          || _        t        | j                  |j                        | _        |j                  | j!                  |j                         d	| _        | j$                  j'                  |j(                         |j
                  }| j+                  |       |j,                  d	u r|t.        j0                     j3                         }t4        j6                  r|J | j8                  !| j8                  j:                  j<                  }nd
}|j>                  d	u s`|j@                  j<                  |k7  r_|j@                  jB                  dv rG|j@                  jD                  dk(  r.|j@                  jF                  s||j@                  _        d	|_        | j
                  jI                  | jJ                  |       | jM                  |       |S # t        $ r<}t        |j                        |_        | j
                  j                  |_        |d}~wt        $ rL}t        j                  d|j                   dd| j
                  j                   dz   t        d       |d}~ww xY w)a3  
        Convert a measure element to a Measure, using
        :class:`~music21.musicxml.xmlToM21.MeasureParser`

        >>> from xml.etree.ElementTree import fromstring as EL

        Full-measure rests get auto-assigned to match the time signature if they
        do not have a type, or have a type of "whole".

        Here is a measure with a rest that lasts 4 beats, but we will put it in a 3/4 context.

        >>> scoreMeasure = '<measure><note><rest/><duration>40320</duration></note></measure>'
        >>> mxMeasure = EL(scoreMeasure)
        >>> pp = musicxml.xmlToM21.PartParser()
        >>> pp.lastDivisions
        10080
        >>> 40320 / 10080
        4.0
        >>> pp.lastTimeSignature = meter.TimeSignature('3/4')
        >>> m = pp.xmlMeasureToMeasure(mxMeasure)

        Test that the rest lasts three, not four beats:

        >>> measureRest = m.notesAndRests[0]
        >>> measureRest
        <music21.note.Rest dotted-half>
        >>> measureRest.duration.type
        'half'
        >>> measureRest.duration.quarterLength
        3.0
        )rU  Nz)The following exception took place in m.  in zpart .r4  r5  T      @wholebrever   )'MeasureParserr!  r/   r  measureNumberr&   r(  	Exceptionr9  r:  r0   r
  maxr   stavestranspositionupdateTranspositionr  r  r   r  setLastMeasureInfofullMeasureRestr!   Restfirstr  TYPE_CHECKINGr  barDurationquarterLengthfullMeasurer   r[  dotstupletsinsertr  adjustTimeAttributesFromMeasure)rv   rY  measureParseremr1lastTSQls          r;   rW  zPartParser.xmlMeasureToMeasure  s   B &i=	! "/T^^]-A-AB&&2$$]%@%@A"&&&}'C'CD  " ((D0499##%B~%~%%111==KK$&KK--9((,>>((A-KK//,4)!% 	41115,,Q/ m ' 	!-"="=>AO--AJG 	MM;M<W<W;XX\]$++../q12	 G	s$   G, ,	J57H,,J8AI??Jc                   | j                   | j                   j                  | j                  du rn| j                   j                  |k7  rt        j                  | j                         }|| _         | j
                  j                  | j                  |       n]t        j                  dt        d       t        j                         }|| _         | j
                  j                  | j                  |       || j                   _        d| _        y)a  
        As one might expect, a measureParser that reveals a change
        in transposition is going to have an effect on the
        Part's instrument list.  This (totally undocumented) method
        deals with it.

        If `measureParser.transposition` is None, does nothing.

        NOTE: Need to test a change of instrument w/o a change of
        transposition such as: Bb clarinet to Bb Soprano Sax to Eb clarinet?
        NFz=Received a transposition tag, but no instrument to put it on!r4  r5  )r  r  r  rn  ro  r&   r5  r  r9  r:  r0   r   r?  r  )rv   newTranspositionnewInstfakeInsts       r;   r  zPartParser.updateTransposition   s       ,%%33;//58 &&448HH
 --(=(=>(/%&&t'='=wG
 MMO
 ",,.H$,D!KK""4#9#98D
 /?+$r=   c                
   |j                   | j                  k(  rn"|j                   | _        |j                  | _        |j                  |j                  | _        y| j
                  t        j                  d      }|| _        yy)at  
        Sets self.lastMeasureNumber and self.lastMeasureSuffix from the measure,
        which is used in fixing Finale unnumbered measure issues.

        Also sets self.lastTimeSignature from the timeSignature found in
        the measure, if any.

        >>> pp = musicxml.xmlToM21.PartParser()

        Here are the defaults:

        >>> pp.lastMeasureNumber
        0
        >>> pp.lastNumberSuffix is None
        True
        >>> pp.lastTimeSignature is None
        True

        After setLastMeasureInfo:

        >>> m = stream.Measure(number=4)
        >>> m.numberSuffix = 'b'
        >>> ts38 = meter.TimeSignature('3/8')
        >>> m.timeSignature = ts38
        >>> pp.setLastMeasureInfo(m)

        >>> pp.lastMeasureNumber
        4
        >>> pp.lastNumberSuffix
        'b'
        >>> pp.lastTimeSignature
        <music21.meter.TimeSignature 3/8>
        >>> pp.lastTimeSignature is ts38
        True

        Note that if there was no timeSignature defined in m,
        and no lastTimeSignature exists,
        the PartParser gets a default of 4/4, because
        after the first measure there's going to be routines
        that need some sort of time signature:

        >>> pp2 = musicxml.xmlToM21.PartParser()
        >>> m2 = stream.Measure(number=2)
        >>> pp2.setLastMeasureInfo(m2)
        >>> pp2.lastTimeSignature
        <music21.meter.TimeSignature 4/4>

        For obscure reasons relating to how Finale gives suffixes
        to unnumbered measures, if a measure has the same number
        as the lastMeasureNumber, the lastNumberSuffix is not updated:

        >>> pp3 = musicxml.xmlToM21.PartParser()
        >>> pp3.lastMeasureNumber = 10
        >>> pp3.lastNumberSuffix = 'X1'

        >>> m10 = stream.Measure(number=10)
        >>> m10.numberSuffix = 'X2'
        >>> pp3.setLastMeasureInfo(m10)
        >>> pp3.lastNumberSuffix
        'X1'
        Nz4/4)r   r  numberSuffixr  timeSignaturer  r   rh  )rv   r  tss      r;   r  zPartParser.setLastMeasureInfoS  ss    ~ 88t--- &'XXD"$%NND!??&%&__D"##+$$U+B%'D" ,r=   c                    |j                   }| j                  !| j                  j                  j                  }nd}||k(  r|}n||kD  r||z
  }d}|dkD  s$t	        |d      d   |k  st	        |d      d   |k  r|}n||}t        j                  d|j                   d	| j                  j                   d
| d| d| dt        d       n.|dk(  rj|j                         j                  j                  d      sAt        j                         }||j                   _        |j#                  d|       |}d| _        n| j&                  dk(  r&|j)                         dk  r|j+                          |}n|}| j$                  du r+|j)                         dk  rg|j+                          d| _        nO|j)                         dk  r(|j                  j                  |j                   z
  |_        ||k  rd| _        nd| _        | xj&                  |z  c_        y)a  
        Adds padAsAnacrusis to pickup measures and other measures that
        do not fill the whole tile, if the first measure of the piece, or
        immediately follows an incomplete measure (such as a repeat sign mid-measure
        in a piece where each phrase begins with a pickup and ends with an
        incomplete measure).

        Fills an empty measure with a measure of rest (bug in PDFtoMusic and
        other MusicXML writers).

        Sets self.lastMeasureWasShort to True or False if it is an incomplete measure
        that is not a pickup and sets paddingRight.

        >>> m = stream.Measure([meter.TimeSignature('4/4'), harmony.ChordSymbol('C7')])
        >>> m.highestTime
        0.0
        >>> pp = musicxml.xmlToM21.PartParser()
        >>> pp.setLastMeasureInfo(m)
        >>> pp.adjustTimeAttributesFromMeasure(m)
        >>> m.highestTime
        4.0
        >>> pp.lastMeasureWasShort
        False

        Incomplete final measure:

        >>> m = stream.Measure([meter.TimeSignature('6/8'), note.Note(), note.Note()])
        >>> m.offset = 24.0
        >>> pp = musicxml.xmlToM21.PartParser()
        >>> pp.lastMeasureOffset = 21.0
        >>> pp.setLastMeasureInfo(m)
        >>> pp.adjustTimeAttributesFromMeasure(m)
        >>> m.paddingRight
        1.0
        Nr  gư>g      ?g      ?r   gUUUUUU?zWarning: measure z	 in part zis overfull: z > z
,assuming z is correct.r4  r5  r3  HarmonyFg      ?T)highestTimer  r  r  r   r9  r:  r   r&   r(  r0   r[  notesAndRestsgetElementsNotOfClassr!   r  r   r  r  r  barDurationProportionpadAsAnacrusispaddingRight)rv   r  mHighestTimelastTimeSignatureQuarterLengthmOffsetShiftdifftolrs           r;   r  z*PartParser.adjustTimeAttributesFromMeasure  s   T }}
 !!--1-C-C-O-O-]-]*-0*99'L::"@@DC s
$T6215;$T6215;+='z4;;;O;O:P#L>5S4T U  ,~\; $  c!iik//EEiP
 		A'EAJJ$HHS!9L',D$
 %%,**,s2$$&  ,+++t3..036((*380 ..036)*)D)Dq}})T#&DD370380,.r=   c                    | j                   y| j                   j                  |       | xj                  dz  c_        | j                  dk(  r.| j                  j	                  d| j                          d| _         yy)a  
        If there is an active MultiMeasureRestSpanner, add the Rest, r, to it:

        >>> pp = musicxml.xmlToM21.PartParser()
        >>> mmrSpanner = spanner.MultiMeasureRest()
        >>> mmrSpanner
        <music21.spanner.MultiMeasureRest 0 measures>

        >>> pp.activeMultiMeasureRestSpanner = mmrSpanner
        >>> pp.multiMeasureRestsToCapture = 2
        >>> r1 = note.Rest(type='whole', id='r1')
        >>> pp.applyMultiMeasureRest(r1)
        >>> pp.multiMeasureRestsToCapture
        1
        >>> pp.activeMultiMeasureRestSpanner
        <music21.spanner.MultiMeasureRest 1 measure>

        >>> pp.activeMultiMeasureRestSpanner is mmrSpanner
        True
        >>> pp.stream.show('text')  # Nothing shown!

        >>> r2 = note.Rest(type='whole', id='r2')
        >>> pp.applyMultiMeasureRest(r2)
        >>> pp.multiMeasureRestsToCapture
        0
        >>> pp.activeMultiMeasureRestSpanner is None
        True

        # spanner added to stream

        >>> pp.stream.show('text')
        {0.0} <music21.spanner.MultiMeasureRest 2 measures>

        >>> r3 = note.Rest(type='whole', id='r3')
        >>> pp.applyMultiMeasureRest(r3)
        >>> pp.stream.show('text')
        {0.0} <music21.spanner.MultiMeasureRest 2 measures>

        Nr   r   )r  r  r  r&   r  )rv   r  s     r;   applyMultiMeasureRestz PartParser.applyMultiMeasureRest	  sn    P --5**==a@''1,'**a/KKq$"D"DE15D. 0r=   )NNN)rX  r  rQ  r  rU  zMusicXMLImporter | None)r  r  )r  zlist[spanner.Spanner]r  list[stream.PartStaff])r!  zbase.Music21Object | Noner  r  r  zstream.PartStaff | Noneri   )rQ  r  r  instrument.Instrument)rD  r  rL  r  r  r  )r  r  )r  StaffReferenceTyper  rN   r  zlist[base.Music21Object])r  z	list[int])rY  r  r  stream.Measure)r  zinterval.Interval)r  r  )r  z	note.Rest)r   r   r   r   r  r!  r  r  r  r&  r  r@  r  rX  r  r  r  rW  r  r  r  r  r  r  s   @r;   rV  rV    s    ,004154:(4:-4: /4:l"MH%'% +%,$ + 
	.,dAF  % 
 	#?:iV*  
"	4[z1%fN(`n/`.6r=   rV  c                      e Zd ZdZddddddZdd	d
ddddddddddZ	 	 dW	 	 	 dX fdZedYd       Z	 	 	 	 	 	 dZdZ		 	 	 	 	 	 d[dZ
d Zd\dZd\dZd]dZd^dZd_dZd`dadZdbdcdZdbdZd Zdbd Z	 db	 	 	 	 	 ddd!Z	 db	 	 	 	 	 ded"Zd# Zd`d$Zdbd%Zd& Zdfd'Zd( Zd) Zed*        Zd+ Z d, Z!dd-	 dgd.Z"	 	 dW	 	 	 	 	 dhd/Z#d0 Z$d1 Z%d2d3d4Z&d5 Z'did6Z(d7 Z)dbdjd8Z*d9 Z+	 db	 	 	 dkd:Z,dld;Z-dbd<Z.dbd=Z/d> Z0	 	 	 	 dmd?Z1d@ Z2	 	 	 	 	 	 	 	 dndAZ3	 	 	 	 	 	 	 	 	 	 dodBZ4dC Z5dD Z6dbdEZ7dpdFZ8dG Z9dH Z:dI Z;dJ Z<	 	 	 	 dqdKZ=drdLZ>dM Z?dN Z@dsdOZAdP ZBdQ ZCdR ZD	 db	 	 	 dtdSZEdT ZFdbdUZGdV ZH xZIS )ur  a7  
    parser to work with a single <measure> tag.

    called out for simplicity.

    >>> from xml.etree.ElementTree import fromstring as EL

    >>> scoreMeasure = '<measure><note><rest/><duration>40320</duration></note></measure>'
    >>> mxMeasure = EL(scoreMeasure)
    >>> mp = musicxml.xmlToM21.MeasureParser(mxMeasure)
    >>> mp.parse()
    >>> mp.restAndNoteCount['rest']
    1
    >>> mp.restAndNoteCount['note']
    0

    fullMeasureRest indicates that a rest lasts the full measure of the current time signature.

    >>> mp.fullMeasureRest
    True
    handleTimeSignature
handleClefhandleKeySignaturehandleStaffDetailshandleMeasureStyle)timer	   r   staff-detailsmeasure-style	xmlToNote	xmlBackup
xmlForwardxmlDirectionparseAttributesTag
xmlHarmonyNxmlSound
xmlBarline)r!   backupforwardr   
attributesr   zfigured-basssoundbarlinegroupinglinkbookmarkc                   t         |           || _        g | _        ||n	t	               | _        d | _        | j
                  j                  | _        i | _        | j
                  j                  | _	        d| _
        i | _        t               | _        d| _        d | _        d| _        d| _        d| _        | j
                  j&                  | _        i | _        t-        j.                         | _        g | _        g | _        d | _        d | _        d| _        ddd| _        | j
                  j<                  | _        d| _        d| _         d | _!        tE        jF                         | _$        y )NFr   Tr   r?   )restr!   r3  )%r  r  rY  mxMeasureElementsrV  rU  r  r  r  r  r]  
voicesByIdr  voiceIndicesr  r  attributesAreInternalr  r  r	  	divisionsr   r&   r  
mxNoteListmxLyricListnLast	lastVoicer  restAndNoteCountr  
parseIndexr   r\  weakrefWeakKeyDictionarypedalToStartOffset)rv   rY  rU  r  s      r;   r  zMeasureParser.__init__r	  sF    	"35,2,>&JL!![[66249=9R9R$79*-% $%)"22
 UWnn&,.-/,0
 (,
  % *+A 648KK4I4I ,/ >B* >E=V=V=Xr=   c                   t        | t              r| S t        | t              rt        |       S | t        S | j                  }|dv rC	 | j                  d      }|)|j                  }||j                         }t        |      S t        S |dv r+	 | j                  d      }|t        |      S 	 |dv ryt        S t        S # t        t        t        f$ r Y t        S w xY w# t        t        t        f$ r Y Fw xY w)ab  
        gets an int representing a staff number from an mxObject or a number:

        >>> mp = musicxml.xmlToM21.MeasureParser()
        >>> from xml.etree.ElementTree import fromstring as EL

        <note> tags store their staff numbers in a <staff> tag's text:

        >>> mp.getStaffNumber(EL('<note><staff>3</staff></note>'))
        3

        If there is no <staff> tag, notes return the special NO_STAFF_ASSIGNED value
        (which is, for now, just a fancy alias for 0!)

        >>> el = EL('<note><pitch><step>C</step><octave>4</octave></pitch></note>')
        >>> NO_STAFF_ASSIGNED = musicxml.xmlToM21.NO_STAFF_ASSIGNED
        >>> NO_STAFF_ASSIGNED
        0
        >>> mp.getStaffNumber(el) == NO_STAFF_ASSIGNED
        True

        Other objects, such as keys, clefs, and various layouts, however,
        store their staff numbers in a `number` attribute.

        >>> mp.getStaffNumber(EL('<key number="2"/>'))
        2

        If there is no number for most objects, such as keys and time signatures,
        it means that the object affects all staves (makes sense for a time signature, no?).
        Hence they are treated as NO_STAFF_ASSIGNED

        >>> mp.getStaffNumber(EL('<key/>')) == NO_STAFF_ASSIGNED
        True


        Clef, staff-layout, and staff-details objects, however, use a missing number
        to mean staff 1.

        >>> mp.getStaffNumber(EL('<clef/>'))
        1

        Passing in None or an int is allowed.

        >>> mp.getStaffNumber(None) == NO_STAFF_ASSIGNED
        True
        >>> mp.getStaffNumber(1)
        1

        Deprecation notice: getting staff number from a string is deprecated
            and will be removed in v11 (this sort of lazy conversion is frowned upon).

        OMIT_FROM_DOCS

        Stop dealing with and documenting this silliness in v11

        >>> mp.getStaffNumber('2')
        2
        )r   r  r!   r   staff)rr  r  r  r	   r   r  	transposer   )rr  r  r	   r   )rm   rN   r  r  r\   rW   r*   r7   r@   ru   rO   rq   )rw   r\   staffObject	staffTextr  k_strs         r;   getStaffNumberzMeasureParser.getStaffNumber	  s   x h$O#&x= $$ll ==&mmG4* + 0 0I ,%OO-"1v %$ " " X.$u:% %    $$$$3 #Iz: $$  z>: s$   ;B> C >CCC43C4c                    | j                   }t        |t              r|}n| j                  |      }||vrg ||<   ||   j	                  |       y)a  
        Utility routine for importing musicXML objects;
        here, we store a reference to the music21 object in a dictionary,
        where keys are the staff values. Staff values may be None, 1, 2, etc.

        >>> MP = musicxml.xmlToM21.MeasureParser()
        >>> MP.addToStaffReference(1, note.Note('C5'))
        >>> MP.addToStaffReference(2, note.Note('D3'))
        >>> MP.addToStaffReference(2, note.Note('E3'))
        >>> len(MP.staffReference)
        2
        >>> list(sorted(MP.staffReference.keys()))
        [1, 2]
        >>> MP.staffReference[1]
        [<music21.note.Note C>]
        >>> MP.staffReference[2]
        [<music21.note.Note D>, <music21.note.Note E>]

        >>> from xml.etree.ElementTree import fromstring as EL
        >>> mxNote = EL('<note><staff>1</staff></note>')
        >>> MP.addToStaffReference(mxNote, note.Note('F5'))
        >>> MP.staffReference[1]
        [<music21.note.Note C>, <music21.note.Note F>]

        No staff reference.

        >>> mxNote = EL('<note />')
        >>> MP.addToStaffReference(mxNote, note.Note('G4'))
        >>> len(MP.staffReference)
        3
        >>> MP.staffReference[0]
        [<music21.note.Note G>]
        N)r  rm   rN   r  r   )rv   mxObjectOrNumberrx   r  r  s        r;   addToStaffReferencez!MeasureParser.addToStaffReference*
  sV    L ,,&,'H**+;<H>)')N8$x ''	2r=   c                `    | j                  ||       | j                  j                  ||       y)ax  
        runs addToStaffReference and then insertCore (which will do opFracs, so no need to do
        so before here)

        >>> from xml.etree.ElementTree import fromstring as EL
        >>> mxNote = EL('<note><staff>1</staff></note>')

        >>> MP = musicxml.xmlToM21.MeasureParser()
        >>> MP.insertCoreAndRef(1.0, mxNote, note.Note('F5'))

        This routine leaves MP.stream in an unusable state, because
        it runs insertCore.  Thus, before querying the stream we need to run at end:

        >>> MP.stream.coreElementsChanged()
        >>> MP.stream.show('text')
        {1.0} <music21.note.Note F>
        N)r  r&   r5  )rv   offsetstaffSourcerx   s       r;   insertCoreAndRefzMeasureParser.insertCoreAndRefY
  s(    . 	  i8vy1r=   c                   | j                   j                  d      D ]  }| j                  |        | j                          | j	                          t        | j                         | _        t        | j                        D ]U  \  }}|| _        |j                  | j                  v s&| j                  |j                     }|Bt        | |      } ||       W | j                  r<| j                  j                         j                  D ]  }|s|j!                           | j                  j!                          | j"                  d   dk(  r| j"                  d   dk(  rd| _        y y y )Nprintr  r   r!   r   T)rY  r7  xmlPrintparseMeasureAttributesupdateVoiceInformationr:  r  r  r  r\   musicDataMethodsgetattrr]  r&   iterr  rF  r  r  )rv   r   rD  rA   methNamemethvs          r;   r!  zMeasureParser.parses
  s4    ~~--g6GMM'" 7 	##%##%!%dnn!5!$"8"89HAuDOyyD11100;'"42DK : >>[[%%'.. ))+	 /
 	'')!!&)Q.))&1Q6#'D  7 /r=   c                    |j                  d      }t        |      x}rOt        | j                  t	        |      | j
                  z  z
        | _        t        | j                  d      | _        yy)a7  
        Parse a backup tag by changing :attr:`offsetMeasureNote`.

        A floor of 0.0 is enforced in case of float rounding issues.

        >>> from xml.etree.ElementTree import fromstring as EL
        >>> MP = musicxml.xmlToM21.MeasureParser()
        >>> MP.divisions = 100
        >>> MP.offsetMeasureNote = 1.875

        >>> mxBackup = EL('<backup><duration>100</duration></backup>')
        >>> MP.xmlBackup(mxBackup)
        >>> MP.offsetMeasureNote
        0.875

        >>> MP.xmlBackup(mxBackup)
        >>> MP.offsetMeasureNote
        0.0
        r   r3  N)rW   rC   r   r   rM   r  r  )rv   rA   
mxDurationdurationTexts       r;   r  zMeasureParser.xmlBackup
  sk    ( ZZ
+
'
33<3%+D,B,B.3L.ADNN.R-S &TD"
 &))?)?%ED" 4r=   c                   |j                  d      }t        |      x}rt        t        |      | j                  z        }| j
                  r| j
                  j
                  rr| j
                  j
                  j                  rRt        j                  |      }d|j                  _
        | j                  ||       | j                  ||       || _        t        | j                  |z         | _        yy)zL
        Parse a forward tag by changing :attr:`offsetMeasureNote`.
        r   r  TN)rW   rC   r   rM   r  rU  r  r!   r  r'   r   r  insertInMeasureOrVoicer\  r   )rv   rA   r#  r$  changer  s         r;   r  zMeasureParser.xmlForward
  s     ZZ
+
'
33<3E,/$..@AF****AA IIF3,0)((2++E15 672 &,D,B,BV,K%LD"' 4r=   c                6   fd}fd} |       } |       }j                  d      du}| j                  }|du r#| j                        }|j                  d|       |du s|du r#| j	                        }	|j                  d|	       |du r| j
                  }
j                  d      D cg c]
  } |
|       }}|D ]7  }||j                  | j                  dt        |j                        |       9 | j                  j                          j                  d      }|mt        j                  t        j                  |j                        }|j                  |_        t        j"                         }| j%                  ||       ||_        yyc c}w )	z
        <print> handles changes in pages, numbering, layout,
        etc. so can generate PageLayout, SystemLayout, or StaffLayout
        objects.

        Should also be able to set measure attributes on `self.stream`
        c                 v     j                  d      dvry j                  d      y j                  d      yy)Nr   Nr   Tr   r   Frq   rW   r   s   r;   hasPageLayoutz-MeasureParser.xmlPrint.<locals>.hasPageLayout
  s>    {{:&l:{{=)5||M*6r=   c                 R     j                  d      dvry j                  d      yy)Nr   r+  Tr   Fr,  r-  s   r;   hasSystemLayoutz/MeasureParser.xmlPrint.<locals>.hasSystemLayout
  s,    {{<(<||O,8r=   rr  NTr3  Fzmeasure-numbering)rW   r&   r   r  r   r   rV  r   r  rN   rF  r  r  r'   StreamStyler*   measureNumberingr|  r   measureNumberStyle)rv   r   r.  r0  addPageLayoutaddSystemLayoutaddStaffLayoutr  plslslFuncmxstlListstlmxMeasureNumberingm_styler  s    `               r;   r  zMeasureParser.xmlPrint
  s   		 &)+ n5TA KKD **73BHHS"d"mu&<,,W5BHHS"T!55F,3,<,<^,LM,Lbvbz,LGM ;#//"9%%c3s+?E  KK++-$\\*=>) ffU..8G'9'>'>G$"B##$6;)+G& * Ns   .Fc                   	 | j                   | j                  dz      }|j                  dk(  r|j                  d      d}nd}d}d}d}|j                  d      d}|j                  d      d}|rDd}|j                  d	      }|/|j
                  }t        |t              r	 t        |      }|| _
        |du rOd}	| j                  j                  |       |j                  d
      D ]  }
| j                  j                  |
        nY|du r-|du r)| j                  dxx   dz  cc<   | j!                  |      }	n(| j                  dxx   dz  cc<   | j#                  |      }	|du rt$        j&                  rt        |	t(        j*                        sJ | j-                  |	|j                  d
             | j/                  ||	       | j1                  ||	       |	j2                  j4                  }|	| _        | j                  r|du r| j9                  | j                        }| j-                  || j                         | j/                  | j                  d   |       | j                  D ]'  }|j                  d	      | j1                  ||        n | j1                  ||       g | _        g | _        |j4                  }|| _        t;        | j<                  |z         | _        d| _        y# t        $ r d}Y w xY w# t        $ r Y Ww xY w)a4  
        Handles everything for creating a Note or Rest or Chord

        Does not actually return the note, but sets self.nLast to the note.

        This routine uses coreInserts for speed, so it can leave either
        `self.stream` or a `Voice` object within `self.stream` in an unstable state.
        r   r!   r   NTFr3  r  voicelyricr   ) r  r  r\   rW   
IndexErrorr*   rm   r  rN   rO   r  r   r   r7  r  r  xmlToSimpleNote	xmlToRestr  r  r!   r^  updateLyricsFromListr  r'  r   r  r  
xmlToChordr   r   r\  )rv   mxNote	mxObjNextnextNoteIsChordisChordisRestoffsetIncrementvoiceOfChordvIndexnmxLyricr   
thisMxNotes                r;   r  zMeasureParser.xmlToNote	  s   	$..t/BCI}}&9>>'+B+N"&"'  58;;v*F;;w+G G!;;w/L''3'8'8fc*!$V "(d?AOO""6*!>>'2  ''0 3&E/!!&)Q.)$$V,A!!&)Q.)v&Ae!!T%5%5666%%a)@A$$VQ/''2jj66ODJ
 ??%70A%%a)9)9:$$T__Q%7;"oo
??7+7//
A>	 . ++FA6 DO!DooODJ "((>(>(P!Q-1*a  	$#O	$: & s$   AK' #K9 'K65K69	LLc                   g }|D ]$  }|j                  | j                  |d             & t        d |D              rt        j                  |      }nt        j                  |      }|r0|d   j                  |_        t        j                         |d   _        t               }t               }d }t        ||      D ]  }|j                         }	|	D ]  }
|
j                  ||        |j                  D ]  }t        |      |v r|j                  j                  |       t!        |t        j"                  t        j$                  t        j&                  f      rf|j)                  t        |              |j*                  D ]E  }t        |      |v r|j*                  j                  |       |j)                  t        |             G g |_        g |_         | j,                  j/                  |       |S )a  
        Given a list of mxNotes, fill the necessary parameters

        >>> from xml.etree.ElementTree import fromstring as EL
        >>> MP = musicxml.xmlToM21.MeasureParser()
        >>> MP.divisions = 10080

        >>> qnDuration = r'<duration>7560</duration><type>quarter</type>'

        >>> a = EL('<note>'
        ...        + '<pitch><step>A</step><octave>3</octave></pitch>'
        ...        + qnDuration
        ...        + '</note>')
        >>> b = EL('<note><chord/>'
        ...        + '<pitch><step>B</step><octave>3</octave></pitch>'
        ...        + qnDuration
        ...        + '</note>')
        >>> c = MP.xmlToChord([a, b])
        >>> len(c.pitches)
        2
        >>> c.pitches[0]
        <music21.pitch.Pitch A3>
        >>> c.pitches[1]
        <music21.pitch.Pitch B3>
        >>> c.duration
        <music21.duration.Duration unlinked type:quarter quarterLength:0.75>

        >>> a = EL('<note><pitch><step>A</step><octave>3</octave></pitch>'
        ...        + qnDuration
        ...        + '<notehead>diamond</notehead></note>')
        >>> c = MP.xmlToChord([a, b])
        >>> c.getNotehead(c.pitches[0])
        'diamond'

        >>> a = EL('<note><unpitched><display-step>A</display-step>'
        ...        + '<display-octave>3</display-octave></unpitched>'
        ...        + qnDuration
        ...        + '<notehead>diamond</notehead></note>')
        >>> MP.xmlToChord([a, b])
        <music21.percussion.PercussionChord [unpitched[A3] B3]>
        FfreeSpannersc              3  B   K   | ]  }|j                  d       du  yw)	unpitchedN)rW   )rj   rG  s     r;   rl   z+MeasureParser.xmlToChord.<locals>.<genexpr>  s     M*v{{;'t3*s   r   c                z    t        | d      r| j                  j                  S | j                         j                  S )Nr#   )r   r#   psdisplayPitchmidi)rk   s    r;   <lambda>z*MeasureParser.xmlToChord.<locals>.<lambda>  s*    '!W*=AGGJJX1>>CSCXCXXr=   r   )r   rC  anyr"   PercussionChordr   Chordbeamsr   Beamsr  r  getSpannerSitesreplaceSpannedElementr   r[  rm   	FingeringStringIndicationFretIndicationra   r   r  #freePendingSpannedElementAssignment)rv   r   notesrG  r   seenArticulationsseenExpressionssortKeyrO  ssrR  artexps                r;   rF  zMeasureParser.xmlToChordj  s   V  FLL--f5-IJ ! M*MM**51AE"A AhnnAG!ZZ\E!HN  E%X7++A""$B((A. 9 11&&s+!#(?(?(5(F(F(5(D(D(F G &))$s)4 ' }}9/$$S)##DI.	 % !AOAM) ,, 	>>qAr=   c                   | j                  |      }|j                  d      }|3t        j                  |      }| j	                  ||j
                         n(t        j                  |      }| j                  ||       |j                  d      }|r| j                  |      |_
        |j                  d      }||j                  j                         |_        |j                  rmt        j                          }| j#                  ||       | j%                  ||       t'        j(                  t        j*                  |j                        }	||	_        |j                  d      }
|
| j/                  ||
       | j1                  |||      S )aD  
        Translate a MusicXML <note> (without <chord/>)
        to a :class:`~music21.note.Note`.

        The `spannerBundle` parameter can be a list or a Stream
        for storing and processing Spanner objects.

        if freeSpanners is False then pending spanners will not be freed.

        >>> from xml.etree.ElementTree import fromstring as EL
        >>> MP = musicxml.xmlToM21.MeasureParser()

        >>> MP.divisions = 10080

        >>> mxNote = EL('<note pizzicato="yes"><pitch><step>D</step>'
        ...             '<alter>-1</alter><octave>6</octave></pitch>'
        ...             '<duration>7560</duration>'
        ...             '<type>eighth</type><dot/></note>')

        >>> n = MP.xmlToSimpleNote(mxNote)
        >>> n
        <music21.note.Note D->
        >>> n.octave
        6
        >>> n.duration
        <music21.duration.Duration 0.75>
        >>> n.articulations
        [<music21.articulations.Pizzicato>]

        >>> beams = EL('<beam>begin</beam>')
        >>> mxNote.append(beams)
        >>> n = MP.xmlToSimpleNote(mxNote)
        >>> n.beams
        <music21.beam.Beams <music21.beam.Beam 1/start>>

        >>> stem = EL('<stem>up</stem>')
        >>> mxNote.append(stem)
        >>> n = MP.xmlToSimpleNote(mxNote)
        >>> n.stemDirection
        'up'

        # TODO: beams over rests?
        rV  r   r   stemnoteheadrS  )xmlToDurationrW   r!   Note
xmlToPitchr#   	UnpitchedxmlToUnpitchedr7  
xmlToBeamsr_  r*   r7   stemDirectionr  r'   rn   r   r   r  r  	NoteStyle	stemStylexmlNoteheadxmlNoteToGeneralNoteHelper)rv   rG  rT  dmxUnpitchedrO  beamListmxStemry  this_note_style
mxNoteheads              r;   rC  zMeasureParser.xmlToSimpleNote  sB   Z v& kk+.		1%AOOFAGG,*AQ/>>&)ooh/AGV$$kk//1AO}}!KKM	fi0  3"#&&!''"B,5) [[,
!Q
+ ..q&|.TTr=   c                   |t        j                         }n|}| j                  ||       | j                  ||dd       t	        |j
                  t              r|j
                  j                         }nd}|dk(  rd|_        nQ|dk(  rd|_        nD|dk(  rd|_        n7|dk(  rd	|_        d
|_	        n#|dk(  rd	|_        d|_	        nt        d| d      ||S y)au  
        given an mxBeam object return a :class:`~music21.beam.Beam` object

        >>> from xml.etree.ElementTree import fromstring as EL
        >>> MP = musicxml.xmlToM21.MeasureParser()

        >>> mxBeam = EL('<beam>begin</beam>')
        >>> a = MP.xmlToBeam(mxBeam)
        >>> a.type
        'start'

        >>> mxBeam = EL('<beam>continue</beam>')
        >>> a = MP.xmlToBeam(mxBeam)
        >>> a.type
        'continue'

        >>> mxBeam = EL('<beam>end</beam>')
        >>> a = MP.xmlToBeam(mxBeam)
        >>> a.type
        'stop'

        >>> mxBeam = EL('<beam>forward hook    </beam>')
        >>> a = MP.xmlToBeam(mxBeam)
        >>> a.type
        'partial'
        >>> a.direction
        'right'

        >>> mxBeam = EL('<beam>backward hook</beam>')
        >>> a = MP.xmlToBeam(mxBeam)
        >>> a.type
        'partial'
        >>> a.direction
        'left'

        >>> mxBeam = EL('<beam>crazy</beam>')
        >>> a = MP.xmlToBeam(mxBeam)
        Traceback (most recent call last):
        music21.musicxml.xmlObjects.MusicXMLImportException:
             unexpected beam type encountered (crazy)
        Nfanbeginr\  continueendr]  zforward hookpartialr   zbackward hookr   z"unexpected beam type encountered ())r   Beamr   r   rm   r*   r  r7   r[  r   r/   )rv   mxBeamr   beamOutmxTypes        r;   	xmlToBeamzMeasureParser.xmlToBeam  s    V iikGG 	fg&>fkk3'[[&&(FFW"GLz!%GLu_!GL~%$GL 'G&$GL &G),NvhVW*XYYN r=   c                    |t        j                         }n|}t        |      D ];  \  }}| j                  |      }|dz   |_        |j
                  j                  |       = ||S y)a  
        given a list of mxBeam objects, sets the beamsList

        >>> from xml.etree.ElementTree import fromstring as EL
        >>> MP = musicxml.xmlToM21.MeasureParser()

        >>> mxBeam1 = EL('<beam>begin</beam>')
        >>> mxBeam2 = EL('<beam>begin</beam>')
        >>> mxBeamList = [mxBeam1, mxBeam2]
        >>> b = MP.xmlToBeams(mxBeamList)
        >>> b
        <music21.beam.Beams <music21.beam.Beam 1/start>/<music21.beam.Beam 2/start>>
        Nr   )r   r`  r  r  r   	beamsListr   )rv   
mxBeamListr   beamsOutrD  r  beamObjs          r;   rv  zMeasureParser.xmlToBeamsh  sl     zz|HH":.IAvnnV,GUGN%%g. /
 O r=   c                Z   |j                   dvr|j                   |_        |j                  d      }|t        j                  |      |_        |j                  d       |j                  d      |j                  _        |j                  d      }|t        j                  |      |_        yy)a  
        Set notehead information from the mxNotehead object

        >>> from xml.etree.ElementTree import fromstring as EL
        >>> MP = musicxml.xmlToM21.MeasureParser()

        >>> n = note.Note()
        >>> nh = EL('<notehead color="#FF0000" filled="no" parentheses="yes">'
        ...         'diamond</notehead>')

        >>> MP.xmlNotehead(n, nh)
        >>> n.notehead
        'diamond'
        >>> n.noteheadFill
        False
        >>> n.noteheadParenthesis
        True
        >>> n.style.color
        '#FF0000'
        )r?   NfilledNr   parentheses)	r*   rp  rq   r,   rs   noteheadFillr'   r   noteheadParenthesis)rv   rO  r  nhfnhps        r;   rz  zMeasureParser.xmlNotehead  s    , ??*,#AJnnX&?'66s;AN>>'".&NN73AGGM nn]+?$.$=$=c$BA! r=   c                   t         }|t        j                         }n|}|j                  dk(  r|}n|j	                  d      }||S  |||dd        |||ddt
               |j	                  d      }d}t        |      x}rt        |      }|j	                  d      }	d}
t        |	      x}r|}
|
X	 | j                  |	      }||_	        d|j                  _
        |*||j                  k7  r|j                  dt        |             |S |,	 t        j                  |      |_	        d|j                  _
        |S # t        j                  $ r Y |S w xY w# t        j                  $ r t        d	| d
|       w xY w)aq  
        Given a MusicXML Note object, set this Pitch object to its values.

        >>> from xml.etree.ElementTree import fromstring as EL
        >>> MP = musicxml.xmlToM21.MeasureParser()

        >>> b = EL('<note><pitch><step>E</step><alter>-1</alter>'
        ...        '<octave>3</octave></pitch></note>')
        >>> a = MP.xmlToPitch(b)
        >>> print(a)
        E-3

        Conflicting alter and accidental -- alter is still stored, but name is :

        >>> b = EL('<note><pitch><step>E</step><alter>-1</alter><octave>3</octave></pitch>'
        ...        '<accidental>sharp</accidental></note>')
        >>> a = MP.xmlToPitch(b)
        >>> print(a)
        E#3
        >>> a.fullName
        'E-sharp in octave 3'

        >>> a.accidental.alter
        -1.0

        >>> a.accidental.name
        'sharp'

        >>> a.accidental.modifier
        '#'
        Nr#   stepoctaverS   alter
accidentalTzincorrect accidental z for pitch F)r_   r#   Pitchr\   rW   rN   rC   rM   xmlToAccidentalr  displayStatusr  setAttributeIndependentlyAccidentalException
Accidentalr/   )rv   rG  r   r   rO  mxPitchmxAlteraccAlter	alterTextmxAccidentalmxAccidentalNameaccidentalTextaccObjs                r;   rs  zMeasureParser.xmlToPitch  s   @ (AA:: Gkk'*GQ(Q8s;,,w'$W--9-Y'H{{<0),77>7  .'
--l;%-1*'H,D44WeHoN  !F$//9
 */ALL& ,,   ,, F-+H:[DF FFs   3AD: E :EE%E9c                    |t        j                         }n|}|j                  d      }|j                  d      }t        |      x}r||_        t        |      x}rt        |      |_        |S )a  
        Set `displayStep` and `displayOctave` from `mxUnpitched`.

        >>> from xml.etree.ElementTree import fromstring as EL
        >>> MP = musicxml.xmlToM21.MeasureParser()
        >>> MP.divisions = 10080

        >>> mxNote = EL('<note><duration>7560</duration><type>eighth</type></note>')
        >>> unpitched = EL('<unpitched>'
        ...                '<display-step>E</display-step>'
        ...                '<display-octave>5</display-octave>'
        ...                '</unpitched>')
        >>> mxNote.append(unpitched)
        >>> n = MP.xmlToSimpleNote(mxNote)
        >>> n.displayStep
        'E'
        >>> n.displayOctave
        5
        >>> n.displayPitch().midi
        76
        display-stepdisplay-octave)r!   rt  rW   rC   displaySteprN   displayOctave)rv   r}  r   unpmxDisplayStepmxDisplayOctavedisplayStepTextdisplayOctaveTexts           r;   ru  zMeasureParser.xmlToUnpitched  sw    4 .."CC#((8%**+;<*=99?9-CO ,_ === #$5 6C
r=   c                   |t        j                         }n|}	 t        |      j                         }|| j
                  v r| j
                  |   }n|}|j                  |d       | j                  ||       |j                  d      }|j                  d      }|dk(  r|dk(  rd|_	        n|dk(  rd|_	        n|dk(  rd|_	        | j                  ||       |S # t        $ r |cY S w xY w)am  
        >>> from xml.etree.ElementTree import fromstring as EL
        >>> MP = musicxml.xmlToM21.MeasureParser()

        >>> a = EL('<accidental parentheses="yes">sharp</accidental>')
        >>> b = MP.xmlToAccidental(a)
        >>> b.name
        'sharp'
        >>> b.alter
        1.0
        >>> b.displayStyle
        'parentheses'

        >>> a = EL('<accidental>half-flat</accidental>')
        >>> b = pitch.Accidental()
        >>> unused = MP.xmlToAccidental(a, b)
        >>> b.name
        'half-flat'
        >>> b.alter
        -0.5

        >>> a = EL('<accidental bracket="yes">sharp</accidental>')
        >>> b = MP.xmlToAccidental(a)
        >>> b.displayStyle
        'bracket'

        >>> a = EL('<accidental bracket="yes" parentheses="yes">sharp</accidental>')
        >>> b = MP.xmlToAccidental(a)
        >>> b.displayStyle
        'both'
        T)allowNonStandardValuer  bracketr   both)r#   r  rC   lowerr@   r   r  r   rq   displayStyler   )rv   r  r   accmxNamery  r  r  s           r;   r  zMeasureParser.xmlToAccidental"  s   H ""$CC	!,/557F T///--f5DD 	D1<- #&&}5""9-%Gu$4%CE!,C(C 	,,
5  	J	s   C C('C(c                v   | j                  |      }t        j                  |      }|j                  d      }|t	        d      |j                  d      }|dk(  r.t        |j                  d            x}r|dv rd| _        d|_        | j                  r| j                  j                  |       |j                  d	      }t        |      x}r|j                  d
      }	t        |	      x}
r||
j                         z  }t        j                  |      }| j                  |      xs d}	 | j                  |   }|t!        |t"        j$                        st"        j&                  }n|j(                  dz   }|j,                  |z
  |_        | j1                  ||      S # t*        $ r t"        j&                  }Y Aw xY w)aa  
        Takes a <note> tag that has been shown to have a <rest> tag in it
        and return a rest.

        >>> from xml.etree.ElementTree import fromstring as EL
        >>> MP = musicxml.xmlToM21.MeasureParser()
        >>> MP.divisions = 10

        >>> mxr = EL('<note><rest/><duration>5</duration><type>eighth</type></note>')
        >>> r = MP.xmlToRest(mxr)
        >>> r
        <music21.note.Rest eighth>
        >>> r.duration.quarterLength
        0.5

        Rests that have display-step and display-octave get converted to rests
        with `stepShift` set.  For instance, this rest appears where G4 normally is:

        >>> mxr_positioned = EL(
        ...     '<note><rest>'
        ...     '<display-step>G</display-step>'
        ...     '<display-octave>4</display-octave>'
        ...     '</rest>'
        ...     '<duration>5</duration><type>eighth</type></note>'
        ... )
        >>> r = MP.xmlToRest(mxr_positioned)
        >>> r
        <music21.note.Rest eighth>

        A rest normally lies at B4 in treble clef, so G4 is shifted down two steps.

        >>> r.stepShift
        -2

        Note that clef context matters.  If the last clef for the staff were BassClef
        this would be a very high clef.  (Rests without a `<staff>` set read the clef
        from staff number 1, or treble clef if no pitch clef has been assigned).

        If the last clef for staff 1 were bass clef, this would be a very high rest.

        >>> MP.lastClefs[1] = clef.BassClef()
        >>> MP.xmlToRest(mxr_positioned).stepShift
        10

        Test full measure rest defined with measure="yes" and a duration indicating
        four quarter notes:

        >>> mxr = EL('<note><rest measure="yes"/><duration>40</duration></note>')
        >>> r = MP.xmlToRest(mxr)
        >>> MP.fullMeasureRest
        True

        Note that here set `r`'s `.fullMeasure` to True or always because it has no type.

        >>> r.fullMeasure
        True

        Same goes for rests which define type of whole (or breve), regardless of duration:

        >>> mxr = EL('<note><rest measure="yes"/><duration>40</duration><type>whole</type></note>')
        >>> r = MP.xmlToRest(mxr)
        >>> MP.fullMeasureRest
        True
        >>> r.fullMeasure
        True

        But a rest that defines `measure="yes"` but has a type other than whole or breve
        will set MeasureParser to fullMeasureRest but not set fullMeasure = True
        on the music21 Rest object itself because pickup measures often use
        measure="yes" in Finale, but display as quarter rests, etc.
        See https://github.com/w3c-cg/musicxml/issues/478

        >>> mxr = EL('<note><rest measure="yes"/><duration>10</duration>'
        ...          '<type>quarter</type></note>')
        >>> r = MP.xmlToRest(mxr)
        >>> MP.fullMeasureRest
        True
        >>> r.fullMeasure
        'auto'

        OMIT_FROM_DOCS

        Check that the `<staff>` element for rests is properly read.

        >>> MP2 = musicxml.xmlToM21.MeasureParser()
        >>> MP2.divisions = 10
        >>> MP2.lastClefs[1] = clef.BassClef()
        >>> MP2.lastClefs[2] = clef.AltoClef()

        >>> staff2 = EL('<staff>2</staff>')
        >>> mxr_positioned.append(staff2)
        >>> r = MP2.xmlToRest(mxr_positioned)

        From treble is -2, but +6 for treble to alto clef conversion = 4

        >>> r.stepShift
        4
        r   r  z@do not call xmlToRest on a <note> unless it contains a rest tag.rU  r   r[  r  Tr  r  r      )rq  r!   r  rW   r/   rq   rC   r  r  rU  r  r7   r#   r  r  r  rm   r	   	PitchClefTREBLE_MID_LINE_DNN
lowestLiner<  diatonicNoteNum	stepShiftr{  )rv   mxRestr|  r  	mxRestTagisFullMeasurerTypedsds_textdodo_texttempP	restStaffcc	ccMidLines                  r;   rD  zMeasureParser.xmlToResti  s   H v&IIq!KK'	) +C D D!i0E!)&++f*=>>E>5L^C^'+$ $ ;;KK--a0^^N+"2&&7& 01B&r**w*7==?*KK(E ++F38qI5^^I.:ZDNN%C $ 8 8I " 1I  //);AK..q&99  5 44	5s   +AF F87F8c                   | j                   }|du r|j                  |       | j                  ||       | j                  ||       |j	                  d      }|+|j
                  st        |      dz  }||j                  _        |j	                  d      dk(  r-|j                  j                  t        j                                |j                  d      }d}|Gd}|j                  d      }	|	t        j                  |d	       | j                  ||j                          |j                  d      }
|
| j#                  |
|d
d       |j                  d      | j%                  |      |_        |du r| j)                  ||      }|j+                  d      }|D ]  }| j-                  ||        | j/                  ||       |S )a  
        Combined function to work on all <note> tags, where n can be
        a Note or Rest.

        >>> from xml.etree.ElementTree import fromstring as EL
        >>> n = note.Note()
        >>> mxNote = EL('<note color="silver"></note>')
        >>> MP = musicxml.xmlToM21.MeasureParser()
        >>> n = MP.xmlNoteToGeneralNoteHelper(n, mxNote)
        >>> n.style.color
        'silver'
        Tr   gmЁ}?	pizzicator   graceFr[  z<type>eighth</type>sizenoteSizer+   	notations)r  rf  r   r   rq   rK  rM   volumevelocityScalarr   r   	PizzicatorW   r   
SubElementrq  r   r   xmlToTier+   xmlGraceToGracer7  xmlNotationsr   )rv   rO  rG  rT  r  dynamPercentage
dynamFloatmxGraceisGrace	graceTyper  mxNotationsmxNs                r;   r{  z(MeasureParser.xmlNoteToGeneralNoteHelper  s    **4==a@ 	61% 	FA& !**Z0&qxx/:>J&0AHH# ::k"e+OO""=#:#:#<= ++g&GF+I  f&;<vqzz2 V$##FAvzB;;u)MM&)AE
 d?$$Wa0A nn[1Cc1%  	&!$r=   c                   d}d}|d}n|}| j                   }|j                  d      }|2t        |j                  j	                               }t        ||z        }	nd}	|j                  d      }
t        |
      x}rOt        |      }d}t        |j                  d            }|j                  d	      }|| j                  |      }nd}nd}d
}|9t        j                  |	      }|j                  |_        |j                  |_        nt        j                  |	      }|st        j                  ||      }|j                   |	k(  r|s||S dS |)|j#                          d|_        |j%                  |       nt        j                  |      }|D ]  }|j'                  |        t)        |j                   |	d      sd|_        |	|_        ||S y)a  
        Translate a `MusicXML` <note> object's
        <duration>, <type>, <dot>, tuplets, etc.
        to a music21 :class:`~music21.duration.Duration` object.

        >>> from xml.etree.ElementTree import fromstring as EL
        >>> MP = musicxml.xmlToM21.MeasureParser()

        >>> MP.divisions = 10080

        >>> mxNote = EL('<note><pitch><step>D</step>'
        ...             '<alter>-1</alter><octave>6</octave></pitch>'
        ...             '<duration>7560</duration>'
        ...             '<type>eighth</type><dot/></note>')

        >>> c = duration.Duration()
        >>> MP.xmlToDuration(mxNote, c)
        >>> c
        <music21.duration.Duration 0.75>
        >>> c.quarterLength
        0.75
        >>> c.type
        'eighth'
        >>> c.dots
        1

        If the `<duration>` doesn't match the `<type>` and `<dots>`,
        an unlinked duration is created so that `.quarterLength` agrees with
        `<duration>` but the notated types can still be represented.

        Create a second dot on `mxNote` and parse again, observing the identical
        `quarterLength`:

        >>> from xml.etree.ElementTree import SubElement
        >>> unused = SubElement(mxNote, 'dot')
        >>> c2 = MP.xmlToDuration(mxNote)
        >>> c2
        <music21.duration.Duration unlinked type:eighth quarterLength:0.75>
        >>> c2.quarterLength
        0.75
        >>> c2.type
        'eighth'
        >>> c2.dots
        2

        Grace note durations will be converted later to GraceDurations:

        >>> mxDuration = mxNote.find('duration')
        >>> mxNote.remove(mxDuration)
        >>> mxGrace = SubElement(mxNote, 'grace')
        >>> MP.xmlToDuration(mxNote, inputM21=c2)
        >>> c2
        <music21.duration.Duration unlinked type:eighth quarterLength:0.0>
        >>> gn1 = note.Note(duration=c2)
        >>> gn2 = MP.xmlGraceToGrace(mxGrace, gn1)
        >>> gn2.duration
        <music21.duration.GraceDuration unlinked type:eighth quarterLength:0.0>
        r   r   Nr   r3  r[  Fdottime-modificationTr&  )durationTuplegHz>)abs_tol)r  rW   rM   r*   r7   r   rC   rK   lenr7  xmlToTupletsr   Duration
componentsr  durationTupleFromTypeDotsr  clearaddDurationTupleappendTupletr   linked)rv   rG  r   numDotsr  r|  r  r#  noteDivisionsqLenr  typeStrdurationTypeforceRawmxTimeModificationdurRawdttups                     r;   rq  zMeasureParser.xmlToDurationS  s   x AANN	[[,
!!*//"7"7"9:M-)34DDV$"6**7*-g6LH &../0G "(-@!A!-++F3   LH =&&T:F!,,ALAI !!5A33L'JB  D(' %,q6$6}		""2&%%B7s# 
 1??D$? "&H r=   c                   |j                         }|j                  d      dv rd|j                  _        nd|j                  _        	 t	        |j                  d            dz  |j                  _        	 t	        |j                  d            dz  |j                  _        |S # t        $ r Y :w xY w# t        $ r Y |S w xY w)z
        Given a completely formed, non-grace Note or Chord that should become one
        create and return a m21 grace version of the same.
        slash)r   NTFzsteal-time-previousd   zsteal-time-following)getGracerq   r   r  rN   stealTimePreviousru   stealTimeFollowing)rv   r  noteOrChordr  s       r;   r  zMeasureParser.xmlGraceToGrace  s    
 ##%;;w=0"&DMM"'DMM	.1'++>S2T.UX[.[DMM+	/27;;?U3V/WZ]/]DMM,   		
  	 	s$   ,B# 5,B2 #	B/.B/2	B?>B?c                J   |j                  d      }|dk(  rdndfd}d } ||d      D ]9  }| j                  |      } ||       ||j                  j                  |       ;  ||d	      D ]9  }| j	                  |      } ||       ||j                  j                  |       ; |j                  d
      D ]y  }t        j                         }	 ||	       | j                  ||	       |j                  d      }
|
|
|	_	        t        |      x}r||	_        |j                  j                  |	       { dD ]  }|j                  |      D ]  }d}|dk(  rd}n|j                  d      xs d}|j                  d      }|1t        j                  |      }|j                  j                  |       g| j                  j                  t        j                  |d      }|r(t!        j"                  t        j                  |d         }n8t        j                  |      }||_        | j                  j                  |       |j'                  |         d} ||d      D ]%  }|j(                  t*        j,                  v s|j(                  dk(  rp| j/                  ||      }| |j(                  dk(  r|j1                  |        ||       | j                  ||       ||}|j                  j                  |       |j(                  dk(  r<| j3                  ||t        j4                        } ||       | j                  ||       |j(                  dk(  s| j7                  ||      } ||       | j                  ||       ( | j9                  ||       y)a  
        >>> from xml.etree.ElementTree import fromstring as EL
        >>> MP = musicxml.xmlToM21.MeasureParser()

        >>> mxNotations = EL('<notations>'
        ...                   '<fermata type="upright">angled</fermata>'
        ...                 '</notations>')
        >>> n = note.Note()
        >>> MP.xmlNotations(mxNotations, n)
        >>> n.expressions
        [<music21.expressions.Fermata>]
        >>> n.expressions[0].type
        'upright'
        >>> n.expressions[0].shape
        'angled'
        r   r   TFc                .    sy d| j                   _        y )NT)r'   r   )r!  
hideObjects    r;   optionalHideObjectz6MeasureParser.xmlNotations.<locals>.optionalHideObject  s    *.CII'r=   c                N    | j                  |      }t        j                  |      S ri   )r7  r
   flattenList)r:  ry  r7  s      r;   flattenz+MeasureParser.xmlNotations.<locals>.flatten  s!    jj&G%%g..r=   	technicalNr   fermatar[  )
arpeggiatenon-arpeggiatenormalr  znon-arpeggior   r   r   )arpeggioType	ornamentsaccidental-markmostRecentOrnamentz	wavy-linetremolo)rq   xmlTechnicalToArticulationr   r   xmlToArticulationr7  r   Fermatar   r[  rC   shapeArpeggioMarkr  getByClassIdLocalCompleterB  r  r  idLocalr  r\   r,   ORNAMENT_MARKSxmlOrnamentToExpressionresolveOrnamentalPitchesxmlOneSpannerTrillExtensionxmlToTremoloxmlNotationsToSpanners)rv   r  rO  printObjectValuer  r  rA   technicalObjarticulationObjr  fermataTypenotationText	tagSearchr  idFoundarpeggiosbarpeggioSpannerr  r  trillExtObjtremr  s                         @r;   r  zMeasureParser.xmlNotations  sb   & '??>:t#JJ	/	/ [+6E::5AL|,'&&|4	 7 [/:E"44U;O/*&&7	 ; !((3E!))+Gw'k73))F+K&*+E22|2 ,MM  ) 4 :I$,,Y7$, 00#1L#(99[#9#EXL$)IIh$7?*77EHMM((2++EE#77%IB*+&&1P1PRTUVRW*X*5*I*IWc*d29/**11/B#66q9) 8 :0 9=[+6EyyJ555FW9W33.@ 4  &1eiiCT6T&??B"4(!!+t4#)-&MM((.k)"00;;U;UV";/!!+{;i'((2"4(!!+t4/ 74 	##K3r=   c                   |j                   }|t        j                  v rt        j                  |          }t        ||       |dk(  r| j	                  ||       |dv rt        |      rt        |      |_        |dv r	 t        |j                        |_	        |dk(  r| j                  ||       |dv r:|j                  d      )t        j                  |j                  d            |_        |dk(  r| j!                  ||       | j#                  ||       |S t$        j'                  d	| d
| d       y# t        t        f$ r
}Y d}~d}~ww xY w)ac  
        Convert an mxArticulationMark to a music21.articulations.Articulation
        object or one of its subclasses.

        >>> from xml.etree.ElementTree import fromstring as EL
        >>> MP = musicxml.xmlToM21.MeasureParser()

        >>> mxTech = EL('<down-bow placement="below"/>')
        >>> a = MP.xmlTechnicalToArticulation(mxTech)
        >>> a
        <music21.articulations.DownBow>
        >>> a.placement
        'below'

        Fingering might have substitution or alternate

        >>> mxTech = EL('<fingering substitution="yes">5</fingering>')
        >>> f = MP.xmlTechnicalToArticulation(mxTech)
        >>> f
        <music21.articulations.Fingering 5>
        >>> f.substitution
        True
        >>> f.alternate
        False

        FingerNumbers get converted to ints if possible

        >>> f.fingerNumber
        5

        >>> mxTech = EL('<fingering alternate="yes">4-3</fingering>')
        >>> f = MP.xmlTechnicalToArticulation(mxTech)
        >>>
        <music21.articulations.Fingering 4-3>
        >>> f.alternate
        True
        >>> f.fingerNumber
        '4-3'
        	fingering)handbellzother-technical)fretstringNharmonic)heeltoesubstitutionbendCannot translate r  r  )r\   r,   TECHNICAL_MARKSsynchronizeIdshandleFingeringrC   displayTextrN   r*   r   rO   ru   setHarmonicrq   rs   r9  setBendr   r=  r>  )rv   rA   r\   tech
unused_errs        r;   r  z(MeasureParser.xmlTechnicalToArticulationj  sA   P ii*,,,--c24D5$'k!$$T5155,u:M $0#6 (("%ejj/DK j   -o%99^,8(2(A(A%))NB[(\D%f}UD) eT*K##&7uDq$IJ! #I. s   =D4 4EEc                &   |j                  d      }|9|j                  -t        j                  t	        |j                              |_        |j                  d      d|_        |j                  d       |j                  d      j                  |_        |j                  d      H	 t	        |j                  d      j                  d            }t        || j                  z        |_        yy# t        t        f$ r}d|_        Y d}~yd}~ww xY w)	aL  
        Gets the bend amplitude from the bend-alter tag,
        then optional pre-bend and with-bar tags are processed,
        as well as release which is converted from divisions to music21 time.

        Called from xmlTechnicalToArticulation

        >>> from xml.etree.ElementTree import fromstring as EL
        >>> MP = musicxml.xmlToM21.MeasureParser()

        >>> mxTech = EL('<bend><bend-alter>2</bend-alter></bend>')
        >>> a = MP.xmlTechnicalToArticulation(mxTech)
        >>> a
        <music21.articulations.FretBend 0>
        >>> a.bendAlter.semitones
        2
        >>> a.release

        >>> a.withBar

        >>> a.preBend
        False

        >>> mxTech = EL('<bend><bend-alter>-2</bend-alter><pre-bend/></bend>')
        >>> a = MP.xmlTechnicalToArticulation(mxTech)
        >>> a.bendAlter.semitones
        -2
        >>> a.preBend
        True

        >>> mxTech = EL('<bend><bend-alter>-2</bend-alter><release offset="1"/></bend>')
        >>> a = MP.xmlTechnicalToArticulation(mxTech)
        >>> a.bendAlter.semitones
        -2
        >>> a.release
        Fraction(1, 10080)

        >>> mxTech = EL('<bend><bend-alter>-1</bend-alter><with-bar>dip</with-bar></bend>')
        >>> a = MP.xmlTechnicalToArticulation(mxTech)
        >>> a.bendAlter.semitones
        -1
        >>> a.withBar
        'dip'
        z
bend-alterNzpre-bendTzwith-barreleaser  r3  )rW   r*   r   IntervalrM   	bendAlterpreBendwithBarrq   r   r  rE  rO   ru   )rv   mxhr:  r  r  rC  s         r;   rA  zMeasureParser.setBend  s    Z &zz%!)!2!253D!E88J+DL88J+88J/44DL88I*#!#((9"5"9"9("CD	%i$..&@A + 	* #"#s   (AC0 0D?DDc                    | j                  d      d|_        n| j                  d      d|_        | j                  d      d|_        y| j                  d      d|_        y| j                  d      d	|_        yy)
a  
        From the artificial or natural tag (or no tag) and
        zero or one of base-pitch, sounding-pitch, touching-pitch,
        sets .harmonicType and .pitchType on an articulations.Harmonic object

        Called from xmlTechnicalToArticulation

        >>> from xml.etree.ElementTree import fromstring as EL
        >>> MP = musicxml.xmlToM21.MeasureParser()

        >>> mxTech = EL('<harmonic><artificial/><sounding-pitch/></harmonic>')
        >>> a = MP.xmlTechnicalToArticulation(mxTech)
        >>> a
        <music21.articulations.StringHarmonic>

        >>> a.harmonicType
        'artificial'
        >>> a.pitchType
        'sounding'
        
artificialNnaturalz
base-pitchr1   zsounding-pitchsoundingztouching-pitchtouching)rW   harmonicType	pitchType)rJ  harms     r;   r@  zMeasureParser.setHarmonic  s{    , 88L!- ,DXXi , )D88L!-#DNXX&'3'DNXX&'3'DN 4r=   c                ~   |j                   |_        	 t        |j                        |_        |j                  d      )t        j                  |j                  d            |_        |j                  d      *t        j                  |j                  d            |_	        yy# t        t        f$ r
}Y d}~d}~ww xY w)zP
        A few specialized functions for dealing with fingering objects
        Nr9  	alternate)
r*   fingerNumberrN   rO   ru   rq   r,   rs   r9  rT  )rv   rB  rA   rC  s       r;   r>  zMeasureParser.handleFingering  s     "JJ	 #D$5$5 6D 99^$0 * 9 9%))N:S TD99[!-'66uyy7MNDN .	 I& 		s   B# #B<7B<c                   |j                   }|t        j                  v rt        j                  |          }t        ||       | j	                  ||       | j                  ||       |dk(  r|j                  d      }|||_        |S |dv r| j                  ||       |S |dk(  rt        |      x}r	||_
        |S |dk(  rt        |      x}r||_        |S t        j                  d| d| d	       y)
a.  
        Return an articulation from an mxObj, setting placement

        >>> from xml.etree.ElementTree import fromstring as EL
        >>> MP = musicxml.xmlToM21.MeasureParser()

        >>> mxArt = EL('<spiccato placement="above"/>')
        >>> a = MP.xmlToArticulation(mxArt)
        >>> a
        <music21.articulations.Spiccato>
        >>> a.placement
        'above'

        >>> mxArt = EL('<doit dash-length="2" default-x="5" default-y="2" '
        ...            'line-shape="curved" line-type="dashed" space-length="1" />')
        >>> a = MP.xmlToArticulation(mxArt)
        >>> a
        <music21.articulations.Doit>
        >>> a.placement is None
        True
        >>> a.style.dashLength
        2
        >>> a.style.absoluteX
        5
        >>> a.style.lineShape
        'curved'
        zstrong-accentr[  N)doitfalloffplopscoopzbreath-markzother-articulationr;  r  r  )r\   r,   ARTICULATION_MARKSr=  r   r   rq   pointDirectionr   rC   r  r?  r=  r>  )rv   rA   r\   r'  r\  
breathText	otherTexts          r;   r  zMeasureParser.xmlToArticulation   s   8 ii*///(;;C@BO5/2uo6e_5 o%!&6!2!-5CO2 #" <<!!%9 #" %e9L+L:+L)3& #" ,,|E?R2R)2R.7+""##&7uDq$IJr=   r  c                  |j                   }|dk(  r|y| j                  |      }d|_        t        |t        j
                        r'|j                  dd      }|dk(  r||_        y||_        yt        |t        j                  t        j                  f      r||_        y	 |dv r(t        j                  |   t        j                        }nC|d	v r(t        j                  |   t        j                         }nt        j                  |          }| j%                  ||       | j'                  ||       |S # t"        $ r Y yw xY w)
aL  
        Convert mxOrnament into a music21 ornament.

        This only processes non-spanner ornaments.
        Many mxOrnaments are spanners: these are handled elsewhere.

        Returns None if it cannot be converted or is not defined, or if the
        mxObj is an accidental-mark (in which case the accidental is placed
        in the mostRecentOrnament instead).

        Return an articulation from an mxObj, setting placement

        >>> from xml.etree.ElementTree import fromstring as EL
        >>> MP = musicxml.xmlToM21.MeasureParser()

        >>> mxOrn = EL('<inverted-turn placement="above" font-size="24"/>')
        >>> a = MP.xmlOrnamentToExpression(mxOrn)
        >>> a
        <music21.expressions.InvertedTurn>
        >>> a.placement
        'above'
        >>> a.style.fontSize
        24

        If it can't be converted, return None

        >>> mxOrn = EL('<crazy-slide placement="above"/>')
        >>> a = MP.xmlOrnamentToExpression(mxOrn)
        >>> a is None
        True

        If it is 'accidental-mark', add to mostRecentOrnament, and return None

        >>> turn = expressions.Turn()
        >>> turn.lowerAccidental is None
        True
        >>> turn.upperAccidental is None
        True
        >>> mxOrn = EL('<accidental-mark placement="below">flat</accidental-mark>')
        >>> a = MP.xmlOrnamentToExpression(mxOrn, mostRecentOrnament=turn)
        >>> a is None
        True
        >>> turn.lowerAccidental
        <music21.pitch.Accidental flat>
        >>> turn.upperAccidental is None
        True

        Not supported currently: 'vertical-turn'
        r  NTr   abovebelow)zdelayed-turnzdelayed-inverted-turn)delay)turnzinverted-turn)r\   r  r  rm   r   Turnrq   lowerAccidentalupperAccidentalGeneralMordentTrillr  r,   r  r   DEFAULT_DELAYNO_DELAYr<  r   r   )rv   rA   r  r\   accidr   orns          r;   r  z%MeasureParser.xmlOrnamentToExpressionU  sF   n ii##!)&*&:&:5&AE"&E,k.>.>? "';!@	'9>&6
  :?&6  .1K1K[M^M^0_`05"-	?? //4=;V;VW11 //4=;Q;QR //46 	5#&%%
  		s   !A/D6 6	EEc                x   | j                   }g }|t        |      }|j                  dk(  r|j                  d      }|dk(  rt        j
                  }n;|dk(  rt        j                  }n%|dk(  rt        j                  }nt        d| d      |dk7  rC| j                  |d|d	
      }|j                  |       | j                  j                  |d       nN|j                  d      }	| j                  j                  d|	d      }
	 |
d   }d	|_        ||j!                  |       |j                  dv r|j                  d      }|j                  d      }	|dk(  rt#        j$                         }|	|_        |j                  dk(  rd|_        d|_        nO|j                  d      }|t-        |      |_        |j                  d      |_        |j                  d      |_        | j                  j                  |       |j                  |       | j                  j                  |d       n|dk(  r	 | j                  j                  d|	d      d   }d	|_        |j                  dk(  rd|_        d|_        nO|j                  d      |_        |j                  d      }|t-        |      |_        |j                  d      |_        | |j!                  |       nt        d|       |j                  dk(  r9|j                  d      }|j                  d       }|j                  d      }	|d!v rt#        j:                         }d|_        |d"k(  r
d#}d$|_        n	d"}d%|_        |	|_        |xs d&|f|_         | j                  j                  |       |j                  |       | j                  j                  |d       nr|d'v r`| j                  j                  d(|	d      }
	 |
d   }|d*k(  r| j                  j                  |d       n)d	|_        | |j!                  |       nt        d+|       |j                  d,k(  r|j                  d      }|j                  d-      }|j                  d.      }|j                  d/      }|j                  d      }	|d0v rtC        jD                         }|	|_        || jF                  |<   |dk(  r tB        jH                  jJ                  |_&        n$|d1k(  rtB        jH                  jN                  |_&        |d2k(  r tB        jP                  j$                  |_)        n)|d3k(  s|d2k(  rtB        jP                  jT                  |_)        |d2k(  rd	|_+        | j                  j                  |       |j                  |       | j                  j                  |d       |S |d4v rr| j                  j                  d5|	d      }
	 |
d   }|d*k(  r	 |S |d7k(  r:tC        jX                         }| j[                  |||       |j!                  |       |S |d8k(  r| jF                  j                  |d      }|jR                  tB        jP                  jT                  k(  r&||k(  r!tB        jP                  j\                  |_)        |S tC        j^                         }| j[                  |||       |j!                  |       |S |d9k(  r:tC        j`                         }| j[                  |||       |j!                  |       |S |dk(  rd	|_        ||j!                  |       |S t        d:|       |S # t        $ r t        d      w xY w# t        $ r1 t1        j2                  d|j                  z   dz   t4        d       g cY S w xY w# t        $ r t        d)      w xY w# t        $ r t        d6      w xY w);a  
        Some spanners, such as MusicXML wedge, bracket, dashes, pedal,
        and ottava are encoded as MusicXML directions.

        :param mxObj: the specific direction element (e.g. <wedge>).
        :param staffKey: staff number (required for <pedal>)
        :param totalOffset: offset in measure of this direction (required for <pedal>)

        >>> from xml.etree.ElementTree import fromstring as EL
        >>> MP = musicxml.xmlToM21.MeasureParser()
        >>> n1 = note.Note('D4')
        >>> MP.nLast = n1

        >>> len(MP.spannerBundle)
        0
        >>> mxDirectionType = EL('<wedge type="crescendo" number="2"/>')
        >>> retList = MP.xmlDirectionTypeToSpanners(mxDirectionType)
        >>> retList
        [<music21.dynamics.Crescendo>]

        >>> len(MP.spannerBundle)
        1
        >>> sp = MP.spannerBundle[0]
        >>> sp
        <music21.dynamics.Crescendo>

        >>> mxDirectionType2 = EL('<wedge type="stop" number="2"/>')
        >>> retList = MP.xmlDirectionTypeToSpanners(mxDirectionType2)

        retList is empty because nothing new has been added.

        >>> retList
        []

        >>> len(MP.spannerBundle)
        1
        >>> sp = MP.spannerBundle[0]
        >>> sp
        <music21.dynamics.Crescendo <music21.note.Note D>>

        >>> mxDirection = EL('<direction place="below"/>')
        >>> mxDirectionType = EL('<pedal type="sostenuto" sign="yes" number="2"/>')
        >>> retList = MP.xmlDirectionTypeToSpanners(mxDirectionType, 1, 0.5)
        >>> retList
        [<music21.expressions.PedalMark>]
        >>> pedalMark = retList[0]
        >>> pedalMark.pedalType
        <PedalType.Sostenuto>
        >>> pedalMark.pedalForm
        <PedalForm.Symbol>

        >>> mxDirectionType1a = EL('<pedal type="resume" line="yes" number="2"/>')
        >>> retList = MP.xmlDirectionTypeToSpanners(mxDirectionType1a, 1, 0.5)
        >>> retList
        []
        >>> pedalMark.pedalForm
        <PedalForm.SymbolLine>

        >>> mxDirectionType2 = EL('<pedal type="change" line="yes" number="2"/>')
        >>> retList = MP.xmlDirectionTypeToSpanners(mxDirectionType2, 1, 1.0)
        >>> retList
        []

        >>> mxDirectionType3 = EL('<pedal type="discontinue" line="yes" number="2"/>')
        >>> retList = MP.xmlDirectionTypeToSpanners(mxDirectionType3, 1, 2.0)
        >>> retList
        []

        >>> mxDirectionType4 = EL('<pedal type="resume" line="yes" number="2"/>')
        >>> retList = MP.xmlDirectionTypeToSpanners(mxDirectionType4, 1, 3.5)
        >>> retList
        []

        >>> mxDirectionType5 = EL('<pedal type="stop" line="yes" number="2"/>')
        >>> retList = MP.xmlDirectionTypeToSpanners(mxDirectionType5, 1, 4.0)
        >>> retList
        []
        >>> pedalMark.getFirst()
        <music21.expressions.PedalBounce at 1.0>
        >>> pedalMark.getLast() is n1
        True
        >>> MP.stream.elements
        (<music21.expressions.PedalBounce at 1.0>, <music21.expressions.PedalGapStart at 2.0>,
        <music21.expressions.PedalGapEnd at 3.5>)
        Nwedger[  	crescendo
diminuendor]  zUnknown type, r  TallowDuplicateIdsr^  r   DynamicWedgeFr   zError in getting DynamicWedges)r  dashesr\  rt  nonedashedz
end-lengthzline-endr   LinezLine <z> stop without startr4  r5  z"unidentified mxType of mxBracket: octave-shiftr  )updownry  rz  ra  r`     )r  r]  r  zError in getting Ottavar  z%unidentified mxType of octave-shift: pedalabbreviatedlinesign)r\  	sostenutor  r   r   )r  r]  discontinueresumer(  	PedalMarkzError in getting PedalMarkr  r  r(  zunidentified mxType of pedal: )1r  r   r\   rq   r   	Crescendo
Diminuendors  r/   r!  r   r  "setPendingSpannedElementAssignmentr  rB  rC  r  r%   rw  r  	startTickr   rM   startHeightr9  r:  r0   endTick	endHeightr  transposingr   r[  r   r  r  	PedalTypeSustain	pedalType	Sostenuto	PedalForm	pedalFormSymbolr}  PedalGapStartr  
SymbolLinePedalGapEndPedalBounce)rv   rA   r  totalOffset
targetLast
returnListmTypespClassrR  r+  spbr  heightmxSizem21TypemxAbbreviatedmxLinemxSignpgStartpedalStartOffsetpgEndpbs                         r;   xmlDirectionTypeToSpannersz(MeasureParser.xmlDirectionTypeToSpanners  s   x ZZ

" -K99IIf%E#7?7I7I,&"--&"//-ugQ.GHH''tWPT'U!!"%""EEb-X))H-((BB"GU4TQB %)!)))*599--YYv&Fii)G \\^$
99(#)BL"*BK"YY|4F)).v#(99Z#8BL"'))K"8BK""))"-!!"% ""EEb-X6!
++EE0013B %)!99(!'BJ"*BK!&:!6BJ"YY|4F)',V}"'))K"8BK )))*5-0RSYRZ.[\\99&YYv&FYYv&Fii)G'^^% "'T>$G#*BL"G#*BL$
!;Q0""))"-!!"%""EEb-X//((BBguMQB Z'&&II"m\(,B%!---j9-0UV\U].^__99YYv&F!IIm4MYYv&FYYv&Fii)G// **,$
.9''+W$#.#8#8#@#@BL{*#.#8#8#B#BBLU?#.#8#8#=#=BLt^v#.#8#8#?#?BL E)%)BN""))"-!!"%""EEb-X` _ RR((BB%PQB Z' B ? },)779G))+xI))'26 5 x'
 7;6M6M6Q6QRTVZ6[$(=(=(D(DD 0K ?'2'<'<'G'G$  !, 7 7 9--k8UK--e4  x'$002B))+xD))"-  v%(,B%!---j9  .0Nvh.WXX] " T12RSSTF " MM 599,/EE'#$
 Ij " M12KLLMZ " P12NOOPs6   \7 0 ] ^ ^$ 7]7^	^	^!$^9c                T   |j                  d      D ]~  }| j                  ||t        j                        }| j	                  ||       | j                  ||       | j                  ||       | j                  ||d       | j                  ||        |j                  d      D ]#  }| j                  ||t        j                         % |j                  d      D ]#  }| j                  ||t        j                         % dD ]  }|j                  |      D ]  }| j                  ||t        j                        }|dk(  rd|_        | j	                  ||       |j                  d      s|dk(  rd	|_        d	|j                   _        | j#                  ||       t%        ||         y )
Nslur)zbezier-offsetzbezier-offset2zbezier-xzbezier-yz	bezier-x2z	bezier-y2ztechnical/hammer-onztechnical/pull-off)	glissandoslider  
continuousr   solid)r7  r!  r%   Slurr   r   r   r   r   r   HammerOnPullOff	Glissando	slideTyperq   r   r'   r   r=  )rv   r  rO  rA   r  r*  glisss          r;   r$  z$MeasureParser.xmlNotationsToSpanners  s    !((0E%%eQ=DeT*UD)eT*##E$(%?& MM%& 1 !(()>?Eua)?)?@ @ !(()=>Eua)>)>? ? 0I$,,Y7**5!W5F5FG'&2EO!!%/yy-)w2F%,EN+2EKK( ""5%0ue, 8 0r=   c                v   |j                  d      }d}|dv rd}	 t        |j                  j                               }|du r8t        j                         }||_        |j                  j                  |       |S | j                  ||t        j                        }||_        |S # t        t
        f$ r d}Y yw xY w)z
        Converts an mxTremolo to either an expression to be added to n.expressions
        or to a spanner, returning either.
        r[  T)r\  r]  F   )rq   rN   r*   r7   rO   r@   r   TremolonumberOfMarksr   r!  TremoloSpanner)rv   	mxTremolorO  tremoloTypeisSinglenumMarksr  tremSpans           r;   r#  zMeasureParser.xmlToTremolo  s      mmF+++H	9>>//12H t$$&B'BMM  $I)))Q8R8RSH%-H"O N+ 	H	s   #B$ $B87B8Frq  c                  |j                  d      }| j                  j                  ||d      }|r
|du r|d   }nC |       }||_        |j                  d      }|||_        | j                  j                  |       ||j                  |       |j                  d      dk(  r	d|_        |S |j                  d      dk(  rt        ||       |S )	z
        Some spanner types do not have an id necessarily, we allow duplicates of them
        if allowDuplicateIds is True. Wedges are one.

        Returns the new spanner created.
        r   Fr   r   r[  r]  Tr\  )	rq   r  r  r  r   r   r  rC  r=  )	rv   rA   rt  spannerClassrr  r+  r-  sur   s	            r;   r!  zMeasureParser.xmlOneSpanner  s     ))H% 99,QVW#u, ABB BJ		+.I$(%%b) !!&) 99V& $B
 	 YYv')5"%	r=   c                   t        j                         }|j                  d      }|syg }|D ]<  }|j                  d      }||j	                  |       (t
        j                  d       > t        |      dk(  r|d   |_        n'd|v rd|v rd	|_        nt
        j                  d
|g       |j                  d      }||j                  d      }|rz|d   }	t        |	|       |	j                  d      }
|
|
dk7  r|
|_        |	j                  d      }|	||_        |S |d   j                  d      }|dk(  r	d|_        |S |dk(  rd|_        |S )aG  
        Translate a MusicXML <note> with <tie> SubElements
        :class:`~music21.tie.Tie` object

        >>> from xml.etree.ElementTree import fromstring as EL
        >>> MP = musicxml.xmlToM21.MeasureParser()

        Create the incomplete part of a Note.

        >>> mxNote = EL('<note><tie type="start" />'
        ...             '<notations>'
        ...             '<tied line-type="dotted" placement="below" type="start" />'
        ...             '</notations></note>')
        >>> m21Tie = MP.xmlToTie(mxNote)
        >>> m21Tie.type
        'start'
        >>> m21Tie.style
        'dotted'
        >>> m21Tie.placement
        'below'

        Same thing but with orientation instead of placement, which both get mapped to
        placement in Tie objects

        >>> mxNote = EL('<note><tie type="start" />'
        ...             '<notations>'
        ...             '<tied line-type="dotted" orientation="over" type="start" />'
        ...             '</notations></note>')
        >>> tieObj = MP.xmlToTie(mxNote)
        >>> tieObj.placement
        'above'
        r+   Nr[  z'found tie element without required typer   r   r]  r\  r  zPfound unexpected arrangement of multiple tie types when importing from musicxml:r  tiedr   wavyr   orientationoverr`  underra  )r+   Tier7  rq   r   r=  r>  r  r[  rW   r=  r'   r   )rv   rG  tieObjallTies
typesFoundmxTie	foundTyper  
mxTiedList	firstTiedtieStyler   r  s                r;   r  zMeasureParser.xmlToTie:  ss   D ..'
E		&)I$!!),''(QR  z?a$Q-FKz!g&;$FK##./9;< kk+."$,,V4J&qM	y&1$==5'H,>#+FL%MM+6	('0F$  #-Q-"3"3M"BK"f,+2(  %/+2(r=   c                2   t        j                         }|j                  d      }|t        d      t        } |||ddt
                |||ddt
               |j                  d	      }t        |      x}r|}nt        |j                  d
            }t        |      }t        |j                  d            }	|j                  ||	       |j                  d      }
|
|| j                  d<   |j                         }|}dgdz  }t               }t               }|
f|
j                  d      }|D ]O  }|j                  d
      }|j                  d      }|t        |      nd}|dk(  rN| j                  |   >| j                  |   }||v r	|d|_        |j!                  |       |j!                  |       |j                  d      }|j                  d      }||t#        j$                  |      }nt        j                         } |||ddt
                |||ddt
               |j                  d      }|^|j&                  x}P|j)                         }t        |      }t        |j                  d            }t        j*                  ||      |_        |j                  d      }|^|j&                  x}P|j)                         }t        |      }t        |j                  d            }t        j*                  ||      |_        t1        j2                  t0        j4                  d   dz  |      |_        |j                  d      }|t7        j8                  |      |_        |j                  d      }||dk(  rd|_        |d|_        n||dk(  rd|_        |j                  d      } | | d k(  r|j<                  dnd
|_        n1|/|dk(  r*|j<                  dnd
|_        |j>                  dnd
|_        |j                  d!      }!|!|!d"k(  rd#|_        t1        j2                  t0        j4                  d$   |j                  d%            |_         |||<   ||j                         z  }|| j                  |<   R tC        dt        | j                              D ]Y  }"| j                  |"   }#|#|#|v rt#        j$                  |#      }$|"|v rd|$_        nd|$_        ||$j                         z  }|$||"<   [ |dk7  rftE        jF                  |      }%t        j                  |%jH                  |%jJ                        }&|j.                  |&_        |j,                  |&_        |&|d&<   |D ]  }'d| j                  |'<    |D cg c]  }||	 }}|S c c}w )'a=  
        Given an mxNote, based on mxTimeModification
        and mxTuplet objects, return a list of Tuplet objects

        >>> from xml.etree.ElementTree import fromstring as EL
        >>> MP = musicxml.xmlToM21.MeasureParser()

        >>> mxNote = EL('<note><type>16th</type>'
        ...             '<time-modification><actual-notes>5</actual-notes>'
        ...             '<normal-notes>4</normal-notes></time-modification></note>')
        >>> tups = MP.xmlToTuplets(mxNote)
        >>> tups
        [<music21.duration.Tuplet 5/4/16th>]

        >>> mxNote = EL('<note><type>eighth</type>'
        ...             '<time-modification><actual-notes>5</actual-notes>'
        ...             '<normal-notes>3</normal-notes>'
        ...             '<normal-type>16th</normal-type><normal-dot /><normal-dot />'
        ...             '</time-modification></note>')
        >>> tup = MP.xmlToTuplets(mxNote)
        >>> tup
        [<music21.duration.Tuplet 5/3/16th>]
        >>> tup[0].durationNormal
        DurationTuple(type='16th', dots=2, quarterLength=0.4375)
        r  Nz.Note without time-modification in xmlToTupletszactual-notesnumberNotesActualrS   znormal-notesnumberNotesNormalznormal-typer[  z
normal-dotr  r   r{  tupletr   r   r]  	startStopztuplet-actualztuplet-normalztuplet-numberztuplet-typez
tuplet-dot)r\  r]  r  Fr  zshow-numberru  Fr  z	show-typeactualr   curvedr  )r`  ra  r   r0  )&r   TupletrW   r/   r_   rN   rC   rK   r  r7  setDurationTyper  tupletMultiplierr  rq   r[  ra   rn  ro  r*   r7   r  durationActualdurationNormalr  r  Literalr,   rs   r  tupletActualShowtupletNormalShowr   range	fractionsFractiondenominator	numerator)(rv   rG  r  r  r   mxNormalTypenormalTypeTextmusicXMLNormalTypedurationNormalTyper  r  !remainingTupletAmountToAccountFor
timeModTupreturnTupletsremoveFromActiveTupletstupletsToStop	mxTupletsmxTupletthis_tuplet_typetupletNumberStrtupletIndexactiveTmxTupletActualmxTupletNormalmxActualTypexmlActualTypedurTyper  mxNormalTypeTextxmlNormalTypebracketMaybe
showNumbershowType	lineShaperD  
thisActivethisActiveCopyremainderFractionremainderTuplettupletIndexToRemoves(                                           r;   r  zMeasureParser.xmlToTuplets  s   6 oo#[[)<=%)*Z[[ (S$n6IUXYS$n6IUXY)..}=),77>7!/!-fkk&.A!B/0BC(00>?.8kk+.$'Dq! -0,@,@,B)
59FQJ"%% "#++H5I%#+<<#7 "*,,x"86E6Qc/2WX#v-))+6B"&"4"4["A"m38K+6GL/33K@%))+6!)!?!)!?!)^-C --
3C"//+Cn(*=Nn(*=N $2#6#6}#EL$02>2C2C!C P(5(;(;(="4]"C"<#7#7#EF-5-O-OPWY]-^*#1#6#6}#EL$05A5F5F!F!1 S(8(>(>(@"4]"C"<#7#7#EF-5-O-OPWY]-^* 66!)),O"PQU"U"24  (||I6+",";";L"ICK%\\-8
)jF.B+/C(#+&++
f0D+3C(#<<4'H,@585I5I5U6[aC(+
f0D585I5I5U6[aC(585I5I5U6[aC($LL6	(Y(-B"(CK !qyy1A'BHLLQ\D] ^-0k*1S5I5I5KK125"";/U &Z q#d0012A++A.J!]*!]]:6NM!&,#&*#-1P1P1RR--M! 3 -1 ) 2 23T U&oo.?.K.K.?.I.IKO-7-F-FO*-7-F-FO* /M" $;6:D23 $; )6II Js   VVc                    d}|D ]L  }| j                  |      }||j                  dk(  r||_        |j                  j                  |       |dz  }N y)a\  
        Takes a list of <lyric> elements and update the
        note's lyrics from that list.

        >>> from xml.etree.ElementTree import fromstring as EL
        >>> MP = musicxml.xmlToM21.MeasureParser()

        >>> mxLyric1 = EL('<lyric><text>Hi</text><elision/><text>There</text></lyric>')
        >>> mxLyric2 = EL('<lyric><text>Bye</text></lyric>')
        >>> n = note.Note()
        >>> MP.updateLyricsFromList(n, [mxLyric1, mxLyric2])
        >>> n.lyrics
        [<music21.note.Lyric number=1 syllabic=composite text='Hi There'>,
         <music21.note.Lyric number=2 text='Bye'>]
        r   Nr   )
xmlToLyricr   lyricsr   )rv   rO  	lyricListcurrentLyricNumberrP  lyricObjs         r;   rE  z"MeasureParser.updateLyricsFromList;  sY    "  Gw/H!#"4HHOOH%!# !r=   c                r   |t        j                         }n|}|j                  d      }|j                  d      }|j                  d      }|s||S yt        |      dk(  rJ|d   j                  }||j                         |_        	 |d   j                  j                         |_        ng |_	        t        |      D ]  \  }}	t        j                         }
|j                  j                  |
       |	j                  |	j                  j                         |
_        	 ||   }|j                  j                         |
_        |dk\  r||dz
     }|j                  }|d}||
_         |j                  d      }	 t        |      }||_        |j                  d	      }|||_        | j'                  ||d
d       | j)                  ||       | j+                  ||       ||S y# t        t        f$ r Y w xY w# t        t        t        f$ r Y Rw xY w# t"        t        f$ r d|_        |||_        Y w xY w)aG  
        Translate a MusicXML <lyric> tag to a
        music21 :class:`~music21.note.Lyric` object or return None if no Lyric object
        should be created (empty lyric tags, for instance)

        If inputM21 is a :class:`~music21.note.Lyric` object, then the values of the
        mxLyric are transferred there and nothing returned.

        Otherwise, a new `Lyric` object is created and returned.

        >>> from xml.etree.ElementTree import fromstring as EL
        >>> MP = musicxml.xmlToM21.MeasureParser()

        >>> mxLyric = EL('<lyric number="4" color="red">'
        ...              '<syllabic>single</syllabic>'
        ...              '<text>word</text></lyric>')
        >>> lyricObj = note.Lyric()
        >>> MP.xmlToLyric(mxLyric, lyricObj)
        >>> lyricObj
        <music21.note.Lyric number=4 syllabic=single text='word'>
        >>> lyricObj.style.color
        'red'

        Non-numeric MusicXML lyric "number"s are converted to identifiers:

        >>> mxLyric.set('number', 'part2verse1')
        >>> l2 = MP.xmlToLyric(mxLyric)
        >>> l2
        <music21.note.Lyric number=0 identifier='part2verse1' syllabic=single text='word'>

        Multiple texts can be created and result in composite lyrics

        >>> mxBianco = EL('<lyric>'
        ...               '<syllabic>end</syllabic>'
        ...               '<text>co</text>'
        ...               '<elision>_</elision>'
        ...               '<syllabic>single</syllabic>'
        ...               '<text>e</text>'
        ...               '</lyric>')
        >>> bianco = MP.xmlToLyric(mxBianco)
        >>> bianco
        <music21.note.Lyric number=0 syllabic=composite text='co_e'>
        >>> bianco.components
        [<music21.note.Lyric number=1 syllabic=end text='co'>,
         <music21.note.Lyric number=1 syllabic=single text='e'>]
        Nr*   syllabicelisionr   r   r?   r   ry  )r   r   r   )r   r   r   )r!   Lyricr7  r  r*   r7   r
  rO   rB  r  r  r   elisionBeforer@   rq   rN   r   ru   
identifierr   r   r   )rv   rP  r   lytext_elementssyllabic_elementselision_elementselement_textrD  mxText	component
mxSyllabic	mxElisionelision_textr   r  s                   r;   r  zMeasureParser.xmlToLyricV  sI   ` BB  /#OOJ7"??95	}"(+00L'&,,./277==?
 BM&}5	6 JJL	$$Y/;;*%+[[%6%6%8IN "31!5J)3)>)>)@I&Av$4QU$;	 (1~~'/+-L2>	/- 66 X&		'[FBI [[(
!&BM H M	O 	gr""%I o 
+ 8 #J?  :& 	' BI! &	's7   	"G$ AG9;H $G65G69HHH65H6c                    | j                   }| j                  s|j                  | j                  |       y|j	                  d      }| j                  |      }||}|j                  | j                  |       y)z
        Adds an object to a measure or a voice.  Needs a note element (obviously)
        but also mxNote to get the voice.  Uses coreInsert and thus leaves insertStream
        on the inner voice in an unusable state.
        Nr@  )r&   r]  r5  r   rW   findM21VoiceFromXmlVoice)rv   	mxElementr  insertStreammxVoice	thisVoices         r;   r'  z$MeasureParser.insertInMeasureOrVoice  sn     {{~~##D$:$:B?..)
 11':	 $L 6 6;r=   c                   | j                   }t        |      rt        |      }	 t        |      | _        n,| j                  }|t        j                  dt        d       d}d}|| j                  v r| j                  |   }|S t        |      | j                  v r| j                  t        |         }|S t        |      | j                  v r| j                  t        |         }|S t        j                  d|dt        d       t        j                  dt        | j                         t        d       t        j                  d	t        |j                         d
|j                   t        d       |S # t        $ r || _        Y )w xY w)zJ
        Find the stream.Voice object from a <voice> tag or None.
        NzmCannot put in an element with a missing voice tag when no previous voice tag was given.  Assuming voice 1... r4  r5  r   zCannot find voice z; putting outside of voices.zCurrent voiceIds: zCurrent voices: z in m. )r&   rC   rN   r  rO   r9  r:  r0   r  r  r:  r  r   )rv   r  r  useVoicer  s        r;   r  z&MeasureParser.findM21VoiceFromXmlVoice  st    KK #G,H*!$X ~~H O#3 '+	t&1I   ]doo-H6I  ]doo-H6I  MM$XL0LMA/ MM$T$//%:$;<A/ MM"4>"2'!((DA/ 9  *!)*s   E7 7F
Fc                   | j                   }|j                  d      }|| j                  |      }n| j                  |      }|j                  d      }|| j                  j                  t        j                        j                  d      }|s1t        j                  |      }| j                  j                  |       n|d   }|j                  |       |j                  d      dk(  re|j                  d      }	 ||_        |j                  }	|	>|	|_        t!        j"                  d
|	      }
|
rt%        |
j'                  d	            |_        |j                  d      dv rd|_        |j*                  dk(  r||_        y|j*                  dk(  r||_        y|j1                          |j                  |       y# t        j                  $ r
 d	|_        Y w xY w)zu
        Handles everything for putting a barline into a Stream
        and updating repeat characteristics.
        r$   NendingFr   r[  r\  r   r   z
^(\d+)\.?$)r]  r  Tr   r   )r&   rW   xmlToRepeatxmlToBarliner  rA  r%   ri  rD  r   r  rq   r   SpannerExceptionr*   overrideDisplayrematchrN   grouprC  locationleftBarlinerightBarlinerF  )rv   	mxBarliner  mxRepeatObjr  mxEndingObj
rbSpannersrbmxNumberendingNumberTextoverrideNumbers              r;   r  zMeasureParser.xmlBarline  s   
 KKnnX."&&y1G''	2G  nnX." ++66%%!!%(  **1-""))"-
  ]%%a(v&'1&??84" (BI $/#3#3 #/)9B&%'XXm=M%NN%$'(<(<Q(?$@	 v&*AA$(! v%#AM($AN!!#HHW9 // " !BI"s   >F; ;GGc                   |t        j                         }n|}t        } |||dd       | j                  ||       |j	                  d      }|||_        nd|_        |j                  d      }|t        j                  d      |j	                  d      }|t        d	      |j                         d
k(  rd|_
        n1|j                         dk(  rd|_
        nt        j                  d|      |j	                  d       	 t        |j	                  d            |_        ||S y# t         j                  $ r Y w xY w)a  
        Given an mxBarline (not an mxRepeat object) with repeatObj as a parameter,
        file the necessary parameters and return a bar.Repeat() object

        >>> from xml.etree.ElementTree import fromstring as EL
        >>> MP = musicxml.xmlToM21.MeasureParser()

        >>> mxBarline = EL('<barline><bar-style>light-heavy</bar-style>'
        ...                '<repeat direction="backward"/></barline>')
        >>> r = MP.xmlToRepeat(mxBarline)
        >>> r
        <music21.bar.Repeat direction=end>

        Test that the music21 type for a backwards repeat is called "final"
        (because it resembles a final barline) even though the musicxml style
        is called light-heavy.

        >>> r.type
        'final'
        >>> r.direction
        'end'

        Test that a forward repeat with times doesn't raise an exception, and
        that the resulting Repeat doesn't have times set.

        >>> mxStartBarline = EL('<barline><bar-style>light-heavy</bar-style>'
        ...                     '<repeat direction="forward" times="2"/></barline>')
        >>> rs = MP.xmlToRepeat(mxStartBarline)
        >>> rs
        <music21.bar.Repeat direction=start>
        N	bar-styler[  r*  r   r$   zPattempting to create a Repeat from an MusicXML bar that does not define a repeatr   z!Repeat sign direction is requiredr  r\  backwardr  z"cannot handle mx direction format:times)r   Repeatr_   r   rq   r*  rW   BarExceptionr/   r  r   rN   r8  )rv   r-  r   r  r   r*  mxRepeatmxDirections           r;   r#  zMeasureParser.xmlToRepeatj  sL   B 

AA'Q	;/)Q' ==,!AJ AJ>>(+"" $I J J ll;/)*MNN)+!AK J.AK""#GUU<< ,hll734
 H 	 ## s   D' 'D=<D=c                    |t        j                         }n|}t        } |||dd       |j                  d      }|||_        nd|_        ||S y)a7  
        Given an mxBarline, fill the necessary parameters

        >>> from xml.etree.ElementTree import fromstring as EL
        >>> MP = musicxml.xmlToM21.MeasureParser()

        >>> mxBarline = EL('<barline location="right">'
        ...                '<bar-style>light-light</bar-style>'
        ...                '</barline>')
        >>> b = MP.xmlToBarline(mxBarline)
        >>> b
        <music21.bar.Barline type=double>
        >>> b.type  # music21.type is different from musicxml.style
        'double'
        >>> b.location
        'right'
        Nr6  r[  r*  r   )r   Barliner_   rq   r*  )rv   r-  r   br   r*  s         r;   r$  zMeasureParser.xmlToBarline  s_    & AA'Q	;/==,!AJ AJH r=   c                    | j                  |      }| j                  |      }| j                  | j                  |z   ||       y)z\
        Create a ChordSymbol object and insert it to the core and staff reference.
        N)xmlToChordSymbolxmlToOffsetr  r   )rv   	mxHarmonyhchordOffsets       r;   r  zMeasureParser.xmlHarmony  sD    
 !!),&&y1d44{B'	,r=   c                `   d}d}d}d}d}|j                  d      }t        |      x}r|}|j                  d      }	|j                  d      }
|
|
j                  d      }|t        d      t        j                  |j
                        }|
j                  d      }|1|j
                  x}#t        j                  t        |            |_        |j                  d	      }t        |      x}rt        |      }|rZt        j                  r|J |t        j                  v rt        j                  |   }|j                  d
      xs d}|dk(  r|dk7  s|}|j                  d      }||j                  d      }t        j                  r|J |j
                  }|dv r|j                  d
      }|Wt        j                  |      }|j                  d      }|/t        |j
                        }t        j                  |      |_        |	t        j                   }n&|dk(  rt        j"                  }nt        j$                  } ||||||      }t&        }| |||dd       |j)                  d      }|D ]m  }t        j*                         } |||ddt               |j,                  t        d       |||ddt                |||dd       |j/                  |d       o | j1                  ||       | j3                  ||       | j5                  ||       | j7                  ||       |		 |S )a  
        Convert a <harmony> tag to a harmony.ChordSymbol object:

        >>> from xml.etree.ElementTree import fromstring as EL
        >>> MP = musicxml.xmlToM21.MeasureParser()

        >>> elStr = '<harmony><root><root-step>D</root-step><root-alter>-1</root-alter>'
        >>> elStr += '</root><kind>major-seventh</kind></harmony>'
        >>> mxHarmony = EL(elStr)

        >>> cs = MP.xmlToChordSymbol(mxHarmony)
        >>> cs
        <music21.harmony.ChordSymbol D-maj7>

        >>> cs.figure
        'D-maj7'

        >>> cs.pitches
        (<music21.pitch.Pitch D-3>,
         <music21.pitch.Pitch F3>,
         <music21.pitch.Pitch A-3>,
         <music21.pitch.Pitch C4>)

        >>> cs.root()
        <music21.pitch.Pitch D-3>

        TODO: this is very classically-oriented.  Make more Jazz/Rock like possible/default?.

        >>> mxHarmony.find('kind').text = 'major-sixth'
        >>> cs = MP.xmlToChordSymbol(mxHarmony)
        >>> cs
        <music21.harmony.ChordSymbol D-6>

        >>> cs.figure
        'D-6'

        >>> cs.pitches
        (<music21.pitch.Pitch D-3>, <music21.pitch.Pitch F3>,
         <music21.pitch.Pitch A-3>, <music21.pitch.Pitch B-3>)

        >>> cs.root()
        <music21.pitch.Pitch D-3>
        Nr?   kindframebassz	bass-stepzbass-step missingz
bass-alter	inversionr*   ru  rootz	root-steprV   z
root-alter)rI  rK  rJ  rG  kindStrfunctionromanNumeraldegreezdegree-valuerS   zdegree-value missingzdegree-alterr   zdegree-typemodTypeT)updatePitches)rW   rC   r/   r#   r  r*   r  rM   r  rN   r  r  r   CHORD_ALIASESrq   r(   ChordWithFretBoardNoChordChordSymbolr_   r7  ChordStepModificationrO  addChordStepModificationr   r   r   r   )rv   rC  r?  r  rJ  	chordKindchordKindStrmxKind
mxKindTextmxFramemxBassbassStepmxBassAlterr  mxInversioninversionTextmxRootmxRSrootTextmxRootAlter
alterFloatcs_classcsr   	mxDegreesmxDegreehds                              r;   rA  zMeasureParser.xmlToChordSymbol  s0   f #""		'%f--:-"I..)$-NN6$:{{;/H-.ABBHMM*A ++l3K&9I9I,II+V$//i0@A  nn[1(55=5 M*I )))G111#11)<	F+1rJ"$f)<) ';;{+D'''yyH:%88F+#KK)$kk,7*!&{'7'7!8J#(#3#3J#?AL
  33H& H**H 
 (>Y
N;%%h/	!H..0BX~x3Gyy -.DEEX~zSIX}i8''$'? " 	)R(9b)Ir* 	)R(  	r=   c                |   | j                  |      }t        || j                  z         }| j                  |      }d}|j	                  d      D ]/  }|D ](  }| j                  ||||       |j                  dk(  s'd}* 1 |s;|j	                  d      D ]&  }d|j                  vr| j                  ||||        y yy)z
        convert a <direction> tag to one or more expressions, metronome marks, etc.
        and add them to the core and staffReference.
        Fzdirection-type	metronomeTr  r)   N)	rB  rM   r   r  r7  setDirectionInDirectionTyper\   r  setSound)	rv   r<  offsetDirectionr  r  metronome_added	mxDirTypemxSpecificDirectionTagmxSounds	            r;   r  zMeasureParser.xmlDirection  s     **;7Od.D.DDE
 &&{3 %,,-=>I*3&001G1<191<> *--<&*O +4 ? &..w7'..0g)&)+  8 r=   c                   |j                   }|dk(  r|D ]  }| j                  |||||        y |dv rd	 | j                  |||      }|D ]J  }	| j                  ||	       | j                  ||	       | j                  ||	       | j                  ||	       L y |dv rr|dk(  rt        j                         }
nt        j                         }
t        ||
       | j                  ||
       | j                  |||
       | j                  ||
       y |d	k(  rE| j!                  |      }t#        ||d
d
       | j                  |||       | j                  ||       y |dk(  rJ| j%                  |      }| j'                  ||d
       | j                  |||       | j                  ||       y |dk(  r}| j)                  |      }t#        ||d
d
       |j+                         }|&| j                  |||       | j                  ||       y | j                  |||       | j                  ||       y y # t        $ r/}t	        j
                  d| d| t        d       g }Y d }~d }~ww xY w)Nr   )rn  r  rt  rx  r|  zCould not import z: r4  r5  )codasegnorw  rm  r   	rehearsalwords)r\   setDynamicsDirectionr  r/   r9  r:  r0   r   r   r   r   r$   SegnoCodar=  r  xmlToTempoIndicationr   xmlToRehearsalMarkr   xmlToTextExpressiongetRepeatExpression)rv   mxDirr<  r  r  r\   mxDynspannerListexceprR  rS  mmrm_gentextExpressionrepeatExpressions                  r;   rn  z)MeasureParser.setDirectionInDirectionType  se   ( ii* ))%X{[  KK!"==8[ "  +!!%,!!%,!!+r2	 " %%g~\\^[[]5"%UB'!!+x<k2.K**51B%b+{KP!!+x<k2.K,,U3F##KE!!+x@k62G^!55e<N &nk;P[\-AAC+ %%k8=MN!!+/?@ %%k8^L!!+~>! G + ! 1#b@/^_` !s   H) )	I!2$II!c                >   |j                   }|dk(  r&|j                  r|j                  j                         }t        j                  |      }t        ||       t        ||dd       | j                  |||       | j                  ||       | j                  ||       y)zN
        Add a single dynamic element to the core and staffReference.
        zother-dynamicr   N)
r\   r*   r7   r   rd  r=  r   r  r   r   )rv   r  r  r<  r  r  m21DynamicTextr|  s           r;   rz  z"MeasureParser.setDynamicsDirection  s     _,"ZZ--/N^,ua !![+{Kk8Q7"+q)r=   c                j    t        |      }t        j                  |      }| j                  ||       |S )a  
        Given an `mxWords`, create a :class:`~music21.expression.TextExpression`
        and set style attributes, fonts, position, etc.

        Calls `setTextFormatting`, which calls `setPrintStyleAlign`.

        >>> from xml.etree.ElementTree import fromstring as EL
        >>> MP = musicxml.xmlToM21.MeasureParser()
        >>> m = EL('<words default-y="17" font-family="Courier" '
        ...           'font-style="italic" relative-x="-6">a tempo</words>')
        >>> te = MP.xmlToTextExpression(m)
        >>> te.content
        'a tempo'
        >>> te.style.relativeX
        -6
        >>> te.style.fontFamily
        ['Courier']
        )rC   r   TextExpressionr   )rv   mxWordswordTexttes       r;   r  z!MeasureParser.xmlToTextExpression)  s3    2  (''1w+	r=   c                j    t        |      }t        j                  |      }| j                  ||       |S )z?
        Return a rehearsal mark from a rehearsal tag.
        )rC   r   RehearsalMarkr   )rv   mxRehearsalrehearsalTextrS  s       r;   r~  z MeasureParser.xmlToRehearsalMarkG  s3     %[1&&}5{B/	r=   c                v   g }g }d}|D ]  }|j                   }|dk(  r=t        |j                        }t        j                  |      }|j                  |       Q|dk(  r#|t        d      |xj                  dz  c_        y|dk(  s|j                  }	|	|	j                         dk7  s	 |j                  t        j                  t        |	                    t        |      dkD  rBt        j                         }
t        |      d	k  rt        d
      |d   |
_        |d   |
_        n/t        j$                         }
|r
|d   |
_        |r
|d   |
_        |	 |j+                  d      }||dk(  rd|
_        t/        ||
       | j1                  ||
       | j3                  ||
       |
S # t        $ r Y w xY w)aO  
        Given an mxMetronome, convert to either a TempoIndication subclass,
        either a tempo.MetronomeMark or tempo.MetricModulation.

        >>> from xml.etree.ElementTree import fromstring as EL
        >>> MP = musicxml.xmlToM21.MeasureParser()

        >>> m = EL('<metronome><per-minute>125</per-minute>'
        ...        '<beat-unit>half</beat-unit></metronome>')
        >>> MP.xmlToTempoIndication(m)
        <music21.tempo.MetronomeMark Half=125>

        Metric modulation:

        >>> m = EL('<metronome><beat-unit>long</beat-unit><beat-unit>32nd</beat-unit>'
        ...        '<beat-unit-dot/></metronome>')
        >>> MP.xmlToTempoIndication(m)
        <music21.tempo.MetricModulation
         <music21.tempo.MetronomeMark Imperfect Longa=None>=<music21.tempo.MetronomeMark
                   Dotted 32nd=None>>
        Nz	beat-unit)r[  zbeat-unit-dotz-encountered metronome components out of orderr   z
per-minuter?   r4  zYfound incompletely specified musicxml metric modulation: fewer than two durations definedr   r  r   T)r\   rK   r*   r   r  r   r/   r  r7   r
   rt   rM   rO   r  r)   MetricModulationoldReferentnewReferentMetronomeMarkr   referentrq   r  r=  r   r   )rv   mxMetronomer  	durationsnumbersdActiverA   r\   r  perMinr  parens               r;   r}  z"MeasureParser.xmlToTempoIndicationP  s   . 	 E))Ck!1%**="++>  )'?12abb!$ %&,,.B*>v'='=eFm'LM# !, y>A'')B9~!-9: :
 'q\BN&q\BN $$&B#AJ	'l".~!%{B'K,b)	G & s   ,-F++	F87F8c                    	 t        |j                  d      j                  j                               }|| j                  z  S # t        t
        f$ r Y yw xY w)ax  
        Finds an <offset> inside the mxObj and returns it as
        a music21 offset (in quarterLengths)

        >>> from xml.etree.ElementTree import fromstring as EL
        >>> MP = musicxml.xmlToM21.MeasureParser()
        >>> MP.divisions = 40
        >>> off = EL('<direction><offset>100</offset></direction>')
        >>> MP.xmlToOffset(off)
        2.5

        Returns a float, not fraction, since the inserts will later convert to a Fraction
        if need be.

        >>> MP.divisions = 30
        >>> off = EL('<direction><offset>10</offset></direction>')
        >>> MP.xmlToOffset(off)
        0.33333...
        r  r3  )rM   rW   r*   r7   rO   r@   r  )rv   rA   r  s      r;   rB  zMeasureParser.xmlToOffset  sS    (	5::h/44::<=F && N+ 		s   2A AAc                   | j                   j                  d      }t        j                  |      r4t        j
                  j                  j                  | j                  _        n3t        j
                  j                  j                  | j                  _        | j                          | j                   j                  d      }|t        |      }|| j                  _        yy)a  
        parses the attributes of the <measure> tag.  Not the
        <attributes> tag inside the measure tag.

        calls parseMeasureNumbers(), and gets the width from the width tag.

        # TODO: non-controlling
        # may need to do a format/unit conversion?
        implicitwidthN)rY  rq   r,   rs   r&   enums
ShowNumberNEVERr  DEFAULTparseMeasureNumbersrR   layoutWidth)rv   r  r  s      r;   r  z$MeasureParser.parseMeasureAttributes  s     >>%%j1$$X.%+\\%<%<%B%BDKK"%+\\%<%<%D%DDKK"  """7+"5)E&+DKK# r=   c                .   d| _         || _        |D ]  }|j                  }|| j                  v r"t	        | | j                  |         } ||       ?|dk(  r$t        t        |j                              | _        h|dk(  rt        |j                        | _
        |dk(  s| j                  |      | _         | j                  || j                         | j                  7| j                  | j                  _        | j                  | j                  _        yy)a  
        Parses a single attributes tag (mxAttributes) and sets

        self.attributesAreInternal to False,
        self.activeAttributes to mxAttributes,
        self.parent.activeAttributes to mxAttributes
        and then runs the appropriate attributeTagsToMethods for
        the attribute.

        Also sets `self.divisions` for the current divisions
        (along with self.parent.lastDivisions)
        and `self.transposition` and
        to the current transpose.
        Fr  r  r  N)r  r  r\   attributeTagsToMethodsr  r   rM   r*   r  rN   r  xmlTransposeToIntervalr  r   r&   rU  r	  )rv   mxAttributesmxSubr\   r   s        r;   r  z MeasureParser.parseAttributesTag  s     &+" ,!E))Cd111tT%@%@%EFU#!'ejj(9!: !%**o#%)%@%@%G" "& 	,4;;"(,DKK%+/+@+@DKK( #r=   c                   d}|j                  d      }|t        |j                        }d}|j                  d      }|t        |j                        }d}|j                  d      }|3t        |j                        dz  }|dt        |j                        z  z  }|-|+|dk  r|dz
  }n|dz   }	 t        j                  |||z         }	nH|t        j                  ||z         }	n-|t        j                  |      }	nt        j                  d	      }	t        ||	       |	S # t        j
                  $ rP |dk  r|t        |dz  dz        z
  dz
  }n|t        |dz  dz        z
  dz   }t        j                  |||z         }	Y pw xY w)
a  
        Convert a MusicXML Transpose object to a music21 Interval object.

        >>> from xml.etree.ElementTree import fromstring as EL
        >>> MP = musicxml.xmlToM21.MeasureParser()

        >>> t = EL('<transpose><diatonic>-1</diatonic>'
        ...        '<chromatic>-2</chromatic></transpose>')
        >>> MP.xmlTransposeToInterval(t)
        <music21.interval.Interval M-2>

        >>> t = EL('<transpose><diatonic>-5</diatonic>'
        ...        '<chromatic>-9</chromatic></transpose>')
        >>> MP.xmlTransposeToInterval(t)
        <music21.interval.Interval M-6>

        Not mentioned in MusicXML XSD but supported in (Finale; MuseScore): octave-change
        refers to both diatonic and chromatic, so we will deal:

        >>> t = EL('<transpose id="x"><diatonic>-1</diatonic><chromatic>-2</chromatic>'
        ...        '<octave-change>-1</octave-change></transpose>')
        >>> inv = MP.xmlTransposeToInterval(t)
        >>> inv
        <music21.interval.Interval M-9>
        >>> inv.id
        'x'
        Ndiatonic	chromaticr   zoctave-change   r  r   P1)	rW   rN   r*   r   intervalFromGenericAndChromaticIntervalExceptionrF  GenericIntervalr=  )
rv   mxTransposediatonicStep
mxDiatonicchromaticStepmxChromaticoctaveChangemxOctaveChangediatonicActualr  s
             r;   r  z$MeasureParser.xmlTransposeToInterval  s   :  %%j1
!z/L!&&{3" 0 01M$))/:%~223b8LAN$7$7 888L #(A a!-!1!-!1^??@MP\@\^ &$$]\%ABD%++L9D$$T*D{D)) -- 	^  !#&2S9IB9N5O&OST%TN&2S9IB9N5O&OST%TN??@MP\@\^	^s   2D" "A FFc                f    | j                  |      }|| j                  | j                  ||       yy)z
        Creates a TimeSignature using xmlToTimeSignature and inserts it into
        the stream if it is appropriate to do so (now always yes.)
        N)xmlToTimeSignaturer  r   )rv   mxTimer  s      r;   r  z!MeasureParser.handleTimeSignatureR  s6    
 $$V,>!!$"8"8&"E r=   c                Z   |j                  d      }|t        j                  |j                        S g }g }|D ]g  }|j                  dk(  r|j                  t        |             -|j                  dk(  r|j                  t        |             W|j                  dk(  sg n t        ||      D cg c]  \  }}| d|  }}}t        |      dk(  r	 t        j                  |d         }	n4t        j                         }	|	j                  d	j                  |             | j                  ||	       | j                  ||	       |j!                  d
      }
|
	 |	S |
dv r	|
|	_        |	S |
dk(  r	d|	_        |	S |
dk(  r	 |	S c c}}w # t        j                  $ r t        d|d          w xY w)as  
        Returns a TimeSignature or SenzaMisuraTimeSignature (for senza-misura)
        from a <time> block.

        >>> from xml.etree.ElementTree import fromstring as EL
        >>> MP = musicxml.xmlToM21.MeasureParser()

        >>> mxTime = EL('<time><beats>3</beats><beat-type>8</beat-type></time>')
        >>> MP.xmlToTimeSignature(mxTime)
        <music21.meter.TimeSignature 3/8>

        >>> mxTime = EL('<time symbol="common"><beats>4</beats>'
        ...             '<beat-type>4</beat-type></time>')
        >>> MP.xmlToTimeSignature(mxTime).symbol
        'common'

        Multiple times give a combined meter

        >>> mxTime = EL('<time><beats>3</beats><beat-type>8</beat-type>'
        ...             '<beats>4</beats><beat-type>4</beat-type></time>')
        >>> MP.xmlToTimeSignature(mxTime)
        <music21.meter.TimeSignature 3/8+4/4>

        Music21 separates into two parts

        >>> mxTime = EL('<time><beats>3+2</beats><beat-type>8</beat-type></time>')
        >>> ts32 = MP.xmlToTimeSignature(mxTime)
        >>> ts32
        <music21.meter.TimeSignature 3/8+2/8>

        Senza Misura gives a special music21 time signature subclass

        >>> mxSenza = EL('<time><senza-misura>0</senza-misura></time>')
        >>> MP.xmlToTimeSignature(mxSenza)
        <music21.meter.SenzaMisuraTimeSignature 0>

        Small Duration Time Signatures are supported

        >>> mxTime = EL('<time><beats>3</beats><beat-type>32</beat-type></time>')
        >>> MP.xmlToTimeSignature(mxTime)
        <music21.meter.TimeSignature 3/32>

        >>> mxTime = EL('<time><beats>3</beats><beat-type>64</beat-type></time>')
        >>> MP.xmlToTimeSignature(mxTime)
        <music21.meter.TimeSignature 3/64>

        >>> mxTime = EL('<time><beats>3</beats><beat-type>128</beat-type></time>')
        >>> MP.xmlToTimeSignature(mxTime)
        <music21.meter.TimeSignature 3/128>
        zsenza-misurabeatsz	beat-typeinterchangeable/r   r   zCannot process time signature +r  )r
   cutzsingle-numberr  r!   Tzdotted-note)rW   r   SenzaMisuraTimeSignaturer*   r\   r   rC   rp   r  rh  MeterExceptionr/   loadrg  r   r   rq   r  symbolizeDenominator)rv   r  isSenzaMisura
numeratorsdenominators
beatOrTypenumdenommsgr  r  s              r;   r  z MeasureParser.xmlToTimeSignature[  s   n N3$11-2D2DEE
 J~~(!!,z":;;.##L$<=#44 ! 36j,2OP2OJC#aw2OP s8q=?((Q0
 $$&BGGCHHSM" 	+FB'
 H%> 	 CCBI 	 v&*B#
 		 }$ 	? Q '' ?-4SVH=? ??s   2E?F %F*c                    | j                  |      }| j                  | j                  ||       | j                  |      }|| j                  |<   |S )a  
        Convert a `<clef>` tag to a music21.clef.Clef object, append it to the
        core, and set self.lastClefs for the staff number.

        >>> from xml.etree.ElementTree import fromstring as EL
        >>> MP = musicxml.xmlToM21.MeasureParser()
        >>> MP.lastClefs
        {}

        >>> mxAltoClef = EL('<clef number="2"><sign>C</sign><line>3</line></clef>')
        >>> altoClef = MP.handleClef(mxAltoClef)
        >>> altoClef
        <music21.clef.AltoClef>

        The MeasureParser (together with its parent PartParser) keeps track of the
        current clef for each staff in a shared dictionary called `.lastClefs`.

        >>> MP.lastClefs
        {2: <music21.clef.AltoClef>}
        >>> MP.lastClefs[2] is altoClef
        True

        The dictionary is shared with the parent PartParser.

        >>> MP.parent.lastClefs is MP.lastClefs
        True

        Per MusicXML standard, a clef without a number is interpreted as being for staff 1

        >>> mxClefBC = EL('<clef><sign>F</sign><line>4</line></clef>')
        >>> bc = MP.handleClef(mxClefBC)
        >>> bc
        <music21.clef.BassClef>

        >>> MP.lastClefs
        {2: <music21.clef.AltoClef>, 1: <music21.clef.BassClef>}

        Changed in v10.5 -- Fixed bug where clefs without number were applied to all staves
            rather than correct MusicXML interpretation of staff 1.  Returns the clef created.
            There is no longer a default treble clef for NO_STAFF_ASSIGNED.
        )	xmlToClefr  r   r  r  )rv   mxClefclefObjstaffNumberStrOrNones       r;   r  zMeasureParser.handleClef  sR    V ..(d44fgF  $226:/6+,r=   c                   |j                  d      j                  j                         }|j                         dv rt	        j
                  |      }n}|j                  d      }||j                  j                         }n
|dk(  rd}nd}|j                  d      }|	 t        |j                        }nd}t	        j
                  ||z   |      }| j                  ||       | j                  ||       |S # t        $ r d}Y Lw xY w)	a  
        Returns a music21 Clef object from an mxClef element.

        >>> from xml.etree.ElementTree import fromstring as EL
        >>> mxClef = EL('<clef><sign>G</sign><line>2</line></clef>')

        >>> MP = musicxml.xmlToM21.MeasureParser()
        >>> MP.xmlToClef(mxClef)
        <music21.clef.TrebleClef>

        >>> mxClef = EL('<clef><sign>G</sign><line>2</line>'
        ...             '<clef-octave-change>-1</clef-octave-change></clef>')
        >>> MP.xmlToClef(mxClef)
        <music21.clef.Treble8vbClef>

        >>> mxClef = EL('<clef><sign>TAB</sign></clef>')
        >>> MP.xmlToClef(mxClef)
        <music21.clef.TabClef>
        r  )tabr"   ru  jianpur~  G24zclef-octave-changer   )
rW   r*   r7   r  r	   clefFromStringrN   rO   r   r   )rv   r  r  r  r  r~  r  r  s           r;   r  zMeasureParser.xmlToClef  s    * {{6"''--/::<BB))$/G[[(F!{{((*#[[)=>N)%#&~':':#;L  !))$+|DG 	67+FG, " %#$L%s   C5 5DDc                `    | j                  |      }| j                  | j                  ||       y)zW
        convert mxKey to a Key or KeySignature and run insertCoreAndRef on it
        N)xmlToKeySignaturer  r   )rv   mxKeykeySigs      r;   r  z MeasureParser.handleKeySignature)  s,     ''.d44eVDr=   c                   |j                  d      | j                  |      }n`t        j                         }t        } |||ddt
               |j                  d      }|"|j                  }|dvr	 |j                  |      }| j                  ||       | j                  ||       | j                  ||       |S # t        j                  $ r Y Mw xY w)a&  
        Returns either a KeySignature (traditional or non-traditional)
        or a Key object based on whether fifths and mode is present.

        >>> from xml.etree.ElementTree import fromstring as EL
        >>> mxKey = EL('<key><fifths>-4</fifths></key>')

        >>> MP = musicxml.xmlToM21.MeasureParser()
        >>> MP.xmlToKeySignature(mxKey)
        <music21.key.KeySignature of 4 flats>

        >>> mxKey = EL('<key><fifths>-4</fifths><mode>minor</mode></key>')
        >>> MP.xmlToKeySignature(mxKey)
        <music21.key.Key of f minor>

        Invalid modes get ignored and returned as KeySignature objects, not keys.

        >>> mxKey = EL('<key><fifths>-4</fifths><mode>crazy</mode></key>')
        >>> MP.xmlToKeySignature(mxKey)
        <music21.key.KeySignature of 4 flats>
        fifthssharpsrS   moderV   )rW   nonTraditionalKeySignaturer   rf  r_   rN   r*   asKeyr   Music21ExceptionmxKeyOctavesr   r   )rv   r  ksr   	mxKeyMode	modeValues         r;   r  zMeasureParser.xmlToKeySignature0  s    0 ::h'007B!!#B+DUHh#>

6*I$%NN	J.XXi0 	%$5"%E2&	 (88 s   3B< <CCc                \   |j                  d      }|syt        j                  |j                        }|D ]X  }|j	                  d      }|dk(  r|j	                  d      }	 |t        |      dz
     }t        |j                        }	|	|_	        Z ||_        y# t        t        f$ r Y tw xY w)a  
        process the <key-octave> tags to potentially change a key signature
        to a non-standard key signature.

        >>> from xml.etree.ElementTree import fromstring as EL
        >>> mxKey = EL('<key><fifths>-4</fifths>'
        ...            '<key-octave number="1">3</key-octave>'
        ...            '<key-octave number="2">4</key-octave>'
        ...            '<key-octave number="4">3</key-octave>'
        ...            '</key>')

        >>> ks = key.KeySignature(-4)
        >>> MP = musicxml.xmlToM21.MeasureParser()
        >>> MP.mxKeyOctaves(mxKey, ks)
        >>> ks.alteredPitches
        [<music21.pitch.Pitch B-3>,
         <music21.pitch.Pitch E-4>,
         <music21.pitch.Pitch A->,
         <music21.pitch.Pitch D-3>]
        z
key-octaveNcancelr   r   r   )
r7  rn  ro  alteredPitchesrq   rN   rB  rO   r*   r  )
rv   r  r  
keyOctavesr  mxKeyOctaver  
pitchIndexalteredPitchoctaveToSets
             r;   r  zMeasureParser.mxKeyOctaves]  s    . ]]<0
r'8'89%K __X.F$2J-c*o.AB k../K"-L & + 
+ s   "BB+*B+c                |   t        |      }d}g }g }g }|D ]  }|j                  }|dk(  r|dk(  r|j                  d       |dk(  r|j                  |j                         nJ|dk(  r%|j                  t	        |j                               n |dk(  r|j                  |j                         |} t        |      t        |      k  r|j                  d       t        |      t        |      k7  rt        d      t        j                  d      }	g }
t        |||      D ]  \  }}}t        j                  |      }|L|| j                  v r| j                  |   }n|}t        j                  |      |_        ||j                  _        nt        j                  |      |_        |
j                  |        |
|	_        |	S )aj  
        Returns a KeySignature object that represents a nonTraditional Key Signature

        called by xmlToKeySignature if <fifths> is not present.

        >>> from xml.etree.ElementTree import fromstring as EL
        >>> MP = musicxml.xmlToM21.MeasureParser()

        >>> mxKey = EL('<key><key-step>E</key-step><key-alter>-1</key-alter></key>')
        >>> MP.nonTraditionalKeySignature(mxKey)
        <music21.key.KeySignature of pitches: [E-]>

        Should be the same:

        >>> MP.xmlToKeySignature(mxKey)
        <music21.key.KeySignature of pitches: [E-]>

        Works with key-accidental also:

        >>> mxKey = EL('<key><key-step>G</key-step><key-alter>1</key-alter>'
        ...            '<key-accidental>sharp</key-accidental></key>')
        >>> MP.nonTraditionalKeySignature(mxKey)
        <music21.key.KeySignature of pitches: [G#]>
        Nz	key-alterzkey-stepzkey-accidentalz;For non traditional signatures each step must have an alter)r  )r:  r\   r   r*   rM   r  r/   r   rf  rp   r#   r  r   r  r  r  r  )rv   r  childrenlastTagstepsaltersaccidentalsr   r\   r  r  r  r  r  rO  accidentalNames                   r;   r  z(MeasureParser.nonTraditionalKeySignature  s   4 ;A%%C+%#*;""4(j QVV$#eAFFm,((""166*G  {c&k)t$u:V$)MO O T*'*5&+'F#D%D!A%!;!;;%)%?%?
%KN%/N$//?%*"$//6!!!$ (G +	r=   c                   |j                  d      }|t        |      }nd}|| j                  f}| j                  j                  |d      }| j	                  ||      }|-| j                  | j                  ||       || j                  |<   yy)a  
        StaffDetails (staff-details) handles attributes about
        the staff itself -- its size, number of lines, tuning,
        frets, etc.

        It is different from StaffLayout (staff-layout) which
        only handles relationship of one staff to another (the
        distance)

        Rather than returning a StaffLayout object,
        it adds it to self.staffLayoutObjects checking
        to see if there is already an incomplete
        StaffLayout object for this staff.
        r   Nr   )m21staffLayout)rq   rN   r   r   xmlStaffLayoutFromStaffDetailsr  )rv   	mxDetailsr   layoutObjectKeyexistingStaffLayoutObjectnewStaffLayoutObjects         r;   r  z MeasureParser.handleStaffDetails  s    &  mmH-"k*KK&(>(>?$($;$;$?$?QU$V!#BB4  C  
 %,!!$"8"8)EYZ7KD##O4 -r=   c                4   t         }|st        j                         }n|}|j                  d      }|t	        |      |_        |j                  d      }|dk(  s|du rd|_        n|dk(  s|du rd|_         |||dt        	       |j                  d
      }|?	 |j                  j                         }t        j                  j                  |      |_         |||dt$        	       |s|S y# t        $ r" t        j                   d| t"        d       Y ?w xY w)a  
        Returns a new StaffLayout object from staff-details or sets attributes on an existing one

        >>> from xml.etree.ElementTree import fromstring as EL
        >>> MP = musicxml.xmlToM21.MeasureParser()
        >>> mxDetails = EL('<details number="2" print-object="no">'
        ...                '<staff-size>21.2</staff-size><staff-lines>4</staff-lines>'
        ...                '</details>')
        >>> stl = MP.xmlStaffLayoutFromStaffDetails(mxDetails)
        >>> stl.staffSize
        21.2
        >>> stl.staffLines
        4
        >>> stl.staffNumber
        2
        >>> stl.hidden
        True

        `staffType` defaults to Regular:

        >>> stl.staffType
        <StaffType.REGULAR: 'regular'>
        >>> mxDetails2 = EL('<details number="2"><staff-type>cue</staff-type></details>')
        >>> MP.xmlStaffLayoutFromStaffDetails(mxDetails2, m21staffLayout=stl)
        >>> stl.staffType
        <StaffType.CUE: 'cue'>
        r   Nr   r   FTr   zstaff-linesrS   z
staff-typez(Got an incorrect staff-type in details: r4  r5  z
staff-size)r_   r   r   rq   rN   r   hiddenrW   r*   r7   r&   r  	StaffType	staffTyperO   r9  r:  r0   rR   )	rv   r   r  r   r<  r   staffPrintedmxStaffTyper  s	            r;   r  z,MeasureParser.xmlStaffLayoutFromStaffDetails  s   B ($$&C C  mmH-"!+.CO }}^44<5#8CJU"ld&:CJ
 	S)]c:  nn\2"*//557 & 6 6w ? 	S)\^D J   >{mL# s   >C, ,(DDc                F   |j                  d      }|| j                  t        |j                        | j                  _        t        j                         }|j                  d      }|dk(  rd|_        nd|_        || j                  _	        | j                  ||       yyy)a!  
        measure + multi-measure repeats, slashed repeats, etc.

        But currently only multiMeasure rests are supported.

        Each of these applies to the entire measure, so there's
        no need to insert into the stream.

        Does not support multiple staves yet.
        zmultiple-restNzuse-symbolsr   TF)rW   rU  rN   r*   r  r%   MultiMeasureRestrq   
useSymbolsr  r   )rv   mxMeasureStylemxMultiRest
mmrSpannerr  s        r;   r  z MeasureParser.handleMeasureStyle@  s     %))/:"t{{'>589I9I5JDKK2 113J$7JU"(,
%(-
%8BDKK5LLj1 (?"r=   c                \   |'| j                   | j                   j                  d      }| j                  }| j                  r-| j                  j                  }| j                  j
                  }nd}d}|d}d}nt        j                  |      \  }}|dvrt        |      |_	        |dvr||_
        |X|j                  dk(  rI|j                  |dz   k7  r7|j                  t        |j                        z   }|||z   }||_	        ||_
        |j                  | _        |j                  | _
        y)a@  
        Gets the measure number from the 'number' attribute of the
        <measure> tag.  (Or, for testing, from the mNumRaw
        argument).  Sets MeasureParser.stream.number and possibly
        MeasureParser.stream.numberSuffix

        >>> MP = musicxml.xmlToM21.MeasureParser()
        >>> MP.parseMeasureNumbers('5')
        >>> MP.stream.number
        5

        Sets not only `stream.number`, but also `MeasureParser.measureNumber` and
        `MeasureParser.numberSuffix`

        >>> MP.parseMeasureNumbers('44b')
        >>> MP.stream.number
        44
        >>> MP.stream.numberSuffix
        'b'
        >>> MP.measureNumber
        44
        >>> MP.numberSuffix
        'b'

        >>> MP.parseMeasureNumbers('X1')
        >>> MP.stream.number
        1
        >>> MP.stream.numberSuffix
        'X'
        Nr   r?   rV   Xr   )rY  rq   r&   rU  r  r  r
   getNumFromStrrN   r   r  r  r  )rv   mNumRawr  lastMNumlastMSuffixmNummSuffix	newSuffixs           r;   r  z!MeasureParser.parseMeasureNumbers^  s   > ?t~~9nn((2GKK;;{{44H++66KHK?DG"009MD' z!4yAH*$$AN ~~$X\)ANNS]:	* +i 7I#!*XXNNr=   c                   | j                   }dD ]R  }|j                  |      D ]<  }|j                  d      }t        |      x}s"| j                  j                  |       > T t        | j                        dkD  rt        | j                        D ]R  }t        j                         }||_
        | j                  j                  d|       || j                  |j                  <   T d| _        | j                  j                          yy)am  
        Finds all the "voice" information in <note> and <forward> tags and updates the set of
        `.voiceIndices` to be a set of all the voice texts, and if there is
        more than one voice in the measure, sets `.useVoices` to True
        and creates a voice for each.

        >>> from xml.etree.ElementTree import fromstring as EL
        >>> MP = musicxml.xmlToM21.MeasureParser()
        >>> MP.mxMeasure = EL('<measure><note><voice>1</voice></note></measure>')
        >>> MP.updateVoiceInformation()

        Puts a set object in `.voiceIndices`

        >>> MP.voiceIndices
        {'1'}
        >>> MP.useVoices
        False

        >>> MP = musicxml.xmlToM21.MeasureParser()
        >>> MP.mxMeasure = EL('<measure><note><voice>1</voice></note>'
        ...                   '<note><voice>2</voice></note></measure>')
        >>> MP.updateVoiceInformation()
        >>> sorted(list(MP.voiceIndices))
        ['1', '2']
        >>> MP.useVoices
        True
        >>> len(MP.stream)
        2
        >>> list(MP.stream.getElementsByClass(stream.Voice))
        [<music21.stream.Voice 1>, <music21.stream.Voice 2>]
        >>> MP = musicxml.xmlToM21.MeasureParser()
        >>> MP.mxMeasure = EL('<measure><note><voice>1</voice></note>'
        ...                   '<forward><voice>2</voice></forward></measure>')
        >>> MP.updateVoiceInformation()
        >>> sorted(list(MP.voiceIndices))
        ['1', '2']
        >>> MP.useVoices
        True
        >>> len(MP.stream)
        2
        >>> list(MP.stream.getElementsByClass(stream.Voice))
        [<music21.stream.Voice 1>, <music21.stream.Voice 2>]
        )r!   r  r@  r   r3  TN)rY  r7  rW   rC   r  ra   r  r  r&   Voicer2  r5  r  r]  rF  )rv   mxmr*  mxnr@  rN  r!  s          r;   r  z$MeasureParser.updateVoiceInformation  s    Z nn,I{{9-))%0060%%))&1 . - t  !A% !2!23LLN&&sA.()%	 4
 "DNKK++- &r=   r  )rY  r  rU  zPartParser | None)rw   zET.Element | int | Noner  rN   )r  int | ET.Element | Nonerx   base.Music21Objectr  r  )r  r  rx   r  r  r  )rA   r  )r   r  )rG  r  r  r  )r   zlist[ET.Element]r  zchord.ChordBase)T)r  znote.Note | note.Unpitchedri   )r  r  )r}  r  r   znote.Unpitched | Noner  znote.Unpitched)r  r  r   zpitch.Accidental | Noner  zpitch.Accidental)r  r  rO  znote.GeneralNote)r  zexpressions.Ornament | None)rA   r  r  z
int | Noner  zOffsetQL | None)rG  r  r  zlist[duration.Tuplet])r  znote.Lyric | None)r  r  r  zstream.Voice | None)r-  r  r  r  )rC  r  r  zDharmony.ChordSymbol | harmony.NoChord | tablature.ChordWithFretBoard)r  r  r<  r  r  rN   r  rM   )
r  r  r  r  r<  r  r  rN   r  rM   )rA   r  r  rM   )r  r  r  z4meter.TimeSignature | meter.SenzaMisuraTimeSignature)r  z	clef.Clef)r  r  r  zkey.KeySignature)r  layout.StaffLayout | Noner  r  )Jr   r   r   r   r  r  r  r  r  r  r  r!  r  r  r  r  rF  rC  r  rv  rz  rs  ru  r  rD  r{  rq  r  r  r  rA  r@  r>  r  r  r  r$  r#  r!  r  r  rE  r  r'  r  r  r#  r$  r  rA  r  rn  rz  r  r~  r}  rB  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  s   @r;   r  r  E	  s   , &#-- #*" /3+/KY+KY(KYZ f% f%T-3--3 &-3 
	-3^2 )2 &	2
 
24(@F:M4:,~_2B[zQUjJX8"CHPj )-&& && 
	&V +/E E (E 
	ENL:\IxDL2v4pHT:#x ( (BO3r 9=	W 6	Wx "%)	ee e #	eN	#-J6 OT #JN`ob$6GR<. $() ) 
)VIVM^!F,ii 
JiV'VS?S?  S? 	S?
 S?j** *  	*
 * *0<Pd'4,,(ATTlFee 
<eN1f2hE+Z*+XEN!LN 37J 0J 
!	JX2<B+H>.r=   r  __main__)r9   
str | Noner  r!  )rA   r  r  r  )rJ   r  r  r  ri   )rb   r  )d
__future__r   rn  r  r(  r  mathr   r'  typingr  r9  xml.etree.ElementTreer$  ElementTreer   music21r   r   r   r   r	   r
   r   r   r   music21.common.enumsr   r   music21.common.numberToolsr   r   r   r   r   r   r   r   r   r   r   r   r   music21.midi.percussionr   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   music21.musicxmlr,   r-   music21.musicxml.xmlSoundParserr.   music21.musicxml.xmlObjectsr/   r0   synchronizeIdsToM21r=  setM21AttributeFromAttributer   r  r1   music21.common.typesr2   dictrN   r:  Music21Objectr  Environmentr=  r  
propertiesALL_NAMESPACE_NAMESALL_UNIQUE_NAMESALL_MUSIC21_WORK_IDSr3   __annotations__r<   rC   rK   rR   r_   rd   rf   r   r  rV  r  r   mainTestr   r=   r;   <module>r:     s   #   	   	   " " !         = >             M            ' $ 9 P,,#@@ ??- c4(:(:#;;<&{&&':;    $++**+../  9 "L<25)QU 5)n	#O Od. .2L} L`B6 B6L[B.M= [B.BE zG r=   