
    3j              	      d   d Z ddlmZ ddlmZ ddlmZ ddlZddlZddl	Z	ddl
Z
ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZmZ ddlmZmZ g dZ G d dej4                        Z G d dej8                        Z G d d edg d            Z G d d      Zd0dZ d Z!d Z"d Z#d Z$d Z%d Z&d Z'd1d Z(d1d!Z)d1d"Z*d1d#Z+d1d$Z,d2d%Z-d3d4d&Z.d5d'Z/d5d(Z0d1d)Z1 G d* d+ejd                        Z3 G d, d-e
jh                        Z5g d.Z6e7d/k(  rddlZ ejp                  e5       yy)6zn
base classes for searching scores.

See User's Guide, Chapter 43: Searching in and Among Scores for details.
    )annotations)
namedtuple)CallableN)windowed)base)exceptions21)chord)duration)note)MeasureStream)filtersiterator)SearchExceptionSearchMatchStreamSearcherWildcardWildcardDurationapproximateNoteSearchapproximateNoteSearchNoRhythmapproximateNoteSearchOnlyRhythmapproximateNoteSearchWeightedmostCommonMeasureRhythmsnoteNameRhythmicSearchnoteNameSearchrhythmicSearchstreamSearchBasetranslateDiatonicStreamToStringtranslateDurationToBytestranslateIntervalsAndSpeedtranslateNoteTieToBytetranslateNoteToByte translateNoteWithDurationToBytestranslateStreamToStringtranslateStreamToStringNoRhythm!translateStreamToStringOnlyRhythmc                      e Zd ZdZy)r   z
    a wildcard duration (it might define a duration
    in itself, but the methods here will see that it
    is a wildcard of some sort)

    No difference from any other duration.
    N)__name__
__module____qualname____doc__     @/DATA/.local/lib/python3.12/site-packages/music21/search/base.pyr   r   =   s     	r-   r   c                  "     e Zd ZdZ fdZ xZS )r   aY  
    An object that may have some properties defined, but others not that
    matches a single object in a music21 stream.  Equivalent to the
    regular expression "."

    >>> wc1 = search.Wildcard()
    >>> wc1.pitch = pitch.Pitch('C')
    >>> st1 = stream.Stream()
    >>> st1.append(note.Note('D', type='half'))
    >>> st1.append(wc1)
    c                B    t        |   di | t               | _        y )Nr,   )super__init__r   r
   )selfkeywords	__class__s     r.   r2   zWildcard.__init__T   s    $8$(*r-   )r(   r)   r*   r+   r2   __classcell__)r5   s   @r.   r   r   H   s    
