o
    2j                      @   s>   d dl Z d dlmZ d dlmZ e je jG dd dZdS )    N)alloc_audio_frame)	err_checkc                   @   s   e Zd ZdZdd Zdd ZejdedB fdd	Z	ejddej
dejfddZejddej
dejfddZedd Zedd Zedd Zedd ZdS )	AudioFifoz7A simple audio sample FIFO (First In First Out) buffer.c                 C   st   z!d| j j d| j d| j d| j d| j dt| dd}W |S  ty9   d| j j dt| dd}Y |S 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result r   H/var/www/html/whisper/venv/lib/python3.10/site-packages/av/audio/fifo.py__repr__   s&   	zAudioFifo.__repr__c                 C   s   | j rt| j  d S d S )N)ptrlibav_audio_fifo_freer   r   r   r   __dealloc__   s   zAudioFifo.__dealloc__frameNc                 C   s  |du rt d|jjsdS | js]t | _| j| | j  |jjr=|jj	r=|jj
t|jj | _|  j|jj	  _nd| _tttj|jj|jj|jjd | _| js\tdn1|jj| jjjks|jj	| jjj	ks|jjr| jjjr|jj| jjjks|jj
| jjj
krtdtt}| jr|jjtjkrtt| j| j }|jj|krtd|jj|f tt| jttjtj |jj |jj |  j|jj7  _dS )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.z4Frame.pts (%d) != expected (%d); 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_ptsr   r   r   write   sd   


zAudioFifo.writer   Fr
   partialc                 C   s   | j sdS t| j }|dk rdS |p|}||k r |r|}ndS t }|| j |ttj	| jj j
ttj| jj j|d tt| j ttjtj |j j| | jrett| j| j |j _ntj|j _|  j|7  _|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_readr3   r4   r5   r%   uint64_tsamples_readr/   r0   )r   r
   r8   buffered_samplesr   r   r   r   readp   s@   

zAudioFifo.readc                 C   s0   g }	 | j ||d}|dur|| n	 |S q)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`.

        T)r8   N)rB   append)r   r
   r8   framesr   r   r   r   	read_many   s   zAudioFifo.read_manyc                 C      | 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   r   r   r   r      
   
zAudioFifo.formatc                 C   rF   )z'The :class:`.AudioLayout` of this FIFO.rG   z,.AudioFifo' object has no attribute 'layout')r   r   r	   r   r   r   r   r   r   r      rH   zAudioFifo.layoutc                 C   rF   )NrG   z1.AudioFifo' object has no attribute 'sample_rate')r   r   r	   r   r   r   r   r   r   r      s
   
zAudioFifo.sample_ratec                 C   s   | j r	t| j S dS )z4Number of audio samples (per channel) in the buffer.r   )r   r   r:   r   r   r   r   r
      s   zAudioFifo.samples)r   F)F)r	   
__module____qualname____doc__r   r   r'   ccall
AudioFramer7   intbintrB   rE   propertyr   r   r   r
   r   r   r   r   r      s$    S9


r   )r'   cython.cimports.av.audio.framer   cython.cimports.av.errorr   finalcclassr   r   r   r   r   <module>   s    