
    rhqe              	         % S r SSKJr  SSKJrJr  SSKrSSKrSSK	J
r
  SSK	Jr  SSK	Jr  SSK	Jr  SS	K	Jr  \R                  (       a  SS
K	Jr  \R"                  " S5      r " S S\R&                  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 " 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$ " S, S-\"5      r% " S. S/\"5      r& " S0 S1\"5      r' " S2 S3\5      r( " S4 S5\(5      r) " S6 S7\(5      r* " S8 S9\(5      r+ " S: S;\(5      r, " S< S=\(5      r-S\\\!SS/S\#\$\%\&\'/SSS\)\*\-/SSSSS\/S>.r.S?\/S@'   SFSGSA jjr0  SH SISB jjr1 " SC SD\Rd                  5      r3\\\*/r4\5SE:X  a  SSK	r	\	Rl                  " \35        gg)Jz
This module defines numerous subclasses of
:class:`~music21.clef.Clef`, providing object representations for all
commonly used clefs. Clef objects are often found
within :class:`~music21.stream.Measure` objects.
    )annotations)IterableSequenceN)base)exceptions21)environment)pitch)style)streamclefc                      \ rS rSrSrg)ClefException'    N)__name__
__module____qualname____firstlineno____static_attributes__r       F/home/james-whalen/.local/lib/python3.13/site-packages/music21/clef.pyr   r   '   s    r   r   c                     ^  \ rS rSr% SrSrSSS.rS\S'   \R                  r
S	rSU 4S
 jjrS r\SS j5       r\R                   SS j5       r\SS j5       rSSS.       SS jjrSrU =r$ )Clef,   a  
A Clef is a basic `music21` object for representing musical clefs
(Treble, Bass, etc.)

Some clefs only represent the graphical element of the clef,
such as G clef, which is subclassed by TrebleClef() and FrenchViolinClef().

>>> tc = clef.TrebleClef()
>>> tc
<music21.clef.TrebleClef>
>>> tc.sign
'G'
>>> tc.line
2

Most clefs also have a "lowestLine" function which represents the
:attr:`~music21.pitch.Pitch.diatonicNoteNum` of the note that would fall on the
lowest line if the Clef were put on a five-line staff. (Where C4,C#4,C##4,C-4
etc. = 29, all types of D4 = 30, etc.)

>>> tc.lowestLine
31

**Equality**

Two Clefs are equal if their class is the same, their sign is the same,
their line is the same and their octaveChange is the same.

>>> c1 = clef.PercussionClef()
>>> c2 = clef.NoClef()
>>> c1 == c2
False
>>> c3 = clef.TrebleClef()
>>> c4 = clef.TrebleClef()
>>> c3 == c4
True
>>> c4.octaveChange = -1
>>> c3 == c4
False

Note that these are not equal:

>>> clef.TrebleClef() == clef.GClef(line=2)
False
)signlineoctaveChangea  
            The sign of the clef, generally, 'C', 'G', 'F', 'percussion', 'none' or None.

            >>> alto = clef.AltoClef()
            >>> alto.sign
            'C'
            >>> percussion = clef.PercussionClef()
            >>> percussion.sign
            'percussion'

            Note the difference here:

            >>> clef.Clef().sign is None
            True
            >>> clef.NoClef().sign
            'none'

            a  
            The line, counting from the bottom up, that the clef resides on.

            >>> clef.AltoClef().line
            3
            >>> clef.TenorClef().line
            4

            May be None:

            >>> print(clef.NoClef().line)
            None
            )r   r   dict[str, str]	_DOC_ATTRr   c                P   > [         TU ]  " S0 UD6  S U l        S U l        SU l        g )Nr   r   )super__init__r   r   _octaveChangeselfkeywords	__class__s     r   r"   Clef.__init__   s)    $8$"	"	"#r   c                    g)N r   r%   s    r   _reprInternalClef._reprInternal   s    r   c                    U R                   $ )z
The number of octaves that the clef "transposes", generally 0.

>>> tc = clef.TrebleClef()
>>> tc.octaveChange
0
>>> clef.Treble8vbClef().octaveChange
-1
r#   r+   s    r   r   Clef.octaveChange   s     !!!r   c                    Xl         g Nr/   )r%   newValues     r   r   r0      s    %r   c                    U R                   R                  R                  SS5      nU(       a  US   R                  5       USS -   $ g)a  
Returns the "name" of the clef, from the class name

