Browse Source

fix(worker): use non-overlapping transcription window to eliminate duplicate outputs

Changed from get_last(5s) every 2s (3s overlap causing duplicates) to
only transcribing new audio since last transcription, with 0.5s context
for ASR stitching accuracy.
wenhongquan 3 weeks ago
parent
commit
1059df76bc
1 changed files with 9 additions and 2 deletions
  1. 9 2
      asr_agent/worker.py

+ 9 - 2
asr_agent/worker.py

@@ -159,10 +159,15 @@ class ASRWorker:
 
         async def _transcribe():
             tick = 0
+            last_pos = 0
             while True:
                 await asyncio.sleep(TRANSCRIBE_INTERVAL)
                 tick += 1
-                audio = buf.get_last(BUFFER_DURATION)
+                total = len(buf.buffer)
+                if total - last_pos < 3200:
+                    continue
+                context = int(0.5 * 16000)
+                audio = buf.buffer[max(0, last_pos - context):]
                 if len(audio) <= 1600:
                     continue
 
@@ -175,9 +180,11 @@ class ASRWorker:
                     logger.exception("transcription failed")
                     continue
 
+                new_samples = total - last_pos
+                last_pos = total
                 raw = result.get("text", "").strip()
                 if tick % 5 == 0:
-                    logger.info(f"[{sid}] transcribe tick={tick} raw='{raw}' buffer_samples={len(audio)}")
+                    logger.info(f"[{sid}] transcribe tick={tick} raw='{raw}' new={new_samples}")
                 if not raw:
                     continue