cdr6934 commited on
Commit
f96c480
·
1 Parent(s): 20e0cd7

Updated download button

Browse files
Files changed (1) hide show
  1. 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 (poll briefly for the <audio> tag).
230
- let tries = 0;
231
- const watch = setInterval(() => {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
232
  const audio = findGradioAudio('tts-audio');
233
- tries++;
234
- if (audio) {
235
- const reset = () => {
236
- playBtn.classList.remove('playing');
237
- playBtn.textContent = '▶ Play wisdom';
238
- audio.removeEventListener('ended', reset);
239
- };
240
- audio.addEventListener('ended', reset);
241
- clearInterval(watch);
242
- } else if (tries > 80) {
243
- clearInterval(watch);
244
- playBtn.classList.remove('playing');
245
- playBtn.textContent = '▶ Play wisdom';
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');