Translate app: sound level meter; translate unfinished segments

This commit is contained in:
James Babcock committed 2026-10-25 13:12:25 -07:00
1 parent bf2addd287
commit 0b7d93a3f1
25 files changed
+679 -175

No files matched your search

+25 -3
View File
@@ -2,7 +2,7 @@ const test = require('node:test');
const assert = require('node:assert/strict');
const { loader } = require('./helpers/load-typescript.cjs');
function harness(provider = 'elevenlabs', continuous = true) {
function harness(provider = 'elevenlabs', continuous = true, extraOptions = {}) {
const timers = new Map();
let timerId = 0;
const sockets = [];
@@ -30,7 +30,7 @@ function harness(provider = 'elevenlabs', continuous = true) {
const Provider = load(`app/native/${provider}-stt.ts`)[names[provider]];
const { ReconnectingSttClient } = load('app/native/reconnecting-stt.ts');
const client = new ReconnectingSttClient(options => new Provider(options), {
apiKey: 'test', onTranscript: event => events.push(event),
apiKey: 'test', ...extraOptions, onTranscript: event => events.push(event),
onStatus: status => statuses.push(status), onError: error => errors.push(error),
}, () => continuous);
client.start();
@@ -203,7 +203,10 @@ test('provider selection falls back to on-device without a key and connects the
FaceclawWebSocket: class { constructor(url, _listener, header, value) { sockets.push({ url, header, value }); } },
} } },
}, { './cloud-stt': { CLOUD_STT_SAMPLE_RATE: 16000, encodeBase64: () => '', toJavaBytes: bytes => bytes } });
const { createCloudSttClient, usesCloudStt } = load('app/native/cloud-stt-provider.ts');
const { createCloudSttClient, usesCloudStt, transcriptionKey } = load('app/native/cloud-stt-provider.ts');
// A running capture is shared only on the same provider and language.
assert.equal(transcriptionKey({ provider: 'soniox', language: ' JA ' }), 'soniox:ja');
assert.notEqual(transcriptionKey({ provider: 'soniox' }), transcriptionKey({ provider: 'soniox', language: 'ja' }));
const statuses = [];
const options = { onTranscript() {}, onStatus: status => statuses.push(status), onError() {} };
const selection = { provider: 'onboard', elevenLabsApiKey: '', openAiApiKey: ' ', sonioxApiKey: 'soniox-key' };
@@ -217,4 +220,23 @@ test('provider selection falls back to on-device without a key and connects the
createCloudSttClient({ ...selection, provider: 'elevenlabs', elevenLabsApiKey: ' xi ' }, options, () => false).start();
assert.deepEqual(sockets.map(({ url, header, value }) => [new URL(url).host, header, value]),
[['stt-rt.soniox.com', null, null], ['api.elevenlabs.io', 'xi-api-key', 'xi']]);
createCloudSttClient({ ...selection, provider: 'elevenlabs', elevenLabsApiKey: 'xi', language: ' ja ' }, options, () => false).start();
assert.equal(new URL(sockets.at(-1).url).searchParams.get('language_code'), 'ja');
});
test('a spoken-language hint reaches each provider on the wire, and is left out to auto-detect', () => {
for (const language of ['ja', '']) {
const elevenlabs = harness('elevenlabs', true, { language });
assert.equal(new URL(elevenlabs.sockets[0].url).searchParams.get('language_code'), language || null);
const soniox = harness('soniox', true, { language });
soniox.open();
assert.deepEqual(JSON.parse(soniox.sockets[0].sent[0]).language_hints, language ? ['ja'] : undefined);
const openai = harness('openai', true, { language });
openai.open();
const transcription = JSON.parse(openai.sockets[0].sent[0]).session.audio.input.transcription;
assert.equal(transcription.model, 'gpt-realtime-whisper');
assert.equal(transcription.language, language || undefined);
}
});
+9 -6
View File
@@ -348,7 +348,7 @@ test('iOS continuous capture is shared by windows and restarts after a disconnec
get continuousActive() { return !!this.source || this.draining; },
prepare: async () => true,
startGlassesCapture: async (_session, voice, _log, _endpointing, continuous) => {
calls.push(['glasses', continuous]); providers.push(`${voice.provider}:${voice.whisperServerLanguage}`); bridge.source = 'glasses'; },
calls.push(['glasses', continuous]); providers.push(`${voice.provider}:${voice.language}`); bridge.source = 'glasses'; },
startPhoneCapture: async (_voice, _log, _endpointing, continuous) => { calls.push(['phone', continuous]); bridge.source = 'phone'; },
drain() { if (this.source) { this.source = null; this.draining = true; } },
stopContinuousCapture: async () => { calls.push(['stop']); bridge.drain(); },
@@ -361,7 +361,7 @@ test('iOS continuous capture is shared by windows and restarts after a disconnec
const { IosPreviewController } = load('app/g2/ios-preview-controller.ts', {
require: id => id === '../native/ios-voice-input' ? { iosVoiceInput: bridge } : id === '../ui/dashboard-settings' ? settings
: id === '../ui/shell/voice-activity' ? { voiceActivity: { setActive: (active, holder) => claims.push([active, holder]) } }
: id === '../native/cloud-stt-provider' ? { transcriptionKey: voice => `${voice.provider}:${voice.whisperServerLanguage}` } : {},
: id === '../native/cloud-stt-provider' ? { transcriptionKey: voice => `${voice.provider}:${voice.language}` } : {},
});
const host = Object.create(IosPreviewController.prototype);
Object.assign(host, { active: true, glassesLocked: false, communicator: {}, state: { phase: 'connected' }, sessionConnected: true,
@@ -381,14 +381,17 @@ test('iOS continuous capture is shared by windows and restarts after a disconnec
host.stopContinuousVoiceCapture('calculator');
assert.deepEqual(calls, [['glasses', true]], 'closing one window keeps the other listening');
// Translate needs a multilingual provider: the shared capture drains and
// restarts on it (Whisper left to detect the language), and moves back to
// the main provider once Translate closes.
calls.length = 0; host.startContinuousVoiceCapture('translate', { multilingualProvider: 'soniox' }); await settle();
// Translate needs its own provider and language: the shared capture drains
// and restarts on them, again when only the language changes, and moves
// back to the main provider (and Whisper's own language) once Translate closes.
calls.length = 0; host.startContinuousVoiceCapture('translate', { transcription: { provider: 'soniox', language: '' } }); await settle();
assert.deepEqual(calls, [['stop']]);
drained(); await settle();
assert.deepEqual(calls, [['stop'], ['glasses', true]]);
assert.deepEqual(providers.slice(-2), ['whisper-server:en', 'soniox:']);
calls.length = 0; host.startContinuousVoiceCapture('translate', { transcription: { provider: 'soniox', language: 'ja' } }); drained(); await settle();
assert.deepEqual(calls, [['stop'], ['glasses', true]]);
assert.equal(providers.at(-1), 'soniox:ja');
calls.length = 0; host.stopContinuousVoiceCapture('translate'); drained(); await settle();
assert.deepEqual(calls, [['stop'], ['glasses', true]]);
assert.equal(providers.at(-1), 'whisper-server:en');
+100
View File
@@ -0,0 +1,100 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const { loader } = require('./helpers/load-typescript.cjs');
const { pcmLevel } = loader()('app/util/audio-level.ts');
function harness() {
const levelListeners = new Set();
let tick = null;
const { MicLevelMeter, paintListeningHeader } = loader({
setInterval(fn) { tick = fn; return 1; },
clearInterval() { tick = null; },
}, {
'../native/voice-control': { voiceControlBridge: {
onMicLevel(listener) { levelListeners.add(listener); return () => levelListeners.delete(listener); },
} },
})('app/ui/listening-header.ts');
const level = value => { for (const listener of levelListeners) listener(value); };
return { MicLevelMeter, paintListeningHeader, level, tick: () => tick?.(), ticking: () => tick !== null, levelListeners };
}
/** Records draws; text is 6px per character, like a fixed-width bitmap face. */
function fakeImage(width = 576) {
const texts = [], rects = [];
return { width, texts, rects,
drawText(_font, x, y, text, gray) { texts.push({ x, y, text, gray }); },
fillRect(x, y, w, h, gray) { rects.push({ x, y, w, h, gray }); } };
}
const font = { ascent: 12, lineHeight: 16, measureText: text => text.length * 6 };
function pcm(amplitude, samples = 800) {
const bytes = new Uint8Array(samples * 2);
for (let i = 0; i < samples; i++) {
const value = (i % 2 ? amplitude : -amplitude) & 0xffff;
bytes[i * 2] = value & 255;
bytes[i * 2 + 1] = value >> 8;
}
return bytes;
}
test('pcm level maps RMS onto a 60 dB meter', () => {
assert.equal(pcmLevel(new Uint8Array(0)), 0);
assert.equal(pcmLevel(pcm(0)), 0);
assert.ok(Math.abs(pcmLevel(pcm(3277)) - 2 / 3) < 0.01, '-20 dBFS sits two thirds up');
assert.ok(pcmLevel(pcm(32767)) > 0.99);
});
test('the meter repaints only when the lit segment count changes, and only while visible', () => {
const h = harness();
const meter = new h.MicLevelMeter();
let renders = 0, visible = true;
meter.start(() => renders++, () => visible);
h.level(0.3); h.level(0.5); h.level(0.2);
h.tick();
assert.equal(renders, 1, 'the peak of the tick lights 4 of 8');
h.level(0.5); h.tick();
assert.equal(renders, 1, 'same segment count, no repaint');
// Off screen the level still tracks, so the next visible paint is current.
visible = false;
h.level(1); h.tick();
assert.equal(renders, 1);
const image = fakeImage();
meter.paint(image, 0, 10, 6);
assert.equal(image.rects.filter(rect => rect.gray === 200).length, 8);
// Silence decays the meter to empty within about a second.
visible = true;
for (let i = 0; i < 5; i++) h.tick();
const after = fakeImage();
meter.paint(after, 0, 10, 6);
assert.equal(after.rects.filter(rect => rect.gray === 200).length, 0);
assert.ok(renders > 1);
meter.stop();
assert.equal(h.ticking(), false);
assert.equal(h.levelListeners.size, 0);
});
test('the header puts title and status on one line, truncated before the meter', () => {
const h = harness();
const meter = new h.MicLevelMeter();
const image = fakeImage(300);
const bodyTop = h.paintListeningHeader(image, font, 'Translate', 'Listening (ElevenLabs)...', meter);
assert.equal(bodyTop, 20 + 18 + 2);
assert.deepEqual(image.texts.map(({ x, y, gray }) => [x, y, gray]), [[24, 20, 200], [24 + 54, 20, 110]]);
assert.equal(image.texts[0].text, 'Translate');
const status = image.texts[1].text;
assert.ok(status.startsWith(' · Listening') && status.endsWith('...'), status);
const meterX = Math.min(...image.rects.map(rect => rect.x));
assert.equal(meterX, 300 - 24 - meter.width);
assert.ok(24 + 54 + status.length * 6 <= meterX - 10, 'status stops short of the meter');
// Segments sit on the text baseline.
assert.ok(image.rects.every(rect => rect.y + rect.h === 20 + font.ascent));
const idle = fakeImage(300);
h.paintListeningHeader(idle, font, 'Transcribe', 'Starting microphone...', null);
assert.equal(idle.rects.length, 0);
assert.equal(idle.texts[1].text, ' · Starting microphone...');
});
+109 -23
View File
@@ -32,14 +32,13 @@ function settingsHarness({ main = 'onboard', setUp = [] } = {}) {
})('app/apps/translate/translate-settings.ts');
}
test('finals become segments; finals arriving mid-translation merge into one pending segment', () => {
test('finals translate in order; finals arriving mid-request merge into one waiting block', () => {
const model = new TranslationModel();
model.accept({ text: 'Hola, ¿qué', isFinal: false });
assert.equal(model.live, 'Hola, ¿qué');
model.accept({ text: 'Hola, ¿qué tal?', isFinal: true });
assert.equal(model.live, '');
assert.equal(model.segments.length, 1);
const first = model.nextJob();
assert.equal(first.segment.source, 'Hola, ¿qué tal?');
assert.equal(first.source, 'Hola, ¿qué tal?');
assert.deepEqual(plain(first.context), []);
assert.equal(model.nextJob(), null, 'one request at a time');
@@ -49,30 +48,91 @@ test('finals become segments; finals arriving mid-translation merge into one pen
assert.equal(model.segments.length, 2);
assert.equal(model.segments[1].source, 'Muy bien. Gracias.');
model.progress(first.segment, 'Hi, how');
assert.equal(first.segment.translation, 'Hi, how');
model.complete(first.segment, 'Hi, how are you?');
assert.equal(model.progress(first, 'Hi, how'), true);
assert.equal(first.segment.streaming, 'Hi, how');
model.complete(first, 'Hi, how are you?');
const second = model.nextJob();
assert.equal(second.segment.source, 'Muy bien. Gracias.');
assert.equal(second.source, 'Muy bien. Gracias.');
assert.deepEqual(plain(second.context), [{ source: 'Hola, ¿qué tal?', translation: 'Hi, how are you?' }]);
model.complete(second.segment, 'Very well. Thanks.');
model.complete(second, 'Very well. Thanks.');
assert.equal(model.nextJob(), null);
});
test('the live block is translated provisionally, redone as it grows, and settles on a matching final', () => {
const model = new TranslationModel();
model.accept({ text: '今日は', isFinal: false });
const first = model.nextJob();
assert.equal(first.source, '今日は');
model.accept({ text: '今日はいい天気', isFinal: false }); // grows while the request is in flight
assert.equal(model.nextJob(), null);
model.complete(first, 'Today');
assert.deepEqual(plain(model.lines(measure, 100, false)), [{ text: 'Today', gray: 170 }]);
const second = model.nextJob();
assert.equal(second.source, '今日はいい天気');
// The redo streams over the old text only once it is as long.
model.progress(second, 'To');
assert.deepEqual(plain(model.lines(measure, 100, false).map(line => line.text)), ['Today']);
model.progress(second, 'The weather');
assert.deepEqual(plain(model.lines(measure, 100, false).map(line => line.text)), ['The weather']);
model.complete(second, 'The weather is nice today');
// A final matching what was translated needs no further request.
model.accept({ text: '今日はいい天気', isFinal: true });
assert.equal(model.nextJob(), null);
assert.deepEqual(plain(model.lines(measure, 100, false)), [{ text: 'The weather is nice today', gray: 230 }]);
});
test('a final that differs from the request in flight makes it moot; a matching one keeps it', () => {
const model = new TranslationModel();
model.accept({ text: 'Bonjour', isFinal: false });
const stale = model.nextJob();
assert.equal(model.accept({ text: 'Bonjour à tous', isFinal: true }), true);
assert.equal(model.isActive(stale), false);
assert.equal(model.progress(stale, 'Hello'), false);
const redo = model.nextJob();
assert.equal(redo.source, 'Bonjour à tous');
model.complete(stale, 'Hello'); // a late reply from the cancelled request is ignored
assert.equal(model.isActive(redo), true);
model.complete(redo, 'Hello everyone');
model.accept({ text: 'Merci', isFinal: false });
const kept = model.nextJob();
assert.equal(model.accept({ text: 'Merci', isFinal: true }), false);
model.complete(kept, 'Thank you');
assert.equal(model.nextJob(), null);
assert.deepEqual(plain(model.lines(measure, 100, false).map(line => line.gray)), [230, 230]);
});
test('small revisions of a live block wait for a bigger change; its final is always redone', () => {
const model = new TranslationModel();
model.accept({ text: 'abcdef', isFinal: false });
model.complete(model.nextJob(), 'one');
model.accept({ text: 'abcdeg', isFinal: false });
assert.equal(model.nextJob(), null);
model.accept({ text: 'abcdefgh', isFinal: false });
assert.equal(model.nextJob(), null);
model.accept({ text: 'abcdefghi', isFinal: false });
model.complete(model.nextJob(), 'two');
model.accept({ text: 'abcdefghj', isFinal: true });
assert.equal(model.nextJob().source, 'abcdefghj');
});
test('an empty final keeps the live partial it replaces', () => {
const model = new TranslationModel();
model.accept({ text: 'Bonjour', isFinal: false });
model.accept({ text: '', isFinal: true });
assert.equal(model.segments[0].source, 'Bonjour');
assert.equal(model.segments[0].final, true);
});
test('context skips failed and dropped turns and stays bounded', () => {
test('context skips failed, dropped and provisional turns and stays bounded', () => {
const model = new TranslationModel();
const translate = (source, translation) => {
model.accept({ text: source, isFinal: true });
const job = model.nextJob();
if (translation === null) model.fail(job.segment);
else model.complete(job.segment, translation);
if (translation === null) model.fail(job);
else model.complete(job, translation);
};
translate('uno', 'one');
translate('ruido', null);
@@ -87,18 +147,29 @@ test('context skips failed and dropped turns and stays bounded', () => {
const long = new TranslationModel();
for (let i = 0; i < 3; i++) {
long.accept({ text: 'x'.repeat(500), isFinal: true });
long.complete(long.nextJob().segment, 'y'.repeat(500));
long.complete(long.nextJob(), 'y'.repeat(500));
}
long.accept({ text: 'next', isFinal: true });
assert.equal(long.nextJob().context.length, 1, 'the newest turn is kept even when it alone is over the cap');
// A final whose redo failed still shows its provisional translation, but
// that translates other text, so it isn't context.
const stale = new TranslationModel();
stale.accept({ text: 'Ich gehe', isFinal: false });
stale.complete(stale.nextJob(), 'I go');
stale.accept({ text: 'Ich gehe nach Hause', isFinal: true });
stale.fail(stale.nextJob());
assert.deepEqual(plain(stale.lines(measure, 100, false)), [{ text: 'I go', gray: 170 }]);
stale.accept({ text: 'Tschüss', isFinal: true });
assert.deepEqual(plain(stale.nextJob().context), []);
});
test('display lines: translations bright, originals dim until translated or when shown, live dimmest', () => {
test('display lines: settled bright, provisional dimmer, originals dim until translated or when shown', () => {
const model = new TranslationModel();
model.accept({ text: 'Guten Morgen', isFinal: true });
model.complete(model.nextJob().segment, 'Good morning');
model.complete(model.nextJob(), 'Good morning');
model.accept({ text: 'Wie geht es?', isFinal: true });
const asking = model.nextJob().segment;
const asking = model.nextJob();
model.accept({ text: 'Danke', isFinal: false });
assert.deepEqual(plain(model.lines(measure, 100, false)), [
{ text: 'Good morning', gray: 230 },
@@ -107,28 +178,29 @@ test('display lines: translations bright, originals dim until translated or when
]);
model.progress(asking, 'How are');
assert.deepEqual(plain(model.lines(measure, 100, false).map(line => line.text)), ['Good morning', 'How are', 'Danke']);
assert.deepEqual(plain(model.lines(measure, 100, true).map(line => line.gray)), [120, 230, 120, 230, 90]);
assert.deepEqual(plain(model.lines(measure, 100, true).map(line => line.gray)), [120, 230, 120, 170, 90]);
assert.deepEqual(plain(model.lines(measure, 7, false).slice(0, 2)), [{ text: 'Good', gray: 230 }, { text: 'morning', gray: 230 }]);
// A failed segment keeps its original on screen.
// A failed block keeps its original on screen.
model.fail(asking);
assert.deepEqual(plain(model.lines(measure, 100, false)[1]), { text: 'Wie geht es?', gray: 120 });
// A finished, empty translation is noise: hidden unless originals are shown.
// A settled, empty translation is noise: hidden unless originals are shown.
model.accept({ text: 'mmm', isFinal: true });
model.complete(model.nextJob().segment, '');
model.complete(model.nextJob(), '');
assert.equal(model.lines(measure, 100, false).some(line => line.text === 'mmm'), false);
assert.equal(model.lines(measure, 100, true).some(line => line.text === 'mmm'), true);
});
test('saved text pairs each original with its translation', () => {
test('saved text pairs each final original with its translation', () => {
const model = new TranslationModel();
for (const [source, translation] of [['Ciao', 'Hello'], ['Okay.', 'Okay.'], ['boh', null]]) {
model.accept({ text: source, isFinal: true });
const job = model.nextJob();
if (translation === null) model.fail(job.segment);
else model.complete(job.segment, translation);
if (translation === null) model.fail(job);
else model.complete(job, translation);
}
model.accept({ text: 'ancora', isFinal: false }); // still live: not saved
assert.equal(model.savedText(), 'Ciao\n→ Hello\n\nOkay.\n\nboh\n→ (not translated)');
});
@@ -138,6 +210,8 @@ test('the user message carries earlier turns as context, then the speech', () =>
assert.match(message, /context only/);
assert.match(message, /<speech>Bien<\/speech>\n<english>Fine<\/english>\n\nTranslate this speech/);
assert.ok(message.endsWith('<speech>¿Y tú?</speech>'));
assert.equal(buildTranslateUserMessage('こんにちは', [], 'Japanese'),
'Translate this Japanese speech into English:\n<speech>こんにちは</speech>');
});
test('Auto picks the main provider when it is multilingual, else the first one set up', () => {
@@ -164,3 +238,15 @@ test('an explicit provider is used only while it is set up, and on-device ones a
assert.equal(settings.translateProviderSetting.isDisabled('auto'), false);
assert.deepEqual(plain(settings.translateProviderSetting.values), ['auto', 'elevenlabs', 'whisper', 'soniox', 'whisper-server']);
});
test('the spoken language defaults to auto-detect and resolves to an ISO 639-1 code once chosen', () => {
const settings = settingsHarness();
assert.equal(settings.resolveTranslateLanguage(), '');
assert.equal(settings.translateLanguageSetting.displayValue('auto'), 'Auto-detect');
settings.translateLanguageSetting.set('ja');
assert.equal(settings.resolveTranslateLanguage(), 'ja');
assert.equal(settings.spokenLanguageName('ja'), 'Japanese');
assert.equal(settings.translateLanguageSetting.values[0], 'auto');
assert.ok(settings.translateLanguageSetting.values.slice(1).every(code => /^[a-z]{2}$/.test(code)));
assert.equal(settings.translateLanguageSetting.values.includes('en'), false, 'English is the target, not a source');
});