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

Text-to-speech providers.

    :ok =
      Octoroom.Agent.TTS.stream("inworld:inworld-tts-2", "Hello!", fn pcm -> ... end,
        voice: "Ashley",
        sample_rate: 24_000
      )

    {:ok, pcm} = Octoroom.Agent.TTS.synthesize(:inworld, "Hello!", model: "inworld-tts-2")

Audio is 16-bit little-endian mono PCM at `:sample_rate`, with no
container header.

## 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:

  * `:api_key` - defaults to the provider's environment variable
  * `:model` - needed: no provider picks one for you
  * `:voice`
  * `:sample_rate` - of the audio returned

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

## Adding a provider

Implement `stream/3` or `synthesize/2`, or both: `stream/3` falls back to
one `synthesize/2` call, and `synthesize/2` collects what `stream/3`
sends. Then register it:

    config :octoroom_agents, tts_providers: [acme: MyApp.AcmeTTS]

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

# `connect`
*optional* 

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

Opens what the provider keeps between calls, such as a WebSocket, tied to
the calling process. Returns the options that use it. Whoever speaks many
times (the TTS processor) calls it once first.

# `default_options`
*optional* 

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

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

# `stream`
*optional* 

```elixir
@callback stream(text :: String.t(), on_audio :: (binary() -&gt; any()), opts :: keyword()) ::
  :ok | {:error, term()}
```

Speaks `text`, calling `on_audio` with each piece of audio as it arrives.
Returns once all of it has been passed on.

# `synthesize`
*optional* 

```elixir
@callback synthesize(text :: String.t(), opts :: keyword()) ::
  {:ok, binary()} | {:error, term()}
```

Speaks `text`, returning all of the audio at once.

# `connect`

`connect/1` of `module`, or `opts` unchanged when it has none.

# `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.

# `stream`

Speaks `text` with `provider`, streaming audio to `on_audio`.

# `stream_with`

`stream/4` with a module and options from `resolve/1`.

# `synthesize`

Speaks `text` with `provider`, returning all of the audio.

# `synthesize_with`

`synthesize/3` with a module and options from `resolve/1`.

---

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