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().

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.0xNo
Output formatsmp3, opus, aac, flac, wav, pcmmp3, pcm
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.

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

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
baseURLstringCustom 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

TTS Options

Options shared by both providers:

FieldTypeDescription
voicestringOverride the default voice
speednumberPlayback speed (OpenAI only, 0.25 - 4.0)
formatVoiceAudioFormatOutput format: "mp3", "pcm16", etc.
instructionsstringVoice style instructions (OpenAI gpt-4o-mini-tts only)

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