
    qi                    X    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      Zy	)
    )annotations   )	FfiClient	FfiHandle)ffi_pb2)get_address)
AudioFramec                  N    e Zd ZdZddddd	 	 	 	 	 	 	 	 	 d	dZd
dZd
dZddZy)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_controlc               b   t        j                         }||j                  _        ||j                  _        ||j                  _        ||j                  _        t        j                  j                  |      }t        |j                  j                  j                  j                        | _        y)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resps          A/opt/pipecat/venv/lib/python3.12/site-packages/livekit/rtc/apm.py__init__zAudioProcessingModule.__init__   s{    " ""$->*0A-/?,.?+!!))#.$T\\%5%5%<%<%?%?@    c                   t        |j                  t              s0t        |j                  t              r0|j                  j                  rt        |j                        |_        t        j                         }| j                  j                  |j                  _        t        |j                        |j                  _        t        |j                        |j                  _        |j                   |j                  _        |j"                  |j                  _        t$        j&                  j)                  |      }|j                  j*                  rt-        |j                  j*                        y)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   datar   r    s       r!   process_streamz$AudioProcessingModule.process_stream)   s     djj%(tzz:.4::3F3F"4::.DJ""$,0,<,<,C,C)*5djj*A'&)$**o#-1-=-=*.2.?.?+!!))#.""((t66<<== )r#   c                   t        |j                  t              s0t        |j                  t              r0|j                  j                  rt        |j                        |_        t        j                         }| j                  j                  |j                  _        t        |j                        |j                  _        t        |j                        |j                  _        |j                   |j                  _        |j"                  |j                  _        t$        j&                  j)                  |      }|j*                  j,                  rt/        |j*                  j,                        y)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   s       r!   process_reverse_streamz,AudioProcessingModule.process_reverse_streamD   s     djj%(tzz:.4::3F3F"4::.DJ""$484D4D4K4K&&12=djj2I&&/.1$**o&&+595E5E&&26:6G6G&&3!!))#.""((t66<<== )r#   c                @   t        j                         }| j                  j                  |j                  _        ||j                  _        t        j                  j                  |      }|j                  j                  rt        |j                  j                        y)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,   delay_msr   r   r   r2   r3   )r   r<   r   r    s       r!   set_stream_delay_msz)AudioProcessingModule.set_stream_delay_ms`   s{      ""$.2.>.>.E.E  +,4  )!!))#.$$**t88>>?? +r#   N)
r   boolr   r>   r   r>   r   r>   returnNone)r5   r	   r?   r@   )r<   intr?   r@   )__name__
__module____qualname____doc__r"   r6   r9   r=    r#   r!   r   r   	   sb     #("'!&"'A  A  	A
 A  A 
A4>6>8@r#   r   N)
__future__r   _ffi_clientr   r   _protor   r   _utilsr   audio_framer	   r   rF   r#   r!   <module>rL      s"    " - (  #n@ n@r#   