diff --git a/Script_Speech_recognition/speech_recognition.js b/Script_Speech_recognition/speech_recognition.js index 49ed2b0..818aa7a 100644 --- a/Script_Speech_recognition/speech_recognition.js +++ b/Script_Speech_recognition/speech_recognition.js @@ -1,122 +1,71 @@ -document.addEventListener('DOMContentLoaded', function() { - const themeToggleBtn = document.getElementById('themeToggleBtn'); - const body = document.body; - const h1 = document.querySelector('h1'); - const textareas = document.querySelectorAll('textarea'); - const startBtn = document.getElementById('startBtn'); - const timerDisplay = document.getElementById('timerDisplay'); - const statusText = document.getElementById('statusText'); +document.addEventListener('DOMContentLoaded', function () { + const startBtn = document.getElementById('startBtn'); // Microphone button + const inputText = document.getElementById('inputText'); // Textarea + const statusText = document.getElementById('statusText'); // Optional status display + const timerDisplay = document.getElementById('timerDisplay'); // Timer display (if used) - themeToggleBtn.addEventListener('click', function() { - // Toggle dark mode classes on different elements - body.classList.toggle('dark-mode'); - h1.classList.toggle('dark-mode'); - textareas.forEach(textarea => textarea.classList.toggle('dark-mode')); - startBtn.classList.toggle('dark-mode'); - timerDisplay.classList.toggle('dark-mode'); - statusText.classList.toggle('dark-mode'); - - // Update the icon based on the mode - if (body.classList.contains('dark-mode')) { - themeToggleBtn.textContent = '🌜'; // Moon icon for dark mode - } else { - themeToggleBtn.textContent = '🌞'; // Sun icon for light mode - } - }); - - // Speech recognition functionality if ('SpeechRecognition' in window || 'webkitSpeechRecognition' in window) { const recognition = new (window.SpeechRecognition || window.webkitSpeechRecognition)(); - recognition.lang = 'sk-SK'; // Set language to Slovak + recognition.lang = 'sk-SK'; // Set language to Slovak recognition.interimResults = true; - recognition.maxAlternatives = 1; recognition.continuous = true; - let recognizing = false; // Recognition state - let startTime; // To store start time of recording - let timerInterval; // For the timer - let transcriptHistory = []; // Store transcripts + let recognizing = false; // Recognition state + let timerInterval; // Timer interval - // Start/stop speech recognition with button + // Start/Stop speech recognition startBtn.onclick = () => { if (!recognizing) { recognition.start(); recognizing = true; - console.log('Waiting for microphone permission...'); - startBtn.textContent = "🛑 Nahrávanie..."; // Button shows "Recording..." - statusText.textContent = "Nahrávanie prebieha..."; + startBtn.textContent = "🛑"; // Change icon to indicate recording + statusText && (statusText.textContent = "Nahrávanie prebieha..."); + startTimer(); } else { recognition.stop(); recognizing = false; - console.log('Speech recognition stopped'); - startBtn.textContent = "🎤"; // Button returns to original state - statusText.textContent = "Nahrávanie ukončené."; - stopTimer(); // Stop timer + startBtn.textContent = "🎤"; // Reset icon + statusText && (statusText.textContent = "Nahrávanie ukončené."); + stopTimer(); } }; - // Timer function + // Timer functionality function startTimer() { - startTime = Date.now(); + const startTime = Date.now(); timerInterval = setInterval(() => { - const elapsedTime = Math.floor((Date.now() - startTime) / 1000); - timerDisplay.textContent = `${elapsedTime}s`; // Display elapsed time - }, 1000); // Update every second + const elapsed = Math.floor((Date.now() - startTime) / 1000); + timerDisplay && (timerDisplay.textContent = `${elapsed}s`); + }, 1000); } function stopTimer() { clearInterval(timerInterval); - timerDisplay.textContent = ''; // Clear the timer display + timerDisplay && (timerDisplay.textContent = ""); } - // When recognition starts - recognition.onstart = function() { - console.log('Speech recognition started'); - startTimer(); // Start the timer - statusText.textContent = "Nahrávanie prebieha..."; // Recording status - }; - - // Handling the results of speech recognition - recognition.onresult = function(event) { - let interimTranscript = ''; // To display interim results - + // Speech recognition results + recognition.onresult = function (event) { + let interim = ""; for (let i = event.resultIndex; i < event.results.length; i++) { const transcript = event.results[i][0].transcript; - if (event.results[i].isFinal) { - transcriptHistory.push(transcript); - inputText.value += transcript + ' '; // Append final transcript to text area + inputText.value += transcript + " "; } else { - interimTranscript += transcript; // Update interim transcript + interim += transcript; } } - - // Show interim results combined with final results - inputText.value = transcriptHistory.join(' ') + ' ' + interimTranscript; - }; - - // When recognition ends - recognition.onend = function() { - 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 + recognition.onerror = function (event) { + console.error("Speech recognition error:", event.error); + recognizing = false; + startBtn.textContent = "🎤"; // Reset icon + statusText && (statusText.textContent = "Chyba pri nahrávaní. Skúste znova."); + stopTimer(); }; } else { - alert('Tento prehliadač nepodporuje rozpoznávanie reči.'); + alert("Tento prehliadač nepodporuje rozpoznávanie reči."); } });