
    rh                      S r SSKJr  SSKrSSKrSSKrSSKJr  SSK	J
r
  SSK	Jr  SSKJr  SSKJr  SS	KJr  SS
K	Jr  SSK	Jr  SSK	Jr  SSK	Jr  SSK	Jr  SSK	Jr  \R0                  (       a  SSK	Jr  SiS jrSS.   SjS jjr " S S\R8                  5      r " S S\
R<                  5      r " S S\5      r  " S S\RB                  5      r" " S S\RB                  5      r# " S S\RH                  5      r% " S  S!\
R<                  5      r& " S" S#\&5      r' " S$ S%\&5      r( " S& S'\&5      r) " S( S)\5      r* " S* S+\5      r+ " S, S-\+5      r, " S. S/\,5      r- " S0 S1\-5      r. " S2 S3\-5      r/ " S4 S5\,5      r0 " S6 S7\05      r1 " S8 S9\05      r2 " S: S;\+5      r3 " S< S=\35      r4 " S> S?\35      r5 " S@ SA\35      r6 " SB SC\35      r7 " SD SE\+5      r8 " SF SG\+5      r9 " SH SI\95      r: " SJ SK\+5      r; " SL SM\;5      r< " SN SO\<5      r= " SP SQ\<5      r> " SR SS\;5      r? " ST SU\?5      r@ " SV SW\?5      rA " SX SY\R8                  5      rB " SZ S[\+5      rC " S\ S]\5      rD " S^ S_\R8                  5      rE " S` Sa\RH                  5      rF " Sb Sc\RH                  5      rG " Sd Se\5      rH " Sf Sg\RH                  5      rI\*/rJ\KSh:X  a  SSK	r	\	R                  " 5         gg)ka_  
This module provides object representations of expressions, that is
notational symbols such as Fermatas, Mordents, Trills, Turns, etc.
which are stored under a Music21Object's .expressions attribute.

A sub-category of Expressions are Ornaments.

Unlike articulations, expressions can be attached to the Stream itself.
For instance, TextExpressions.
    )annotationsN)Fraction)base)common)OrnamentDelay)opFrac)OffsetQL)exceptions21)interval)key)pitch)spanner)stylenotec                X   [        U [        R                  5      (       a/  U R                  S:H  =(       a    U R                  R
                  S:H  $ [        U [        R                  5      (       a  U R                  S:H  $ [        U [        R                  5      (       a  U R
                  S:H  $ g)NP1r   F)
isinstancer   Intervalname	chromatic	semitonesDiatonicIntervalChromaticInterval)intvs    M/home/james-whalen/.local/lib/python3.13/site-packages/music21/expressions.pyisUnisonr   /   s    $))**yyD BT^^%=%=%BB	D(33	4	4yyD  	D(44	5	5~~""    keySigc               6   U n[         R                  (       a  Uc   e[        US5      (       d  U/$ UR                  (       d  U/$ / n/ nSnU(       a  [         R                  (       a  Uc   eUS-  nUR                  S   n[        US5      (       a  UR	                  X!S9u  pxn	U H  n
UR                  U
5        M     U	 H  n
UR                  U
5        M     Uc  UnOrUR                  SS Ul        Un[         R                  (       a  Uc   eUR                  (       d  O0O&UR                  SS Ul        UR                  (       d  O	U(       a  M  / nU H  n
UR                  U
5        M     Ub  UR                  U5        U H  n
UR                  U
5        M     U$ )a  
given a Note or Unpitched with Ornament expressions,
convert them into a list of objects that represents
the performed version of the object:

>>> n1 = note.Note('D5')
>>> n1.quarterLength = 1
>>> n1.expressions.append(expressions.WholeStepMordent())
>>> expList = expressions.realizeOrnaments(n1)
>>> st1 = stream.Stream()
>>> st1.append(expList)
>>> #_DOCS_SHOW st1.show()

.. image:: images/expressionsMordentRealize.*
     :width: 218
Nexpressionsd      r   realizer   )tTYPE_CHECKINGhasattrr"   r%   append)srcObjr    	srcObjectpreExpandListpostExpandList
loopBusterthisExpression	preExpandnewSrcObject
postExpandiretLists               r   realizeOrnamentsr5   8   s   * 06I$$$9m,,{""{
 ,,,!OJ&2215N~y116D6L6L 7M 73	 #A!((+ ##A"))!, $' ,I+4+@+@+D((	??$000 ,, - )2(=(=ab(A	% ,,9 j< ANN1  NN9%ANN1  r   c                      \ rS rSrSrg)ExpressionException    N__name__
__module____qualname____firstlineno____static_attributes__r9   r   r   r7   r7          r   r7   c                  b   ^  \ rS rSrSr\R                  rU 4S jrSS jr	\
SS j5       rSrU =r$ )
Expression   z;
This base class is inherited by many diverse expressions.
c                4   > [         TU ]  " S0 UD6  SU l        g )Nfirstr9   )super__init__	tieAttachselfkeywords	__class__s     r   rG   Expression.__init__   s    $8$ r   c                    g)N r9   rJ   s    r   _reprInternalExpression._reprInternal   s    r   c                X    U R                   R                  n[        R                  " USS9$ )a&  
returns the name of the expression, which is generally the
class name lowercased and spaces where a new capital occurs.

Subclasses can override this as necessary.

>>> sc = expressions.Schleifer()
>>> sc.name
'schleifer'

>>> iTurn = expressions.InvertedTurn()
>>> iTurn.name
'inverted turn'
 )replacement)rL   r;   r   camelCaseToHyphen)rJ   	classNames     r   r   Expression.name   s&      NN++	''	sCCr   )rH   returnstr)r;   r<   r=   r>   __doc__r   	TextStyle_styleClassrG   rQ   propertyr   r?   __classcell__rL   s   @r   rB   rB      s3     //K! D Dr   rB   c                  z   ^  \ rS rSrSrSr\R                  rSSS.U 4S jjjr	S r
\SS j5       rS	 rS
 rSrU =r$ )RehearsalMark   a?  
A rehearsal mark is a type of Expression that designates a rehearsal
marking, such as A., B., etc.

Takes two inputs, content ('B', 5, 'III') and an optional numbering system which
is helpful for getting the next rehearsal mark.

>>> rm = expressions.RehearsalMark('B')
>>> rm
<music21.expressions.RehearsalMark 'B'>

N	numberingc                  > [         TU ]  " S0 UD6  Xl        US;  a  [        S5      eX l        SU R
                  l        SU R
                  l        g )N)alphabeticalromannumberNz<Numbering must be "alphabetical", "roman", "number", or Nonecentermiddler9   )rF   rG   contentr7   rg   r   alignHorizontalalignVertical)rJ   rn   rg   rK   rL   s       r   rG   RehearsalMark.__init__   sP    $8$EE%NP P"%-

"#+

 r   c                ,    [        U R                  5      $ N)reprrn   rP   s    r   rQ   RehearsalMark._reprInternal   s    DLL!!r   c                X   U c  g[        U [        5      (       a  g[        U [        5      (       d  g [        U 5      ng! [         a     Of = f [        R
                  R                  U 5      n[        U 5      S:  a  gUS:  a  gg! [         a     Of = fU [        R                  ;   a  gg)a  
if numbering was not set, get it from the content

>>> ex = expressions.RehearsalMark()
>>> ex._getNumberingFromContent('C')
'alphabetical'

>>> ex._getNumberingFromContent('VII')
'roman'
>>> ex._getNumberingFromContent('X')
'roman'
>>> ex._getNumberingFromContent('CI')
'roman'

>>> ex._getNumberingFromContent('5')
'number'
>>> ex._getNumberingFromContent(5)
'number'

>>> print(ex._getNumberingFromContent('*'))
None

Nrk      rj   2   ri   )
r   intr[   
ValueErrorr   numberTools	fromRomanlenstringascii_letters)cunused
romanValues      r   _getNumberingFromContent&RehearsalMark._getNumberingFromContent   s    2 9a!S!!	VF 			++55a8J1v{B% 		 $$$!s'   > 
A
A.B >B 
BBc                   U R                   nU(       d  U R                  U R                  5      nU(       d  U R                  c  gU R                  S-  $ US:X  aU  [        [	        U R                  S   5      S-   5      nU[
        R                  ;  a  S[        U R                  5      S-   -  $ U$ US:X  a  [        U R                  5      S-   $ US:X  a7  [        R                  " [        R                  " U R                  5      S-   5      $ g)	a?  
Return the next content based on the numbering

>>> expressions.RehearsalMark('A').nextContent()
'B'

>>> expressions.RehearsalMark('II').nextContent()
'III'

>>> expressions.RehearsalMark('IV').nextContent()
'V'

>>> expressions.RehearsalMark(7).nextContent()
8

>>> expressions.RehearsalMark('Z').nextContent()
'AA'


With rehearsal mark 'I' default is to consider it
as a roman numeral:

>>> expressions.RehearsalMark('I').nextContent()
'II'

Specify `numbering` directly to avoid problems:

>>> expressions.RehearsalMark('I', numbering='alphabetical').nextContent()
'J'
Nrw   ri   r$   Ark   rj   )rg   r   rn   chrordr~   r   r}   ry   r   toRomanr|   )rJ   rg   nextContents      r   r   RehearsalMark.nextContent  s    > NN	55dllCI||#<<!##&c$,,r"23a78K&"6"66c$,,/!344""("t||$q(('!>>&"2"24<<"@1"DEE "r   c                F    [        U R                  5       U R                  S9$ )a  
Return the next rehearsal mark.

>>> rm = expressions.RehearsalMark('C')
>>> rm.nextMark()
<music21.expressions.RehearsalMark 'D'>


>>> rm = expressions.RehearsalMark('IV', numbering='roman')
>>> nm = rm.nextMark()
>>> nm.content
'V'
>>> nm.numbering
'roman'
rf   )rc   r   rg   rP   s    r   nextMarkRehearsalMark.nextMark5  s      T--/4>>JJr   )rn   rg   rs   )rZ   
str | None)r;   r<   r=   r>   r\   classSortOrderr   TextStylePlacementr^   rG   rQ   staticmethodr   r   r   r?   r`   ra   s   @r   rc   rc      sX     N**K,$ , ," 5 5n2FhK Kr   rc   c                  $    \ rS rSrSrSrSrSrSrg)	PedalTypeiI  sustain	sostenutosoftsilentr9   N)	r;   r<   r=   r>   Sustain	SostenutoSoftSilentr?   r9   r   r   r   r   I  s    GIDFr   r   c                  $    \ rS rSrSrSrSrSrSrg)	PedalFormiO  linesymbol	symbolalt
symbolliner9   N)	r;   r<   r=   r>   LineSymbol	SymbolAlt
SymbolLiner?   r9   r   r   r   r   O  s    DFIJr   r   c                  4   ^  \ rS rSrSr  SU 4S jjrSrU =r$ )	PedalMarkiU  a  
A pedal mark spanner contains a pedaled set of notes.
The spanner starts at the moment of down-pedal, and
ends at the moment of up-pedal.  There can be any
number of pedal "bounces" in the middle of the spanner.

The visual form of the pedal mark can vary.  The following
examples use a pedal mark with one "bounce" in the middle::

    Pedal marks can be lines:            |_______^________|
    Pedal marks can be normal symbolic:  Ped.    * Ped.   *
    Pedal marks can be altered symbolic: Ped.    Ped.     *
    Pedal marks can be symbol and line:  Ped.____^________|

Pedal marks, whether lines, symbols, or a combination, can
represent the sustain pedal (Ped.), the sostenuto pedal
(Sost.), the soft (una corda) pedal, or (more rarely) the
silent (muting) pedal.

Pedal marks that are symbolic can be abbreviated: e.g. P. instead
of Ped., S. instead of Sost.

Pedal marks that are lines can have non-printed portions
(gaps) in them; these are usually started with "simile",
but not necessarily.
c                   > [         TU ]  " U0 UD6  SSKJn  UR                  /U l        S U l        S U l        SU l        S U l	        g )Nr   r   F)
rF   rG   music21r   GeneralNotefillElementTypes	pedalType	pedalFormabbreviated	placement)rJ   spannedElementsrK   r   rL   s       r   rG   PedalMark.__init__p  sH    
 	/6X6 !%!1!1 2)-)-!&#'r   )r   r   r   r   r   rZ   Noner;   r<   r=   r>   r\   rG   r?   r`   ra   s   @r   r   r   U  s    4( 
	( (r   r   c                  :   ^  \ rS rSrSrSU 4S jjrSS jrSrU =r$ )PedalObjecti  zX
Base class of individual objects that mark various transitions
in a PedalMark spanner.
c                4   > [         TU ]  " S0 UD6  S U l        g Nr9   )rF   rG   r   rI   s     r   rG   PedalObject.__init__  s    $8$%)r   c                <    U R                   c  gSU R                   3$ )N
uninsertedzat )
activeSiteoffsetrP   s    r   rQ   PedalObject._reprInternal  s     ??"T[[M""r   )r   r   rY   )	r;   r<   r=   r>   r\   rG   rQ   r?   r`   ra   s   @r   r   r     s    *# #r   r   c                      \ rS rSrSrSrg)PedalBouncei  z[
This object, when seen in a PedalMark spanner, represents an up/down bounce
of the pedal.
r9   Nr;   r<   r=   r>   r\   r?   r9   r   r   r   r         r   r   c                      \ rS rSrSrSrg)PedalGapStarti  z
This object, when seen in a PedalMark spanner, represents a disappearance of
the pedal line, usually with a TextExpression('simile').  The pedaling should
continue (similarly to before), but the line is invisible during this gap.
r9   Nr   r9   r   r   r   r     s    r   r   c                      \ rS rSrSrSrg)PedalGapEndi  z_
This object, when seen in a PedalMark spanner, represents the reappearance of
the pedal line.
r9   Nr   r9   r   r   r   r     r   r   r   c                     ^  \ rS rSr% SrSr\R                  rSS0r	S\
S'   SU 4S jjrS	 r\SS
 j5       r\R                  SS j5       r\S 5       r\R                  S 5       rS rS rSrU =r$ )TextExpressioni  a  
A TextExpression is a word, phrase, or similar
bit of text that is positioned in a Stream or Measure.
Conventional expressive indications are text
like "agitato" or "con fuoco."

>>> te = expressions.TextExpression('Con fuoco')
>>> te.content
'Con fuoco'

Most configuration of style is done
on the `.style` :class:`~music21.style.TextStyle` object
itself.

>>> te.style.fontSize = 24.0
>>> te.style.fontSize
24
>>> te.style.fontStyle = 'italic'
>>> te.style.fontWeight = 'bold'
>>> te.style.letterSpacing = 0.5
re   r   a  
            Staff placement: 'above', 'below', or None.

            A setting of None implies that the placement will be determined
            by notation software and no particular placement is demanded.

            This is not placed in the `.style` property, since for some
            expressions, the placement above or below an object has semantic
            meaning and is not purely presentational.
            zdict[str, str]	_DOC_ATTRc                   > [         TU ]  " S0 UD6  [        U[        5      (       d  [        U5      U l        OXl        S U l        g r   )rF   rG   r   r[   _contentr   )rJ   rn   rK   rL   s      r   rG   TextExpression.__init__  s;    $8$ '3''LDM#M r   c                    [        U R                  5      S:  a  U R                  S S S-   n[        U5      $ U R                  b  [        U R                  5      $ g)N   
   z...rO   )r}   r   rt   )rJ   shortContents     r   rQ   TextExpression._reprInternal  sO    t}}#=="-5L%%]]&&&r   c                R    U R                   (       d  gU R                  R                  $ )a  
Returns or sets the enclosure on the Style object
stored on .style.

Exposed directly on the expression for backwards
compatibility.  Does not create a .style object if
one does not exist and the value is None.

>>> te = expressions.TextExpression('Bridge')
>>> te.enclosure is None
True
>>> te.enclosure = style.Enclosure.RECTANGLE
>>> te.enclosure
<Enclosure.RECTANGLE>

Note that this is also set on `.style`.

>>> te.style.enclosure
<Enclosure.RECTANGLE>
NhasStyleInformationr   	enclosurerP   s    r   r   TextExpression.enclosure  s     , ''zz###r   c                N    U R                   (       d  Uc  g XR                  l        g rs   r   rJ   values     r   r   r     s    ''EM$

r   c                    U R                   $ )z
Get or set the content.

>>> te = expressions.TextExpression('dolce')
>>> te.content
'dolce'
>>> te.content = 'sweeter'
>>> te
<music21.expressions.TextExpression 'sweeter'>
)r   rP   s    r   rn   TextExpression.content  s     }}r   c                $    [        U5      U l        g rs   )r[   r   r   s     r   rn   r     s    E
r   c                    SSK Jn  UR                   Hb  nUR                  U R                  5      (       d  M%  [
        R                  " U5      nUR                  [
        R                  " U 5      5        Us  $    g)z
If this TextExpression can be a RepeatExpression,
return a new :class:`~music21.repeat.RepeatExpression`.
object, otherwise, return None.
r   )repeatN)r   r   repeatExpressionReferenceisValidTextr   copydeepcopysetTextExpression)rJ   r   objres       r   getRepeatExpression"TextExpression.getRepeatExpression  s[     	#33Ct}}--]]3' $$T]]4%89	 4 r   c                    g rs   r9   rP   s    r   getTempoTextTextExpression.getTempoText0  s     	r   )r   r   rs   )rZ   style.Enclosure | None)r   r   )r;   r<   r=   r>   r\   r   r   r]   r^   r   __annotations__rG   rQ   r_   r   setterrn   r   r   r?   r`   ra   s   @r   r   r     s    . N//K 	 	!I~  $ $2 % %
   ^^# #* r   r   c            	         ^  \ rS rSrSrSU 4S jjrSSS.       SS jjrSS.         SS	 jjrSS
.   SS jjr\	SS j5       r
SSSSSSSSS.                 SS jjrSrU =r$ )Ornamenti7  a)  
An Ornament is a type of Expression that, when attached to a Note
(in the future: Notes) can transform into the main note.

All ornaments have an `.autoScale` boolean which determines
whether to shrink (not currently to expand) the ornament if the
note it is attached to is too short to realize.
c                P   > [         TU ]  " S0 UD6  SU l        SU l        SU l        g )NTr9   )rF   rG   connectedToPrevious	autoScale_ornamentalPitchesrI   s     r   rG   Ornament.__init__@  s*    $8$#' ;=r   NFr    inPlacec               F    U(       d  [         R                  " U5      n/ U/ 4$ )a|  
subclassable method call that takes a sourceObject and optional keySig
and returns a three-element tuple of a list of notes before the
"main note" or the result of the expression if it gobbles up the entire note,
the "main note" itself (or None) to keep processing for ornaments,
and a list of notes after the "main note".

* New in v8: inPlace boolean; note that some ornaments
  might not return a Note in the second position at all (such as trills)
  so inPlace does nothing.
* Changed in v9: Optional keySig can be passed in (useful in cases where there
  is no keySig in srcObj's context, or where a different keySig is desired).
)r   r   )rJ   r*   r    r   s       r   r%   Ornament.realizeG  s"    , ]]6*FFBr   useQLc                  [        U5      (       + nU(       a(  [        US5      (       d  [        S[        U5       35      eUc  U R                  n[
        R                  " U5      nXFR                  l        [
        R                  " U5      nXGR                  l        U(       aF  [        R                  (       a!  [        U[        R                  5      (       d   eUR                  USS9  UR                  U5        UR                  U5        g)z<
Used by trills and mordents to fill out their realization.
	transposezExpected note; got NTr   )r   r(   	TypeErrortypequarterLengthr   r   durationr&   r'   r   r   Noter   r)   )rJ   r*   fillObjectstransposeIntervalr   isTransposed	firstNote
secondNotes           r   fillListOfRealizedNotes Ornament.fillListOfRealizedNotesb  s     "**;!<< < <1$v,@AA=&&EMM&)	 ,1(]]6*
,1) !*dii8888  !2D A9%:&r   r   c                   g6
Only implemented in Turn, GeneralMordent, and Trill.
Nr9   rJ   r*   r    s      r   resolveOrnamentalPitches!Ornament.resolveOrnamentalPitches  s     	r   c                    [        5       $ )r  )tuplerP   s    r   ornamentalPitchesOrnament.ornamentalPitches  s    
 wr   T	pitchPastpitchPastMeasureotherSimultaneousPitchesalteredPitchescautionaryPitchClasscautionaryAlloverrideStatuscautionaryNotImmediateRepeatc                   gr  r9   )	rJ   r  r  r  r  r  r  r  r  s	            r   updateAccidentalDisplay Ornament.updateAccidentalDisplay  s     	r   )r   r   r   r   r*   note.Note | note.Unpitchedr    key.KeySignature | Noner   boolrZ   zltuple[list[note.Note | note.Unpitched], note.Note | note.Unpitched | None, list[note.Note | note.Unpitched]])
r*   r#  r  z list[note.Note | note.Unpitched]r  interval.IntervalBaser   zOffsetQL | NonerZ   r   r*   note.GeneralNoter    r$  rZ   ztuple[pitch.Pitch, ...])r  list[pitch.Pitch] | Noner  r*  r  r*  r  r*  r  r%  r  r%  r  r%  r  r%  rZ   r   )r;   r<   r=   r>   r\   rG   r%   r
  r  r_   r  r   r?   r`   ra   s   @r   r   r   7  s*   > )- (  &	 
  
0 B  $!'(!' 4!' 1	!' !' 
!'N )-		 	 &		   -137;?15%)#$-1 * 1	
 #9 / #   '+ 
 r   r   c            	      n  ^  \ rS rSr% SrSrS\S'   SS.SU 4S jjjr\SU 4S	 jj5       r	\SS
 j5       r
\
R                  SS j5       r
\SS j5       rSS.     SS jjrSS.   SS jjr\SS j5       r\S S j5       rSSSSSSSSS.               S!S jjrSSS.       S"S jjrSrU =r$ )#GeneralMordenti  z#
Base class for all Mordent types.
rO   r[   
_directionN
accidentalc               N   > [         TU ]  " S0 UD6  Xl        SU l        SU l        g )N      ?abover9   )rF   rG   _accidentalr  r   rJ   r/  rK   rL   s      r   rG   GeneralMordent.__init__  s(    $8$2<" r   c                t   > [         TU ]  nU R                  b  USU R                  R                  -   S-   -  nU$ )a  
returns the name of the Mordent/InvertedMordent, which is generally
the class name lowercased, with spaces where a new capital occurs. The
name also will include any accidental, if it exists.

Subclasses can override this as necessary.

>>> mordent = expressions.Mordent()
>>> mordent.name
'mordent'

>>> sharp = pitch.Accidental('sharp')
>>> invertedMordent = expressions.InvertedMordent(accidental=sharp)
>>> invertedMordent.name
'inverted mordent (sharp)'

 ()rF   r   r/  rJ   theNamerL   s     r   r   GeneralMordent.name  s;    & w|??&tdoo222S88Gr   c                    U R                   $ )z*
This is the GeneralMordent's accidental.
r3  rP   s    r   r/  GeneralMordent.accidental  s    
 r   c                    Xl         g rs   r>  rJ   newAccidentals     r   r/  r?        (r   c                    U R                   $ )z\
The direction of the mordent's ornamental pitch from the main note.
Can be 'up' or 'down'.
r-  rP   s    r   	directionGeneralMordent.direction       r   r   c                  U R                   S;  a  [        S5      eUR                  (       d  [        R                  " S5      $ U=(       d<    UR                  [        R                  5      =(       d    [        R                  " S5      nUR                  S   n[        R                  " U5      nSUl
        UR                  c  UR                  Ul        U R                   S:X  a%  UR                  [        R                  " S5      S	S
9  O$UR                  [        R                  " S5      S	S
9  U R                  (       a  U R                  Ul
        O UR                  UR                   5      Ul
        [        R                  " X45      $ )a  
Returns the size of the mordent's interval, given a source note and
an optional key signature.  If the key signature is not specified, the
source note's context is searched for the current key signature, and if
there is no such key signature, a key signature with no sharps and no flats
will be used.  Any `accidental` that has been set on the mordent will also
be taken into account.  If no `accidental` has been set, the appropriate
accidental from the key signature will be used.

If keySig is specified, this can be considered to be a theoretical question:
"If this particular mordent were to be attached to this note, in this key,
what would the size of the mordent interval be?"
updownz:Cannot compute mordent size if I do not know its directionr   r   r   NrK  rw   Tr   r-  r7   pitchesr   r   getContextByClassr   KeySignaturer   r   r/  octaveimplicitOctaver   GenericIntervalaccidentalByStepsteprJ   r*   r    srcPitchornamentalPitchs        r   getSizeGeneralMordent.getSize  s'   & ??.0%&bcc~~$$T** \633C4D4DE\IYIYZ[I\ &r 2'+}}X'>%)"!!)%4%C%CO"??d"%%h&>&>q&A4%P%%h&>&>r&BD%Q??)-O& *0)@)@AUAU)VO&  ;;r   c                  UR                   (       d  gUR                   S   nU R                  XS9n[        R                  " U5      nUR                  c  UR
                  Ul        UR                  USS9  UR                  R                  S:w  a  UR                  SS9  U R                  bM  UR                  c  [        R                  " S5      Ul
        U R                  R                  UR                  l        U4U l        g)a  
Computes and stores the ornamental pitch for a GeneralMordent, given the srcObj
(can be any kind of ornamented GeneralNote) and an optional keySig.

If keySig is None, srcNote's context will be searched for a key signature.
If no key signature is found, a key signature with no sharps and no flats
will be used.


A mordent on a G in a key with no sharps or flats (ornamental pitch will be F).

>>> noSharpsOrFlats = key.KeySignature(sharps=0)
>>> n1 = note.Note('G4')
>>> mordent = expressions.Mordent()
>>> mordent.resolveOrnamentalPitches(n1, keySig=noSharpsOrFlats)
>>> mordent.ornamentalPitches
(<music21.pitch.Pitch F4>,)
>>> mordent.ornamentalPitch
<music21.pitch.Pitch F4>

e.g. A mordent on a G in a key with one sharp (ornamental pitch will be F#).

>>> oneSharp = key.KeySignature(sharps=1)
>>> mordent.resolveOrnamentalPitches(n1, keySig=oneSharp)
>>> mordent.ornamentalPitches
(<music21.pitch.Pitch F#4>,)
>>> mordent.ornamentalPitch
<music21.pitch.Pitch F#4>

e.g. A mordent with a natural, on a G, in a key with one sharp
(ornamental pitch will be F).

>>> mordent.accidental = pitch.Accidental('natural')
>>> mordent.resolveOrnamentalPitches(n1, keySig=oneSharp)
>>> mordent.ornamentalPitches
(<music21.pitch.Pitch F4>,)
>>> mordent.ornamentalPitch
<music21.pitch.Pitch F4>
Nr   r   Tr   r   rO  rZ  r   r   rR  rS  r   	microtonecentsconvertMicrotonesToQuarterTonesr/  r   
AccidentaldisplayStatusr   rJ   r*   r    rX  r  rY  s         r   r  'GeneralMordent.resolveOrnamentalPitches  s    Z ~~ &r 237<<<3V'+}}X'>!!)%4%C%CO"!!"3T!B$$**a/;;D;I??&
 ))1-2-=-=a-@*7;7T7TO&&4#2"4r   c                D    U R                   (       a  U R                   S   $ g)zp
Returns the mordent's ornamentalPitch.  If resolveOrnamentalPitches
has not yet been called, None is returned.
r   Nr   rP   s    r   rY  GeneralMordent.ornamentalPitchX  !     ""**1--r   c                    U R                   $ z
Returns any ornamental pitch that has been resolved (see
`resolveOrnamentalPitches`, which must be called first, or an
empty tuple will be returned).
rf  rP   s    r   r   GeneralMordent.ornamentalPitchesb       &&&r   TFr  c               4   U R                   n	U	c  gU R                  be  U R                  R                  bN  U	R                  c  [        R                  " S5      U	l        U R                  R                  U	R                  l        gU	R                  UUUUUUUSS9  g)a?  
Updates accidental display for a GeneralMordent's ornamental pitch.
Defined exactly like Pitch.updateAccidentalDisplay, with two changes:
Instead of self being the pitch to update, self is a GeneralMordent whose
ornamentalPitch is to be updated; and we pay no attention to ties,
since ornamental notes cannot be tied.
Nr   Fr  r  r  r  r  r  r  lastNoteWasTied)rY  r/  rb  r   ra  r   
rJ   r  r  r  r  r  r  r  r  ps
             r   r   &GeneralMordent.updateAccidentalDisplayk  s    &   9??&4??+H+H+T||#$//2)-)F)FALL&	!!-)!5'))E! 	" 	#r   r   c                  U R                   S;  a  [        S5      eUR                  R                  S:X  a  [        S5      eU R                  nUR                  R                  U R                  S-  ::  a5  U R                  (       d  [        S5      eUR                  R                  S-  nUnUc8  UR                  [        R                  5      nUc  [        R                  " S5      nUR                  R                  SU-  -
  nU R                  XS9n/ nU R                  XXtS	9  [        U5      (       + n	U	(       a  [        U5       H  u  p[        R                  (       a!  [        U[        R                   5      (       d   eU
S
-   nUR"                  R$                  b  MY  US:X  d  Ma  UR'                  UR"                  R(                  5      UR"                  l        M     SnXR*                  ;   a  UR*                  R-                  U 5      nU(       d  [.        R0                  " U5      OUnXnR                  l        US:w  a  UR*                  R3                  U5        X/ 4$ )a  
Realize a mordent.

returns a three-element tuple.
The first is a list of the two notes that the beginning of the note were converted to.
The second is the rest of the note.
The third is an empty list (since there are no notes at the end of a mordent).

>>> n1 = note.Note('C4')
>>> n1.quarterLength = 0.5
>>> m1 = expressions.Mordent()
>>> m1.realize(n1)
([<music21.note.Note C>, <music21.note.Note B>], <music21.note.Note C>, [])

Note: use one of the subclasses, not the GeneralMordent class

>>> n2 = note.Note('C4')
>>> n2.quarterLength = 0.125
>>> m2 = expressions.GeneralMordent()
>>> m2.realize(n2)
Traceback (most recent call last):
music21.expressions.ExpressionException: Cannot realize a mordent if I do not
    know its direction
rJ  z7Cannot realize a mordent if I do not know its directionr   1Cannot steal time from an object with no durationrw   z0The note is not long enough to realize a mordent   r   r   r$   r   )r-  r7   r  r  r   rP  r   rQ  rZ  r
  r   	enumerater&   r'   r   r   r  r   r/  rU  rV  r"   indexr   r   pop)rJ   r*   r    r   use_qlcurrentKeySigremainderQLr  	mordNotesr  noteIdxnnoteNuminExpressionsremainderNotes                  r   r%   GeneralMordent.realize  s   B ??.0%&_``??((A-%&YZZ##??((D,>,>,BB>>)*\]]__22Q6F/5 "44S5E5EFM$ # 0 0 3oo33q6zB LLLF46	$$V8I$X!)*;!<< (	2
??%a3333&{77%%-'Q,)6)G)G)UAGG& 3 %%%"..44T:M5<f-&/:,B%%))-8"--r   )r3  r   r   r  )r/  pitch.Accidental | NonerY   rZ   r  rB  r  r*   r(  r    r$  rZ   r&  r'  rZ   zpitch.Pitch | Noner)  r  r*  r  r*  r  r*  r  r*  r  r%  r  r%  r  r%  r  r%  r"  )r;   r<   r=   r>   r\   r-  r   rG   r_   r   r/  r   rF  rZ  r  rY  r  r   r%   r?   r`   ra   s   @r   r,  r,    s    J>B ! !  .     ) )   )-	/< /< &	/<
 
