
    /j                    X    S SK Jr  SSKJrJr  SSKJr  SSKJ	r	  SSK
Jr   " S S5      rg	)
    )annotations   )	FfiClient	FfiHandle)ffi_pb2)get_address)
AudioFramec                  `    \ rS rSrSrSSSSS.         SS jjrSS jrSS jrSS jrS	r	g
)AudioProcessingModule	   z
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               \   [         R                  " 5       nXR                  l        X%R                  l        X5R                  l        XER                  l        [        R                  R                  U5      n[        UR                  R                  R                  R                  5      U l        g)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          @/app/agent/.venv/lib/python3.13/site-packages/livekit/rtc/apm.py__init__AudioProcessingModule.__init__   ss    " ""$->*0A-/?,.?+!!))#.$T\\%5%5%<%<%?%?@    c                   [        UR                  [        5      (       d:  [        UR                  [        5      (       a5  UR                  R                  (       a  [        UR                  5      Ul        [        R                  " 5       nU R                  R                  UR                  l        [        UR                  5      UR                  l        [        UR                  5      UR                  l        UR                   UR                  l        UR"                  UR                  l        [$        R&                  R)                  U5      nUR                  R*                  (       a  [-        UR                  R*                  5      eg)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_stream$AudioProcessingModule.process_stream)   s     djj%((tzz:..4::3F3F"4::.DJ""$,0,<,<,C,C)*5djj*A'&)$**o#-1-=-=*.2.?.?+!!))#.""((t66<<== )r%   c                   [        UR                  [        5      (       d:  [        UR                  [        5      (       a5  UR                  R                  (       a  [        UR                  5      Ul        [        R                  " 5       nU R                  R                  UR                  l        [        UR                  5      UR                  l        [        UR                  5      UR                  l        UR                   UR                  l        UR"                  UR                  l        [$        R&                  R)                  U5      nUR                  R*                  (       a  [-        UR                  R*                  5      eg)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/   r0   r1   r2   r3   r   r   r   r4   r5   r6   s       r"   process_reverse_stream,AudioProcessingModule.process_reverse_streamD   s     djj%((tzz:..4::3F3F"4::.DJ""$484D4D4K4K&&12=djj2I&&/.1$**o&&+595E5E&&26:6G6G&&3!!))#.**00t>>DDEE 1r%   c                J   [         R                  " 5       nU R                  R                  UR                  l        XR                  l        [        R                  R                  U5      nUR                  R                  (       a  [        UR                  R                  5      eg)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   r4   r5   )r   r@   r    r!   s       r"   set_stream_delay_ms)AudioProcessingModule.set_stream_delay_ms`   sy      ""$.2.>.>.E.E  +,4  )!!))#.$$**t88>>?? +r%   )r   N)
r   boolr   rC   r   rC   r   rC   returnNone)r7   r	   rD   rE   )r@   intrD   rE   )
__name__
__module____qualname____firstlineno____doc__r#   r8   r<   rA   __static_attributes__ r%   r"   r   r   	   sc     #("'!&"'A  A  	A
 A  A 
A4>6F8@r%   r   N)
__future__r   _ffi_clientr   r   _protor   r   _utilsr   audio_framer	   r   rM   r%   r"   <module>rS      s"    " - (  #n@ n@r%   