Repository navigation
Implement OpenAITTSHandler.synthesize() for text-to-speech synthesis #619
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,186 @@ | ||
| # OpenAI TTS Handler - Usage Examples | ||
|
|
||
| This document provides examples of using the `OpenAITTSHandler` for text-to-speech synthesis. | ||
|
|
||
| ## Basic Usage | ||
|
|
||
| ```typescript | ||
| import { OpenAITTSHandler } from "@juspay/neurolink/adapters/tts"; | ||
|
|
||
| // Initialize handler with API key | ||
| const ttsHandler = new OpenAITTSHandler(process.env.OPENAI_API_KEY); | ||
|
|
||
| // Synthesize text with default settings | ||
| const result = await ttsHandler.synthesize({ | ||
| text: "Hello, world!", | ||
| }); | ||
|
|
||
| console.log(`Audio format: ${result.format}`); | ||
| console.log(`Buffer size: ${result.size} bytes`); | ||
| console.log(`Duration: ${result.duration} seconds`); | ||
| console.log(`Voice: ${result.voice}`); | ||
|
|
||
| // Save audio to file | ||
| import { writeFileSync } from "fs"; | ||
| writeFileSync("output.mp3", result.buffer); | ||
| ``` | ||
|
|
||
| ## Supported Voices | ||
|
|
||
| The handler supports all 6 OpenAI voices: | ||
|
|
||
| - `alloy` (default) - Neutral, balanced voice | ||
| - `echo` - Clear and articulate | ||
| - `fable` - British accent, storytelling | ||
| - `onyx` - Deep, authoritative | ||
| - `nova` - Energetic and enthusiastic | ||
| - `shimmer` - Warm and friendly | ||
|
|
||
| ```typescript | ||
| // Use a specific voice | ||
| const result = await ttsHandler.synthesize({ | ||
| text: "Welcome to our application!", | ||
| voice: "nova", | ||
| }); | ||
| ``` | ||
|
|
||
| ## Supported Formats | ||
|
|
||
| The handler supports all OpenAI TTS formats: | ||
|
|
||
| - `mp3` (default) - Compressed, good quality | ||
| - `opus` - High compression, web streaming | ||
| - `aac` - Advanced audio coding | ||
| - `flac` - Lossless, best quality | ||
| - `wav` - Uncompressed, best quality | ||
| - `pcm` - Raw PCM audio | ||
|
|
||
| ```typescript | ||
| // Generate high-quality WAV audio | ||
| const result = await ttsHandler.synthesize({ | ||
| text: "High quality audio", | ||
| format: "wav", | ||
| }); | ||
| ``` | ||
|
|
||
| ## Quality Levels | ||
|
|
||
| Choose between standard and HD quality: | ||
|
|
||
| - `standard` (default) - Uses `tts-1` model | ||
| - `hd` - Uses `tts-1-hd` model (higher quality, slower) | ||
|
|
||
| ```typescript | ||
| // Generate HD quality audio | ||
| const result = await ttsHandler.synthesize({ | ||
| text: "High definition speech", | ||
| quality: "hd", | ||
| }); | ||
| ``` | ||
|
|
||
| ## Speed Control | ||
|
|
||
| Adjust speech speed from 0.25x to 4.0x (default: 1.0x): | ||
|
|
||
| ```typescript | ||
| // Slow speech for language learning | ||
| const slow = await ttsHandler.synthesize({ | ||
| text: "This is slow speech", | ||
| speed: 0.5, | ||
| }); | ||
|
|
||
| // Fast speech for efficiency | ||
| const fast = await ttsHandler.synthesize({ | ||
| text: "This is fast speech", | ||
| speed: 2.0, | ||
| }); | ||
| ``` | ||
|
|
||
| ## Complete Example | ||
|
|
||
| ```typescript | ||
| import { OpenAITTSHandler } from "@juspay/neurolink/adapters/tts"; | ||
| import { writeFileSync } from "fs"; | ||
|
adarsh02125 marked this conversation as resolved.
|
||
|
|
||
| async function generatePodcastIntro() { | ||
| const handler = new OpenAITTSHandler(); | ||
|
|
||
| const result = await handler.synthesize({ | ||
| text: "Welcome to Tech Insights Podcast, episode 42. Today we're discussing the future of AI.", | ||
| voice: "onyx", | ||
| format: "mp3", | ||
| quality: "hd", | ||
| speed: 0.95, // Slightly slower for clarity | ||
| }); | ||
|
|
||
| // Save to file | ||
| writeFileSync("podcast-intro.mp3", result.buffer); | ||
|
|
||
| console.log(`Generated ${result.size} bytes of audio`); | ||
| console.log( | ||
| `Estimated duration: ${result.duration?.toFixed(2)} seconds`, | ||
| ); | ||
| console.log(`Voice used: ${result.voice}`); | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| generatePodcastIntro(); | ||
| ``` | ||
|
|
||
| ## Error Handling | ||
|
|
||
| The handler validates all inputs and provides clear error messages: | ||
|
|
||
| ```typescript | ||
| try { | ||
| const result = await handler.synthesize({ | ||
| text: "", // Empty text | ||
| }); | ||
| } catch (error) { | ||
| console.error(error.message); // "Text is required for TTS synthesis" | ||
| } | ||
|
|
||
| try { | ||
| const result = await handler.synthesize({ | ||
| text: "test", | ||
| speed: 5.0, // Invalid speed | ||
| }); | ||
| } catch (error) { | ||
| console.error(error.message); // "Speed must be between 0.25 and 4.0" | ||
| } | ||
| ``` | ||
|
|
||
| ## Result Structure | ||
|
|
||
| The result includes the audio data and metadata: | ||
|
|
||
| ```typescript | ||
| const result = await handler.synthesize({ | ||
| text: "Sample text", | ||
| }); | ||
|
|
||
| console.log(result); | ||
| // { | ||
| // buffer: Buffer, // Audio data | ||
| // format: "mp3", // Audio format | ||
| // size: 38400, // Buffer size in bytes | ||
| // duration: 2.4, // Estimated duration in seconds | ||
| // voice: "alloy", // Voice used | ||
| // sampleRate: undefined // Sample rate (not provided by OpenAI) | ||
| // } | ||
| ``` | ||
|
|
||
| ## Getting Supported Options | ||
|
|
||
| ```typescript | ||
| const handler = new OpenAITTSHandler(); | ||
|
|
||
| // Get all supported voices | ||
| const voices = handler.getSupportedVoices(); | ||
| console.log(voices); // ["alloy", "echo", "fable", "onyx", "nova", "shimmer"] | ||
|
|
||
| // Get all supported formats | ||
| const formats = handler.getSupportedFormats(); | ||
| console.log(formats); // ["mp3", "opus", "aac", "flac", "wav", "pcm"] | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| /** | ||
| * TTS Adapters Index | ||
| * | ||
| * Exports TTS handler implementations | ||
| * | ||
| * @module adapters/tts | ||
| */ | ||
|
|
||
| export { OpenAITTSHandler } from "./openaiTTSHandler.js"; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.