From 96819abcf2275a40984ab442cece106e51c86f3e Mon Sep 17 00:00:00 2001
From: ValueOn AG
Date: Sat, 7 Mar 2026 02:12:20 +0100
Subject: [PATCH] fix: Audio-Chunk Overlap - 1s Pre-Roll nach gesendeten Chunks
behalten, STT Wortanfang nicht abschneiden
Made-with: Cursor
---
src/bot/audioCaptureProcedure.ts | 16 ++++++++++------
1 file changed, 10 insertions(+), 6 deletions(-)
diff --git a/src/bot/audioCaptureProcedure.ts b/src/bot/audioCaptureProcedure.ts
index 293978d..3e9a670 100644
--- a/src/bot/audioCaptureProcedure.ts
+++ b/src/bot/audioCaptureProcedure.ts
@@ -23,7 +23,7 @@ class AudioCaptureProcessor extends AudioWorkletProcessor {
this.targetRate = opts.targetRate || 16000;
this.maxSamplesPerChunk = this.nativeRate * 8;
this.minRmsThreshold = 0.0003;
- this.preRollSamples = Math.ceil(this.nativeRate * 0.5);
+ this.preRollSamples = Math.ceil(this.nativeRate * 1.0);
this.minFlushSamples = Math.ceil(this.nativeRate * 0.5);
this.silenceFlushCallbacks = 6;
this.ratio = this.nativeRate / this.targetRate;
@@ -99,8 +99,10 @@ class AudioCaptureProcessor extends AudioWorkletProcessor {
return true;
}
- this.chunkBuffer = [];
- this.samplesCollected = 0;
+ const keep = Math.min(this.preRollSamples, merged.length);
+ const preRoll = merged.slice(merged.length - keep);
+ this.chunkBuffer = [preRoll];
+ this.samplesCollected = keep;
}
return true;
}
@@ -274,7 +276,7 @@ export class AudioCaptureProcedure {
const useScriptProcessor = () => {
const minRmsThreshold = 0.0003;
const maxSamplesPerChunk = nativeRate * 8;
- const preRollSamples = Math.ceil(nativeRate * 0.5);
+ const preRollSamples = Math.ceil(nativeRate * 1.0);
const minFlushSamples = Math.ceil(nativeRate * 0.5);
const silenceFlushCallbacks = 6;
const ratio = nativeRate / targetRate;
@@ -348,8 +350,10 @@ export class AudioCaptureProcedure {
samplesCollected = keep;
return;
}
- chunkBuffer = [];
- samplesCollected = 0;
+ const keep = Math.min(preRollSamples, merged.length);
+ const preRoll = merged.slice(merged.length - keep);
+ chunkBuffer = [preRoll];
+ samplesCollected = keep;
}
};