
    \j                     t    d dl Z d dlmZ d dlmZ e j
                  e j                   G d d                    Zy)    N)alloc_audio_frame)	err_checkc                   <   e Zd ZdZd Zd Zej                  dedz  fd       Z	ej                  ddej                  dej                  fd	       Zej                  ddej                  dej                  fd
       Zed        Zed        Zed        Zed        Zy)	AudioFifoz7A simple audio sample FIFO (First In First Out) buffer.c                 ,   	 d| j                   j                   d| j                   d| j                   d| j                   d| j
                   dt        |       dd}|S # t        $ r+ d| j                   j                   dt        |       dd}Y |S w xY w)	Nz<av. z samples of zhz z at 0xx>z, uninitialized, use fifo.write(frame), at 0x)	__class____name__samplessample_ratelayoutformatidAttributeError)selfresults     @/root/.hermes/venv/lib/python3.12/site-packages/av/audio/fifo.py__repr__zAudioFifo.__repr__   s    		t~~../ql##$C}Adkk]&DRSTUW    	t~~../ 0D!A'  	s   AA 0BBc                 \    | j                   r t        j                  | j                          y y )N)ptrlibav_audio_fifo_freer   s    r   __dealloc__zAudioFifo.__dealloc__   s     88""488,     frameNc           	      V   |t        d      |j                  j                  sy| j                  sqt               | _        | j                  j                  |       | j                  j                          |j                  j                  r{|j                  j                  re|j                  j                  t        |j                  j                        z  | _        | xj                  |j                  j                  z  c_        nd| _        t        j                  t        j                   t        j"                  |j                  j$                        |j&                  j(                  |j                  j                  dz        | _        | j                  s(t+        d      |j                  j$                  | j                  j                  j$                  k7  s|j                  j                  | j                  j                  j                  k7  s|j                  j                  r| j                  j                  j                  ry|j                  j                  | j                  j                  j                  k7  s7|j                  j                  | j                  j                  j                  k7  rt-        d      t        j.                  t0              }| j                  r|j                  j2                  t        j4                  k7  rpt        j                   t0        | j                  | j6                  z        }|j                  j2                  |k7  r&t-        d|j                  j2                   d| d	      t9        t        j;                  | j                  t        j                   t        j<                  t        j>                     |j                  j@                        |j                  j                               | xj6                  |j                  j                  z  c_        y)
a  write(frame)

        Push a frame of samples into the queue.

        :param AudioFrame frame: The frame of samples to push.

        The FIFO will remember the attributes from the first frame, and use those
        to populate all output frames.

        If there is a :attr:`~.Frame.pts` and :attr:`~.Frame.time_base` and
        :attr:`~.AudioFrame.sample_rate`, then the FIFO will assert that the incoming
        timestamps are continuous.

        Nz&AudioFifo must be given an AudioFrame.r      zCould not allocate AVAudioFifo.z*Frame does not match AudioFifo parameters.zFrame.pts (z) != expected (z); fix or set to None.)!	TypeErrorr   
nb_samplesr   template_copy_internal_attributes_init_user_attributes
_time_basenumr   denfloatpts_per_sampler   av_audio_fifo_alloccythoncastAVSampleFormatr   r   nb_channelsRuntimeError
ValueErrordeclareint64_tptsAV_NOPTS_VALUEsamples_writtenr   av_audio_fifo_writepointerp_voidextended_data)r   r   expected_ptss      r   writezAudioFifo.write   s   " =DEEyy##xx ./DMMM33E:MM//1 ##		(=(=&+&6&6&:&:U5CSCSCWCW=X&X###uyy'<'<<#&'#..C..		0@0@A((		$$DH 88"#DEE II 1 1 8 88yy$$(9(9(E(EE  $$MM,,00$$((DMM,D,D,H,HH''++t}}/G/G/K/KK IJJ ~~g.599==C4F4F#F!;;,,t/C/CCL yy}}, !%))--~ N* * 
 	##FNN6==9599;R;RS		$$	
 			 4 44r   r   partialc           	         | j                   syt        j                  | j                         }|dk  ry|xs |}||k  r|r|}nyt               }|j	                  | j
                         |j                  t        j                  t        j                  | j
                  j                   j                        t        j                  t        j                  | j
                  j                   j                        |d       t        t        j                  | j                   t        j                  t        j                  t        j                      |j                   j"                        |             | j$                  rAt        j                  t&        | j$                  | j(                  z        |j                   _        nt        j,                  |j                   _        | xj(                  |z  c_        |S )a  read(samples=0, partial=False)

        Read samples from the queue.

        :param int samples: The number of samples to pull; 0 gets all.
        :param bool partial: Allow returning less than requested.
        :returns: New :class:`AudioFrame` or ``None`` (if empty).

        If the incoming frames had valid a :attr:`~.Frame.time_base`,
        :attr:`~.AudioFrame.sample_rate` and :attr:`~.Frame.pts`, the returned frames
        will have accurate timing.

        N   )r   r   av_audio_fifo_sizer   r$   r#   _initr,   r-   r.   r   AVChannelLayout	ch_layoutr   av_audio_fifo_readr8   r9   r:   r*   uint64_tsamples_readr4   r5   )r   r   r=   buffered_samplesr   s        r   readzAudioFifo.readp   se     xx'*'='=dhh'Ga--g%*-/''6KK**DMM,=,=,D,DEKK++T]]->->-H-HI		
 	""FNN6==9599;R;RS	
 "KK$--0A0AAEIIM  ..EIIMW$r   c                 ^    g }	 | j                  ||      }||j                  |       n	 |S +)a  read_many(samples, partial=False)

        Read as many frames as we can.

        :param int samples: How large for the frames to be.
        :param bool partial: If we should return a partial frame.
        :returns: A ``list`` of :class:`AudioFrame`.

        )r=   )rH   append)r   r   r=   framesr   s        r   	read_manyzAudioFifo.read_many   s=     IIgwI7E e$ r   c                 l    | j                   st        dt         d      | j                  j                  S )z'The :class:`.AudioFormat` of this FIFO.'z,.AudioFifo' object has no attribute 'format')r   r   r   r#   r   r   s    r   r   zAudioFifo.format   7     xx H:IJ  }}###r   c                 l    | j                   st        dt         d      | j                  j                  S )z'The :class:`.AudioLayout` of this FIFO.rN   z,.AudioFifo' object has no attribute 'layout')r   r   r   r#   r   r   s    r   r   zAudioFifo.layout   rO   r   c                 l    | j                   st        dt         d      | j                  j                  S )NrN   z1.AudioFifo' object has no attribute 'sample_rate')r   r   r   r#   r   r   s    r   r   zAudioFifo.sample_rate   s5    xx H:NO  }}(((r   c                 \    | j                   rt        j                  | j                         S dS )z4Number of audio samples (per channel) in the buffer.r   )r   r   r@   r   s    r   r   zAudioFifo.samples   s$     4888s%%dhh/BBr   )r   F)F)r   
__module____qualname____doc__r   r   r,   ccall
AudioFramer<   intbintrH   rL   propertyr   r   r   r    r   r   r   r      s     B- \\Q5:, Q5 Q5f \\7FJJ 7V[[ 7 7r \\ fkk  , $ $ $ $ ) ) C Cr   r   )r,   cython.cimports.av.audio.framer   cython.cimports.av.errorr   finalcclassr   r[   r   r   <module>r`      s=     < . VC VC  VCr   