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