max words and sec
This commit is contained in:
@@ -866,6 +866,8 @@ speakBtn.addEventListener('click', () => {
|
||||
|
||||
// ── Speech recognition ────────────────────────────────────────────────────────
|
||||
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
|
||||
const MAX_RECORD_SECONDS = 10;
|
||||
const MAX_RECORD_WORDS = 30;
|
||||
|
||||
function initRecognition() {
|
||||
if (!SpeechRecognition) return null;
|
||||
@@ -895,6 +897,10 @@ function initRecognition() {
|
||||
interim = t;
|
||||
}
|
||||
}
|
||||
if (finalText.trim().split(/\s+/).length >= MAX_RECORD_WORDS) {
|
||||
stopRecording();
|
||||
return;
|
||||
}
|
||||
const display = finalText + (interim ? ' ' + interim : '');
|
||||
updateTranscriptBox(display, !!interim);
|
||||
state.transcript = finalText;
|
||||
@@ -903,8 +909,10 @@ function initRecognition() {
|
||||
|
||||
r.onend = () => {
|
||||
if (state.isRecording) {
|
||||
// auto-restart if not manually stopped
|
||||
try { r.start(); } catch (_) {}
|
||||
// create a fresh instance to avoid duplicate results on restart
|
||||
const fresh = initRecognition();
|
||||
recognition = fresh;
|
||||
try { fresh.start(); } catch (_) {}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -917,6 +925,8 @@ function initRecognition() {
|
||||
return r;
|
||||
}
|
||||
|
||||
let recordingTimer = null;
|
||||
|
||||
function startRecording() {
|
||||
if (!SpeechRecognition) {
|
||||
alert('Spracherkennung wird in diesem Browser nicht unterstützt. Bitte nutze Chrome oder Edge.');
|
||||
@@ -926,9 +936,13 @@ function startRecording() {
|
||||
recognition = initRecognition();
|
||||
state.isRecording = true;
|
||||
try { recognition.start(); } catch (_) {}
|
||||
|
||||
clearTimeout(recordingTimer);
|
||||
recordingTimer = setTimeout(() => stopRecording(), MAX_RECORD_SECONDS * 1000);
|
||||
}
|
||||
|
||||
function stopRecording() {
|
||||
clearTimeout(recordingTimer);
|
||||
state.isRecording = false;
|
||||
if (recognition) {
|
||||
try { recognition.stop(); } catch (_) {}
|
||||
|
||||
Reference in New Issue
Block a user