
    rh                       S 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	\R                  5      r " S
 S\R                  5      r " S S\R                  5      r " S S\R                  5      r " S S\R                  5      r " S S\R                  5      r " S S\R                  5      r " S S\R                  5      r " S S\R                  5      r " S S\R                  5      r " S S\R                  5      r " S S\R                  5      r " S  S!\R                  5      r " S" S#\R                  5      r " S$ S%\R                  5      r " S& S'\R                  5      r " S( S)\R                  5      r " S* S+\R                  5      r " S, S-\R                  5      r  " S. S/\R                  5      r! " S0 S1\R                  5      r" " S2 S3\R                  5      r# " S4 S5\R                  5      r$\\\\\\\\\\\\\\\\\ \!\#\$/r% " S6 S7\RL                  5      r'\(S8:X  a  SSKr\RR                  " \'5        gg)9z&
Original music21 feature extractors.
    )annotationsN)environment)base)textzfeatures.nativec                      \ rS rSrSrg)NativeFeatureException5    N)__name__
__module____qualname____firstlineno____static_attributes__r
       Q/home/james-whalen/.local/lib/python3.13/site-packages/music21/features/native.pyr   r   5   s    r   r   c                  >   ^  \ rS rSrSrSrSU 4S jjrSS jrSrU =r	$ )	QualityFeature9   a  
Extends the jSymbolic QualityFeature to automatically find mode.

Set to 0 if the key signature indicates that
a recording is major, set to 1 if it indicates
that it is minor.  A Music21
addition: if no key mode is found in the piece, or conflicting modes in the keys,
analyze the piece to discover what mode it is most likely in.

Example: Handel, Rinaldo Aria (musicxml) is explicitly encoded as being in Major:

>>> s = corpus.parse('handel/rinaldo/lascia_chio_pianga')
>>> fe = features.native.QualityFeature(s)
>>> f = fe.extract()
>>> f.vector
[0]

now we will try it with the last movement of Schoenberg's opus 19 which has
no mode explicitly encoded in the musicxml but which our analysis routines
believe (having very little to go on) fits the profile of e minor best.

>>> schoenberg19mvmt6 = corpus.parse('schoenberg/opus19', 6)
>>> fe2 = features.native.QualityFeature(schoenberg19mvmt6)
>>> f2 = fe2.extract()
>>> f2.vector
[1]


OMIT_FROM_DOCS

# for monophonic melodies
# incomplete measures / pickups for monophonic melodies

P22c                b   > [         TU ]  " SSU0UD6  SU l        SU l        SU l        SU l        g )NdataOrStreamQualityaa  
            Set to 0 if the Key or KeySignature indicates that
            a recording is major, set to 1 if it indicates
            that it is minor.
            Music21 addition: if no key mode is found in the piece, or conflicting
            modes in the keys, analyze the piece to
            discover what mode it is most likely in.
            T   r
   )super__init__namedescriptionisSequential