+ +r-   r   c                  4    e Zd ZU dZdZdddddZded	<   d
 Zy)r   ze
    A lightweight object representing the match (if any) for a search.  Derived from namedtuple
    r,   z(The first element that matches the list.z%A tuple of all the matching elements.zAThe index in the iterator at which the first element can be foundz+The iterator which produced these elements.elStartelsindexr   zdict[str, str]	_DOC_ATTRc                t    | j                   }t        | j                        }| j                  }d|d| d|dS )NzSearchMatch(elStart=z
, els=len(z	), index=z, iterator=[...]))r9   lenr:   r;   )r3   eselixs       r.   __repr__zSearchMatch.__repr__e   s=    \\]ZZ%bV:bT2&HYZZr-   N)r(   r)   r*   r+   	__slots__r<   __annotations__rB   r,   r-   r.   r   r   Y   s.     IA:XE	!I~ [r-   r   r8   c                  8    e Zd ZdZddZd	dZd
dZd
dZd
dZy)r   a  
    An object that can search through streams for a set of elements
    or notes or something of that sort.

    Create a basic Stream first:

    >>> thisStream = converter.parse('tinynotation: 3/4 c4. d8 e4 g4. a8 f4. c4. d4')
    >>> thisStream.show('text')
    {0.0} <music21.stream.Measure 1 offset=0.0>
        {0.0} <music21.clef.TrebleClef>
        {0.0} <music21.meter.TimeSignature 3/4>
        {0.0} <music21.note.Note C>
        {1.5} <music21.note.Note D>
        {2.0} <music21.note.Note E>
    {3.0} <music21.stream.Measure 2 offset=3.0>
        {0.0} <music21.note.Note G>
        {1.5} <music21.note.Note A>
        {2.0} <music21.note.Note F>
    {6.0} <music21.stream.Measure 3 offset=6.0>
        {0.5} <music21.note.Note C>
        {2.0} <music21.note.Note D>
        {3.0} <music21.bar.Barline type=final>

    Let's create something to search for:

    >>> c = note.Note('C', quarterLength=1.5)
    >>> d = note.Note('D', quarterLength=0.5)
    >>> searchList = [c, d]

    Now create a StreamSearcher:

    >>> ss = search.StreamSearcher(thisStream, searchList)

    `searchList` could also be a Stream in itself.

    Let's configure it for recursive search and to filter so only notes are there:

    >>> ss.recurse = True
    >>> ss.filterNotes = True  # or `.filterNotesAndRests`

    Alternatively, we could have passed in a StreamIterator instead of `thisStream`.

    Now let's configure the algorithms:

    >>> ss.algorithms
    [<function StreamSearcher.wildcardAlgorithm at 0x111b6e340>]

    Wildcard search is a default algorithm that lets you use wildcards.
    I suggest you leave it in place and add to the algorithms list.  We can add the
    `rhythmAlgorithm` to it:

    >>> ss.algorithms.append(search.StreamSearcher.rhythmAlgorithm)
    >>> ss.algorithms
    [<function StreamSearcher.wildcardAlgorithm at 0x111b6e340>,
     <function StreamSearcher.rhythmAlgorithm at 0x11200000>]

    Now run it:

    >>> results = ss.run()
    >>> results
    [SearchMatch(elStart=<music21.note.Note C>, els=len(2), index=0, iterator=[...]),
     SearchMatch(elStart=<music21.note.Note G>, els=len(2), index=3, iterator=[...])]

    >>> results[0].elStart.measureNumber
    1
    >>> results[1].elStart.measureNumber
    2

    Wildcards can be useful:

    >>> searchStream2 = stream.Stream()
    >>> searchStream2.append(note.Note(quarterLength=0.5))
    >>> searchStream2.append(search.Wildcard())
    >>> searchStream2.append(note.Note(quarterLength=1.5))

    >>> ss.searchList = searchStream2
    >>> results = ss.run()
    >>> results
    [SearchMatch(elStart=<music21.note.Note D>, els=len(3), index=1, iterator=[...]),
     SearchMatch(elStart=<music21.note.Note A>, els=len(3), index=4, iterator=[...])]
    >>> results[0].els
    (<music21.note.Note D>, <music21.note.Note E>, <music21.note.Note G>)
    >>> [n.duration.quarterLength for n in results[0].els]
    [0.5, 1.0, 1.5]

    OMIT_FROM_DOCS

    >>> emptyS = stream.Stream()
    >>> ss.searchList = emptyS
    >>> ss.run()
    Traceback (most recent call last):
    music21.search.base.SearchException: the search Stream or list cannot be empty

    why doesn't this work?  thisStream[found].expressions.append(expressions.TextExpression('*'))
    c                    || _         || _        d| _        d| _        d| _        t
        j                  g| _        d | _        y )NF)	streamSearch
searchListrecursefilterNotesfilterNotesAndRestsr   wildcardAlgorithm
algorithmsactiveIterator)r3   rG   rH   s      r.   r2   zStreamSearcher.__init__   sH    <H7A"!&). 
 --. 	
 =Ar-   c                   t        | j                  t        j                        r| j                  }n| j                  r| j                  j	                         }n| j                  j                         }| j                  r3|j                  t        j                  t        j                              }nN| j                  rB|j                  t        j                  t        j                  t        j                  g            }|| _        t#        |      }t%        |      }t%        | j&                        }|dk(  rt)        d      g }||kD  r|S t+        t-        ||            D ]  \  }}d }t/        |      D ]H  }	||	   }
|
d} n=| j&                  |	   }| j0                  D ]  } || |
|      }| n |du r n	|du sGd }J |dusct3        |d   ||| j                         }|j5                  |        |S )Nr   )the search Stream or list cannot be emptyFT)
isinstancerG   r   StreamIteratorrI   iterrK   	addFilterr   ClassFilterr   GeneralNoterJ   Noter	   ChordrN   listr>   rH   r   	enumerater   rangerM   r   append)r3   thisStreamIteratorstreamIteratorElsstreamLengthsearchLengthfoundElsstartPosition	streamElsresultjstreamElsearchElthisAlgorithmsms                 r.   runzStreamSearcher.run   s   d'')@)@A!%!2!2||%)%6%6%>%>%@"%)%6%6%;%;%="''%7%A%A''(8(89&" !!%7%A%A''EKK(@A&" 19=>P9Q,-4??+1!"MNN&(,&O(1(;Ll2[(\$M9F<($Q<# #F??1-%)__M*48DF) &5 U?T>!F# )& U" 1y-I\I\]#/ )]2 r-   c                &    t        |t              ryy)zb
        An algorithm that supports Wildcards -- added by default to the search function.
        TN)rQ   r   r3   rf   rg   s      r.   rL   z StreamSearcher.wildcardAlgorithm  s     h)r-   c                    t        |j                  t              ry|j                  j                  |j                  j                  k7  ryy )NTF)rQ   r
   r   quarterLengthrl   s      r.   rhythmAlgorithmzStreamSearcher.rhythmAlgorithm  s<    h'')9:**h.?.?.M.MMr-   c                l    t        |d      syt        |d      sy|j                  |j                  k7  ryy )NnameF)hasattrrq   rl   s      r.   noteNameAlgorithmz StreamSearcher.noteNameAlgorithm&  s2    x(x(==HMM)r-   N)rG   r   rH   zlist[m21Base.Music21Object])returnzlist[SearchMatch])rf   m21Base.Music21Objectrg   ru   )	r(   r)   r*   r+   r2   rj   rL   ro   rs   r,   r-   r.   r   r   l   s$    ^@A9vr-   r   c                   |t        d      d}d| j                  v r| }n| j                         }t        |      }t	        |      }t	        |      }|dk(  rt        d      g }||kD  r|S t        t        ||            D ]@  \  }	}
