Spaces:
Running
Running
Update bot_script.js
Browse files- bot_script.js +67 -22
bot_script.js
CHANGED
|
@@ -452,35 +452,80 @@ function increaseCount() {
|
|
| 452 |
}
|
| 453 |
|
| 454 |
// Function to speak the mantra with repeat functionality
|
| 455 |
-
function speakMantraSequential(mantraText, repeatCount, callback) {
|
| 456 |
-
const
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
|
|
|
|
|
|
| 469 |
|
| 470 |
-
|
| 471 |
-
|
| 472 |
-
|
| 473 |
-
|
| 474 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 475 |
}
|
| 476 |
};
|
| 477 |
|
| 478 |
-
//
|
| 479 |
-
|
| 480 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 481 |
}
|
| 482 |
|
| 483 |
|
|
|
|
| 484 |
// Function to handle mantra repetition based on user input
|
| 485 |
function repeatMantra() {
|
| 486 |
let mantraText = document.getElementById('mantraText').textContent;
|
|
|
|
| 452 |
}
|
| 453 |
|
| 454 |
// Function to speak the mantra with repeat functionality
|
| 455 |
+
async function speakMantraSequential(mantraText, repeatCount, callback) {
|
| 456 |
+
const serverUrl = "https://thevera-botveradb.hf.space/generate_sanskrit_audio";
|
| 457 |
+
|
| 458 |
+
// Helper function to play audio from the server
|
| 459 |
+
const playServerAudio = async (text, repeats) => {
|
| 460 |
+
try {
|
| 461 |
+
for (let i = 0; i < repeats; i++) {
|
| 462 |
+
const response = await fetch(serverUrl, {
|
| 463 |
+
method: "POST",
|
| 464 |
+
headers: { "Content-Type": "application/json" },
|
| 465 |
+
body: JSON.stringify({ text }),
|
| 466 |
+
});
|
| 467 |
+
|
| 468 |
+
if (!response.ok) {
|
| 469 |
+
throw new Error("Failed to fetch audio from server.");
|
| 470 |
+
}
|
| 471 |
|
| 472 |
+
const audioBlob = await response.blob();
|
| 473 |
+
const audioUrl = URL.createObjectURL(audioBlob);
|
| 474 |
+
const audio = new Audio(audioUrl);
|
| 475 |
+
|
| 476 |
+
await new Promise((resolve) => {
|
| 477 |
+
audio.onended = resolve;
|
| 478 |
+
audio.onerror = (error) => {
|
| 479 |
+
console.error("Audio playback error:", error);
|
| 480 |
+
resolve(); // Resolve even on error to continue
|
| 481 |
+
};
|
| 482 |
+
audio.play();
|
| 483 |
+
});
|
| 484 |
+
}
|
| 485 |
+
|
| 486 |
+
if (callback) callback(); // Callback after all repetitions are played
|
| 487 |
+
} catch (error) {
|
| 488 |
+
console.error("Error during server audio generation:", error);
|
| 489 |
+
if (callback) callback(); // Call callback even on error
|
| 490 |
}
|
| 491 |
};
|
| 492 |
|
| 493 |
+
// Check for sa-IN support in speech synthesis
|
| 494 |
+
const voices = speechSynthesis.getVoices();
|
| 495 |
+
const saInVoice = voices.find((voice) => voice.lang === "sa-IN");
|
| 496 |
+
|
| 497 |
+
if (saInVoice) {
|
| 498 |
+
// Browser supports Sanskrit, use SpeechSynthesis
|
| 499 |
+
const utterance = new SpeechSynthesisUtterance(mantraText);
|
| 500 |
+
utterance.voice = saInVoice;
|
| 501 |
+
utterance.lang = "sa-IN"; // Sanskrit
|
| 502 |
+
utterance.rate = 0.8; // Slow speech for mantras
|
| 503 |
+
utterance.pitch = 1.0; // Natural pitch
|
| 504 |
+
|
| 505 |
+
utterance.onend = function () {
|
| 506 |
+
if (repeatCount > 1) {
|
| 507 |
+
repeatCount--;
|
| 508 |
+
speechSynthesis.speak(utterance); // Repeat if more repetitions are needed
|
| 509 |
+
} else if (callback) {
|
| 510 |
+
callback(); // Callback when done
|
| 511 |
+
}
|
| 512 |
+
};
|
| 513 |
+
|
| 514 |
+
utterance.onerror = function (event) {
|
| 515 |
+
console.error("SpeechSynthesisUtterance error:", event.error);
|
| 516 |
+
playServerAudio(mantraText, repeatCount); // Fall back to server on error
|
| 517 |
+
};
|
| 518 |
+
|
| 519 |
+
window.speechSynthesis.cancel(); // Cancel any ongoing speech
|
| 520 |
+
speechSynthesis.speak(utterance);
|
| 521 |
+
} else {
|
| 522 |
+
// Fall back to server-side audio generation
|
| 523 |
+
playServerAudio(mantraText, repeatCount);
|
| 524 |
+
}
|
| 525 |
}
|
| 526 |
|
| 527 |
|
| 528 |
+
|
| 529 |
// Function to handle mantra repetition based on user input
|
| 530 |
function repeatMantra() {
|
| 531 |
let mantraText = document.getElementById('mantraText').textContent;
|