# chandailrc/talkingheadrc architecture

> Architecture diagram of the GitHub repository chandailrc/talkingheadrc, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-22). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/chandailrc/talkingheadrc
- Repository: https://github.com/chandailrc/talkingheadrc

## Overview

##### Project type and purpose
- This is a client-side JavaScript 3D avatar library (“TalkingHead”) plus a demo/web-app (index.html and examples/) showcasing how to integrate it with various external AI and TTS services.
- Purpose: render a 3D avatar in the browser (ThreeJS/WebGL), fetch synthesized speech (visemes + audio), and drive real-time lip-sync and animations.

##### File structure
Top-level:
- modules/ – ES‐module files: talkinghead.mjs core class and lipsync-xx language modules
- index.html – full demo/test web-app integrating all features
- examples/ – minimal usage samples for hobbyists (static HTML/JS)
- animations/, poses/, avatars/, audio/, views/, fonts/ – static assets consumed by the library/demo
- siteconfig.js – configuration for the demo app (i18n, background media, API endpoints)
- eslint.config.mjs, jsconfig.json – linting/IDE support
- README.md – description, usage, architecture, proxy setup, appendices

##### Extracted architecture insights from README
- Core class: TalkingHead – loads GLB avatar, plays FBX Mixamo animations, manages camera & lighting (ThreeJS).
- Lip-sync modules (lipsync-en/fi/lt) transform text → visemes.
- speakText() uses Google TTS via a proxy endpoint to get word timestamps.
- speakAudio() supports Azure/Elevenlabs via WebSocket.
- The demo app (index.html) configures API proxies for OpenAI, Azure, ElevenLabs, Google TTS, Vertex AI, secured by JWT SSO.

##### Components
a. Main components:
1. Browser-based Web Client
2. TalkingHead JS Library (talkinghead.mjs)
– LipSync Modules (modules/lipsync-xx.mjs)
– ThreeJS Renderer & Scene Manager
– Animation Controller (Mixamo FBX loader)
– Audio Manager & Viseme Scheduler
3. Static Asset Store (avatars/, animations/, poses/, audio/, views/, fonts/)
4. Demo Web App (index.html + siteconfig.js + examples/)
5. Proxy Layer (Node or Apache CGI)
– JWT Auth Service
– REST/WebSocket Proxies for:
- Google TTS API
- Microsoft Azure Speech SDK
- ElevenLabs WebSocket TTS
- OpenAI API (Chat, Whisper, Moderation)
- Vertex AI (Gemini)
6. External AI/TTS Services
– Google Cloud TTS
– Microsoft Speech Services
– ElevenLabs
– OpenAI / Whisper / ChatGPT
– Vertex AI / Gemini

b. Relationships & data flows:
1. User interacts with demo UI → Web Client
2. Web Client loads TalkingHead library (ES modules)
3. TalkingHead fetches 3D model & assets from Static Asset Store
4. Browser calls speakText()/speakAudio()
5. Audio Manager forwards text/audio request to Proxy Layer
6. Proxy authenticates via JWT and routes to External Service
7. External Service returns audio + word-timestamp or viseme stream
9. ThreeJS renderer drives WebGL animation in real time
10. Demo UI displays subtitles (Marked markdown parser) and stats

c. Architectural patterns & design principles:
- Modular ES‐module design: core + pluggable lip-sync modules
- Separation of concerns: rendering vs. audio processing vs. networking
- Proxy pattern for service integration and centralized auth (JWT)
- Asynchronous pipelines: Promises/async‐await for asset loading and TTS/network calls
- Extensibility: new lip-sync languages or TTS services can be added via modules and proxies

d. Key technologies & libraries:
- ThreeJS (WebGL rendering)
- Marked + DOMPurify (subtitles/Markdown)
- Mixamo FBX loader (animations/poses)
- Web Audio API (audio playback, viseme sync)
- ES Modules (talkinghead.mjs, lipsync-xx.mjs)
- JWT + Apache/NGINX proxy for secured service integration

