Update Script_Speech_recognition/speech_recognition.js

This commit is contained in:
Mário Panenko 2024-10-21 10:04:20 +00:00
parent cf4d2f60ce
commit 956abd7348

View File

@ -1,102 +1,122 @@
document.addEventListener('DOMContentLoaded', function() { document.addEventListener('DOMContentLoaded', function() {
if ('SpeechRecognition' in window || 'webkitSpeechRecognition' in window) { const themeToggleBtn = document.getElementById('themeToggleBtn');
const recognition = new (window.SpeechRecognition || window.webkitSpeechRecognition)(); const body = document.body;
recognition.lang = 'sk-SK'; // Nastavenie jazyka const h1 = document.querySelector('h1');
recognition.interimResults = true; const textareas = document.querySelectorAll('textarea');
recognition.maxAlternatives = 1; const startBtn = document.getElementById('startBtn');
recognition.continuous = true; const timerDisplay = document.getElementById('timerDisplay');
const statusText = document.getElementById('statusText');
let recognizing = false; // Stavová premenná, či beží rozpoznávanie
let startTime; // Na ukladanie času začiatku nahrávania themeToggleBtn.addEventListener('click', function() {
let timerInterval; // Interval na časovač // Toggle dark mode classes on different elements
let transcriptHistory = []; // Na dočasné uloženie textových nahrávok body.classList.toggle('dark-mode');
h1.classList.toggle('dark-mode');
const startBtn = document.getElementById('startBtn'); textareas.forEach(textarea => textarea.classList.toggle('dark-mode'));
const statusText = document.getElementById('statusText'); startBtn.classList.toggle('dark-mode');
const timerDisplay = document.getElementById('timerDisplay'); // Element pre zobrazovanie času timerDisplay.classList.toggle('dark-mode');
const inputText = document.getElementById('inputText'); // Textové pole na výsledky statusText.classList.toggle('dark-mode');
// Funkcia pre spustenie časovača // Update the icon based on the mode
function startTimer() { if (body.classList.contains('dark-mode')) {
startTime = Date.now(); themeToggleBtn.textContent = '🌜'; // Moon icon for dark mode
timerInterval = setInterval(() => { } else {
const elapsedTime = Math.floor((Date.now() - startTime) / 1000); themeToggleBtn.textContent = '🌞'; // Sun icon for light mode
timerDisplay.textContent = `${elapsedTime}s`; // Zobrazenie počtu sekúnd }
}, 1000); // Aktualizácia každú sekundu });
}
// Speech recognition functionality
// Funkcia pre zastavenie časovača if ('SpeechRecognition' in window || 'webkitSpeechRecognition' in window) {
function stopTimer() { const recognition = new (window.SpeechRecognition || window.webkitSpeechRecognition)();
clearInterval(timerInterval); recognition.lang = 'sk-SK'; // Set language to Slovak
timerDisplay.textContent = ''; // Vymaže sa čas po zastavení nahrávania recognition.interimResults = true;
} recognition.maxAlternatives = 1;
recognition.continuous = true;
// Po stlačení tlačidla začne alebo zastaví rozpoznávanie reči
startBtn.onclick = () => { let recognizing = false; // Recognition state
if (!recognizing) { let startTime; // To store start time of recording
recognition.start(); let timerInterval; // For the timer
recognizing = true; let transcriptHistory = []; // Store transcripts
console.log('Čaká sa na povolenie mikrofónu...');
startBtn.textContent = "🛑 Nahrávanie..."; // Zmena textu na "Nahrávanie..." // Start/stop speech recognition with button
statusText.textContent = "Nahrávanie prebieha..."; startBtn.onclick = () => {
} else { if (!recognizing) {
recognition.stop(); recognition.start();
recognizing = false; recognizing = true;
console.log('Rozpoznávanie reči zastavené'); console.log('Waiting for microphone permission...');
startBtn.textContent = "🎤"; // Obnova textu na "Pripravený na nahrávanie" startBtn.textContent = "🛑 Nahrávanie..."; // Button shows "Recording..."
statusText.textContent = "Nahrávanie ukončené."; statusText.textContent = "Nahrávanie prebieha...";
stopTimer(); // Zastavenie časovača } else {
} recognition.stop();
}; recognizing = false;
console.log('Speech recognition stopped');
// Spustenie po povolení a začatí rozpoznávania startBtn.textContent = "🎤"; // Button returns to original state
recognition.onstart = function() { statusText.textContent = "Nahrávanie ukončené.";
console.log('Rozpoznávanie reči začalo'); stopTimer(); // Stop timer
startTimer(); // Spustenie časovača po začatí nahrávania }
statusText.textContent = "Nahrávanie prebieha..."; // Stav počas nahrávania };
};
// Timer function
// Výsledok rozpoznávania function startTimer() {
recognition.onresult = function(event) { startTime = Date.now();
let interimTranscript = ''; // Zmeníme na zobrazenie medzivýsledkov timerInterval = setInterval(() => {
const elapsedTime = Math.floor((Date.now() - startTime) / 1000);
for (let i = event.resultIndex; i < event.results.length; i++) { timerDisplay.textContent = `${elapsedTime}s`; // Display elapsed time
const transcript = event.results[i][0].transcript; }, 1000); // Update every second
}
// Ak je to medzivýsledok, zobraz ho, ale neulož
if (event.results[i].isFinal) { function stopTimer() {
transcriptHistory.push(transcript); clearInterval(timerInterval);
inputText.value += transcript + ' '; // Pridanie rozpoznaného textu timerDisplay.textContent = ''; // Clear the timer display
} else { }
interimTranscript += transcript;
} // When recognition starts
} recognition.onstart = function() {
console.log('Speech recognition started');
// Zobrazenie medzivýsledkov, kým nie sú konečné startTimer(); // Start the timer
inputText.value = transcriptHistory.join(' ') + ' ' + interimTranscript; statusText.textContent = "Nahrávanie prebieha..."; // Recording status
}; };
recognition.onend = function() { // Handling the results of speech recognition
// Ak bol mikrofón manuálne zastavený recognition.onresult = function(event) {
if (!recognizing) { let interimTranscript = ''; // To display interim results
recognizing = false;
console.log('Rozpoznávanie reči ukončené'); for (let i = event.resultIndex; i < event.results.length; i++) {
stopTimer(); const transcript = event.results[i][0].transcript;
startBtn.textContent = "🎤"; // Obnova textu
statusText.textContent = "Nahrávanie ukončené."; // Zmena stavu po ukončení if (event.results[i].isFinal) {
} else { transcriptHistory.push(transcript);
// Automaticky znovu spustí rozpoznávanie, ak nebolo zastavené manuálne inputText.value += transcript + ' '; // Append final transcript to text area
recognition.start(); } else {
} interimTranscript += transcript; // Update interim transcript
} }
recognition.onerror = function(event) { }
console.error('Chyba pri rozpoznávaní reči:', event.error);
recognizing = false; // V prípade chyby, zastav rozpoznávanie // Show interim results combined with final results
stopTimer(); // Zastav časovač aj pri chybe inputText.value = transcriptHistory.join(' ') + ' ' + interimTranscript;
startBtn.textContent = "🎤"; // Obnova textu };
statusText.textContent = "Chyba pri nahrávaní. Skúste znova."; // Zobrazenie chyby
}; // When recognition ends
} else { recognition.onend = function() {
alert('Tento prehliadač nepodporuje rozpoznávanie reči.'); if (!recognizing) {
} recognizing = false;
}); console.log('Speech recognition ended');
stopTimer();
startBtn.textContent = "🎤"; // Restore button to original state
statusText.textContent = "Nahrávanie ukončené."; // Update status text
} else {
recognition.start(); // Auto-restart recognition if not manually stopped
}
};
// Handle recognition errors
recognition.onerror = function(event) {
console.error('Speech recognition error:', event.error);
recognizing = false; // Stop recognition on error
stopTimer(); // Stop timer
startBtn.textContent = "🎤"; // Restore button to original state
statusText.textContent = "Chyba pri nahrávaní. Skúste znova."; // Display error status
};
} else {
alert('Tento prehliadač nepodporuje rozpoznávanie reči.');
}
});