o
    ]i                     @   s   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d	Zd
d
ddddddddd
Ze jG dd deZdS )    N)get_audio_format)get_audio_layout
AudioPlane)	err_check)check_ndarrayreturnc                   C   s   t tS N)
AudioFrame_cinit_bypass_sentinel r   r   B/home/ubuntu/.local/lib/python3.10/site-packages/av/audio/frame.pyalloc_audio_frame   s   r   f8f4i2i4u1)
dbldblpfltfltps16s16ps32s32pu8u8pc                	   @   s   e Zd ZdZd#ddZejdejd	ej	d
ej
dej
fddZdd Zejdd Zdd Zed$ddZedd Zedd Zedd Zejdd Zedd Zejdd Zd d! Zd"S )%r
   zA frame of audio.r   stereor      c                 C   s4   |t u rd S t|}t|}| |j|j|| d S r	   )r   AudioFormatAudioLayout_init
sample_fmtlayout)selfformatr$   samplesalign	cy_format	cy_layoutr   r   r   	__cinit__"   s
   zAudioFrame.__cinit__r&   r$   
nb_samplesr(   c              	   C   s   || j _|| j _|| j _|   | jjdkr_|ratt	
| j ttt	j| jj|||| _t	t	jt t| j| _| jsEtdtt| j | jjt	tj| j j| j| j| d S d S d S )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(   r   r   r   r"   *   s6   zAudioFrame._initc                 C   s   t t| j d S r	   )r1   r2   r3   r4   r5   r%   r   r   r   __dealloc__U   s   zAudioFrame.__dealloc__c                 C   s*   t | jj| _tttj| jj	| _	d S r	   )
r   r-   r.   r$   r   r3   r9   r1   r?   r&   r@   r   r   r   r/   X   s   z AudioFrame._init_user_attributesc                 C   sH   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&   idr@   r   r   r   __repr__]   s   zAudioFrame.__repr__c                 C   s(  ddl }t|tr|nt|}t|tr|nt|}|j}z	|t| }W n ty5   td| dw |j	}t
| |d |jr]| jd |krWtd| d| jd  d| jd	 }n| jd d	krotd
| jd  d| jd	 | }t|||d}	t|	jD ]\}
}|| |
ddf  q|	S )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 ``r   z7Expected packed `array.shape[0]` to equal `1` but got `)r&   r$   r'   )numpy
isinstancer    r!   rH   dtypeformat_dtypesKeyError
ValueErrorr0   r   	is_planarshaper
   	enumerateplanesupdate)arrayr&   r$   np	py_format	py_layoutrO   r0   r'   frameiplaner   r   r   from_ndarrayc   s8   
zAudioFrame.from_ndarrayc                    s>   d} j j| r|d7 } j j| st fddt|D S )zW
        A tuple of :class:`~av.audio.plane.AudioPlane`.

        :type: tuple
        r   r   c                    s   g | ]}t  |qS r   r   ).0r]   r@   r   r   
<listcomp>   s    z%AudioFrame.planes.<locals>.<listcomp>)r-   extended_datatuplerange)r%   plane_countr   r@   r   rV      s
   zAudioFrame.planesc                 C      | j jS )zL
        Number of audio samples (per channel).

        :type: int
        )r-   r,   r@   r   r   r   r'         zAudioFrame.samplesc                 C   rf   )z[
        Sample rate of the audio data, in samples per second.

        :type: int
        r-   sample_rater@   r   r   r   ri      rg   zAudioFrame.sample_ratec                 C      || j _d S r	   rh   r%   valuer   r   r   ri         c                 C   rf   )z%Another name for :attr:`sample_rate`.rh   r@   r   r   r   rG      s   zAudioFrame.ratec                 C   rj   r	   rh   rk   r   r   r   rG      rm   c                    s~   ddl zt| jj W n ty    td| jjdw | jjr)| j n| j| j	j
   fdd| jD S )zVGet a numpy array of this frame.

        .. note:: Numpy must be installed.

        r   NzConversion from z( format to numpy array is not supported.c                    s   g | ]
}j | d qS ))rO   count)
frombuffer)r`   rB   rn   rO   rY   r   r   ra      s    z)AudioFrame.to_ndarray.<locals>.<listcomp>)rM   rO   rP   r&   rH   rQ   rR   rS   r'   r$   r0   vstackrV   r@   r   rp   r   
to_ndarray   s   zAudioFrame.to_ndarrayN)r   r   r   r   )r   r   )rE   
__module____qualname____doc__r+   r3   cfuncr1   r?   AVChannelLayoutuintr"   rA   r/   rJ   staticmethodr_   propertyrV   r'   ri   setterrG   rr   r   r   r   r   r
      s@    
*
'





r
   )r3   cython.cimports.av.audio.formatr   cython.cimports.av.audio.layoutr   cython.cimports.av.audio.planer   cython.cimports.av.errorr   cython.cimports.av.utilsr   objectr   rv   r
   r   rP   cclassFramer   r   r   r   <module>   s,    