
    3jj                       U d Z ddlm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 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  ej$                  d      Z G d de
j(                        Z G d dej,                  e      Z	 	 	 	 ddZ G d dej2                        Zg Zded<   edk(  rddlZ ej<                  e       yy)z
This module defines the object model of Volume, covering all representation of
amplitude, volume, velocity, and related parameters.
    )annotations)IterableN)articulations)exceptions21)common)SlottedObjectMixin)dynamics)environment)prebase)notevolumec                      e Zd Zy)VolumeExceptionN)__name__
__module____qualname__     ;/DATA/.local/lib/python3.12/site-packages/music21/volume.pyr   r   $   s    r   r   c                  $   e Zd ZdZdZddddd	 	 	 	 	 	 	 ddZddZd Zd	 Zd
 Z		 	 	 	 	 d	 	 	 ddZ
	 	 	 	 	 d	 	 	 ddZed        Zed        Zed        Zedd       Zej"                  dd       Zedd       Zej"                  dd       Zy)Volumea  
    The Volume object lives on NotRest objects and subclasses. It is not a
    Music21Object subclass.

    Generally, just assume that a Note has a volume object and don't worry
    about creating this class directly:

    >>> n = note.Note('C5')
    >>> v = n.volume
    >>> v.velocity = 20
    >>> v.client is n
    True

    But if you want to create it yourself, you can specify the client, velocity,
    velocityScalar, and

    >>> v = volume.Volume(velocity=90)
    >>> v
    <music21.volume.Volume realized=0.71>
    >>> v.velocity
    90

    * Changed in v9: all constructor attributes are keyword only.
        (client as first attribute was confusing)
    )client_velocityScalar_cachedRealizedvelocityIsRelativeNT)r   velocityvelocityScalarr   c                   || _         d | _        |t        |      | _        n|t	        |      | _        d | _        || _        y N)r   r   intr   floatr   r   r   )selfr   r   r   r   s        r   __init__zVolume.__init__K   sG     +/MDM'"'"7D#"4r   c                Z    t        j                  | |dh      }| j                  |_        |S )z7
        Don't copy the client; set to current
        r   )ignoreAttributes)r   defaultDeepcopyr   )r"   memonews      r   __deepcopy__zVolume.__deepcopy___   s*     $$T48*M[[

