Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions livekit-agents/livekit/agents/llm/duplex_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,18 +143,6 @@ def update(self, frame: rtc.AudioFrame) -> bool:
samples = np.frombuffer(frame.data, dtype=np.int16).astype(np.float32)
rms = float(np.sqrt(np.mean(np.square(samples)))) / 32768.0 if samples.size else 0.0

if not self._open:
# only silence teaches the floor, and only a whole stretch of it
self._stretch_sum += rms * frame.duration
self._stretch_duration += frame.duration
if self._stretch_duration >= self._min_silence_duration:
mean = self._stretch_sum / self._stretch_duration
self._history.append((mean, self._stretch_duration))
self._history_duration += self._stretch_duration
self._stretch_sum = self._stretch_duration = 0.0
while self._history_duration > self._window and len(self._history) > 1:
self._history_duration -= self._history.popleft()[1]

if self._history:
floor = min(level for level, _ in self._history)
elif self._stretch_duration:
Expand All @@ -167,6 +155,17 @@ def update(self, frame: rtc.AudioFrame) -> bool:
if rms > floor * self._activation_ratio:
self._open = True
self._quiet = 0.0
self._stretch_sum = self._stretch_duration = 0.0

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 First duplex utterance cut short

When speech opens the gate before a full silence stretch, AdaptiveNoiseGate.update clears its only floor estimate. Each following frame then counts as quiet because the gate compares it with 1.8 times its own level. After 500 ms by default, the audio handler ends the burst even though speech continues.

Learn more

DuplexRealtimeAdapter.session uses the adaptive gate for models that do not supply their own. Before the gate records a complete silence stretch, update uses the pending stretch as a provisional floor. Opening clears that stretch, so floor selection and the closing check fall back to each speech frame's own RMS and count it as quiet. The audio handler closes the generation on the first false result and stops forwarding speech.

Example: Feed four 100 ms frames at RMS 0.001, then ten at RMS 0.3. The base gate forwards all ten speech frames. The new gate forwards only five and stays closed for the rest of the sustained sound.

Recommended fix: Save the floor used to open the gate and use it for deactivation while the burst is open. Keep clearing the pending stretch and do not commit the opening frame to silence history. Extend the regression coverage with sustained speech after less than 500 ms of initial silence and verify that actual silence still closes the burst.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the detailed finding. We're leaving this as-is for now and will address it if a maintainer requests it.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the finding. We're leaving this as-is for now and will address it only if a maintainer requests it.

else:
self._stretch_sum += rms * frame.duration
self._stretch_duration += frame.duration
if self._stretch_duration >= self._min_silence_duration:
mean = self._stretch_sum / self._stretch_duration
self._history.append((mean, self._stretch_duration))
self._history_duration += self._stretch_duration
self._stretch_sum = self._stretch_duration = 0.0
while self._history_duration > self._window and len(self._history) > 1:
self._history_duration -= self._history.popleft()[1]
elif rms < floor * self._deactivation_ratio:
self._quiet += frame.duration
if self._quiet >= self._min_silence_duration:
Expand Down
11 changes: 11 additions & 0 deletions tests/test_duplex_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,17 @@ def test_one_dropped_frame_does_not_define_the_floor() -> None:
assert not any(gate.update(_frame(0.002)) for _ in range(300))


def test_speech_that_opens_the_gate_does_not_pollute_the_next_floor() -> None:
gate = AdaptiveNoiseGate(min_silence_duration=0.5)
for _ in range(4):
assert not gate.update(_frame(0.001))
assert gate.update(_frame(0.3))
gate.deactivate()
for _ in range(3):
gate.update(_frame(0.001))
assert gate.update(_frame(0.004))


def test_the_floor_recovers_once_the_model_is_really_silent() -> None:
gate = AdaptiveNoiseGate()
# the session opens mid-speech, so the gate starts out with speech for a floor
Expand Down
Loading