# Speakers and channels

Label each final and each word with its speaker, get the text before its speakers, or transcribe a stereo call with each side labelled.

## Speakers in a session

With `diarize=true`:

- Each `final` has a `speaker` (the speaker of most of its words), and each of its words has its own.
- Each `partial` has a `speaker` and its `words`, even without `timestamps=true`. Speaker separation follows the audio about a second behind, so the newest words of a partial have `"speaker": null` until it has decided. It never changes what it decided.
- When someone else starts talking in the middle of a segment, the words already said keep their speaker: you can show both voices before the final. At a change of speaker, the partial's own `speaker` may show the previous label for about a second.
- A final waits up to 1.5 s for speaker separation to pass its last word. In `vad` mode the pause before the end of a segment usually covers the wait. If it is not done in time, the final goes out with the speakers known so far, and the other words have `"speaker": null`. To get the text without the wait: [The text first, its speakers after](https://platform.vatis.tech/docs/realtime/speakers-and-channels#the-text-first-its-speakers-after).
- Labels are `speaker_0`, `speaker_1`, ... in the order they first speak, kept for the whole session. A new session starts again at `speaker_0`.
- `num_speakers` (1 to 8) helps when you know how many people speak; a session follows at most 8.

Without `diarize`, a final's `speaker` is `null` and a partial has no `speaker`. Speakers in a file: [The transcript object](https://platform.vatis.tech/docs/transcript-object#speakers).

A partial, with the newest word not decided yet, and its final with `timestamps=true` too, where each word has its times, its confidence and its speaker:

```json
{"type": "partial", "segment_id": 1, "text": "bună ziua luați loc", "start": 0.12, "end": 1.92, "speaker": "speaker_0",
 "words": [{"text": "bună", "start": 0.12, "end": 0.4, "speaker": "speaker_0"}, ...,
           {"text": "loc", "start": 1.42, "end": 1.7, "speaker": null}]}
{"type": "final", "segment_id": 1, "text": "Bună ziua, luați loc.", "start": 0.12, "end": 1.7, "speaker": "speaker_0",
 "words": [{"text": "Bună", "start": 0.12, "end": 0.4, "confidence": 0.98, "speaker": "speaker_0"}, ...]}
```

## The text first, its speakers after

With `diarize=true`, a final waits until speaker separation has passed its last word, about a second behind the audio. Add `speaker_updates=true` and the final comes as soon as its text is done; its speakers follow in a `final.speakers` message.

- The `final` comes at once. Its `text`, times and `words` are complete and never change. Its `speaker` and each word's are those known so far: `null` for about the last second of words, as in a partial.
- Then exactly one `final.speakers` per final, also when no label changed: always after its final, in the order of the finals, and before `session.ended` or an `error` that ends the session (`service_restart` too). It changes speakers only, never text or times.
- Its `speaker` is the segment's, the speaker of most of its words. Its `words`, only with `timestamps=true`, are the final's words again, in order, each with its speaker.
- Find its final by `segment_id`, and set the speaker of that segment and of each of its words.
- `session.started` has `"speaker_updates": true` in its `config` (the key is absent without it). Without the parameter nothing changes. Sent without `diarize=true`, the session is refused: `invalid_parameter`, `param` `speaker_updates`.

A final whose last words were the other person's, then its speakers:

```json
{"type": "final", "segment_id": 3, "text": "Vă mulțumesc pentru timp. Cu plăcere.", "start": 4.1, "end": 6.2, "speaker": "speaker_1",
 "words": [{"text": "Vă", "start": 4.1, "end": 4.3, "confidence": 0.98, "speaker": "speaker_1"}, ...,
           {"text": "plăcere.", "start": 5.7, "end": 6.2, "confidence": 0.95, "speaker": null}]}
{"type": "final.speakers", "segment_id": 3, "speaker": "speaker_1",
 "words": [{"text": "Vă", "start": 4.1, "end": 4.3, "confidence": 0.98, "speaker": "speaker_1"}, ...,
           {"text": "plăcere.", "start": 5.7, "end": 6.2, "confidence": 0.95, "speaker": "speaker_0"}]}
```

**When it helps:** after a `commit`, and in `vad` mode with a short `vad_silence_s`, where the text is ready and would wait for its speakers. A final after a commit takes about 0.76 s with speakers and 0.32 s without; with `speaker_updates=true` the text comes at the faster time and the speakers a moment later. A voice agent that answers as soon as the person stops gains the most.

**When it does not:** with the default pause of 1.5 s, the pause already covers the wait, so the text comes no sooner. With two channels a final never waits: each channel is its speaker.

Show the text at once, and its speakers when they come:

```javascript
const finals = new Map(); // a final's text until its speakers come

// in ws.onmessage
if (message.type === "final") {
  // the text at once; who said it comes in final.speakers
  finals.set(message.segment_id, message.text);
  console.log(message.text);
} else if (message.type === "final.speakers") {
  console.log(`${message.speaker}: ${finals.get(message.segment_id)}`);
  finals.delete(message.segment_id);
}
```

```go
// in Message, the segment id
SegmentID int `json:"segment_id"`

finals := map[int]string{} // a final's text until its speakers come

// in the switch over the messages
case "final":
	// the text at once; who said it comes in final.speakers
	finals[message.SegmentID] = message.Text
	fmt.Println(message.Text)
case "final.speakers":
	fmt.Printf("%s: %s\n", message.Speaker, finals[message.SegmentID])
	delete(finals, message.SegmentID)
```

```python
finals = {}  # a final's text until its speakers come

# in the loop over the messages
if message["type"] == "final":
    # the text at once; who said it comes in final.speakers
    finals[message["segment_id"]] = message["text"]
    print(message["text"])
elif message["type"] == "final.speakers":
    text = finals.pop(message["segment_id"])
    print(f"{message['speaker']}: {text}")
```

```java
// in Session: a final's text until its speakers come
final Map<String, String> finals = new HashMap<>();

// in print's switch
case "final":
    // the text at once; who said it comes in final.speakers
    String text = field(raw, "text");
    finals.put(segment(raw), text);
    System.out.println(text);
    break;
case "final.speakers":
    String speaker = field(raw, "speaker");
    System.out.println(speaker + ": " + finals.remove(segment(raw)));
    break;

// Reads the segment id, a number.
static String segment(String json) {
    String value = "\"segment_id\":\\s*(\\d+)";
    Matcher match = Pattern.compile(value).matcher(json);
    return match.find() ? match.group(1) : null;
}
```

```csharp
var finals = new Dictionary<int, string>(); // a final's text until its speakers come

// in the switch over the messages
case "final":
    // the text at once; who said it comes in final.speakers
    var text = message.GetProperty("text").GetString()!;
    finals[message.GetProperty("segment_id").GetInt32()] = text;
    Console.WriteLine(text);
    break;
case "final.speakers":
    var id = message.GetProperty("segment_id").GetInt32();
    var speaker = message.GetProperty("speaker");
    Console.WriteLine($"{speaker}: {finals[id]}");
    finals.Remove(id);
    break;
```

## Two-channel calls

A call recorded in stereo, one side on each channel, can be transcribed live with each side labelled: `multichannel=true`.

- **Audio:** two channels, interleaved sample by sample (left, right, left, ...: the order of a stereo WAV), in the declared `encoding` (`pcm_s16le`, `mulaw` or `alaw`) and `sample_rate`. The frame limits are of the two-channel frame: 100 ms of 16 kHz PCM is 6,400 bytes, of 8 kHz mu-law or A-law 1,600 bytes, and at most 1 s.
- **Messages:** each channel is transcribed on its own, and its messages go out as they come, so the two channels' messages interleave in the order the text is ready. Every `partial` and `final` carries `"channel": 0 | 1` and `"speaker": "channel_0" | "channel_1"`; with `timestamps` each word carries both too. A channel's `partial` replaces only that channel's previous partial: show one line being spoken per channel.
- **`segment_id`** is unique in the session, one counter for both channels. Ids grow per channel, but the finals of the two channels can arrive out of id order (a long turn on one channel ends after a short one on the other).
- `commit` ends the current segment of both channels (a channel with nothing said gives its empty final). `end` gives both channels' last finals, then one `session.ended`, whose `duration_s` is the recording's length, once.
- **Not with** `diarize`: `invalid_parameter`, `param` `diarize`. Each channel already is one speaker; speakers within a channel are not separated.
- **It counts as 2 live sessions** of your workspace's limit, and needs both free ([Limits and free credit](https://platform.vatis.tech/docs/limits)). Its usage counts once, with the recording's length.
- `punctuation`, `numbers`, `remove_fillers`, `commit`, `vad_silence_s`, `timestamps` and `store` apply to both channels.

```json
{"type": "partial", "segment_id": 2, "text": "aș vrea să", "start": 1.6, "end": 2.56, "speaker": "channel_1", "channel": 1,
 "words": [{"text": "aș", "start": 1.6, "end": 1.74, "speaker": "channel_1", "channel": 1}, ...]}
{"type": "final", "segment_id": 1, "text": "Bună ziua, cu ce vă pot ajuta?", "start": 0.12, "end": 1.9,
 "speaker": "channel_0", "channel": 0,
 "words": [{"text": "Bună", "start": 0.12, "end": 0.4, "confidence": 0.98, "speaker": "channel_0", "channel": 0}, ...]}
```

When your phone system delivers each side as its own stream, open one mono session per side and label them yourself instead. That also counts as 2 sessions, and each session has its own `segment_id` and times.
