π This README is in English. ηΉε»ζ₯ηδΈζζζ‘£ (δΈζθ―΄ζ)
UNITTS is a unified Text-to-Speech (TTS) library written in TypeScript, providing a unified API interface to support multiple TTS service providers.
- π Unified Interface: Consistent API experience for all TTS providers
- π§© Adapter Pattern: Seamlessly connect to different TTS services via adapters
- π Streaming Support: Supports streaming and incremental TTS synthesis
- π§ Middleware Support: Onion-model middleware architecture, supports logging, timing, etc.
- π¦ TypeScript: Full type support and type safety
- π Extensibility: Easily add new TTS providers
- β‘ High Performance: Asynchronous processing and streaming output
npm install unitts
# or
pnpm add unitts
# or
yarn add unittsimport { TTSRelay } from 'unitts';
import { MinimaxProviderAdapter } from 'unitts/adapters';
// Create TTS relay instance
const ttsRelay = new TTSRelay();
// Register Minimax adapter
const minimaxAdapter = new MinimaxProviderAdapter('your-api-key', 'your-group-id');
ttsRelay.registerAdapter('minimax', minimaxAdapter);
// Text-to-speech
const result = await ttsRelay.synthesize('minimax', {
text: 'Hello, welcome to UNITTS!',
voice: 'female-tianmei',
model: 'speech-02-hd',
format: 'mp3',
});
console.log('Audio ID:', result.id);
console.log('Audio Data:', result.data); // Base64 encoded audio dataCurrently supports the following TTS providers:
| Provider | Status | Description |
|---|---|---|
| Minimax | β Ready | Minimax TTS service |
| Tencent | β Ready | Tencent Cloud TTS service |
| Elevenlabs | β Ready | Elevenlabs TTS service |
| OpenAI | π§ WIP | GPT series TTS service |
| Anthropic | π§ WIP | Claude TTS service |
| Google Gemini | π§ WIP | Gemini TTS service |
| Lovo.ai | π§ WIP |
import { TTSRelay } from 'unitts';
import { MinimaxProviderAdapter } from 'unitts/adapters';
const ttsRelay = new TTSRelay();
const minimaxAdapter = new MinimaxProviderAdapter('your-api-key', 'your-group-id');
ttsRelay.registerAdapter('minimax', minimaxAdapter);
// Streaming synthesis
const stream = ttsRelay.synthesizeStream('minimax', {
text: 'This is a streaming TTS synthesis example',
voice: 'male-qn-qingse',
model: 'speech-02-hd',
format: 'mp3',
stream: true,
});
for await (const chunk of stream) {
console.log('Audio chunk:', chunk.id, chunk.data.length);
if (chunk.final) {
console.log('Synthesis complete!');
break;
}
}// Incremental synthesis - suitable for real-time text streams
async function* textGenerator() {
const sentences = ['Hello,', 'welcome to', 'UNITTS!'];
for (const sentence of sentences) {
yield sentence;
await new Promise((resolve) => setTimeout(resolve, 1000));
}
}
const stream = ttsRelay.synthesizeIncremental('minimax', textGenerator(), {
voice: 'female-tianmei',
model: 'speech-02-hd',
format: 'mp3',
});
for await (const chunk of stream) {
console.log('Incremental audio chunk:', chunk.id);
}import { LoggingMiddleware, TimingMiddleware } from 'unitts/middleware';
// Add logging middleware
ttsRelay.use(new LoggingMiddleware());
// Add timing middleware
ttsRelay.use(new TimingMiddleware());
// All TTS calls will go through middleware
const result = await ttsRelay.synthesize('minimax', {
text: 'Test middleware feature',
voice: 'female-tianmei',
});// Use Minimax-specific parameters
const result = await ttsRelay.synthesize('minimax', {
text: 'Test provider-specific parameters',
voice: 'female-tianmei',
format: 'mp3',
extra: {
// Minimax-specific parameters
speed: 1.2,
vol: 0.8,
pitch: 0,
timber_weights: [{ voice_id: 'female-tianmei', weight: 1 }],
},
});The main TTS relay class, providing a unified API interface.
registerAdapter(provider, adapter)- Register a TTS provider adapteruse(middleware)- Add middlewaresynthesize(provider, params, options?)- Text-to-speechsynthesizeStream(provider, params, options?)- Streaming text-to-speechsynthesizeIncremental(provider, textStream, params, options?)- Incremental text-to-speechlistProviders()- List registered providers
interface UnifiedTTSParams {
text: string; // Text to synthesize
model?: string; // Model name
voice?: string; // Voice ID
pitch?: number; // Pitch (-20 to 20)
emotion?: string; // Emotion
rate?: number; // Speed (0.5 to 2.0)
volume?: number; // Volume (0 to 1)
format?: string; // Audio format (mp3, wav, pcm, etc.)
sampleRate?: number; // Sample rate
stream?: boolean; // Whether to output as stream
extra?: any; // Provider-specific parameters
}interface UnifiedTTSAudio {
id: string; // Audio ID
data: string; // Base64 encoded audio data
model?: string; // Model used
object: 'tts.audio'; // Object type
metadata?: Record<string, any>; // Metadata
final: boolean; // Is this the final chunk
originalResponse?: any; // Original response
}UNITTS uses the adapter pattern, making it easy to add new TTS providers:
- Create Client: Create a new provider client under
src/clients/ - Implement Adapter: Create an adapter under
src/adapters/and implement theIProviderAdapterinterface - Type Definitions: Add provider-specific types in
src/types/unified.ts - Register Export: Export the new adapter in the relevant
index.tsfile
For detailed development guide, see Development Docs.
# Run all tests
pnpm test
# Run tests and watch for file changes
pnpm test:watch
# Run a single test
pnpm test:run# Build the project
pnpm build
# Build in watch mode
pnpm build:watch
# Clean build files
pnpm cleanSee more usage examples in the examples directory:
- Basic Usage
- Streaming Synthesis
- Incremental Synthesis
- Multi-provider Usage
- Provider-specific Parameters
Contributions are welcome! Please follow these steps:
- Fork this repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
This project is open-sourced under the MIT License.
- boilcy - Project creator - 0x6c6379@gmail.com
Thanks to all the developers who contributed to this project!
If you find this project useful, please give it a β!