Skip to content
Merged
Show file tree
Hide file tree
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
19 changes: 11 additions & 8 deletions portal/static/js/listener-event.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 goes first until one is scheduled.
if (
expectedSeq === null ||
expectedSeq === undefined ||
(seq < expectedSeq && expectedSeq === firstQueuedSeq && !isSegmentPlaying)
Comment thread
ArnavBallinCode marked this conversation as resolved.
Resolved
) {
expectedSeq = firstQueuedSeq = seq;
}
if (seq < expectedSeq) return; // Late stage-2 audio must not replay an expired segment.

Expand Down Expand Up @@ -941,18 +947,15 @@ 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";

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) {
Expand Down
74 changes: 74 additions & 0 deletions tests/test_listener_event.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: none is scheduled 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);
Expand Down
Loading