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