
    rhDj                       % S r SSKJr  SSKJr  SSKrSSKrSSKJ	r	  SSKJ
r
  SSKJr  SSKJr  SS	KJ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 jr " S S\R2                  5      r/ rS\S'   \S:X  a  SSKr\R<                  " \5        gg)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                      \ rS rSrSrg)VolumeException%    N)__name__
__module____qualname____firstlineno____static_attributes__r       H/home/james-whalen/.local/lib/python3.13/site-packages/music21/volume.pyr   r   %   s    r   r   c                  B   \ rS rSrSrSrSSSSS.       SS jjrSS jrS	 rS
 r	S r
     S   SS jjr     S   SS jjr\S 5       r\S 5       r\S 5       r\SS j5       r\R$                  SS j5       r\SS j5       r\R$                  SS j5       rSrg)Volume*   aR  
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                   Xl         S U l        Ub  [        U5      U l        OUb  [	        U5      U l        S U l        X@l        g N)r   r   intr    floatr!   r   r   )selfr   r    r!   r   s        r   __init__Volume.__init__L   sC     +/MDM'"'"7D#"4r   c                T    [         R                  " XS1S9nU R                  Ul        U$ )z'
Don't copy the client; set to current
r   )ignoreAttributes)r   defaultDeepcopyr   )r&   memonews      r   __deepcopy__Volume.__deepcopy__`   s(     $$T8*M[[

r   c                4    S[        U R                  S5       3$ )Nz	realized=   )roundrealizedr&   s    r   _reprInternalVolume._reprInternalh   s    52344r   c                8    U R                   R                  S5      $ )a
  
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   getContextByClassr4   s    r   getDynamicContextVolume.getDynamicContextl   s     {{,,Y77r   c                P    Ub#  UR                   U l         UR                  U l        gg)a  
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   mergeAttributesVolume.mergeAttributesz   s+      #(#8#8D &+&>&>D# r   c                R    U R                  UUUUUS9n[        [        US5      5      $ )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clipr1   )getRealizedstrr2   )r&   rA   rB   rC   rD   rE   vals          r   getRealizedStrVolume.getRealizedStr   s;      1B+60@)2$(	  *
 5a=!!r   c                   UnSnU(       aB  U R                   b0  U R                  (       d  U R                   nOX`R                   S-  -  nOUS-  nU R                  (       a  USLak  [        U[        R                  5      (       a  UnO5U R
                  b  U R                  5       nO[        R                  SS/5        Ub  XgR                  S-  -  nUSLa~  [        U[        R                  5      (       a  U/nOD[        R                  " U5      (       a  UnO&U R
                  b  U R
                  R                  nO/ nU H  n	XiR                  -  nM     U(       a  US:  a  SnOUS	:  a  S
nX`l        U$ )aH  
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	   r8   r   r:   environLocal
printDebugvolumeScalarr   Articulationr   
isIterablevolumeShiftr   )
r&   rA   rB   rC   rD   rE   rH   dmamas
             r   rF   Volume.getRealized   sd   P  ##/.. ..C!5!5!;<C w"" -/1A1ABB*B[[,//1B ++(_-  > 3!67C  u,.0J0JKK*+B&&'788)B[[,22BBA==(C  Qwq  #
r   c                ^    U R                   c  U R                  5       U l         U R                   $ )aV  
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   rF   r4   s    r   cachedRealizedVolume.cachedRealized)  s-     '#'#3#3#5D ###r   c                @    [        [        U R                  S5      5      $ )zf
Convenience property for testing.

>>> v = volume.Volume(velocity=128)
>>> v.cachedRealizedStr
'1.0'
r1   )rG   r2   r[   r4   s    r   cachedRealizedStrVolume.cachedRealizedStr:  s     5,,a011r   c                "    U R                  5       $ r#   )rF   r4   s    r   r3   Volume.realizedE  s    !!r   c                d    U R                   nUc  gUS-  nUS:  a  SnOUS:  a  Sn[        U5      $ )z
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   r2   )r&   vsvs      r   r    Volume.velocityI  sA     !!:Hs7AUAQxr   c                    Uc  S U l         g [        R                  " U5      (       d  [        SU 35      eUS::  a  SU l         g US:  a  SU l         g US-  U l         g )Nz2value provided for velocity must be a number, not r   rN   rc   rM   g     _@)r   r   isNumr   )r&   values     r   r    rf   a  s]    =#'D e$$!$VW\V]"^__aZ#&D c\#&D #(5=D r   c                &    U R                   nUc  gU$ )a  
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&   re   s     r   r!   Volume.velocityScalarn  s    :   9Hr   c                    Uc  S U l         [        R                  " U5      (       d  [        SSU 3-   5      eUS:  a  SnO.US:  a  SnO%[        R
                  (       a  Uc   e[        U5      nX l         g )Nz4value provided for velocityScalar must be a number, znot r   rN   rL   rM   )r   r   rh   r   tTYPE_CHECKINGr%   )r&   ri   scalars      r   r!   rk     sx    =#'D ||E""!"X&*5'N#3 4 4 19FQYF(((5\F%r   )r   r   r   r    r   r!   )r   znote.NotRest | Noner    
int | Noner!   float | Noner   boolr#   )TTTg      ?T)rA   zdynamics.Dynamic | boolrC   Ot.Union[bool, articulations.Articulation, Iterable[articulations.Articulation]])rA   zbool | dynamics.DynamicrC   rs   )returnrp   )ri   zint | float | None)rt   rq   )r   r   r   r   __doc__	__slots__r'   r.   r5   r:   r>   rI   rF   propertyr[   r^   r3   r    setterr!   r   r   r   r   r   r   *   sN   4I %)!%)#'5 "5 	5
 #5 !5(58?* CG#' 6:!$ "*?"*3"2 48 A0A
	AH $ $  2 2 " "  . __
1 
1    D & &r   r   c                   / n0 nU R                  5       nSnUR                  [        R                  5      (       a  SnOUSL a  SnU(       a  UR	                  [        R                  SS9  UR                  [        R                  5      n	U	 H1  n
UR                  U
5      nXR                  R                  -   nXX4'   M3     [        UR                  5       5      nUR                  5         SnU H  n
[        U
S5      (       d  M  [        U
[        R                  5      (       d  M7  U
R                  U5      nU(       aF  USL aA  Sn[!        U[#        U5      5       H%  nUU   u  pXs=:  a  U:  d  M  O  M  UnXeU      n  O   OUnU
R$                  R'                  UUUS9nU(       d  M  SU
R$                  l        UU
R$                  l        M     g)aA  
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   )rA   rB   rC   N)flattengetElementsByClassr	   r8   extendDurationelementOffsetdurationquarterLengthlistkeyssorthasattrrO   r   NotRestgetOffsetBySiterangelenr   rF   r   r!   )	srcStreamsetAbsoluteVelocityrA   rB   rC   bKeys
boundariesflatSrcdynamicsAvailableelementsestartendlastRelevantKeyIndexeStartrV   krH   s                     r   realizeVolumer     s   ( EJ!G !!("2"233 $ % 	x//>--h.>.>?A))!,E**222C'(|$  Z__&'


 1hJq$,,$?$?&&w/F !%6$%>3SZ@A!&qJE,u,, 01,'a1 A ' ((&&3>8H ' JC #".3+*-'5 r   c                  J    \ rS rSrS rS rS rS rS rS r	S r
S	 rS
 rSrg)Testi  c                   SS K nSSKJn  [        R                  " S5      nUR                  US9nU R                  UR                  U5        AUR                  5         U R                  UR                  [        R                  " S5      5        g )Nr   r   zG#4r   )	gcmusic21r   r   Noter   assertEqualr   collect)r&   r   r   n1re   s        r   	testBasicTest.testBasic  sc    "YYuMMM$2&


499U#34r   c                   SSK Jn  SSK Jn  UR                  5       n[        R
                  " S5      nUR                  SU5        [        R
                  " S5      nUR                  SU5        [        R                  " S5      nUR                  US9nUR                  S	U5        U R                  UR                  R                  S
5      U5        U R                  UR                  5       U5        g )Nr   streamr   mffr1   gr      r8   )r   r   r   Streamr	   r8   insertr   r   r   r   r   r9   r:   )r&   r   r   sd1d2r   v1s           r   testGetContextSearchATest.testGetContextSearchA  s    ""MMOd#	Bc"	BYYs^]]"]%	B 	44Y?D--/4r   c                t   SSK Jn  UR                  5       n[        R                  " S5      nUR                  SU5        [        R                  " S5      nUR                  SU5        [        R                  " S5      nUR                  SU5        U R                  UR                  R                  5       U5        g )Nr   r   r   r   r1   r   r   )r   r   r   r	   r8   r   r   r   r   r   r:   )r&   r   r   r   r   r   s         r   testGetContextSearchBTest.testGetContextSearchB  s    "MMOd#	Bc"	BYYs^	B 	446;r   c                ~   SS K nSSKJn  [        R                  " 5       nUR                  5       nSUl        X4l        UR                  U5      nU R                  UR                  S5        U R                  UR                  S5        U R                  UR                  U5        U R                  UR                  U5        g )Nr   r   o   )
copyr   r   r   r   r   r    r   deepcopyr   )r&   r   r   r   r   v1Copys         r   testDeepCopyATest.testDeepCopyA+  s    "YY[]]_	r"c*#.B'+r   c                r   SSK Jn  UR                  SS9nU R                  UR	                  5       S5        [
        R                  " S5      nU R                  UR	                  US9S5        [
        R                  " S	5      nU R                  UR	                  US9S
5        [
        R                  " S5      nU R                  UR	                  US9S5        UR                  SS9n[
        R                  " S5      nU R                  UR	                  US9S5        [
        R                  " S	5      nU R                  UR	                  US9S5        [
        R                  " S5      nU R                  UR	                  US9S5        [
        R                  " S5      nU R                  UR	                  US9S5        g )Nr   r   @   r    0.5p)rA   0.35pppz0.15fffz0.91rc   1.0z0.3mpz0.9z0.7)r   r   r   r   rI   r	   r8   )r&   r   r   r   s       r   testGetRealizedA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                &   [        SS9nU R                  UR                  5       S5        [        R                  " 5       nU R                  UR                  US9S5        [        R
                  " 5       nU R                  UR                  US9S5        g )Nr   r   r   )rC   z0.65z0.6)r   r   rI   r   StrongAccentAccent)r&   r   a1a2s       r   testGetRealizedBTest.testGetRealizedBZ  s}    R **,e4'')**B*?H!!#**B*?Gr   c                   SSK Jn  SSK Jn  UR                  5       nUR	                  [
        R                  " S5      S5        UR                   Vs/ s H  oDR                  R                  PM     nnU R                  US/S-  5        [        / SQ5       H.  u  pgUR                  US-  [        R                  " U5      5        M0     UR                   Vs/ s H  oDR                  R                  PM     nnU R                  US/S-  5        UR                  U5        UR                   Vs/ s H  oDR                  R                  PM     nnU R                  U/ S	Q5        UR                  5       nUR	                  [
        R                  " S5      S5        [        / SQ5       H.  u  pgUR                  US-  [        R                  " U5      5        M0     UR                   Vs/ s H  oDR                  R                  PM     nnU R                  U/ S	Q5        UR                   Vs/ s H  oDR                  R                  PM     nnU R                  US /S-  5        UR                  US
S9  UR                   Vs/ s H  oDR                  R                  PM     nnU R                  U/ SQ5        g s  snf s  snf s  snf s  snf s  snf s  snf )Nr   r   r   g3   0.71)ppr   r   r   r   ffr   r   r1   )r   r   r   r   0.64r   0.99r   0.78r   r   r   0.21r   r   r   T)r   )-   r   ?   r   Q   r   ~   r   c   r   rc   rc      r   r   r   )r   r   r   r   repeatAppendr   r   notesr^   r   	enumerater   r	   r8   r   r    )r&   r   r   r   nmatchids           r   testRealizeVolumeATest.testRealizeVolumeAk  s9   ""MMO	tyy+ 67WW=W++W=2.MNDAHHQUH,,Q/0 O 67WW=W++W=2. 	Q56WW=W++W= !A 	B MMO	tyy+MNDAHHQUH,,Q/0 O56WW=W++W= !1 	2 -.GG4Gq""G4, 	QD9,-GG4Gq""G4 !; 	<U > >
 > > 5
 5s$   J1J6+J;#K &K;K
c                   SSK Jn  SSK Jn  UR                  S5      nUR                  S-  n/ SQn[        UR                  5       H  u  pg[        U5       Hy  u  pX-  n
UR                  U
SS9R                  UR                  5      R                  5       nXR                  U5      -
  nUR                  U[        R                  " U	5      5        M{     US	S  US S	 -   nM     UR                  S   R                  5       R                    Vs/ s H  oR"                  R$                  PM     nnU R'                  U/ S
Q5        UR                  S   R                  5       R                    Vs/ s H  oR"                  R$                  PM     nnU R'                  U/ SQ5        UR                  S   R                  5       R                    Vs/ s H  oR"                  R$                  PM     nnU R'                  U/ SQ5        g s  snf s  snf s  snf )Nr   )corpusr   zbwv66.6   )r   r   r   r   r   r   r   r   F)mustBeginInSpanr1   )%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   rL   )*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getElementsByOffsetr|   Measurefirstr   r   r	   r8   r{   r   r   r^   r   )r&   r   r   r   durUnitdynsr   r   jr   oTargetmoInsertr   r   s                  r   testRealizeVolumeBTest.testRealizeVolumeB  s   ""LL#--1$<agg&DA!$+))':? * ,,>,>v~~,Nuuw  "$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   G=)HHc                H   SSK Jn  UR                  5       nUR                  [        R
                  " S5      S5        [        SSS5       H>  nUR                  U   R                  R                  [        R                  " 5       5        M@     [        SSS5       H>  nUR                  U   R                  R                  [        R                  " 5       5        M@     UR                   Vs/ s H  oDR                  R                  PM     nnU R                  U/ SQ5        g s  snf )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   r   r   r   appendr   r   r   r^   r   )r&   r   r   r   r   r   s         r   testRealizeVolumeC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r   N)r   r   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)ru   
__future__r   collections.abcr   typingrm   unittestr   r   r   r   music21.common.objectsr   r	   r
   r   r   EnvironmentrP   Music21Exceptionr   ProtoM21Objectr   r   TestCaser   r  __annotations__r   mainTestr   r   r   <module>r     s    # $   !    5    &&x0	l33 	
y&W##%7 y&B ',$("#'	K.^q28 q2l 
J  zT r   