dimensionsselfr   keywords	__class__s      r   r   QualityFeature.__init__^   s:    ?l?h?	 !r   c                   U R                   b  U R                  c  [        S5      eU R                   S   nSn[        U5      S:X  aC  US   nUR                  S:X  a  SnOUR                  S:X  a  SnX R                  R
                  S'   gSn[        U5      S:X  a  US   nOQ[        U5      S:  aB  [        5       nU H  nUR                  UR                  5        M      [        U5      S:X  a  US   nUc  U R                   S   nUR                  nUS:X  a  SnOUS:X  a  SnO[        S	5      eX R                  R
                  S'   g)
5
Do processing necessary, storing result in feature.
Nz2Cannot process without a data instance or feature.zflat.getElementsByClass(Key)r   r   majorminorzflat.analyzedKeyzOshould be able to get a mode from something here -- perhaps there are no notes?)	datafeature
ValueErrorlenmodevectorsetaddr   )r!   allKeys
keyFeaturek0useKey
seen_modeskanalyzedModes           r   processQualityFeature.processm   s@    99 4QRR)):;#
w<1Bww'!
G#
%/LL"w<1QZF\AJqvv& :!#
 ! >YY12F{{7"JW$J(0 
 ",Ar   )r   r   r   r   N)returnNone
r   r   r   r   __doc__idr   r8   r   __classcell__r#   s   @r   r   r   9   s    !D 
B/, /,r   r   c                  :   ^  \ rS rSrSrSrSU 4S jjrS rSrU =r	$ )TonalCertainty   a  
>>> s = corpus.parse('bwv66.6')
>>> fe = features.native.TonalCertainty(s)
>>> f = fe.extract()
>>> f.vector
[1.26...]

>>> pitches = [56, 55, 56, 57, 58, 57, 58, 59, 60, 59, 60, 61, 62, 61,
...            62, 63, 64, 63, 64, 65, 66, 65, 66, 67]
>>> s = stream.Stream()
>>> for pitch in pitches:
...   s.append(note.Note(pitch))
>>> features.native.TonalCertainty(s).extract().vector
[0.0]
K1c                b   > [         TU ]  " SSU0UD6  SU l        SU l        SU l        SU l        g )Nr   zTonal Certaintyz^A floating point magnitude value that suggest tonal certainty based on automatic key analysis.r   Fr
   r   r   r   r   r   discreter    s      r   r   TonalCertainty.__init__   s8    ?l?h?%	Ir   c                P    U R                   S   U R                  R                  S'   g)r&   zflat.analyzedKey.tonalCertaintyr   Nr)   r*   r.   )r!   s    r   r8   TonalCertainty.process   s"     "&+L!MAr   r   r   rH   r   r:   r=   rA   s   @r   rC   rC      s      
BN Nr   rC   c                  4   ^  \ rS rSrSrSrSU 4S jjrSrU =r$ )FirstBeatAttackPrevalence   z
NOT IMPLEMENTED!

>>> s = corpus.parse('bwv66.6')
>>> fe = features.native.FirstBeatAttackPrevalence(s)
>>> f = fe.extract()
>>> f.vector
[0]

TODO: Implement!
MP1c                b   > [         TU ]  " SSU0UD6  SU l        SU l        SU l        SU l        g )Nr   zFirst Beat Attack PrevalencezMFraction of first beats of a measure that have notes that start on this beat.r   Fr
   rG   r    s      r   r   "FirstBeatAttackPrevalence.__init__   s7    ?l?h?2	7r   rM   r:   )	r   r   r   r   r>   r?   r   r   r@   rA   s   @r   rO   rO      s    
 
B r   rO   c                  :   ^  \ rS rSrSrSrSU 4S jjrS rSrU =r	$ )UniqueNoteQuarterLengths   zr
>>> s = corpus.parse('bwv66.6')
>>> fe = features.native.UniqueNoteQuarterLengths(s)
>>> fe.extract().vector
[3]
QL1c                b   > [         TU ]  " SSU0UD6  SU l        SU l        SU l        SU l        g )Nr   zUnique Note Quarter Lengthsz*The number of unique note quarter lengths.r   Tr
   rG   r    s      r   r   !UniqueNoteQuarterLengths.__init__   s5    ?l?h?1	Gr   c                    SnU R                   S   nU H  nX#   S:  d  M  US-  nM     XR                  R                  S'   g)r&   r   !flat.notes.quarterLengthHistogramr   NrK   r!   counthistokeys       r   r8    UniqueNoteQuarterLengths.process   sH     		=>CzA~
  "'Ar   rM   r:   r=   rA   s   @r   rU   rU      s     
B
' 
'r   rU   c                  :   ^  \ rS rSrSrSrSU 4S jjrS rSrU =r	$ )MostCommonNoteQuarterLength   zw
>>> s = corpus.parse('bwv66.6')
>>> fe = features.native.MostCommonNoteQuarterLength(s)
>>> fe.extract().vector
[1.0]
QL2c                b   > [         TU ]  " SSU0UD6  SU l        SU l        SU l        SU l        g )Nr   zMost Common Note Quarter Lengthz,The value of the most common quarter length.r   Fr
   rG   r    s      r   r   $MostCommonNoteQuarterLength.__init__  s5    ?l?h?5	Ir   c                    U R                   S   nSnSnU H  nX   U:  d  M  X   nUnM     X0R                  R                  S'   g)r&   r[   r   NrK   )r!   r^   maximumqlr_   s        r   r8   #MostCommonNoteQuarterLength.process  sQ     		=>CzW$*	 
 "$Ar   rM   r:   r=   rA   s   @r   rb   rb      s     