>>> tc = clef.TrebleClef()
>>> tc.name
'treble'

>>> tc = clef.Treble8vbClef()
>>> tc.name
'treble8vb'

>>> tc = clef.MezzoSopranoClef()
>>> tc.name
'mezzoSoprano'

OMIT_FROM_DOCS

>>> clef.Clef().name
''
r   r*   r      N)r'   r   replacelower)r%   	classNames     r   name	Clef.name   sD    , NN++33FB?	Q<%%')AB-77r   TFfirstLastOnlyextremePitchOnlyc                  [        U[        R                  5      (       a  U/nOUnU(       d  [        S5      eU(       a  [	        US S9n[        US S9nXV/nO#U(       a  [        U5      S:  a  US   US   /nOUnSn[        U [        [        45      (       a  U R                  b  U R                  S-   n	OS	n	U H  n
U
R                  U	-
  nX-  nM     US:  a  g
g)a  
Return a string representing the stem direction for a single
:class:`~music21.pitch.Pitch` object or a list/tuple/Stream of pitches.

>>> P = pitch.Pitch
>>> bc = clef.BassClef()
>>> bc.getStemDirectionForPitches(P('C3'))
'up'

For two pitches, the most extreme pitch determines the direction:

>>> pitchList = [P('C3'), P('B3')]
>>> bc.getStemDirectionForPitches(pitchList)
'down'

If `firstLastOnly` is True (as by default) then only the first and last pitches are
examined, as in a beam group.  Here we have C3, B3, C3, so despite the B in bass
clef being much farther from the center line than either of the Cs, it is stem up:

>>> pitchList.append(P('C3'))
>>> bc.getStemDirectionForPitches(pitchList)
'up'

If `firstLastOnly` is False, then each of the pitches has a weight on the process

>>> bc.getStemDirectionForPitches(pitchList, firstLastOnly=False)
'down'

If extremePitchOnly is True, then whatever pitch is farthest from the center line
determines the direction, regardless of order.  (default False).

>>> bc.getStemDirectionForPitches(pitchList, extremePitchOnly=True)
'down'
>>> pitchList.insert(1, P('C2'))
>>> bc.getStemDirectionForPitches(pitchList, extremePitchOnly=True)
'up'
z:getStemDirectionForPitches cannot operate on an empty listc                    U R                   $ r2   diatonicNoteNumpps    r   <lambda>1Clef.getStemDirectionForPitches.<locals>.<lambda>   
    R5G5Gr   )keyc                    U R                   $ r2   r@   rB   s    r   rD   rE      rF   r   r5   r      #   downup)
isinstancer	   Pitch
ValueErrorminmaxlenPercussionClef	PitchClef
lowestLinerA   )r%   pitchesr<   r=   	pitchListpitchMinpitchMaxrelevantPitchesdifferenceSummidLinepdistanceFromMidLines               r   getStemDirectionForPitchesClef.getStemDirectionForPitches   s    \ gu{{++ 	II YZZ9*GHH9*GHH'2Os9~1(|Yr];O'Od^Y788T__=Xoo)GG A"#"3"3g"=0M ! Ar   )r#   r   r   returnNonerc   intr3   rf   )rc   str)rW   z#pitch.Pitch | Sequence[pitch.Pitch]r<   boolr=   ri   rc   rh   )r   r   r   r   __doc__equalityAttributesr   __annotations__r
   	TextStyle_styleClassclassSortOrderr"   r,   propertyr   setterr9   r`   r   __classcell__r'   s   @r   r   r   ,   s    ,Z :$' !I~  D //KN$ 
" 
" & &  > #!&N2N 	N
 N 
N Nr   r   c                     ^  \ rS rSr% SrSS0rS\S'   SU 4S jjr\SU 4S jj5       r	\	R                  SS	 j5       r	S
rU =r$ )rU   i  z<
Superclass for all other clef subclasses that use pitches.
rV   z
            The diatonicNoteNumber of the lowest line of the clef.
            (Can be `None`)

            >>> clef.TrebleClef().lowestLine
            31
            r   r   c                4   > [         TU ]  " S0 UD6  SU l        g )N   r   )r!   r"   rV   r$   s     r   r"   PitchClef.__init__  s    $8$!r   c                   > [         TU ]  $ )aK  
The number of octaves that the clef "transposes", generally 0.

>>> tc = clef.TrebleClef()
>>> tc.octaveChange
0
>>> clef.Treble8vbClef().octaveChange
-1

Changing octaveChange changes lowestLine (but not vice-versa)

>>> tc.lowestLine
31
>>> tc.octaveChange = 1
>>> tc.lowestLine
38
>>> tc.octaveChange = -1
>>> tc.lowestLine
24
)r!   r   )r%   r'   s    r   r   PitchClef.octaveChange"  s    , w##r   c                x    U R                   nXl         U R                  b  U =R                  X-
  S-  -  sl        g g )N   r#   rV   )r%   r3   oldOctaveChanges      r   r   ry   :  s8    ,,%??&OO :a??O 'r   r|   rb   re   rg   )r   r   r   r   rj   r   rl   r"   rp   r   rq   r   rr   rs   s   @r   rU   rU     sU     	 !I~ " $ $. @ @r   rU   c                  <   ^  \ rS rSr% Sr0 rS\S'   U 4S jrSrU =r	$ )rT   iB  a  
represents a Percussion clef.

>>> pc = clef.PercussionClef()
>>> pc.sign
'percussion'
>>> pc.line is None
True

Percussion clefs should not, technically have a
"lowestLine," but it is a common usage to assume that
in pitch-centric contexts to use the pitch numbers
from treble clef for percussion clefs.  Thus:

>>> pc.lowestLine == clef.TrebleClef().lowestLine
True

* Changed in v7.3: setting `octaveChange` no longer affects
  `lowestLine`
r   r   c                B   > [         TU ]  " S0 UD6  SU l        SU l        g )N
percussionrv   r   )r!   r"   r   rV   r$   s     r   r"   PercussionClef.__init__Y  s!    $8$ 	%r   )rV   r   
r   r   r   r   rj   r   rl   r"   r   rr   rs   s   @r   rT   rT   B  s    ( !#I~"& &r   rT   c                  <   ^  \ rS rSr% Sr0 rS\S'   U 4S jrSrU =r	$ )NoClefi_  z
represents the absence of a Clef.

