
    ^j@E              
          d dl Z d dlZd dlZd dlmZ d dlmZmZm	Z	m
Z
mZmZmZmZmZ d dlmZmZmZ d dlmZ d dlmZmZmZmZ d dlmZmZ  ed          ZerZe j        se j        se j         s ej!        d	           e j"        s ej!        d
           e j#        se j$        s ej!        d           e j        re j        se j         r e j#        se j$        se j"        s e%d          e j"        r ee	 ee j&        j'                  e          Z(e j&        j)        e j&        j*        j+        _,         ee j&        j'                  e j&        j-        eeeee j&        j.        e j&        j/        e j&        j0        eg
e j&        j*        j+        _1        d Z2e2e j3        _2         G d d          Z4 G d de j5                  Z6 G d de j7                  Z8 G d d          Z9 G d de8          Z: G d de6          Z; G d de j3                  Z< G d de<          Z= G d de          Z> G d  d!e>          Z? G d" d#e>          Z@ G d$ d%e>          ZA G d& d'e          ZBd( ZCd) ZDdS )*    N)abstractmethod)	c_void_pPOINTERc_intpointercastc_charc_char_p	CFUNCTYPEc_ubyte)memmovecreate_string_bufferbyref)StreamingSource)AudioFormat	AudioDataMediaDecoderStaticSource)debug_printDecodeExceptionzDebug PyOgg codecz=PyOgg determined the ogg/vorbis libraries were not available.z4PyOgg determined the flac library was not available.z7PyOgg determined the opus libraries were not available.z2PyOgg determined no supported libraries were foundc                     |j         j        j        j        | _        |j         j        j        j        | _        |j         j        j        j        | _        |j         j        j        j        | _        d S N)contentsdatastream_infobits_per_sampletotal_sampleschannelssample_rate	frequency)selfdecodermetadataclient_datas       T/home/agentuser/manim-venv/lib/python3.11/site-packages/pyglet/media/codecs/pyogg.pymetadata_callbackr&   4   sT    '05AQ%.3?M ).:C!*/;G    c                       e Zd Zd ZdS )MemoryVorbisObjectc                     | _          fd} fd}d } fd}t          j                            |           _        t          j                            |           _        t          j                            |           _        t          j                            |           _        t          j                             j         j         j         j                   _        d S )Nc                     ||z  }j                             |          }t          |          }t          | ||           |S r   filereadlenr   )ptr	byte_sizesize_to_read
datasource	data_sizer   	read_sizer!   s          r%   read_func_cbz1MemoryVorbisObject.__init__.<locals>.read_func_cbC   sC    $y0I9>>),,DD		ICy)))r'   c                 >    j                             ||          }|S r   r-   seek)r3   offsetwhenceposr!   s       r%   seek_func_cbz1MemoryVorbisObject.__init__.<locals>.seek_func_cbJ   s    )..00CJr'   c                     dS Nr    )r3   s    r%   close_func_cbz2MemoryVorbisObject.__init__.<locals>.close_func_cbN       1r'   c                 6    j                                         S r   r-   tell)r3   r!   s    r%   tell_func_cbz1MemoryVorbisObject.__init__.<locals>.tell_func_cbQ   s    9>>###r'   )	r-   pyoggvorbis	read_func	seek_func
close_func	tell_funcov_callbacks	callbacks)r!   r-   r6   r=   rA   rF   s   `     r%   __init__zMemoryVorbisObject.__init__@   s    		 	 	 	 		 	 	 	 		 	 		$ 	$ 	$ 	$ 	$ //==//==,11-@@//==224>4>SWSbdhdrssr'   N__name__
__module____qualname__rO   r@   r'   r%   r)   r)   ?   s(        t t t t tr'   r)   c                       e Zd Zd Zd ZdS )UnclosedVorbisFileStreamc                     | j         r1t          j                            t	          | j                             d| _         d S )NF)existsrG   rH   ov_clearr   vfr!   s    r%   __del__z UnclosedVorbisFileStream.__del__]   s5    ; 	2L!!%..111r'   c                     dS )zPyOgg calls clean_up on end of data. We may want to loop a sound or replay. Prevent this.
        Rely on GC (__del__) to clean up objects instead.
        Nr@   rZ   s    r%   clean_upz!UnclosedVorbisFileStream.clean_upb   s	     	r'   NrQ   rR   rS   r[   r]   r@   r'   r%   rU   rU   \   s2          
    r'   rU   c                       e Zd Zd Zd ZdS )UnclosedOpusFileStreamc                     | j         | j        j        _        | `| j        r&t
          j                            | j                   d S d S r   )ptr_initr0   r   valueofrG   opusop_freerZ   s    r%   r[   zUnclosedOpusFileStream.__del__j   sH    "&-H7 	(Jtw'''''	( 	(r'   c                     d S r   r@   rZ   s    r%   r]   zUnclosedOpusFileStream.clean_upr   s    r'   Nr^   r@   r'   r%   r`   r`   i   s2        ( ( (    r'   r`   c                       e Zd Zd ZdS )MemoryOpusObjectc                     | _         | _         fd} fd} fd}d }t          j                            |           _        t          j                            |           _        t          j                            |           _	        t          j        
                    |           _        t          j                             j         j         j	         j                   _        d S )Nc                 |    j                             |          }t          |          }t          |||           |S r   r,   )streambuffersizer   r5   r!   s        r%   r6   z/MemoryOpusObject.__init__.<locals>.read_func_cb{   s9    9>>$''DD		IFD),,,r'   c                 >    j                             ||           dS r?   r8   )rl   r:   r;   r!   s      r%   r=   z/MemoryOpusObject.__init__.<locals>.seek_func_cb   s    INN66***1r'   c                 :    j                                         }|S r   rD   )rl   r<   r!   s     r%   rF   z/MemoryOpusObject.__init__.<locals>.tell_func_cb   s    )..""CJr'   c                     dS r?   r@   )rl   s    r%   rA   z0MemoryOpusObject.__init__.<locals>.close_func_cb   rB   r'   )r-   filenamerG   re   op_read_funcrI   op_seek_funcrJ   op_tell_funcrL   op_close_funcrK   OpusFileCallbacksrN   )r!   rr   r-   r6   r=   rF   rA   s   `      r%   rO   zMemoryOpusObject.__init__w   s    	 	 	 	 	 		 	 	 	 		 	 	 	 		 	 	 00>>00>>00>>*22=AA55dndnVZVdfjfuvvr'   NrP   r@   r'   r%   ri   ri   v   s(        w w w w wr'   ri   c                       e Zd Zd ZdS )MemoryOpusFileStreamc           	      \   || _         t          ||          | _        t                      | _        t                      }t          t          j                  | _	        t          | j	        t          t                              | _        t          j                            | j        t!          | j        j                  | j        dt!          |                    | _        |j        dk    r(t)          d                    ||j                            t          j                            | j        d          | _        t          j                            | j        d          | _        d| _        t          j        j        t          j        | j        z  dz  z  | _        t          t;          |                                           t          j        j                  | _        t          t;          | j                  t          t                              | _         | j         j!        j        | _"        d S )Nr   z;file-like object: {} couldn't be processed. Error code : {}i     )#r-   ri   memory_objectr   _dummy_fileobjr   r   rG   PYOGG_STREAM_BUFFER_SIZEread_bufferr   r   r   
ptr_bufferre   op_open_callbacksr   rN   rd   rc   r   formatop_channel_countr   op_pcm_totalpcm_sizer    
opus_int16bfarr_tr   opus_int16_prm   r0   r   rb   )r!   rr   r-   errors       r%   rO   zMemoryOpusFileStream.__init__   s   	-h==&jj/0NOOt/1A1ABB*..$$.//O%LL
 
 ;!!MTTU]_d_jkkm m m 
33DGR@@
//<<z,0NQUQ^0^ab0bc74<<>>22EJ4KLL,,gh.?.?@@)/r'   NrP   r@   r'   r%   ry   ry      s#        %0 %0 %0 %0 %0r'   ry   c                       e Zd Zd ZdS )MemoryVorbisFileStreamc                    t          t          j                  }t          j                                        | _        t          |          | _        t          j        j        	                    || j        d d| j        j
                  }|dk    r"t          d                    |                    t          j                            t          | j                  d          }|j        j        | _        |j        j        | _        t'          t          j        | j        z  z              }t)          t+          |          t,                    | _        t1                      | _        t+          | j                  | _        d| _        d S )Nr   z9file couldn't be opened or doesn't exist. Error code : {}r{   T)r   rG   r   rH   OggVorbis_FilerY   r)   r}   libvorbisfileov_open_callbacksrN   r   r   ov_infor   r   r   rater    r	   r   r   r
   buffer_r   	bitstreambitstream_pointerrW   )r!   pathr-   buffr   infoarrays          r%   rO   zMemoryVorbisFileStream.__init__   s   #E$BCC,--///55*<<T47DRSUYUgUqrrA::!"]"d"dej"k"klll|##E$'NNB77.+59DMIJMMGENNH55!(!8!8r'   NrP   r@   r'   r%   r   r      s#            r'   r   c                   $     e Zd Z fdZd Z xZS )UnclosedFLACFileStreamc                 H     t                      j        |i | d| _        d S )NT)superrO   seekable)r!   argskw	__class__s      r%   rO   zUnclosedFLACFileStream.__init__   s*    $%"%%%r'   c                 `    | j         r&t          j                            | j                    d S d S r   )r"   rG   flacFLAC__stream_decoder_finishrZ   s    r%   r[   zUnclosedFLACFileStream.__del__   s7    < 	AJ224<@@@@@	A 	Ar'   )rQ   rR   rS   rO   r[   __classcell__)r   s   @r%   r   r      sN            A A A A A A Ar'   r   c                   2    e Zd Zd Zd Zd Zd Zd Zd ZdS )MemoryFLACFileStreamc                    || _         d| _        t          | j         dd           rqt          | j         dd           r[d| _        | j                             dd           | j                                         | _        | j                             d           nt          j        d| d           d| _        t          j	        
                                | _        t                      | _        d | _        d | _        d | _        d | _        d | _        t          j	                            | j                  | _        t          j	                            | j                  | _        t          j	                            | j                  | _        t          j	                            | j                  | _        | j        rt          j	                            | j                   | _!        t          j	        "                    | j#                  | _$        t          j	        %                    | j&                  | _'        tQ          | j)                  | _*        nd | _!        d | _$        d | _'        d | _*        t          j	        j+        ,                    | j        | j        | j!        | j$        | j'        | j*        | j        | j        | j        | j        
  
        }|r8t[          d	.                    |t          j	        j/        |                             t          j	        0                    | j                  }|s"t[          d