B$ $r   rb   c                  :   ^  \ rS rSrSrSrSU 4S jjrS rSrU =r	$ )%MostCommonNoteQuarterLengthPrevalencei  z
Fraction of notes that have the most common quarter length.

>>> s = corpus.parse('bwv66.6')
>>> fe = features.native.MostCommonNoteQuarterLengthPrevalence(s)
>>> fe.extract().vector
[0.60...]
QL3c                b   > [         TU ]  " SSU0UD6  SU l        SU l        SU l        SU l        g )Nr   z*Most Common Note Quarter Length Prevalencez;Fraction of notes that have the most common quarter length.r   Fr
   rG   r    s      r   r   .MostCommonNoteQuarterLengthPrevalence.__init__*  s5    ?l?h?@	Xr   c                    SnU R                   S   nU(       d  [        S5      eSnU H"  nX$   S:  d  M  XU   -  nX$   U:  d  M  X$   nM$     X1-  U R                  R                  S'   g)r&   r   r[   input lacks notesNr)   r   r*   r.   r!   	summationr^   maxKeyr_   s        r   r8   -MostCommonNoteQuarterLengthPrevalence.process2  su     			=>()<==CzA~3Z'	:'"ZF  "(!3Ar   rM   r:   r=   rA   s   @r   rl   rl     s     
B4 4r   rl   c                  :   ^  \ rS rSrSrSrSU 4S jjrS rSrU =r	$ )RangeOfNoteQuarterLengthsiD  z
Difference between the longest and shortest quarter lengths.

>>> s = corpus.parse('bwv66.6')
>>> fe = features.native.RangeOfNoteQuarterLengths(s)
>>> fe.extract().vector
[1.5]
QL4c                b   > [         TU ]  " SSU0UD6  SU l        SU l        SU l        SU l        g )Nr   zRange of Note Quarter Lengthsz<Difference between the longest and shortest quarter lengths.r   Fr
   rG   r    s      r   r   "RangeOfNoteQuarterLengths.__init__O  s5    ?l?h?3	Yr   c                    U R                   S   nU(       d  [        S5      e[        UR                  5       5      n[	        UR                  5       5      nX2-
  U R
                  R                  S'   g)r&   r[   rq   r   N)r)   r   minkeysmaxr*   r.   )r!   r^   minValmaxVals       r   r8   !RangeOfNoteQuarterLengths.processW  sX     		=>()<==UZZ\"UZZ\"!'Ar   rM   r:   r=   rA   s   @r   rx   rx   D  s     
B	1 	1r   rx   c                  :   ^  \ rS rSrSrSrSU 4S jjrS rSrU =r	$ )!UniquePitchClassSetSimultaneitiesim  z
Number of unique pitch class simultaneities.

>>> s = corpus.parse('bwv66.6')
>>> fe = features.native.UniquePitchClassSetSimultaneities(s)
>>> fe.extract().vector
[27]
CS1c                b   > [         TU ]  " SSU0UD6  SU l        SU l        SU l        SU l        g )Nr   z%Unique Pitch Class Set Simultaneitiesz,Number of unique pitch class simultaneities.r   Fr
   rG   r    s      r   r   *UniquePitchClassSetSimultaneities.__init__x  s5    ?l?h?;	Ir   c                    SnU R                   S   nU H  nX#   S:  d  M  US-  nM     XR                  R                  S'   g)r&   r   >chordify.flat.getElementsByClass(Chord).pitchClassSetHistogramr   NrK   r\   s       r   r8   )UniquePitchClassSetSimultaneities.process  sH     		Z[CzA~
  "'Ar   rM   r:   r=   rA   s   @r   r   r   m       
B
' 
'r   r   c                  :   ^  \ rS rSrSrSrSU 4S jjrS rSrU =r	$ )UniqueSetClassSimultaneitiesi  z
Number of unique set class simultaneities.

