o
    §¼–i®  ã                   @  sR   d dl mZ ddlmZmZ ddlmZ ddlm	Z	 ddl
mZ G dd„ dƒZd	S )
é    )Úannotationsé   )Ú	FfiClientÚ	FfiHandle)Úffi_pb2)Úget_address)Ú
AudioFramec                   @  sD   e Zd ZdZdddddœddd„Zddd„Zddd„Zddd„ZdS )ÚAudioProcessingModulez’
    Provides WebRTC audio processing capabilities including echo cancellation, noise suppression,
    high-pass filtering, and gain control.
    F)Úecho_cancellationÚnoise_suppressionÚhigh_pass_filterÚauto_gain_controlr
   Úboolr   r   r   ÚreturnÚNonec                C  sJ   t  ¡ }||j_||j_||j_||j_tj 	|¡}t
|jjjjƒ| _dS )aÏ  
        Initialize an AudioProcessingModule instance with the specified audio processing features.

        Args:
            echo_cancellation (bool, optional): Whether to enable echo cancellation.
            noise_suppression (bool, optional): Whether to enable noise suppression.
            high_pass_filter (bool, optional): Whether to enable a high-pass filter.
            auto_gain_control (bool, optional): Whether to enable auto gain control.
        N)Ú	proto_ffiÚ
FfiRequestÚnew_apmÚecho_canceller_enabledÚnoise_suppression_enabledÚhigh_pass_filter_enabledÚgain_controller_enabledr   ÚinstanceÚrequestr   ÚapmÚhandleÚidÚ_ffi_handle)Úselfr
   r   r   r   ÚreqÚresp© r!   úQ/var/www/html/livekit_bhavya/venv/lib/python3.10/site-packages/livekit/rtc/apm.pyÚ__init__   s   zAudioProcessingModule.__init__Údatar   c                 C  s”   t |jtƒst |jtƒr|jjrt|jƒ|_t ¡ }| jj	|j
_t|jƒ|j
_t|jƒ|j
_|j|j
_|j|j
_tj |¡}|j
jrHt|j
jƒ‚dS )a\  
        Process the provided audio frame using the configured audio processing features.

        The input audio frame is modified in-place (if applicable) by the underlying audio
        processing module (e.g., echo cancellation, noise suppression, etc.).

        Important:
            Audio frames must be exactly 10 ms in duration.
        N)Ú
isinstanceÚ_dataÚbytesÚ
memoryviewÚreadonlyÚ	bytearrayr   r   r   r   Úapm_process_streamÚ
apm_handler   Údata_ptrÚlenÚsizeÚsample_rateÚnum_channelsr   r   r   ÚerrorÚRuntimeError©r   r$   r   r    r!   r!   r"   Úprocess_stream)   s    

ÿÿ

ÿz$AudioProcessingModule.process_streamc                 C  s”   t |jtƒst |jtƒr|jjrt|jƒ|_t ¡ }| jj	|j
_t|jƒ|j
_t|jƒ|j
_|j|j
_|j|j
_tj |¡}|jjrHt|jjƒ‚dS )aÌ  
        Process the reverse audio frame (typically used for echo cancellation in a full-duplex setup).

        In an echo cancellation scenario, this method is used to process the "far-end" audio
        prior to mixing or feeding it into the echo canceller. Like `process_stream`, the
        input audio frame is modified in-place by the underlying processing module.

        Important:
            Audio frames must be exactly 10 ms in duration.
        N)r%   r&   r'   r(   r)   r*   r   r   r   r   Úapm_process_reverse_streamr,   r   r-   r.   r/   r0   r1   r   r   r   r+   r2   r3   r4   r!   r!   r"   Úprocess_reverse_streamD   s    
ÿÿ

ÿz,AudioProcessingModule.process_reverse_streamÚdelay_msÚintc                 C  s@   t  ¡ }| jj|j_||j_tj 	|¡}|jj
rt|jj
ƒ‚dS )a!  
        This must be called if and only if echo processing is enabled.

        Sets the `delay` in ms between `process_reverse_stream()` receiving a far-end
        frame and `process_stream()` receiving a near-end frame containing the
        corresponding echo. On the client-side this can be expressed as
            delay = (t_render - t_analyze) + (t_process - t_capture)
        where,
            - t_analyze is the time a frame is passed to `process_reverse_stream()` and
            t_render is the time the first sample of the same frame is rendered by
            the audio hardware.
            - t_capture is the time the first sample of a frame is captured by the
            audio hardware and t_process is the time the same frame is passed to
            `process_stream()`.
        N)r   r   r   r   Úapm_set_stream_delayr,   r8   r   r   r   r2   r3   )r   r8   r   r    r!   r!   r"   Úset_stream_delay_ms`   s   ÿz)AudioProcessingModule.set_stream_delay_msN)
r
   r   r   r   r   r   r   r   r   r   )r$   r   r   r   )r8   r9   r   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r#   r5   r7   r;   r!   r!   r!   r"   r	   	   s    ú

r	   N)Ú
__future__r   Ú_ffi_clientr   r   Ú_protor   r   Ú_utilsr   Úaudio_framer   r	   r!   r!   r!   r"   Ú<module>   s    