
    \
jf<                         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  ed	          Zd
 Z G d d          Z G d d          Z G d d          Z G d d          ZdS )z$
Pythonic interface to DirectSound.
    N)
namedtuple)debug_print)_user32   )
lib_dsound)DirectSoundNativeErrordebug_mediac                 D    | t           j        k    rt          |           d S N)libDS_OKr   )hresults    d/DATA/AppData/hermes/venv/lib/python3.11/site-packages/pyglet/media/drivers/directsound/interface.py_checkr   3   s%    #)$W---     c                   &    e 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                             t          | j                  | _        | j                                        | _        d S )NzConstructing DirectSoundDriver)_debugr   IDirectSound_native_dsoundr   DirectSoundCreatectypesbyrefr   GetDesktopWindowSetCooperativeLevelDSSCL_NORMALDirectSoundBufferFactory_buffer_factorycreate_primary_bufferprimary_buffer)selfhwnds     r   __init__zDirectSoundDriver.__init__9   s    677777!.00!$T5H(I(I4PP	
 	
 	
 '))33D#:JKK	
 	
 	
  88KLL"2HHJJr   c                 j    	 d | _         | j                                         d S # t          $ r Y d S w xY wr   )r!   r   Release
ValueErrorr"   s    r   __del__zDirectSoundDriver.__del__L   sL    	"&D''))))) 	 	 	DD	s    $ 
22c                 6    | j                             |          S r   )r   create_buffer)r"   audio_formats     r   r+   zDirectSoundDriver.create_bufferS   s    #11,???r   c                 4    | j                                         S r   )r!   create_listenerr(   s    r   r.   z!DirectSoundDriver.create_listenerV   s    "22444r   N)__name__
__module____qualname__r$   r)   r+   r.    r   r   r   r   8   sV        K K K&  @ @ @5 5 5 5 5r   r   c                   l    e Zd ZdZd Zd Zd Zd Zed             Z	e
d             Ze
d             Zd	S )
r   g       @c                 8    t          j        |          | _        d S r   )weakrefproxyr   )r"   native_dsounds     r   r$   z!DirectSoundBufferFactory.__init__]   s     &mM::r   c                     t          |j        | j        z            }|                     |          }|                     ||          }t          |                     |          ||          S r   )intsample_ratedefault_buffer_size_create_wave_format_create_buffer_descDirectSoundBuffer_create_buffer)r"   r,   buffer_sizewave_formatbuffer_descs        r   r+   z&DirectSoundBufferFactory.create_bufferb   sk    ,2T5MMNN..|<<..{KHH ##K00  	r   c                 n    t          |                     |                                           d d          S Nr   )r>   r?   _create_primary_buffer_descr(   s    r   r    z.DirectSoundBufferFactory.create_primary_bufferk   s7     ##D$D$D$F$FGG  	r   c                     t          j                    }t          | j                            |t          j        |          d                      |S r   )r   IDirectSoundBufferr   r   CreateSoundBufferr   r   )r"   rB   bufs      r   r?   z'DirectSoundBufferFactory._create_bufferq   sL    $&&11+v|C?P?PRVWW	
 	
 	
 
r   c                     t          j                    }t           j        |_        | j        |_        | j        |_        | j        |_	        |j	        |j        z  dz  |_
        |j        |j
        z  |_        |S )N   )r   WAVEFORMATEXWAVE_FORMAT_PCM
