
    ^j:2                         d 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  ed
          Zd Zd Zd Zd Z G d d          Z edddg          Z G d d          Z G d d          ZdS )z$
Pythonic interface to DirectSound.
    N)
namedtuple)MediaException)debug_print)_user32   )
lib_dsound)DirectSoundNativeErrordebug_mediac                 D    | t           j        k    rt          |           d S N)libDS_OKr	   )hresults    e/home/agentuser/manim-venv/lib/python3.11/site-packages/pyglet/media/drivers/directsound/interface.py_checkr      s%    #)$W---     c                 8   | j         dk    s	| j        dvrt          d|            t          j                    }t          j        |_        | j         |_        | j        |_	        | j        |_
        |j
        |j        z  dz  |_        |j	        |j        z  |_        |S )N   )      zUnsupported audio format: r   )channelssample_sizer   r   WAVEFORMATEXWAVE_FORMAT_PCM
wFormatTag	nChannelssample_ratenSamplesPerSecwBitsPerSamplenBlockAlignnAvgBytesPerSec)audio_formatwfxs     r   _create_wave_formatr$      s    q  L$<G$K$KH,HHIII



C(CN )CM%1C%1C(3=8A=CO,s>CJr   c                 X   t          j                    }t          j        |          |_        t           j        t           j        z  t           j        z  t           j        z  |_	        | j
        dk    r|xj	        t           j        z  c_	        ||_        t          j        |           |_        |S Nr   )r   DSBUFFERDESCctypessizeofdwSizeDSBCAPS_GLOBALFOCUSDSBCAPS_GETCURRENTPOSITION2DSBCAPS_CTRLFREQUENCYDSBCAPS_CTRLVOLUMEdwFlagsr   DSBCAPS_CTRL3DdwBufferBytespointerlpwfxFormat)wave_formatbuffer_sizedsbdescs      r   _create_buffer_descr7   %   s      G]7++GN.7812 ./GO !!3--'G .55GNr   c                      t          j                    } t          j        |           | _        t           j        t           j        z  t           j        z  | _        | S )z.Primary buffer with 3D and volume capabilities)	r   r'   r(   r)   r*   r0   r.   DSBCAPS_PRIMARYBUFFERr/   )buffer_descs    r   _create_primary_buffer_descr;   4   sK    "$$K{33K-2356K r   c                   2    e Zd Zd Zd Zd Zd Zd Zd ZdS )DirectSoundDriverc                    t          d          sJ t          j                    | _        t	          t          j        d t          j        | j                  d                      t          j	                    }t	          | j        
                    |t          j                             |                                 | _        d S )NzConstructing DirectSoundDriver)_debugr   IDirectSound_native_dsoundr   DirectSoundCreater(   byrefr   GetDesktopWindowSetCooperativeLevelDSSCL_NORMAL_create_primary_bufferprimary_buffer)selfhwnds     r   __init__zDirectSoundDriver.__init__@   s    677777!.00!$T5H(I(I4PP	
 	
 	
 '))33D#:JKK	
 	
 	
 #99;;r   c                 x    | j                                          d | _         | j                                         d S r   )rH   deleterA   ReleaserI   s    r   rM   zDirectSoundDriver.deleteR   s:    ""$$$"##%%%%%r   c                     t          |          }t          ||          }t          |                     |          ||          S r   )r$   r7   DirectSoundBuffer_create_native_buffer)rI   r"   r5   r4   r:   s        r   create_bufferzDirectSoundDriver.create_bufferW   sG    ),77)+{CC **;77  	r   c                 4    | j                                         S r   )rH   create_listenerrO   s    r   rU   z!DirectSoundDriver.create_listener_   s    "22444r   c                 b    t          |                     t                                d d          S Nr   )rQ   rR   r;   rO   s    r   rG   z(DirectSoundDriver._create_primary_bufferb   s2     **+F+H+HII  	r   c                     t          j                    }t          | j                            |t          j        |          d                      |S r   )r   IDirectSoundBufferr   rA   CreateSoundBufferr(   rC   )rI   r:   bufs      r   rR   z'DirectSoundDriver._create_native_bufferh   sL    $&&11+v|C?P?PRVWW	
 	
 	
 
r   N)	__name__
__module____qualname__rK   rM   rS   rU   rG   rR    r   r   r=   r=   ?   sn        < < <$& & &
  5 5 5      r   r=   _CurrentPositionplay_cursorwrite_cursorc                      e Zd Zd Zd Zed             Zej        d             Zed             Zej        d             Zed             Z	ed             Z
ed	             Zd
 Zed             Zej        d             Zed             Zej        d             Zed             Zej        d             Zed             Zej        d             Zed             Zej        d             Z edddg          Zed             Zd Zed             Zej        d             Zd Zd Zd Z G d d           Zd! Zd" Zd#S )$rQ   c                    || _         || _        || _        |a|j        dk    rVt	          j                    | _        | j                            t          j        t          j
        | j                             d S d | _        d S r&   )r"   r5   _native_bufferr   r   IDirectSound3DBuffer_native_buffer3dQueryInterfaceIID_IDirectSound3DBufferr(   rC   )rI   native_bufferr"   r5   s       r   rK   zDirectSoundBuffer.__init__t   s    (&+#(=(B(B$'$<$>$>D!..s/K/5|D<Q/R/RT T T T T %)D!!!r   c                     | j         b| j                                          | j                                          d | _         | j        $| j                                         d | _        d S d S d S r   )re   StoprN   rg   rO   s    r   rM   zDirectSoundBuffer.delete   sy    *$$&&&'')))"&D$0%--///(,%%% +* 10r   c                     t          j                    }t          | j                            t          j        |                               |j        S r   )r   LONGr   re   	GetVolumer(   rC   value)rI   vols     r   volumezDirectSoundBuffer.volume   sG    hjj))&,s*;*;<<	
 	
 	
 yr   c                 T    t          | j                            |                     d S r   )r   re   	SetVolumerI   rp   s     r   rr   zDirectSoundBuffer.volume   s2    ))%00	
 	
 	
 	
 	
