Increased Minimum Audio Chunks, and No more missing the beginning of actual speech (pre-buffer captures it)

This commit is contained in:
rahulpscraftech360 2025-08-16 15:57:44 +05:30
parent eb5b2e4250
commit 547efab228
4 changed files with 32 additions and 1 deletions

View file

@ -127,6 +127,10 @@ class ConnectionHandler:
# Therefore, variables related to ASR need to be defined here as private variables of the connection.
self.asr_audio = []
self.asr_audio_queue = queue.Queue()
# Pre-buffer to store audio chunks before voice detection
# This helps capture the beginning of speech that might be missed
self.audio_pre_buffer = deque(maxlen=10) # Store last 10 chunks (600ms) before voice detection
# LLM related variables
self.llm_finish_task = True
@ -1071,6 +1075,9 @@ class ConnectionHandler:
self.client_audio_buffer = bytearray()
self.client_have_voice = False
self.client_voice_stop = False
# Clear pre-buffer when resetting VAD states
if hasattr(self, 'audio_pre_buffer'):
self.audio_pre_buffer.clear()
self.logger.bind(tag=TAG).debug("VAD states reset.")
def chat_and_close(self, text):

View file

@ -14,6 +14,17 @@ TAG = __name__
async def handleAudioMessage(conn, audio):
# Whether the current segment has someone speaking
have_voice = conn.vad.is_vad(conn, audio)
# Check if this is the initial connection period (ignore first 1 second of audio)
if have_voice and hasattr(conn, "initial_connection_handled") and not conn.initial_connection_handled:
current_time = asyncio.get_event_loop().time()
if current_time - conn.initial_connection_time < 1.0: # Ignore first 1 second
have_voice = False
conn.asr_audio.clear() # Clear any accumulated audio
return
else:
conn.initial_connection_handled = True
# If the device was just woken up, briefly ignore VAD detection
if have_voice and hasattr(conn, "just_woken_up") and conn.just_woken_up:
have_voice = False

View file

@ -38,6 +38,10 @@ async def handleTextMessage(conn, message):
f"Client audio capture mode: {conn.client_listen_mode}"
)
if msg_json["state"] == "start":
# Add initial connection flag to prevent false positive on first audio
if not hasattr(conn, "initial_connection_handled"):
conn.initial_connection_handled = False
conn.initial_connection_time = asyncio.get_event_loop().time()
conn.client_have_voice = True
conn.client_voice_stop = False
elif msg_json["state"] == "stop":

View file

@ -58,6 +58,15 @@ class ASRProviderBase(ABC):
else:
have_voice = conn.client_have_voice
# Always add audio to pre-buffer (rolling buffer)
conn.audio_pre_buffer.append(audio)
# If voice is detected and we're not already collecting
if have_voice and not conn.client_have_voice:
# Add pre-buffered audio to the beginning of ASR audio
conn.asr_audio = list(conn.audio_pre_buffer) + conn.asr_audio
conn.audio_pre_buffer.clear()
conn.asr_audio.append(audio)
if not have_voice and not conn.client_have_voice:
conn.asr_audio = conn.asr_audio[-10:]
@ -68,7 +77,7 @@ class ASRProviderBase(ABC):
conn.asr_audio.clear()
conn.reset_vad_states()
if len(asr_audio_task) > 15:
if len(asr_audio_task) > 20: # Increased from 15 to 20 chunks (1.2 seconds)
await self.handle_voice_stop(conn, asr_audio_task)
# Handle voice stop