.                    |                    d S )Nr   r9   rE   Tr|   z	Warning: z file object is not seekable.Fz.An error occurred when trying to open '{}': {}z:An error occurred when trying to decode the metadata of {})1r-   	file_sizegetattrr   r9   rE   warningswarnrG   r   FLAC__stream_decoder_newr"   r   r$   r   r    r   rm   bytes_written FLAC__StreamDecoderWriteCallbackwrite_callbackwrite_callback_#FLAC__StreamDecoderMetadataCallbackr&   metadata_callback_ FLAC__StreamDecoderErrorCallbackerror_callbackerror_callback_FLAC__StreamDecoderReadCallbackread_callbackread_callback_FLAC__StreamDecoderSeekCallbackseek_callbackseek_callback_FLAC__StreamDecoderTellCallbacktell_callbacktell_callback_!FLAC__StreamDecoderLengthCallbacklength_callbacklength_callback_FLAC__StreamDecoderEofCallbackeof_callbackeof_callback_libflac FLAC__stream_decoder_init_streamr   r   !FLAC__StreamDecoderInitStatusEnum2FLAC__stream_decoder_process_until_end_of_metadata)r!   r   r-   init_statusmetadata_statuss        r%   rO   zMemoryFLACFileStream.__init__   s   	49fd++ 	"	640P0P 	" DMINN1a   !Y^^--DNINN1MIdIIIJJJ!DMz::<<#::!!$zJJ4K^__"'*"P"PQUQg"h"h$zJJ4K^__#jHHI[\\= 		&"'*"L"LTM_"`"`D"'*"L"LTM_"`"`D$)J$P$PQUQe$f$fD!!?@Q!R!RD"&D"&D$(D!!%Dj(IIL! # 
 
  	R!"R"Y"YejB;O#Q #Q R R R  *WWX\Xdee 	m!"^"e"efj"k"klll	m 	mr'   c                     |j         j        }| j                            |          }t	          |          }t          |||           ||j         _        |dk    rdS |dk    rdS dS )Nr      r|   )r   rc   r-   r.   r/   r   )r!   r"   rm   rn   r   chunkr5   s          r%   r   z"MemoryFLACFileStream.read_callback$  sg    #y~~e$$II	i((('q==1!^^11r'   c                 L    | j                             |d          }|dk     rdS dS )Nr   r   r8   r!   r"   r:   r   r<   s        r%   r   z"MemoryFLACFileStream.seek_callback3  s*    innVQ''7711r'   c                 `    | j                                         }|dk     rdS ||j        _        dS )z>Decoder wants to know the current position of the file stream.r   r   )r-   rE   r   rc   r   s        r%   r   z"MemoryFLACFileStream.tell_callback:  s0    inn771$'FO!1r'   c                 B    | j         dk    rdS | j         |j        _        dS )z5Decoder wants to know the total length of the stream.r   r   )r   r   rc   )r!   r"   lengthr   s       r%   r   z$MemoryFLACFileStream.length_callbackC  s%    >Q1$(NFO!1r'   c                 F    | j                                         | j        k    S r   )r-   rE   r   )r!   r"   r   s      r%   r   z!MemoryFLACFileStream.eof_callbackK  s    y~~4>11r'   N)	rQ   rR   rS   rO   r   r   r   r   r   r@   r'   r%   r   r      sr        ?m ?m ?mB        2 2 2 2 2r'   r   c                   8    e Zd Zd Zed             ZddZd ZdS )PyOggSourcec                     || _         || _        d | _        d| _        |                                  t          | j        j        | j        | j        j                  | _        d S )N   )r   sample_sizer   )	rr   r-   _streamr   _load_sourcer   r   r    audio_format)r!   rr   r-   s      r%   rO   zPyOggSource.__init__P  sf     	'1FTXTd48L4JL L Lr'   c                     d S r   r@   rZ   s    r%   r   zPyOggSource._load_source[  s    r'           c                     | j                                         }|B|\  }}t          |t          t                              }t          |d|         |ddg           S dS )zPData returns as c_short_array instead of LP_c_char or c_ubyte, cast each buffer.N  )r   
get_bufferr   r   r	   r   )r!   	num_bytescompensation_timer   r   r   buff_char_ps          r%   get_audio_datazPyOggSource.get_audio_data_  s]    |&&((LD&tWV__55K[&164rJJJtr'   c                     | j         r| ` d S d S r   )r   rZ   s    r%   r[   zPyOggSource.__del__i  s    < 		 	r'   Nr   )rQ   rR   rS   rO   r   r   r   r[   r@   r'   r%   r   r   O  sd        	L 	L 	L   ^       r'   r   c                       e Zd Zd Zd ZdS )PyOggFLACSourcec                    | j         r t          | j        | j                   | _        nt	          | j                  | _        | j        j        | _        | j        j        | j        j        z  | _	        | j        j        dk    r-t          rt          j        d| j         d           d| _        d S | j	        | j        j        z  | _        d S )Nr   z#Unknown amount of samples found in z. Seeking may be limited.)r-   r   rr   r   r   r   r   r   r    	_duration_debugr   r   _duration_per_framerZ   s    r%   r   zPyOggFLACSource._load_sourcep  s    9 	A/tyIIDLL1$-@@DL<73dl6LL <%** nlDMlllmmm'(D$$$'+~8R'RD$$$r'   c                 |   | j         j        r| j        r;t          dt	          || j                            }t          || j        z            }nd}t          j        	                    | j         j
        |          }|du rt          j        d| j                    d S d S t          j        d| j         d           d S )Nr   r   FzFailed to seek FLAC file: z&Stream is not seekable for FLAC file: .)r   r   r   maxminr   intrG   r   "FLAC__stream_decoder_seek_absoluter"   r   r   rr   )r!   	timestamppositionseek_succeededs       r%   r9   zPyOggFLACSource.seek  s    <  	U' SDN%C%CDD	y4+CCDD"ZJJ4<K_aijjN&&J4=JJKKKKK '& MS4=SSSTTTTTr'   NrQ   rR   rS   r   r9   r@   r'   r%   r   r   n  s:        S S S"U U U U Ur'   r   c                   "    e Zd Zd ZddZd ZdS )PyOggVorbisSourcec                    | j         r t          | j        | j                   | _        nt	          | j                  | _        t
          j        j                            t          | j        j
                  d          | _        d S )Nr{   )r-   r   rr   r   rU   rG   rH   r   ov_time_totalr   rY   r   rZ   s    r%   r   zPyOggVorbisSource._load_source  sb    9 	C1$-KKDLL3DMBBDL3AA%BXBXZ\]]r'   r   c                 `    | j                                         }|t          g |ddg R  S d S )Nr   )r   r   r   )r!   r   r   r   s       r%   r   z PyOggVorbisSource.get_audio_data  sC    |&&((3d3D3$33333tr'   c                     t           j                            | j        j        |          }|dk    r(t
          r#t          j        d| j         d|            d S d S d S )Nr   zFailed to seek file z - )	rG   rH   ov_time_seekr   rY   r   r   r   rr   )r!   r   r   s      r%   r9   zPyOggVorbisSource.seek  sn    224<?INNQ YWT]WW~WWXXXXX Y Yr'   Nr   )rQ   rR   rS   r   r   r9   r@   r'   r%   r   r     sN        ^ ^ ^   Y Y Y Y Yr'   r   c                       e Zd Zd Zd ZdS )PyOggOpusSourcec                     | j         r t          | j        | j                   | _        nt	          | j                  | _        | j        j        | j        j        z  | _        | j        | j        j        z  | _        d S r   )	r-   ry   rr   r   r`   r   r    r   r   rZ   s    r%   r   zPyOggOpusSource._load_source  se    9 	A/tyIIDLL1$-@@DL.1GG#'>DL4I#I   r'   c                    t          dt          || j                            }t          || j        z            }t
          j                            | j        j	        |          }|rt          j        d| d           d S d S )Nr   z$Opus stream could not seek properly r   )r   r   r   r   r   rG   re   op_pcm_seekr   rd   r   r   )r!   r   r   r   s       r%   r9   zPyOggOpusSource.seek  s    SDN;;<<	y4#;;<<