t        |      D ]  }|
|   }||   } |||      }|r n |s0|j                  |	       B |S )z
    A basic search function that is used by other search mechanisms,
    which takes in a stream or StreamIterator and a searchList or stream
    and an algorithm to run on each pair of elements to determine if they match.
    Nz%algorithm must be a function not NonerR   r   rP   )	r   classesrI   rY   r>   rZ   r   r[   r\   )thisStreamOrIteratorrH   	algorithmrd   r]   r^   r_   r`   ra   rb   rc   re   rf   rg   s                 r.   r   r   0  s     EFFF/7771199;/0()Lz?LqIJJHl"$-h7H,.W$X y|$A |H!!}Hx2F % OOM* %Y Or-   c                $    d }t        | ||      S )a#  
    Takes two streams -- the first is the stream to be searched and the second
    is a stream of elements whose rhythms must match the first.  Returns a list
    of indices which begin a successful search.

    searches are made based on quarterLength.
    thus a dotted sixteenth-note and a quadruplet (4:3) eighth
    will match each other.

    Example 1: First we will set up a simple stream for searching:

    >>> thisStream = converter.parse('tinynotation: 3/4 c4. d8 e4 g4. a8 f4. c4. r4')
    >>> thisStream.show('text')
    {0.0} <music21.stream.Measure 1 offset=0.0>
        {0.0} <music21.clef.TrebleClef>
        {0.0} <music21.meter.TimeSignature 3/4>
        {0.0} <music21.note.Note C>
        {1.5} <music21.note.Note D>
        {2.0} <music21.note.Note E>
    {3.0} <music21.stream.Measure 2 offset=3.0>
        {0.0} <music21.note.Note G>
        {1.5} <music21.note.Note A>
        {2.0} <music21.note.Note F>
    {6.0} <music21.stream.Measure 3 offset=6.0>
        {0.5} <music21.note.Note C>
        {2.0} <music21.note.Rest quarter>
        {3.0} <music21.bar.Barline type=final>

    Now we will search for all dotted-quarter/eighth elements in the Stream:

    >>> thisStreamIter = thisStream.recurse().notes

    >>> searchStream1 = stream.Stream()
    >>> searchStream1.append(note.Note(quarterLength=1.5))
    >>> searchStream1.append(note.Note(quarterLength=0.5))

    >>> l = search.rhythmicSearch(thisStreamIter, searchStream1)
    >>> l
    [0, 3]
    >>> stream.Stream(thisStreamIter[3:5]).show('text')
    {0.0} <music21.note.Note G>
    {1.5} <music21.note.Note A>

    Slightly more advanced search: we will look for any instances of eighth,
    followed by a note (or other element) of any length, followed by a dotted quarter
    note.  Again, we will find two instances; this time we will tag them both with
    a TextExpression of "*" and then show the original stream:

    >>> searchStream2 = stream.Stream()
    >>> searchStream2.append(note.Note(quarterLength=0.5))
    >>> searchStream2.append(search.Wildcard())
    >>> searchStream2.append(note.Note(quarterLength=1.5))
    >>> l = search.rhythmicSearch(thisStreamIter, searchStream2)
    >>> l
    [1, 4]
    >>> for found in l:
    ...     thisStreamIter[found].lyric = '*'
    >>> #_DOCS_SHOW thisStream.show()

    .. image:: images/searchRhythmic1.*
        :width: 221

    Now we can test the search on a real dataset and show the types
    of preparation that are needed to make it most likely a success.
    We will look through the first movement of Corelli Trio Sonata op. 3 no. 1 (F major)
    looking to see how much more common the first search term (dotted-quarter, eighth)
    is than the second (eighth, anything, dotted-quarter).  In fact, my hypothesis
    was wrong, and the second term is actually more common than the first! (n.b. rests
    are being counted here as well as notes)

    >>> grave = corpus.parse('corelli/opus3no1/1grave')
    >>> term1results = []
    >>> term2results = []
    >>> for p in grave.parts:
    ...    pf = p.flatten().stripTies().notesAndRests  # consider tied notes as one long note
    ...    temp1 = search.rhythmicSearch(pf, searchStream1)
    ...    temp2 = search.rhythmicSearch(pf, searchStream2)
    ...    for found in temp1:
    ...        term1results.append(found)
    ...    for found in temp2:
    ...        term2results.append(found)
    >>> term1results
    [0, 7, 13, 21, 42, 57, 64, 66, 0, 5, 7, 19, 21, 40, 46, 63, 0, 8, 31, 61, 69, 71, 73, 97]
    >>> term2results
    [5, 29, 95]
    >>> float(len(term1results)) / len(term2results)
    8.0
    c                    d|j                   j                  v ry|j                   j                  | j                   j                  k7  ryy)Nr   TF)r
   rw   rn   rf   rg   s     r.   ro   z'rhythmicSearch.<locals>.rhythmAlgorithm  s?    !2!2!:!::**h.?.?.M.MMr-   ry   r   )rx   rH   ro   s      r.   r   r   U  s    r 0*XXr-   c                $    d }t        | ||      S )a  
    >>> thisStream = converter.parse('tinynotation: 3/4 c4 d8 e c d e f c D E c c4 d# e')
    >>> searchList = [note.Note('C'), note.Note('D'), note.Note('E')]
    >>> thisStreamIter = thisStream.recurse().notes

    >>> search.noteNameSearch(thisStreamIter, searchList)
    [0, 3, 7]
    >>> searchList2 = [note.Note('C'), search.Wildcard(), note.Note('E')]
    >>> search.noteNameSearch(thisStreamIter, searchList2)
    [0, 3, 7, 11]
    c                    d|j                   v ryt        |d      syt        | d      sy|j                  | j                  k7  ryy)Nr   Trq   F)rw   rr   rq   r|   s     r.   rs   z)noteNameSearch.<locals>.noteNameAlgorithm  sC    )))x(x(==HMM)r-   r}   r~   )rx   rH   rs   s      r.   r   r     s    
 0*HYZZr-   c                $    d }t        | ||      S )a  
    >>> thisStream = converter.parse('tinynotation: 3/4 c4 d8 e c d e f c D E c c4 d# e')
    >>> searchList = [note.Note('C'), note.Note('D'), note.Note('E')]
    >>> for n in searchList:
    ...     n.duration.type = 'eighth'
    >>> thisStreamIter = thisStream.recurse().notes

    >>> search.noteNameRhythmicSearch(thisStreamIter, searchList)
    [3, 7]

    >>> searchList[0].duration = search.WildcardDuration()
    >>> search.noteNameRhythmicSearch(thisStreamIter, searchList)
    [0, 3, 7]
    c                   d|j                   v ryt        |d      syt        | d      sy|j                  | j                  k7  ryd|j                  j                   v ry|j                  j                  | j                  j                  k7  ryy)Nr   Trq   Fr   )rw   rr   rq   r
   rn   r|   s     r.   noteNameRhythmAlgorithmz7noteNameRhythmicSearch.<locals>.noteNameRhythmAlgorithm  s}    )))x(x(==HMM)!2!2!:!::**h.?.?.M.MMr-   r}   r~   )rx   rH   r   s      r.   r   r     s     $ 0*H_``r-   c                x   d}| j                         j                  }t        |      }g }|D ]f  }|j                         j                  }t        |      }t        j                  |||      j                         }	|	|_        |j                  |	|f       h t        |d       }
|
D cg c]  }|d   	 }}|S c c}w )a  
    searches the list of otherStreams and returns an ordered list of matches
    (each stream will have a new property of matchProbability to show how
    well it matches)

    >>> s = converter.parse("tinynotation: 4/4 c4 d8 e16 FF a'4 b-")
    >>> o1 = converter.parse("tinynotation: 4/4 c4 d8 e GG a' b-4")
    >>> o1.id = 'o1'
    >>> o2 = converter.parse("tinynotation: 4/4 d#2 f A a' G b")
    >>> o2.id = 'o2'
    >>> o3 = converter.parse("tinynotation: 4/4 c8 d16 e32 FF32 a'8 b-8")
    >>> o3.id = 'o3'
    >>> l = search.approximateNoteSearch(s, [o1, o2, o3])
    >>> for i in l:
    ...     print(f'{i.id} {i.matchProbability!r}')
    o1 0.666666...
    o3 0.333333...
    o2 0.083333...
    Nc                    d| d   z
  S N   r   r,   xs    r.   <lambda>z'approximateNoteSearch.<locals>.<lambda>      !ad(r-   keyr   )	flattennotesAndRestsr$   difflibSequenceMatcherratiomatchProbabilityr\   sorted
thisStreamotherStreamsisJunknthisStreamStr
sorterListssnthatStreamStrr   
sortedListr   sortedStreamss                r.   r   r     s    * F**A+A.MJYY[&&/3''}MSSU"5!*%  
(:;J#-.:aQqT:M. /s   'B7c                   d}| j                         j                  j                         }t        |      }g }|D ]t  }|j                         j                  j                         }t        |      }t	        j
                  |||      j                         }	|	|_        |j                  |	|f       v t        |d       }
|
D cg c]  }|d   	 }}|S c c}w )a  
    searches the list of otherStreams and returns an ordered list of matches
    (each stream will have a new property of matchProbability to show how
    well it matches)

    >>> s = converter.parse("tinynotation: 4/4 c4 d8 e16 FF a'4 b-")
    >>> o1 = converter.parse("tinynotation: 4/4 c4 d8 e GG a' b-4")
    >>> o1.id = 'o1'
    >>> o2 = converter.parse("tinynotation: 4/4 d#2 f A a' G b")
    >>> o2.id = 'o2'
    >>> o3 = converter.parse("tinynotation: 4/4 c4 d e GG CCC r")
    >>> o3.id = 'o3'
    >>> l = search.approximateNoteSearchNoRhythm(s, [o1, o2, o3])
    >>> for i in l:
    ...     print(f'{i.id} {i.matchProbability!r}')
    o1 0.83333333...
    o3 0.5
    o2 0.1666666...
    Nc                    d| d   z
  S r   r,   r   s    r.   r   z/approximateNoteSearchNoRhythm.<locals>.<lambda><  r   r-   r   r   )
r   r   streamr%   r   r   r   r   r\   r   r   s                r.   r   r     s    * F**113A3A6MJYY[&&--/7;''}MSSU"5!*%  
(:;J#-.:aQqT:M. /   Cc                   d}| j                         j                  j                         }t        |      }g }|D ]t  }|j                         j                  j                         }t        |      }t	        j
                  |||      j                         }	|	|_        |j                  |	|f       v t        |d       }
|
D cg c]  }|d   	 }}|S c c}w )a  
    searches the list of otherStreams and returns an ordered list of matches
    (each stream will have a new property of matchProbability to show how
    well it matches)

    >>> s = converter.parse("tinynotation: 4/4 c4 d8 e16 FF a'4 b-")
    >>> o1 = converter.parse("tinynotation: 4/4 c4 d8 e GG a' b-4")
    >>> o1.id = 'o1'
    >>> o2 = converter.parse("tinynotation: 4/4 d#2 f A a' G b")
    >>> o2.id = 'o2'
    >>> o3 = converter.parse("tinynotation: 4/4 c4 d e GG CCC r")
    >>> o3.id = 'o3'
    >>> l = search.approximateNoteSearchOnlyRhythm(s, [o1, o2, o3])
    >>> for i in l:
    ...     print(f'{i.id} {i.matchProbability!r}')
    o1 0.5
    o3 0.33...
    o2 0.0
    Nc                    d| d   z
  S r   r,   r   s    r.   r   z1approximateNoteSearchOnlyRhythm.<locals>.<lambda>`  r   r-   r   r   )
