Updated download button
Browse files- starscape.py +32 -18
starscape.py
CHANGED
|
@@ -226,25 +226,39 @@ playBtn.addEventListener('click', () => {
|
|
| 226 |
playBtn.textContent = '✦ Speaking…';
|
| 227 |
trigger.click();
|
| 228 |
|
| 229 |
-
// Reset the button when the audio finishes
|
| 230 |
-
|
| 231 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 232 |
const audio = findGradioAudio('tts-audio');
|
| 233 |
-
|
| 234 |
-
if (audio)
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
audio.
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
}, 250);
|
| 248 |
});
|
| 249 |
|
| 250 |
const THREE = await import('https://esm.sh/three@0.160.0');
|
|
|
|
| 226 |
playBtn.textContent = '✦ Speaking…';
|
| 227 |
trigger.click();
|
| 228 |
|
| 229 |
+
// Reset the button when the freshly-loaded audio finishes. Gradio replaces
|
| 230 |
+
// the <audio> element when it sets a new src, so we wait for that src to
|
| 231 |
+
// appear, then listen on THAT element.
|
| 232 |
+
let lastSrc = '';
|
| 233 |
+
let finished = false;
|
| 234 |
+
let poll = null;
|
| 235 |
+
const startTime = Date.now();
|
| 236 |
+
|
| 237 |
+
function finish() {
|
| 238 |
+
if (finished) return;
|
| 239 |
+
finished = true;
|
| 240 |
+
if (poll) clearInterval(poll);
|
| 241 |
+
playBtn.classList.remove('playing');
|
| 242 |
+
playBtn.textContent = '▶ Play wisdom';
|
| 243 |
+
}
|
| 244 |
+
|
| 245 |
+
poll = setInterval(() => {
|
| 246 |
+
if (Date.now() - startTime > 90000) return finish();
|
| 247 |
const audio = findGradioAudio('tts-audio');
|
| 248 |
+
if (!audio || !audio.src) return;
|
| 249 |
+
if (audio.src === lastSrc) return;
|
| 250 |
+
|
| 251 |
+
lastSrc = audio.src;
|
| 252 |
+
audio.addEventListener('ended', finish, { once: true });
|
| 253 |
+
|
| 254 |
+
const scheduleByDuration = () => {
|
| 255 |
+
if (audio.duration > 0 && !Number.isNaN(audio.duration)) {
|
| 256 |
+
setTimeout(finish, (audio.duration + 1.5) * 1000);
|
| 257 |
+
}
|
| 258 |
+
};
|
| 259 |
+
if (audio.readyState >= 1) scheduleByDuration();
|
| 260 |
+
else audio.addEventListener('loadedmetadata', scheduleByDuration, { once: true });
|
| 261 |
+
}, 300);
|
|
|
|
| 262 |
});
|
| 263 |
|
| 264 |
const THREE = await import('https://esm.sh/three@0.160.0');
|