Octoroom.Agent.TTS behaviour (octoroom_agents v0.1.0)

Copy Markdown View Source

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.

Summary

Callbacks

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.

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

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

Speaks text, returning all of the audio at once.

Functions

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

The registered providers, by name.

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

Speaks text with provider, streaming audio to on_audio.

Speaks text with provider, returning all of the audio.

Callbacks

connect(opts)

(optional)
@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)
@callback default_options() :: keyword()

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

stream(text, on_audio, opts)

(optional)
@callback stream(text :: String.t(), on_audio :: (binary() -> 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(text, opts)

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

Speaks text, returning all of the audio at once.

Functions

connect(module, opts)

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

providers()

The registered providers, by name.

resolve(opts)

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

stream(provider, text, on_audio, opts \\ [])

Speaks text with provider, streaming audio to on_audio.

stream_with(module, text, on_audio, opts)

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

synthesize(provider, text, opts \\ [])

Speaks text with provider, returning all of the audio.

synthesize_with(module, text, opts)

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