# Sounds API

This API reads and changes the sound settings of the gateway. The values are the settings of the
**Sounds** section in the Companion app and the TUI.

The examples use the [Python SDK](python-sdk.md). To see the same steps as [HTTP API](http-api.md)
requests, select **HTTP** at the top of the page.

## When to use

- **A setup script must turn on sounds.** [Turn on sounds and choose a
  preset](#turn-on-sounds-and-choose-a-preset) with one call.
- **An extension must use its own sound files.** [Save your own preset](#save-your-own-preset)
  with `vis.sounds` in the extension.
- **A tool must play a sound before a person saves it.** [Preview a sound](#preview-a-sound).
- **A tool must show the current sounds.** [Read the presets](#read-the-presets).

To learn which events play a sound and where the sound plays, read [Sounds](sounds.md).

## Before you start

Connect a `GatewayClient` as in [Connect to a gateway and run a
task](python-sdk.md#connect-to-a-gateway-and-run-a-task). The examples use `client`. Inside an
extension, `vis.sounds` is the same API.

## Operations

`client.sounds` returns a `Sounds` object. Its calls use the generated settings methods.

| Task | Typed call | Generated method |
|---|---|---|
| Read the presets | `client.sounds.get()` | `client.routes.get_setting(id)` |
| Turn on sounds or choose a preset | `client.sounds.set(...)` | `client.routes.post_settings(body=...)` |
| Save your own preset | `client.sounds.save_preset(...)` | `client.routes.post_settings(body=...)` |
| Delete your own preset | `client.sounds.delete_preset(name)` | `client.routes.post_settings(body=...)` |
| Preview a sound | `client.sounds.preview(slot, value)` | `client.routes.post_setting_preview(id, body=...)` |

A preset has one value for each slot: `finish`, `input` for the question sound, and `error`. A slot
value is one of these:

- An empty value, for no sound.
- The name of a built-in preset, for the sound of that preset. The built-in presets are `classic`,
  `glass`, `soft`, `minimal`, and `bright`.
- A file path on the gateway computer.

## Read the presets

```python
sounds = client.sounds.get()
print(sounds.is_enabled, sounds.active)
for preset in sounds.presets:
    print(preset.name, preset.is_builtin, preset.finish, preset.input, preset.error)
```

`get()` returns a `SoundSettings` object. `presets` lists the built-in presets, then your own.
`preset` is the active preset, and `custom` lists only your own presets.

## Turn on sounds and choose a preset

```python
sounds = client.sounds.set(enabled=True, preset="glass")
print(sounds.active)
```

An argument that you omit changes nothing. A preset name matches in any case. An unknown name
raises `ValueError` and changes nothing.

## Save your own preset

```python
from pathlib import Path

sounds = client.sounds.save_preset(
    "My mix",
    finish="glass",
    input=Path("~/Sounds/question.wav"),
    error=None,
    activate=True,
)
print(sounds.preset)
client.sounds.delete_preset("My mix")
```

`save_preset()` replaces a preset that has the same name. `None` or an empty value plays no sound
in that slot. After you delete the active preset, the first built-in preset plays.

The gateway checks each file path when you save. For a path that names no file, the gateway returns
an error with a message and saves nothing. The gateway expands a leading `~` on its own computer.

## Preview a sound

```python
played = client.sounds.preview("input", "glass")
print(played)
```

`preview()` plays one slot value on the gateway computer and saves nothing. It returns `False`
when the value is empty, when another sound plays, or when the computer has no sound program.

## See also

- To learn which events play a sound and where the sound plays, read [Sounds](sounds.md).
- To read and change all settings, read [Configuration API](configuration-api.md).
- To install the SDK, connect a client, and handle gateway errors, read [Python SDK](python-sdk.md).