/<j )-	D5 D5 &	D5L   ' ' -137;?15%)#$-1&# *&# 1	&#
 #9&# /&# #&# &# &# '+&#X )-K.(K. &	K.
 K.
0K. K.r   r,  c                  (    \ rS rSr% SrSrS\S'   Srg)Mordenti  a	  
A normal Mordent -- goes downwards and has a line through it.

Note that some computer terminology calls this one an inverted mordent, but this
is a modern term.  See Apel, *Harvard Dictionary of Music*, "Mordent"::

    A musical ornament consisting of the alternation of the written note
    with the note immediately below it.


A mordent has the size of a second, of some form, depending on the note
that will have the mordent, the current key signature in that note's context, as
well as any accidental on the mordent itself.

e.g. Mordent without accidentals in default key (no flats or sharps)

>>> m = expressions.Mordent()
>>> m.direction
'down'
>>> m.getSize(note.Note('C4'))
<music21.interval.Interval m-2>
>>> m.getSize(note.Note('B3'))
<music21.interval.Interval M-2>

e.g. Mordent with flat, in default key (no flats or sharps)

>>> mFlat = expressions.Mordent(accidental=pitch.Accidental('flat'))
>>> mFlat.direction
'down'
>>> mFlat.getSize(note.Note('C4'))
<music21.interval.Interval M-2>
>>> mFlat.getSize(note.Note('B3'))
<music21.interval.Interval A-2>

e.g. Mordent without accidentals, in key with one flat

>>> oneFlat = key.KeySignature(-1)
>>> mNotFlat = expressions.Mordent()
>>> mNotFlat.direction
'down'
>>> mNotFlat.getSize(note.Note('C4'), keySig=oneFlat)
<music21.interval.Interval M-2>
>>> mNotFlat.getSize(note.Note('B3'), keySig=oneFlat)
<music21.interval.Interval M-2>

e.g. Mordent without accidentals, with a key from context with one flat (same results)

>>> noteC4 = note.Note('C4')
>>> noteB3 = note.Note('B3')
>>> measure = stream.Measure([oneFlat, noteC4, noteB3])
>>> mNotFlatWithKeyFromContext = expressions.Mordent()
>>> mNotFlatWithKeyFromContext.direction
'down'
>>> mNotFlatWithKeyFromContext.getSize(noteC4)
<music21.interval.Interval M-2>
>>> mNotFlatWithKeyFromContext.getSize(noteB3)
<music21.interval.Interval M-2>



* Changed in v7: Mordent sizes are GenericIntervals -- as was originally
  intended but programmed incorrectly.
* Changed in v9: Support an accidental on Mordent. This also adds the concept of
  an ornamental pitch that is processed by makeAccidentals.
  The size property has been removed and replaced with `.getSize()` (which requires
  a `srcObj` and optional `keySig` param).  Added optional `keySig` param to
  `.realize()` as well.
rL  r[   r-  r9   Nr;   r<   r=   r>   r\   r-  r   r?   r9   r   r   r  r    s    CH Jr   r  c                     ^  \ rS rSrSrS
U 4S jjrSS.     SS jjr\SS j5       r\R                  SS j5       rS	r
U =r$ )HalfStepMordenti+  z
A half step normal Mordent.

>>> m = expressions.HalfStepMordent()
>>> m.direction
'down'
>>> m.getSize(note.Note('C4'))
<music21.interval.Interval m-2>
c                ~   > SU;   a  [        S5      e[        TU ]  " S0 UD6  [        R                  " S5      U l        g )Nr/  z1Cannot initialize HalfStepMordent with accidentalzm-2r9   )r7   rF   rG   r   r   _minorSecondDownrI   s     r   rG   HalfStepMordent.__init__5  s:    8#%&YZZ$8$7?7H7H7Or   Nr   c                   U R                   $ rs   r  r  s      r   rZ  HalfStepMordent.getSize<       $$$r   c                    g rs   r9   rP   s    r   r/  HalfStepMordent.accidentalD      r   c                    [        S5      e)Nz(Cannot set accidental of HalfStepMordentr7   rA  s     r   r/  r  H  s    !"LMMr   r  r   r  r  r  r;   r<   r=   r>   r\   rG   rZ  r_   r/  r   r?   r`   ra   s   @r   r  r  +  sd    P )-	% % &	%
 