r   r   r   r&   r   r   r   r   r\   r   r   s                r.   r   r   A  s    * F**113A5a8MJYY[&&--/9"=''}MSSU"5!*%  
(:;J#-.:aQqT:M. /r   c                    d}| j                         j                  j                         }t        |      }t	        |      }g }|D ]  }|j                         j                  }t        |      }	t	        |      }
t        j                  |||	      j                         }t        j                  |||
      j                         }d|z  |z   dz  }||_        |j                  ||f        t        |d       }|D cg c]  }|d   	 }}|S c c}w )a7  
    searches the list of otherStreams and returns an ordered list of matches
    (each stream will have a new property of matchProbability to show how
    well it matches)

    >>> s = converter.parse("tinynotation: 4/4 c4 d8 e16 FF a'4 b-")
    >>> o1 = converter.parse("tinynotation: 4/4 c4 d8 e GG2 a' b-4")
    >>> o1.id = 'o1'
    >>> o2 = converter.parse("tinynotation: 4/4 AAA4 AAA8 AAA16 AAA16 AAA4 AAA4")
    >>> o2.id = 'o2'
    >>> o3 = converter.parse("tinynotation: 4/4 c8 d16 e32 FF32 a'8 b-8")
    >>> o3.id = 'o3'
    >>> o4 = converter.parse("tinynotation: 4/4 c1 d1 e1 FF1 a'1 b-1")
    >>> o4.id = 'o4'
    >>> l = search.approximateNoteSearchWeighted(s, [o1, o2, o3, o4])
    >>> for i in l:
    ...     print(f'{i.id} {i.matchProbability!r}')
    o3 0.83333...
    o1 0.75
    o4 0.75
    o2 0.25
    N   g      @c                    d| d   z
  S r   r,   r   s    r.   r   z/approximateNoteSearchWeighted.<locals>.<lambda>  r   r-   r   r   )r   r   r   r%   r&   r   r   r   r   r\   r   )r   r   r   r   thisStreamStrPitchesthisStreamStrDurationr   r   r   thatStreamStrPitchesthatStreamStrDurationratioPitchesratioDurationr   r   r   r   s                    r.   r   r   e  s    0 F**113A:1==a@ JYY[&&>rB A" E ..v/C/CEEJUW 	  //0E0EGGLuw 	 \!M1S8"5!*%  
(:;J#-.:aQqT:M. /s   ;Dc                    d}g }| D ].  }|t        |      z  }|s|j                  |j                         0 |s|S ||fS )a  
    takes a stream (or streamIterator) of notesAndRests only and returns
    a string for searching on.

    >>> s = converter.parse("tinynotation: 3/4 c4 d8 r16 FF8. a'8 b-2.")
    >>> sn = s.flatten().notesAndRests
    >>> streamString = search.translateStreamToString(sn)
    >>> print(streamString)
    <P>F<)KQFF_
    >>> len(streamString)
    12

    Chords give the pitch only of the first note and Unpitched objects are
    treated as rests:

    >>> s = stream.Stream([note.Note('C4'), note.Rest(),
    ...                    chord.Chord(['C4', 'E4']), note.Unpitched()])
    >>> streamString = search.translateStreamToString(s)
    >>> list(streamString.encode('utf-8'))
    [60, 80, 127, 80, 60, 80, 127, 80]
     )r#   r\   measureNumber)inputStreamOrIteratorreturnMeasuresbmeasuresr   s        r.   r$   r$     sO    , 	AH"	-a00OOAOO, # 8}r-   c                J   g }g }d}d}d}| D ]  }d}|r|j                   }|j                  r(|r"d}|j                  d       |j                  |       Gd}|r(|j                  |j                  j                  dk(  rd}s|j                  d}|j
                  j                  }	|||	k(  rd}