wFormatTagchannels	nChannelsr:   nSamplesPerSecsample_sizewBitsPerSamplenBlockAlignnAvgBytesPerSec)r,   wfxs     r   r<   z,DirectSoundBufferFactory._create_wave_formatx   sg      ,$-)5)5,s}<A!03?B
r   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   DSBUFFERDESCr   sizeofdwSizeDSBCAPS_GLOBALFOCUSDSBCAPS_GETCURRENTPOSITION2DSBCAPS_CTRLFREQUENCYDSBCAPS_CTRLVOLUMEdwFlagsrP   DSBCAPS_CTRL3DdwBufferBytespointerlpwfxFormat)clsrA   r@   dsbdescs       r   r=   z,DirectSoundBufferFactory._create_buffer_desc   s    "$$w//2:;45 12  A%%OOs11OO +$n[99r   c                     t          j                    }t          j        |          |_        t           j        t           j        z  t           j        z  |_        |S )z.Primary buffer with 3D and volume capabilities)	r   rY   r   rZ   r[   ra   r_   DSBCAPS_PRIMARYBUFFERr`   )re   rB   s     r   rE   z4DirectSoundBufferFactory._create_primary_buffer_desc   sM     &((#];77"1"5 6"8 9 r   N)r/   r0   r1   r;   r$   r+   r    r?   staticmethodr<   classmethodr=   rE   r2   r   r   r   r   Z   s        ; ; ;
         \   [   [  r   r   c                      e Zd Zd Zd Zd Zed             Zej        d             Z e	dddg          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 )(r>   c                    || _         || _        || _        |a|j        dk    rVt	          j                    | _        | j                            t          j        t          j
        | j                             d S d | _        d S rX   )r,   r@   _native_bufferrO   r   IDirectSound3DBuffer_native_buffer3dQueryInterfaceIID_IDirectSound3DBufferr   r   )r"   native_bufferr,   r@   s       r   r$   zDirectSoundBuffer.__init__   s    (&+#(=(B(B$'$<$>$>D!..s/K/5|D<Q/R/RT T T T T %)D!!!r   c                 R    	 |                                   d S # t          $ r Y d S w xY wr   )deleteOSErrorr(   s    r   r)   zDirectSoundBuffer.__del__   s:    	KKMMMMM 	 	 	DD	s    
&&c                     | j         b| j                                          | j                                          d | _         | j        $| j                                         d | _        d S d S d S r   )rm   Stopr&   ro   r(   s    r   rt   zDirectSoundBuffer.delete   sy    *$$&&&'')))"&D$0%--///(,%%% +* 10r   c                     t          j                    }t          | j                            t          j        |                               |j        S r   )r   LONGr   rm   	GetVolumer   r   value)r"   vols     r   volumezDirectSoundBuffer.volume   sG    hjj))&,s*;*;<<	
 	
 	
 yr   c                 T    t          | j                            |                     d S r   )r   rm   	SetVolumer"   r{   s     r   r}   zDirectSoundBuffer.volume   s2    ))%00	
 	
 	
 	
 	
r   _CurrentPositionplay_cursorwrite_cursorc                     t          j                    }t          j                    }t          | j                            ||                     |                     |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   rm   GetCurrentPositionr   r{   )r"   r   r   s      r   current_positionz"DirectSoundBuffer.current_position   sg     ikky{{22;3?A A	
 	
 	
 $$[%68JKKKr   c                 T    t          | j                            |                     d S r   )r   rm   SetCurrentPositionr   s     r   r   z"DirectSoundBuffer.current_position   s2    22599	
 	
 	
 	
 	
r   c                     | j         d uS r   )ro   r(   s    r   is3dzDirectSoundBuffer.is3d   s    $D00r   c                 L    |                                  t          j        z  dk    S rD   )_get_statusr   DSBSTATUS_PLAYINGr(   s    r   
is_playingzDirectSoundBuffer.is_playing   s!      ""S%::q@@r   c                 L    |                                  t          j        z  dk    S rD   )r   r   DSBSTATUS_BUFFERLOSTr(   s    r   is_buffer_lostz DirectSoundBuffer.is_buffer_lost   s!      ""S%==!CCr   c                     t          j                    }t          | j                            |                     |j        S r   )r   r   r   rm   	GetStatusr{   )r"   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   ro   GetPositionr   r   xyz)r"   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   ro   SetPositionr   DS3D_IMMEDIATE)r"   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   ro   GetMinDistancer   r   r{   r   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   ro   SetMinDistancer   r   r   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   ro   GetMaxDistancer   r   r{   r   s     r   max_distancezDirectSoundBuffer.max_distance  r   r   c                 |    | j         r4t          | j                            |t          j                             d S d S r   )r   r   ro   SetMaxDistancer   r   r   s     r   r   zDirectSoundBuffer.max_distance$  r   r   c                     t          j                    }t          | j                            |                     |j        S r   )r   r   r   rm   GetFrequencyr{   r   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   rm   SetFrequencyr   s     r   r   zDirectSoundBuffer.frequency3  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   ro   GetConeOrientationr   r   r   r   r   r"   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   ro   SetConeOrientationr   r   )r"   r{   r   r   r   s        r   r   z"DirectSoundBuffer.cone_orientationF  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   r   r   ro   GetConeAnglesr   r   r   r{   r"   r   r   s      r   cone_angleszDirectSoundBuffer.cone_anglesO  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   ro   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   ry   r   ro   GetConeOutsideVolumer   r   r{   )r"   r}   s     r   cone_outside_volumez%DirectSoundBuffer.cone_outside_volumec  sX     9 	XZZF%::6<;O;OPP   <1r   c                 |    | j         r4t          | j                            |t          j                             d S d S r   )r   r   ro   SetConeOutsideVolumer   r   r   s     r   r   z%DirectSoundBuffer.cone_outside_volumeo  sK    9 	%::5#BTUU    	 	r   c                     t          j                    }| j                            t           j        t          j        |                     t          | |          S r   )r   IDirectSound3DListenerrm   rp   IID_IDirectSound3DListenerr   r   DirectSoundListener)r"   native_listeners     r   r.   z!DirectSoundBuffer.create_listenerv  sP    466**3+I+1<+H+H	J 	J 	J"4999r   c                 l    t          | j                            ddt          j                             d S rD   )r   rm   Playr   DSBPLAY_LOOPINGr(   s    r   playzDirectSoundBuffer.play|  s9    $$Q3+>??	
 	
 	
 	
 	
r   c                 R    t          | j                                                   d S r   )r   rm   rw   r(   s    r   stopzDirectSoundBuffer.stop  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   r   audio_length_1audio_ptr_2audio_length_2r(   s    r   r$   z(DirectSoundBuffer._WritePointer.__init__  sD    %00D"%)++D%00D"%)++Dr   N)r/   r0   r1   r$   r2   r   r   _WritePointerr     s#        	. 	. 	. 	. 	.r   r   c                 B   t          d                    ||                    sJ |                                 }t          | j                            ||t          j        |j                  |j	        t          j        |j
                  |j        d                     |S )NzDirectSoundBuffer.lock({}, {})r   )r   formatr   r   rm   Lockr   r   r   r   r   r   )r"   r   