##### Final remarks
- Essential elements but avoid clutter: group similar services (e.g., OpenAI APIs)
- Error path / fallback (e.g., missing visemes → default lip-sync) if relevant
- Top: User & UI
- Middle: Core Library & Renderer
- Bottom left: Asset Store
- Bottom right: Proxy Layer & External Services

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Top Layer: User and UI
    subgraph "Web Client" 
        direction TB
        User["User"]:::frontend
        UI["Demo UI (index.html)"]:::frontend
        Examples["Examples/"]:::frontend
    end

    SiteConfig["siteconfig.js"]:::frontend

    %% Middle Layer: Core Library
    subgraph "TalkingHead Library" 
        direction TB
        Core["TalkingHead Core"]:::frontend
        subgraph "LipSync Modules"
            direction TB
            LS_EN["lipsync-en.mjs"]:::frontend
            LS_FI["lipsync-fi.mjs"]:::frontend
            LS_LT["lipsync-lt.mjs"]:::frontend
        end
        Renderer["ThreeJS Renderer & Scene Manager"]:::frontend
        AnimCtrl["Animation Controller"]:::frontend
        AudioMgr["Audio Manager & Viseme Scheduler"]:::frontend
    end

    %% Bottom Left: Static Asset Store
    subgraph "Asset Store (HTTP/CDN)"
        direction TB
        Avatars["avatars/"]:::asset
        Animations["animations/"]:::asset
        Poses["poses/"]:::asset
        AudioAssets["audio/"]:::asset
        Views["views/"]:::asset
        Fonts["fonts/"]:::asset
    end

    %% Bottom Right: Proxy & External Services
    subgraph "Proxy Layer" 
        direction TB
        Auth["JWT Auth Service"]:::proxy
        REST["REST Proxy"]:::proxy
        WS["WebSocket Proxy"]:::proxy
    end

    subgraph "External AI/TTS Services"
        direction TB
        GoogleTTS["Google Cloud TTS"]:::external
        Azure["Microsoft Azure Speech"]:::external
        Eleven["ElevenLabs TTS"]:::external
        OpenAI["OpenAI API"]:::external
        Vertex["Vertex AI (Gemini)"]:::external
    end

    %% Connections
    User -->|"interacts"| UI
    UI -->|"loads"| Core
    UI -->|"uses config"| SiteConfig
    UI -->|"loads samples"| Examples

    Core -->|"loads model"| Avatars
    Core --> Animations
    Core --> Poses
    Core --> AudioAssets
    Core --> Views
    Core --> Fonts

    Core -->|"speakText()/speakAudio()"| REST
    REST --> Auth
    Auth --> REST
    REST --> GoogleTTS
    REST --> Azure
    REST --> Eleven
    REST --> OpenAI
    WS --> Eleven
    WS --> Azure
    REST --> Vertex

    GoogleTTS -->|audio+timestamps| REST
    Azure -->|audio stream| WS
    Eleven -->|audio stream| WS
    OpenAI -->|chat/whisper| REST
    Vertex -->|responses| REST

    REST -->|"response"| Core
    WS -->|"response"| Core

    Core -->|"schedule visemes"| Renderer
    Renderer -->|"renders frames"| UI
    Core -->|"generate subtitles"| UI

    %% Click Events
    click UI "https://github.com/chandailrc/talkingheadrc/blob/main/index.html"
    click Examples "https://github.com/chandailrc/talkingheadrc/tree/main/examples/"
    click SiteConfig "https://github.com/chandailrc/talkingheadrc/blob/main/siteconfig.js"
    click Core "https://github.com/chandailrc/talkingheadrc/blob/main/modules/talkinghead.mjs"
    click LS_EN "https://github.com/chandailrc/talkingheadrc/blob/main/modules/lipsync-en.mjs"
    click LS_FI "https://github.com/chandailrc/talkingheadrc/blob/main/modules/lipsync-fi.mjs"
    click LS_LT "https://github.com/chandailrc/talkingheadrc/blob/main/modules/lipsync-lt.mjs"
    click Avatars "https://github.com/chandailrc/talkingheadrc/tree/main/avatars/"
    click Animations "https://github.com/chandailrc/talkingheadrc/tree/main/animations/"
    click Poses "https://github.com/chandailrc/talkingheadrc/tree/main/poses/"
    click AudioAssets "https://github.com/chandailrc/talkingheadrc/tree/main/audio/"
    click Views "https://github.com/chandailrc/talkingheadrc/tree/main/views/"
    click Fonts "https://github.com/chandailrc/talkingheadrc/tree/main/fonts/"

    %% Styles
    classDef frontend fill:#cce5ff,stroke:#1f77b4,color:#034e7b;
    classDef asset fill:#ffe5cc,stroke:#ff7f0e,color:#8c2d04,shape:cylinder;
    classDef proxy fill:#e0e0e0,stroke:#7f7f7f,color:#4d4d4d;
    classDef external fill:#e6ffcc,stroke:#2ca02c,color:#0a610a,shape:hexagon;
```