r   c                     t          j                    }t          j                    }t          | j                            ||                     t          |j        |j                  S )zTuple of current play position and current write position.
        Only play position can be modified, so setter only accepts a single value.)r   DWORDr   re   GetCurrentPositionr`   rp   )rI   ra   rb   s      r   current_positionz"DirectSoundBuffer.current_position   sa     ikky{{22;3?A A	
 	
 	
   1<3EFFFr   c                 T    t          | j                            |                     d S r   )r   re   SetCurrentPositionru   s     r   ry   z"DirectSoundBuffer.current_position   s2    22599	
 	
 	
 	
 	
r   c                     | j         d uS r   )rg   rO   s    r   is3dzDirectSoundBuffer.is3d   s    $D00r   c                 L    |                                  t          j        z  dk    S rW   )_get_statusr   DSBSTATUS_PLAYINGrO   s    r   
is_playingzDirectSoundBuffer.is_playing   s!      ""S%::q@@r   c                 L    |                                  t          j        z  dk    S rW   )r   r   DSBSTATUS_BUFFERLOSTrO   s    r   is_buffer_lostz DirectSoundBuffer.is_buffer_lost   s!      ""S%==!CCr   c                     t          j                    }t          | j                            |                     |j        S r   )r   rw   r   re   	GetStatusrp   )rI   statuss     r   r   zDirectSoundBuffer._get_status   s=    ))&11	
 	
 	
 |r   c                     | j         r`t          j                    }t          | j                            t          j        |                               |j        |j	        |j
        fS dS )Nr   r   r   )r}   r   	D3DVECTORr   rg   GetPositionr(   rC   xyz)rI   positions     r   r   zDirectSoundBuffer.position   sa    9 	}H%11&,x2H2HII   :xz8:557r   c                     | j         r<|\  }}}t          | j                            |||t          j                             d S d S r   )r}   r   rg   SetPositionr   DS3D_IMMEDIATE)rI   r   r   r   r   s        r   r   zDirectSoundBuffer.position   sZ    9 	GAq!%11!Q3;MNN    	 	r   c                     | j         rSt          j                    }t          | j                            t          j        |                               |j        S dS )z|The minimum distance, which is the distance from the
        listener at which sounds in this buffer begin to be attenuated.r   )	r}   r   D3DVALUEr   rg   GetMinDistancer(   rC   rp   ru   s     r   min_distancezDirectSoundBuffer.min_distance   X     9 	LNNE%44V\%5H5HII   ;1r   c                 |    | j         r4t          | j                            |t          j                             d S d S r   )r}   r   rg   SetMinDistancer   r   ru   s     r   r   zDirectSoundBuffer.min_distance   K    9 	%44UC<NOO    	 	r   c                     | j         rSt          j                    }t          | j                            t          j        |                               |j        S dS )zThe maximum distance, which is the distance from the listener beyond which
        sounds in this buffer are no longer attenuated.r   )	r}   r   r   r   rg   GetMaxDistancer(   rC   rp   ru   s     r   max_distancezDirectSoundBuffer.max_distance   r   r   c                 |    | j         r4t          | j                            |t          j                             d S d S r   )r}   r   rg   SetMaxDistancer   r   ru   s     r   r   zDirectSoundBuffer.max_distance   r   r   c                     t          j                    }t          | j                            |                     |j        S r   )r   rw   r   re   GetFrequencyrp   ru   s     r   	frequencyzDirectSoundBuffer.frequency   s=    	,,U33	
 	
 	
 {r   c                 T    t          | j                            |                     dS )zEThe frequency, in samples per second, at which the buffer is playing.N)r   re   SetFrequencyru   s     r   r   zDirectSoundBuffer.frequency   s4     	,,U33	
 	
 	
 	
 	
