Skip to content

Commit

Permalink
chore: Refacto silence detection func
Browse files Browse the repository at this point in the history
  • Loading branch information
clemlesne committed Dec 5, 2024
1 parent 9819c96 commit 8efc0bc
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions app/helpers/call_llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -574,18 +574,18 @@ async def _in_audio( # noqa: PLR0913
timeout_callback: Callable[[], Awaitable[None]],
) -> None:
clear_tts_task: asyncio.Task | None = None
flush_task: asyncio.Task | None = None
silence_task: asyncio.Task | None = None
vad = Vad(
# Aggressiveness mode (0, 1, 2, or 3)
# Sets the VAD operating mode. A more aggressive (higher mode) VAD is more restrictive in reporting speech. Put in other words the probability of being speech when the VAD returns 1 is increased with increasing mode. As a consequence also the missed detection rate goes up.
mode=3,
)

async def _flush_callback() -> None:
async def _silence_callback() -> None:
"""
Flush the audio buffer if no audio is detected for a while.
Flush the audio buffer if no audio is detected for a while and trigger the timeout if required.
"""
# Wait for the timeout
# Wait and flush the audio buffer
nonlocal clear_tts_task
timeout_ms = await vad_silence_timeout_ms()
await asyncio.sleep(timeout_ms / 1000)
Expand All @@ -595,7 +595,7 @@ async def _flush_callback() -> None:
logger.debug("Flushing audio buffer after %i ms", timeout_ms)
await response_callback()

# Wait for the timeout, if any
# Wait for silence and trigger timeout
timeout_sec = await phone_silence_timeout_sec()
while call.in_progress:
await asyncio.sleep(timeout_sec)
Expand Down Expand Up @@ -650,17 +650,17 @@ async def _clear_tts_callback() -> None:
):
in_empty = True
# Start timeout if not already started
if not flush_task:
flush_task = asyncio.create_task(_flush_callback())
if not silence_task:
silence_task = asyncio.create_task(_silence_callback())

if in_empty:
# Continue to the next audio packet
continue

# Voice detected, cancel the timeout if any
if flush_task:
flush_task.cancel()
flush_task = None
if silence_task:
silence_task.cancel()
silence_task = None

# Start the TTS clear task
if not clear_tts_task:
Expand Down

0 comments on commit 8efc0bc

Please sign in to comment.