Spaces:
Running
Running
Update templates/index.html
Browse files- templates/index.html +11 -13
templates/index.html
CHANGED
|
@@ -446,18 +446,18 @@
|
|
| 446 |
});
|
| 447 |
}
|
| 448 |
|
| 449 |
-
// Function to
|
| 450 |
function getVoiceForLanguage(lang) {
|
| 451 |
const voices = window.speechSynthesis.getVoices();
|
| 452 |
-
const matchingVoice = voices.find(voice => voice.lang
|
| 453 |
return matchingVoice || null; // Return first match or null if not found
|
| 454 |
}
|
| 455 |
-
|
| 456 |
// Text-to-Speech function
|
| 457 |
function speak(text, lang) {
|
| 458 |
const utterance = new SpeechSynthesisUtterance(text);
|
| 459 |
const selectedVoice = getVoiceForLanguage(lang);
|
| 460 |
-
|
| 461 |
if (selectedVoice) {
|
| 462 |
utterance.voice = selectedVoice; // Set the matching voice
|
| 463 |
utterance.lang = lang; // Set the language
|
|
@@ -465,17 +465,15 @@
|
|
| 465 |
console.warn(`No voice found for language: ${lang}. Falling back to default.`);
|
| 466 |
utterance.lang = 'en-US'; // Fallback to English
|
| 467 |
}
|
| 468 |
-
|
| 469 |
utterance.pitch = 1; // Set pitch
|
| 470 |
utterance.rate = 1; // Set rate
|
| 471 |
window.speechSynthesis.speak(utterance); // Speak the text
|
| 472 |
}
|
| 473 |
-
|
| 474 |
// Language Handling Function
|
| 475 |
function speakResponse(text, selectedLanguage) {
|
| 476 |
let lang;
|
| 477 |
-
|
| 478 |
-
// Map selected language to appropriate language code
|
| 479 |
switch (selectedLanguage) {
|
| 480 |
case 'hindi': lang = 'hi-IN'; break;
|
| 481 |
case 'bengali': lang = 'bn-IN'; break;
|
|
@@ -492,16 +490,16 @@
|
|
| 492 |
case 'sanskrit': lang = 'sa-IN'; break;
|
| 493 |
default: lang = 'en-US'; break; // English (default)
|
| 494 |
}
|
| 495 |
-
|
| 496 |
speak(text, lang); // Call the speak function with the determined language
|
| 497 |
}
|
| 498 |
-
|
| 499 |
// Ensure voices are loaded before running the TTS
|
| 500 |
window.speechSynthesis.onvoiceschanged = () => {
|
| 501 |
-
console.log("Voices loaded and ready to use.");
|
| 502 |
-
// Populate voices (optional, you may also load voices on demand)
|
| 503 |
const voices = window.speechSynthesis.getVoices();
|
| 504 |
-
voices.forEach(voice =>
|
|
|
|
|
|
|
| 505 |
};
|
| 506 |
|
| 507 |
// Event listeners for buttons
|
|
|
|
| 446 |
});
|
| 447 |
}
|
| 448 |
|
| 449 |
+
// Function to get the voice list and match the language
|
| 450 |
function getVoiceForLanguage(lang) {
|
| 451 |
const voices = window.speechSynthesis.getVoices();
|
| 452 |
+
const matchingVoice = voices.find(voice => voice.lang === lang); // Match exact language code
|
| 453 |
return matchingVoice || null; // Return first match or null if not found
|
| 454 |
}
|
| 455 |
+
|
| 456 |
// Text-to-Speech function
|
| 457 |
function speak(text, lang) {
|
| 458 |
const utterance = new SpeechSynthesisUtterance(text);
|
| 459 |
const selectedVoice = getVoiceForLanguage(lang);
|
| 460 |
+
|
| 461 |
if (selectedVoice) {
|
| 462 |
utterance.voice = selectedVoice; // Set the matching voice
|
| 463 |
utterance.lang = lang; // Set the language
|
|
|
|
| 465 |
console.warn(`No voice found for language: ${lang}. Falling back to default.`);
|
| 466 |
utterance.lang = 'en-US'; // Fallback to English
|
| 467 |
}
|
| 468 |
+
|
| 469 |
utterance.pitch = 1; // Set pitch
|
| 470 |
utterance.rate = 1; // Set rate
|
| 471 |
window.speechSynthesis.speak(utterance); // Speak the text
|
| 472 |
}
|
| 473 |
+
|
| 474 |
// Language Handling Function
|
| 475 |
function speakResponse(text, selectedLanguage) {
|
| 476 |
let lang;
|
|
|
|
|
|
|
| 477 |
switch (selectedLanguage) {
|
| 478 |
case 'hindi': lang = 'hi-IN'; break;
|
| 479 |
case 'bengali': lang = 'bn-IN'; break;
|
|
|
|
| 490 |
case 'sanskrit': lang = 'sa-IN'; break;
|
| 491 |
default: lang = 'en-US'; break; // English (default)
|
| 492 |
}
|
| 493 |
+
|
| 494 |
speak(text, lang); // Call the speak function with the determined language
|
| 495 |
}
|
| 496 |
+
|
| 497 |
// Ensure voices are loaded before running the TTS
|
| 498 |
window.speechSynthesis.onvoiceschanged = () => {
|
|
|
|
|
|
|
| 499 |
const voices = window.speechSynthesis.getVoices();
|
| 500 |
+
voices.forEach(voice => {
|
| 501 |
+
console.log(`Voice: ${voice.name}, Language: ${voice.lang}`);
|
| 502 |
+
});
|
| 503 |
};
|
| 504 |
|
| 505 |
// Event listeners for buttons
|