
    ^j@                     b   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 G d	 d
e          Z G d d          Z G d de          Z G d de          Z G d de          Z edddg          Z G d de          Z G d de          Z G d de          ZdS )    N)
namedtuple   )
lib_openal)lib_alc)debug_print)MediaExceptiondebug_mediac                       e Zd ZddZd ZdS )OpenALExceptionNc                 0    || _         || _        || _        d S Nmessage
error_codeerror_stringselfr   r   r   s       `/home/agentuser/manim-venv/lib/python3.11/site-packages/pyglet/media/drivers/openal/interface.py__init__zOpenALException.__init__   s    $(    c                 X    | j         
d| j         S d| j          d| j         d| j         S )NzOpenAL Exception: zOpenAL Exception [z: z]: )r   r   r   r   s    r   __str__zOpenALException.__str__   sA    ?"6666___4;L__QUQ]___r   )NNN)__name__
__module____qualname__r   r    r   r   r   r      s<        ) ) ) )
` ` ` ` `r   r   c                   *    e Zd ZdZedd            ZdS )OpenALObjectzBase class for OpenAL objects.Nc                     t          j                    }|dk    rWt          j        |          }t          j        |t          j                  }t          ||t          |j                            dS zFCheck whether there is an OpenAL error and raise exception if present.r   r   N)	al
alGetErroralGetStringctypescastc_char_pr   strvalue)clsr   r   r   s       r   _check_errorzOpenALObject._check_error   so     ]__
??>*55L!;|V_EEL!'-7/2<3E/F/FH H H H	 ?r   r   )r   r   r   __doc__classmethodr+   r   r   r   r   r      s>        ((	H 	H 	H [	H 	H 	Hr   r   c                   V    e Zd ZdZddZd Zed             Zd Zd Z	d Z
dd	Zd
 ZdS )OpenALDevicezOpenAL audio device.Nc                     t          j        |          | _        |                     d           | j        t	          d          t                      | _        d S )NzFailed to open device.zNo OpenAL devices.)alcalcOpenDevice
_al_devicecheck_context_errorr   OpenALBufferPoolbuffer_pool)r   device_names     r   r   zOpenALDevice.__init__,   sT    +K88  !9:::?"!"6777+--r   c                     t          d          sJ t          j        | j                  t          j        k    r|                     d           d| _        dS )zAClose this ALDevice. No more buffers or contexts may exist on it.zClosing interface.OpenALDevicezFailed to close device.N)_debugr1   alcCloseDevicer3   	ALC_FALSE_raise_context_errorr   s    r   closezOpenALDevice.close4   sQ    677777do..#-??%%&?@@@r   c                     | j         d uS r   )r3   r   s    r   is_readyzOpenALDevice.is_ready;   s    d**r   c                     t          j        | j        d           }|                     d           t	          | |          S )NzFailed to create context)r1   alcCreateContextr3   r4   OpenALContext)r   
al_contexts     r   create_contextzOpenALDevice.create_context?   s;    )$/4@@
  !;<<<T:...r   c                    t          j                    }t          j                    }t          j        | j        t           j        t          j        |          |           |                     d           t          j        | j        t           j        t          j        |          |           |                     d           |j	        |j	        fS )NzFailed to get version.)
r1   ALCintalcGetIntegervr3   ALC_MAJOR_VERSIONr%   sizeofr4   ALC_MINOR_VERSIONr)   )r   majorminors      r   get_versionzOpenALDevice.get_versionD   s    

4?C,A!=//	8 	8 	8  !9:::4?C,A!=//	8 	8 	8  !9:::{EK''r   c                    t          j        | j        t           j                  }|                     d           t          j        |t
          j                  j        	                    d          
                                S )NzFailed to get extensions.ascii)r1   alcGetStringr3   ALC_EXTENSIONSr4   r%   r&   r'   r)   decodesplit)r   
extensionss     r   get_extensionszOpenALDevice.get_extensionsO   s_    %dos7IJJ
  !<==={:v77=DDWMMSSUUUr   c                     t          j        | j                  }|dk    r]t          j        | j        |          }t	          j        |t          j                  }t          ||t          |j	                            dS r!   )