%   N Nr   r  c                     ^  \ rS rSrSrS
U 4S jjrSS.     SS jjr\SS j5       r\R                  SS j5       rS	r
U =r$ )WholeStepMordentiM  z
A whole step normal Mordent.

>>> m = expressions.WholeStepMordent()
>>> m.direction
'down'
>>> m.getSize(note.Note('C4'))
<music21.interval.Interval M-2>
c                ~   > SU;   a  [        S5      e[        TU ]  " S0 UD6  [        R                  " S5      U l        g )Nr/  z2Cannot initialize WholeStepMordent with accidentalzM-2r9   )r7   rF   rG   r   r   _majorSecondDownrI   s     r   rG   WholeStepMordent.__init__W  s:    8#%&Z[[$8$7?7H7H7Or   Nr   c                   U R                   $ rs   r  r  s      r   rZ  WholeStepMordent.getSize^  r  r   c                    g rs   r9   rP   s    r   r/  WholeStepMordent.accidentalf  r  r   c                    [        S5      e)Nz)Cannot set accidental of WholeStepMordentr  rA  s     r   r/  r  j  s    !"MNNr   r  r   r  r  r  r  ra   s   @r   r  r  M  sd    P )-	% % &	%
 
%   O Or   r  c                  (    \ rS rSr% SrSrS\S'   Srg)InvertedMordentip  a  
An inverted Mordent -- goes upwards and has no line through it.

Note that some computer terminology calls this one a (normal) mordent, but this
is a modern term.    See Apel, *Harvard Dictionary of Music*,
"Inverted Mordent"::

    An 18th-century ornament involving alternation of the
    written note with the note immediately above it.

An inverted mordent has the size of a second, of some form, depending on the note
that will have the mordent, the current key signature in that note's context, as
well as any accidental on the mordent itself.

>>> m = expressions.InvertedMordent()
>>> m.direction
'up'
>>> m.getSize(note.Note('C4'))
<music21.interval.Interval M2>
>>> m.getSize(note.Note('B3'))
<music21.interval.Interval m2>

>>> mSharp = expressions.InvertedMordent(accidental=pitch.Accidental('sharp'))
>>> mSharp.direction
'up'
>>> mSharp.getSize(note.Note('C4'))
<music21.interval.Interval A2>
>>> mSharp.getSize(note.Note('B3'))
<music21.interval.Interval M2>

* Changed in v7: InvertedMordent sizes are GenericIntervals -- as was originally
  intended but programmed incorrectly.
* Changed in v9: Support an accidental on InvertedMordent. This also adds the concept of
  an ornamental pitch that is processed by makeAccidentals.
  The size property has been removed and replaced with `.getSize()` (which requires
  a `srcObj` and optional `keySig` param).  Added optional `keySig` param to
  `.realize()` as well.
rK  r[   r-  r9   Nr  r9   r   r   r  r  p  s    %L Jr   r  c                     ^  \ rS rSrSrS
U 4S jjrSS.     SS jjr\SS j5       r\R                  SS j5       rS	r
U =r$ )HalfStepInvertedMordenti  z
A half-step inverted Mordent.

>>> m = expressions.HalfStepInvertedMordent()
>>> m.direction
'up'
>>> m.getSize(note.Note('C4'))
<music21.interval.Interval m2>
c                ~   > SU;   a  [        S5      e[        TU ]  " S0 UD6  [        R                  " S5      U l        g )Nr/  z9Cannot initialize HalfStepInvertedMordent with accidentalm2r9   )r7   rF   rG   r   r   _minorSecondUprI   s     r   rG    HalfStepInvertedMordent.__init__  s@    8#%KM M$8$5=5F5Ft5Lr   Nr   c                   U R                   $ rs   r  r  s      r   rZ  HalfStepInvertedMordent.getSize       """r   c                    g rs   r9   rP   s    r   r/  "HalfStepInvertedMordent.accidental  r  r   c                    [        S5      e)Nz0Cannot set accidental of HalfStepInvertedMordentr  rA  s     r   r/  r    s    !"TUUr   r  r   r  r  r  r  ra   s   @r   r  r    sd    M )-	# # &	#
 
