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

@ -2,8 +2,10 @@
Special redeem visuals, called by polaroid.js (shared queue). Each returns a Promise that resolves when done.
FxAlert.giant(d, cur, snd) Gigantify Power-up: the viewer's emote grows huge, pulses while the sound plays
FxAlert.rain(d, cur, snd) On-Screen Celebration Power-up: copies of the emote bounce around the screen
FxAlert.message(d, cur, snd) Highlight my message / Message Effect / TTS redeems: big taped paper note with the message
(d.tts = spoken message URL, d.ttsMs = its length; TTS redeems come without a sound)
FxAlert.message(d, cur, snd) Highlight my message / Message Effect: big taped paper note with the message
FxAlert.envelope(d, cur, snd) TTS redeems: envelope opens, the letter rises out and settles in front, words ink in as
they are spoken (d.tts URL, d.ttsMs length, d.ttsEnv loudness per 40 ms, d.ttsPrefix
= words said before the message, e.g. "<name> says:")
d: { name (user), line, msg, emote (image URL or null) }; snd: { url, len, volume }
cur: { anims, timer, audio } so reset/skip can stop it (anything with cancel() can go in anims).
*/
@ -131,5 +133,63 @@
{ delay:exitAt, duration:500, easing:'ease-in' }));
}
window.FxAlert = { giant, rain, message, hide };
// ---- TTS redeems: envelope letter ----
const VOICE_AT = 1300; // ms: the voice starts while the letter comes back down
const ENV_STEP = 0.04; // s per loudness value (src/tts.js ENV_STEP_MS)
// when each shown word starts (s), spread over the voiced part of the clip, longer words and pauses after
// punctuation get more time. The first `prefix` spoken words ("<name> says:") are not on the letter.
function wordStarts(env, prefixWords, words) {
let s = 0, e = env.length - 1;
while (s < e && env[s] < .08) s++;
while (e > s && env[e] < .08) e--;
const all = [...prefixWords, ...words];
const weight = all.map(x => x.length + 2 + (/[.!?,:]$/.test(x) ? 5 : 0));
const total = weight.reduce((a, b) => a + b, 0) || 1;
let acc = 0;
const starts = all.map((_, i) => { const t = (s + (e - s + 1) * acc / total) * ENV_STEP; acc += weight[i]; return t; });
return starts.slice(prefixWords.length);
}
function envelope(d, cur, snd) {
const el = root();
const text = d.msg || '';
const words = text.split(/\s+/).filter(Boolean);
el.innerHTML = `<div class="envl"><div class="flap"></div>
<div class="slot"><div class="letter"><div class="to">from ${esc(d.name)} ♡</div>
<div class="tx">${words.map(w => `<span class="w">${esc(w)}</span>`).join(' ')}</div></div></div>
<div class="env"></div><div class="seal">♥</div></div>`;
el.style.visibility = 'visible';
const box = el.querySelector('.envl');
const spans = [...box.querySelectorAll('.w')];
A(cur, box, [{ transform:'translateY(-40px) scale(.92) rotate(-3deg)', opacity:0 }, { transform:'none', opacity:1 }],
{ duration:600, easing:'cubic-bezier(.2,1.25,.4,1)' });
let exitAt;
if (d.tts) {
const prefix = d.ttsPrefix ? [d.name, 'says:'].slice(0, d.ttsPrefix) : [];
const starts = d.ttsEnv && d.ttsEnv.length ? wordStarts(d.ttsEnv, prefix, words) : null;
const a = new Audio(d.tts); a.volume = snd.volume;
const t = setTimeout(() => { cur.audio = a; a.play().catch(err => console.warn('[alerts] TTS blocked:', err.message)); }, VOICE_AT);
let raf = 0, failed = false;
const fail = () => { failed = true; }; // voice missing/blocked: show the whole text anyway
a.addEventListener('error', fail, { once:true });
const t2 = setTimeout(() => { if (!a.currentTime) fail(); }, VOICE_AT + 1500);
const tick = () => { // ink in each word as it is said
const now = a.ended ? Infinity : a.currentTime + .05;
spans.forEach((w, i) => w.classList.toggle('on', failed || a.ended || (!a.paused || a.currentTime > 0) && (!starts || now >= starts[i])));
raf = requestAnimationFrame(tick);
};
raf = requestAnimationFrame(tick);
cur.anims.push({ cancel: () => { clearTimeout(t); clearTimeout(t2); cancelAnimationFrame(raf); a.pause(); } });
exitAt = VOICE_AT + (d.ttsMs || 4000) + 1400;
} else { // blocked or no voice: just show the letter
spans.forEach(w => w.classList.add('on'));
exitAt = 2400 + 2000 + 60 * text.length;
}
return done(cur, A(cur, box, [{ transform:'none', opacity:1 }, { transform:'translateY(40px) scale(.95) rotate(4deg)', opacity:0 }],
{ delay:exitAt, duration:500, easing:'ease-in' }));
}
window.FxAlert = { giant, rain, message, envelope, hide };
})();