>>> nc = clef.NoClef()
>>> nc.sign
'none'

Note that the sign is the string 'none' not the None object

>>> nc.sign is None
False
r   r   c                4   > [         TU ]  " S0 UD6  SU l        g )Nnoner   r!   r"   r   r$   s     r   r"   NoClef.__init__n  s    $8$	r   r   r   rs   s   @r   r   r   _  s     !#I~" r   r   c                  ,   ^  \ rS rSrSrU 4S jrSrU =r$ )
JianpuClefis  z
Jianpu notation does not use a clef, but musicxml marks it
with a specialized "jianpu" sign.

>>> jc = clef.JianpuClef()
>>> jc.sign
'jianpu'
c                4   > [         TU ]  " S0 UD6  SU l        g )Njianpur   r   r$   s     r   r"   JianpuClef.__init__}  s    $8$	r   r   r   r   r   r   rj   r"   r   rr   rs   s   @r   r   r   s  s     r   r   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$ )
TabClefi  zG
represents a Tablature clef.

>>> a = clef.TabClef()
>>> a.sign
'TAB'
c                B   > [         TU ]  " S0 UD6  SU l        SU l        g )NTAB   r   )r!   r"   r   r   r$   s     r   r"   TabClef.__init__  s!    $8$		r   TFr;   c                   g)z5
Overridden to simply return 'down' for guitar tabs.
rL   r   )r%   rX   r<   r=   s       r   r`   "TabClef.getStemDirectionForPitches  s     r   )r   r   )rX   z#pitch.Pitch | Iterable[pitch.Pitch]r<   ri   r=   ri   rc   rh   )	r   r   r   r   rj   r"   r`   r   rr   rs   s   @r   r   r     sE     #!&
4
 	

 
 

 
r   r   c                  ,   ^  \ rS rSrSrU 4S jrSrU =r$ )GClefi  z
A generic G Clef

>>> a = clef.GClef()
>>> a.sign
'G'

If not defined, the lowestLine is set as a Treble Clef (E4 = 31)