r   c                     | j         r`t          j                    }t          | j                            t          j        |                               |j        |j	        |j
        fS dS )z-The orientation of the sound projection cone.r   )r}   r   r   r   rg   GetConeOrientationr(   rC   r   r   r   rI   orientations     r   cone_orientationz"DirectSoundBuffer.cone_orientation  sc     9 	-//K%88k9R9RSS   =+->>7r   c                     | j         r<|\  }}}t          | j                            |||t          j                             d S d S r   )r}   r   rg   SetConeOrientationr   r   )rI   rp   r   r   r   s        r   r   z"DirectSoundBuffer.cone_orientation  sZ    9 	GAq!%88Aq#BTUU    	 	r   _ConeAnglesinsideoutsidec                 `   | j         rt          j                    }t          j                    }t          | j                            t          j        |          t          j        |                               |                     |j	        |j	                  S |                     dd          S );The inside and outside angles of the sound projection cone.r   )
r}   r   rw   r   rg   GetConeAnglesr(   rC   r   rp   rI   r   r   s      r   cone_angleszDirectSoundBuffer.cone_angles  s     9 	*Y[[FikkG%33FL4H4H&,W^J_J_``   ##FL'-@@@##Aq)))r   c                 ~    | j         r5t          | j                            ||t          j                             dS dS )r   N)r}   r   rg   SetConeAnglesr   r   r   s      r   set_cone_anglesz!DirectSoundBuffer.set_cone_angles)  sM    9 	%33FGSEWXX    	 	r   c                     | j         rSt          j                    }t          | j                            t          j        |                               |j        S dS )zOThe volume of the sound outside the outside angle of the sound projection cone.r   )	r}   r   rn   r   rg   GetConeOutsideVolumer(   rC   rp   )rI   rr   s     r   cone_outside_volumez%DirectSoundBuffer.cone_outside_volume0  sX     9 	XZZF%::6<;O;OPP   <1r   c                 |    | j         r4t          | j                            |t          j                             d S d S r   )r}   r   rg   SetConeOutsideVolumer   r   ru   s     r   r   z%DirectSoundBuffer.cone_outside_volume<  sK    9 	%::5#BTUU    	 	r   c                     t          j                    }| j                            t           j        t          j        |                     t          |          S r   )r   IDirectSound3DListenerre   rh   IID_IDirectSound3DListenerr(   rC   DirectSoundListenerrI   native_listeners     r   rU   z!DirectSoundBuffer.create_listenerC  sN    466**3+I+1<+H+H	J 	J 	J"?333r   c                 l    t          | j                            ddt          j                             d S rW   )r   re   Playr   DSBPLAY_LOOPINGrO   s    r   playzDirectSoundBuffer.playI  s9    $$Q3+>??	
 	
 	
 	
 	
r   c                 R    t          | j                                                   d S r   )r   re   rl   rO   s    r   stopzDirectSoundBuffer.stopN  s0    $$&&	
 	
 	
 	
 	
r   c                       e Zd Zd ZdS )DirectSoundBuffer._WritePointerc                     t          j                    | _        t          j                    | _        t          j                    | _        t          j                    | _        d S r   )r(   c_void_paudio_ptr_1r   rw   audio_length_1audio_ptr_2audio_length_2rO   s    r   rK   z(DirectSoundBuffer._WritePointer.__init__T  sD    %00D"%)++D%00D"%)++Dr   N)r\   r]   r^   rK   r_   r   r   _WritePointerr   S  s#        	. 	. 	. 	. 	.r   r   c                    t          d                    ||                    sJ |                                 }t          | j                            ||t          j        |j                  t          j        |j	                  t          j        |j
                  t          j        |j                  d                     |S )NzDirectSoundBuffer.lock({}, {})r   )r?   formatr   r   re   Lockr(   rC   r   r   r   r   )rI   rb   
