TTS redeems: envelope letter with words inked in as spoken

- TTS alerts use the new `tts` style: envelope opens, the letter rises out
  and settles in front near the top, words ink in as the voice says them
  (timed from a loudness curve the server sends: tts.loudness, tts_env,
  tts_prefix); if the voice fails, the full text shows
- Highlight my message keeps its taped note

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Bashy 2026-11-21 14:42:36 +02:00
parent 4fda6060bf
commit 060b2351f7
6 changed files with 122 additions and 9 deletions

View file

@ -50,8 +50,8 @@ function pick(variations) {
}
function toAlert(v, { user, input, emote }) {
const spoken = !!(v.tts_voice_id && input); // TTS variations are shown as the big paper note
const style = spoken ? 'message' : (STYLE[`${v.kind}:${v.key}`] || 'cassette');
const spoken = !!(v.tts_voice_id && input); // TTS variations: envelope letter (fx.js envelope())
const style = spoken ? 'tts' : (STYLE[`${v.kind}:${v.key}`] || 'cassette');
const line = spoken ? LINE.tts : LINE[style];
return {
type: 'redeem', kind: v.kind, key: v.key, style,
@ -81,6 +81,8 @@ async function addSpeech(v, alert) {
const out = await tts.synth(voice.config, say);
alert.tts = out.url;
alert.tts_ms = out.durationMs;
alert.tts_env = await tts.loudness(out.file); // word timing on the overlay
alert.tts_prefix = say === text ? 0 : 2; // words spoken before the message ("<name> says:")
} catch (e) {
console.warn(`[tts] ${alert.user}: ${e.message}`);
}

View file

@ -181,6 +181,24 @@ function durationMs(file) {
{ timeout: 10000 }, (err, out) => resolve(err ? 0 : Math.round(parseFloat(out) * 1000) || 0)));
}
// Loudness curve: one value 0..1 per 40 ms (peak = 1). The overlay uses it to time the words of the
// envelope letter (ink in as they are spoken). ~25 numbers per second of speech.
const ENV_STEP_MS = 40;
function loudness(file) {
return new Promise(resolve => execFile('ffmpeg', ['-loglevel', 'error', '-i', file, '-ac', '1', '-ar', '8000', '-f', 's16le', '-'],
{ encoding: 'buffer', maxBuffer: 16 * 1024 * 1024, timeout: 15000 }, (err, out) => {
if (err || !out.length) return resolve([]);
const step = 8 * ENV_STEP_MS, env = [];
for (let i = 0; i + 1 < out.length / 2; i += step) {
let sum = 0, n = 0;
for (let j = i; j < Math.min(i + step, out.length / 2); j++) { const v = out.readInt16LE(j * 2) / 32768; sum += v * v; n++; }
env.push(Math.sqrt(sum / n));
}
const max = Math.max(...env) || 1;
resolve(env.map(v => Math.round(100 * v / max) / 100));
}));
}
function prune() {
try {
const files = fs.readdirSync(OUT_DIR).filter(f => f.endsWith('.mp3'))
@ -225,4 +243,4 @@ async function synth(configIn, textIn) {
return { url, file, cached: false, ms: Date.now() - t0, durationMs: await durationMs(file) };
}
module.exports = { voices, synth, normalize, cleanText, prepareViewerText, parseBlocklist, EFFECTS, LIMITS, DEFAULT_CONFIG, MAX_CHARS };
module.exports = { voices, synth, loudness, ENV_STEP_MS, normalize, cleanText, prepareViewerText, parseBlocklist, EFFECTS, LIMITS, DEFAULT_CONFIG, MAX_CHARS };