Implementation:23blocks OS Ai maestro Speech Level
| Knowledge Sources | |
|---|---|
| Domains | Audio, Animation |
| Last Updated | 2026-10-26 18:00 GMT |
Overview
Concrete tool for computing speech energy from a Web Audio AnalyserNode (RMS with noise gate) or a synthetic envelope fallback, DOM-free for testing, provided by AI Maestro's `lib/speech-level.ts`.
Description
The speech-level module converts speech energy to a 0..1 number for driving avatar mouth animation. Two sources: `analysed` (a Web Audio AnalyserNode tapping the real `<audio>` element — the mouth moves with the actual waveform) and `synthetic` (a plausible envelope generated from nothing, used when the provider cannot supply audio, like `web-speech`). The mode is reported, not hidden, because a synthetic mouth is an animation and an analysed one is a measurement. `rms` computes root-mean-square of time-domain samples (tracks perceived loudness, not peak which twitches). `levelFromRms` applies a noise gate (`SPEECH_NOISE_FLOOR` 0.012), gain, and soft knee — without the gate, room tone makes the mouth hum during silence. `syntheticLevel` generates a plausible envelope from elapsed time. DOM-free maths live here; React wiring is in `hooks/useSpeechLevel.ts`.
Usage
Call `rms(samples)` on AnalyserNode float output. Call `levelFromRms(rmsValue)` to map to 0..1. Call `syntheticLevel(elapsedMs, durationMs)` for the fallback.
Code Reference
Source Location
- Repository: ai-maestro
- File: lib/speech-level.ts
- Lines: L1-L86
Signature
export type SpeechLevelMode = 'analysed' | 'synthetic' | 'silent'
export const SPEECH_NOISE_FLOOR = 0.012
export function rms(samples: ArrayLike<number>): number
export function levelFromRms(rmsValue: number): number
export function syntheticLevel(elapsedMs: number, durationMs?: number): number
Import
import { rms, levelFromRms, syntheticLevel, type SpeechLevelMode } from '@/lib/speech-level'
I/O Contract
| Name | Type | Description |
|---|---|---|
| rms() returns | number | RMS of samples (0..1) |
| levelFromRms() returns | number | 0..1 mouth-open value |
| syntheticLevel() returns | number | 0..1 synthetic envelope |
Usage Examples
import { rms, levelFromRms } from '@/lib/speech-level'
// In a rAF loop with an AnalyserNode
analyser.getFloatTimeDomainData(buffer)
const energy = levelFromRms(rms(buffer))
// Drive mouth: mouthOpen = energy