Preventing emitting the UserStartedSpeaking event multiple times.

This commit is contained in:
Filipi Fuchter
2025-04-17 17:21:29 -03:00
parent a80dc94e91
commit 6e06bf97c0
2 changed files with 16 additions and 6 deletions

View File

@@ -57,6 +57,10 @@ class BaseSmartTurn(ABC):
def set_sample_rate(self, sample_rate: int):
self._sample_rate = sample_rate
@property
def speech_triggered(self) -> bool:
return self._speech_triggered
def append_audio(self, buffer: bytes, is_speech: bool) -> EndOfTurnState:
# Convert raw audio to float32 format and append to the buffer
audio_int16 = np.frombuffer(buffer, dtype=np.int16)

View File

@@ -196,12 +196,18 @@ class BaseInputTransport(FrameProcessor):
and new_vad_state != VADState.STOPPING
):
frame = None
if new_vad_state == VADState.SPEAKING:
frame = UserStartedSpeakingFrame()
# TODO: need to double check if this is the expected behavior
# Not triggering the UserStoppedSpeakingFrame if the turn analyser is enabled
elif new_vad_state == VADState.QUIET and not self.end_of_turn_analyzer:
frame = UserStoppedSpeakingFrame()
# If the turn analyser is enabled, this will prevent:
# - Creating the UserStoppedSpeakingFrame
# - Creating the UserStartedSpeakingFrame multiple times
can_create_user_frames = (
self._params.end_of_turn_analyzer is None
or not self._params.end_of_turn_analyzer.speech_triggered
)
if can_create_user_frames:
if new_vad_state == VADState.SPEAKING:
frame = UserStartedSpeakingFrame()
elif new_vad_state == VADState.QUIET:
frame = UserStoppedSpeakingFrame()
if frame:
await self._handle_user_interruption(frame)