n
||	kD  rd}
nd}
|	}t        t        |j                  d   j                        |
z         }|j                  |       |j                  |        d	j                  |      }|s|S ||fS )
a  
    Translates a Stream or StreamIterator of Notes and Rests only into a string,
    encoding only the .step (no accidental or octave) and whether
    the note is slower, faster, or the same speed as the previous
    note.

    Skips all but the first note of tie. Skips multiple rests in a row

    Each note gets one byte:

    A-G = note of same length as previous
    H-N = note of longer length than previous
    O-U = note of shorter length than previous
    Z = rest

    >>> s = converter.parse("tinynotation: 3/4 c4 d8~ d16 r16 FF8 F#8 a'8 b-2.")
    >>> streamString = search.translateDiatonicStreamToString(s.recurse().notesAndRests)
    >>> print(streamString)
    CRZFFAI
    >>> len(streamString)
    7

    If returnMeasures is True, returns an array of measureNumbers where each entry represents
    the measure number of the measure of the object at that character position :

    >>> streamString2, measures = search.translateDiatonicStreamToString(s.recurse().notesAndRests,
    ...                                    returnMeasures=True)
    >>> streamString == streamString2
    True
    >>> measures
    [1, 1, 1, 1, 1, 2, 2]
    FNTZstopr         r   )r   isRestr\   tietyper
   rn   chrordpitchesstepjoin)r   r   r   r   previousRestpreviousTie