>>> s = corpus.parse('bwv66.6')
>>> fe = features.native.UniqueSetClassSimultaneities(s)
>>> fe.extract().vector
[14]
CS2c                b   > [         TU ]  " SSU0UD6  SU l        SU l        SU l        SU l        g )Nr   zUnique Set Class Simultaneitiesz*Number of unique set class simultaneities.r   Fr
   rG   r    s      r   r   %UniqueSetClassSimultaneities.__init__  s5    ?l?h?5	Gr   c                    SnU R                   S   nU H  nX#   S:  d  M  US-  nM     XR                  R                  S'   g)r&   r   9chordify.flat.getElementsByClass(Chord).setClassHistogramr   NrK   r\   s       r   r8   $UniqueSetClassSimultaneities.process  sH     		UVCzA~
  "'Ar   rM   r:   r=   rA   s   @r   r   r     r   r   r   c                  :   ^  \ rS rSrSrSrSU 4S jjrS rSrU =r	$ )-MostCommonPitchClassSetSimultaneityPrevalencei  z
Fraction of all pitch class simultaneities that are the most common simultaneity.

>>> s = corpus.parse('bwv66.6')
>>> fe = features.native.MostCommonPitchClassSetSimultaneityPrevalence(s)
>>> fe.extract().vector
[0.134...]
CS3c                b   > [         TU ]  " SSU0UD6  SU l        SU l        SU l        SU l        g )Nr   z3Most Common Pitch Class Set Simultaneity PrevalencezQFraction of all pitch class simultaneities that are the most common simultaneity.r   Fr
   rG   r    s      r   r   6MostCommonPitchClassSetSimultaneityPrevalence.__init__  s7    ?l?h?I	<r   c                   SnU R                   S   nSnU(       d  [        S5      eU H"  nX$   S:  d  M  XU   -  nX$   U:  d  M  X$   nM$     US:w  a  X1-  U R                  R                  S'   gSU R                  R                  S'   g)r&   r   r   rq   Nrr   rs   s        r   r8   5MostCommonPitchClassSetSimultaneityPrevalence.process  s     			Z[()<==CzA~3Z'	:'"ZF  >%+%7DLL"%&DLL"r   rM   r:   r=   rA   s   @r   r   r     s     
B' 'r   r   c                  :   ^  \ rS rSrSrSrSU 4S jjrS rSrU =r	$ )(MostCommonSetClassSimultaneityPrevalencei  ax  
Fraction of all set class simultaneities that the most common simultaneity
occupies.

>>> s = corpus.parse('bwv66.6')
>>> fe = features.native.MostCommonSetClassSimultaneityPrevalence(s)
>>> fe.extract().vector
[0.653...]
>>> s2 = corpus.parse('schoenberg/opus19', 6)
>>> fe2 = features.native.MostCommonSetClassSimultaneityPrevalence(s2)
>>> fe2.extract().vector
[0.235...]
CS4c                b   > [         TU ]  " SSU0UD6  SU l        SU l        SU l        SU l        g )Nr   z-Most Common Set Class Simultaneity PrevalencezOFraction of all set class simultaneities that are the most common simultaneity.r   Fr
   rG   r    s      r   r   1MostCommonSetClassSimultaneityPrevalence.__init__  s8    ?l?h?C	@r   c                   SnU R                   S   nU(       d  [        S5      eSnU H"  nX$   S:  d  M  XU   -  nX$   U:  d  M  X$   nM$     US:w  a  X1-  U R                  R                  S'   gSU R                  R                  S'   g)r&   r   r   rq   Nrr   rs   s        r   r8   0MostCommonSetClassSimultaneityPrevalence.process  s     			UV()<==CzA~3Z'	:'"ZF  >%+%7DLL"%&DLL"r   rM   r:   r=   rA   s   @r   r   r     s     
B' 'r   r   c                  :   ^  \ rS rSrSrSrSU 4S jjrS rSrU =r	$ ) MajorTriadSimultaneityPrevalencei  z
Percentage of all simultaneities that are major triads.

>>> s = corpus.parse('bwv66.6')
>>> fe = features.native.MajorTriadSimultaneityPrevalence(s)
>>> fe.extract().vector
[0.46...]
CS5c                b   > [         TU ]  " SSU0UD6  SU l        SU l        SU l        SU l        g )Nr   z#Major Triad Simultaneity Prevalencez7Percentage of all simultaneities that are major triads.r   Fr
   rG   r    s      r   r   )MajorTriadSimultaneityPrevalence.__init__  5    ?l?h?9	Tr   c                    [        U R                  S   5      nU R                  S   nUS:w  a'  US   US   -   nX1-  U R                  R                  S'   gSU R                  R                  S'   g)r&   'chordify.flat.getElementsByClass(Chord)6chordify.flat.getElementsByClass(Chord).typesHistogramr   isMajorTriadisIncompleteMajorTriadNr,   r)   r*   r.   r!   totalr^   parts       r   r8   (MajorTriadSimultaneityPrevalence.process  m    
 DIIGHI		RSA:(51I+JJD%)\DLL"%&DLL"r   rM   r:   r=   rA   s   @r   r   r          
B' 'r   r   c                  :   ^  \ rS rSrSrSrSU 4S jjrS rSrU =r	$ ) MinorTriadSimultaneityPrevalencei'  z
Percentage of all simultaneities that are minor triads.

>>> s = corpus.parse('bwv66.6')
>>> fe = features.native.MinorTriadSimultaneityPrevalence(s)
>>> fe.extract().vector  # same as major in this work
[0.211...]
CS6c                b   > [         TU ]  " SSU0UD6  SU l        SU l        SU l        SU l        g )Nr   z#Minor Triad Simultaneity Prevalencez7Percentage of all simultaneities that are minor triads.r   Fr
   rG   r    s      r   r   )MinorTriadSimultaneityPrevalence.__init__2  r   r   c                    [        U R                  S   5      nU R                  S   nUS:w  a'  US   US   -   nX1-  U R                  R                  S'   gSU R                  R                  S'   g)r&   r   r   r   isMinorTriadisIncompleteMinorTriadNr   r   s       r   r8   (MinorTriadSimultaneityPrevalence.process:  r   r   rM   r:   r=   rA   s   @r   r   r   '  r   r   r   c                  :   ^  \ rS rSrSrSrSU 4S jjrS rSrU =r	$ )%DominantSeventhSimultaneityPrevalenceiI  z
Percentage of all simultaneities that are dominant seventh.

>>> s = corpus.parse('bwv66.6')
>>> fe = features.native.DominantSeventhSimultaneityPrevalence(s)
>>> fe.extract().vector
[0.076...]
CS7c                b   > [         TU ]  " SSU0UD6  SU l        SU l        SU l        SU l        g )Nr   z(Dominant Seventh Simultaneity Prevalencez;Percentage of all simultaneities that are dominant seventh.r   Fr
   rG   r    s      r   r   .DominantSeventhSimultaneityPrevalence.__init__T  s5    ?l?h?>	Xr   c                    [        U R                  S   5      nU R                  S   nUS:w  a!  US   nX1-  U R                  R                  S'   gSU R                  R                  S'   g)r&   r   r   r   isDominantSeventhNr   r   s       r   r8   -DominantSeventhSimultaneityPrevalence.process\  c    
 DIIGHI		RSA:,-D%)\DLL"%&DLL"r   rM   r:   r=   rA   s   @r   r   r   I  r   r   r   c                  :   ^  \ rS rSrSrSrSU 4S jjrS rSrU =r	$ )%DiminishedTriadSimultaneityPrevalenceik  z