#   V Vr   r  c                     ^  \ rS rSrSrS
U 4S jjrSS.     SS jjr\SS j5       r\R                  SS j5       rS	r
U =r$ )WholeStepInvertedMordenti  z
A whole-step inverted Mordent.

>>> m = expressions.WholeStepInvertedMordent()
>>> m.direction
'up'
>>> m.getSize(note.Note('C4'))
<music21.interval.Interval M2>
c                ~   > SU;   a  [        S5      e[        TU ]  " S0 UD6  [        R                  " S5      U l        g )Nr/  z:Cannot initialize WholeStepInvertedMordent with accidentalM2r9   )r7   rF   rG   r   r   _majorSecondUprI   s     r   rG   !WholeStepInvertedMordent.__init__  s@    8#%LN N$8$5=5F5Ft5Lr   Nr   c                   U R                   $ rs   r  r  s      r   rZ   WholeStepInvertedMordent.getSize  r  r   c                    g rs   r9   rP   s    r   r/  #WholeStepInvertedMordent.accidental  r  r   c                    [        S5      e)Nz1Cannot set accidental of WholeStepInvertedMordentr  rA  s     r   r/  r    s    !"UVVr   r  r   r  r  r  r  ra   s   @r   r  r    sd    M )-	# # &	#
 
#   W Wr   r  c            	      t  ^  \ rS rSr% SrSrS\S'   SS.SU 4S jjjr\SU 4S	 jj5       r	\SS
 j5       r
\SS j5       r\R                  SS j5       rS rSS.     SS jjrSS.   SS jjr\S S j5       r\S!S j5       rSSSSSSSSS.               S"S jjrSSS.       S#S jjrSrU =r$ )$Trilli  a  
A basic trill marker without the trill extension

>>> tr = expressions.Trill()
>>> tr.placement
'above'
>>> tr.getSize(note.Note('C4'))
<music21.interval.Interval M2>
>>> tr.getSize(note.Note('B4'))
<music21.interval.Interval m2>

Trills have a `.nachschlag` attribute which determines whether there
should be extra gracenotes at the end of the trill.

>>> tr.nachschlag
False
>>> tr.nachschlag = True

The Trill also has a "quarterLength" attribute that sets how long
each trill note should be.  Defaults to 32nd note:

>>> tr.quarterLength
0.125
>>> tr.quarterLength == duration.Duration('32nd').quarterLength
True

* Changed in v7: the size should be a generic second.
* Changed in v9: Support an accidental on trills. This also adds the concept of
  an ornamental pitch that is processed by makeAccidentals.
  The size property has been removed and replaced with `.getSize()` (which requires
  a `srcObj` and optional `keySig` param).  Added optional `keySig` param to
  `.realize()` as well.