&&t|AA 	KMIIIIJJJJJ	K 	Kr'   Nr   r@   r'   r%   r   r     s:        J J JK K K K Kr'   r   c                       e Zd Zej        rej        r	ej        rdndZej        rdndZ	ej
        r	ej        rdndZee	z   ez   Zd Zd	dZdS )
PyOggDecoder)z.oggr@   )z.flac)z.opusc                     t           j        S r   )r  extsrZ   s    r%   get_file_extensionsz PyOggDecoder.get_file_extensions  s      r'   Tc                 6   t           j                            |          \  }}|t          j        v rt
          }n;|t          j        v rt          }n%|t          j        v rt          }nt          d          |r |||          S t           |||                    S )NzCDecoder could not find a suitable source to use with this filetype.)osr   splitextr  vorbis_extsr   	flac_extsr   	opus_extsr   r   r   )r!   rr   r-   	streamingnameextsources          r%   decodezPyOggDecoder.decode  s    G$$X..	c,***&FFL***$FFL***$FF!"ghhh 	86(D)))x 6 6777r'   N)T)rQ   rR   rS   rG   PYOGG_OGG_AVAILPYOGG_VORBIS_AVAILPYOGG_VORBIS_FILE_AVAILr  PYOGG_FLAC_AVAILr  PYOGG_OPUS_AVAILPYOGG_OPUS_FILE_AVAILr  r  r  r  r@   r'   r%   r  r    s        $4{9Q{V[Vs{))y{K#4<

