Skip to content

Audio generation models ​

Audio generation models turn text into speech bytes. Use them for narration, accessibility, voice replies, and media-processing workflows.

1. Create an audio model ​

Create the provider model in server-side configuration.

ts
import { OpenAIClient } from '@anvia/openai'

const client = new OpenAIClient({ apiKey })

export const audioModel = client.speechGenerationModel({ modelId: 'tts-1' })

OpenAI and Grok provide v1 speech-generation adapters. Available voices, formats, and limits are provider-specific.

2. Generate speech ​

Pass the model, text, voice, optional speed, and provider options in one object.

ts
import { writeFile } from 'node:fs/promises'
import { generateSpeech } from '@anvia/core/speech-generation'
import { audioModel } from './models'

const speech = await generateSpeech({
    text: 'Your incident summary is ready.',
    model: audioModel,
    voice: 'alloy',
    speed: 1,
    providerOptions: {
        response_format: 'mp3',
    }
})

await writeFile('incident-summary.mp3', speech.audio.data)

The text and voice must be non-empty. Speed defaults to 1 and must be a positive finite number.

3. Read the response ​

The normalized result contains the generated bytes and optional media type.

ts
console.log({
  bytes: speech.audio.data.byteLength,
  mediaType: speech.audio.mediaType,
})

The raw provider response is available as speech.rawResponse when provider-specific metadata is required.

4. Design the production boundary ​

Validate text length, voice, speed, output format, and product entitlement before generation. Use a queue for long scripts and bulk workloads, then store audio in application-owned media storage.

Do not keep generated bytes in agent memory, traces, or normal application logs. Log identifiers and usage metadata instead.

Continue with Transcription models.

Built for Anvia.