Skip to content

Commit 8ef6ea6

Browse files
committed
docs
1 parent ed0f889 commit 8ef6ea6

File tree

2 files changed

+37
-10
lines changed

2 files changed

+37
-10
lines changed

livekit-rtc/livekit/rtc/apm.py

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77

88

99
class AudioProcessingModule:
10+
"""
11+
Provides WebRTC audio processing capabilities including echo cancellation, noise suppression,
12+
high-pass filtering, and gain control.
13+
"""
14+
1015
def __init__(
1116
self,
1217
*,
@@ -15,6 +20,15 @@ def __init__(
1520
high_pass_filter_enabled: bool = False,
1621
gain_controller_enabled: bool = False,
1722
) -> None:
23+
"""
24+
Initialize an AudioProcessingModule instance with the specified audio processing features.
25+
26+
Args:
27+
echo_canceller_enabled (bool, optional): Whether to enable echo cancellation.
28+
noise_suppression_enabled (bool, optional): Whether to enable noise suppression.
29+
high_pass_filter_enabled (bool, optional): Whether to enable a high-pass filter.
30+
gain_controller_enabled (bool, optional): Whether to enable a gain controller.
31+
"""
1832
req = proto_ffi.FfiRequest()
1933
req.new_apm.echo_canceller_enabled = echo_canceller_enabled
2034
req.new_apm.noise_suppression_enabled = noise_suppression_enabled
@@ -25,6 +39,15 @@ def __init__(
2539
self._ffi_handle = FfiHandle(resp.new_apm.apm.handle.id)
2640

2741
def process_stream(self, data: AudioFrame) -> None:
42+
"""
43+
Process the provided audio frame using the configured audio processing features.
44+
45+
The input audio frame is modified in-place (if applicable) by the underlying audio
46+
processing module (e.g., echo cancellation, noise suppression, etc.).
47+
48+
Important:
49+
Audio frames must be exactly 10 ms in duration.
50+
"""
2851
bdata = data.data.cast("b")
2952

3053
req = proto_ffi.FfiRequest()
@@ -37,9 +60,19 @@ def process_stream(self, data: AudioFrame) -> None:
3760
resp = FfiClient.instance.request(req)
3861

3962
if resp.apm_process_stream.error:
40-
raise Exception(resp.apm_process_stream.error)
63+
raise RuntimeError(resp.apm_process_stream.error)
4164

4265
def process_reverse_stream(self, data: AudioFrame) -> None:
66+
"""
67+
Process the reverse audio frame (typically used for echo cancellation in a full-duplex setup).
68+
69+
In an echo cancellation scenario, this method is used to process the "far-end" audio
70+
prior to mixing or feeding it into the echo canceller. Like `process_stream`, the
71+
input audio frame is modified in-place by the underlying processing module.
72+
73+
Important:
74+
Audio frames must be exactly 10 ms in duration.
75+
"""
4376
bdata = data.data.cast("b")
4477

4578
req = proto_ffi.FfiRequest()
@@ -52,4 +85,4 @@ def process_reverse_stream(self, data: AudioFrame) -> None:
5285
resp = FfiClient.instance.request(req)
5386

5487
if resp.apm_process_stream.error:
55-
raise Exception(resp.apm_process_stream.error)
88+
raise RuntimeError(resp.apm_process_stream.error)

livekit-rtc/tests/test_apm.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ def test_audio_processing():
3939
wf_out.setsampwidth(sampwidth)
4040
wf_out.setframerate(sample_rate)
4141

42-
bytes_per_sample = sampwidth
43-
4442
while True:
4543
capture_bytes = wf_in_cap.readframes(frames_per_chunk)
4644
render_bytes = wf_in_rend.readframes(frames_per_chunk)
@@ -54,13 +52,9 @@ def test_audio_processing():
5452

5553
# Pad if necessary
5654
if len(capture_data) < frames_per_chunk:
57-
capture_data = np.pad(
58-
capture_data, (0, frames_per_chunk - len(capture_data))
59-
)
55+
capture_data = np.pad(capture_data, (0, frames_per_chunk - len(capture_data)))
6056
if len(render_data) < frames_per_chunk:
61-
render_data = np.pad(
62-
render_data, (0, frames_per_chunk - len(render_data))
63-
)
57+
render_data = np.pad(render_data, (0, frames_per_chunk - len(render_data)))
6458

6559
capture_frame = AudioFrame(
6660
data=capture_data.tobytes(),

0 commit comments

Comments
 (0)