
    3jP                    |   d Z ddlmZ ddlZddlmZ ddlZddlZddl	m
Z
 ddlmZ ddlmZ ej                  rddlmZ dd	lmZ  ej"                  d
      Z G d dej&                        Z G d d      Z G d de      Z G d de      Z G d dej0                        Zedk(  rddlZ ej6                  e       yy)za
Tools for grouping notes and chords into a searchable tree
organized by start and stop offsets.
    )annotationsN)inf)
OffsetQLIn)environment)exceptions21)basestreamz
tree.spansc                      e Zd Zy)TimespanExceptionN__name__
__module____qualname__     ?/DATA/.local/lib/python3.12/site-packages/music21/tree/spans.pyr   r   #       r   r   c                  d    e Zd ZdZe efdZd Zd Zed        Z	ed        Z
ddZd	 Zd
 Zd Zy)Timespana+  
    A span of time, with a start offset and stop offset.

    Useful for demonstrating various properties of the timespan-collection class
    family.

    >>> timespan = tree.spans.Timespan(-1.5, 3.25)
    >>> print(timespan)
    <Timespan -1.5 3.25>

    A timespan has two attributes, its offset and its endTime.  They are immutable.

    >>> timespan.offset
    -1.5
    >>> timespan.endTime
    3.25

    To create a changed timespan, call the .new() method on the timespan.

    >>> ts2 = timespan.new(offset=0.0)
    >>> ts2
    <Timespan 0.0 3.25>

    >>> ts3 = timespan.new(endTime=5.0)
    >>> ts3
    <Timespan -1.5 5.0>

    Two timespans are equal if they have the same offset and endTime

    >>> ts4 = tree.spans.Timespan(-1.5, 5.0)
    >>> ts3 == ts4
    True
    >>> ts4 == ts2
    False
    c                    |t        |      }|| _        |t        |      }|| _        ||||kD  rt        d|d|      y y y )Noffset z must be after endTime )float_offset_endTimer   selfoffsetendTimes      r   __init__zTimespan.__init__N   si    6]FGnG'"5''&;RSZR](^__   #6r   c                    t        |       t        |      u r3| j                  |j                  k(  r| j                  |j                  k(  ryy)NTF)typer   r   )r   exprs     r   __eq__zTimespan.__eq__Y   s8    :d#{{dkk)<<4<</r   c                l    t        |       j                  }d| d| j                   d| j                   dS )N< >)r"   r   r   r   r   typeNames     r   __repr__zTimespan.__repr__`   s4    :&&8*Adkk]!DLL>;;r   c                    | j                   S )aM  
        The start offset of the Timespan, relative to its
        containing score.

        >>> score = corpus.parse('bwv66.6')
        >>> scoreTree = score.asTimespans()
        >>> verticality = scoreTree.getVerticalityAt(1.0)
        >>> timespan = verticality.startTimespans[0]
        >>> timespan.offset
        1.0
        )r   r   s    r   r   zTimespan.offsetd   s     ||r   c                    | j                   S )aM  
        The stop offset of the Timespan, relative to its
        containing score.

        >>> score = corpus.parse('bwv66.6')
        >>> scoreTree = score.asTimespans()
        >>> verticality = scoreTree.getVerticalityAt(1.0)
        >>> timespan = verticality.startTimespans[0]
        >>> timespan.endTime
        2.0
        )r   r-   s    r   r   zTimespan.endTimet   s     }}r   Nc                `    || j                   }|| j                  } t        |       ||      S )zG
        return a new object with the given offset and endTime
        r   r   )r   r   r"   r   s      r   newzTimespan.new   s3     >[[F?llGtDz99r   c                    t        |t        |             sd|  d| d}d|fS | j                  |j                  k(  s&|j                  | j                  k(  sd|  d| d}d|fS y)a  
        returns a tuple of (True or False) if these timespans can be merged
        with the second element being a message or None.

        >>> ts1 = tree.spans.Timespan(0, 5)
        >>> ts2 = tree.spans.Timespan(5, 7)
        >>> ts1.canMerge(ts2)
        (True, '')
        >>> ts3 = tree.spans.Timespan(6, 10)
        >>> ts1.canMerge(ts3)
        (False, 'Cannot merge <Timespan 0.0 5.0> with <Timespan 6.0 10.0>: not contiguous')

        Overlapping Timespans cannot be merged, just contiguous ones.

        >>> ts4 = tree.spans.Timespan(3, 4)
        >>> ts1.canMerge(ts4)
        (False, 'Cannot merge <Timespan 0.0 5.0> with <Timespan 3.0 4.0>: not contiguous')
        Cannot merge  with z: wrong typesFz: not contiguous)T )
isinstancer"   r   r   )r   othermessages      r   canMergezTimespan.canMerge   st    & %d,%dV6%FG7##-MMT[[0%dV6%8HIG7##r   c                    | j                  |      \  }}|du rt        |      | j                  |j                  k  r| j                  |j                        }|S |j                  | j                        }|S )ad  
        Merges two consecutive/contiguous timespans, keeping the
        information from the former of the two.

        >>> ts1 = tree.spans.Timespan(0, 5)
        >>> ts2 = tree.spans.Timespan(5, 7)
        >>> ts3 = ts1.mergeWith(ts2)
        >>> ts3
        <Timespan 0.0 7.0>

        Note that (for now), overlapping timespans cannot be merged:

        >>> ts4 = tree.spans.Timespan(6, 10)
        >>> ts3.mergeWith(ts4)
        Traceback (most recent call last):
        music21.tree.spans.TimespanException: Cannot merge <Timespan 0.0 7.0> with
            <Timespan 6.0 10.0>: not contiguous
        Fr   )r9   r   r   r1   r   )r   r7   canr8   mergedTimespans        r   	mergeWithzTimespan.mergeWith   sp    & }}U+W%<#G,,;;%!XXemmX<N  #YYt||Y<Nr   c                    || j                   k  s| j                  |k  r| fS | j                  |      }| j                  |      }||fS )a  
        Split Timespan at `offset`.

        >>> score = corpus.parse('bwv66.6')
        >>> scoreTree = score.asTimespans(classList=(note.Note,))
        >>> verticality = scoreTree.getVerticalityAt(0)
        >>> verticality
        <music21.tree.verticality.Verticality 0 {A3 E4 C#5}>

        >>> timespan = verticality.startTimespans[0]
        >>> timespan
        <PitchedTimespan (0.0 to 0.5) <music21.note.Note C#>>

        >>> for shard in timespan.splitAt(0.25):
        ...     shard
        ...
        <PitchedTimespan (0.0 to 0.25) <music21.note.Note C#>>
        <PitchedTimespan (0.25 to 0.5) <music21.note.Note C#>>

        >>> timespan.splitAt(1000)
        (<PitchedTimespan (0.0 to 0.5) <music21.note.Note C#>>,)
        r;   )r   )r   r   r1   )r   r   leftrights       r   splitAtzTimespan.splitAt   sL    0 DKK4<<&#87Nxxx''U{r   )NN)r   r   r   __doc__r   r    r$   r+   propertyr   r   r1   r9   r>   rB   r   r   r   r   r   )   s]    "H  #dC 	`<    	:8:r   r   c                       e Zd ZU dZddiZded<   	 	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 d fdZd Zd Ze	d	        Z
	 	 	 	 	 dd
