# `Octoroom.Agent.STT`
[🔗](https://github.com/samrat/octoroom/blob/v0.1.0/agents/lib/octoroom/agent/stt.ex#L1)

Streaming speech-to-text providers.

A provider is a process, usually a WebSocket client, that one owner feeds
with audio and that reports back with messages:

    {:ok, stt} = Octoroom.Agent.STT.start_link("soniox:stt-rt-v6", sample_rate: 16_000)
    Octoroom.Agent.STT.send_audio(stt, pcm)
    # receive {:stt, ^stt, {:final, "hello"}}

## Options

`start_link/2` takes a provider and options. The provider is a
`"provider:model"` string, a registered name, or a module; options can also
name it with `:provider` and `:model`. Every provider takes:

  * `:owner` - the process that gets the events (default: the caller)
  * `:api_key` - defaults to the provider's environment variable
  * `:model` - needed: no provider picks one for you
  * `:sample_rate` - of the audio sent: 16-bit little-endian mono PCM
  * `:language_hints`

Anything else is passed to the provider, which ignores what it doesn't use.

## Events

The owner gets `{:stt, pid, event}`, where `event` is:

  * `:connected` - audio sent from now on is heard
  * `{:partial, text}` - an interim transcript of the current utterance
  * `{:final, text}` - the final transcript of an utterance, after the
    provider's endpointing or a `finalize/1`. After `finalize/1`, it comes
    even if `text` is `""`
  * `:finished` - the provider is done, after `close/1`
  * `{:warning, reason}` - something went wrong but the session goes on
  * `{:error, reason}` - the session failed; `reason` is a string or a
    map with `:message` when the provider gave one
  * `{:disconnected, reason, %{intentional?: boolean}}` - the connection
    closed; intentional after `close/1`

## Adding a provider

Implement this behaviour, sending events with `notify/2`, and register it:

    config :octoroom_agents, stt_providers: [acme: MyApp.AcmeSTT]

A module that implements the behaviour also works as `:provider` without
being registered.

# `event`

```elixir
@type event() ::
  :connected
  | {:partial, String.t()}
  | {:final, String.t()}
  | :finished
  | {:warning, term()}
  | {:error, term()}
  | {:disconnected, term(), %{intentional?: boolean()}}
```

# `close`

```elixir
@callback close(pid()) :: :ok
```

Flushes the audio sent, then ends the session.

# `default_options`
*optional* 

```elixir
@callback default_options() :: keyword()
```

Options used when the caller doesn't give them, such as `:sample_rate`.

# `finalize`
*optional* 

```elixir
@callback finalize(pid()) :: :ok
```

Finalizes what's been heard so far, without waiting for the provider's
endpointing. Answered with a `{:final, text}`. Providers without it just
wait for their endpointing.

# `send_audio`

```elixir
@callback send_audio(pid(), audio :: binary()) :: :ok
```

# `start_link`

```elixir
@callback start_link(opts :: keyword()) :: {:ok, pid()} | {:error, term()}
```

Starts a session that sends events to `opts[:owner]`.

# `error_message`

A message for a user to read about an `{:error, reason}` event.

# `finalize`

Asks `module`'s session to finalize, if the provider can.

# `notify`

Sends `event` from the calling provider process to `owner`.

# `providers`

The registered providers, by name.

# `resolve`

Finds the provider for `opts`. Returns the module and its options, with
the provider's defaults filled in.

# `start_link`

Starts a session. See the module docs for `provider` and `opts`.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
