# C API: Lip sync and voice mouths

Mouth shapes from audio, from timed phonemes or from text, and cheap mouths that move with a voice's loudness: [Lip sync](../../guides/lipsync.md).

## sona_lipsync_defaults

```c
void sona_lipsync_defaults(sona_lipsync_options *o);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `o` | `sona_lipsync_options *` | The [`sona_lipsync_options`](../fields.md#sona_lipsync_options) to fill with the defaults | required |

**Returns** Nothing.

**Example**:

```c
sona_lipsync_options x_lipsync_options;
memset(&x_lipsync_options, 0, sizeof x_lipsync_options);
x_lipsync_options.size = sizeof x_lipsync_options;
sona_lipsync_defaults(&x_lipsync_options);
```

`size` set; `sample_rate` 48000, `channels` 1, `hold_seconds` 0.07, `attack_seconds` 0.02, `release_seconds` 0.07, `gate_db` -55, `lead_seconds` 0.04.

## sona_lipsync_create

```c
sona_result sona_lipsync_create(const sona_lipsync_options *o, const sona_allocator *al, sona_lipsync **out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `o` | `const sona_lipsync_options *` | The options, a [`sona_lipsync_options`](../fields.md#sona_lipsync_options) | `NULL`: the defaults |
| `al` | `const sona_allocator *` | A [`sona_allocator`](../fields.md#sona_allocator) | required |
| `out` | `sona_lipsync **` | Receives the new lip sync analyser | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_MEMORY` ([what each means](../../troubleshooting.md#error-codes)).

NULL options: the defaults. al NULL: malloc. `SONA_E_ARGUMENT` for a size, rate, channel count, format or time out of range.

## sona_lipsync_destroy

```c
void sona_lipsync_destroy(sona_lipsync *ls);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ls` | `sona_lipsync *` | The lip sync analyser | required |

**Returns** Nothing.

**Example**:

```c
sona_lipsync_destroy(ls);
```

Frees the analyser.

## sona_lipsync_reset

```c
void sona_lipsync_reset(sona_lipsync *ls);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ls` | `sona_lipsync *` | The lip sync analyser | required |

**Returns** Nothing.

**Example**:

```c
sona_lipsync_reset(ls);
```

Back to time 0: hints and frames waiting to be read are dropped.

## sona_lipsync_feed

```c
sona_result sona_lipsync_feed(sona_lipsync *ls, const void *pcm, size_t frames);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ls` | `sona_lipsync *` | The lip sync analyser | required |
| `pcm` | `const void *` | Audio samples, interleaved, in the format the options name | required |
| `frames` | `size_t` | How many sample frames `pcm` holds | - |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT` ([what each means](../../troubleshooting.md#error-codes)).

`frames`: samples per channel. Analyses every 10 ms that is complete.

## sona_lipsync_read

```c
uint32_t sona_lipsync_read(sona_lipsync *ls, sona_lipsync_frame *out, uint32_t max);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ls` | `sona_lipsync *` | The lip sync analyser | required |
| `out` | `sona_lipsync_frame *` | Receives the [`sona_lipsync_frame`](../fields.md#sona_lipsync_frame) | required |
| `max` | `uint32_t` | How many entries `out` holds | - |

**Returns** `uint32_t`.

**Example**:

```c
sona_lipsync_frame x_lipsync_frame;
memset(&x_lipsync_frame, 0, sizeof x_lipsync_frame);
uint32_t x_got = sona_lipsync_read(ls, &x_lipsync_frame, 1);
printf("sona_lipsync_read: %lld\n", (long long)x_got);
```

Moves up to max frames made since the last read into out, oldest first; returns how many.

## sona_lipsync_current

```c
sona_result sona_lipsync_current(const sona_lipsync *ls, sona_lipsync_frame *out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ls` | `const sona_lipsync *` | The lip sync analyser | required |
| `out` | `sona_lipsync_frame *` | Receives the [`sona_lipsync_frame`](../fields.md#sona_lipsync_frame) | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT` ([what each means](../../troubleshooting.md#error-codes)).

**Example**:

```c
sona_lipsync_frame x_lipsync_frame;
memset(&x_lipsync_frame, 0, sizeof x_lipsync_frame);
sona_result result = sona_lipsync_current(ls, &x_lipsync_frame);
if (result != SONA_OK) printf("sona_lipsync_current: %s\n", sona_result_name(result));
```

The newest frame (a shut mouth before the first).

## sona_lipsync_latency

```c
double sona_lipsync_latency(const sona_lipsync *ls);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ls` | `const sona_lipsync *` | The lip sync analyser | required |

**Returns** `double`.

**Example**:

```c
double x_got = sona_lipsync_latency(ls);
printf("sona_lipsync_latency: %g\n", (double)x_got);
```

Seconds from a sample being fed to the first frame that hears it.

## sona_lipsync_hint

```c
sona_result sona_lipsync_hint(sona_lipsync *ls, const sona_phoneme *ph, uint32_t count);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ls` | `sona_lipsync *` | The lip sync analyser | required |
| `ph` | `const sona_phoneme *` | A [`sona_phoneme`](../fields.md#sona_phoneme) | required |
| `count` | `uint32_t` | How many entries the array holds | - |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT` ([what each means](../../troubleshooting.md#error-codes)).

**Example**:

```c
sona_phoneme x_phoneme;
memset(&x_phoneme, 0, sizeof x_phoneme);
sona_result result = sona_lipsync_hint(ls, &x_phoneme, 1);
if (result != SONA_OK) printf("sona_lipsync_hint: %s\n", sona_result_name(result));
```

Phonemes the host's TTS reported, on the fed audio's clock: their shapes win while the audio is sounding, and lips close for M, B and P. Up to 256 held; ones that ended a second ago go.

## sona_phoneme_mouth

```c
uint32_t sona_phoneme_mouth(const char *symbol);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `symbol` | `const char *` | A phoneme symbol | required |

**Returns** `uint32_t`.

**Example**:

```c
uint32_t x_got = sona_phoneme_mouth("example");
printf("sona_phoneme_mouth: %lld\n", (long long)x_got);
```

The mouth for a phoneme symbol; `SONA_MOUTH_UNKNOWN` when it is none of the sets.

## sona_lipsync_phonemes

```c
sona_result sona_lipsync_phonemes(const sona_phoneme *ph, uint32_t count, const sona_lipsync_options *o, sona_mouth_key *out, uint32_t cap, uint32_t *total);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `ph` | `const sona_phoneme *` | A [`sona_phoneme`](../fields.md#sona_phoneme) | required |
| `count` | `uint32_t` | How many entries the array holds | - |
| `o` | `const sona_lipsync_options *` | The options, a [`sona_lipsync_options`](../fields.md#sona_lipsync_options) | required |
| `out` | `sona_mouth_key *` | Receives the [`sona_mouth_key`](../fields.md#sona_mouth_key) | required |
| `cap` | `uint32_t` | How many entries `out` holds | - |
| `total` | `uint32_t *` | Receives how many there are in all; may be NULL | `NULL`: not written |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_MEMORY` ([what each means](../../troubleshooting.md#error-codes)).

Timed phonemes to keys (lead, closures kept, short shapes merged). *total is the count; writes up to cap. o may be NULL.

## sona_lipsync_text

```c
sona_result sona_lipsync_text(const char *text, const void *pcm, size_t frames, const sona_lipsync_options *o, sona_mouth_key *out, uint32_t cap, uint32_t *total);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `text` | `const char *` | The text to read | required |
| `pcm` | `const void *` | Audio samples, interleaved, in the format the options name | required |
| `frames` | `size_t` | How many sample frames `pcm` holds | - |
| `o` | `const sona_lipsync_options *` | The options, a [`sona_lipsync_options`](../fields.md#sona_lipsync_options) | required |
| `out` | `sona_mouth_key *` | Receives the [`sona_mouth_key`](../fields.md#sona_mouth_key) | required |
| `cap` | `uint32_t` | How many entries `out` holds | - |
| `total` | `uint32_t *` | Receives how many there are in all; may be NULL | `NULL`: not written |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_LIMIT`, `SONA_E_MEMORY` ([what each means](../../troubleshooting.md#error-codes)).

Text to keys. With audio (frames > 0, in o's format) the text is aligned to it; without, an even pace. `SONA_E_LIMIT` past 4096 letters or the 10 minutes limit of audio.

## sona_mouth_keys_sample

```c
sona_result sona_mouth_keys_sample(const sona_mouth_key *keys, uint32_t count, double t, sona_lipsync_frame *out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `keys` | `const sona_mouth_key *` | A [`sona_mouth_key`](../fields.md#sona_mouth_key) | required |
| `count` | `uint32_t` | How many entries the array holds | - |
| `t` | `double` | The time to sample, seconds | - |
| `out` | `sona_lipsync_frame *` | Receives the [`sona_lipsync_frame`](../fields.md#sona_lipsync_frame) | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT` ([what each means](../../troubleshooting.md#error-codes)).

**Example**:

```c
sona_mouth_key x_mouth_key;
memset(&x_mouth_key, 0, sizeof x_mouth_key);
sona_lipsync_frame x_lipsync_frame2;
memset(&x_lipsync_frame2, 0, sizeof x_lipsync_frame2);
sona_result result = sona_mouth_keys_sample(&x_mouth_key, 1, 0.5f, &x_lipsync_frame2);
if (result != SONA_OK) printf("sona_mouth_keys_sample: %s\n", sona_result_name(result));
```

The mouth at time t of a key list, eased between keys.

## sona_avatar_mouth

```c
sona_result sona_avatar_mouth(sona_avatar *a, uint32_t shape);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `a` | `sona_avatar *` | The avatar | required |
| `shape` | `uint32_t` | A `SONA_MOUTH_*` shape | - |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_DESTROYED` ([what each means](../../troubleshooting.md#error-codes)).

**Example**:

```c
sona_result result = sona_avatar_mouth(avatar, 0);
if (result != SONA_OK) printf("sona_avatar_mouth: %s\n", sona_result_name(result));
```

Only the mouth of the expression: a `SONA_MOUTH_`* or another shape index, or 255 to hand it back to the clip. The rest of the face keeps what `sona_avatar_expression` or the clip gave it. The same shape again does nothing.

## sona_voice_mouth_init

```c
void sona_voice_mouth_init(sona_voice_mouth *v, uint32_t seed);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `v` | `sona_voice_mouth *` | Receives the [`sona_voice_mouth`](../fields.md#sona_voice_mouth) | required |
| `seed` | `uint32_t` | The seed: the same seed gives the same result | - |

**Returns** Nothing.

**Example** (also in [Lip sync: a microphone or any audio drives the mouth](../../guides/lipsync.md)):

```c
sona_voice_mouth v[64];
for (i = 0; i < n; i++) sona_voice_mouth_init(&v[i], sona_avatar_id(avatars[i]));
/* each frame: */
uint32_t changed = sona_voice_mouths(avatars, v, levels, n, dt);
```

Sets `size` and starts the state from `seed`. Avatars with different seeds move their mouths differently to the same voice.

## sona_voice_mouth_step

```c
uint32_t sona_voice_mouth_step(sona_voice_mouth *v, float level, float dt, float *open);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `v` | `sona_voice_mouth *` | Receives the [`sona_voice_mouth`](../fields.md#sona_voice_mouth) | required |
| `level` | `float` | The audio level, linear RMS | - |
| `dt` | `float` | Seconds since the last call | - |
| `open` | `float *` | Receives how open the mouth is, 0 to 1 | required |

**Returns** `uint32_t`.

Advances by dt seconds; returns the `SONA_MOUTH_`* to show. open may be NULL.

## sona_avatar_voice

```c
sona_result sona_avatar_voice(sona_avatar *a, sona_voice_mouth *v, float level, float dt);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `a` | `sona_avatar *` | The avatar | required |
| `v` | `sona_voice_mouth *` | Receives the [`sona_voice_mouth`](../fields.md#sona_voice_mouth) | required |
| `level` | `float` | The audio level, linear RMS | - |
| `dt` | `float` | Seconds since the last call | - |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_DESTROYED` ([what each means](../../troubleshooting.md#error-codes)).

**Example**:

```c
sona_voice_mouth x_voice_mouth;
memset(&x_voice_mouth, 0, sizeof x_voice_mouth);
x_voice_mouth.size = sizeof x_voice_mouth;
sona_result result = sona_avatar_voice(avatar, &x_voice_mouth, 0, 1.0f / 60);
if (result != SONA_OK) printf("sona_avatar_voice: %s\n", sona_result_name(result));
```

`sona_voice_mouth_step` with the voice's level, put on the avatar's mouth with `sona_avatar_mouth`.

## sona_voice_mouths

```c
uint32_t sona_voice_mouths(sona_avatar *const *avatars, sona_voice_mouth *states, const float *levels, uint32_t count, float dt);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `avatars` | `sona_avatar *const *` | The avatars, `count` of them | required |
| `states` | `sona_voice_mouth *` | A mouth state per avatar | required |
| `levels` | `const float *` | An audio level per avatar, linear RMS | required |
| `count` | `uint32_t` | How many entries the array holds | - |
| `dt` | `float` | Seconds since the last call | - |

**Returns** `uint32_t`.

**Example** (also in [Lip sync: a microphone or any audio drives the mouth](../../guides/lipsync.md)):

```c
sona_voice_mouth v[64];
for (i = 0; i < n; i++) sona_voice_mouth_init(&v[i], sona_avatar_id(avatars[i]));
/* each frame: */
uint32_t changed = sona_voice_mouths(avatars, v, levels, n, dt);
```

`sona_avatar_voice` for count avatars (NULL entries skipped); returns how many mouths changed.