Percentage of all simultaneities that are diminished triads.

>>> s = corpus.parse('bwv66.6')
>>> fe = features.native.DiminishedTriadSimultaneityPrevalence(s)
>>> fe.extract().vector
[0.019...]
CS8c                b   > [         TU ]  " SSU0UD6  SU l        SU l        SU l        SU l        g )Nr   z(Diminished Triad Simultaneity Prevalencez<Percentage of all simultaneities that are diminished triads.r   Fr
   rG   r    s      r   r   .DiminishedTriadSimultaneityPrevalence.__init__v  s5    ?l?h?>	Yr   c                    [        U R                  S   5      nU R                  S   nUS:w  a!  US   nX1-  U R                  R                  S'   gSU R                  R                  S'   g)r&   r   r   r   isDiminishedTriadNr   r   s       r   r8   -DiminishedTriadSimultaneityPrevalence.process~  r   r   rM   r:   r=   rA   s   @r   r   r   k  r   r   r   c                  :   ^  \ rS rSrSrSrSU 4S jjrS rSrU =r	$ )TriadSimultaneityPrevalencei  au  
Gives the proportion of all simultaneities which form triads (major,
minor, diminished, or augmented)


>>> s = corpus.parse('bwv66.6')
>>> fe = features.native.TriadSimultaneityPrevalence(s)
>>> fe.extract().vector
[0.692...]
>>> s2 = corpus.parse('schoenberg/opus19', 2)
>>> fe2 = features.native.TriadSimultaneityPrevalence(s2)
>>> fe2.extract().vector
[0.02272727...]
CS9c                b   > [         TU ]  " SSU0UD6  SU l        SU l        SU l        SU l        g )Nr   zTriad Simultaneity Prevalencez2Proportion of all simultaneities that form triads.r   Fr
   rG   r    s      r   r   $TriadSimultaneityPrevalence.__init__  s5    ?l?h?3	Or   c                    [        U R                  S   5      nU R                  S   nUS:w  a!  US   nX1-  U R                  R                  S'   gSU R                  R                  S'   g)r&   r   r   r   isTriadNr   r   s       r   r8   #TriadSimultaneityPrevalence.process  sb    
 DIIGHI		RSA:#D%)\DLL"%&DLL"r   rM   r:   r=   rA   s   @r   r   r     s     
