Package Exports
- ttc-sc
- ttc-sc/dist/browser-bundle.js
- ttc-sc/dist/esm/index.js
- ttc-sc/dist/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (ttc-sc) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
TTC Speech Client SDK
A TypeScript client SDK for the TTC Speech Service, providing real-time speech-to-text and text-to-speech capabilities with a simple, callback-based API.
Features
- 🎤 Real-time Speech-to-Text - Stream audio to get live transcriptions
- 🔊 Text-to-Speech Synthesis - Convert text to natural-sounding speech
- 🔌 Auto-reconnection - Automatic connection management with exponential backoff
- 🌐 Browser Compatible - Works in modern browsers with Web Audio API support
- 📦 TypeScript Ready - Full type definitions included
- 🎛️ Flexible Configuration - Customizable audio settings and connection options
Installation
npm install ttc-scQuick Start
import { TTCSpeechClient } from 'ttc-sc';
// Create client with conversation ID and callbacks
const client = new TTCSpeechClient({
conversationId: 'your-conversation-id',
serverUrl: 'ws://localhost:8080',
// Receive transcriptions
onTranscript: (result) => {
console.log('Transcription:', result.text);
if (result.isFinal) {
console.log('Final result:', result.text);
}
},
// Receive audio chunks
onAudioChunk: (audioBlob) => {
// Play audio immediately
const audio = new Audio();
audio.src = URL.createObjectURL(audioBlob);
audio.play();
},
// Handle errors
onError: (error) => {
console.error('Speech service error:', error);
}
});
// Connect and start using the service
async function startSpeechSession() {
await client.connect();
// Start recording for speech-to-text
await client.startRecording();
// Stop recording after some time
setTimeout(async () => {
await client.stopRecording();
// Synthesize text-to-speech
await client.synthesize('Hello, this is a test message!');
}, 5000);
}
startSpeechSession();API Reference
TTCSpeechClient
Constructor
new TTCSpeechClient(config: TTCClientConfig)Configuration Options
interface TTCClientConfig {
conversationId: string; // Required: Unique conversation identifier
serverUrl?: string; // Server WebSocket URL (default: ws://localhost:8080)
debug?: boolean; // Enable debug logging (default: false)
// Callbacks
onTranscript?: (result: TranscriptResult) => void; // Speech-to-text results
onAudioChunk?: (audioBlob: Blob) => void; // Text-to-speech audio
onConnectionChange?: (connected: boolean) => void; // Connection status changes
onRecordingChange?: (recording: boolean) => void; // Recording status changes
onError?: (error: ErrorEvent) => void; // Error handling
onVoicesUpdated?: (voices: Voice[]) => void; // Available voices updated
// Audio configuration
audioOptions?: {
sampleRate?: number; // Audio sample rate (default: 16000)
channels?: number; // Number of channels (default: 1)
bufferSize?: number; // Buffer size (default: 4096)
};
// Connection configuration
connectionOptions?: {
timeout?: number; // Connection timeout (default: 10000ms)
maxRetries?: number; // Max reconnection attempts (default: 5)
retryDelay?: number; // Initial retry delay (default: 1000ms)
};
}Methods
Connection Management
// Connect to the service
await client.connect(): Promise<void>
// Disconnect from the service
client.disconnect(): void
// Check connection status
client.isConnected(): boolean
client.getConnectionState(): ConnectionStateSpeech-to-Text
// Start recording audio for transcription
await client.startRecording(): Promise<void>
// Stop recording
await client.stopRecording(): Promise<void>
// Check recording status
client.isRecording(): boolean
client.getRecordingState(): RecordingStateText-to-Speech
// Synthesize text to speech
await client.synthesize(text: string, options?: TTSOptions): Promise<void>
// Cancel ongoing synthesis
client.cancelSynthesis(): void
// Get available voices
await client.getVoices(): Promise<Voice[]>Utilities
// Get client statistics
client.getStats(): ClientStatsTypes
TranscriptResult
interface TranscriptResult {
text: string; // Transcribed text
confidence: number; // Confidence score (0-1)
isFinal: boolean; // Whether this is a final result
timestamp: number; // Timestamp of the result
}ErrorEvent
interface ErrorEvent {
type: 'connection' | 'stt' | 'tts' | 'audio';
message: string;
timestamp: number;
code?: string;
originalError?: any;
}Voice
interface Voice {
id: string;
name: string;
language: string;
gender?: 'male' | 'female';
accent?: string;
}Browser Compatibility
The SDK requires modern browser features:
- Web Audio API - For microphone access and audio processing
- AudioWorklet - For efficient audio processing (falls back to ScriptProcessor)
- WebSocket - For real-time communication
- Blob/URL APIs - For audio playback
Supported browsers:
- Chrome/Edge 66+
- Firefox 76+
- Safari 14+
Examples
Basic Recording and Playback
const client = new TTCSpeechClient({
conversationId: 'demo-session',
onTranscript: (result) => {
if (result.isFinal) {
document.getElementById('transcript').textContent = result.text;
}
},
onAudioChunk: (audioBlob) => {
const audio = new Audio(URL.createObjectURL(audioBlob));
audio.play();
}
});
await client.connect();Voice Selection
await client.connect();
const voices = await client.getVoices();
// Use a specific voice
await client.synthesize('Hello world!', {
voiceId: voices[0].id,
speed: 1.0,
pitch: 1.0
});Error Handling
const client = new TTCSpeechClient({
conversationId: 'demo',
onError: (error) => {
switch (error.type) {
case 'connection':
console.log('Connection issue:', error.message);
break;
case 'audio':
console.log('Microphone issue:', error.message);
break;
case 'stt':
console.log('Speech recognition issue:', error.message);
break;
case 'tts':
console.log('Speech synthesis issue:', error.message);
break;
}
}
});Development
Building
# Install dependencies
npm install
# Build all formats
npm run build
# Build specific format
npm run build:cjs # CommonJS
npm run build:esm # ES Modules
npm run build:types # Type definitionsTesting
# Run tests
npm test
# Type checking
npm run type-check
# Linting
npm run lintLicense
MIT License - see LICENSE file for details.
Support
For issues and questions, please open an issue on the GitHub repository.