"I#4\9T\

Z\I"Y.D! ! !8 8 8 8 8 8r'   r  c                       t                      gS r   )r  r@   r'   r%   get_decodersr    s    NNr'   c                      g S r   r@   r@   r'   r%   get_encodersr    s    Ir'   )ErG   os.pathr
  r   abcr   ctypesr   r   r   r   r   r	   r
   r   r   r   r   r   pyglet.mediar   pyglet.media.codecsr   r   r   r   pyglet.utilr   r   r   r  r  r  r   r  r  r  ImportErrorr   FLAC__StreamDecoderr   FLAC__StreamDecoderInitStatusr   r   restyper   r   r   r   argtypesr&   FlacFileStreamr)   VorbisFileStreamrU   OpusFileStreamr`   ri   ry   r   r   r   r   r   r   r   r  r  r  r@   r'   r%   <module>r,     s#            ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` 7 7 7 7 7 7 7 7 7 7 ( ( ( ( ( ( R R R R R R R R R R R R 4 4 4 4 4 4 4 4 
(	)	)	 Q  W)A W%Jg WUVVV! NLMMM! Q%*E QOPPP 	L&+&>LGLGdL"L+0+FLPUPfL +J
K
KK 	 ? &/Yuggej>\6]6]_g%h%h" CH*BjEJ7?DKGEJLjDkDkDIJDnDLDLDLDLDIJDoDIJDrDIJDoDL	DNEJ7@H H H .?E*t t t t t t t t:
 
 
 
 
u5 
 
 

 
 
 
 
U1 
 
 
w w w w w w w w<&0 &0 &0 &0 &01 &0 &0 &0R    5   6A A A A AU1 A A Aj2 j2 j2 j2 j21 j2 j2 j2Z    /   >U U U U Uk U U UDY Y Y Y Y Y Y Y2K K K K Kk K K K$8 8 8 8 8< 8 8 84      r'   