Ze	d        Zd Ze	d        ZdddZ xZS )ElementTimespana  
    A span of time anchored to an element in a score.  The span of time may
    be the same length as the element in the score.  It may be shorter (a
    "slice" of an element) or it may be longer (in the case of a timespan
    that is anchored to a single element but extends over rests or other
    notes following a note)

    PitchedTimespans give information about an element (such as a Note).  It knows
    its absolute position with respect to the element passed into TimespanTree.
    It contains information about what measure it's in, what part it's in, etc.

    Example, getting a passing tone from a known location from a Bach chorale.

    First we create an Offset tree:

    >>> score = corpus.parse('bwv66.6')
    >>> scoreTree = score.asTimespans()
    >>> scoreTree
    <TimespanTree {199} (0.0 to 36.0) <music21.stream.Score ...>>

    Then get the verticality from offset 6.5, which is beat two-and-a-half of
    measure 2 (the piece is in 4/4 with a quarter-note pickup)

    >>> verticality = scoreTree.getVerticalityAt(6.5)
    >>> verticality
    <music21.tree.verticality.Verticality 6.5 {E3 D4 G#4 B4}>

    There are four PitchedTimespans in the verticality -- each representing
    a note.  The notes are arranged from lowest to highest.

    We can find all the PitchedTimespans that start exactly at 6.5. There's
    one.

    >>> verticality.startTimespans
    (<PitchedTimespan (6.5 to 7.0) <music21.note.Note D>>,)

    >>> pitchedTimespan = verticality.startTimespans[0]
    >>> pitchedTimespan
    <PitchedTimespan (6.5 to 7.0) <music21.note.Note D>>

    What can we do with a PitchedTimespan? We can get its Part object and from there the
    Part object name

    >>> pitchedTimespan.part
    <music21.stream.Part Tenor>
    >>> pitchedTimespan.part.partName
    'Tenor'

    Find out what measure it's in:

    >>> pitchedTimespan.measureNumber
    2
    >>> pitchedTimespan.parentOffset
    5.0

    The position in the measure is given by subtracting that from the
    .offset:

    >>> pitchedTimespan.offset - pitchedTimespan.parentOffset
    1.5

    >>> pitchedTimespan.element
    <music21.note.Note D>

    These are not dynamic, so changing the Score object does not change the
    measureNumber, etc.
    	parentageau  
            The Stream hierarchy above the element in a ElementTimespan.

            >>> score = corpus.parse('bwv66.6')
            >>> scoreTree = score.asTimespans()
            >>> verticality = scoreTree.getVerticalityAt(1.0)
            >>> pitchedTimespan = verticality.startTimespans[0]
            >>> pitchedTimespan
            <PitchedTimespan (1.0 to 2.0) <music21.note.Note A>>
            >>> for streamSite in pitchedTimespan.parentage:
            ...     streamSite
            <music21.stream.Measure 1 offset=1.0>
            <music21.stream.Part Soprano>
            <music21.stream.Score ...>
            zdict[str, str]	_DOC_ATTRc                    t         |   ||       || _        || _        |t	        |      }|| _        |t	        |      }|| _        ||||kD  rt        d|d|      y y y )Nr0   r   z must be after parentEndTime )superr    elementrG   r   parentOffsetparentEndTimer   r   rK   rL   rM   rG   r   r   	__class__s          r   r    zElementTimespan.__init__C  s     	807"# .L($!-0M*#(Am+'l--J=J[\^ ^ , )B#r   c                
    | |u S )Nr   )r   r7   s     r   r$   zElementTimespan.__eq__\  s    u}r   c           	         t        |       j                  }d| d| j                   d| j                   d| j                  d	S )Nr&   z (z to z) r(   )r"   r   r   r   rK   r)   s     r   r+   zElementTimespan.__repr___  s?    :&&8*Bt{{m4~R?OqQQr   c                4    | j                   | j                  z
  S )a  
        The quarterLength of the Timespan, which, due to manipulation, may be different
        from that of the element.

        >>> n = note.Note('D-')
        >>> n.offset = 1.0
        >>> n.duration.quarterLength = 2.0

        >>> pts = tree.spans.PitchedTimespan(n, offset=n.offset, endTime=3.0)
        >>> pts
        <PitchedTimespan (1.0 to 3.0) <music21.note.Note D->>
        >>> pts.quarterLength
        2.0
        >>> n.duration.quarterLength
        2.0

        >>> pts2 = pts.new(offset=0.0)
        >>> pts2
        <PitchedTimespan (0.0 to 3.0) <music21.note.Note D->>
        >>> pts2.quarterLength
        3.0
        >>> pts2.element.duration.quarterLength
        2.0
        )r   r   r-   s    r   quarterLengthzElementTimespan.quarterLengthe  s    4 ||dkk))r   c                    |xs | j                   }|| j                  }|| j                  }|| j                  }|| j                  } t        |       |||| j                  ||      S )a  
        Create a new object that is identical to the calling object
        but with some parameters overridden.

        >>> n = note.Note('C#')
        >>> pts = tree.spans.PitchedTimespan(n, offset=11.0, endTime=12.0)
        >>> pts
        <PitchedTimespan (11.0 to 12.0) <music21.note.Note C#>>
        >>> pts2 = pts.new(endTime=13.0)
        >>> pts2
        <PitchedTimespan (11.0 to 13.0) <music21.note.Note C#>>
        >>> pts.element is pts2.element
        True
        rK   rL   rM   rG   r   r   )rK   rL   rM   r   r   r"   rG   )r   rK   rL   rM   r   r   s         r   r1   zElementTimespan.new  sy    * )T\\,,L  ..M>[[F?llGtDz%'nn
 	
