typescriptbeginner
OpenAI Text-to-Speech
Generate natural speech audio from text using OpenAI TTS API with multiple voice options and formats.
typescriptPress ⌘/Ctrl + Shift + C to copy
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.
pythonbeginner
OpenAI Text-to-Speech Synthesis
Convert text to natural-sounding speech using the OpenAI TTS API with voice selection and streaming.
Best for: text-to-speech
#openai#tts
pythonbeginner
Whisper Audio Transcription Pipeline
Transcribe audio files to text using OpenAI Whisper API with language detection and timestamps.
Best for: meeting transcription
#whisper#transcription
typescriptintermediate
OpenAI Chat Completion with Streaming
Stream GPT responses token-by-token using the OpenAI SDK with async iteration.
Best for: chatbot UI
#openai#streaming
typescriptbeginner
Generate Text Embeddings with OpenAI
Create vector embeddings for semantic search and similarity matching using text-embedding-3-small.
Best for: semantic search
#openai#embeddings