Spaces:
Running
Running
Update templates/index.html
Browse files- templates/index.html +121 -49
templates/index.html
CHANGED
|
@@ -446,61 +446,133 @@
|
|
| 446 |
});
|
| 447 |
}
|
| 448 |
|
| 449 |
-
// Function to get the voice list and match the language
|
| 450 |
-
function getVoiceForLanguage(lang) {
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
}
|
| 455 |
|
| 456 |
-
// Text-to-Speech function
|
| 457 |
-
function speak(text, lang) {
|
| 458 |
-
|
| 459 |
-
|
| 460 |
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
|
| 468 |
|
| 469 |
-
|
| 470 |
-
|
| 471 |
-
|
| 472 |
-
}
|
| 473 |
|
| 474 |
-
// Language Handling Function
|
| 475 |
-
function speakResponse(text, selectedLanguage) {
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
|
| 481 |
-
|
| 482 |
-
|
| 483 |
-
|
| 484 |
-
|
| 485 |
-
|
| 486 |
-
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
| 493 |
|
| 494 |
-
|
| 495 |
-
}
|
| 496 |
|
| 497 |
-
// Ensure voices are loaded before running the TTS
|
| 498 |
-
window.speechSynthesis.onvoiceschanged = () => {
|
| 499 |
-
|
| 500 |
-
|
| 501 |
-
|
| 502 |
-
|
| 503 |
-
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 504 |
|
| 505 |
// Event listeners for buttons
|
| 506 |
sendBtn.addEventListener('click', () => {
|
|
|
|
| 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
|
| 464 |
+
// } else {
|
| 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;
|
| 480 |
+
// case 'telugu': lang = 'te-IN'; break;
|
| 481 |
+
// case 'marathi': lang = 'mr-IN'; break;
|
| 482 |
+
// case 'tamil': lang = 'ta-IN'; break;
|
| 483 |
+
// case 'gujarati': lang = 'gu-IN'; break;
|
| 484 |
+
// case 'kannada': lang = 'kn-IN'; break;
|
| 485 |
+
// case 'malayalam': lang = 'ml-IN'; break;
|
| 486 |
+
// case 'punjabi': lang = 'pa-IN'; break;
|
| 487 |
+
// case 'odia': lang = 'or-IN'; break;
|
| 488 |
+
// case 'urdu': lang = 'ur-IN'; break;
|
| 489 |
+
// case 'assamese': lang = 'as-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 |
+
// Function to initialize and fetch voices
|
| 505 |
+
function initializeVoices() {
|
| 506 |
+
return new Promise((resolve) => {
|
| 507 |
+
const voices = window.speechSynthesis.getVoices();
|
| 508 |
+
if (voices.length) {
|
| 509 |
+
resolve(voices);
|
| 510 |
+
} else {
|
| 511 |
+
window.speechSynthesis.onvoiceschanged = () => {
|
| 512 |
+
resolve(window.speechSynthesis.getVoices());
|
| 513 |
+
};
|
| 514 |
+
}
|
| 515 |
+
});
|
| 516 |
+
}
|
| 517 |
+
|
| 518 |
+
// Function to get the voice for a given language
|
| 519 |
+
function getVoiceForLanguage(lang, voices) {
|
| 520 |
+
const voice = voices.find(voice => voice.lang === lang);
|
| 521 |
+
if (voice) {
|
| 522 |
+
return voice;
|
| 523 |
+
}
|
| 524 |
+
console.warn(`No voice found for language: ${lang}.`);
|
| 525 |
+
return null;
|
| 526 |
+
}
|
| 527 |
+
|
| 528 |
+
// Text-to-Speech function
|
| 529 |
+
async function speak(text, lang) {
|
| 530 |
+
const voices = await initializeVoices();
|
| 531 |
+
const selectedVoice = getVoiceForLanguage(lang, voices);
|
| 532 |
+
|
| 533 |
+
const utterance = new SpeechSynthesisUtterance(text);
|
| 534 |
+
if (selectedVoice) {
|
| 535 |
+
utterance.voice = selectedVoice;
|
| 536 |
+
utterance.lang = selectedVoice.lang;
|
| 537 |
+
} else {
|
| 538 |
+
console.warn(`Falling back to default language.`);
|
| 539 |
+
utterance.lang = 'en-US';
|
| 540 |
+
}
|
| 541 |
+
|
| 542 |
+
utterance.pitch = 1; // Set pitch
|
| 543 |
+
utterance.rate = 1; // Set rate
|
| 544 |
+
window.speechSynthesis.speak(utterance); // Speak the text
|
| 545 |
+
}
|
| 546 |
+
|
| 547 |
+
// Language Handling Function
|
| 548 |
+
function speakResponse(text, selectedLanguage) {
|
| 549 |
+
let lang;
|
| 550 |
+
switch (selectedLanguage.toLowerCase()) {
|
| 551 |
+
case 'hindi': lang = 'hi-IN'; break;
|
| 552 |
+
case 'bengali': lang = 'bn-IN'; break;
|
| 553 |
+
case 'telugu': lang = 'te-IN'; break;
|
| 554 |
+
case 'marathi': lang = 'mr-IN'; break;
|
| 555 |
+
case 'tamil': lang = 'ta-IN'; break;
|
| 556 |
+
case 'gujarati': lang = 'gu-IN'; break;
|
| 557 |
+
case 'kannada': lang = 'kn-IN'; break;
|
| 558 |
+
case 'malayalam': lang = 'ml-IN'; break;
|
| 559 |
+
case 'punjabi': lang = 'pa-IN'; break;
|
| 560 |
+
case 'odia': lang = 'or-IN'; break;
|
| 561 |
+
case 'urdu': lang = 'ur-IN'; break;
|
| 562 |
+
case 'assamese': lang = 'as-IN'; break;
|
| 563 |
+
case 'sanskrit': lang = 'sa-IN'; break;
|
| 564 |
+
default: lang = 'en-US'; break; // English (default)
|
| 565 |
+
}
|
| 566 |
+
|
| 567 |
+
speak(text, lang); // Call the speak function with the determined language
|
| 568 |
+
}
|
| 569 |
+
|
| 570 |
+
// Test voices and log them
|
| 571 |
+
initializeVoices().then(voices => {
|
| 572 |
+
console.log("Available voices:");
|
| 573 |
+
voices.forEach(voice => console.log(`Voice: ${voice.name}, Language: ${voice.lang}`));
|
| 574 |
+
});
|
| 575 |
+
|
| 576 |
|
| 577 |
// Event listeners for buttons
|
| 578 |
sendBtn.addEventListener('click', () => {
|