r1   alcGetErrorr3   rP   r%   r&   r'   r   r(   r)   r   s       r   r4   z OpenALDevice.check_context_errorT   sx    _T_55
??+DOZHHL!;|V_EEL!'-7/2<3E/F/FH H H H	 ?r   c                 J    |                      |           t          |          )zyTry to check for OpenAL error and raise that, and then
        definitely raise an error with the given message.
        )r4   r   )r   r   s     r   r<   z!OpenALDevice._raise_context_error_   s&     	  )))g&&&r   r   )r   r   r   r,   r   r=   propertyr?   rD   rM   rU   r4   r<   r   r   r   r/   r/   *   s        . . . .   + + X+/ / /
	( 	( 	(V V V
	H 	H 	H 	H' ' ' ' 'r   r/   c                   2    e Zd Zd Zd Zd Zd Zd Zd ZdS )rB   c                 p    || _         || _        t                      | _        |                                  d S r   )device_al_contextset_sourcesmake_current)r   r\   rC   s      r   r   zOpenALContext.__init__h   s4    %r   c                 \    t          | j                  D ]}|                                 d S r   )tupler_   delete)r   ss     r   delete_sourceszOpenALContext.delete_sourcesn   s4    t}%% 	 	AHHJJJJ	 	r   c                    t          d          sJ t          j        t          j                    t          j                  j        t          j        | j        t          j                  j        k    r.t          j        d            | j	        
                    d           t          j        | j                   | j	        
                    d           d | _        d S )NzDelete interface.OpenALContextz)Failed to make context no longer current.zFailed to destroy context.)r9   r%   r&   r1   alcGetCurrentContextc_void_pr)   r]   alcMakeContextCurrentr\   r4   alcDestroyContextr   s    r   rc   zOpenALContext.deleter   s    677777 K022FODDJK(&/::@A A %d+++K++,WXXXd.///''(DEEEr   c                 l    t          j        | j                   | j                            d           d S )NzFailed to make context current.)r1   ri   r]   r\   r4   r   s    r   r`   zOpenALContext.make_current   s3    !$"2333''(IJJJJJr   c                     |                                   t          |           }| j                            |           |S r   )r`   OpenALSourcer_   add)r   
new_sources     r   create_sourcezOpenALContext.create_source   s=    !$''
*%%%r   c                 :    | j                             |           d S r   )r_   remove)r   sources     r   source_deletedzOpenALContext.source_deleted   s    V$$$$$r   N)	r   r   r   r   re   rc   r`   rp   rt   r   r   r   rB   rB   g   sq                 K K K  % % % % %r   rB   c                      e Zd Zd Zd Zed             Zed             Zed             Zed             Z	d Z
d Zd	 Z eej                  Z eej                  Z eej                  Z e
ej                  Z e
ej                  Z eej                  Z eej                  Z eej                  Z eej                  Z eej                   Z! eej"                  Z# eej$                  Z% eej&                  Z' eej(                  Z) eej*                  Z+ eej,                  Z- e
ej.                  Z/ e
ej0                  Z1[
[[d
 Z2d Z3d Z4d Z5d Z6d Z7d Z8d Z9d Z:d Z;d Z<d Z=d Z>d Z?dS )rm   c                    || _         t          j                    | _        t          j        d| j                   |                     d           |j        j        | _        d | _        | 	                                 i | _
        d S )Nr   zFailed to create source.)contextr"   ALuint
_al_sourcealGenSourcesr+   r\   r6   _state
_get_state_owned_buffers)r   rw   s     r   r   zOpenALSource.__init__   sq    )++
4?+++4555">5 r   c                    | j         t          d          sJ d S t          d          sJ t          j        d| j                   |                     d           | j                             |            d | _        d | _         d | _        d S )Nz9Delete interface.OpenAlSource on deleted source, ignoringzDelete interface.OpenALSourcer   zFailed to delete source.)rw   r9   r"   alDeleteSourcesry   r+   rt   r6   r   s    r   rc   zOpenALSource.delete   s    <UVVVVVF566666
1do...4555##D)))r   c                 T    |                                   | j        t          j        k    S r   )r|   r{   r"   
AL_INITIALr   s    r   
is_initialzOpenALSource.is_initial   !    {bm++r   c                 T    |                                   | j        t          j        k    S r   )r|   r{   r"   
AL_PLAYINGr   s    r   
is_playingzOpenALSource.is_playing   r   r   c                 T    |                                   | j        t          j        k    S r   )r|   r{   r"   	AL_PAUSEDr   s    r   	is_pausedzOpenALSource.is_paused   s!    {bl**r   c                 T    |                                   | j        t          j        k    S r   )r|   r{   r"   
AL_STOPPEDr   s    r   
is_stoppedzOpenALSource.is_stopped   r   r   c                 0     t           fd fd          S )Nc                 .    |                                S r   )_get_intr   	attributes    r   <lambda>z3OpenALSource._int_source_property.<locals>.<lambda>   s    T]]9%=%= r   c                 0    |                      |          S r   )_set_intr   r)   r   s     r   r   z3OpenALSource._int_source_property.<locals>.<lambda>   s    DMM)U,K,K r   rY   r   s   `r   _int_source_propertyz!OpenALSource._int_source_property   s0    ====KKKKM M 	Mr   c                 0     t           fd fd          S )Nc                 .    |                                S r   )
_get_floatr   s    r   r   z5OpenALSource._float_source_property.<locals>.<lambda>   s    T__Y%?%? r   c                 0    |                      |          S r   )
_set_floatr   s     r   r   z5OpenALSource._float_source_property.<locals>.<lambda>   s    DOOIu,M,M r   r   r   s   `r   _float_source_propertyz#OpenALSource._float_source_property   s0    ????MMMMO O 	Or   c                 0     t           fd fd          S )Nc                 .    |                                S r   )_get_3floatsr   s    r   r   z7OpenALSource._3floats_source_property.<locals>.<lambda>   s    T%6%6y%A%A r   c                 0    |                      |          S r   )_set_3floatsr   s     r   r   z7OpenALSource._3floats_source_property.<locals>.<lambda>   s    D,=,=i,O,O r   r   r   s   `r   _3floats_source_propertyz%OpenALSource._3floats_source_property   s0    AAAAOOOOQ Q 	Qr   c                 b    t          j        | j                   |                     d           d S )NzFailed to play source.)r"   alSourcePlayry   r+   r   s    r   playzOpenALSource.play   /    
(((233333r   c                 b    t          j        | j                   |                     d           d S )NzFailed to pause source.)r"   alSourcePausery   r+   r   s    r   pausezOpenALSource.pause   s0    
)))344444r   c                 b    t          j        | j                   |                     d           d S )NzFailed to stop source.)r"   alSourceStopry   r+   r   s    r   stopzOpenALSource.stop   r   r   c                     |                      t          j        t          j                   | j                            | j                                                   | j                                         d S r   )	r   r"   	AL_BUFFERAL_NONEr6   return_buffersr}   valuesclearr   s    r   r   zOpenALSource.clear   s[    blBJ///''(;(B(B(D(DEEE!!#####r   c                 4    | j                                         S r   )r6   
get_bufferr   s    r   r   zOpenALSource.get_buffer   s    **,,,r   c                     |j         sJ t          j        | j        dt	          j        |j                             |                     d           || j        |j	        <   d S )Nr   zFailed to queue buffer.)
is_validr"   alSourceQueueBuffersry   r%   byrefal_namer+   r}   name)r   bufs     r   queue_bufferzOpenALSource.queue_buffer   s\    ||
FL4M4MNNN3444(+CH%%%r   c                 \     j         }t          d                    |                    sJ |dk    ryt          j        |z              }t          j         j        t          |          |                                d            j	        
                     fd|D                        |S )NzProcessed buffer count: {}r   z&Failed to unqueue buffers from source.c                 D    g | ]}j                             |          S r   )r}   pop).0bnr   s     r   