write_sizerc   s       r   lockzDirectSoundBuffer.lock  s    6==lJWWXXXXX$$&&$$\%/%+\'2E%F%F%,%;%+\'2E%F%F%,%;%&( (	
 	
 	
 r   c                     t          | j                            |j        |j        |j        |j                             d S r   )r   rm   Unlockr   r   r   r   )r"   rc   s     r   unlockzDirectSoundBuffer.unlock  sK    &&w':'.'='.':'.'=? ?	
 	
 	
 	
 	
r   N)r/   r0   r1   r$   r)   rt   propertyr}   setterr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r.   r   r   r   r   r   r2   r   r   r>   r>      sA       ) ) )  - - -   X ]
 
 ]

 "z"4}n6UVV	L 	L X	L 
 
 

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


 
 

. . . . . . . .  
 
 
 
 
r   r>   c                       e Zd 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                 F    t          j        |          | _        || _        d S r   )r5   r6   	ds_buffer_native_listener)r"   r   r   s      r   r$   zDirectSoundListener.__init__  s#     !y11 /r   c                 .    |                                   d S r   )rt   r(   s    r   r)   zDirectSoundListener.__del__  s    r   c                 X    | j         r"| j                                          d | _         d S d S r   )r   r&   r(   s    r   rt   zDirectSoundListener.delete  s:      	)!))+++$(D!!!	) 	)r   c                     t          j                    }t          | j                            t          j        |                               |j        |j        |j	        fS r   )
r   r   r   r   r   r   r   r   r   r   )r"   vectors     r   r   zDirectSoundListener.position  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   r   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   r   r   r   r   )r"   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/   r0   r1   r$   r)   rt   r   r   r   r   r2   r   r   r   r     s        0 0 0  ) ) )
 , , X, _
 
 _

 > > X> 
 
 
 
 
r   r   )__doc__r   r5   collectionsr   pyglet.utilr   pyglet.window.win32r    r   r   
exceptionsr   r   r   r   r   r>   r   r2   r   r   <module>r      sY  F    " " " " " " # # # # # # ' ' ' ' ' '       . . . . . .	]	#	#. . .
5 5 5 5 5 5 5 5DA A A A A A A AHC
 C
 C
 C
 C
 C
 C
 C
L*
 *
 *
 *
 *
 *
 *
 *
 *
 *
r   