rK  r[   r-  Nr.  c               x   > [         TU ]  " S0 UD6  Xl        SU l        SU l        SU l        SU l        SU l        g )Nr2  Fallr1  Tr9   )rF   rG   r3  r   
nachschlagrH   r  _setAccidentalFromKeySigr4  s      r   rG   Trill.__init__  s>    $8$2< "(,%r   c                |   > [         TU ]  nU R                  (       a  USU R                  R                  -   S-   -  nU$ )a  
returns the name of the Trill, which is generally the class name
lowercased, with spaces where a new capital occurs. The name also
will include the accidental, if it exists.

Subclasses can override this as necessary.

>>> trill = expressions.Trill()
>>> trill.name
'trill'

>>> doubleSharpedTrill = expressions.Trill(accidental=pitch.Accidental('double-sharp'))
>>> doubleSharpedTrill.name
'trill (double-sharp)'

r7  r8  r9  r:  s     r   r   
Trill.name  s8    $ w|??tdoo222S88Gr   c                    U R                   $ )zZ
The direction of the trill's ornamental pitch from the main note.
Can be 'up' or 'down'.
rE  rP   s    r   rF  Trill.direction&  rH  r   c                    U R                   $ )zn
This is the Trill's accidental.  Whether or not it is visible is dictated by
the accidental's displayStatus.
r>  rP   s    r   r/  Trill.accidental.  s     r   c                    Xl         g rs   r>  rA  s     r   r/  r  6  rC  r   c                    / nU(       a  US   R                   R                  U 5        [        U5      S:  a5  US   R                  S5      (       d  [	        U5      nUR                  U5        U$ )a  
splitClient is called by base.splitAtQuarterLength() to support splitting trills.

>>> n = note.Note(type='whole')
>>> n.expressions.append(expressions.Trill())
>>> st = n.splitAtQuarterLength(3.0)
>>> n1, n2 = st
>>> st.spannerList
[<music21.expressions.TrillExtension <music21.note.Note C><music21.note.Note C>>]
>>> n1.getSpannerSites()
[<music21.expressions.TrillExtension <music21.note.Note C><music21.note.Note C>>]
r   r$   TrillExtension)r"   r)   r}   getSpannerSitesr  )rJ   noteListreturnSpannerstes       r   splitClientTrill.splitClient:  sc     QK##**40x=1Xa[%@%@AQ%R%R)B!!"%r   r   c                  U R                   S;  a  [        S5      eUR                  (       d  [        R                  " S5      $ U=(       d<    UR                  [        R                  5      =(       d    [        R                  " S5      nUR                  S   n[        R                  " U5      nSUl
        UR                  c  UR                  Ul        U R                   S:X  a%  UR                  [        R                  " S5      S	S
9  O$UR                  [        R                  " S5      S	S
9  U R                  (       a  U R                  Ul
        O UR                  UR                   5      Ul
        [        R                  " X45      $ )aD  
Returns the size of the trill's interval, given a source note and
an optional key signature.  If the key signature is not specified, the
source note's context is searched for the current key signature, and if
there is no such key signature, a key signature with no sharps and no flats
will be used.  Any `accidental` that has been set on the trill will also
be taken into account.

If keySig is specified, this can be considered to be a theoretical question:
"If this particular trill were to be attached to this note, in this key,
what would the size of the trill interval be?"
rJ  z8Cannot compute trill size if I do not know its directionr   r   r   NrK  rw   Tr   rM  rN  rW  s        r   rZ  Trill.getSizeP  s'   $ ??.0%&`aa~~$$T** \633C4D4DE\IYIYZ[I\ &r 2'+}}X'>%)"!!)%4%C%CO"??d"%%h&>&>q&A4%P%%h&>&>r&BD%Q??)-O& *0)@)@AUAU)VO&  ;;r   c                  UR                   (       d  gUR                   S   nU R                  XS9n[        R                  " U5      nUR                  c  UR
                  Ul        UR                  USS9  UR                  R                  S:w  a  UR                  SS9  U R                  bM  UR                  c  [        R                  " S5      Ul
        U R                  R                  UR                  l        U4U l        g)a  
Computes and stores the ornamental pitch for a Trill, given the srcObj
(can be any kind of ornamented GeneralNote) and an optional keySig.

If keySig is None, srcNote's context will be searched for a key signature.
If no key signature is found, a key signature with no sharps and no flats
will be used.

e.g. A trill on a D in a key with no sharps or flats (ornamental pitch will be E).

>>> noSharpsOrFlats = key.KeySignature(0)
>>> n2 = note.Note('D4')
>>> trill = expressions.Trill()
>>> trill.resolveOrnamentalPitches(n2, keySig=noSharpsOrFlats)
>>> trill.ornamentalPitches
(<music21.pitch.Pitch E4>,)
>>> trill.ornamentalPitch
<music21.pitch.Pitch E4>
Nr   r   Tr   r   r]  rc  s         r   r  Trill.resolveOrnamentalPitches  s    2 ~~ &r 237<<<3V'+}}X'>!!)%4%C%CO"!!"3T!B$$**a/;;D;I??&
 ))1-2-=-=a-@*7;7T7TO&&4#2"4r   c                D    U R                   (       a  U R                   S   $ g)zn
Returns the trill's ornamentalPitch.  If resolveOrnamentalPitches
has not yet been called, None is returned.
r   Nrf  rP   s    r   rY  Trill.ornamentalPitch  rh  r   c                    U R                   $ rj  rf  rP   s    r   r  Trill.ornamentalPitches  rl  r   TFr  c                  U R                   n	U	c  gU R                  (       aN  U	R                  c  [        R                  " S5      U	l        U R                  R                  U	R                  l        gU	R                  UUUUUUUSS9  g)a  
Updates accidental display for a Trill's ornamental pitch.
Defined exactly like Pitch.updateAccidentalDisplay, with two changes:
Instead of self being the pitch to update, self is an Trill whose
ornamentalPitch is to be updated; and we pay no attention to ties,
since ornamental notes cannot be tied.

These examples show a Trill whose main note is a G in a key with no sharps or
flats, so the trill's ornamental pitch is an A. We show various situations
where the A might or might not end up with a natural accidental.

If updateAccidentalDisplay is called with cautionaryAll, the A gets a (cautionary)
natural accidental.

>>> noSharpsOrFlats = key.KeySignature(0)
>>> trill1 = expressions.Trill()
>>> trill1.resolveOrnamentalPitches(note.Note('g4'), keySig=noSharpsOrFlats)
>>> trill1.ornamentalPitch
<music21.pitch.Pitch A4>
>>> trill1.ornamentalPitch.accidental is None
True
>>> past = [pitch.Pitch('a#4'), pitch.Pitch('c#4'), pitch.Pitch('c4')]
>>> trill1.updateAccidentalDisplay(pitchPast=past, cautionaryAll=True)
>>> trill1.ornamentalPitch.accidental, trill1.ornamentalPitch.accidental.displayStatus
(<music21.pitch.Accidental natural>, True)

If updateAccidentalDisplay is called without cautionaryAll, the A gets a natural
accidental, because a previous A had a sharp accidental.

>>> trill2 = expressions.Trill()
>>> trill2.resolveOrnamentalPitches(note.Note('g4'), keySig=noSharpsOrFlats)
>>> trill2.ornamentalPitch
<music21.pitch.Pitch A4>
>>> trill2.ornamentalPitch.accidental is None
True
>>> past = [pitch.Pitch('a#4'), pitch.Pitch('c#4'), pitch.Pitch('c4')]
>>> trill2.updateAccidentalDisplay(pitchPast=past)  # should add a natural
>>> trill2.ornamentalPitch.accidental, trill2.ornamentalPitch.accidental.displayStatus
(<music21.pitch.Accidental natural>, True)

If updateAccidentalDisplay is called with cautionaryPitchClass=False, the A does
not get a natural accidental because the previous A# was in a different octave.

>>> trill3 = expressions.Trill()
>>> trill3.resolveOrnamentalPitches(note.Note('g4'), keySig=noSharpsOrFlats)
>>> trill3.ornamentalPitch
<music21.pitch.Pitch A4>
>>> trill3.ornamentalPitch.accidental is None
True
>>> past = [pitch.Pitch('a#3'), pitch.Pitch('c#'), pitch.Pitch('c')]
>>> trill3.updateAccidentalDisplay(pitchPast=past, cautionaryPitchClass=False)
>>> trill3.ornamentalPitch.accidental is None
True

If we add a natural accidental to the trill (with displayStatus True), and then
updateAccidentalDisplay is called with cautionaryPitchClass=False, the A gets a
visible natural accidental because of that added natural accidental.

>>> trill4 = expressions.Trill()
>>> natural = pitch.Accidental('natural')
>>> natural.displayStatus = True
>>> trill4.accidental = natural
>>> trill4.resolveOrnamentalPitches(note.Note('g4'), keySig=noSharpsOrFlats)
>>> trill4.ornamentalPitch
<music21.pitch.Pitch A4>
>>> trill4.ornamentalPitch.accidental
<music21.pitch.Accidental natural>
>>> trill4.ornamentalPitch.accidental.displayStatus
True
>>> past = [pitch.Pitch('a#3'), pitch.Pitch('c#'), pitch.Pitch('c')]
>>> trill4.updateAccidentalDisplay(pitchPast=past, cautionaryPitchClass=False)
>>> trill4.ornamentalPitch.accidental
<music21.pitch.Accidental natural>
>>> trill4.ornamentalPitch.accidental.displayStatus
True
Nr   Frn  )rY  r/  r   ra  rb  r   rp  s
             r   r   Trill.updateAccidentalDisplay  s    p   9??||#$//2)-)F)FALL&	!!-)!5'))E! 	" 	#r   r   c               X   U R                   nUR                  R                   S:X  a  [        S5      eUR                  R                   SU-  :  a5  U R                  (       d  [        S5      eUR                  R                   S-  nUR                  R                   SU R                   -  :  aF  U R                  (       a5  U R                  (       d  [        S5      eUR                  R                   S-  nUnUc8  UR                  [        R                  5      nUc  [        R                  " S5      nU R                  XS9nUR                  5       n[        U5      (       + n[        UR                  R                   U-  5      n	U R                  (       a  U	S-  n	/ n
[        [        U	S-  5      5       H  nU R                  XXdS	9  M     U(       a  U R                  nU(       a  U
 H  n[        R                   (       aB  [#        U[$        R&                  5      (       d   e[#        U[$        R&                  5      (       d   eUR(                  R*                  UR(                  R*                  :w  d  M  UR(                  R,                  b  M  UR/                  UR0                  5      UR(                  l        M     U(       a*  XR2                  ;   a  UR2                  R5                  U 5        U R                  (       Ga2  [6        R8                  " U5      n/ Ul        XNR                  l         [6        R8                  " U5      n/ Ul        XOR                  l         U(       a  [        R                   (       aB  [#        U[$        R&                  5      (       d   e[#        U[$        R&                  5      (       d   eUR;                  US
S9  W(       a[  U(       aT  UR/                  UR0                  5      UR(                  l        UR/                  UR0                  5      UR(                  l        X/nU
SU4$ U
S/ 4$ )a
  
realize a trill.

Returns a three-element tuple:

* The first is a list of the notes that the note was converted to.
* The second is None because the trill "eats up" the whole note.
* The third is a list of the notes at the end if nachschlag is True,
  and empty list if False.

>>> n1 = note.Note('C4')
>>> n1.duration.type = 'eighth'
>>> t1 = expressions.Trill()
>>> n1.expressions.append(t1)
>>> realization = t1.realize(n1)
>>> realization
([<music21.note.Note C>,
  <music21.note.Note D>,
  <music21.note.Note C>,
  <music21.note.Note D>], None, [])
>>> realization[0][0].quarterLength
0.125
>>> realization[0][0].pitch.octave
4

When inside a stream, the realizations will consult the current key to see
if it should be a whole-step or half-step trill:

>>> m = stream.Measure()
>>> k1 = key.Key('D-')
>>> m.insert(0, k1)
>>> m.append(n1)
>>> t1.realize(n1)
([<music21.note.Note C>,
  <music21.note.Note D->,
  <music21.note.Note C>,
  <music21.note.Note D->], None, [])

Note that if the key contradicts the note of the trill, for instance, here
having a C-natural rather than a C-sharp, we do not correct the C to C#.

>>> k2 = key.Key('A')
>>> m.replace(k1, k2)
>>> t1.realize(n1)
([<music21.note.Note C>,
  <music21.note.Note D>,
  <music21.note.Note C>,
  <music21.note.Note D>], None, [])

This can lead to certain unusual circumstances such as augmented second trills
which are technically correct, but probably not what a performer exprects.

>>> k3 = key.Key('E')
>>> m.replace(k2, k3)
>>> t1.realize(n1)
([<music21.note.Note C>,
  <music21.note.Note D#>,
  <music21.note.Note C>,
  <music21.note.Note D#>], None, [])


To avoid this case, create a :class:`~music21.expressions.HalfStepTrill` or
:class:`~music21.expressions.WholeStepTrill`.

If there is a nachschlag, it will appear in the third element of the list.

>>> n1.duration.type = 'quarter'
>>> m.replace(k3, k1)  # back to D-flat major
>>> t1.nachschlag = True
>>> t1.realize(n1)
([<music21.note.Note C>,
  <music21.note.Note D->,
  <music21.note.Note C>,
  <music21.note.Note D->,
  <music21.note.Note C>,
  <music21.note.Note D->], None, [<music21.note.Note C>, <music21.note.Note B->])

Some notes can be too short to realize if autoscale is off.

>>> n2 = note.Note('D4')
>>> n2.duration.type = '32nd'
>>> t2 = expressions.Trill()
>>> t2.autoScale = False
>>> t2.realize(n2)
Traceback (most recent call last):
music21.expressions.ExpressionException: The note is not long enough to realize a trill

A quicker trill makes it possible:

>>> t2.quarterLength = duration.Duration('64th').quarterLength
>>> t2.realize(n2)
([<music21.note.Note D>,
  <music21.note.Note E>], None, [])

inPlace is not used for Trills.
r   rt  rw   z.The note is not long enough to realize a trillru  z,The note is not long enough for a nachschlagNr   r   Tr   )r  r  r7   r   r  rP  r   rQ  rZ  reverser   ry   ranger
  r  r&   r'   r   r   r  r   nameWithOctaver/  rU  rV  r"   remover   r   r   )rJ   r*   r    r   r   rz  r  transposeIntervalReverser  numberOfTrillNotes
trillNotesunused_countersetAccidentalFromKeySigr~  firstNoteNachschlagsecondNoteNachschlagr  s                    r   r%   Trill.realize2  sR   R ""??((A-%&YZZ??((1u94>>)*Z[[OO11A5E??((1t/A/A+AAdoo>>)*XYYOO11A5E "44S5E5EFM$ # 0 0 3 LLLF#4#<#<#> !)*;!<< !>!>!FG??!#57
#C(:Q(>$?@N((=N(\ A &*&C&C#&#A)!TYY7777)&$))<<<<ww--1L1LL77--5 !. > >qvv F GG. $ t111%%d+???"&--"7.0+9>((6#'==#8 /1 ,:?))7??%&:DIIFFFF%&9499EEEE$../G8< / >*};H;Y;Y+00<2'--8<I<Z<Z,11=3(..9 .DJj11 b))r   )r3  r   r  r  r   r  rH   )r/  r  rZ   r   rY   r  r  r  r'  r  r)  r  r"  )r;   r<   r=   r>   r\   r-  r   rG   r_   r   rF  r/  r   r  rZ  r  rY  r  r   r%   r?   r`   ra   s   @r   r  r    s   !D J>B - -  ,       ) )4 )-	.< .< &	.<
 
.<h )-	05 05 &	05d   ' ' -137;?15%)#$-1k# *k# 1	k#
 #9k# /k# #k# k# k# '+k#b )-r*(r* &	r*
 r*
0r* r*r   r  c                  $    \ rS rSr% SrS\S'   Srg)InvertedTrilli  rL  r[   r-  r9   N)r;   r<   r=   r>   r-  r   r?   r9   r   r   r  r    s    Jr   r  c                     ^  \ rS rSrSrS
U 4S jjrSS.     SS jjr\SS j5       r\R                  SS j5       rS	r
U =r$ )HalfStepTrilli  a  
A trill confined to half steps.

>>> halfTrill = expressions.HalfStepTrill()
>>> halfTrill.placement
'above'
>>> halfTrill.getSize(note.Note('C4'))
<music21.interval.Interval m2>

Here the key signature of 2 sharps will not affect the trill:

>>> n = note.Note('B4', type='eighth')
>>> m = stream.Measure()
>>> m.insert(0, key.KeySignature(2))
>>> m.append(n)
>>> halfTrill.realize(n)
([<music21.note.Note B>,
  <music21.note.Note C>,
  <music21.note.Note B>,
  <music21.note.Note C>], None, [])
c                   > SU;   a  [        S5      e[        TU ]  " S0 UD6  [        R                  " S5      U l        SU l        g )Nr/  z/Cannot initialize HalfStepTrill with accidentalr  Fr9   )r7   rF   rG   r   r   r  r  rI   s     r   rG   HalfStepTrill.__init__  sH    8#%AC C$8$5=5F5Ft5L(-%r   Nr   c                   U R                   $ rs   r  r  s      r   rZ  HalfStepTrill.getSize
  r  r   c                    g rs   r9   rP   s    r   r/  HalfStepTrill.accidental  r  r   c                    [        S5      e)Nz&Cannot set accidental of HalfStepTrillr  rA  s     r   r/  r    s    !"JKKr   )r  r  r   r  r  r  r  ra   s   @r   r  r    sc    *. )-	# # &	#
 
#   L Lr   r  c                     ^  \ rS rSrSrS
U 4S jjrSS.     SS jjr\SS j5       r\R                  SS j5       rS	r
U =r$ )WholeStepTrilli  a	  
A trill that yields whole steps no matter what.

>>> wholeTrill = expressions.WholeStepTrill()
>>> wholeTrill.placement
'above'
>>> wholeTrill.getSize(note.Note('C4'))
<music21.interval.Interval M2>

Here the key signature of one sharp will not affect the trill:

>>> n = note.Note('B4', type='eighth')
>>> m = stream.Measure()
>>> m.insert(0, key.KeySignature(1))
>>> m.append(n)
>>> wholeTrill.realize(n)
([<music21.note.Note B>,
  <music21.note.Note C#>,
  <music21.note.Note B>,
  <music21.note.Note C#>], None, [])
c                   > SU;   a  [        S5      e[        TU ]  " S0 UD6  [        R                  " S5      U l        SU l        g )Nr/  z0Cannot initialize WholeStepTrill with accidentalr  Fr9   )r7   rF   rG   r   r   r  r  rI   s     r   rG   WholeStepTrill.__init__1  sH    8#%BD D$8$5=5F5Ft5L(-%r   Nr   c                   U R                   $ rs   r  r  s      r   rZ  WholeStepTrill.getSize:  r  r   c                    g rs   r9   rP   s    r   r/  WholeStepTrill.accidentalB  r  r   c                    [        S5      e)Nz'Cannot set accidental of WholeStepTrillr  rA  s     r   r/  r   F  s    !"KLLr   )r  r  r   r  r  r  r  ra   s   @r   r  r    sc    *. )-	# # &	#
 
#   M Mr   r  c                  ,   ^  \ rS rSrSrU 4S jrSrU =r$ )ShakeiK  zO
A slower trill.

>>> shake = expressions.Shake()
>>> shake.quarterLength
0.25
c                4   > [         TU ]  " S0 UD6  SU l        g )N      ?r9   )rF   rG   r  rI   s     r   rG   Shake.__init__S  s    $8$!r   )r  r   ra   s   @r   r  r  K  s    " "r   r  c                  ,   ^  \ rS rSrSrU 4S jrSrU =r$ )	Schleiferi\  zd
A slide or culee

* Changed in v7: size is a Generic second.  removed unused nachschlag component.
c                j   > [         TU ]  " S0 UD6  [        R                  " S5      U l        SU l        g )Nrw   r  r9   )rF   rG   r   rT  sizer  rI   s     r   rG   Schleifer.__init__b  s-    $8$,,Q/	!r   )r  r
  r   ra   s   @r   r  r  \  s    
" "r   r  c            	         ^  \ rS rSr% SrSrS\S'   \R                  SSS.     SU 4S jjjr	\
SS	 j5       r\R                  SS
 j5       r\
SS j5       r\R                  S S j5       r\
S!S j5       r\R                  S"S j5       r\
S#S j5       r\
S$U 4S jj5       rSS.       S%S jjrSS.   S&S jjr\
S'S j5       r\
S'S j5       r\
S(S j5       rSSSSSSSSS.               S)S jjrSSS.       S*S jjrSrU =r$ )+Turnii  a$  
A turn or Gruppetto.

* Changed in v7: size is a Generic second.  removed unused nachschlag component.
* Changed in v9: Added support for delayed vs non-delayed Turn.
* Changed in v9: Support upper and lower accidentals on turns. This also adds
  the concept of ornamental pitches that are processed by makeAccidentals.
  The size property has been removed and replaced with `.getSize()` (which requires
  a `srcObj` and optional `keySig` param, as well as which='upper' or which='lower').
  Added optional `keySig` param to `.realize()` as well.
Fr%  _isInvertedN)delayupperAccidentallowerAccidentalc                  > [         TU ]  " S0 UD6  X l        X0l        SU l        SU l        SU l        SU l        Xl        g )Nr2  r  r          r9   )	rF   rG   _upperAccidental_lowerAccidentalr   rH   r  _delayr  )rJ   r  r  r  rK   rL   s        r   rG   Turn.__init__w  sC     	$8$7F7F%#'+.1
r   c                    U R                   $ )zv
This is the Turn's upperAccidental. Whether or not it is visible is dictated by
the upperAccidental's displayStatus.
r  rP   s    r   r  Turn.upperAccidental  r  r   c                    Xl         g rs   r  )rJ   newUpperAccidentals     r   r  r         2r   c                    U R                   $ )zv
This is the Turn's lowerAccidental. Whether or not it is visible is dictated by
the upperAccidental's displayStatus.
r  rP   s    r   r  Turn.lowerAccidental  r  r   c                    Xl         g rs   r  )rJ   newLowerAccidentals     r   r  r     r  r   c                    U R                   $ )z
If delay is NO_DELAY, the turn is not delayed.

If delay is anything else (an OffsetQL or DEFAULT_DELAY), the turn is delayed.

Note that if you set delay to OffsetQL(0), and then get the delay, you will
get NO_DELAY, not 0.
)r  rP   s    r   r  
Turn.delay  s     {{r   c                r    [        U[        [        45      (       a  US::  a  [        R                  nXl        g )Nr   )r   floatr   r   NO_DELAYr  )rJ   newDelays     r   r  r$    s,     h 122x1}$--Hr   c                <    U R                   [        R                  :g  $ )z}
Whether the Turn is delayed (i.e. between a specific note and the following note) or
not (i.e. exactly on a specific note).
)r  r   r'  rP   s    r   	isDelayedTurn.isDelayed  s     zz]3333r   c                  > [         TU ]  nU R                  [        R                  :X  a  SU-   nO8[        U R                  [        [        45      (       a  SU R                   S3U-   nU R                  c  U R                  bn  US-  nU R                  b.  USU R                  R                  -   -  nU R                  b  US-  nU R                  b  USU R                  R                  -   -  nUS-  nU$ )	a  
returns the name of the Turn/InvertedTurn, which is generally the class
name lowercased, with spaces where a new capital occurs, but also with
a 'delayed' prefix, if the Turn/InvertedTurn is delayed.  If the delay
is of a specific duration, the prefix will include that duration. The
name also will include upper and lower accidentals, if they exist.

Subclasses can override this as necessary.

>>> nonDelayedTurn = expressions.Turn()
>>> nonDelayedTurn.name
'turn'

>>> from music21.common.enums import OrnamentDelay
>>> delayedInvertedTurn = expressions.InvertedTurn(
...     delay=OrnamentDelay.DEFAULT_DELAY,
...     upperAccidental=pitch.Accidental('sharp'),
...     lowerAccidental=pitch.Accidental('natural')
... )
>>> delayedInvertedTurn.name
'delayed inverted turn (upper=sharp, lower=natural)'

>>> delayedBy1Turn = expressions.Turn(
...     delay=1.0, lowerAccidental=pitch.Accidental('double-flat'))
>>> delayedBy1Turn.name
'delayed(delayQL=1.0) turn (lower=double-flat)'

zdelayed zdelayed(delayQL=z) r7  zupper=z, zlower=r8  )
rF   r   r  r   DEFAULT_DELAYr   r&  r   r  r  r:  s     r   r   	Turn.name  s    < w|::444 7*G

UH$566(B7'AG+t/C/C/OtOG##/8d&:&:&?&???''3tOG##/8d&:&:&?&???sNGr   r   c                  US;  a  [        S5      eUR                  (       d  [        R                  " S5      $ UR                  S   nU=(       d<    UR	                  [
        R                  5      =(       d    [
        R                  " S5      n[        R                  " U5      nSUl	        UR                  c  UR                  Ul
        SnUS:X  a1  UR                  [        R                  " S5      S	S
9  U R                  nO0UR                  [        R                  " S5      S	S
9  U R                  nU(       a  Xel	        O UR!                  UR"                  5      Ul	        [        R                  " XE5      $ )a  
Returns the size of one of the turn's two intervals (which='upper'
or which='lower'), given a source note and an optional key signature.
If the key signature is not specified, the source note's context is
searched for the current key signature, and if there is no such key
signature, a key signature with no sharps and no flats will be used.
Any `upperAccidental` or `lowerAccidental` that has been set on the
turn will also be taken into account.  If either has not been set,
the appropriate accidental from the key signature will be used.

If keySig is specified, this can be considered to be a theoretical
question: "If this particular turn were to be attached to this note,
in this key, what would the ('upper' or 'lower') size of the turn
interval be?"
)upperlowerzDTurn.getSize requires 'which' parameter be set to 'upper' or 'lower'r   r   r   Nr0  rw   Tr   rM  )r7   rO  r   r   rP  r   rQ  r   r   r/  rR  rS  r   rT  r  r  rU  rV  )rJ   r*   whichr    rX  rY  r/  s          r   rZ  Turn.getSize  s9   , **%VX X ~~$$T** &r 2 \633C4D4DE\IYIYZ[I\'+}}X'>%)"!!)%4%C%CO",0
G%%h&>&>q&A4%P--J%%h&>&>r&BD%Q--J)3& *0)@)@AUAU)VO&  ;;r   c                  UR                   (       d  gUR                   S   nU R                  USUS9nU R                  USUS9n[        R                  " U5      nUR                  c  UR
                  Ul        UR                  USS9  UR                  R                  S:w  a  UR                  SS9  [        R                  " U5      nUR                  c  UR
                  Ul        UR                  USS9  UR                  R                  S:w  a  UR                  SS9  U R                  bM  UR                  c  [        R                  " S5      Ul        U R                  R                  UR                  l        U R                  bM  UR                  c  [        R                  " S5      Ul        U R                  R                  UR                  l        Xg4U l        g)	at  
Computes and stores the ornamental pitches for a Turn, given the srcObj
(can be any kind of ornamented GeneralNote) and an optional keySig.

If keySig is None, srcNote's context will be searched for a key signature.
If no key signature is found, a key signature with no sharps and no flats
will be used.

e.g. A turn on a D in a key with two flats (upper ornamental pitch will be E flat,
lower ornamental pitch will be C).

>>> twoFlats = key.KeySignature(sharps=-2)
>>> n1 = note.Note('D4')
>>> turn = expressions.Turn()
>>> turn.resolveOrnamentalPitches(n1, keySig=twoFlats)
>>> turn.ornamentalPitches
(<music21.pitch.Pitch E-4>, <music21.pitch.Pitch C4>)
>>> turn.upperOrnamentalPitch
<music21.pitch.Pitch E-4>
>>> turn.lowerOrnamentalPitch
<music21.pitch.Pitch C4>

e.g. A turn with a sharp over it and a flat under it, on a C, in a key with
no sharps or flats (upper ornamental pitch will be D#, lower ornamental pitch
will be B flat).

>>> noSharpsOrFlats = key.KeySignature(0)
>>> n2 = note.Note('C4')
>>> turn = expressions.Turn(
...     upperAccidental=pitch.Accidental('sharp'),
...     lowerAccidental=pitch.Accidental('flat'))
>>> turn.resolveOrnamentalPitches(n2, keySig=noSharpsOrFlats)
>>> turn.ornamentalPitches
(<music21.pitch.Pitch D#4>, <music21.pitch.Pitch B-3>)
>>> turn.upperOrnamentalPitch
<music21.pitch.Pitch D#4>
>>> turn.lowerOrnamentalPitch
<music21.pitch.Pitch B-3>
Nr   r0  r   r1  Tr   r   )rO  rZ  r   r   rR  rS  r   r^  r_  r`  r  r/  r   ra  rb  r  r   )rJ   r*   r    rX  transposeIntervalUptransposeIntervalDown
upperPitch
lowerPitchs           r   r  Turn.resolveOrnamentalPitches#  s   Z ~~ &r 259\\GF 6B 6,7;||GF 8D 8, #'--"9
$ * 9 9J0$?%%*66t6D"&--"9
$ * 9 9J2DA%%*66t6D +$$,(-(8(8(;
%262F2F2T2TJ!!/+$$,(-(8(8(;
%262F2F2T2TJ!!/ $.":r   c                T    [        U R                  5      S:  a  U R                  S   $ g)zt
Returns the turn's upper ornamental pitch.  If resolveOrnamentalPitches
has not yet been called, None is returned.
r$   r   Nr}   r   rP   s    r   upperOrnamentalPitchTurn.upperOrnamentalPitch{  +     t&&'1,**1--r   c                T    [        U R                  5      S:  a  U R                  S   $ g)zt
Returns the turn's lower ornamental pitch.  If resolveOrnamentalPitches
has not yet been called, None is returned.
rw   r$   Nr;  rP   s    r   lowerOrnamentalPitchTurn.lowerOrnamentalPitch  r>  r   c                    U R                   $ )z
Returns any ornamental pitches that have been resolved (see
`resolveOrnamentalPitches`, which must be called first, or an
empty tuple will be returned).
rf  rP   s    r   r  Turn.ornamentalPitches  rl  r   Tr  c               z   U R                   (       d  gU R                  U R                  4U R                  U R                  44 Hu  u  pU	c  M
  U
bR  U
R
                  bE  U	R                  c  [        R                  " S5      U	l        U
R
                  U	R                  l        M_  U	R                  UUUUUUUSS9  Mw     g)aB  
Updates accidental display for a Turn's ornamental pitches (upper and lower).
Defined exactly like Pitch.updateAccidentalDisplay, with two changes:
Instead of self being the pitch to update, self is a Turn whose
ornamentalPitches are to be updated; and we pay no attention to ties,
since ornamental notes cannot be tied.
Nr   Frn  )
r  r<  r  r@  r  rb  r/  r   ra  r   )rJ   r  r  r  r  r  r  r  r  rq  accs              r   r   Turn.updateAccidentalDisplay  s    & %% &&(<(<=&&(<(<=
FA }?s'8'8'D||+','7'7':141B1BALL.--"+)9'5-A&3'55Q(- . /
r   r   c               	   U R                   nUR                  R                   S:X  a  [        S5      eUnUc8  UR                  [        R
                  5      nUc  [        R
                  " S5      nU R                  [        R                  :X  a  SnOU R                  [        R                  :X  a#  [        UR                  R                   S-  5      nO@U R                  n[        R                  (       a  [        U[        [        45      (       d   eUnUR                  R                   U-
  nSn	USU R                   -  :  a+  U R                   (       d  [        S5      e[        US-  5      nO0USU R                   -  :  a  U R                   n[        USU-  -
  5      n	U R"                  (       d#  U R%                  US	US
9n
U R%                  USUS
9nO"U R%                  USUS
9n
U R%                  US	US
9n['        U
5      (       + n/ n[(        R*                  " U5      n/ Ul        XNR                  l         U(       aF  [        R                  (       a!  [        U[.        R0                  5      (       d   eUR3                  U
SS9  [(        R*                  " U5      n/ Ul        XOR                  l         [(        R*                  " U5      n/ Ul        UUR                  l         U(       aF  [        R                  (       a!  [        U[.        R0                  5      (       d   eUR3                  USS9  [(        R*                  " U5      n/ Ul        U	c  UUR                  l         OU	UR                  l         UR5                  U5        UR5                  U5        UR5                  U5        UR5                  U5        U(       a  [7        U5       H  u  nn[        R                  (       a!  [        U[.        R0                  5      (       d   eUS-   nUR8                  R:                  b  MZ  US;   d  Mb  UR=                  UR8                  R>                  5      UR8                  l        M     SnXR,                  ;   a  UR,                  RA                  U 5      nUS:X  a  / SU4$ U(       d  [(        R*                  " U5      nOUnUUR                  l         US:w  a  UR,                  RC                  U5        / UU4$ )a:  
realize a turn.

returns a three-element tuple.
The first element is an empty list because there are no notes at the start of a turn.
The second element is the original note with a duration equal to the delay (but if there
is no delay, the second element is None, because the turn "eats up" the entire note).
The third element is a list of the four turn notes, adding up to the duration of the
original note (less the delay, if there is one).  The four turn notes will either be
of equal duration, or the fourth note will be longer, to "eat up" the entire note.

>>> from  music21 import *
>>> from music21.common.enums import OrnamentDelay
>>> m1 = stream.Measure()
>>> m1.append(key.Key('F', 'major'))
>>> n1 = note.Note('C5')
>>> m1.append(n1)
>>> t1 = expressions.Turn()
>>> t1.realize(n1)
([], None, [<music21.note.Note D>,
            <music21.note.Note C>,
            <music21.note.Note B->,
            <music21.note.Note C>])

>>> m2 = stream.Measure()
>>> m2.append(key.KeySignature(5))
>>> n2 = note.Note('B4', type='quarter')
>>> m2.append(n2)
>>> t2 = expressions.InvertedTurn(delay=OrnamentDelay.DEFAULT_DELAY)
>>> n2.expressions.append(t2)
>>> t2.realize(n2)
([], <music21.note.Note B>, [<music21.note.Note A#>,
                             <music21.note.Note B>,
                             <music21.note.Note C#>,
                             <music21.note.Note B>])

Realizing an expression leaves the original note and expression alone

>>> n2.duration.type
'quarter'
>>> n2.expressions
[<music21.expressions.InvertedTurn>]

If `inPlace` is True then the note is affected and the turn is
removed from `.expressions`:

>>> n2 = note.Note('C4')
>>> n2.duration.type = '32nd'
>>> t2 = expressions.Turn(delay=OrnamentDelay.DEFAULT_DELAY)
>>> _empty, newOrigNote, turnNotes = t2.realize(n2, inPlace=True)
>>> for turnNote in turnNotes:
...     print(turnNote, turnNote.duration.type)
<music21.note.Note D> 256th
<music21.note.Note C> 256th
<music21.note.Note B> 256th
<music21.note.Note C> 256th
>>> n2.duration.type
'64th'
>>> n2.expressions
[]
>>> newOrigNote is n2
True

If the four turn notes (self.quarterLength each) don't add up to the original note
duration, the fourth turn note should be held to the length of any remaining unused
duration.  Here, for example, we have a dotted eighth note total duration, a delay
of a 16th note, and a turn note duration of a triplet 32nd note, leaving the fourth
turn note with a duration of a 16th note.  This sort of turn is seen all over the
music of Weber.

>>> from fractions import Fraction
>>> n3 = note.Note('C4')
>>> n3.quarterLength = 0.75
>>> t3 = expressions.Turn(delay=0.25)
>>> t3.quarterLength = 0.125 * Fraction(2, 3)
>>> _empty, newOrigNote, turnNotes = t3.realize(n3, inPlace=True)
>>> print(newOrigNote, newOrigNote.quarterLength)
<music21.note.Note C> 0.25
>>> for turnNote in turnNotes:
...     print(turnNote, turnNote.quarterLength)
<music21.note.Note D> 1/12
<music21.note.Note C> 1/12
<music21.note.Note B> 1/12
<music21.note.Note C> 0.25

If `.autoScale` is off and the note is not long enough to realize 4
32nd notes, then an exception is raised.

>>> n2 = note.Note('C4')
>>> n2.duration.type = '32nd'
>>> t2 = expressions.Turn()
>>> t2.autoScale = False
>>> t2.realize(n2)
Traceback (most recent call last):
music21.expressions.ExpressionException: The note is not long enough to realize a turn
r   rt  Nr  rw   ru  z-The note is not long enough to realize a turn   r0  r   r1  Tr   r$   )r$   rH  r   )"r  r  r7   rP  r   rQ  r  r   r'  r-  r   r&   r'   r   r&  r   r   r  rZ  r   r   r   r"   r   r  r   r)   rv  r   r/  rU  rV  rw  rx  )rJ   r*   r    r   r   rz  remainderDurationtheDelayturnDurationfourthNoteQLfirstTransposeIntervalsecondTransposeIntervalr  	turnNotesr  r	  	thirdNote
fourthNoter}  r~  r  r  r  s                          r   r%   Turn.realize  s   T ""??((A-%&YZZ  "44S5E5EFM$ # 0 0 3 ::/// #ZZ=666 &v'D'Dq'H IzzH!(UH,=>>>> (447HH&*!d0000>>)*YZZ<!+,EA 2 222 &&E!,!e)"<=L%)\\&'-\%X"&*ll67=l&Y#%)\\&'-\%X"&*ll67=l&Y# "**@!AA46	MM&)	 "	+0(!)TYY7777 6E]]6*
!#
,1)MM&)	 "	+0	(!)TYY7777 7F]]6*
!#
05J-0<J-#$#$ (	2
??%a3333&{77%%-'V2C)6)G)G)UAGG& 3 %%%"..44T:M!i(( MM&1M"M/@,B%%))-8M9--r   )r  r  r   r  r  r   r  rH   )r  OrnamentDelay | OffsetQLr  r  r  r  r  )r  r  )r"  r  )rZ   rS  )r(  rS  )rZ   r%  rY   )r*   r(  r2  r[   r    r$  rZ   r&  r'  r  r)  r  r"  )r;   r<   r=   r>   r\   r  r   r   r'  rG   r_   r  r   r  r  r*  r   rZ  r  r<  r@  r  r   r%   r?   r`   ra   s   @r   r  r  i  s9   
 K
 )6(>(>1515 & /	
 / " % % 3 3 % % 3 3 	 	 \\  4 4 - -h )-6< 6< 6<
 &6< 
6<x )-	V; V; &	V;p     ' ' -137;?15%)#$-1)/ *)/ 1	)/
 #9)/ /)/ #)/ )/ )/ '+)/^ )-Q.(Q. &	Q.
 Q.
0Q. Q.r   r  c                  $    \ rS rSr% SrS\S'   Srg)InvertedTurni	  Tr%  r  r9   N)r;   r<   r=   r>   r  r   r?   r9   r   r   rU  rU  	  s    Kr   rU  c                  N   ^  \ rS rSrSrU 4S jrSSS.       S	S jjrSrU =r$ )
GeneralAppoggiaturai	  rO   c                \   > [         TU ]  " S0 UD6  [        R                  " S5      U l        g )Nrw   r9   rF   rG   r   r   r
  rI   s     r   rG   GeneralAppoggiatura.__init__	  s%    $8$%%a(	r   NFr   c               b   U R                   S;  a  [        S5      eU R                  S:X  a  [        S5      eUR                  R                  S:X  a  [        S5      eUR                  R                  S-  nU R                   S:X  a  U R                  nOU R                  R                  5       n[        R                  " U5      nXFR                  l        [        U5      (       + nU(       aF  [        R                  (       a!  [        U[        R                  5      (       d   eUR                  US	S
9  SnXR                  ;   a  UR                  R!                  U 5      nU(       d  [        R                  " U5      OUn	XIR                  l        US:w  a  U	R                  R#                  U5        U/U	/ 4$ )aF  
realize an appoggiatura

returns a three-element tuple.
The first is the list of notes that the grace note was converted to.
The second is the rest of the note
The third is an empty list (since there are no notes at the end of an appoggiatura)

>>> n1 = note.Note('C4')
>>> n1.quarterLength = 0.5
>>> a1 = expressions.Appoggiatura()
>>> a1.realize(n1)
([<music21.note.Note D>], <music21.note.Note C>, [])


>>> n2 = note.Note('C4')
>>> n2.quarterLength = 1
>>> a2 = expressions.HalfStepInvertedAppoggiatura()
>>> a2.realize(n2)
([<music21.note.Note B>], <music21.note.Note C>, [])
rJ  z=Cannot realize an Appoggiatura if I do not know its directionrO   z8Cannot realize an Appoggiatura if there is no size givenr   rt  rw   rL  Tr   r   )rF  r7   r
  r  r  r  r   r   r   r&   r'   r   r   r  r   r"   rw  rx  )
rJ   r*   r    r   newDurationr  appoggiaturaNoter  r  r  s
             r   r%   GeneralAppoggiatura.realize	  sl   < >>/%OQ Q99?%JL L??((A-%&YZZoo33a7>>V# $		 $		 1 1 3==02=!!/!)*;!<<!"2DII>>>>&&'8$&G%%%"..44T:M5<f-&/:,B%%))-8
 ""M266r   r
  r"  )	r;   r<   r=   r>   rF  rG   r%   r?   r`   ra   s   @r   rW  rW  	  sK    I) )-A7(A7 &	A7
 A7
0A7 A7r   rW  c                      \ rS rSrSrSrg)Appoggiaturai	  rL  r9   Nr;   r<   r=   r>   rF  r?   r9   r   r   ra  ra  	  s    Ir   ra  c                  (   ^  \ rS rSrU 4S jrSrU =r$ )HalfStepAppoggiaturai	  c                \   > [         TU ]  " S0 UD6  [        R                  " S5      U l        g Nr  r9   rY  rI   s     r   rG   HalfStepAppoggiatura.__init__	  %    $8$%%d+	r   r_  r;   r<   r=   r>   rG   r?   r`   ra   s   @r   rd  rd  	      , ,r   rd  c                  (   ^  \ rS rSrU 4S jrSrU =r$ )WholeStepAppoggiaturai	  c                \   > [         TU ]  " S0 UD6  [        R                  " S5      U l        g Nr  r9   rY  rI   s     r   rG   WholeStepAppoggiatura.__init__	  rh  r   r_  ri  ra   s   @r   rl  rl  	  rj  r   rl  c                      \ rS rSrSrSrg)InvertedAppoggiaturai	  rK  r9   Nrb  r9   r   r   rq  rq  	  s    Ir   rq  c                  (   ^  \ rS rSrU 4S jrSrU =r$ )HalfStepInvertedAppoggiaturai	  c                \   > [         TU ]  " S0 UD6  [        R                  " S5      U l        g rf  rY  rI   s     r   rG   %HalfStepInvertedAppoggiatura.__init__	  rh  r   r_  ri  ra   s   @r   rs  rs  	  rj  r   rs  c                  (   ^  \ rS rSrU 4S jrSrU =r$ )WholeStepInvertedAppoggiaturai
  c                \   > [         TU ]  " S0 UD6  [        R                  " S5      U l        g rn  rY  rI   s     r   rG   &WholeStepInvertedAppoggiatura.__init__
  rh  r   r_  ri  ra   s   @r   rw  rw  
  rj  r   rw  c                      \ rS rSrSrg)TremoloExceptioni

  r9   Nr:   r9   r   r   r{  r{  

  r@   r   r{  c                     ^  \ rS rSrSrU 4S jr\S 5       r\R                  S 5       rSSS.       SS	 jjr	S
r
U =r$ )Tremoloi
  a  
A tremolo ornament represents a single-note tremolo, whether measured or unmeasured.

>>> n = note.Note(type='quarter')
>>> trem = expressions.Tremolo()
>>> trem.measured = True  # default
>>> trem.numberOfMarks = 3  # default

>>> trem.numberOfMarks = 'Hi'
Traceback (most recent call last):
music21.expressions.TremoloException: Number of marks must be a number from 0 to 8

>>> trem.numberOfMarks = -1
Traceback (most recent call last):
music21.expressions.TremoloException: Number of marks must be a number from 0 to 8

TODO: (someday) realize triplet Tremolos, etc. differently from other tremolos.
TODO: deal with unmeasured tremolos.
c                B   > [         TU ]  " S0 UD6  SU l        SU l        g )NTrH  r9   )rF   rG   measured_numberOfMarksrI   s     r   rG   Tremolo.__init__"
  s"    $8$r   c                    U R                   $ )zL
The number of marks on the note.  Currently, completely controls playback.
r  rP   s    r   numberOfMarksTremolo.numberOfMarks'
      
 """r   c                     [        U5      nUS:  d  US:  a  [        [        U5      5      eXl        g ! [         a  n[	        S5      UeS nAff = fNr      z,Number of marks must be a number from 0 to 8ry   rz   r[   r  r{  rJ   numves      r   r  r  .
  sU    	c(CQw#' S**"% 	">	   14 
AA

ANFr   c                  SSU R                   -  -  n/ nU(       d  [        R                  " U5      OUnXR                  ;   a  UR                  R	                  U 5        UbH  UR
                  U:  a8  UR                  USS9u  pvUR                  U5        Ub  UR
                  U:  a  M8  Ub  UR                  U5        US/ 4$ )a  
Realize the ornament

>>> n = note.Note(type='quarter')
>>> trem = expressions.Tremolo()
>>> trem.measured = True  # default
>>> trem.numberOfMarks = 3  # default
>>> trem.realize(n)
([<music21.note.Note C>, <music21.note.Note C>, <music21.note.Note C>,
  <music21.note.Note C>, <music21.note.Note C>, <music21.note.Note C>,
  <music21.note.Note C>, <music21.note.Note C>], None, [])
>>> c2 = trem.realize(n)[0]
>>> [ts.quarterLength for ts in c2]
[0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.125]

Same thing with Streams:

>>> n = note.Note(type='quarter')
>>> trem = expressions.Tremolo()
>>> n.expressions.append(trem)
>>> s = stream.Stream()
>>> s.append(n)
>>> s.show('text')
{0.0} <music21.note.Note C>

>>> y = stream.makeNotation.realizeOrnaments(s)
>>> y.show('text')
{0.0} <music21.note.Note C>
{0.125} <music21.note.Note C>
{0.25} <music21.note.Note C>
{0.375} <music21.note.Note C>
{0.5} <music21.note.Note C>
{0.625} <music21.note.Note C>
{0.75} <music21.note.Note C>
{0.875} <music21.note.Note C>


>>> trem.numberOfMarks = 1
>>> y = stream.makeNotation.realizeOrnaments(s)
>>> y.show('text')
{0.0} <music21.note.Note C>
{0.5} <music21.note.Note C>
rw   r   NF)retainOrigin)r  r   r   r"   r  r  splitAtQuarterLengthr)   )rJ   r*   r    r   lengthOfEachobjsConvertedeRemainaddNotes           r   r%   Tremolo.realize:
  s    h 2 2 223/6$--'F&&&&&t,!g&;&;l&J&;;LW\;]G  ) !g&;&;l&J   )tR((r   )r  r  r"  )r;   r<   r=   r>   r\   rG   r_   r  r   r%   r?   r`   ra   s   @r   r}  r}  
  sy    & 
 # # 	 	 )-@)(@) &	@)
 @)
0@) @)r   r}  c                  ,   ^  \ rS rSrSrU 4S jrSrU =r$ )Fermatai~
  a  
Fermatas by default get appended to the last
note if a note is split because of measures.

To override this (for Fermatas or for any
expression) set .tieAttach to 'all' or 'first'
instead of 'last'.

>>> p1 = stream.Part()
>>> p1.append(meter.TimeSignature('6/8'))
>>> n1 = note.Note('D-2')
>>> n1.quarterLength = 6
>>> n1.expressions.append(expressions.Fermata())
>>> p1.append(n1)
>>> #_DOCS_SHOW p1.show()
.. image:: images/expressionsFermata.*
     :width: 193
c                P   > [         TU ]  " S0 UD6  SU l        SU l        SU l        g )Nnormalinvertedlastr9   )rF   rG   shaper  rH   rI   s     r   rG   Fermata.__init__
  s*    $8$
 	r   )r  rH   r  r   ra   s   @r   r  r  ~
  s    $   r   r  c                      \ rS rSrSrg)TrillExtensionExceptioni
  r9   Nr:   r9   r   r   r  r  
  r@   r   r  c                  H   ^  \ rS rSrSrU 4S jrS rS r\" \\SS9r	Sr
U =r$ )	r  i
  a  
A wavy line trill extension, placed between two notes. N
ote that some MusicXML readers include a trill symbol with the wavy line.

>>> s = stream.Stream()
>>> s.repeatAppend(note.Note(), 8)

Create TrillExtension between notes 2 and 3

>>> te = expressions.TrillExtension(s.notes[1], s.notes[2])
>>> s.append(te)  # spanner can go anywhere in the Stream
>>> print(te)
<music21.expressions.TrillExtension <music21.note.Note C><music21.note.Note C>>
c                4   > [         TU ]  " U0 UD6  S U l        g rs   )rF   rG   
_placementrJ   r   rK   rL   s      r   rG   TrillExtension.__init__
  s    /6X6
 r   c                    U R                   $ rs   r  rP   s    r   _getPlacementTrillExtension._getPlacement
  s    r   c                    Ub"  UR                  5       S;  a  [        SU 35      eUb  UR                  5       U l        g g )N)r2  belowzincorrect placement value: )r1  r  r  r   s     r   _setPlacementTrillExtension._setPlacement
  sA    6H!H),Gw*OPP#kkmDO r   a  
        Get or set the placement as either above, below, or None.

        >>> s = stream.Stream()
        >>> s.repeatAppend(note.Note(), 8)
        >>> te = expressions.TrillExtension(s.notes[1], s.notes[2])
        >>> te.placement = 'above'
        >>> te.placement
        'above'

        A setting of None implies that the placement will be determined
        by notation software and no particular placement is demanded.
        )docr  )r;   r<   r=   r>   r\   rG   r  r  r_   r   r?   r`   ra   s   @r   r  r  
  s,    ",  < Ir   r  c                  `   ^  \ rS rSrSrU 4S jr\S 5       r\R                  S 5       rSr	U =r
$ )TremoloSpanneri
  a  
