# C API: The stock clip library and the procedural idle

Section 3.22 of the header. The guide, with the full clip table, the tags and how every clip was named, is
[Clips](../../guides/clips.md). The library calls are pure (no ctx) and safe from any thread. Structs
carry a `size` the caller sets to `sizeof`; a call fills at most that many bytes.

## sona_stock_clip_count

```c
uint32_t sona_stock_clip_count(void);
```

**Returns** `uint32_t`.

**Example**:

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

The clips in the library: 100 (`SONA_STOCK_CLIP_COUNT`).

## sona_stock_clip_at

```c
sona_result sona_stock_clip_at(uint32_t index, sona_stock_clip *out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `index` | `uint32_t` | Which one, from 0 | - |
| `out` | `sona_stock_clip *` | Receives the [`sona_stock_clip`](../fields.md#sona_stock_clip) | required |

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

**Example**:

```c
sona_stock_clip x_stock_clip;
memset(&x_stock_clip, 0, sizeof x_stock_clip);
x_stock_clip.size = sizeof x_stock_clip;
sona_result result = sona_stock_clip_at(0, &x_stock_clip);
if (result != SONA_OK) printf("sona_stock_clip_at: %s\n", sona_result_name(result));
```

The clip at index, in id order. `SONA_E_NOT_FOUND` past the end, `SONA_E_ARGUMENT` for a `NULL` out or a size below 8.

## sona_stock_clip_get

```c
sona_result sona_stock_clip_get(uint32_t id, sona_stock_clip *out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `id` | `uint32_t` | A stock clip id, from 1 | - |
| `out` | `sona_stock_clip *` | Receives the [`sona_stock_clip`](../fields.md#sona_stock_clip) | required |

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

**Example**:

```c
sona_stock_clip x_stock_clip;
memset(&x_stock_clip, 0, sizeof x_stock_clip);
x_stock_clip.size = sizeof x_stock_clip;
sona_result result = sona_stock_clip_get(0, &x_stock_clip);
if (result != SONA_OK) printf("sona_stock_clip_get: %s\n", sona_result_name(result));
```

The clip with this id (`SONA_STOCK_*`: bytes 4 to 7 of its asset id). `SONA_E_NOT_FOUND` for an unknown id.

## sona_stock_clip_find

```c
uint32_t sona_stock_clip_find(const char *name);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `name` | `const char *` | The name to look up | required |

**Returns** `uint32_t`.

**Example** (also in [Clips: the stock clip library and the procedural idle](../../guides/clips.md)):

```c
/* every idle this body can play, the procedural idle on top */
uint32_t ids[64], n = sona_stock_clips_tagged(SONA_CLIP_TAG_IDLE, SONA_CLIP_TAG_COPY, body, ids, 64);
sona_idle_options idle;
sona_idle_defaults(&idle);                  /* blinks, glances, weight shifts and breathing, seed from the ctx */
sona_avatar_idle(avatar, &idle);

sona_avatar_play_stock(avatar, SONA_STOCK_WAVE, NULL, 0.3f);           /* crossfade into a wave */
sona_avatar_play_stock(avatar, sona_stock_clip_find("laugh"), NULL, 0); /* NULL opts: loops when the clip loops */
```

A library name (`"wave"`), an asset id in either case or a `sona_clip_get` key (`"GenericWave"`); 0 when none.

## sona_stock_clip_for_body

```c
uint32_t sona_stock_clip_for_body(uint32_t id, uint32_t body);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `id` | `uint32_t` | A stock clip id | - |
| `body` | `uint32_t` | The body a clip or item is for: 1 male, 2 female | - |

**Returns** `uint32_t`.

**Example**:

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

The clip of id's family made for body (1 male, 2 female): its own variant, else one for both, else id itself.

## sona_stock_clips_tagged

```c
uint32_t sona_stock_clips_tagged(uint32_t all, uint32_t none, uint32_t body, uint32_t *ids, uint32_t cap);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `all` | `uint32_t` | Tag bits a clip must all have | - |
| `none` | `uint32_t` | Tag bits a clip must not have | - |
| `body` | `uint32_t` | The body the clips must suit: 1 male, 2 female; 0: any | `0`: any |
| `ids` | `uint32_t *` | Receives the ids; may be NULL to count | `NULL`: not written |
| `cap` | `uint32_t` | How many entries `out` holds | - |

**Returns** `uint32_t`.

**Example** (also in [Clips: the stock clip library and the procedural idle](../../guides/clips.md)):

```c
/* every idle this body can play, the procedural idle on top */
uint32_t ids[64], n = sona_stock_clips_tagged(SONA_CLIP_TAG_IDLE, SONA_CLIP_TAG_COPY, body, ids, 64);
sona_idle_options idle;
sona_idle_defaults(&idle);                  /* blinks, glances, weight shifts and breathing, seed from the ctx */
sona_avatar_idle(avatar, &idle);

sona_avatar_play_stock(avatar, SONA_STOCK_WAVE, NULL, 0.3f);           /* crossfade into a wave */
sona_avatar_play_stock(avatar, sona_stock_clip_find("laugh"), NULL, 0); /* NULL opts: loops when the clip loops */
```

Clips with every `SONA_CLIP_TAG_*` bit of all and none of none that suit body (0 any; 1 or 2: that body's clips and
the clips for both), in id order. Returns how many match and writes up to cap ids; ids may be `NULL` with cap 0.

## sona_clip_tag_name

```c
const char *sona_clip_tag_name(uint32_t tag);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `tag` | `uint32_t` | A tag's bit | - |

**Returns** `const char *`.

**Example**:

```c
const char * x_got = sona_clip_tag_name(0);
printf("sona_clip_tag_name: %s\n", x_got ? "a value" : "none");
```

The name of one tag bit (`"idle"`, `"travel"`, ...); `NULL` for 0, several bits or an unknown bit.

## sona_clip_tag_find

```c
uint32_t sona_clip_tag_find(const char *name);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `name` | `const char *` | The name to look up | required |

**Returns** `uint32_t`.

**Example**:

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

The bit for a tag name; 0 when none.

## sona_avatar_play_stock

```c
sona_result sona_avatar_play_stock(sona_avatar *a, uint32_t id, const sona_play_options *opts, float fade_seconds);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `a` | `sona_avatar *` | The avatar | required |
| `id` | `uint32_t` | A stock clip id | - |
| `opts` | `const sona_play_options *` | The options, a [`sona_play_options`](../fields.md#sona_play_options) | required |
| `fade_seconds` | `float` | Seconds to blend over | - |

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

**Example** (also in [Cookbook: recipes for game moments](../../cookbook.md)):

```c
sona_placement to = { { 2.0f, 0.0f, -3.0f }, 90.0f, 1.0f };   /* position, turn, scale */
sona_avatar_play_stock(avatar, SONA_STOCK_WALK_FORWARD, NULL, 0.25f);
sona_avatar_move(avatar, &to, 2.5f);
/* 2.5 s later, when it has arrived: */
sona_avatar_crossfade(avatar, "idle", NULL, 0.3f);
```

Plays id's variant for the avatar's body (`sona_stock_clip_for_body` with the manifest's gender) through
`sona_avatar_play`, or `sona_avatar_crossfade` when fade_seconds is above 0. `NULL` opts: loop when the clip has
`SONA_CLIP_TAG_LOOP`, speed 1, from 0. `SONA_E_NOT_FOUND` for an unknown id; otherwise what the play call returns.

## sona_idle_defaults

```c
void sona_idle_defaults(sona_idle_options *o);
```

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

**Returns** Nothing.

**Example** (also in [Clips: the stock clip library and the procedural idle](../../guides/clips.md)):

```c
sona_idle_options o;
sona_idle_defaults(&o);        /* parts SONA_IDLE_ALL, seed 0, amount 1, face_px 240 */
o.seed = 42;                   /* 0: from the ctx seed and the avatar id */
sona_avatar_idle(avatar, &o);  /* NULL turns it off */
```

`size` set, `parts` `SONA_IDLE_ALL`, `seed` 0, `amount` 1, `face_px` 240.

## sona_avatar_idle

```c
sona_result sona_avatar_idle(sona_avatar *a, const sona_idle_options *o);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `a` | `sona_avatar *` | The avatar | required |
| `o` | `const sona_idle_options *` | The options, a [`sona_idle_options`](../fields.md#sona_idle_options) | required |

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

**Example** (also in [Clips: the stock clip library and the procedural idle](../../guides/clips.md)):

```c
sona_idle_options o;
sona_idle_defaults(&o);        /* parts SONA_IDLE_ALL, seed 0, amount 1, face_px 240 */
o.seed = 42;                   /* 0: from the ctx seed and the avatar id */
sona_avatar_idle(avatar, &o);  /* NULL turns it off */
```

Turns the procedural idle on with these options, or off for `NULL`. A seed of 0 is resolved from the ctx seed and
the avatar id. `SONA_E_ARGUMENT` for a size below 20, unknown parts, amount outside 0..2 or a negative or infinite
face_px; `SONA_E_DESTROYED` for a removed avatar. Off, the avatar poses exactly as without it.

## sona_avatar_idle_get

```c
sona_result sona_avatar_idle_get(const sona_avatar *a, sona_idle_options *out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `a` | `const sona_avatar *` | The avatar | required |
| `out` | `sona_idle_options *` | Receives the [`sona_idle_options`](../fields.md#sona_idle_options) | required |

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

**Example**:

```c
sona_idle_options x_idle_options;
memset(&x_idle_options, 0, sizeof x_idle_options);
x_idle_options.size = sizeof x_idle_options;
sona_result result = sona_avatar_idle_get(avatar, &x_idle_options);
if (result != SONA_OK) printf("sona_avatar_idle_get: %s\n", sona_result_name(result));
```

The options in use, seed resolved; parts 0 when off.

## sona_avatar_idle_state

```c
sona_result sona_avatar_idle_state(const sona_avatar *a, sona_idle_state *out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `a` | `const sona_avatar *` | The avatar | required |
| `out` | `sona_idle_state *` | Receives the [`sona_idle_state`](../fields.md#sona_idle_state) | required |

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

**Example**:

```c
sona_idle_state x_idle_state;
memset(&x_idle_state, 0, sizeof x_idle_state);
x_idle_state.size = sizeof x_idle_state;
sona_result result = sona_avatar_idle_state(avatar, &x_idle_state);
if (result != SONA_OK) printf("sona_avatar_idle_state: %s\n", sona_result_name(result));
```

The channels the idle gave the avatar's last pose (before amount); all 0 before one or when off.

## sona_idle_sample

```c
sona_result sona_idle_sample(const sona_idle_options *o, double seconds, sona_idle_state *out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `o` | `const sona_idle_options *` | The options, a [`sona_idle_options`](../fields.md#sona_idle_options) | required |
| `seconds` | `double` | The time to sample, seconds | - |
| `out` | `sona_idle_state *` | Receives the [`sona_idle_state`](../fields.md#sona_idle_state) | required |

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

**Example**:

```c
sona_idle_options x_idle_options;
memset(&x_idle_options, 0, sizeof x_idle_options);
x_idle_options.size = sizeof x_idle_options;
sona_idle_state x_idle_state2;
memset(&x_idle_state2, 0, sizeof x_idle_state2);
x_idle_state2.size = sizeof x_idle_state2;
sona_result result = sona_idle_sample(&x_idle_options, 0.5f, &x_idle_state2);
if (result != SONA_OK) printf("sona_idle_sample: %s\n", sona_result_name(result));
```

Pure: the channels at seconds on the ctx clock for `o->seed` (0 is a seed here) and `o->parts`, before amount.
`sona_avatar_idle_state` equals it at the clock of the last pose. `SONA_E_ARGUMENT` for a negative or infinite
time.