B' 'r   r   c                  :   ^  \ rS rSrSrSrSU 4S jjrS rSrU =r	$ )'DiminishedSeventhSimultaneityPrevalencei  z
Percentage of all simultaneities that are diminished seventh chords.

>>> s = corpus.parse('bwv66.6')
>>> fe = features.native.DiminishedSeventhSimultaneityPrevalence(s)
>>> fe.extract().vector
[0.0]
CS10c                b   > [         TU ]  " SSU0UD6  SU l        SU l        SU l        SU l        g )Nr   z*Diminished Seventh Simultaneity PrevalencezDPercentage of all simultaneities that are diminished seventh chords.r   Fr
   rG   r    s      r   r   0DiminishedSeventhSimultaneityPrevalence.__init__  s5    ?l?h?@	ar   c                    [        U R                  S   5      nU R                  S   nUS:w  a!  US   nX1-  U R                  R                  S'   gSU R                  R                  S'   g)r&   r   r   r   isDiminishedSeventhNr   r   s       r   r8   /DiminishedSeventhSimultaneityPrevalence.process  sc    
 DIIGHI		RSA:./D%)\DLL"%&DLL"r   rM   r:   r=   rA   s   @r   r   r     s     
B' 'r   r   c                  :   ^  \ rS rSrSrSrSU 4S jjrS rSrU =r	$ )!IncorrectlySpelledTriadPrevalencei  aK  
Percentage of all triads that are spelled incorrectly.

example:

Mozart k155 movement 2 has a single instance of an incorrectly spelled
triad (m. 17, where the C# of an A-major chord has a lower neighbor B#
thus temporarily creating an incorrectly spelled A-minor chord).

We would expect highly chromatic music such as Reger or Wagner to have
a higher percentage, or automatically rendered MIDI
transcriptions (which don't distinguish between D# and Eb).

>>> s = corpus.parse('bwv66.6')
>>> fe = features.native.IncorrectlySpelledTriadPrevalence(s)
>>> fe.extract().vector
[0.02...]
CS11c                b   > [         TU ]  " SSU0UD6  SU l        SU l        SU l        SU l        g )Nr   z$Incorrectly Spelled Triad Prevalencez6Percentage of all triads that are spelled incorrectly.r   Fr
   rG   r    s      r   r   *IncorrectlySpelledTriadPrevalence.__init__  s5    ?l?h?:	Sr   c                    U R                   S   nU(       d  [        S5      eUS   nU R                   S   nSnSU;   a  XCS   -  nSU;   a  XCS   -  nSU;   a  XCS   -  nXB-
  nUS:w  a  XT-  U R                  R                  S'   g
[        S	5      e)r&   r   rq   r   r   r   z3-11z3-12z3-10zinput lacks Forte triadsNrr   )r!   r^   totalCorrectlySpelled	forteDatatotalForteTriadstotalIncorrectlySpelleds         r   r8   )IncorrectlySpelledTriadPrevalence.process  s    
 		RS()<== %i 0IIYZ	Y& 11Y& 11Y& 11"2"Jq %<%ODLL"()CDDr   rM   r:   r=   rA   s   @r   r   r     s     $ 
BE Er   r   c                  :   ^  \ rS rSrSrSrSU 4S jjrS rSrU =r	$ )ChordBassMotionFeaturei  af  
A twelve element feature that reports the fraction
of all chord motion of music21.harmony.Harmony objects
that move up by i-half-steps. (a half-step motion down would
be stored in i = 11).  i = 0 is always 0.0 since consecutive
chords on the same pitch are ignored (unless there are 0 or 1 harmonies, in which case it is 1)