A tremolo that spans multiple notes

>>> ts = expressions.TremoloSpanner()
>>> n1 = note.Note('C')
>>> n2 = note.Note('D')
>>> ts.addSpannedElements([n1, n2])
>>> ts.numberOfMarks = 2
>>> ts
<music21.expressions.TremoloSpanner <music21.note.Note C><music21.note.Note D>>

>>> ts.numberOfMarks = -1
Traceback (most recent call last):
music21.expressions.TremoloException: Number of marks must be a number from 0 to 8
c                P   > [         TU ]  " U0 UD6  S U l        SU l        SU l        g )NTrH  )rF   rG   r   r  r  r  s      r   rG   TremoloSpanner.__init__
  s+    /6X6r   c                    U R                   $ )zE
The number of marks on the note.  Will eventually control playback.
r  rP   s    r   r  TremoloSpanner.numberOfMarks
  r  r   c                     [        U5      nUS:  d  US:  a  [        [        U5      5      eXl        g ! [         a  n[	        S5      UeS nAff = fr  r  r  s      r   r  r  
  sT    	[c(CQw#' S**"% 	["#QRXZZ	[r  )r  r  r   )r;   r<   r=   r>   r\   rG   r_   r  r   r?   r`   ra   s   @r   r  r  
  s=    $  # # [ [r   r  c                  4   ^  \ rS rSrSrSSU 4S jjjrSrU =r$ )ArpeggioMarki
  a  
ArpeggioMark must be applied to a Chord (not to a single Note).

The parameter arpeggioType can be 'normal' (a squiggly line), 'up' (a squiggly line
with an up arrow), 'down' (a squiggly line with a down arrow), or 'non-arpeggio' (a
bracket instead of a squiggly line, used to indicate a non-arpeggiated chord
intervening in a sequence of arpeggiated ones).

>>> am = expressions.ArpeggioMark('normal')
>>> am.type
'normal'

>>> am = expressions.ArpeggioMark('down')
>>> am.type
'down'
c                v   > [         TU ]  " S0 UD6  U(       d  SnUS;  a  [        SSU< S3-   5      eXl        g )Nr  r  rK  rL  znon-arpeggioAArpeggio type must be "normal", "up", "down", or "non-arpeggio", not .r9   rF   rG   rz   r  )rJ   arpeggioTyperK   rL   s      r   rG   ArpeggioMark.__init__  sM    $8$#LGGS(*+  !	r   r  rs   )r  r   r   ra   s   @r   r  r  
  s     	! 	!r   r  c                  D   ^  \ rS rSrSrSS. SU 4S jjjrS	S jrSrU =r$ )
ArpeggioMarkSpanneri  a_  
ArpeggioMarkSpanner is a multi-staff or multi-voice (i.e. multi-chord) arpeggio.
The spanner should contain all the simultaneous Chords that are to be
arpeggiated together.  If there is only one arpeggiated note in a particular staff
or voice (i.e. the rest are in other staves/voices), then in that case only the
spanner can contain a Note.  Do not ever put a Note that is within a Chord into a
spanner; put the Chord in instead.  And do not ever put an ArpeggioMark in a note
or chord's .expressions.

The parameter arpeggioType can be 'normal' (a squiggly line), 'up' (a squiggly line
with an up arrow), 'down' (a squiggly line with a down arrow), or 'non-arpeggio' (a
bracket instead of a squiggly line, used to indicate a non-arpeggiated multi-chord
intervening in a sequence of arpeggiated ones).

>>> ams = expressions.ArpeggioMarkSpanner(arpeggioType='non-arpeggio')
>>> c1 = chord.Chord('C3 E3 G3')
>>> c2 = chord.Chord('C4 E4 G4')
>>> ams.addSpannedElements([c1, c2])
>>> ams.type
'non-arpeggio'
>>> ams
<music21.expressions.ArpeggioMarkSpanner
 <music21.chord.Chord C3 E3 G3><music21.chord.Chord C4 E4 G4>>
r  )r  c               d   > [         TU ]  " U0 UD6  US;  a  [        SSU< S3-   5      eXl        g )Nr  r  r  r  r  )rJ   r  r   rK   rL   s       r   rG   ArpeggioMarkSpanner.__init__5  sI     	/6X6GGS(*+  !	r   c                $   SSK Jn  SSK Jn  / nU  Hg  n[        XBR                  5      (       a  UR                  U5        M0  [        XAR                  5      (       d  ML  UR                  UR                  5        Mi     [        U5      [        U5      4$ )a'  
Return the lowest and highest note spanned by the element,
extracting them from Chords if need be.

>>> ch = chord.Chord(['C4', 'E4', 'G4'])
>>> n = note.Note('C#3')
>>> nonArp = expressions.ArpeggioMarkSpanner([ch, n])
>>> nonArp.noteExtremes()
(<music21.note.Note C#>, <music21.note.Note G>)
r   )chordr   )r   r  r   r   r  r)   Chordextendnotesminmax)rJ   r  r   r  n_or_chs        r   noteExtremes ArpeggioMarkSpanner.noteExtremesB  sh     	" G'99--W%G[[11W]]+	 
 E
CJ''r   r  )r  r[   )rZ   z)tuple[note.Note | None, note.Note | None])	r;   r<   r=   r>   r\   rG   r  r?   r`   ra   s   @r   r  r    s)    4 &.!"! !( (r   r  __main__)r   r&  rZ   r%  )r*   r#  r    r$  )Mr\   
__future__r   r   r~   typingr&   	fractionsr   r   r   r   music21.common.enumsr   music21.common.numberToolsr   music21.common.typesr	   r
   r   r   r   r   r   r'   r   r   r5   Music21Exceptionr7   Music21ObjectrB   rc   StrEnumr   r   Spannerr   r   r   r   r   r   r   r,  r  r  r  r  r  r  r  r  r  r  r  r  r  rU  rW  ra  rd  rl  rq  rs  rw  r{  r}  r  r  r  r  r  r  
_DOC_ORDERr;   mainTestr9   r   r   <module>r     s  	 #       . - )        ?? %)I$I "IZ	,77 	D## DFYKJ YKz  '( '(T#$$$ #+ K + NZ Ndoz oft.X t.r	En EPNg NDOw OF'n 'T Vo  VF W  WHC*H C*LE -LE -L`-MU -M`
"E 
""	" 	"k.8 k.\4 
I7( I7X& ,< ,,L ,. ,#7 ,,$8 ,	|44 	l)h l)` j  >	l;; 	/W__ /d)[W__ )[X!: !::('// :(F 
z r   