typescriptbeginner

OpenAI Text-to-Speech

Generate natural speech audio from text using OpenAI TTS API with multiple voice options and formats.

typescript
import OpenAI from 'openai';
import * as fs from 'fs';

const openai = new OpenAI();

type Voice = 'alloy' | 'echo' | 'fable' | 'onyx' | 'nova' | 'shimmer';

export async function textToSpeech({
  text,
  voice = 'nova',
  outputPath = './output.mp3',
}: {
  text: string;
  voice?: Voice;
  outputPath?: string;
}) {
  const response = await openai.audio.speech.create({
    model: 'tts-1-hd',
    voice,
    input: text,
    response_format: 'mp3',
    speed: 1.0,
  });

  const buffer = Buffer.from(await response.arrayBuffer());
  fs.writeFileSync(outputPath, buffer);
  return { path: outputPath, size: buffer.length };
}

// Usage:
// await textToSpeech({
//   text: 'Welcome to SnippetsLab.',
//   voice: 'nova',
// });

Use Cases

  • Audiobook generation
  • Accessibility features
  • Voice notifications

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.