
    Hui                         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       Ze j                  defd       Zd	d	d
d
ddddddd
Ze j"                   G d de             Zy)    N)get_audio_format)get_audio_layout)
AudioPlane)	err_check)check_ndarrayreturnc                       t        t              S N)
AudioFrame_cinit_bypass_sentinel     @/opt/pipecat/venv/lib/python3.12/site-packages/av/audio/frame.pyalloc_audio_framer      s    ,--r   f8f4i2i4u1)
dbldblpfltfltps16s16ps32s32pu8u8pc            	       n   e Zd ZdZddZej                  dej                  dej                  dej                  dej                  fd       Zd Zej                  d	        Zd
 Zedd       Zed        Zed        Zed        Zej*                  d        Zed        Zej*                  d        Zd Zy)r   zA frame of audio.c                     |t         u ry t        |      }t        |      }| j                  |j                  |j
                  ||       y r
   )r   AudioFormatAudioLayout_init
sample_fmtlayout)selfformatr&   samplesalign	cy_format	cy_layouts          r   	__cinit__zAudioFrame.__cinit__"   s@    ++!,V!4	!,V!4	

9'')9)97EJr   r(   r&   
nb_samplesr*   c           
         || j                   _        || j                   _        || j                   _        | j	                          | j
                  j                  dk7  ro|rkt        j                  t        j                  | j                               t        t        j                  t        j                  | j
                  j                  |||            | _        t        j                   t        j"                  t$           t        j'                  | j                              | _        | j                  st)        d      t        t        j+                  | j                   | j
                  j                  t        j                   t        j,                  | j                   j                        | j                  | j                  |             y y y )Nr   z!cannot allocate AudioFrame buffer)ptrr.   r(   	ch_layout_init_user_attributesr&   nb_channelslibav_freepcythonaddress_bufferr   av_samples_get_buffer_sizeNULL_buffer_sizecastpointeruint8_t	av_mallocMemoryErroravcodec_fill_audio_frameAVSampleFormat)r'   r(   r&   r.   r*   s        r   r$   zAudioFrame._init*   s?    ) # 	""$;;""a'JLL56 !*..KK!8!8*fe!D
 ";;w't7H7H)IDL <<!"EFF ,,HHKK++KK 2 2DHHOODLL%%	# -7'r   c                 h    t         j                  t        j                  | j                               y r
   )r4   r5   r6   r7   r8   r'   s    r   __dealloc__zAudioFrame.__dealloc__U   s    V^^DLL12r   c                     t        | j                  j                        | _        t	        t        j                  t        j                  | j                  j                              | _	        y r
   )
r   r0   r1   r&   r   r6   r<   r4   rB   r(   rD   s    r   r2   z AudioFrame._init_user_attributesX   s=    &txx'9'9:&v{{33E3Etxx'WXr   c                     d| j                   j                   d| j                   d| j                   d| j                   d| j
                  j                   d| j                  j                   dt        |       ddS )	Nz<av.z pts=z, z samples at zHz, z at 0xx>)		__class____name__ptsr)   rater&   namer(   idrD   s    r   __repr__zAudioFrame.__repr__]   sx    4>>**+5
"T\\N K))D)9)9(:"T[[=M=M<NfUWX\U]^_T``ac	
r   c                    ddl }t        |t              r|n
t        |      }t        |t              r|n
t        |      }|j                  }	 |j                  t        |         }|j                  }t        | |d       |j                  rA| j                  d   |k7  rt        d| d| j                  d    d      | j                  d	   }n@| j                  d   d	k7  rt        d
| j                  d    d      | j                  d	   |z  }t        |||      }	t        |	j                        D ]  \  }
}|j!                  | |
ddf           |	S # t        $ r t        d| d      w xY w)z7
        Construct a frame from a numpy array.
        r   Nz)Conversion from numpy array with format `z` is not yet supported   z+Expected planar `array.shape[0]` to equal `z` but got ``   z7Expected packed `array.shape[0]` to equal `1` but got `)r(   r&   r)   )numpy
isinstancer"   r#   rN   dtypeformat_dtypesKeyError
ValueErrorr3   r   	is_planarshaper   	enumerateplanesupdate)arrayr(   r&   np	py_format	py_layoutrW   r3   r)   frameiplanes               r   from_ndarrayzAudioFrame.from_ndarrayc   sw   
 	(=F;vCV	(=F;vCV		HH]623E  ++eUA&{{1~, A+kZ_ZeZefgZhYiijk  kk!nG{{1~" MekkZ[nM]]^_  kk!n3G)IwO!%,,/ 	&HAuLLq!t%	&1  	;F8CYZ 	s   E E(c           	          d}| j                   j                  |   r|dz  }| j                   j                  |   rt        t        |      D cg c]  }t	        | |       c}      S c c}w )zW
        A tuple of :class:`~av.audio.plane.AudioPlane`.

        :type: tuple
        r   rT   )r0   extended_datatupleranger   )r'   plane_countre   s      r   r^   zAudioFrame.planes   s`     #$hh$$[11K hh$$[1 53EFajq)FGGFs   A'c                 .    | j                   j                  S )zL
        Number of audio samples (per channel).

        :type: int
        )r0   r.   rD   s    r   r)   zAudioFrame.samples   s     xx"""r   c                 .    | j                   j                  S )z[
        Sample rate of the audio data, in samples per second.

        :type: int
        r0   sample_raterD   s    r   rp   zAudioFrame.sample_rate   s     xx###r   c                 &    || j                   _        y r
   ro   r'   values     r   rp   zAudioFrame.sample_rate       $r   c                 .    | j                   j                  S )z%Another name for :attr:`sample_rate`.ro   rD   s    r   rM   zAudioFrame.rate   s     xx###r   c                 &    || j                   _        y r
   ro   rr   s     r   rM   zAudioFrame.rate   rt   r   c           
         ddl }	 |j                  t        | j                  j                           }| j                  j                  r| j                  }n#| j                  | j                  j                  z  }|j                  | j                  D cg c]  }|j                  |||       c}      S # t
        $ r$ t        d| j                  j                  d      w xY wc c}w )zVGet a numpy array of this frame.

        .. note:: Numpy must be installed.

        r   NzConversion from z( format to numpy array is not supported.)rW   count)rU   rW   rX   r(   rN   rY   rZ   r[   r)   r&   r3   vstackr^   
frombuffer)r'   ra   rW   rx   rH   s        r   
to_ndarrayzAudioFrame.to_ndarray   s     		HH]4;;+;+;<=E ;;  LLELL4;;#:#::EyyAEMAR]]1E]7M
 	
  	"4;;#3#3"66^_ 	 Ns   ,B3 C#3-C N)r   stereor   rT   )r   r|   )rK   
__module____qualname____doc__r-   r6   cfuncr4   rB   AVChannelLayoutuintr$   rE   r2   rP   staticmethodrg   propertyr^   r)   rp   setterrM   r{   r   r   r   r   r      s    K \\(""( ##( KK	(
 {{( (T3 \\Y Y
 % %N 
H 
H # # $ $ % % $ $ 
[[% %
r   r   )r6   cython.cimports.av.audio.formatr   cython.cimports.av.audio.layoutr   cython.cimports.av.audio.planer   cython.cimports.av.errorr   cython.cimports.av.utilsr   objectr   r   r   r   rX   cclassFramer   r   r   <module>r      s     < < 5 . 2  .: . .
 
 n
 n
 n
r   