Sample test on Dylan's Blowing In The Wind (not included), showing all
motion is 3rds, 6ths, or especially 4ths and 5ths.

s = corpus.parse('demos/BlowinInTheWind')
fe = features.native.ChordBassMotionFeature(s)
fe.extract().vector

[0.0, 0.0, 0.0, 0.0416..., 0.0416..., 0.166..., 0.0, 0.54166..., 0.0, 0.0, 0.2083... 0.0]

For comparison, the Beatles Here Comes the Sun has more tone motion

[0.0, 0.05..., 0.14..., 0.03..., 0.06..., 0.3..., 0.008..., 0.303...,
 0.0, 0.0, 0.07..., 0.008...]

Post 1990s music has a lot more semitone motion.

CS12c                b   > [         TU ]  " SSU0UD6  SU l        SU l        SU l        SU l        g )Nr   zChord Bass Motionz12-element vector showing the fraction of chords that move by x semitones (where x=0 is always 0 unless there are 0 or 1 harmonies, in which case it is 1).   Fr
   rG   r    s      r   r   ChordBassMotionFeature.__init__*  s:    ?l?h?'	F r   c                <   U R                   S   n/ SQnSnSnU H  nUc  UnM
  UR                  5       b  UR                  5       nOUR                  5       nUR                  5       b  UR                  5       nOUR                  5       nUR                  UR                  :X  a  M  UR                  UR                  -
  S-  nX(==   S-  ss'   US-  nUnM     US:X  a  / SQn	O+/ SQn
[	        SS5       H  n[        X+   5      U-  X'   M     U
n	XR                  l        g)	r&   z flat.getElementsByClass(Harmony))r   r   r   r   r   r   r   r   r   r   r   r   r   Nr   r   )g      ?        r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   )r)   bassroot
pitchClassrangefloatr*   r.   )r!   harms	totMotiontotalHarmonicMotionlastHarmthisHarmlastBassthisBasshalfStepMotionr.   totHarmonicMotionFractionis               r   r8   ChordBassMotionFeature.process4  s   
 		<=8	H#==?.'}}H'}}H==?.'}}H'}}H&&(*=*==&.&9&9H<O<O&OSU%UN-2-'1,''H) , !#QF(N%1b\/4Y\/BEX/X), ".F$r   rM   r:   r=   rA   s   @r   r   r     s    0 
B)% )%r   r   c                      \ rS rSrSrSrSrg)ComposerPopularityic  z
REMOVED in v7 because Google's repsonse no longer includes result counts.
Empty class still here so that id won't be reused, but it's been removed
from this module's list of features.
MD1r
   N)r   r   r   r   r>   r?   r   r
   r   r   r  r  c  s    
 
Br   r  c                  :   ^  \ rS rSrSrSrSU 4S jjrS rSrU =r	$ )LandiniCadenceip  zQ
Return a boolean if one or more Parts end with a Landini-like cadential figure.
MC1c                b   > [         TU ]  " SSU0UD6  SU l        SU l        SU l        SU l        g )Nr   z!Ends With Landini Melodic Contourz\Boolean that indicates the presence of a Landini-like cadential figure in one or more parts.r   Fr
   rG   r    s      r   r   LandiniCadence.__init__v  s8    ?l?h?7	Er   c                L   SS// SQ/n/ nU R                   R                  S:  aM  [        U R                   R                  5       H)  nU R                   S   U   S   nUR                  U5        M+     O U R                   S   nUR                  U5        SnU Hj  n/ nU H  nUS:w  d  M  UR                  U5        M     U H4  n[	        U5      [	        U5      :  d  M  U[	        U5      * S U:X  d  M2  S	n  O   U(       d  Mj    O   U(       a  S
U R
                  R                  S'   gg)r&      )r
  r  r   partscontourListFNTr   )r)   
