
    rh                       S SK Jr  S SKrS SKJr  S SKrS SKJr  S SKJr  S SKJ	r	  \R                  " S5      r " S S	5      r " S
 S\5      r " S S5      r " S S\R                  5      r\S:X  a  S SKr\R$                  " \5        gg)    )annotationsN)inf)environment)pitch)musedatazanalysis.enharmonicsc                      \ rS rSrS rSrg)EnharmonicScoreRules   c                <    SU l         SU l        SU l        SU l        g )NF      )sameStaffLinealterationPenaltyaugDimPenaltymixSharpsFlatsPenaltyselfs    V/home/james-whalen/.local/lib/python3.13/site-packages/music21/analysis/enharmonics.py__init__EnharmonicScoreRules.__init__   s"    "!"%*"    )r   r   r   r   N)__name__
__module____qualname____firstlineno__r   __static_attributes__ r   r   r	   r	      s    +r   r	   c                  (   ^  \ rS rSrU 4S jrSrU =r$ )ChordEnharmonicScoreRules    c                0   > [         TU ]  5         SU l        g )Nr   )superr   r   )r   	__class__s    r   r   "ChordEnharmonicScoreRules.__init__!   s    %&"r   )r   )r   r   r   r   r   r   __classcell__)r#   s   @r   r   r       s    ' 'r   r   c                  H    \ rS rSrSr\4S jrS rS rS r	S r
S rS	 rS
rg)EnharmonicSimplifier%   a  
Takes any pitch list input and returns the best enharmonic respelling according to the input
criteria and rule weightings.
Those criteria and rule weightings are currently fixed, but in future the user should be able
to select their own combination and weighting of rules according to preferences,
with predefined defaults for melodic and harmonic norms.
Note: EnharmonicSimplifier itself returns nothing.
c                    [        US   [        5      (       a&  U Vs/ s H  n[        R                  " U5      PM     nnXl        U" 5       U l        S U l        / U l        U R                  5         g s  snf )Nr   )	
isinstancestrr   Pitch	pitchList
ruleObjectallPossibleSpellingsallSpellingsgetRepresentations)r   r-   	ruleClassps       r   r   EnharmonicSimplifier.__init__.   s^    ilC((1:;AQI;"#+$(!! <s    A0c                    / nU R                    H)  nU/UR                  S5      -   nUR                  U5        M+     Xl        g)z
Takes a list of pitches or pitch names and retrieves all enharmonic spellings.
Note: getRepresentations itself returns nothing.
   N)r-   getAllCommonEnharmonicsappendr0   )r   r0   r3   	spellingss       r   r1   'EnharmonicSimplifier.getRepresentations8   sD    
 Aa77::I	*   )r   c                p    [        [        R                  " U R                  6 5      U l        U R                  $ )N)list	itertoolsproductr0   r/   r   s    r   
getProductEnharmonicSimplifier.getProductC   s,    $():):D<M<M)N$O!(((r   c                    U R                  5         / n[        nU R                   HH  nU R                  U5      nU R	                  U5      nU R                  U5      nXE-   U-   nXr:  d  MD  UnUnMJ     U$ )a  
Returns a list of pitches in the best enharmonic
spelling according to the input criteria.

>>> pList1 = [pitch.Pitch('C'), pitch.Pitch('D'), pitch.Pitch('E')]
>>> es = analysis.enharmonics.EnharmonicSimplifier(pList1)
>>> es.bestPitches()
(<music21.pitch.Pitch C>, <music21.pitch.Pitch D>, <music21.pitch.Pitch E>)
>>> pList2 = ['D--', 'E', 'F##']
>>> es = analysis.enharmonics.EnharmonicSimplifier(pList2)
>>> es.bestPitches()
(<music21.pitch.Pitch C>, <music21.pitch.Pitch E>, <music21.pitch.Pitch G>)
)r?   r   r/   getAugDimScoregetAlterationScoregetMixSharpFlatsScore)r   bestPitchesminScorepossibilitythisAugDimScorethisAlterationScorethisMixSharpsFlatScore	thisScores           r   rE    EnharmonicSimplifier.bestPitchesG   s     	44K"11+>O"&"9"9+"F%)%?%?%L"'=@VVI#$) 5 r   c                   U R                   R                  SL a  gSR                  U Vs/ s H  o"R                  PM     sn5      nUR	                  S5      nUR	                  S5      nXE-   S-   U R                   R                  -  nU$ s  snf )z
Returns a score according to the number of sharps and flats in a possible spelling.
The score is the sum of the flats and sharps + 1, multiplied by the alterationPenalty.
Fr6    -#)r.   r   joinnamecountr   rG   r3   joinedPossibility	flatCount
sharpCountscores          r   rC   'EnharmonicSimplifier.getAlterationScoreb   s    
 ??,,5GG[$A[VV[$AB%++C0	&,,S1
'!+t/P/PP	 %Bs   Bc                $   U R                   R                  SL a  gSR                  U Vs/ s H  o"R                  PM     sn5      nUR	                  S5      nUR	                  S5      n[        XE/5      U R                   R                  -  nU$ s  snf )z
Returns a score based on the mixture of sharps and flats in a possible spelling:
the score is given by the number of the lesser used accidental (sharps or flats)
multiplied by the mixSharpsFlatsPenalty.
Fr6   rN   rO   rP   )r.   r   rQ   rR   rS   minrT   s          r   rD   *EnharmonicSimplifier.getMixSharpFlatsScorep   s     ??00E9GG[$A[VV[$AB%++C0	&,,S1
Y+,t/T/TT	 %Bs   Bc                   U R                   R                  SL a  gSn[        [        U5      S-
  5       H  n[        R
                  R                  X   R                     n[        R
                  R                  XS-      R                     nXT-
  S-  nU[        R
                  R                  R                  US5      -  nM     UR                  S5      nUR                  S5      nXx-   S-   U R                   R                  -  n	U	$ )z}
Returns a score based on the number of augmented and diminished intervals between
successive pitches in the given spelling.
Fr6   rN   (   dddAd)r.   r   rangelenr   base40base40RepresentationrR   base40IntervalTablegetrS   )
r   rG   intervalStrip0p1
base40diffdimCountaugCountrX   s
             r   rB   #EnharmonicSimplifier.getAugDimScore   s    
 ??((E1s;'!+,A55kn6I6IJB55ka%6H6M6MNB'RJ8??>>BB:uUUK	 -
 $$S)$$S)$q(DOO,I,IIr   )r/   r0   r-   r.   N)r   r   r   r   __doc__r	   r   r1   r?   rE   rC   rD   rB   r   r   r   r   r'   r'   %   s0     -A "	))6r   r'   c                  ,    \ rS rSrS rS rS rS rSrg)Test   c                n   [         R                  " S5      [         R                  " S5      [         R                  " S5      /n[        U5      nUR                  5       nU R	                  [        U5      S5        U R	                  [        U5      S5        U R                  US   [         R                  5        g )NCDE   r   )r   r,   r'   rE   assertEqualrc   assertIsInstance)r   pListesbestPitchLists       r   testBestPitchesTest.testBestPitches   s    S!5;;s#3U[[5EF!%((UQ']+Q/mA.<r   c                   [         R                  " S5      [         R                  " S5      [         R                  " S5      /n[        U5      n[         R                  " S5      [         R                  " S5      [         R                  " S5      /nUR                  U5      nU R	                  [        U5      S5        U R                  U[        5        g Nru   rv   rw   rx   )r   r,   r'   rC   ry   rc   rz   int)r   r{   r|   posstestAltScores        r   testGetAlterationScoreTest.testGetAlterationScore   s    S!5;;s#3U[[5EF!%(C %++c"2EKK4DE,,T2UQ'lC0r   c                   [         R                  " S5      [         R                  " S5      [         R                  " S5      /n[        U5      n[         R                  " S5      [         R                  " S5      [         R                  " S5      /nUR                  U5      nU R	                  [        U5      S5        U R                  U[        5        g r   )r   r,   r'   rD   ry   rc   rz   r   )r   r{   r|   r   testMixScores        r   testGetMixSharpFlatsScoreTest.testGetMixSharpFlatsScore   s    S!5;;s#3U[[5EF!%(C %++c"2EKK4DE//5UQ'lC0r   c                   [         R                  " S5      [         R                  " S5      [         R                  " S5      /n[        U5      n[         R                  " S5      [         R                  " S5      [         R                  " S5      /nUR                  U5      nU R	                  [        U5      S5        U R                  U[        5        g r   )r   r,   r'   rB   ry   rc   rz   r   )r   r{   r|   r   testAugDimScores        r   testGetAugDimScoreTest.testGetAugDimScore   s    S!5;;s#3U[[5EF!%(C %++c"2EKK4DE++D1UQ'os3r   r   N)	r   r   r   r   r~   r   r   r   r   r   r   r   rr   rr      s    =114r   rr   __main__)
__future__r   r=   mathr   unittestmusic21r   r   r   EnvironmentenvironLocalr	   r   r'   TestCaserr   r   mainTestr   r   r   <module>r      s    #      &&'=>+ +' 4 '
k k\$48 $4P zT r   