write_sizer2   s       r   lockzDirectSoundBuffer.lockZ  s    6==lJWWXXXXX$$&&$$\%/%+\'2E%F%F%+\'2H%I%I%+\'2E%F%F%+\'2H%I%I%&( (	
 	
 	
 r   c                     t          | j                            |j        |j        |j        |j                             d S r   )r   re   Unlockr   r   r   r   )rI   r2   s     r   unlockzDirectSoundBuffer.unlockh  sK    &&w':'.'='.':'.'=? ?	
 	
 	
 	
 	
r   N)r\   r]   r^   rK   rM   propertyrr   setterry   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   rU   r   r   r   r   r   r_   r   r   rQ   rQ   s   s       ) ) )- - -   X ]
 
 ]

 	G 	G X	G 
 
 

 1 1 X1 A A XA D D XD     X _  _ 
 
 X
    
 
 X
      X 
 
 
 	 	 X	    *]Xy,ABBK
* 
* X
*   	 	 X	    4 4 4
 
 


 
 

. . . . . . . .  
 
 
 
 
r   rQ   c                       e Zd Zd Zd Zed             Zej        d             Zed             Zej        d             ZdS )r   c                     || _         d S r   )_native_listenerr   s     r   rK   zDirectSoundListener.__init__r  s     /r   c                 X    | j         r"| j                                          d | _         d S d S r   )r   rN   rO   s    r   rM   zDirectSoundListener.deleteu  s:      	)!))+++$(D!!!	) 	)r   c                     t          j                    }t          | j                            t          j        |                               |j        |j        |j	        fS r   )
r   r   r   r   r   r(   rC   r   r   r   )rI   vectors     r   r   zDirectSoundListener.positionz  sR    !--fl6.B.BCC	
 	
 	
 x68++r   c                 t    t           | j        j        t          |          t          j        gz               d S r   )r   r   r   listr   r   ru   s     r   r   zDirectSoundListener.position  s?    -D!-Us?Q>R0RT	
 	
 	
 	
 	
r   c                 2   t          j                    }t          j                    }t          | j                            t          j        |          t          j        |                               |j        |j        |j	        |j        |j        |j	        fS r   )
r   r   r   r   GetOrientationr(   rC   r   r   r   )rI   fronttops      r   r   zDirectSoundListener.orientation  sv    moo!00e1D1DflSVFWFWXX	
 	
 	
 w#%==r   c                 t    t           | j        j        t          |          t          j        gz               d S r   )r   r   SetOrientationr   r   r   r   s     r   r   zDirectSoundListener.orientation  sA    0D!043D3DHZG[3[]	
 	
 	
 	
 	
r   N)	r\   r]   r^   rK   rM   r   r   r   r   r_   r   r   r   r   q  s        0 0 0) ) )
 , , X, _
 
 _

 > > X> 
 
 
 
 
r   r   )__doc__r(   weakrefcollectionsr   pyglet.media.exceptionsr   pyglet.utilr   pyglet.window.win32r    r   r   
exceptionsr	   r?   r   r$   r7   r;   r=   r`   rQ   r   r_   r   r   <module>r      s      " " " " " " 2 2 2 2 2 2 # # # # # # ' ' ' ' ' '       . . . . . .	]	#	#. . .
      . . . . . . . .b :0=.2QRR {
 {
 {
 {
 {
 {
 {
 {
|$
 $
 $
 $
 $
 $
 $
 $
 $
 $
r   