Natural Lip Sync
Talkmoji uses a hybrid amplitude + phoneme controller to drive Rive mouth shapes in real time. The result is smooth, expressive lip sync that works for any voice engine without requiring phoneme data from the TTS service.
Architecture overview
The lip-sync system lives entirely inside public/lipsync-sdk.js's LipsyncAvatar class — the audio scheduling, phoneme timing, and viseme lookup are internal to that class rather than split across separate files. LipsyncAvatar runs continuously while audio is playing, driving the active Rive number input from either the phoneme queue (timed-ramp mode, see below) or, when the queue runs dry but audio is still audible, straight from playback amplitude (amplitudeSensitivity, see "Amplitude mode" below) — the "hybrid" in hybrid amplitude + phoneme controller.
How viseme mapping works
The 23 viseme slots
Talkmoji uses Azure's 22 viseme IDs (0 = silence + 1–22 for phoneme groups) as an intermediate representation. Each viseme ID maps to one Rive number input (inputs 100–122):
| Viseme ID | Phoneme group | Rive input | Example sounds |
|---|---|---|---|
| 0 | silence | 100 | pause, breath |
| 1 | aa | 101 | "father" |
| 2 | aw | 102 | "caught" |
| 3 | o | 110 | "boat" |
| 4 | ey | 103 | "say" |
| 5 | er | 113 | "her" |
| 6 | ih | 105 | "sit" |
| 7 | uw | 104 | "you" |
| 21 | p_b_m | 107 | "pot", "bat", "map" |
| 22 | ng | 122 | sing |
At any moment, at most one Rive input is non-zero. What the SDK writes into that input depends on visemeSpeedMode — see "Drive modes" next.
Drive modes
On the platform's characters, a mouth input's value is not "how far open". It is the transition speed into that mouth pose: setting input 110 to 10 makes the mouth ease into the O shape slowly, setting it to 100 changes to it instantaneously, and Rive itself animates the in-between frames of a switch from one pose to the next. The SDK has three ways of driving that:
visemeSpeedMode | What is written to the active input | Use when |
|---|---|---|
'rive-transition' (default) |
One speed value per phoneme, sized from the phoneme's spoken duration: short consonants get a high value (fast), long vowels and pauses a low one (slow). The value is pushed to Rive as-is, and the previous input is released to 0 at the switch — Rive's own transition produces the crossover mouth shape. | Characters whose inputs behave as described above (all stock characters). |
'timed-ramp' |
The value ramps 1 → 100 across the phoneme, with a per-frame glide (visemeSmoothingMs) and a short overlap into the next phoneme (visemeOverlapMs). |
Custom characters whose input value means "how far open". On a speed-driven character this reads as "crawl, then snap at the end of every phoneme". |
'instant' |
visemeMaxValue straight away. |
Debugging the viseme map. |
In 'rive-transition' the auto speed is 4800 / durationMs × visemeTransitionScale, clamped to [visemeTransitionMinValue, visemeTransitionMaxValue]:
| Phoneme duration | Speed written | Typical sound |
|---|---|---|
| ≤ 48 ms | 100 (instant) | very fast consonant |
| 60 ms | 80 | t / d / k |
| 120 ms | 40 | ordinary vowel |
| 200 ms | 24 | stressed vowel |
| 300 ms | 16 | gap between words |
| ≥ 600 ms | 8 (visemeTransitionMinValue) | sentence pause, end of speech |
Tuning: turn visemeTransitionScale first — above 1 if the mouth trails the audio, below 1 if it looks too snappy. Set visemeTransitionValue to a number (e.g. 30) to write one uniform speed for every phoneme instead of the auto curve.
Amplitude mode
The Web Audio AnalyserNode runs at 50 Hz (every 20ms) with an FFT size of 512, giving 256 frequency bins. The RMS power of the speech-frequency band (200–3400 Hz) drives a continuous jaw-open value:
// From lipsync-sdk.js's LipsyncAvatar class — simplified
const fftData = new Float32Array(analyser.frequencyBinCount); // 256 bins
analyser.getFloatFrequencyData(fftData);
// Speech band: bins 3–43 at 48kHz (roughly 200–3400 Hz)
const rms = Math.sqrt(fftData.slice(3, 43).reduce((s, v) => s + v*v, 0) / 40);
const jawOpen = Math.min(1, rms * amplitudeSensitivity);
This value modulates the active viseme's Rive input in the range [visemeMinValue, visemeMaxValue], ensuring the mouth opens proportionally to loudness even when no discrete phoneme event is scheduled.
Phoneme schedule mode
The scheduler estimates when phonemes occur based on audio playback timing. It fires at PHONEMES_PER_SEC = 13.5 phonemes per second — approximately the average English speech rate.
Each phoneme event:
- Determines the next viseme ID by cycling through a plausible phoneme sequence
- Writes the Rive input per the drive mode: one duration-derived speed (
'rive-transition'), or avisemeMinValue→visemeMaxValueramp over the phoneme duration ('timed-ramp') - Triggers silence (input 100) for
SILENCE_PAUSE_MS = 130msbetween words — in'rive-transition'a pause's length sets how softly the mouth closes - In
'timed-ramp'only, appliesvisemeOverlapMs = 18msof co-articulation overlap between adjacent phonemes
Phoneme-based vs amplitude-based
| Amplitude-based | Phoneme-based | |
|---|---|---|
| Data source | Real-time FFT of audio output | Scheduled events based on audio timing |
| Accuracy | Approximate — jaw movement only | Better — includes consonants, lip closure, rounding |
| Latency | Zero — real-time analysis | ~20ms anticipation pre-roll (anticipationMs) |
| Use case | Fallback, low-CPU mode | Primary mode (phoneme schedule) |
Both run together: the phoneme scheduler selects which Rive input is active and sets its value; the amplitude controller only takes over while audio is audible but no phoneme is scheduled yet (the gap before the first transcript delta). In 'rive-transition' loudness then sets how briskly the jaw opens, and quiet audio rests the mouth.
Customising viseme weights
You can remap any Azure viseme ID to a different Rive input by passing riveInputMap to the LipsyncAvatar constructor:
const avatar = new LipsyncAvatar({
container: '#chat',
riveSrc: '/path/to/your-character.riv',
apiKey: config.apiKey,
// Override specific viseme → Rive input mappings
riveInputMap: {
0: 100, // silence → input 100 (closed)
1: 101, // aa vowel → input 101 (wide open)
21: 107, // p/b/m → input 107 (lip closure)
},
// Drive mode (see "Drive modes")
visemeSpeedMode: 'rive-transition', // 'rive-transition' | 'timed-ramp' | 'instant'
visemeTransitionValue: 'auto', // or a fixed speed 1–100 for every phoneme
visemeTransitionMinValue: 8, // slowest auto speed (long vowels, pauses)
visemeTransitionMaxValue: 100, // fastest auto speed (short consonants)
visemeTransitionScale: 1.0, // >1 snappier, <1 softer — the first knob to try
// 'timed-ramp' mode only
visemeMinValue: 1, // Rive value at phoneme start (1–99)
visemeMaxValue: 100, // Rive value at phoneme peak (visemeMinValue–100)
visemePeakRatio: 0.88, // fraction of phoneme duration at which peak is reached
visemeOverlapMs: 18, // co-articulation overlap between phonemes (ms)
visemeSmoothingMs: 45, // per-frame glide time constant (ms)
// Amplitude
amplitudeSensitivity: 1.0, // multiply FFT amplitude (0.5 = quieter, 2.0 = more exaggerated)
// Timing
anticipationMs: 20, // pre-roll: mouth starts moving N ms before phoneme
mouthDelayMs: 0, // positive = delay mouth (audio arrives late)
});
Rive input reference
Each Rive character file uses number inputs 100–122 for mouth poses. Input 100 is the closed/silent pose. Inputs 101–122 correspond to the 22 phoneme groups in the Azure viseme set. Setting an input to 0 deactivates it (no pull toward that pose); any value 1–100 moves the mouth into the pose at that speed — 10 eases in slowly, 100 changes instantly.
If you're designing your own .riv file, use the same input numbering (100–122) to ensure compatibility with the SDK's phoneme scheduler out of the box.