HipcallTts.Provider behaviour (hipcall_tts v0.5.0)
View SourceBehaviour for Text-to-Speech (TTS) providers.
This behaviour defines the contract that all TTS providers must implement. It includes callbacks for generating speech, streaming audio, validating parameters, and querying provider capabilities.
Types
params- Parameters for TTS generation (text, voice, model, etc.)result- The generated audio dataerror- Error information when generation failsmodel- TTS model informationvoice- Voice information (name, gender, language, etc.)language- Language informationcapabilities- Provider capabilities (streaming, formats, etc.)
Example
defmodule MyProvider do
@behaviour HipcallTts.Provider
@impl HipcallTts.Provider
def generate(params) do
# Implementation
end
# ... other callbacks
end
Summary
Callbacks
Returns the capabilities of this provider.
Returns the list of models compatible with a specific voice.
Generates speech audio from the given parameters.
Returns a list of supported languages for this provider.
Returns a list of available models for this provider.
Streams speech audio generation from the given parameters.
Validates the given parameters before attempting generation.
Returns a list of available voices for this provider.
Types
Callbacks
@callback capabilities() :: capabilities()
Returns the capabilities of this provider.
This includes information about supported features like streaming, audio formats, sample rates, and text length limits.
Examples
caps = Provider.capabilities()
# => %{streaming: true, formats: ["mp3", "wav"], ...}
Returns the list of models compatible with a specific voice.
For providers where some voices only support certain models/engines (e.g., AWS Polly), this filters the model list accordingly. For providers where all voices support all models, this returns the full model list.
Examples
models = Provider.compatible_models("Filiz")
# => [%{id: "standard", ...}]
models = Provider.compatible_models("Amy")
# => [%{id: "standard", ...}, %{id: "neural", ...}]
Generates speech audio from the given parameters.
Returns {:ok, result} on success or {:error, error} on failure.
Parameters
The params should include at minimum:
:textor"text"- The text to convert to speech:voiceor"voice"- The voice identifier to use:modelor"model"- The model identifier to use (optional)
Examples
{:ok, audio_data} = Provider.generate(text: "Hello", voice: "en-US-Standard-A")
{:error, "Invalid voice"} = Provider.generate(text: "Hello", voice: "invalid")
@callback languages() :: [language()]
Returns a list of supported languages for this provider.
Examples
languages = Provider.languages()
# => [%{code: "en", name: "English", ...}, ...]
@callback models() :: [model()]
Returns a list of available models for this provider.
Examples
models = Provider.models()
# => [%{id: "model-1", name: "Standard Model", ...}, ...]
@callback stream(params()) :: {:ok, Enumerable.t()} | {:error, error()}
Streams speech audio generation from the given parameters.
Returns {:ok, Enumerable.t()} that yields audio chunks, or {:error, error} on failure.
This is useful for long texts or real-time applications where you want to start playing audio before the entire generation is complete.
Examples
{:ok, stream} = Provider.stream(text: "Long text...", voice: "en-US-Standard-A")
Enum.each(stream, fn chunk -> play_audio(chunk) end)
Validates the given parameters before attempting generation.
Returns :ok if parameters are valid, or {:error, String.t()} with a description
of the validation error.
This allows clients to check parameters before making potentially expensive API calls.
Examples
:ok = Provider.validate_params(text: "Hello", voice: "en-US-Standard-A")
{:error, "Text cannot be empty"} = Provider.validate_params(text: "", voice: "en-US-Standard-A")
@callback voices() :: [voice()]
Returns a list of available voices for this provider.
Examples
voices = Provider.voices()
# => [%{id: "voice-1", name: "Alice", gender: :female, ...}, ...]