|
<html>
|
|
<head>
|
|
</head>
|
|
<body>
|
|
<div id="messages"></div>
|
|
<script>
|
|
|
|
function speak(text) {
|
|
// Create a new SpeechSynthesisUtterance instance
|
|
const utterance = new SpeechSynthesisUtterance(text);
|
|
|
|
// Set voice properties (optional)
|
|
utterance.lang = 'en-US'; // Set language (adjust as needed)
|
|
utterance.pitch = 1; // Adjust pitch (0 to 2)
|
|
utterance.rate = 1; // Adjust rate/speed (0.1 to 10)
|
|
utterance.volume = 1; // Adjust volume (0 to 1)
|
|
|
|
// Speak the text
|
|
window.speechSynthesis.speak(utterance);
|
|
}
|
|
let recognition;
|
|
|
|
function startSpeechRecognition() {
|
|
// Check if the Web Speech API is supported
|
|
if (!('SpeechRecognition' in window || 'webkitSpeechRecognition' in window)) {
|
|
console.error('Web Speech API is not supported in this browser.');
|
|
return;
|
|
}
|
|
|
|
// Initialize SpeechRecognition
|
|
recognition = new (window.SpeechRecognition || window.webkitSpeechRecognition)();
|
|
|
|
// Configure SpeechRecognition
|
|
recognition.lang = 'en-US'; // Set the desired language
|
|
recognition.continuous = true; // Allow continuous recognition
|
|
recognition.interimResults = true; // Capture interim results
|
|
|
|
// Event listener for speech recognition results
|
|
recognition.onresult = (event) => {
|
|
let transcript = '';
|
|
for (let i = event.resultIndex; i < event.results.length; i++) {
|
|
transcript += event.results[i][0].transcript;
|
|
|
|
|
|
}
|
|
|
|
console.log('Recognized Speech:', transcript);
|
|
};
|
|
|
|
// Handle errors
|
|
recognition.onerror = (event) => {
|
|
console.error('Speech Recognition Error:', event.error);
|
|
};
|
|
|
|
// Automatically restart recognition if it stops
|
|
recognition.onend = () => {
|
|
console.log('Speech recognition stopped. Restarting...');
|
|
recognition.start();
|
|
};
|
|
|
|
// Start speech recognition
|
|
recognition.start();
|
|
console.log('Speech recognition started.');
|
|
}
|
|
|
|
// Start the speech recognition loop
|
|
startSpeechRecognition();
|
|
|
|
|
|
|
|
</script>
|
|
</body?>
|
|
</html>
|