@asr-forward/client
v0.0.1
Published
Simple ASR client for connecting to the ASR proxy server. Provides a clean API for streaming speech recognition without exposing provider-specific details.
Readme
@asr-forward/client
Simple ASR client for connecting to the ASR proxy server. Provides a clean API for streaming speech recognition without exposing provider-specific details.
Features
- WebSocket-based streaming communication
- Simple, clean API
- No API keys required (handled by proxy server)
- TypeScript support with full type definitions
Installation
pnpm install @asr-forward/clientUsage
import { ASRClient } from '@asr-forward/client';
// Configuration
const config = {
serverUrl: 'ws://localhost:3000', // Your proxy server URL
provider: 'volc' // Optional: ASR provider to use
};
// Event handlers
const handlers = {
onOpen: () => {
console.log('✅ Connected to ASR service');
},
onResult: (result) => {
if (result.isFinal) {
console.log(`[Final] ${result.text}`);
} else {
console.log(`[Partial] ${result.text}`);
}
},
onError: (error) => {
console.error('❌ ASR Error:', error);
},
onClose: (code, reason) => {
console.log(`🔌 Connection closed: ${code} - ${reason}`);
}
};
// Create and use client
const client = new ASRClient(config, handlers);
async function startASR() {
try {
// Connect to proxy server
await client.connect();
// Send audio data (ArrayBuffer)
const audioData = getAudioBuffer(); // Your audio data
await client.sendAudio(audioData);
// Close when done
client.close();
} catch (error) {
console.error('Failed to use ASR:', error);
}
}
startASR();API Reference
ASRClient
Constructor
new ASRClient(config: ASRClientConfig, handlers: ASREventHandlers)Methods
connect(): Promise<void>- Connect to the proxy serversendAudio(audioData: ArrayBuffer): Promise<void>- Send audio data for recognitionclose(): void- Close the connectionisConnected(): boolean- Check connection status
Types
ASRClientConfig
interface ASRClientConfig {
serverUrl: string; // WebSocket URL of proxy server
provider?: string; // ASR provider (optional, defaults to 'volc')
}ASREventHandlers
interface ASREventHandlers {
onOpen?: () => void;
onResult?: (result: ASRResult) => void;
onError?: (error: ASRError) => void;
onClose?: (code: number, reason: string) => void;
}ASRResult
interface ASRResult {
text: string; // Recognized text
isFinal: boolean; // Whether this is a final result
timestamp?: number; // Timestamp (optional)
confidence?: number; // Confidence score (optional)
}ASRError
interface ASRError {
code: number; // Error code
message: string; // Error message
}Environment Variables
The client itself doesn't require environment variables, but you can configure the server URL:
PROXY_SERVER_URL=ws://localhost:3000Examples
Check the examples/ directory in the root project for complete usage examples.