r   c                4    dt        | j                  d       S )Nz	realized=   )roundrealizedr"   s    r   _reprInternalzVolume._reprInternalg   s    52344r   c                8    | j                   j                  d      S )aJ  
        Return the dynamic context of this Volume, based on the position of the
        client of this object.

        >>> n = note.Note()
        >>> n.volume.velocityScalar = 0.9
        >>> s = stream.Measure([dynamics.Dynamic('ff'), n])
        >>> n.volume.getDynamicContext()
        <music21.dynamics.Dynamic ff>
        Dynamic)r   getContextByClassr.   s    r   getDynamicContextzVolume.getDynamicContextk   s     {{,,Y77r   c                N    |#|j                   | _         |j                  | _        yy)ay  
        Given another Volume object, gather all attributes except client.
        Values are always copied, not passed by reference.

        >>> n1 = note.Note()
        >>> v1 = n1.volume
        >>> v1.velocity = 111

        >>> v2 = volume.Volume()
        >>> v2.mergeAttributes(v1)
        >>> v2.client is None
        True
        >>> v2.velocity
        111
        N)r   r   )r"   others     r   mergeAttributeszVolume.mergeAttributesy   s+      #(#8#8D &+&>&>D# r   c                X    | j                  |||||      }t        t        |d            S )z
        Return the realized as rounded and formatted string value. Useful for testing.

        >>> v = volume.Volume(velocity=64)
        >>> v.getRealizedStr()
        '0.5'
        )useDynamicContextuseVelocityuseArticulations	baseLevelclipr+   )getRealizedstrr,   )r"   r8   r9   r:   r;   r<   vals          r   getRealizedStrzVolume.getRealizedStr   s;    " 1B+60@)2$(	  *
 5a=!!r   c                   |}d}|r=| j                   ,| j                  s| j                   }n|| j                   dz  z  }n|dz  }| j                  r|duret        |t        j                        r|}n4| j
                  | j                         }nt        j                  ddg       |||j                  dz  z  }|durqt        |t        j                        r|g}n=t        j                  |      r|}n%| j
                  | j
                  j                  }ng }|D ]  }	||	j                  z  } |r|dkD  rd}n|d	k  rd
}|| _        |S )a  
        Get a realized unit-interval scalar for this Volume. This scalar is to
        be applied to the dynamic range of whatever output is available,
        whatever that may be.

        The `baseLevel` value is a middle value between 0 and 1 that all
        scalars modify. This also becomes the default value for unspecified
        dynamics. When scalars (between 0 and 1) are used, their values are
        doubled, such that mid-values (around 0.5, which become 1) make no
        change.

        This can optionally take into account `dynamicContext`, `useVelocity`,
        and `useArticulation`.

        If `useDynamicContext` is True, a context search for a dynamic will be
        done, else dynamics are ignored. Alternatively, the useDynamicContext
        may supply a Dynamic object that will be used instead of a context
        search.

        If `useArticulations` is True and client is not None, any articulations
        found on that client will be used to adjust the volume. Alternatively,
        the `useArticulations` parameter may supply an iterable of articulations
        that will be used instead of that available on a client.

        The `velocityIsRelative` tag determines if the velocity value includes
        contextual values, such as dynamics and accents, or not.

        >>> s = stream.Stream()
        >>> s.repeatAppend(note.Note('d3', quarterLength=0.5), 8)
        >>> s.insert([0, dynamics.Dynamic('p'),
        ...           1, dynamics.Dynamic('mp'),
        ...           2, dynamics.Dynamic('mf'),
        ...           3, dynamics.Dynamic('f')])

        >>> s.notes[0].volume.getRealized()
        0.496...

        >>> s.notes[1].volume.getRealized()
        0.496...

        >>> s.notes[2].volume.getRealized()
        0.63779...

        >>> s.notes[7].volume.getRealized()
        0.99212...

        velocity, if set, will be scaled by dynamics:

        >>> s.notes[7].volume.velocity = 20
        >>> s.notes[7].volume.getRealized()
        0.22047...

        Unless we set the velocity to not be relative:

        >>> s.notes[7].volume.velocityIsRelative = False
        >>> s.notes[7].volume.getRealized()
        0.1574803...

        Ng       @g^?FzgetRealized():zEuseDynamicContext is True but no dynamic supplied or found in context         ?r           )r   r   
isinstancer	   r1   r   r3   environLocal
printDebugvolumeScalarr   Articulationr   
isIterablevolumeShiftr   )
r"   r8   r9   r:   r;   r<   r?   dmamas
             r   r=   zVolume.getRealized   sc   T  ##/.. ..C!5!5!;<C w"" -/1A1AB*B[[,//1B ++(_-  > 3!67C  u,.0J0JK*+B&&'78)B[[,22BBA1==(C  Qwq  #
