Skip to content

Commit 3619c6b

Browse files
Bikram GoleBikram Gole
authored andcommitted
Reduce name speak delay and use en-IN
1 parent ac9557b commit 3619c6b

1 file changed

Lines changed: 48 additions & 19 deletions

File tree

script.js

Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1687,9 +1687,16 @@ function initNamePronounce() {
16871687
const synth = window.speechSynthesis;
16881688
let currentUtterance = null;
16891689
let cachedVoices = [];
1690+
let pendingSpeak = false;
1691+
let pendingRetries = 0;
1692+
let pendingTimer = null;
1693+
const MAX_SPEAK_RETRIES = 4;
16901694

16911695
const syncVoices = () => {
16921696
cachedVoices = synth.getVoices?.() || [];
1697+
if (pendingSpeak && cachedVoices.length) {
1698+
attemptSpeak(true);
1699+
}
16931700
};
16941701

16951702
syncVoices();
@@ -1727,7 +1734,26 @@ function initNamePronounce() {
17271734
setSpeakingState(false);
17281735
};
17291736

1730-
const speakName = () => {
1737+
const clearPending = () => {
1738+
pendingSpeak = false;
1739+
pendingRetries = 0;
1740+
if (pendingTimer) {
1741+
window.clearTimeout(pendingTimer);
1742+
pendingTimer = null;
1743+
}
1744+
};
1745+
1746+
const scheduleRetry = () => {
1747+
if (!pendingSpeak || pendingRetries >= MAX_SPEAK_RETRIES) return;
1748+
if (pendingTimer) return;
1749+
pendingTimer = window.setTimeout(() => {
1750+
pendingTimer = null;
1751+
if (!pendingSpeak) return;
1752+
attemptSpeak(true);
1753+
}, 420);
1754+
};
1755+
1756+
const attemptSpeak = (isRetry = false) => {
17311757
stopSpeech();
17321758
if (synth.paused) {
17331759
try {
@@ -1738,29 +1764,32 @@ function initNamePronounce() {
17381764
}
17391765
const text = heroName?.dataset.name || heroName?.textContent || "Bikram Gole";
17401766
const voice = pickVoice();
1741-
const buildUtterance = () => {
1742-
const utterance = new SpeechSynthesisUtterance(text);
1743-
utterance.lang = voice?.lang || "en-US";
1744-
utterance.rate = 1.06;
1745-
utterance.pitch = 0.96;
1746-
utterance.volume = 1;
1747-
if (voice) utterance.voice = voice;
1748-
utterance.onstart = () => setSpeakingState(true);
1749-
utterance.onend = () => setSpeakingState(false);
1750-
utterance.onerror = () => setSpeakingState(false);
1751-
return utterance;
1767+
const utterance = new SpeechSynthesisUtterance(text);
1768+
utterance.lang = voice?.lang || "en-IN";
1769+
utterance.rate = 0.9;
1770+
utterance.pitch = 0.92;
1771+
utterance.volume = 1;
1772+
if (voice) utterance.voice = voice;
1773+
utterance.onstart = () => {
1774+
clearPending();
1775+
setSpeakingState(true);
17521776
};
1753-
1754-
const utterance = buildUtterance();
1777+
utterance.onend = () => setSpeakingState(false);
1778+
utterance.onerror = () => setSpeakingState(false);
17551779
currentUtterance = utterance;
17561780
synth.speak(utterance);
17571781

1782+
pendingSpeak = true;
1783+
if (!isRetry) pendingRetries = 0;
17581784
window.setTimeout(() => {
17591785
if (synth.speaking || synth.pending) return;
1760-
const retry = buildUtterance();
1761-
currentUtterance = retry;
1762-
synth.speak(retry);
1763-
}, 240);
1786+
if (pendingRetries < MAX_SPEAK_RETRIES) {
1787+
pendingRetries += 1;
1788+
scheduleRetry();
1789+
} else {
1790+
clearPending();
1791+
}
1792+
}, 260);
17641793
};
17651794

17661795
nameSpeakBtn.addEventListener("click", () => {
@@ -1771,7 +1800,7 @@ function initNamePronounce() {
17711800
if (!cachedVoices.length) {
17721801
syncVoices();
17731802
}
1774-
speakName();
1803+
attemptSpeak(false);
17751804
});
17761805

17771806
nameSpeakBtn.addEventListener("pointerdown", () => {

0 commit comments

Comments
 (0)