APTalkmoji Gemini Live API Avatar
Docs›SDK Introduction

Gemini Live API Avatar

The default voice engine. Gemini Live opens a persistent WebSocket directly from the browser to Google's Generative Language service, giving you sub-300ms speech-to-speech latency with full lip sync.

How the WebSocket works

Unlike traditional REST-based TTS pipelines, Gemini Live maintains a bidirectional streaming connection between the browser and the Gemini service:

  1. The SDK opens a WebSocket to wss://generativelanguage.googleapis.com/ws/google.ai.generativelanguage.v1beta.GenerativeService.BidiGenerateContent
  2. A BidiGenerateContentSetup message configures the model, voice, system prompt, and knowledge context
  3. The user's microphone audio is captured at 16 kHz PCM, chunked into 100ms frames, and streamed as BidiGenerateContentRealtimeInput messages
  4. Gemini returns BidiGenerateContentServerContent messages containing 24 kHz PCM audio chunks
  5. Audio chunks are queued into the Web Audio API's destination node for seamless playback
  6. In parallel, the FFT analyser reads amplitude data at 50 Hz and the phoneme scheduler drives the Rive lip-sync inputs
Browser mic (16kHz PCM)
    │
    ▼
WebSocket ──────────────────────► Gemini Live
                                       │
    ◄──────────────────────────── PCM audio (24kHz)
    │
    ├─ Web Audio API → speaker
    └─ FFT analyser → lip-sync controller → Rive inputs

PUBLIC_GEMINI_API_KEY requirement

Because the WebSocket is opened directly from the browser, the Gemini API key is exposed to the client. You should use a separate, restricted key for this purpose:

# .env
PUBLIC_GEMINI_API_KEY=AIza...  # browser-safe key (set quotas + referrer restrictions)
GEMINI_API_KEY=AIza...         # server-side key for embeddings and async /ask

The server returns PUBLIC_GEMINI_API_KEY in the GET /embed/:publicId/config response, and the embed widget uses this key to authenticate the WebSocket. It never falls back to GEMINI_API_KEY — the server key stays server-side. If PUBLIC_GEMINI_API_KEY is unset (or set equal to the server key), the config response carries no key and the widget runs in text-only mode, answering typed questions through the server-side /ask path instead.

⚠️

In production: Create a separate API key in Google AI Studio and restrict it to the generativelanguage.googleapis.com service with HTTP referrer restrictions matching your embed domains.

Audio pipeline

Input (microphone → Gemini)

  • Sample rate: 16 kHz, mono, 16-bit PCM
  • Chunk size: ~100ms frames (1600 samples)
  • Captured via getUserMedia → AudioWorkletNode or ScriptProcessorNode
  • Resampled from the device's native sample rate if needed

Output (Gemini → speaker + lip sync)

  • Sample rate: 24 kHz, mono, 16-bit PCM
  • Decoded via AudioContext.decodeAudioData or directly buffered into an AudioBufferSourceNode
  • Scheduled at audioContext.currentTime + bufferOffset to prevent gaps
  • Simultaneously fed to an AnalyserNode (FFT size 512) for amplitude data

Viseme → Rive animation pipeline

The lip-sync controller operates in two modes simultaneously:

Amplitude mode (always active)

Every 20ms (50 Hz), the analyser reads 512 frequency bins. The RMS amplitude of the speech band (200–3400 Hz) controls a continuous mouth-open value, providing smooth movement even between phoneme transitions.

Phoneme schedule mode (overlaid)

The SDK estimates phoneme timing from the audio using a scheduler that fires at PHONEMES_PER_SEC = 13.5 phonemes per second. Each phoneme maps to one of 23 Azure-style viseme IDs, which in turn map to one of 23 Rive number inputs (inputs 100–122).

Audio timing
    │
    ▼
Phoneme scheduler
    │ fires 13.5x/sec
    ▼
Azure viseme ID (0–22)
    │
    ▼
Rive input number (100–122)
    │
    ▼
Rive state machine → character mouth pose

Supported languages

Gemini Live auto-detects the speaker's language. The following are confirmed to produce accurate lip sync:

LanguageCodeRTL
EnglishenNo
SpanishesNo
FrenchfrNo
GermandeNo
PortugueseptNo
JapanesejaNo
KoreankoNo
Chinese (Mandarin)zhNo
ArabicarYes ✓
HebrewheYes ✓
PersianfaYes ✓
UrduurYes ✓

RTL languages are fully supported — the widget panel and text direction flip automatically based on the detected script.

Model configuration

The default model is gemini-3.8-live. When using the SDK directly you can override it with the model option.

// Available models (check Google AI Studio for latest list)
// gemini-3.8-live                — stable, lowest latency, recommended
// gemini-3.1-flash-live-preview  — legacy preview
💡

See Natural Lip Sync for details on how to tune the viseme timing, overlap, and amplitude sensitivity.