r   c                \    | j                   | j                         | _         | j                   S )a  
        Return the cached realized value of this volume. This will be the last
        realized value or, if that value has not been set, a newly realized
        value. If the caller knows that the realized values have all been
        recently set, using this property will add significant performance
        boost.

        >>> v = volume.Volume(velocity=128)
        >>> v.cachedRealized
        1.0
        )r   r=   r.   s    r   cachedRealizedzVolume.cachedRealized+  s-     '#'#3#3#5D ###r   c                @    t        t        | j                  d            S )z
        Convenience property for testing.

        >>> v = volume.Volume(velocity=128)
        >>> v.cachedRealizedStr
        '1.0'
        r+   )r>   r,   rP   r.   s    r   cachedRealizedStrzVolume.cachedRealizedStr<  s     5,,a011r   c                "    | j                         S r   )r=   r.   s    r   r-   zVolume.realizedG  s    !!r   c                r    | j                   }|y|dz  }|dkD  rd}t        |      S |dk  rd}t        |      S )a4  
        Get or set the velocity value, a numerical value between 0 and 127 and
        available setting amplitude on each Note or Pitch in chord.

        >>> n = note.Note()
        >>> n.volume.velocity = 20
        >>> n.volume.client == n
        True

        >>> n.volume.velocity
        20
        N   r   )r   r,   )r"   vsvs      r   r   zVolume.velocityK  sN     !!:Hs7A Qx UAQxr   c                    |d | _         y t        j                  |      st        d|       |dk  rd| _         y |dk\  rd| _         y |dz  | _         y )Nz2value provided for velocity must be a number, not r   rD   rU   rC   g     _@)r   r   isNumr   )r"   values     r   r   zVolume.velocityc  sZ    =#'D e$!$VW\V]"^__aZ#&D c\#&D #(5=D r   c                $    | j                   }|y|S )a9  
        Get or set the velocityScalar value, a numerical value between 0
        and 1 and available setting amplitude on each Note or Pitch in
        chord. This value is mapped to the range 0 to 127 on output.

        Note that this value is derived from the set velocity value.
        Floating point error seen here will not be found in the velocity
        value.

        When setting this value, an integer-based velocity value will be
        derived and stored.

        >>> n = note.Note()
        >>> n.volume.velocityScalar = 0.5
        >>> n.volume.velocity
        64

        >>> n.volume.velocity = 127
        >>> n.volume.velocityScalar
        1.0

        If velocity is not set, then this will return None

        >>> n = note.Note()
        >>> n.volume.velocityScalar is None
        True
        N)r   )r"   rW   s     r   r   zVolume.velocityScalarp  s    :   9Hr   c                    |d | _         t        j                  |      st        dd| z         |dk  r
