/* Silly Sound Squad listener: finds the word in a short recording and measures the hiss next to the vowel. Verdicts: soft (a soft, spread hiss: TH or F), sharp (a loud, narrow S/Z hiss), pop (no long hiss: T, P, D), voice (voicing, used for V), quiet (nothing heard). TH and F sound almost the same, so the listener cannot tell them apart: the mouth picture and a grown-up cover that one. Nothing is saved or sent. */ (function (G) { function fft(re, im) { const n = re.length; for (let i = 1, j = 0; i < n; i++) { let bit = n >> 1; for (; j & bit; bit >>= 1) j ^= bit; j ^= bit; if (i < j) { let t = re[i]; re[i] = re[j]; re[j] = t; t = im[i]; im[i] = im[j]; im[j] = t; } } for (let len = 2; len <= n; len <<= 1) { const a = -2 * Math.PI / len, wr = Math.cos(a), wi = Math.sin(a); for (let i = 0; i < n; i += len) { let cr = 1, ci = 0; for (let k = 0; k < len / 2; k++) { const ur = re[i + k], ui = im[i + k]; const vr = re[i + k + len / 2] * cr - im[i + k + len / 2] * ci; const vi = re[i + k + len / 2] * ci + im[i + k + len / 2] * cr; re[i + k] = ur + vr; im[i + k] = ui + vi; re[i + k + len / 2] = ur - vr; im[i + k + len / 2] = ui - vi; const t = cr * wr - ci * wi; ci = cr * wi + ci * wr; cr = t; } } } } const winCache = {}; function hann(N) { if (!winCache[N]) { const w = new Float32Array(N); for (let i = 0; i < N; i++) w[i] = 0.5 - 0.5 * Math.cos(2 * Math.PI * i / (N - 1)); winCache[N] = w; } return winCache[N]; } function frameSize(sr) { return sr > 32000 ? 1024 : 512; } // One frame of features from N samples starting at s. function frame(x, s, N, sr) { const w = hann(N), re = new Float64Array(N), im = new Float64Array(N); let sq = 0, zc = 0, prev = x[s]; for (let i = 0; i < N; i++) { const v = x[s + i]; sq += v * v; re[i] = v * w[i]; if ((v >= 0) !== (prev >= 0)) zc++; prev = v; } fft(re, im); const bin = sr / N, half = N / 2, top = Math.min(11000, sr / 2 - bin); let low = 0, mid = 0, high = 0, cn = 0, cd = 0, fl = 0, fa = 0, fc = 0, mx = 0, ca = 0, cc = 0; for (let k = 1; k < half; k++) { const f = k * bin, p = re[k] * re[k] + im[k] * im[k] + 1e-12; if (f >= 80 && f < 900) low += p; else if (f >= 900 && f < 3000) mid += p; else if (f >= 3000 && f <= top) high += p; if (f >= 1000 && f <= top) { cn += f * p; cd += p; } if (f >= 1500 && f <= Math.min(10000, top)) { fl += Math.log(p); fa += p; fc++; } if (f >= 3000 && f <= Math.min(10000, top)) { if (p > mx) mx = p; ca += p; cc++; } } const rms = Math.sqrt(sq / N); return { db: 20 * Math.log10(rms + 1e-9), low, mid, high, zcr: zc / N, cen: cd ? cn / cd : 0, flat: fc ? Math.exp(fl / fc) / (fa / fc) : 0, crest: cc ? mx / (ca / cc) : 0 }; } function features(x, sr) { const N = frameSize(sr), H = N / 2, out = []; for (let s = 0; s + N <= x.length; s += H) { const f = frame(x, s, N, sr); f.t = s / sr; out.push(f); } out.hopMs = 1000 * H / sr; return out; } // The hiss right before (dir -1) or after (dir +1) the vowel. function hissNear(F, from, dir, floor) { const got = []; let miss = 0; for (let i = from, n = 0; i >= 0 && i < F.length && n < 45; i += dir, n++) { const f = F[i]; const fric = f.db > floor + 6 && f.high > f.low * 1.2 && f.zcr > 0.15; if (fric) { got.push(f); miss = 0; } else if (f.db < floor + 6) { if (++miss > 2 && got.length) break; if (miss > 4) break; } else { if (++miss > 3) break; } } return got; } function analyze(x, sr, kind) { const F = features(x, sr), hop = F.hopMs; if (F.length < 8) return { verdict: 'quiet' }; const sorted = F.map(f => f.db).sort((a, b) => a - b); const floor = Math.max(-75, sorted[Math.floor(sorted.length * 0.1)]), peak = sorted[sorted.length - 1]; if (peak - floor < 15) return { verdict: 'quiet', floor, peak }; // The vowel: the longest run of loud, low-pitched frames. const voiced = F.map(f => f.db > peak - 24 && f.low > f.high && f.zcr < 0.2); let best = null, s = -1, gap = 0; const runs = []; for (let i = 0; i <= F.length; i++) { if (i < F.length && voiced[i]) { if (s < 0) s = i; gap = 0; } else if (s >= 0) { if (i < F.length && ++gap <= 2) continue; const e = i - gap; runs.push({ s, e }); if (!best || e - s > best.e - best.s) best = { s, e }; s = -1; gap = 0; } } if (!best || (best.e - best.s + 1) * hop < 50) return { verdict: 'quiet', floor, peak }; const big = runs.filter(r => (r.e - r.s + 1) * hop >= 50); if (kind === 'I' || kind === 'dhI' || kind === 'V') best = big[0]; else if (kind === 'F') best = big[big.length - 1]; let vpk = -200; for (let i = best.s; i <= best.e; i++) vpk = Math.max(vpk, F[i].db); const info = { floor, peak, vowelMs: Math.round((best.e - best.s + 1) * hop) }; if (kind === 'thM' || kind === 'V') { // Buzzy sounds (V, buzzy TH) and sounds in the middle of a word: heard is enough, unless a loud sharp hiss starts it (Z). const pre = hissNear(F, best.s - 1, -1, floor); if (pre.length >= 3) { const top = pre.map(f => f.db).sort((a, b) => b - a).slice(0, 3), rel = top.reduce((a, b) => a + b, 0) / top.length - vpk; const fl = pre.reduce((a, f) => a + f.flat, 0) / pre.length; if (rel > -16 && fl < 0.25) return Object.assign({ verdict: 'sharp', rel, flat: fl }, info); } return Object.assign({ verdict: 'voice' }, info); } if (kind === 'dhI') { // Buzzy TH: a soft start. D starts with a sudden burst; Z starts with a loud hiss. const pre = hissNear(F, best.s - 1, -1, floor); if (pre.length >= 3) { const top = pre.map(f => f.db).sort((a, b) => b - a).slice(0, 3); const rel = top.reduce((a, b) => a + b, 0) / top.length - vpk; const cen = pre.reduce((a, f) => a + f.cen, 0) / pre.length; info.rel = rel; info.cen = cen; if (rel > -16 && cen > 4000) return Object.assign({ verdict: 'sharp' }, info); } // Rise time from first sound to near the vowel's peak. let on = best.s; while (on > 0 && F[on - 1].db > floor + 8) on--; let r = on; while (r < F.length && F[r].db < vpk - 8) r++; info.rise = (r - on) * hop; let jump = 0; for (let i = Math.max(1, on - 1); i <= Math.min(r, F.length - 1); i++) jump = Math.max(jump, F[i].db - F[i - 1].db); info.jump = jump; info.onHigh = F[on].high / (F[on].low + 1e-12); info.onDb = F[on].db - vpk; const quietBefore = on > 0 && F[on - 1].db < floor + 6; return Object.assign({ verdict: 'voice' }, info); } const fr = kind === 'F' ? hissNear(F, best.e + 1, 1, floor) : hissNear(F, best.s - 1, -1, floor); info.hissMs = Math.round(fr.length * hop); if (fr.length) { const chron = kind === 'F' ? fr : fr.slice().reverse(), mx = Math.max(...fr.map(f => f.db)); info.attack = Math.max(chron[0].db, (chron[1] || chron[0]).db) - mx; info.mean = fr.reduce((a, f) => a + f.db, 0) / fr.length - vpk; info.flat = fr.reduce((a, f) => a + f.flat, 0) / fr.length; } if (fr.length * hop < 40) return Object.assign({ verdict: 'pop' }, info); const top = fr.map(f => f.db).sort((a, b) => b - a).slice(0, 3); const rel = top.reduce((a, b) => a + b, 0) / top.length - vpk; const cen = fr.reduce((a, f) => a + f.cen, 0) / fr.length; const crest = fr.reduce((a, f) => a + f.crest, 0) / fr.length; Object.assign(info, { rel, cen, crest }); // A T or D: a loud puff that starts all at once and is over quickly. if (kind === 'I' && info.mean > -13.5 && info.attack > -5 && info.hissMs <= 120) return Object.assign({ verdict: 'pop' }, info); if (kind === 'I' && info.hissMs <= 65 && info.attack > -3.5) return Object.assign({ verdict: 'pop' }, info); // An S: a narrow, sharp hiss (low flatness) that is fairly loud next to the vowel. if (info.flat < 0.25 && info.mean > -18.5) return Object.assign({ verdict: 'sharp' }, info); if (info.mean > -11) return Object.assign({ verdict: 'sharp' }, info); if (info.mean > -20.5 && cen > 8000 && crest >= 10) return Object.assign({ verdict: 'sharp' }, info); return Object.assign({ verdict: 'soft' }, info); } // Live sound for the blowing game: is this frame a soft TH breeze, a snake hiss or a voice? function liveKind(f, floor) { if (f.db < floor + 9) return 'none'; const hissy = f.high > f.low && f.zcr > 0.15; if (hissy) return (f.flat < 0.25 && f.db > floor + 22) ? 'sharp' : 'soft'; if (f.low > f.high && f.zcr < 0.18) return (f.high > f.low * 0.05 || f.mid > f.low * 0.3) ? 'buzz' : 'voice'; return 'none'; } G.SoundEar = { features, analyze, frame, frameSize, liveKind }; })(typeof window !== 'undefined' ? window : globalThis);