From 93d5f2124d8b0c5ee83289549a0608693ea37b01 Mon Sep 17 00:00:00 2001 From: Arnav Angarkar Date: Tue, 1 Dec 2026 00:04:53 +0530 Subject: [PATCH 1/2] fix(listener): keep AI voice sentences that finish translating out of order Final captions were stored in the TTS segment queue under "seq-N" keys that nothing read or removed. After six captions the queue's overflow guard counted them and fired on every bundle, jumping past a sentence whose translation was still in flight when the next one landed; that sentence's text and speech were then dropped as late. Captions also seeded the queue's start, so the first listener of a language could wait for a sentence that was translated before their feed subscribed, then hear several sentences in a burst. Only the TTS feed now moves the queue. Until a sentence plays, an earlier one that arrives late still goes first. Refs #415 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- portal/static/js/listener-event.js | 19 ++++---- tests/test_listener_event.js | 74 ++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+), 8 deletions(-) diff --git a/portal/static/js/listener-event.js b/portal/static/js/listener-event.js index 3ebbe8fb6..5c75db772 100644 --- a/portal/static/js/listener-event.js +++ b/portal/static/js/listener-event.js @@ -301,6 +301,7 @@ var pendingRoomId = null; var lastBroadcastUnlocked = null; // last broadcast_unlocked seen for the selected booth var segmentStore = Object.create(null); var expectedSeq = null; +var firstQueuedSeq = null; var isSegmentPlaying = false; var fallbackQueueTimer = null; var seqWaitTimer = null; @@ -421,9 +422,14 @@ function openTtsWs(roomId, langCode, boothId) { var seq = frame.header.seq; if (!seq) return; - // Align sequence counter to the stream if it was reset (e.g. after language switch) - if (expectedSeq === null || expectedSeq === undefined) { - expectedSeq = seq; + // The queue starts at the first segment this feed sends. Translations can + // finish out of order, so an earlier one still goes first until one plays. + if ( + expectedSeq === null || + expectedSeq === undefined || + (seq < expectedSeq && expectedSeq === firstQueuedSeq && !isSegmentPlaying) + ) { + expectedSeq = firstQueuedSeq = seq; } if (seq < expectedSeq) return; // Late stage-2 audio must not replay an expired segment. @@ -941,7 +947,8 @@ function handleCaptionsMessage(event) { }); } } else if (data.status === "final") { - if (expectedSeq === null && data.seq) expectedSeq = data.seq; + // Only the TTS feed moves the speech queue: a caption's segment may have + // been translated before that feed subscribed, and then never arrives on it. var mode = captionModeSelect ? captionModeSelect.value : "original"; var isTranslationActive = Boolean(translationLangSelect.value) && mode !== "original"; @@ -949,10 +956,6 @@ function handleCaptionsMessage(event) { var sid = data.seq ? "seq-" + data.seq : data.segment_id || "legacy-" + Date.now(); - if (!segmentStore[sid]) { - segmentStore[sid] = { id: sid }; - } - segmentStore[sid].caption = data.text; // Seamlessly rename the partial block to the seq ID so pumpSegmentQueue can target it if (data.segment_id && data.seq) { diff --git a/tests/test_listener_event.js b/tests/test_listener_event.js index a714f83e7..846e87134 100644 --- a/tests/test_listener_event.js +++ b/tests/test_listener_event.js @@ -671,6 +671,80 @@ async function run() { ); }); + console.log('\n── AI voice keeps every sentence, in order and without gaps (#415) ──'); + + // The real scheduler on an audio clock the test holds still, so each + // sentence's start time can be compared with the end of the one before. + const stubScheduler = window.AudioScheduler; + require(path.join(__dirname, '..', 'portal', 'static', 'js', 'audio-scheduler.js')); + const realScheduler = window.AudioScheduler; + const audioClock = { + currentTime: 10, + state: 'running', + sampleRate: 48000, + destination: {}, + createBufferSource: () => ({ connect() {}, start() {}, stop() {}, onended: null }), + }; + let timeline = []; + window.AudioScheduler = { + create: () => { + const scheduler = realScheduler.create(audioClock, { jitterBufferSec: 0.25, comfortNoiseEnabled: false }); + return { + scheduleBuffer(buffer) { + const times = scheduler.scheduleBuffer({ length: buffer.length, duration: buffer.length / 24000 }); + timeline.push({ seq: buffer.length / 10, ...times }); + }, + reset() { + scheduler.reset(); + }, + }; + }, + }; + const order = () => JSON.stringify(timeline.map((t) => t.seq)); + const gapless = () => timeline.every((t, i) => i === 0 || Math.abs(t.startTime - timeline[i - 1].endTime) < 1e-9); + const translationLangSelect = document.getElementById('translation-lang-select'); + + // A sentence whose translation is still in flight when the next one's lands + // (two run at once per language) must still play, and play first. + choose(translationLangSelect, 'fr'); + for (let seq = 1; seq <= 6; seq++) { + for (const stage of ['caption', 'text', 'audio']) sendFloorSegment(seq, 'o' + seq, 'One ' + seq, 'Un ' + seq, stage); + } + for (const seq of [7, 8]) sendFloorSegment(seq, 'o' + seq, 'One ' + seq, 'Un ' + seq, 'caption'); + for (const stage of ['text', 'audio']) sendFloorSegment(8, 'o8', 'One 8', 'Un 8', stage); + for (const stage of ['text', 'audio']) sendFloorSegment(7, 'o7', 'One 7', 'Un 7', stage); + await waitFor(() => timeline.length >= 8); + assert(order() === '[1,2,3,4,5,6,7,8]', 'a sentence translated after the next one still plays, before it, got ' + order()); + assert(gapless(), 'each sentence starts where the one before it ends, got ' + JSON.stringify(timeline)); + assert( + historyBox.textContent.includes('Un 7'), + 'the slower sentence keeps its translation text, got "' + historyBox.textContent + '"', + ); + + // The first listener of a language: the sentence captioned as the feed + // opened was never translated for it, so speech must not wait for it. + timeline = []; + choose(translationLangSelect, 'fr'); + for (const seq of [21, 22, 23]) sendFloorSegment(seq, 'f' + seq, 'First ' + seq, 'Premier ' + seq, 'caption'); + for (const seq of [22, 23]) { + for (const stage of ['text', 'audio']) sendFloorSegment(seq, 'f' + seq, 'First ' + seq, 'Premier ' + seq, stage); + } + await waitFor(() => timeline.length >= 2); + assert(order() === '[22,23]', 'speech starts with the first sentence the feed sends, got ' + order()); + assert(gapless(), 'and plays the next one straight after it'); + + // The feed's first two sentences arrive out of order: nothing has played yet, so the earlier one goes first. + timeline = []; + choose(translationLangSelect, 'fr'); + sendFloorSegment(32, 's32', 'Second', 'Deuxième', 'text'); + sendFloorSegment(31, 's31', 'First', 'Premier', 'text'); + sendFloorSegment(32, 's32', 'Second', 'Deuxième', 'audio'); + sendFloorSegment(31, 's31', 'First', 'Premier', 'audio'); + await waitFor(() => timeline.length >= 2); + assert(order() === '[31,32]', 'the feed plays its first two sentences in order, got ' + order()); + assert(gapless(), 'back to back'); + window.AudioScheduler = stubScheduler; + console.log('\n── Empty translations (#1184) ──'); await import(pathToFileURL(path.join(__dirname, '..', 'portal', 'static', 'js', 'tts-parser.js')).href); From 20d9c9517f2b4bd0724e540cf9841bf8b34d4a85 Mon Sep 17 00:00:00 2001 From: Arnav Angarkar Date: Tue, 1 Dec 2026 00:21:24 +0530 Subject: [PATCH 2/2] docs(listener): say the queue rewinds until a sentence is scheduled Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- portal/static/js/listener-event.js | 2 +- tests/test_listener_event.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/portal/static/js/listener-event.js b/portal/static/js/listener-event.js index 5c75db772..43ee47388 100644 --- a/portal/static/js/listener-event.js +++ b/portal/static/js/listener-event.js @@ -423,7 +423,7 @@ function openTtsWs(roomId, langCode, boothId) { if (!seq) return; // The queue starts at the first segment this feed sends. Translations can - // finish out of order, so an earlier one still goes first until one plays. + // finish out of order, so an earlier one goes first until one is scheduled. if ( expectedSeq === null || expectedSeq === undefined || diff --git a/tests/test_listener_event.js b/tests/test_listener_event.js index 846e87134..70b0acfb3 100644 --- a/tests/test_listener_event.js +++ b/tests/test_listener_event.js @@ -733,7 +733,7 @@ async function run() { assert(order() === '[22,23]', 'speech starts with the first sentence the feed sends, got ' + order()); assert(gapless(), 'and plays the next one straight after it'); - // The feed's first two sentences arrive out of order: nothing has played yet, so the earlier one goes first. + // The feed's first two sentences arrive out of order: none is scheduled yet, so the earlier one goes first. timeline = []; choose(translationLangSelect, 'fr'); sendFloorSegment(32, 's32', 'Second', 'Deuxième', 'text');