d}|| _         y |dkD  r
d}|| _         y t        j
                  r|J t        |      }|| _         y )Nz4value provided for velocityScalar must be a number, znot r   rD   rB   rC   )r   r   rY   r   tTYPE_CHECKINGr!   )r"   rZ   scalars      r   r   zVolume.velocityScalar  s    =#'D ||E"!"X&*5'N#3 4 4 19F  & QYF
  & (((5\F%r   )r   znote.NotRest | Noner   
int | Noner   float | Noner   boolr   )TTTg      ?T)r8   zdynamics.Dynamic | boolr:   Hbool | articulations.Articulation | Iterable[articulations.Articulation])r8   zbool | dynamics.Dynamicr:   rc   )returnr`   )rZ   zint | float | None)rd   ra   )r   r   r   __doc__	__slots__r#   r)   r/   r3   r6   r@   r=   propertyrP   rR   r-   r   setterr   r   r   r   r   r   )   sO   4I %)!%)#'5 "5 	5
 #5 !5(58?* CG#'
  !$ "*?"B	"4 48
 C0C
3CL $ $  2 2 " "  . __
1 
1    D & &r   r   c                ^   g }i }| j                         }d}|j                  t        j                        rd}n|du rd}|r|j	                  t        j                  d       |j                  t        j                        }	|	D ]3  }
|j                  |
      }||
j                  j                  z   }|
|||f<   5 t        |j                               }|j                          d}|D ]  }
t        |
d      st        |
t        j                        s+|
j                  |      }|rC|du r?d}t!        |t#        |            D ]$  }||   \  }}||cxkD  r|k\  sn |}|||      } n n|}|
j$                  j'                  |||      }|sd|
j$                  _        ||
j$                  _         y)am  
    Given a Stream with one level of dynamics
    (e.g., a Part, or two Staffs that share Dynamics),
    destructively modify it to set all realized volume levels.
    These values will be stored in the Volume object as `cachedRealized` values.

    This is a top-down routine, as opposed to bottom-up values available with
    context searches on Volume. This thus offers a performance benefit.

    This is always done in place.

    If setAbsoluteVelocity is True, the realized values will overwrite all
    existing velocity values, and the Volume objects velocityIsRelative
    parameters will be set to False.
    FT)inPlacer   r   )r8   r9   r:   N)flattengetElementsByClassr	   r1   extendDurationelementOffsetdurationquarterLengthlistkeyssorthasattrrE   r   NotRestgetOffsetBySiterangelenr   r=   r   r   )	srcStreamsetAbsoluteVelocityr8   r9   r:   bKeys
boundariesflatSrcdynamicsAvailableelementsestartendlastRelevantKeyIndexeStartrL   kr?   s                     r   realizeVolumer     s   ( EJ!G !!("2"23 $ % 	x//>--h.>.>?A))!,E!**222C'(Js|$  Z__&'


 1hJq$,,$?&&w/F !%6$%>3SZ@A!&qJE3V,u, 01,'a1 A ' ((&&3>8H ' JC #.3+*-'5 r   c                  B    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zy
)Testc                   dd l }ddlm} t        j                  d      }|j                  |      }| j                  |j                  |       ~|j                          | j                  |j                  t        j                  d             y )Nr   r   zG#4r   )	gcmusic21r   r   Noter   assertEqualr   collect)r"   r   r   n1rW   s        r   	testBasiczTest.testBasic  sc    "YYuMMM$2&


499U#34r   c                   ddl m} ddl m} |j                         }t	        j
                  d      }|j                  d|       t	        j
                  d      }|j                  d|       t        j                  d      }|j                  |      }|j                  d	|       | j                  |j                  j                  d
      |       | j                  |j                         |       y )Nr   streamr   mffr+   gr      r1   )r   r   r   Streamr	   r1   insertr   r   r   r   r   r2   r3   )r"   r   r   sd1d2r   v1s           r   testGetContextSearchAzTest.testGetContextSearchA
  s    ""MMOd#	Bc"	BYYs^]]"]%	B 	44Y?D--/4r   c                n   ddl m} |j                         }t        j                  d      }|j                  d|       t        j                  d      }|j                  d|       t        j                  d      }|j                  d|       | j                  |j                  j                         |       y )Nr   r   r   r   r+   r   r   )r   r   r   r	   r1   r   r   r   r   r   r3   )r"   r   r   r   r   r   s         r   testGetContextSearchBzTest.testGetContextSearchB  s    "MMOd#	Bc"	BYYs^	B 	446;r   c                ~   dd l }ddlm} t        j                         }|j                         }d|_        ||_        |j                  |      }| j                  |j                  d       | j                  |j                  d       | j                  |j                  |       | j                  |j                  |       y )Nr   r   o   )
copyr   r   r   r   r   r   r   deepcopyr   )r"   r   r   r   r   v1Copys         r   testDeepCopyAzTest.testDeepCopyA-  s    "YY[]]_	r"c*#.B'+r   c                   ddl m} |j                  d      }| j                  |j	                         d       t        j                  d      }| j                  |j	                  |      d       t        j                  d	      }| j                  |j	                  |      d
       t        j                  d      }| j                  |j	                  |      d       |j                  d      }t        j                  d      }| j                  |j	                  |      d       t        j                  d	      }| j                  |j	                  |      d       t        j                  d      }| j                  |j	                  |      d       t        j                  d      }| j                  |j	                  |      d       y )Nr   r   @   r   0.5p)r8   0.35pppz0.15fffz0.91rU   1.0z0.3mpz0.9z0.7)r   r   r   r   r@   r	   r1   )r"   r   r   r   s       r   testGetRealizedAzTest.testGetRealizedA>  s   "]]B]'**,e4c"**R*@&Ie$**R*@&I e$**R*@&I ]]C](e$**R*@%He$**R*@%Hd#**R*@%Hc"**R*@%Hr   c                4   t        d      }| j                  |j                         d       t        j                         }| j                  |j                  |      d       t        j
                         }| j                  |j                  |      d       y )Nr   r   r   )r:   z0.65z0.6)r   r   r@   r   StrongAccentAccent)r"   r   a1a2s       r   testGetRealizedBzTest.testGetRealizedB\  s}    R **,e4'')**B*?H!!#**B*?Gr   c                   ddl m} ddl m} |j                         }|j	                  t        j                  d      d       |j                  D cg c]  }|j                  j                   }}| j                  |dgdz         t        g d      D ]-  \  }}|j                  |dz  t        j                  |             / |j                  D cg c]  }|j                  j                   }}| j                  |dgdz         |j                  |       |j                  D cg c]  }|j                  j                   }}| j                  |g d	       |j                         }|j	                  t        j                  d      d       t        g d      D ]-  \  }}|j                  |dz  t        j                  |             / |j                  D cg c]  }|j                  j                   }}| j                  |g d	       |j                  D cg c]  }|j                  j                   }}| j                  |d gdz         |j                  |d
       |j                  D cg c]  }|j                  j                   }}| j                  |g d       y c c}w c c}w c c}w c c}w c c}w c c}w )Nr   r   r   g3   0.71)ppr   r   r   r   ffr   r   r+   )r   r   r   r   0.64r   0.99r   0.78r   r   r   0.21r   r   r   T)rz   )-   r   ?   r   Q   r   ~   r   c   r   rU   rU      r   r   r   )r   r   r   r   repeatAppendr   r   notesrR   r   	enumerater   r	   r1   r   r   )r"   r   r   r   nmatchids           r   testRealizeVolumeAzTest.testRealizeVolumeAm  sI   ""MMO	tyy+ 67WW=W++W=2.MNDAqHHQUH,,Q/0 O 67WW=W++W=2. 	Q56WW=W++W= !A 	B MMO	tyy+MNDAqHHQUH,,Q/0 O56WW=W++W= !1 	2 -.GG4Gq""G4, 	QD9,-GG4Gq""G4 !; 	<U > >
 > > 5
 5s$   J"J'$J,J1J6.J;c                   ddl m} ddl m} |j                  d      }|j                  dz  }g d}t        |j                        D ]  \  }}t        |      D ]}  \  }}	||z  }
|j                  |
d      j                  |j                        j                         }|
|j                  |      z
  }|j                  |t        j                  |	              |d	d  |d d	 z   } |j                  d   j                         j                   D cg c]  }|j"                  j$                   }}| j'                  |g d
       |j                  d   j                         j                   D cg c]  }|j"                  j$                   }}| j'                  |g d       |j                  d   j                         j                   D cg c]  }|j"                  j$                   }}| j'                  |g d       y c c}w c c}w c c}w )Nr   )corpusr   zbwv66.6   )r   r   r   r   r   r   r   r   F)mustBeginInSpanr+   )%r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rB   )*r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r      ))r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   parsehighestTimer   partsgetElementsByOffsetrl   Measurefirstrv   r   r	   r1   rk   r   r   rR   r   )r"   r   r   r   durUnitdynsr   r   jr   oTargetmoInsertr   r   s                  r   testRealizeVolumeBzTest.testRealizeVolumeB  s   ""LL#--1$<agg&DAq!$1g+))':? * ,,>,>v~~,Nuuw  "A$5$5a$88("2"21"56 ( 8d2Ah&D '  67WWQZ5G5G5I5O5OP5O++5OP !Y 	Z 67WWQZ5G5G5I5O5OP5O++5OP !` 	a 67WWQZ5G5G5I5O5OP5O++5OP !K 	L/ Q Q Qs   G9)G>Hc                6   ddl m} |j                         }|j                  t	        j
                  d      d       t        ddd      D ]<  }|j                  |   j                  j                  t        j                                > t        ddd      D ]<  }|j                  |   j                  j                  t        j                                > |j                  D cg c]  }|j                  j                   }}| j                  |g d       y c c}w )Nr   r   r   r   r   r   )0.96r   r   0.810.86r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   rw   r   r   appendr   r   r   rR   r   )r"   r   r   r   r   r   s         r   testRealizeVolumeCzTest.testRealizeVolumeC  s    "MMO	tyy+q"aAGGAJ$$++M,@,@,BC !q"aAGGAJ$$++M,F,F,HI ! 67WW=W++W= !1 	2 >s   "DN)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r     s4    
55&< ,"I<H"3<n7Lt2r   r   z
list[type]
_DOC_ORDER__main__)FTTT)re   
__future__r   collections.abcr   typingr]   unittestr   r   r   r   music21.common.objectsr   r	   r
   r   r   EnvironmentrF   Music21Exceptionr   ProtoM21Objectr   r   TestCaser   r   __annotations__r   mainTestr   r   r   <module>r      s    # $   !    5    &{&&x0	l33 	
|&W##%7 |&H ',$("#'	K.^q28 q2l 
J  zGT r   