>>> a.lowestLine
31
c                4   > [         TU ]  " S0 UD6  SU l        g )NGr   r   r$   s     r   r"   GClef.__init__      $8$	r   r   r   rs   s   @r   r   r     s     r   r   c                  ,   ^  \ rS rSrSrU 4S jrSrU =r$ )FrenchViolinClefi  z
A G Clef that appears in many old French Violin scores,
appearing on the lowest line, and thus higher than
a treble clef.

>>> a = clef.FrenchViolinClef()
>>> a.sign
'G'
>>> a.line
1
c                B   > [         TU ]  " S0 UD6  SU l        SU l        g )Nr5   !   r   r!   r"   r   rV   r$   s     r   r"   FrenchViolinClef.__init__  !    $8$	%r   r   rV   r   rs   s   @r   r   r         
& &r   r   c                  ,   ^  \ rS rSrSrU 4S jrSrU =r$ )
TrebleClefi  z
The most common clef of all, a treble clef.

>>> a = clef.TrebleClef()
>>> a.sign
'G'
>>> a.line
2
>>> a.lowestLine
31
>>> note.Note('E4').pitch.diatonicNoteNum
31
c                B   > [         TU ]  " S0 UD6  SU l        SU l        g )N   rv   r   r   r$   s     r   r"   TrebleClef.__init__  r   r   r   r   rs   s   @r   r   r     s    & &r   r   c                  ,   ^  \ rS rSrSrU 4S jrSrU =r$ )Treble8vbClefi  zq
A vocal tenor treble clef. Also for guitars.

>>> a = clef.Treble8vbClef()
>>> a.sign
'G'
>>> a.octaveChange
-1
c                B   > [         TU ]  " S0 UD6  SU l        SU l        g )NrI      r   r!   r"   r   rV   r$   s     r   r"   Treble8vbClef.__init__  s"    $8$%r   rV   r   r   rs   s   @r   r   r         & &r   r   c                  ,   ^  \ rS rSrSrU 4S jrSrU =r$ )Treble8vaClefi  zu
A treble clef an octave up (such as for piccolos)

>>> a = clef.Treble8vaClef()
>>> a.sign
'G'
>>> a.octaveChange
1
c                B   > [         TU ]  " S0 UD6  SU l        SU l        g )Nr5   r   r   r   r$   s     r   r"   Treble8vaClef.__init__  s"    $8$%r   r   r   rs   s   @r   r   r     r   r   r   c                  ,   ^  \ rS rSrSrU 4S jrSrU =r$ )GSopranoClefi  z
A G clef on the middle line, formerly occasionally used
for soprano parts.

>>> a = clef.GSopranoClef()
>>> a.sign
'G'
>>> a.line
3
c                B   > [         TU ]  " S0 UD6  SU l        SU l        g )N      r   r   r$   s     r   r"   GSopranoClef.__init__  r   r   r   r   rs   s   @r   r   r         	& &r   r   c                  ,   ^  \ rS rSrSrU 4S jrSrU =r$ )CClefi  zI
A generic C Clef, with no line set

>>> a = clef.CClef()
>>> a.sign
'C'
c                4   > [         TU ]  " S0 UD6  SU l        g )NCr   r   r$   s     r   r"   CClef.__init__  r   r   r   r   rs   s   @r   r   r          r   r   c                  ,   ^  \ rS rSrSrU 4S jrSrU =r$ )SopranoClefi  zy
A soprano clef, with C on the lowest line
(found in Bach often)

>>> a = clef.SopranoClef()
>>> a.sign
'C'
>>> a.line
1
c                B   > [         TU ]  " S0 UD6  SU l        SU l        g )Nr5   r   r   r   r$   s     r   r"   SopranoClef.__init__*  r   r   r   r   rs   s   @r   r   r     r   r   r   c                  ,   ^  \ rS rSrSrU 4S jrSrU =r$ )MezzoSopranoClefi0  z
A C clef with C on the second line.  Perhaps
the rarest of the C clefs

>>> a = clef.MezzoSopranoClef()
>>> a.sign
'C'
>>> a.line
2
c                B   > [         TU ]  " S0 UD6  SU l        SU l        g )Nr      r   r   r$   s     r   r"   MezzoSopranoClef.__init__<  r   r   r   r   rs   s   @r   r   r   0  r   r   r   c                  ,   ^  \ rS rSrSrU 4S jrSrU =r$ )AltoClefiB  zW
A C AltoClef, common for violas.