<listcomp>z0OpenALSource.unqueue_buffers.<locals>.<listcomp>  s*    ,[,[,[RT-@-D-DR-H-H,[,[,[r   )buffers_processedr9   formatr"   rx   alSourceUnqueueBuffersry   lenr+   r6   r   )r   	processedbufferss   `  r   unqueue_bufferszOpenALSource.unqueue_buffers   s    *	299)DDEEEEEq==y9,//G%dos7||WMMMFGGG++,[,[,[,[SZ,[,[,[\\\r   c                 `    | j         &|                     t          j                  | _        d S d S r   )ry   r   r"   AL_SOURCE_STATEr{   r   s    r   r|   zOpenALSource._get_state  s,    ?&--(:;;DKKK '&r   c                     | j         J t          j                    }t          j        | j         ||           |                     d           |j        S NFailed to get value)ry   r"   ALintalGetSourceir+   r)   )r   keyal_ints      r   r   zOpenALSource._get_int
  sN    ***
f555/000|r   c                     | j         J t          j        | j         |t          |                     |                     d           d S NzFailed to set value.)ry   r"   	alSourceiintr+   r   r   r)   s      r   r   zOpenALSource._set_int  sG    ***
T_c3u::666011111r   c                     | j         J t          j                    }t          j        | j         ||           |                     d           |j        S r   )ry   r"   ALfloatalGetSourcefr+   r)   r   r   al_floats      r   r   zOpenALSource._get_float  sN    ***:<<
h777/000~r   c                     | j         J t          j        | j         |t          |                     |                     d           d S r   )ry   r"   	alSourceffloatr+   r   s      r   r   zOpenALSource._set_float  sG    ***
T_c5<<888011111r   c                    | j         J t          j                    }t          j                    }t          j                    }t          j        | j         ||||           |                     d           |j        |j        |j        fS r   )ry   r"   r   alGetSource3fr+   r)   r   r   xyzs        r   r   zOpenALSource._get_3floats"  st    ***JLLJLLJLL
#q!Q777/000w((r   c                     | j         J t          t          |          \  }}}t          j        | j         ||||           |                     d           d S r   )ry   mapr   r"   
alSource3fr+   r   r   r   r   r   r   s         r   r   zOpenALSource._set_3floats+  sZ    ***eV$$1a
dosAq!444011111r   N)@r   r   r   r   rc   rY   r   r   r   r   r   r   r   r"   AL_POSITIONpositionAL_VELOCITYvelocityAL_GAINgainAL_BUFFERS_QUEUEDbuffers_queuedAL_BUFFERS_PROCESSEDr   AL_MIN_GAINmin_gainAL_MAX_GAINmax_gainAL_REFERENCE_DISTANCEreference_distanceAL_ROLLOFF_FACTORrolloff_factorAL_PITCHpitchAL_MAX_DISTANCEmax_distanceAL_DIRECTION	directionAL_CONE_INNER_ANGLEcone_inner_angleAL_CONE_OUTER_ANGLEcone_outer_angleAL_CONE_OUTER_GAINcone_outer_gainAL_SEC_OFFSET
sec_offsetAL_SAMPLE_OFFSETsample_offsetAL_BYTE_OFFSETbyte_offsetr   r   r   r   r   r   r   r|   r   r   r   r   r   r   r   r   r   rm   rm      s       ! ! !   , , X, , , X, + + X+ , , X,M M MO O OQ Q Q ('77H''77H!!"*--D))"*>??N,,R-DEE%%bn55H%%bn55H//0HII++B,@AAN""2;//E))"*<==L((99I--b.DEE--b.DEE,,R-BCCO''(899J(()<==M&&r'899K 4 4 45 5 54 4 4$ $ $
- - -, , ,  < < <  2 2 2
  2 2 2
) ) )2 2 2 2 2r   rm   OpenALOrientationatupc                   
   e 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d	 Z	d
 Z