previousQLr   mNumqlascShiftnewNamejoineds                r.   r   r     s0   D 	AHLKJ"??D88#% Luu}

f 4#UUKZZ%%r!1H"_HH
c!))A,++,x78	? #B WWQZF!!r-   c                   g }g }d}d}d}d}| j                  t        j                        D ]  }|j                  d   j                  } n | D ]$  }d}	|r|j
                  }	|j                  r(|r#d}|j                  d       |j                  |	       Hd}|r(|j                  |j                  j                  dk(  rd}t|j                  d}|j                  j                  }
|||
k(  rd}n
||
kD  rd	}nd
}|
}||j                  d   j                  z
  }|dkD  rd}n|dk  rd}|j                  d   j                  }t        d|z   |z         }|j                  |	       |j                  |       ' dj                  |      }|s|S ||fS )a  
    Translates a Stream (not StreamIterator) of Notes and Rests only into a string,
    encoding only the chromatic distance from the last note and whether
    the note is slower, faster, or the same speed as the previous
    note.

    Skips all but the first note of tie. Skips multiple rests in a row

    Each note gets one byte and encodes up from -13 to 13 (all notes > octave are 13 or -13)

    >>> s = converter.parse("tinynotation: 3/4 c4 d8~ d16 r16 F8 F#8 F8 a'8 b-2")
    >>> sn = s.flatten().notesAndRests.stream()
    >>> streamString = search.translateIntervalsAndSpeed(sn)
    >>> print(streamString)
    Ib RHJ<9
    >>> print([ord(x) for x in streamString])
    [73, 98, 32, 82, 72, 74, 60, 57]
    >>> len(streamString)
    8

    If returnMeasures is True, returns a triplet of whether the last note
    was a rest, whether the last note was tied, what the last quarterLength was, and what the
    last pitches' midi number was

    which can be fed back into this algorithm:

    >>> streamString2, measures = search.translateIntervalsAndSpeed(sn, returnMeasures=True)
    >>> streamString == streamString2
    True
    >>> measures
    [1, 1, 1, 1, 1, 2, 2, 2]
    FN<   r   T r   )   D   r      i    r   )getElementsByClassr   rW   r   midir   r   r\   r   r   r
   rn   r   r   )inputStreamr   r   r   r   r   r   previousMidir   r   r   r   pitchDifferencer   r   s                  r.   r    r    
  s   D 	AHLKJL++DII6yy|(( 7 ??D88#% Luu}

f 4#UUKZZ%%r!1H"_"HH
&1):)::R Os"!Oyy|((b?*X56	I L WWQZF!!r-   c                    d}g }| D ].  }|t        |      z  }|s|j                  |j                         0 |r||fS |S )a  
    takes a stream or streamIterator of notesAndRests only and returns
    a string for searching on, using translateNoteToByte.

    >>> s = converter.parse("tinynotation: 4/4 c4 d e FF a'2 b-2")
    >>> sn = s.flatten().notesAndRests
    >>> search.translateStreamToStringNoRhythm(sn)
    '<>@)QF'

    With returnMeasures, will return a tuple of bytes and a list of measure numbers:

    >>> search.translateStreamToStringNoRhythm(sn, returnMeasures=True)
    ('<>@)QF', [1, 1, 1, 1, 2, 2])
    r   )r"   r\   r   r   r   r   r   r   s        r.   r%   r%   d  sO     	AH	 ##OOAOO,  8}r-   c                    d}g }| D ].  }|t        |      z  }|s|j                  |j                         0 |r||fS |S )ag  
    takes a stream or streamIterator of notesAndRests only and returns
    a string for searching on.

    >>> s = converter.parse("tinynotation: 3/4 c4 d8 e16 FF8. a'8 b-2.")
    >>> sn = s.flatten().notesAndRests
    >>> streamString = search.translateStreamToStringOnlyRhythm(sn)
    >>> print(streamString)
    PF<KF_
    >>> len(streamString)
    6
    r   )r   r\   r   r   s        r.   r&   r&     sO     	AH	%a((OOAOO,  8}r-   c                0   t        | t        j                        rt        | j                  j
                        S t        | t        j                        r9| j                  r"t        | j                  d   j
                        S t        d      S t        d      S )a  
    takes a note.Note object and translates it to a single byte representation in a string.

    currently returns the chr() for the note's midi number. or chr(127) for rests
    and unpitched.

    >>> n = note.Note('C4')
    >>> b = search.translateNoteToByte(n)
    >>> b
    '<'
    >>> ord(b)
    60
    >>> ord(b) == n.pitch.midi
    True

    Chords are currently just searched on the first Note (or treated as a Rest if None)
    r      )	rQ   r   rW   r   pitchr   r	   rX   r   r   s    r.   r"   r"     se    & !TYY177<<  	Au{{	#99qyy|(())s8O3xr-   c                b    t        |       }t        |       }t        |       }|r||z   |z   S ||z   S )aG  
    takes a note.Note object and translates it to a three-byte representation as a string.

    currently returns the chr() for the note's midi number. or chr(127) for rests
    followed by the log of the quarter length (fitted to 1-127, see
    :func:`~music21.search.base.translateDurationToBytes`)
    followed by 's', 'c', or 'e' if includeTieByte is True and there is a tie.

    >>> n = note.Note('C4')
    >>> n.duration.quarterLength = 3  # dotted half
    >>> trans = search.translateNoteWithDurationToBytes(n)
    >>> trans
    '<_'
    >>> (2**(ord(trans[1])/10.0))/256  # approximately 3
    2.828...

    >>> n.tie = tie.Tie('stop')
    >>> trans = search.translateNoteWithDurationToBytes(n)
    >>> trans
    '<_e'

    >>> trans = search.translateNoteWithDurationToBytes(n, includeTieByte=False)
    >>> trans
    '<_'
    )r"   r   r!   )r   includeTieByte	firstByte
secondByte	thirdBytes        r.   r#   r#     s@    6 $A&I)!,J&q)I:%	11:%%r-   c                    | j                   y| j                   j                  dk(  ry| j                   j                  dk(  ry| j                   j                  dk(  ryy)a  
    takes a note.Note object and returns a one-byte representation
    of its tie status.
    's' if start tie, 'e' if stop tie, 'c' if continue tie, and '' if no tie

    >>> n = note.Note('E')
    >>> search.translateNoteTieToByte(n)
    ''

    >>> n.tie = tie.Tie('start')
    >>> search.translateNoteTieToByte(n)
    's'

    >>> n.tie.type = 'continue'
    >>> search.translateNoteTieToByte(n)
    'c'

    >>> n.tie.type = 'stop'
    >>> search.translateNoteTieToByte(n)
    'e'
    r   startr   continuecr   e)r   r   r   s    r.   r!   r!     sL    . 	uu}	
