88
99class Aec :
1010 """
11- Acoustic Echo Cancellation (AEC) module for removing echo from audio data.
12- (It uses the AEC3 implementation from libwebrtc).
11+ Acoustic Echo Cancellation (AEC) module for removing echo from audio data using
12+ the AEC3 implementation from libwebrtc.
13+
14+ Note: Frames must be processed in chunks of exactly 10 ms.
15+ The length of the provided buffers must correspond to 10 ms of audio at the
16+ given sample rate and number of channels.
17+ Only the capture data buffer is modified (in-place). The render data
18+ buffer is used as a reference and is not changed by this process.
1319 """
1420
1521 def __init__ (
1622 self ,
1723 sample_rate : int ,
1824 num_channels : int ,
1925 ) -> None :
26+ """
27+ Initialize a new acoustic echo cancellation (AEC) instance.
28+
29+ Parameters:
30+ - sample_rate (int): The sample rate (in Hz) of the audio to process.
31+ - num_channels (int): The number of channels in the audio stream.
32+
33+ Ensure that each processed chunk represents exactly 10 ms of audio for
34+ reliable echo cancellation.
35+
36+ Example:
37+ aec = Aec(sample_rate=48000, num_channels=2)
38+ """
2039 self ._sample_rate = sample_rate
2140 self ._mum_channels = num_channels
2241
@@ -27,7 +46,37 @@ def __init__(
2746 resp = FfiClient .instance .request (req )
2847 self ._ffi_handle = FfiHandle (resp .new_aec .aec .handle .id )
2948
30- def cancel_echo (self , capture_data : bytearray | AudioFrame , render_data : bytearray | AudioFrame ) -> None :
49+ def cancel_echo (
50+ self ,
51+ capture_data : bytearray | AudioFrame ,
52+ render_data : bytearray | AudioFrame
53+ ) -> None :
54+ """
55+ Perform in-place echo cancellation on the capture data based on the render data.
56+
57+ This method processes two separate audio buffers:
58+ 1. capture_data (modified in-place)
59+ 2. render_data (read-only, not modified)
60+
61+ Parameters:
62+ - capture_data (bytearray | AudioFrame): The capture audio buffer or frame.
63+ This buffer will be edited in-place to remove the echo.
64+ - render_data (bytearray | AudioFrame): The render audio buffer or frame.
65+ This buffer is read-only and provides the reference signal used to
66+ remove echo from capture_data.
67+
68+ Important:
69+ - Each buffer must represent exactly 10 ms of audio (based on the
70+ sample rate and number of channels used to initialize Aec).
71+ - If you pass more or less than 10 ms, the behavior is undefined.
72+
73+ Raises:
74+ - Exception: If the AEC processing fails internally.
75+
76+ Example:
77+ # Assuming capture_data and render_data each hold exactly 10 ms of audio:
78+ aec.cancel_echo(capture_data=capture_data, render_data=render_data)
79+ """
3180 cap_data = capture_data if isinstance (capture_data , bytearray ) else capture_data .data .cast ("b" )
3281 rend_data = render_data if isinstance (render_data , bytearray ) else render_data .data .cast ("b" )
3382
@@ -42,3 +91,4 @@ def cancel_echo(self, capture_data: bytearray | AudioFrame, render_data: bytearr
4291
4392 if resp .cancel_echo .error :
4493 raise Exception (resp .cancel_echo .error )
94+
0 commit comments