# C API: Face tracking module (sona_track.h)

An optional library, `sona_track`: a face tracker's blendshapes and head pose to the avatar. Include `sona_track.h` and link `sona_track`. The guide: [Face tracking (sona_track)](../../guides/face-tracking.md).

## sona_track_blendshape_name

```c
const char * sona_track_blendshape_name(uint32_t index);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `index` | `uint32_t` | Which one, from 0 | - |

**Returns** `const char *`.

**Example**:

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

The name of blendshape `index` ("eyeBlinkLeft", ...) as both trackers spell them; NULL past the last.

## sona_track_blendshape_find

```c
int32_t sona_track_blendshape_find(const char *name);
```

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

**Returns** `int32_t`.

**Example**:

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

The index of a blendshape by name; -1 when unknown.

## sona_track_defaults

```c
void sona_track_defaults(sona_track_options *o);
```

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

**Returns** Nothing.

**Example**:

```c
sona_track_options x_track_options;
memset(&x_track_options, 0, sizeof x_track_options);
x_track_options.size = sizeof x_track_options;
sona_track_defaults(&x_track_options);
```

`size` set and the filter and head settings the [face tracking](../../guides/face-tracking.md) guide lists.

## sona_track_create

```c
sona_result sona_track_create(const sona_track_options *o, sona_tracker **out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `o` | `const sona_track_options *` | The options, a [`sona_track_options`](../fields.md#sona_track_options) | `NULL`: the defaults |
| `out` | `sona_tracker **` | Receives the new face tracker | required |

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

NULL options: the defaults. `SONA_E_ARGUMENT` for a wrong size or a value out of range.

## sona_track_destroy

```c
void sona_track_destroy(sona_tracker *t);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `t` | `sona_tracker *` | The face tracker | required |

**Returns** Nothing.

Frees the tracker.

## sona_track_set_options

```c
sona_result sona_track_set_options(sona_tracker *t, const sona_track_options *o);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `t` | `sona_tracker *` | The face tracker | required |
| `o` | `const sona_track_options *` | The options, a [`sona_track_options`](../fields.md#sona_track_options) | required |

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

New options from the next frame; the filters keep their state. `SONA_E_ARGUMENT` for a wrong size or a value out of range.

## sona_track_reset

```c
void sona_track_reset(sona_tracker *t);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `t` | `sona_tracker *` | The face tracker | required |

**Returns** Nothing.

Forgets the filters, the shapes held and the tracking state; keeps the options and the calibration.

## sona_track_face_input_defaults

```c
void sona_track_face_input_defaults(sona_track_face_input *in);
```

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

**Returns** Nothing.

**Example**:

```c
sona_track_face_input x_track_face_input;
sona_track_face_input_defaults(&x_track_face_input);
sona_track_face_input_defaults(&x_track_face_input);
```

`size` set, the ARKit layout, nothing found.

## sona_track_face_feed

```c
sona_result sona_track_face_feed(sona_tracker *t, const sona_track_face_input *in);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `t` | `sona_tracker *` | The face tracker | required |
| `in` | `const sona_track_face_input *` | A [`sona_track_face_input`](../fields.md#sona_track_face_input) | required |

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

`SONA_E_ARGUMENT` for a wrong size, layout or count, a missing weights pointer with FOUND, or a time that is not finite. A time more than a second away from the last one (or earlier than it) starts the filters again.

## sona_track_face_get

```c
sona_result sona_track_face_get(const sona_tracker *t, sona_track_face_state *out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `t` | `const sona_tracker *` | The face tracker | required |
| `out` | `sona_track_face_state *` | Receives the [`sona_track_face_state`](../fields.md#sona_track_face_state) | required |

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

What the tracker makes of the frames so far: the filtered, calibrated weights in ARKit order (sides swapped under `SONA_TRACK_MIRROR`, so Left is the avatar's left) and the head.

## sona_track_calibrate_begin

```c
sona_result sona_track_calibrate_begin(sona_tracker *t);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `t` | `sona_tracker *` | The face tracker | required |

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

Starts collecting a neutral face: every frame with a face from now on counts.

## sona_track_calibrate_end

```c
sona_result sona_track_calibrate_end(sona_tracker *t, sona_track_calibration *out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `t` | `sona_tracker *` | The face tracker | required |
| `out` | `sona_track_calibration *` | Receives the [`sona_track_calibration`](../fields.md#sona_track_calibration) | `NULL` allowed (below) |

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

Stops and makes each weight's 30th percentile over the frames (the first 300) and their mean head rotation the neutral, and writes it to `out` (may be NULL). `SONA_E_NOT_READY` when no face came in between. Each weight's neutral value is taken off it and the rest rescaled to 0..1; the neutral head rotation becomes identity.

## sona_track_calibration_set

```c
sona_result sona_track_calibration_set(sona_tracker *t, const sona_track_calibration *c);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `t` | `sona_tracker *` | The face tracker | required |
| `c` | `const sona_track_calibration *` | A [`sona_track_calibration`](../fields.md#sona_track_calibration) | required |

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

A saved calibration, for the next session; NULL clears it.

## sona_track_calibration_get

```c
sona_result sona_track_calibration_get(const sona_tracker *t, sona_track_calibration *out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `t` | `const sona_tracker *` | The face tracker | required |
| `out` | `sona_track_calibration *` | Receives the [`sona_track_calibration`](../fields.md#sona_track_calibration) | required |

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

The calibration in use, as `sona_track_calibrate_end` or `sona_track_calibration_set` left it.

## sona_track_apply

```c
sona_result sona_track_apply(sona_tracker *t, sona_avatar *a);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `t` | `sona_tracker *` | The face tracker | required |
| `a` | `sona_avatar *` | The avatar | required |

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

Puts the state on an avatar: the face shapes through `sona_avatar_expression` (only when they change) and the head through ADD overrides on Neck and Head, aimed in the avatar's body space from its posed joints, so it holds for any turn, lean or clip (overrides need a clip playing: the default idle does). Call it after the feed and before `sona_update`. One tracker drives one avatar at a time: moving it to another leaves the first as it is, so release that one first. `SONA_E_NOT_READY` until the avatar is READY and posed.

## sona_track_release

```c
void sona_track_release(sona_tracker *t, sona_avatar *a);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `t` | `sona_tracker *` | The face tracker | required |
| `a` | `sona_avatar *` | The avatar | required |

**Returns** Nothing.

Hands the face and the tracker's joints back to the clip; nothing when a is not the avatar it drives.

## sona_track_quat_from_matrix

```c
sona_result sona_track_quat_from_matrix(const float *m, uint32_t stride, int transpose, float q[4]);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `m` | `const float *` | A 3x3 or 4x4 rotation matrix | required |
| `stride` | `uint32_t` | Bytes from one row to the next | - |
| `transpose` | `int` | 1 when the matrix is column-major | - |
| `q` | `float [4]` | Receives a unit quaternion (x, y, z, w) | required |

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

A rotation matrix to the quaternion head_rotation takes. m holds the 3x3 rotation row by row (m[0..2] the first row); stride is the distance between rows in floats, 3 for a 3x3 and 4 for the upper left of a row-major 4x4 (MediaPipe's facial transformation matrix as numpy holds it). A column-major 4x4 (ARKit's simd_float4x4, glTF) is the transpose: pass transpose 1. Scale is removed.

## sona_track_quat_from_euler

```c
void sona_track_quat_from_euler(float pitch_deg, float yaw_deg, float roll_deg, float q[4]);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `pitch_deg` | `float` | Pitch in degrees, up positive | - |
| `yaw_deg` | `float` | Yaw in degrees | - |
| `roll_deg` | `float` | Roll in degrees | - |
| `q` | `float [4]` | Receives a unit quaternion (x, y, z, w) | required |

**Returns** Nothing.

**Example**:

```c
float x_q[4];
memset(x_q, 0, sizeof x_q);
sona_track_quat_from_euler(30.0f, 30.0f, 30.0f, x_q);
```

Degrees, right-handed about the camera axes: pitch about +X (nodding down is positive), yaw about +Y (turning towards the image's right, the subject's own left, is positive), roll about +Z; applied roll, then pitch, then yaw (q = yaw * pitch * roll). Many trackers (OpenSeeFace, head-pose solvers) give these.

## sona_track_struct_size

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

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `name` | `const char *` | A public struct's name | required |

**Returns** `uint32_t`.

**Example**:

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

sizeof a public struct of this header by name ("sona_track_options"), for the bindings' layout checks; 0 when unknown.

## sona_track_struct_offset

```c
uint32_t sona_track_struct_offset(const char *struct_dot_field);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `struct_dot_field` | `const char *` | `"struct.field"` | required |

**Returns** `uint32_t`.

**Example**:

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

offsetof a field by "struct.field" ("sona_track_face_state.weights"); `UINT32_MAX` when unknown.