w		
z	!	
v	r-   c                    d}| j                   j                  rNt        t        j                  | j                   j                  dz        dz        }t        t        |d      d      }t        |      }|S )a  
    takes a note.Note object and translates it to a two-byte representation

    currently returns the chr() for the note's midi number. or chr(127) for rests
    followed by the log of the quarter length (fitted to 1-127, see formula below)

    >>> n = note.Note('C4')
    >>> n.duration.quarterLength = 3  # dotted half
    >>> trans = search.translateDurationToBytes(n)
    >>> trans
    '_'
    >>> (2 ** (ord(trans[0]) / 10)) / 256  # approximately 3
    2.828...

    r      
   r   )r
   rn   intmathlog2maxminr   )r   duration1to127r   s      r.   r   r     s`    " NzzTYYqzz'?'?#'EFKLS5q9^$Jr-   c                4   g }| t            D ]  }t        |j                        }d}|D ]/  }|d   |k(  sd}|dxx   dz  cc<   |d   j                  |        n |rQd|d}|j                  }|D ]x  }	t        |	t        j                        sd|d	   j                  j                  z
  }
t        j                  |      }|j                  D ]  }|j                  |
d
        ||d<    n ||d<   |g|d<   |j                  |        t        |d d      }|S )a  
    returns a sorted list of dictionaries
    of the most common rhythms in a stream where
    each dictionary contains:

    number: the number of times a rhythm appears
    rhythm: the rhythm found (with the pitches of the first instance of the rhythm transposed to C5)
    measures: a list of measures containing the rhythm
    rhythmString: a string representation of the rhythm (see translateStreamToStringOnlyRhythm)

    >>> bach = corpus.parse('bwv1.6')
    >>> sortedRhythms = search.mostCommonMeasureRhythms(bach)
    >>> for in_dict in sortedRhythms[0:3]:
    ...     number = in_dict['number']
    ...     rhythmString = in_dict['rhythmString']
    ...     measures = in_dict['measures']
    ...     print(
    ...         f'no: {number} '
    ...         f'rhythmString: {rhythmString}'
    ...     )
    ...     bars = [
    ...         (m.number, str(m.getContextByClass(stream.Part).id))
    ...         for m in measures
    ...     ]
    ...     print(f'bars: {bars!r}')
    ...     in_dict['rhythm'].show('text')
    ...     print('-----')
    no: 34 rhythmString: PPPP
    bars: [(1, 'Soprano'), (2, 'Soprano'), (3, 'Soprano'), ..., (1, 'Alto'), ..., (10, 'Bass')]
    {0.0} <music21.note.Note C>
    {1.0} <music21.note.Note A>
    {2.0} <music21.note.Note F>
    {3.0} <music21.note.Note C>
    -----
    no: 7 rhythmString: ZZ
    bars: [(13, 'Soprano'), (14, 'Soprano'), ..., (14, 'Bass')]
    {0.0} <music21.note.Note C>
    {2.0} <music21.note.Note A>
    -----
    no: 6 rhythmString: ZPP
    bars: [(6, 'Soprano'), (12, 'Soprano'), ..., (18, 'Tenor'), ... (12, 'Bass')]
    {0.0} <music21.note.Note C>
    {2.0} <music21.note.Note B->
    {3.0} <music21.note.Note B->
    -----

    * Changed in v7: bars are ordered first by number, then by part.
    FrhythmStringTnumberr   r   )r   r   H   r   )inPlacerhythmc                    | d   S )Nr   r,   )ks    r.   r   z*mostCommonMeasureRhythms.<locals>.<lambda>g  s    AhKr-   )r   reverse)r   r&   r   r\   notesrQ   r   rW   r   pscopydeepcopy	transposer   )streamIntransposeDiatonicreturnDictsthisMeasurer   rhythmFoundentrynewDictmeasureNotesmeasureNotedistanceToTransposethisMeasureCopyr   sortedDictss                 r.   r   r     s;   b K(89R9RS E^$4"h1$j!((5 !  ,G ',,L+k4995*,|A/D/D/G/G*G'&*mmK&@O,22$7F 3 )8GH%  , %0!#.-GJw'7 ): *?NKr-   c                      e Zd Zy)r   N)r(   r)   r*   r,   r-   r.   r   r   k  s    r-   r   c                      e Zd Zd Zy)Testc                2    ddl m}  || t                      y )Nr   )testCopyAll)music21.test.commonTestr  globals)r3   r  s     r.   testCopyAndDeepcopyzTest.testCopyAndDeepcopyq  s    7D')$r-   N)r(   r)   r*   r  r,   r-   r.   r  r  o  s    %r-   r  )r   r   r   __main__)N)F)r   note.GeneralNotert   str)T)r   r  r   boolrt   r  )r   r  )9r+   
__future__r   collectionsr   collections.abcr   r   r   r   unittestmore_itertoolsr   music21r   m21Baser   r	   r
   r   music21.streamr   r   r   r   __all__Durationr   Music21Objectr   r   r   r   r   r   r   r   r   r   r   r$   r   r    r%   r&   r"   r#   r!   r   r   Music21Exceptionr   TestCaser  
_DOC_ORDERr(   mainTestr,   r-   r.   <module>r'     sB  
 # " $     # #      * ,6	x(( 	+w$$ +"[*],ST [&A AH"J`YF[8"aJ!H!H!H0hDL"^W"t62:!&H F6Qh	l33 	%8 %
 zGT r-   