Cogitator
Voice

TTS Providers

Text-to-speech providers for @cogitator-ai/voice — OpenAI (gpt-4o-mini-tts with 13 voices and instructions) and ElevenLabs (eleven_flash_v2_5 with low-latency streaming).

Overview

TTS providers convert text to audio. Every provider implements the TTSProvider interface:

interface TTSProvider {
  readonly name: string;
  synthesize(text: string, options?: TTSOptions): Promise<Buffer>;
  streamSynthesize(text: string, options?: TTSOptions): AsyncGenerator<Buffer>;
}

Both providers support batch synthesis via synthesize() and streaming via streamSynthesize(). Empty or whitespace-only text throws.

Provider Comparison

FeatureOpenAI TTSElevenLabs TTS
Default modelgpt-4o-mini-ttseleven_flash_v2_5
StreamingYesYes
Voice selectionBy name (13 built-in)By voice ID
Voice instructionsYes (style, tone, etc.)No
Speed control0.25x - 4.0x0.7x - 1.2x
Output formatsmp3, opus, aac, flac, wav, pcm16mp3, opus, flac, pcm16
Other modelstts-1, tts-1-hdeleven_turbo_v2_5, eleven_multilingual_v2, eleven_v3

OpenAI TTS

Uses the OpenAI Audio Speech API. Default model is gpt-4o-mini-tts with 13 built-in voices.

Requires the optional openai peer (pnpm add openai). It is loaded on the first synthesize / streamSynthesize call, not when you import the package or construct OpenAITTS; if it is missing, that call throws with an install hint.

import { OpenAITTS } from '@cogitator-ai/voice';

const tts = new OpenAITTS({
  apiKey: process.env.OPENAI_API_KEY!,
  model: 'gpt-4o-mini-tts',
  voice: 'coral',
});

Batch Synthesis

import fs from 'node:fs';

const audio = await tts.synthesize('Hello, world!', {
  speed: 1.0,
  format: 'mp3',
  instructions: 'Speak in a warm, friendly tone',
});

fs.writeFileSync('output.mp3', audio);

Streaming

for await (const chunk of tts.streamSynthesize('Streaming response...')) {
  playAudio(chunk);
}

Voice Instructions

The gpt-4o-mini-tts model supports the instructions parameter to control speaking style:

await tts.synthesize('Welcome to the show!', {
  instructions: 'Speak with excitement and energy, like a TV host',
});

await tts.synthesize('I am sorry for the inconvenience.', {
  instructions: 'Speak softly with genuine empathy',
});

Available Voices

alloy, ash, ballad, coral, echo, fable, onyx, nova, sage, shimmer, verse, marin, cedar

Configuration

FieldTypeDefaultDescription
apiKeystringrequiredOpenAI API key
modelstringgpt-4o-mini-ttsModel ID
voicestringalloyDefault voice name
baseURLstring—Custom API base URL

ElevenLabs TTS

Uses the ElevenLabs text-to-speech API. Default model is eleven_flash_v2_5 which offers low latency (~75ms).

import { ElevenLabsTTS } from '@cogitator-ai/voice';

const tts = new ElevenLabsTTS({
  apiKey: process.env.ELEVENLABS_API_KEY!,
  model: 'eleven_flash_v2_5',
  voiceId: '21m00Tcm4TlvDq8ikWAM',
});

Batch Synthesis

const audio = await tts.synthesize('Hello!', { format: 'mp3' });
fs.writeFileSync('output.mp3', audio);

Streaming

for await (const chunk of tts.streamSynthesize('Streaming audio...')) {
  playAudio(chunk);
}

Configuration

FieldTypeDefaultDescription
apiKeystringrequiredElevenLabs API key
voiceIdstring21m00Tcm4TlvDq8ikWAMDefault voice ID
modelstringeleven_flash_v2_5Model ID

Formats map to ElevenLabs output formats: mp3 → 44.1kHz 128kbps (default), pcm16 → 24kHz, opus → 48kHz, flac → 22.05kHz; wav and aac throw. Requests use stability: 0.5 and similarity_boost: 0.75 and time out after 60 seconds.

TTS Options

Options shared by both providers:

FieldTypeDescription
voicestringOverride the default voice (OpenAI voice name, ElevenLabs voice ID)
speednumberPlayback speed (OpenAI 0.25 - 4.0, ElevenLabs 0.7 - 1.2)
formatVoiceAudioFormatOutput format: "mp3" (default), "pcm16" (raw PCM16 mono 24kHz for both providers), etc.
instructionsstringVoice style instructions (OpenAI gpt-4o-mini-tts only)

VoicePipeline and pipeline-mode VoiceAgent pass their ttsOptions to every call (see TTS options).

Custom TTS Provider

Implement TTSProvider to integrate any speech synthesis service:

import type { TTSProvider, TTSOptions } from '@cogitator-ai/voice';

class PiperTTS implements TTSProvider {
  readonly name = 'piper';

  async synthesize(text: string, options?: TTSOptions): Promise<Buffer> {
    // call your local Piper instance
    return Buffer.alloc(0);
  }

  async *streamSynthesize(text: string, options?: TTSOptions): AsyncGenerator<Buffer> {
    const audio = await this.synthesize(text, options);
    yield audio;
  }
}

On this page