r   c                .    | j                   j                  S )aN  
        The measure number of the measure containing the element.

        >>> score = corpus.parse('bwv66.6')
        >>> scoreTree = score.asTimespans()
        >>> verticality = scoreTree.getVerticalityAt(1.0)
        >>> pitchedTimespan = verticality.startTimespans[0]
        >>> pitchedTimespan.measureNumber
        1
        )rK   measureNumberr-   s    r   rW   zElementTimespan.measureNumber  s     ||)))r   c                F    | j                   D ]  }t        ||      s|c S  y)a>  
        returns that is the first parentage that has this classList.
        default stream.Part

        >>> score = corpus.parse('bwv66.6')
        >>> score.id = 'bach'
        >>> scoreTree = score.asTimespans()
        >>> verticality = scoreTree.getVerticalityAt(1.0)
        >>> pitchedTimespan = verticality.startTimespans[2]
        >>> pitchedTimespan
        <PitchedTimespan (1.0 to 2.0) <music21.note.Note C#>>
        >>> pitchedTimespan.getParentageByClass(classList=(stream.Part,))
        <music21.stream.Part Tenor>
        >>> pitchedTimespan.getParentageByClass(classList=(stream.Measure,))
        <music21.stream.Measure 1 offset=1.0>
        >>> pitchedTimespan.getParentageByClass(classList=(stream.Score,))
        <music21.stream.Score bach>

        The closest parent is returned in case of a multiple list:

        >>> searchTuple = (stream.Voice, stream.Measure, stream.Part)
        >>> pitchedTimespan.getParentageByClass(classList=searchTuple)
        <music21.stream.Measure 1 offset=1.0>

        TODO: this should take a normal class list.
        N)rG   r6   )r   	classListparents      r   getParentageByClassz#ElementTimespan.getParentageByClass  s&    6 nnF&), % r   c                H    ddl m} | j                  |j                  f      S )a  
        find the object in the parentage that is a Part object:

        >>> score = corpus.parse('bwv66.6')
        >>> scoreTree = score.asTimespans()
        >>> verticality = scoreTree.getVerticalityAt(1.0)
        >>> pitchedTimespan = verticality.startTimespans[2]
        >>> pitchedTimespan
        <PitchedTimespan (1.0 to 2.0) <music21.note.Note C#>>
        >>> pitchedTimespan.part
        <music21.stream.Part Tenor>
        r   r	   )rY   )music21r
   r[   Part)r   r
   s     r   partzElementTimespan.part  s!     	#''6;;.'AAr   c                    | j                   }|y|r9|}t        j                  |      }||j                  _        d|j                  _        | j                  |j                  _        |S )z
        Return a copy of the element (or the same one if makeCopy is False)
        with the quarterLength set to the length of the timespan
        Nzspans.makeElement)rK   copydeepcopy
