OpenAI Text-to-Speech
Generate natural speech audio from text using OpenAI TTS API with multiple voice options and formats.
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.
OpenAI Chat Completion with Streaming
Stream GPT responses token-by-token using the OpenAI SDK with async iteration.
Generate Text Embeddings with OpenAI
Create vector embeddings for semantic search and similarity matching using text-embedding-3-small.
RAG Pipeline (Retrieve + Augment + Generate)
Minimal RAG implementation: embed a query, retrieve top-k chunks, inject into prompt.
OpenAI Tool Calling (Function Calling)
Define tools for GPT to call, parse the response, execute the function, and return results.