fix: Improve ASR accuracy and eliminate phantom transcripts

- Fix audio length calculation to use WAV file metadata instead of PCM estimation
   - Implement comprehensive ASR logging system (CSV, JSON, TXT formats)
   - Disable audio file deletion for easier debugging
   - Add audio buffer clearing when VAD states reset
   - Implement echo suppression (100ms) to prevent speaker audio feedback
   - Add stale audio detection to skip leftover chunks after TTS
   - Clear ASR audio queue on VAD reset to prevent processing queued chunks
   - Add timestamp tracking for listen start events
   - Fix undefined send_initial_greeting function error                                                               │
│
   These changes eliminate phantom transcripts like 'SHE', 'UP' that occurred
   when echo from the device's speaker was picked up by the microphone.
This commit is contained in:
rahulpscraftech360 2025-08-18 13:09:04 +05:30
parent 0fe4c2d0b3
commit 3371c45608
5 changed files with 2245 additions and 2234 deletions

View file

@ -42,6 +42,8 @@ async def handleTextMessage(conn, message):
conn.reset_vad_states()
# Mark that we just started listening to prevent processing stale audio
conn.just_started_listening = True
# Set a timestamp for when we started listening
conn.listen_start_time = time.time()
# Add initial connection flag to prevent false positive on first audio
if not hasattr(conn, "initial_connection_handled"):
conn.initial_connection_handled = False

View file

@ -60,6 +60,14 @@ class ASRProviderBase(ABC):
else:
have_voice = conn.client_have_voice
# Echo suppression: Ignore audio for a brief period after starting to listen
if hasattr(conn, 'listen_start_time'):
time_since_listen_start = time.time() - conn.listen_start_time
if time_since_listen_start < 0.1: # Ignore first 300ms of audio (echo period)
if have_voice:
logger.bind(tag=TAG).debug(f"Ignoring potential echo audio ({time_since_listen_start:.2f}s after listen start)")
have_voice = False
# Always add audio to pre-buffer (rolling buffer)
conn.audio_pre_buffer.append(audio)
@ -330,10 +338,11 @@ class ASRProviderBase(ABC):
f.write(f"Transcript: {transcript}\n")
f.write(f"{'='*80}\n")
logger.bind(tag=TAG).info(
f"Logged ASR transcript to {log_dir} - File: {os.path.basename(file_path)}, "
f"Length: {audio_length_seconds:.2f}s, Transcript: {transcript[:50]}..."
)
# Logging disabled - comment out the log message
# logger.bind(tag=TAG).info(
# f"Logged ASR transcript to {log_dir} - File: {os.path.basename(file_path)}, "
# f"Length: {audio_length_seconds:.2f}s, Transcript: {transcript[:50]}..."
# )
except Exception as e:
logger.bind(tag=TAG).error(f"Failed to log audio transcript: {e}")

View file

@ -360,8 +360,8 @@ class ASRProvider(ASRProviderBase):
rate = wav_file.getframerate()
audio_length_seconds = frames / float(rate)
# Log the transcript information
self.log_audio_transcript(file_path, audio_length_seconds, text)
# Log the transcript information - TEMPORARILY DISABLED
# self.log_audio_transcript(file_path, audio_length_seconds, text)
return text, file_path
@ -375,10 +375,10 @@ class ASRProvider(ASRProviderBase):
logger.bind(tag=TAG).info(
f"Audio file preserved (deletion disabled): {file_path}")
# Commented out deletion code to preserve audio files
# try:
# os.remove(file_path)
# logger.bind(tag=TAG).debug(
# f"Deleted temporary audio file: {file_path}")
# except Exception as e:
# logger.bind(tag=TAG).error(
# f"File deletion failed: {file_path} | Error: {e}")
try:
os.remove(file_path)
logger.bind(tag=TAG).debug(
f"Deleted temporary audio file: {file_path}")
except Exception as e:
logger.bind(tag=TAG).error(
f"File deletion failed: {file_path} | Error: {e}")