d Zd Zd Zd ZdS )OpenALListenerc                 @    |                      t          j                  S r   )r   r"   r   r   s    r   r   zOpenALListener.position6        000r   c                 F    |                      t          j        |           d S r   )r   r"   r   r   r   s     r   r   zOpenALListener.position:       ".&11111r   c                 @    |                      t          j                  S r   )r   r"   r   r   s    r   r   zOpenALListener.velocity>  r  r   c                 F    |                      t          j        |           d S r   )r   r"   r   r  s     r   r   zOpenALListener.velocityB  r  r   c                 @    |                      t          j                  S r   )r   r"   r   r   s    r   r   zOpenALListener.gainF  s    rz***r   c                 F    |                      t          j        |           d S r   )r   r"   r   )r   r)   s     r   r   zOpenALListener.gainJ  s    
E*****r   c                     |                      t          j        d          }t          |dd         |dd                   S )N   r      )_get_float_vectorr"   AL_ORIENTATIONr  r  s     r   orientationzOpenALListener.orientationN  s:    ''(91== !fQqSk:::r   c                    t          |          dk    r|d         |d         z   }nt          |          dk    r|}ng }t          |          dk    rt          d          |                     t          j        |           d S )N   r   r   r  z#Need 2 tuples of 3 or 1 tuple of 6.)r   
ValueError_set_float_vectorr"   r!  )r   r   actual_valuess      r   r"  zOpenALListener.orientationS  s    v;;!"1Iq	1MM[[A"MMM}""BCCCr0-@@@@@r   c                     t          j                    }t          j        ||           |                     d           |j        S r   )r"   r   alGetListenerfr+   r)   r   s      r   r   zOpenALListener._get_float_  s=    :<<
#x(((/000~r   c                 t    t          j        |t          |                     |                     d           d S r   )r"   alListenerfr   r+   r   s      r   r   zOpenALListener._set_floate  s5    
sE%LL)))011111r   c                     t          j                    }t          j                    }t          j                    }t          j        ||||           |                     d           |j        |j        |j        fS r   )r"   r   alGetListener3fr+   r)   r   s        r   r   zOpenALListener._get_3floatsi  sb    JLLJLLJLL
31a(((/000w((r   c                     t          t          |          \  }}}t          j        ||||           |                     d           d S r   )r   r   r"   alListener3fr+   r   s         r   r   zOpenALListener._set_3floatsq  sH    eV$$1a
Q1%%%011111r   c                     t          j        |z              }t          j        ||           |                     d           d |D             S )Nr   c                     g | ]}|S r   r   )r   r   s     r   r   z4OpenALListener._get_float_vector.<locals>.<listcomp>z  s    +++a+++r   )r"   r   alGetListenerfvr+   )r   r   countal_float_vectors       r   r   z OpenALListener._get_float_vectorv  sP    :-00
3000/000++?++++r   c                     t          j        t          |          z  | }t          j        ||           |                     d           d S r   )r"   r   r   alListenerfvr+   )r   r   r   r4  s       r   r&  z OpenALListener._set_float_vector|  sF    :F3f=
_---011111r   N)r   r   r   rY   r   setterr   r   r"  r   r   r   r   r   r&  r   r   r   r  r  5  sQ       1 1 X1 _2 2 _2 1 1 X1 _2 2 _2 + + X+ 
[+ + [+ ; ; X; 	A 	A 	A  2 2 2) ) )2 2 2
, , ,2 2 2 2 2r   r  c                   l    e Zd Zej        ej        ej        ej        dZd Z	e
d             Zd Zd ZdS )OpenALBuffer))r      )r      )r$  r:  )r$  r;  c                 >    || _         |j        | _        | j        sJ d S r   )r   r)   r   r   )r   r   s     r   r   zOpenALBuffer.__init__  s%    M	}}r   c                     |                      d           | j        dS t          t          j        | j                            }|st          j                     |S )NzBefore validate buffer.F)r+   r   boolr"   
alIsBufferr#   )r   valids     r   r   zOpenALBuffer.is_valid  sU    3444<5R]4<0011 	MOOOr   c                     | j         Q| j        rLt          j        dt	          j        | j                              |                     d           d | _         d S d S d S )Nr   zError deleting buffer.)r   r   r"   alDeleteBuffersr%   r   r+   r   s    r   rc   zOpenALBuffer.delete  s^    <##q&,t|"<"<===6777DLLL $###r   c                    | j         sJ 	 | j        |j        |j        f         }n&# t          $ r t          d|j         d          w xY wt          j        | j        ||j	        |j
        |j                   |                     d           d S )NzOpenAL does not support 'zbit' audio.zFailed to add data to buffer.)r   _format_mapchannelssample_sizeKeyErrorr   r"   alBufferDatar   pointerlengthsample_rater+   )r   