>>> a = clef.AltoClef()
>>> a.sign
'C'
>>> a.line
3
c                B   > [         TU ]  " S0 UD6  SU l        SU l        g )Nr      r   r   r$   s     r   r"   AltoClef.__init__M  r   r   r   r   rs   s   @r   r   r   B  r   r   r   c                  ,   ^  \ rS rSrSrU 4S jrSrU =r$ )	TenorClefiS  z
A C Tenor Clef, often used in bassoon and cello parts
and orchestral trombone parts.

>>> a = clef.TenorClef()
>>> a.sign
'C'
>>> a.line
4

c                B   > [         TU ]  " S0 UD6  SU l        SU l        g )NrJ      r   r   r$   s     r   r"   TenorClef.__init__`  r   r   r   r   rs   s   @r   r   r   S  r   r   r   c                  ,   ^  \ rS rSrSrU 4S jrSrU =r$ )CBaritoneClefif  zp
A Baritone C clef (as opposed to an F Baritone Clef)

>>> a = clef.CBaritoneClef()
>>> a.sign
'C'
>>> a.line
5
c                B   > [         TU ]  " S0 UD6  SU l        SU l        g )Nr      r   r   r$   s     r   r"   CBaritoneClef.__init__q  r   r   r   r   rs   s   @r   r   r   f  r   r   r   c                  ,   ^  \ rS rSrSrU 4S jrSrU =r$ )FClefix  zI
A generic F-Clef, like a Bass clef

>>> a = clef.FClef()
>>> a.sign
'F'
c                4   > [         TU ]  " S0 UD6  SU l        g )NFr   r   r$   s     r   r"   FClef.__init__  r   r   r   r   rs   s   @r   r   r   x  r   r   r   c                  ,   ^  \ rS rSrSrU 4S jrSrU =r$ )FBaritoneClefi  z
an F Baritone Clef

>>> a = clef.FBaritoneClef()
>>> a.sign
'F'
>>> a.line
3
>>> b = clef.CBaritoneClef()
>>> a.lowestLine == b.lowestLine
True
>>> a.sign == b.sign
False
c                B   > [         TU ]  " S0 UD6  SU l        SU l        g )Nr   r   r   r   r$   s     r   r"   FBaritoneClef.__init__  r   r   r   r   rs   s   @r   r   r     s    & &r   r   c                  ,   ^  \ rS rSrSrU 4S jrSrU =r$ )BassClefi  z>
A standard Bass Clef

>>> a = clef.BassClef()
>>> a.sign
'F'
c                B   > [         TU ]  " S0 UD6  SU l        SU l        g )NrJ      r   r   r$   s     r   r"   BassClef.__init__  r   r   r   r   rs   s   @r   r   r         & &r   r   c                  ,   ^  \ rS rSrSrU 4S jrSrU =r$ )Bass8vbClefi  zp
A bass clef configured to be an octave lower.

>>> a = clef.Bass8vbClef()
>>> a.sign
'F'
>>> a.octaveChange
-1
c                P   > [         TU ]  " S0 UD6  SU l        SU l        SU l        g )NrJ   rI   r   r   r!   r"   r   r   rV   r$   s     r   r"   Bass8vbClef.__init__  s)    $8$	%r   r   rV   r   r   rs   s   @r   r   r     s    & &r   r   c                  ,   ^  \ rS rSrSrU 4S jrSrU =r$ )Bass8vaClefi  zV
A rarely used Bass Clef an octave higher.

>>> a = clef.Bass8vaClef()
>>> a.sign
'F'
c                P   > [         TU ]  " S0 UD6  SU l        SU l        SU l        g )NrJ   r5   r   r   r   r$   s     r   r"   Bass8vaClef.__init__  s)    $8$	%r   r   r   rs   s   @r   r   r     s    & &r   r   c                  ,   ^  \ rS rSrSrU 4S jrSrU =r$ )SubBassClefi  zG
An F clef on the top line.

>>> a = clef.SubBassClef()
>>> a.sign
'F'
c                B   > [         TU ]  " S0 UD6  SU l        SU l        g )Nr      r   r   r$   s     r   r"   SubBassClef.__init__  r   r   r   r   rs   s   @r   r   r     r   r   r   )r   r   r   r   z"dict[str, list[type[Clef] | None]]CLASS_FROM_TYPEc                @   U R                  5       nUR                  5       S;   ax  UR                  5       S:X  a
  [        5       $ UR                  5       S:X  a
  [        5       $ UR                  5       S:X  a
  [	        5       $ UR                  5       S:X  a
  [        5       $ [        U5      S:X  a!  US   R                  5       [        US   5      pCO[        U5      S:X  a1  US   R                  5       nUS	:X  a  SnOUS
:X  a  SnOUS:X  a  SnOSnO[        U5      S:  a  SSK	J
n  UR                  5       n[        U5       H`  nSU;  a  M  XgR                  5       :w  a  US-   UR                  5       :w  a  M7  [        XW5      n[        U[        5      (       d  MY  U" 5       s  $    [        SU-   5      e[        S5      eUS:w  aD  X4U4n	U	S:X  a
  [!        5       $ U	S:X  a
  [#        5       $ U	S:X  a
  [%        5       $ U	S:X  a
  ['        5       $ USL d  USL a  [        SU S35      eUS:  d  US:  a  [        SSU< 3-   5      eU[(        ;   a  [(        U   n
[        U
[*        5      (       d   eX   cU  US	:X  a  [-        5       nO=US
:X  a  [/        5       nO,US:X  a  [1        5       nOUS:X  a  [        5       nO
[3        5       nXKl        OSX   n[6        R8                  (       a  Uc   e[;        U[2        5      (       d   eU" 5       nO[3        5       nX;l        XKl        US:w  a  Xl        U$ )a  
Returns a Clef object given a string like "G2" or "F4" etc.

Does not refer to a violin/guitar string.


>>> tc = clef.clefFromString('G2')
>>> tc
<music21.clef.TrebleClef>
>>> nonStandard1 = clef.clefFromString('F1')
>>> nonStandard1
<music21.clef.FClef>
>>> nonStandard1.line
1
>>> nonStandard2 = clef.clefFromString('D4')
>>> nonStandard2
<music21.clef.PitchClef>
>>> nonStandard2.sign
'D'
>>> nonStandard2.line
4


>>> tc8vb = clef.clefFromString('G2', -1)
>>> tc8vb
<music21.clef.Treble8vbClef>

Three special clefs, Tab, Percussion, and None are also supported.

>>> tabClef = clef.clefFromString('TAB')
>>> tabClef
<music21.clef.TabClef>

Case does not matter.

>>> tc8vb = clef.clefFromString('g2', -1)
>>> tc8vb
<music21.clef.Treble8vbClef>

>>> percussionClef = clef.clefFromString('Percussion')
>>> percussionClef
<music21.clef.PercussionClef>

>>> noClef = clef.clefFromString('None')
>>> noClef
<music21.clef.NoClef>

Invalid line numbers raise an exception:

>>> invalidClef = clef.clefFromString('F6')
Traceback (most recent call last):
music21.clef.ClefException: line number (second character) must be 1-5;
            do not use this function for clefs on special staves such as 'F6'


Can find any clef in the module

>>> clef.clefFromString('Treble')
<music21.clef.TrebleClef>
>>> clef.clefFromString('trebleclef')
<music21.clef.TrebleClef>
>>> clef.clefFromString('treble8vb')
<music21.clef.Treble8vbClef>
)tabr   r   r   r   r   r   r   r   r   r5   r   r   rJ   r   r   F)r   r   r   zCould not find clef z)Entry has clef info but no clef specified)r   r   rI   )r   r   r5   )r   rJ   rI   )r   rJ   r5   zcannot read z$ as clef str, should be G2, F4, etc.r   z<line number (second character) must be 1-5; do not use this z-function for clefs on special staves such as r   ) stripr7   r   rT   r   r   rS   upperrf   music21r   dirgetattrrN   typer   r   r   r   r   r   listr   r   r   rU   r   tTYPE_CHECKING
issubclassr   r   )
clefStringoctaveShiftxnStrthisTypelineNummyselfxnLowerxobjTypeparams	line_listclefObjClefTypes                r   clefFromStringr    s   B E{{}??;;=E!9[[]l*!##[[]f$8O[[]h&<
5zQ$Qx~~/U1X7	Uq8>>#s?G_G_GG	Ua*++-VAQ'')#&(8AGGI(Ef(G'4((y   2U:;;GHHa[1\! ?"{" ?"|#= {"=  5Gu,l5'1UVWW{gkZ MeYWX Y 	Y ?"#H-	)T****%3'S'S'U"!)#+"L )H+++!(I6666jG+a*Nr   c                h   S nSnSnU(       a  U R                  5       OU R                  5       nUR                  nU Hw  nUR                  (       a  M  UR                  (       a  US-  nXS" UR
                  5      -  nMB  UR                  (       d  MU  UR                   H  n	US-  nXS" U	5      -  nM     My     US:X  a  Sn
OXT-  n
U
S:  a
  [        5       $ U(       a  U
S:  a
  [        5       $ U(       d  U
S:  a
  [        5       $ U(       a  U
S:  a
  [        5       $ U
S	:  a
  [        5       $ [        5       $ )
a  
Returns the clef that is the best fit for notes and chords found in this Stream.

>>> import random
>>> a = stream.Stream()
>>> for x in range(30):
...    n = note.Note()
...    n.pitch.midi = random.randint(70, 81)
...    a.insert(n)
>>> b = clef.bestClef(a)
>>> b
<music21.clef.TrebleClef>
>>> b.line
2
>>> b.sign
'G'

>>> c = stream.Stream()
>>> for x in range(10):
...    n = note.Note()
...    n.pitch.midi = random.randint(45, 54)
...    c.insert(n)
>>> d = clef.bestClef(c)
>>> d
<music21.clef.BassClef>
>>> d.line
4
>>> d.sign
'F'

This does not automatically get a flat representation of the Stream.

There are a lot more high notes in `a` (30) than low notes in `c` (10),
but it will not matter here, because the pitches in `a` will not be found:

>>> c.insert(0, a)
>>> clef.bestClef(c)
<music21.clef.BassClef>

But with recursion, it will matter:

>>> clef.bestClef(c, recurse=True)
<music21.clef.TrebleClef>


Notes around middle C can get Treble8vb if the setting is allowed:

>>> clef.bestClef(stream.Stream([note.Note('D4')]))
<music21.clef.TrebleClef>
>>> clef.bestClef(stream.Stream([note.Note('D4')]), allowTreble8vb=True)
<music21.clef.Treble8vbClef>

Streams of extremely high notes or extremely low notes can get
Treble8va or Bass8vb clefs:

>>> clef.bestClef(stream.Stream([note.Note('D7')]))
<music21.clef.Treble8vaClef>
>>> clef.bestClef(stream.Stream([note.Note('C0')]))
<music21.clef.Bass8vbClef>
c                v    U R                   nU R                   S:  a  US-  nU$ U R                   S:  a  US-  nU$ )Nr   r   r   r@   )pInnerheights     r   
findHeightbestClef.<locals>.findHeight  sI    ''!!B&aKF  ##b(bLFr   r   r5   g      =@1             
   )recurseiternotesAndRestsisRestisNoter	   isChordrW   r   r   r   r   r   )	streamObjallowTreble8vbr"  r  
totalNotestotalHeightsIternotesnr^   averageHeights              r   bestClefr0    s   @ JK#*I	0@EE88XX!OJ:agg..KYYYYYa
z!},   Q#0 r	MB.| 2|	MB.		z}r   c                      \ rS rSrS rSrg)Testi  c                2    SSK Jn  U" U [        5       5        g )Nr   )testCopyAll)music21.test.commonTestr4  globals)r%   r4  s     r   testCopyAndDeepcopyTest.testCopyAndDeepcopy  s    7D')$r   r   N)r   r   r   r   r7  r   r   r   r   r2  r2    s    %r   r2  __main__)r   )rc   r   )FF)r(  zstream.Streamrc   rU   )7rj   
__future__r   collections.abcr   r   typingr  unittestr   r   r   r   r	   r
   r  r   EnvironmentenvironLocalMusic21Exceptionr   Music21Objectr   rU   rT   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rl   r  r0  TestCaser2  
_DOC_ORDERr   mainTestr   r   r   <module>rE     s5   # .         ?? &&v.	L11 	
\4 \H/@ /@d&T &:T ( i :I &&u &&& &*&J &"&J &"&5 &(I &% &$&u &$&u &"& &&&E &$I &E &,&u &&% &$&% & &% &"  *lD$	G
-xM	R
dM8[	A$dD'2	51 Yz "k(k`%8 % J)
 zT r   