derivationoriginmethodrS   duration)r   makeCopyelel_olds       r   makeElementzElementTimespan.makeElement  sZ    
 \\:Fv&B#)BMM #6BMM $($6$6!	r   )NNNr   NN)rK   base.Music21Object | NonerL   OffsetQLIn | NonerM   rl   rG   ztuple[stream.Stream, ...]r   rl   r   rl   )NNNNN)T)rg   boolreturnrk   )r   r   r   rC   rH   __annotations__r    r$   r+   rD   rS   r1   rW   r[   r_   rj   __classcell__rO   s   @r   rF   rF      s    BL 	 !I~ , ,0(,)-/1"&#'^(^ &^ '	^
 -^  ^ !^2R * *< &
T * *&@ B B  r   rF   c                  F     e Zd Z	 	 	 	 	 	 d fd	Zed        Z fdZ xZS )PitchedTimespanc                0    t         |   ||||||       y )NrU   )rJ   r    rN   s          r   r    zPitchedTimespan.__init__  s(     	&2'4#, &!( 	 	*r   c                .    | j                   j                  S )a  
        Gets the pitches of the element wrapped by this PitchedTimespan.

        >>> c = chord.Chord('C4 E4 G4')
        >>> pts = tree.spans.PitchedTimespan(c, offset=0.0, endTime=1.0)
        >>> pts.pitches
        (<music21.pitch.Pitch C4>, <music21.pitch.Pitch E4>, <music21.pitch.Pitch G4>)
        >>> pts.pitches == c.pitches
        True
        >>> pts.pitches is c.pitches
        False

        )rK   pitchesr-   s    r   rv   zPitchedTimespan.pitches  s     ||###r   c                    t         |   |      \  }}|du r$| j                  |j                  k7  rd|  d| d}d}||fS )a9	  
        sub-method of base canMerge that checks to see if the pitches are the same.

        For quick score reductions, we can merge two consecutive
        like-pitched element timespans, keeping
        score-relevant information from the first of the two, such as its
        Music21 Element.

        This is useful when using timespans to perform score reduction.

        Let's demonstrate merging some contiguous E's in the alto part of a Bach
        chorale:

        >>> score = corpus.parse('bwv66.6')
        >>> scoreTree = score.asTimespans(classList=(note.Note,))
        >>> timespan_one = scoreTree[12]
        >>> print(timespan_one)
        <PitchedTimespan (2.0 to 3.0) <music21.note.Note E>>

        >>> print(timespan_one.part)
        <music21.stream.Part Alto>

        >>> timespan_two = scoreTree.findNextPitchedTimespanInSameStreamByClass(timespan_one)
        >>> print(timespan_two)
        <PitchedTimespan (3.0 to 4.0) <music21.note.Note E>>

        >>> timespan_one.canMerge(timespan_two)
        (True, '')

        >>> merged = timespan_one.mergeWith(timespan_two)
        >>> print(merged)
        <PitchedTimespan (2.0 to 4.0) <music21.note.Note E>>

        >>> merged.part is timespan_one.part
        True

        Attempting to merge timespans which are not contiguous, or which do not
        have identical pitches will result in error:

        >>> scoreTree[0].canMerge(scoreTree[50])
        (False, 'Cannot merge <PitchedTimespan (0.0 to 0.5) <music21.note.Note C#>>
             with <PitchedTimespan (9.5 to 10.0) <music21.note.Note B>>: not contiguous')

        >>> scoreTree[0].mergeWith(scoreTree[50])
        Traceback (most recent call last):
        music21.tree.spans.TimespanException: Cannot merge
                <PitchedTimespan (0.0 to 0.5) <music21.note.Note C#>>
                with <PitchedTimespan (9.5 to 10.0) <music21.note.Note B>>: not contiguous

        This is probably not what you want to do: get the next element timespan in
        the same score:

        >>> timespan_twoWrong = scoreTree.findNextPitchedTimespanInSameStreamByClass(
        ...     timespan_one, classList=(stream.Score,))
        >>> print(timespan_twoWrong)
        <PitchedTimespan (3.0 to 4.0) <music21.note.Note C#>>
        >>> print(timespan_twoWrong.part)
        <music21.stream.Part Soprano>

        Tr3   r4   z: different pitchesF)rJ   r9   rv   )r   r7   r<   r8   rO   s       r   r9   zPitchedTimespan.canMerge(  sT    z w'.W$;||u}},)$veW<OPW~r   )NNNNNN)r   r   r   r    rD   rv   r9   rp   rq   s   @r   rs   rs     s:    "#* $ $ B Br   rs   c                      e Zd Zy)TestNr   r   r   r   ry   ry   o  r   r   ry   __main__)rC   
__future__r   ra   mathr   typingtunittestmusic21.common.typesr   r]   r   r   TYPE_CHECKINGr   r
   EnvironmentenvironLocalTreeExceptionr   r   rF   rs   TestCasery   r   mainTestr   r   r   <module>r      s    #     +   ??&{&&|4	22 	{ {@Yh Y|co cP	8 	 zGT r   