audio_dataaudio_format	al_formats       r   datazOpenALBuffer.data  s    }}	d(,*?AY)Z[II 	d 	d 	d !b\=U!b!b!bccc	d 	!"*")$0		2 	2 	2
 	9:::::s	   % #AN)r   r   r   r"   AL_FORMAT_MONO8AL_FORMAT_MONO16AL_FORMAT_STEREO8AL_FORMAT_STEREO16rD  r   rY   r   rc   rO  r   r   r   r9  r9    s        #$%&	 K     X     ; ; ; ; ;r   r9  c                   6    e Zd ZdZd Zd Zd Zd Zd Zd Z	dS )	r5   zAt least Mac OS X doesn't free buffers when a source is deleted; it just
    detaches them from the source.  So keep our own recycled queue.
    c                     g | _         d S r   )_buffersr   s    r   r   zOpenALBufferPool.__init__  s    ''r   c                 *    t          | j                  S r   )r   rV  r   s    r   __len__zOpenALBufferPool.__len__  s    4=!!!r   c                     t          d          sJ | j        r4| j                                                                         | j        2d S d S )Nz!Delete interface.OpenALBufferPool)r9   rV  r   rc   r   s    r   rc   zOpenALBufferPool.delete  sa    9:::::m 	)M&&((( m 	) 	) 	) 	) 	)r   c                 8    |                      d          d         S )z)Convenience for returning one buffer namer   r   )get_buffersr   s    r   r   zOpenALBufferPool.get_buffer  s    ""1%%r   c                    g }| j         rbt          |          |k     rO| j                                         }|j        r|                    |           | j         rt          |          |k     O|t          |          z
  x}dk    r_t          j        |z              }t          j        ||           |                     d           |	                    d |D                        |S )zReturns an array containing `number` buffer names.  The returned list must
        not be modified in any way, and may get changed by subsequent calls to
        get_buffers.
        r   zError generating buffers.c              3   X   K   | ]%}t          t          j        |                    V  &d S r   )r9  r"   rx   )r   r   s     r   	<genexpr>z/OpenALBufferPool.get_buffers.<locals>.<genexpr>  s2      OO|BIdOO<<OOOOOOr   )
