Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -1111,6 +1111,9 @@ function startVoicePlaybackForMessage(message, voice) {
if (!job.cancelled) console.warn('TTS fetch failed', e);
job.results[index] = null;
setTtsChunkState(job, index, 'error');
if (!job.cancelled && index === job.playIndex) {
tryStartPlayback(job);
}
} finally {
job.inflight -= 1;
}
Expand All @@ -1127,9 +1130,20 @@ function tryStartPlayback(job) {
if (job.cancelled) return;
// If already playing, nothing to do; the 'ended' handler will pick next
if (job.audio && !job.audio.ended && !job.audio.paused) return;
while (job.playIndex < job.results.length) {
const status = job.status?.[job.playIndex];
const url = job.results[job.playIndex];
if (url) break;
if (url === null || status === 'error') {
job.playIndex += 1;
continue;
}
return; // waiting on fetch to finish
}
if (job.playIndex >= job.results.length) return;
const index = job.playIndex;
const url = job.results[index];
if (!url) return; // not ready yet
if (!url) return;
const audio = new Audio(url);
audio.preload = 'auto';
audio.currentTime = 0;
Expand Down Expand Up @@ -1186,6 +1200,10 @@ function tryStartPlayback(job) {
if (!started) {
// Give slower decoders more time; mark as error only after generous grace
setTtsChunkState(job, index, 'error');
clearWatchdog();
try { audio.pause(); } catch {}
job.audio = null;
audio.removeEventListener('timeupdate', onTimeUpdate);
job.playIndex += 1;
tryStartPlayback(job);
}
Expand All @@ -1196,12 +1214,18 @@ function tryStartPlayback(job) {
clearTimeout(stallTimer);
clearWatchdog();
setTtsChunkState(job, index, 'done');
job.audio = null;
audio.removeEventListener('timeupdate', onTimeUpdate);
job.playIndex += 1;
tryStartPlayback(job);
});
audio.addEventListener('error', () => {
if (job.cancelled) return;
setTtsChunkState(job, index, 'error');
clearTimeout(stallTimer);
clearWatchdog();
job.audio = null;
audio.removeEventListener('timeupdate', onTimeUpdate);
job.playIndex += 1; // skip broken chunk
tryStartPlayback(job);
});
Expand Down