TTS redeems, skip button, per-source chat placement

- Redeem variations can have a TTS voice: the viewer's text is cleaned
  (links out, repeats collapsed, max length, blocked words -> not read),
  spoken as "<name> says: ..." and shown as the big paper note; the
  variation's sound is not played when TTS is on (picker greyed out)
- Settings: TTS max length, say-the-name, blocked words (never sent to
  overlay pages: db.getOverlaySettings)
- Dashboard "Skip current alert" -> /alerts/skip -> overlay skips it
- db.migrate()/addColumn: new columns on existing databases
- Chat: ?chat=left,top,width,height and ?chatflow=up per browser source
  (e.g. the Intermission panel); default unchanged

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Bashy 2026-11-21 14:34:41 +02:00
parent b5a81bdb15
commit 4fda6060bf
16 changed files with 228 additions and 28 deletions

View file

@ -83,6 +83,32 @@ function cleanText(text) {
return String(text || '').replace(/[\u0000-\u001f\u007f]/g, ' ').replace(/\s+/g, ' ').trim().slice(0, MAX_CHARS);
}
// ---------- viewer text (redeems) ----------
// Returns { text, blocked }. blocked = a blocklist word was found: do not read anything.
const LEET = { 0: 'o', 1: 'i', 3: 'e', 4: 'a', 5: 's', 7: 't', '@': 'a', $: 's', '!': 'i' };
function parseBlocklist(list) {
return String(list || '').split(/[\n,]/).map(w => w.trim().toLowerCase()).filter(Boolean);
}
function prepareViewerText(input, { maxChars = 200, blocklist = '' } = {}) {
let t = cleanText(input)
.replace(/\b(?:https?:\/\/|www\.)\S+/gi, ' ') // links
.replace(/\b[\w-]+(?:\.[\w-]+)*\.(?:com|net|org|gg|tv|io|ru|xyz|ly|me|co|app|link|sk|ro|de|uk|info|site|shop)\b\S*/gi, ' ')
.replace(/(\S)\1{3,}/g, '$1$1$1') // aaaaaaa -> aaa
.replace(/\b(\w+)(?:\s+\1\b){2,}/gi, '$1 $1') // lol lol lol lol -> lol lol
.replace(/\s+/g, ' ').trim();
const max = Math.max(20, Math.min(MAX_CHARS, maxChars | 0 || 200));
if (t.length > max) t = t.slice(0, max).replace(/\s+\S*$/, '') + '…';
const plain = t.toLowerCase().replace(/[013457@$!]/g, c => LEET[c] || c);
const words = ' ' + plain.replace(/[^a-z0-9\u00c0-\u024f]+/g, ' ') + ' ';
const squashed = plain.replace(/[^a-z\u00c0-\u024f]+/g, '');
const blocked = parseBlocklist(blocklist).some(w => {
const ww = w.replace(/[013457@$!]/g, c => LEET[c] || c);
return ww.length >= 4 ? squashed.includes(ww.replace(/[^a-z\u00c0-\u024f]+/g, '')) // longer words: also when spaced out
: words.includes(' ' + ww.replace(/[^a-z0-9\u00c0-\u024f]+/g, ' ').trim() + ' ');
});
return { text: t, blocked };
}
// ---------- worker ----------
let proc = null, outBuf = '', nextId = 1;
const pending = new Map();
@ -150,6 +176,11 @@ function layerFilter(l, i) {
return `[${i}:a]${f.join(',')}[l${i}]`;
}
function durationMs(file) {
return new Promise(resolve => execFile('ffprobe', ['-v', 'error', '-show_entries', 'format=duration', '-of', 'csv=p=0', file],
{ timeout: 10000 }, (err, out) => resolve(err ? 0 : Math.round(parseFloat(out) * 1000) || 0)));
}
function prune() {
try {
const files = fs.readdirSync(OUT_DIR).filter(f => f.endsWith('.mp3'))
@ -158,7 +189,7 @@ function prune() {
} catch (e) { console.warn('[tts] prune:', e.message); }
}
// Returns { url, file, cached, ms }. Same config + text = same file (cached).
// Returns { url, file, cached, ms, durationMs }. Same config + text = same file (cached).
async function synth(configIn, textIn) {
const config = normalize(configIn);
const text = cleanText(textIn);
@ -166,7 +197,7 @@ async function synth(configIn, textIn) {
const hash = crypto.createHash('sha1').update(JSON.stringify([config, text])).digest('hex').slice(0, 20);
const file = path.join(OUT_DIR, `${hash}.mp3`);
const url = `/tts/${hash}.mp3`;
if (fs.existsSync(file)) return { url, file, cached: true, ms: 0 };
if (fs.existsSync(file)) return { url, file, cached: true, ms: 0, durationMs: await durationMs(file) };
const t0 = Date.now();
fs.mkdirSync(TMP_DIR, { recursive: true });
@ -191,7 +222,7 @@ async function synth(configIn, textIn) {
for (const w of wavs) fs.rm(w, { force: true }, () => {});
}
prune();
return { url, file, cached: false, ms: Date.now() - t0 };
return { url, file, cached: false, ms: Date.now() - t0, durationMs: await durationMs(file) };
}
module.exports = { voices, synth, normalize, cleanText, EFFECTS, LIMITS, DEFAULT_CONFIG, MAX_CHARS };
module.exports = { voices, synth, normalize, cleanText, prepareViewerText, parseBlocklist, EFFECTS, LIMITS, DEFAULT_CONFIG, MAX_CHARS };