rV  r   r   r   appendr"   rx   alGenBuffersr+   extend)r   numberret_buffersbmissingnamess         r   r[  zOpenALBufferPool.get_buffers  s    
 m 	&K 0 06 9 9!!##Az &""1%%% m 	&K 0 06 9 9 K 0 000GA55Y(++EOGU+++9:::OOOOOOOOr   c                 R    |D ]#}|j         r| j                            |           $dS )z2Throw buffers not used anymore back into the pool.N)r   rV  r_  )r   r   r   s      r   r   zOpenALBufferPool.return_buffers  s<     	* 	*C| *$$S)))	* 	*r   N)
r   r   r   r,   r   rX  rc   r   r[  r   r   r   r   r5   r5     sx         ( ( (" " ") ) )
& & &  ,* * * * *r   r5   )r%   weakrefcollectionsr    r   r"   r   r1   pyglet.utilr   pyglet.media.exceptionsr   r9   r   r   r/   rB   rm   r  r  r9  r5   r   r   r   <module>rm     s     " " " " " "             # # # # # # 2 2 2 2 2 2	]	#	#
` 
` 
` 
` 
`n 
` 
` 
`H H H H H H H H:' :' :' :' :'< :' :' :'z$% $% $% $% $%L $% $% $%Na2 a2 a2 a2 a2< a2 a2 a2H J2T4LAA J2 J2 J2 J2 J2\ J2 J2 J2Z,; ,; ,; ,; ,;< ,; ,; ,;^.* .* .* .* .*| .* .* .* .* .*r   