# Text to emote

A sentence in, an emote out: `sona_emote_from_text("wave excitedly", &e)` picks [stock clips](clips.md)
with blend weights, a playback speed and a face, and `sona_avatar_emote` plays them. It is part of the core library
(`sona.h`), with no ctx and no network: the same text always gives the same answer. Agents get the
same through MCP's `emote_from_text` tool.

```c
sona_emote e;
memset(&e, 0, sizeof e);
e.size = sizeof e;
sona_emote_from_text("wave excitedly", &e);   /* wave 74%, celebrate 26%, speed 1.25, a happy face */
if (e.count || e.expression[0] != 255)
  sona_avatar_emote(avatar, &e, 0.25f);        /* fades in over a quarter second */
```

```cpp
sona_emote e;
sona::emote_from_text("wave excitedly", e);   // wave 74%, celebrate 26%, speed 1.25, a happy face
if (e.count || e.expression[0] != 255)
  avatar.emote(e, 0.25f);                     // fades in over a quarter second
```

```csharp
var e = new Native.sona_emote { size = (uint)sizeof(Native.sona_emote) };
fixed (byte* text = System.Text.Encoding.UTF8.GetBytes("wave excitedly\0"))
    Native.sona_emote_from_text(text, &e);    // wave 74%, celebrate 26%, speed 1.25, a happy face
if (e.count > 0 || e.expression[0] != 255)
    Native.sona_avatar_emote(avatar.Handle, &e, 0.25f);   // fades in over a quarter second
```

```rust
let e = emote_from_text("wave excitedly")?;   // wave 74%, celebrate 26%, speed 1.25, a happy face
if e.count > 0 || e.expression[0] != 255 {
    avatar.emote(&e, 0.25)?;                   // fades in over a quarter second
}
```

```python
e = sona.emote_from_text("wave excitedly")   # wave 74%, celebrate 26%, speed 1.25, a happy face
avatar.emote(e, 0.25)                         # fades in over a quarter second
```

## How a sentence is read

Words are matched against a table of 132 action and mood words and 20 manners, after taking off -ing, -ed and -s
(`waving`, `clapped`, `laughs`):

- **Actions** name a clip: wave, hello, bye, clap, cheer, celebrate, raise your arms, fist pump, pick me, laugh,
  angry, shrug, not sure, cry, shocked, wow, yawn, sleep, stretch, dance, spin, jump, look around, look up, punch,
  kick, sit, faint, stumble, talk and more. Two-word actions ("look around", "not sure") may have a word between
  them ("raise your arms").
- **Moods** give the face: happy, smile, frown and the moods the actions carry (laugh is laughing, cry is sad).
- **Manners** change an action: excitedly (faster, with some celebrate in it), slowly and gently (slower), sadly
  and angrily (the mood, and a little of that clip), nervously and awkwardly. "!" makes it a little faster.
- **Very**, **really** and **so** make the next action count for more; **not**, **don't** and **never** turn the
  next action within three words off ("don't dance" picks nothing).

The four heaviest clips are kept, their weights summing to 1. `confidence` is how sure the strongest word makes it
(0.5 and up for a plain action word). Nothing matched: `count` 0 and `confidence` 0. A mood with no action ("smile")
gives `count` 0 with a face.

Clips made for one body (`laugh_female`, `laugh_male`) are named once; `sona_avatar_emote` plays the avatar's own
body's variant, as `sona_avatar_play_stock` does.

## Playing it

`sona_avatar_emote` blends the clips on the emote layers, the top ones (`SONA_LAYER_MAX - count` to
`SONA_LAYER_MAX - 1`, so layers 4 to 7 at most): each layer blends over the ones below by its share of the weight so
far, which gives every clip its own weight in the end. Layers below 4 stay the game's. An emote never stops a
layer the game played: when one sits where the emote would go, `sona_avatar_emote` answers `SONA_E_LIMIT` and
changes nothing, and an emote that needs fewer layers leaves the game's lower ones playing. A layer the emote played
is the game's again once the game plays anything on it, even the same clip. Out of memory, the call answers
`SONA_E_MEMORY` and changes nothing: the clips and layers are held before any layer or the face changes. An emote
plays once and hands back to the clip, except what goes on until stopped (a dance, sleep, a hover, a run), which loops until the next
emote. The face, when the emote has one, stays until the next emote; an emote with `count` 0 and no face stops the
emote layers and hands the face back to the clip.

## Faces on the web

The web SDK has no text-to-emote. Its clips carry their faces: a laugh opens the mouth and closes the eyes, a shock
raises the eyebrows. The bust framing shows them close, with room for the head to move:

```js
import { SonaRenderer } from './sona-renderer.js';

const canvas = document.querySelector('canvas');
const sona = await SonaRenderer.create({ canvas });
const avatar = sona.add(manifestBase64, { clip: 'Laugh' });
await avatar.ready;
sona.camera.preset('bust', avatar);
await new Promise((done) => setTimeout(done, 3000));
for (const clip of ['ShockedSurprised', 'Angry', 'SadCry']) {
  await avatar.play(clip);
  await new Promise((done) => setTimeout(done, 3000));
}
```
## MCP

`emote_from_text {text, body?}` answers with the clips (name, family, clip id, caption, weight), the speed, the
confidence and the face (its mood and shape numbers). With `body` set to male or female the clip names are that
body's variants. `render_avatar` with a clip's id as `clip` shows a frame of it.