partsCountr   appendr,   r*   r.   )	r!   matchcBundler   cListfound
cListCleanccMatchs	            r   r8   LandiniCadence.process  s    a+&99!#499//0		'*1-m<u% 1 IIm,ENN5! EJ6%%a(    z?c&k1!3v;,-0F: $   u# $ %&DLL" r   rM   r:   r=   rA   s   @r   r  r  p  s     
B&' &'r   r  c                  :   ^  \ rS rSrSrSrSU 4S jjrS rSrU =r	$ )LanguageFeaturei  a:  
language of text as a number
the number is the index of text.LanguageDetector.languageCodes + 1
or 0 if there is no language.

Detect that the language of a Handel aria is Italian.

>>> s = corpus.parse('handel/rinaldo/lascia_chio_pianga')
>>> fe = features.native.LanguageFeature(s)
>>> fe.extract().vector
[3]

TX1c                   > [         TU ]  " SSU0UD6  SU l        SU l        SU l        SU l        [        R                  " 5       U l        g )Nr   zLanguage FeatureztLanguage of the lyrics of the piece given as a numeric value from text.LanguageDetector.mostLikelyLanguageNumeric().r   Tr
   )	r   r   r   r   r   rH   r   LanguageDetectorlanguageDetectorr    s      r   r   LanguageFeature.__init__  sI    ?l?h?&	\ $ 5 5 7r   c                    U R                   S   nU R                  R                  U5      U R                  R                  S'   g)r&   assembledLyricsr   N)r)   r  mostLikelyLanguageNumericr*   r.   )r!   storedLyricss     r   r8   LanguageFeature.process  s9     yy!23!%!6!6!P!PQ]!^Ar   )r   r   rH   r  r   r:   r=   rA   s   @r   r  r    s      
B8_ _r   r  c                       \ rS rSrS rS rSrg)Testi  c                   SSK Jn  SSK Jn  SSK Jn  UR	                  5       nUR                  UR                  / SQ5      5        UR                  UR                  / SQ5      5        UR                  UR                  / SQ5      5        UR                  UR                  / SQ5      5        UR                  R                  U5      nU R                  [        UR                  5       R                  S   5      S	5        g )
Nr   )streamfeatures)chord)r  eg)r  r,  a)r  d#r-  )r  r/  za--z0.5)music21r(  r*  r+  Streamr  Chordnativer   assertEqualstrextractr.   )r!   r(  r*  r+  sfes         r   %testIncorrectlySpelledTriadPrevalence*Test.testIncorrectlySpelledTriadPrevalence  s    "$!MMO	_-.	_-.	-./	/01__>>qARZZ\0034e<r   c                2   SSK Jn  SSK Jn  UR                  S5      nUR                  R                  U5      nU R                  UR                  5       R                  S   S5        UR                  S5      nUR                  R                  U5      nU R                  UR                  5       R                  S   S5        UR                  S5      nUR                  R                  U5      nU R                  UR                  5       R                  S   S5        g )Nr   )	converterr)  ztinynotation: 3/4 f#4 f# e g2r   ztinynotation: 3/4 f#4 f# f# g2ztinynotation: 3/4 f#4 e a g2)	r0  r<  r*  parser3  r  r4  r6  r.   )r!   r<  r*  r7  r8  s        r   testLandiniCadenceTest.testLandiniCadence  s    %$OO;<__++A.,,Q/3OO<=__++A.,,Q/3OO:;__++A.,,Q/3r   r
   N)r   r   r   r   r9  r>  r   r
   r   r   r&  r&    s    =4r   r&  __main__)*r>   
__future__r   unittestr0  r   music21.featuresr   featuresModuler   EnvironmentenvironLocalFeatureExceptionr   FeatureExtractorr   rC   rO   rU   rb   rl   rx   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  featureExtractorsTestCaser&  r   mainTestr
   r   r   <module>rL     sd   #   3 &&'89>	^<< 	c,^44 c,NN^44 NJ ? ? 8'~>> '<$."A"A $@"4N,K,K "4J1 ? ? 1R'(G(G '@'>#B#B '@''''''T+'~/N/N +'\'~'F'F 'D'~'F'F 'D'N,K,K 'D'N,K,K 'D%'."A"A %'P'n.M.M 'D5E(G(G 5EpN%^<< N%h88 5'^44 5'v_n55 _H )% 1,$$))+% 7 B48 4B zT r   