# Voice

> The Voice interface of @irtio/client/voice is a room connection's voice, with who is on it and the player's own controls.

The **`Voice`** interface is a room connection's voice: who is on the voice server and speaking, and the player's own microphone and settings. [`joinVoice()`](/docs/client/joinvoice) from `@irtio/client/voice` returns one.

## Instance properties

All are read only.

[`Voice.participants`](/docs/client/voice/participants)
: The other players on the voice server, whether or not this player hears them.

[`Voice.speaking`](/docs/client/voice/speaking)
: The players speaking now, this one included.

[`Voice.mutedPlayers`](/docs/client/voice/mutedplayers)
: The other players on the voice server sending no audio.

[`Voice.muted`, `Voice.micOn`, `Voice.pushToTalk` and `Voice.level`](#microphone)
: This player's microphone: muted, on, push-to-talk and its input level from 0 to 1.

[`Voice.blocked`, `Voice.input`, `Voice.output` and `Voice.canSetOutput`](#player-settings)
: The players this player blocked, the chosen microphone and speaker and whether the browser can choose the speaker.

[`Voice.project` and `Voice.livekit`](#connection)
: The project URL the settings are kept by, and the voice server's connection.

## Instance methods

[`Voice.enableMic()`, `Voice.setMuted()`, `Voice.setPushToTalk()` and `Voice.talk()`](#microphone)
: Turn the microphone on, mute it and send it only while a key is held.

[`Voice.volume()`, `Voice.setVolume()`, `Voice.setBlocked()`, `Voice.devices()`, `Voice.setInput()` and `Voice.setOutput()`](#player-settings)
: This player's volume for each other player, local blocks and the choice of microphone and speaker.

[`Voice.disconnect()`](#connection)
: Leaves the voice server for good.

## Events

Listen to these events with `addEventListener()`.

[`participants` event](/docs/client/voice/participants-event)
: A player joined or left the voice server.

[`speaking` and `mutedPlayers`](#event-listeners)
: `speaking` or `mutedPlayers` changed.

[`micError`](#microphone)
: The microphone failed to start and was turned off.

## Description

The voice follows the room connection through reconnects and ends when it closes for good. Its three sets empty while voice is down and fill again after it rejoins, so listeners added once keep working.

### Event listeners

`Voice` is an `EventTarget`. `participants`, `speaking` and `mutedPlayers` each fire a `CustomEvent` when the set of the same name changes, with the new set, a `ReadonlySet<string>` of player IDs, in its `detail`. `micError` fires a `CustomEvent` with the `Error` in its `detail`.

### Microphone

`enableMic()` publishes the microphone, asking the browser for permission the first time; call it from a click. Called before voice has joined, it turns the microphone on when it does, and while the room mutes this player it publishes once the mute ends. A refusal or other failure turns the microphone off again, rejects and fires `micError`, as does a failure on a later join. An error named `NotAllowedError` is a refusal.

`setMuted(muted)` mutes or unmutes it, and `muted` is that mute. `micOn` is `true` once `enableMic()` was called and `setMuted()` hasn't muted it. `setPushToTalk(true)` keeps the microphone muted except between `talk(true)` and `talk(false)`, which the page calls from its own key handlers, and `pushToTalk` says whether it's on. `level` is the microphone's input level from 0 to 1, 0 while it isn't sending; read it on animation frames for a meter.

### Player settings

`setVolume(player, volume, keep)` sets how loud this player hears another, from 0 to 1, multiplied into the distance attenuation; `volume(player)` reads it, 1 by default. With `keep` set to `false`, the value isn't saved, for a slider being dragged. `setBlocked(player, blocked)` is a local mute: the client never subscribes to a blocked player, whatever the room picks, and `blocked` is the set of blocked players. Volumes and blocks are kept in `localStorage` per project, or for the page where storage is blocked.

`devices()` resolves to the microphones and speakers the browser offers, as `{ inputs, outputs }` of `{ id, label }`; labels are empty until the page has microphone permission. `setInput(id)` and `setOutput(id)` choose one, and `input` and `output` are the chosen IDs, `undefined` for the browser's default. Only a browser with `setSinkId` can choose the speaker, which `canSetOutput` says; elsewhere `devices()` lists no speakers and `setOutput()` rejects.

### Connection

`project` is the room connection's project URL, by which settings are kept. `livekit` is the voice server's LiveKit room while voice is joined, for features `Voice` doesn't cover; it's replaced on each reconnect, so listeners on it need adding again. `disconnect()` leaves the voice server for good.

## Examples

### Muting with a key

A team shooter turns the microphone on from a button and mutes it with the M key.

```ts
// team-voice.ts
import { connect } from "@irtio/client";
import { joinVoice } from "@irtio/client/voice";

const room = await connect("squad");
const voice = joinVoice(room);

voice.addEventListener("micError", ({ detail: e }) => console.warn(e.name === "NotAllowedError" ? "microphone refused" : e.message));
document.querySelector("#mic")?.addEventListener("click", () => void voice.enableMic().catch(() => {}));
addEventListener("keydown", (e) => {
  if (e.code === "KeyM") void voice.setMuted(!voice.muted);
});
```

`micError` reports the refusal, so the click handler's rejection is caught and dropped.

## See also

- [`joinVoice()`](/docs/client/joinvoice)
- [`RoomClient.peers`](/docs/client/room-client/peers)
- [Proximity voice](/docs/room/guides/voice)
- [UI kit](/docs/client/ui-kit), for ready-made voice controls
