expo-edge-speech
Version:
Text-to-speech library for Expo using Microsoft Edge TTS service
126 lines • 3.98 kB
TypeScript
/**
* Provides connection-scoped memory management for real-time audio streaming.
* Handles automatic cleanup, memory tracking, and audio data coordination.
*/
import type { SpeechStorageConfig } from "../types";
/**
* Memory usage statistics
*/
interface MemoryStats {
/** Total memory used across all connections */
totalMemoryUsed: number;
/** Number of active connections */
activeConnections: number;
/** Largest buffer size */
largestBuffer: number;
/** Memory limit per connection */
memoryLimitPerConnection: number;
/** Global memory limit */
globalMemoryLimit: number;
}
/**
* Connection-scoped memory management service
* Manages audio buffering for real-time streaming with automatic cleanup
*/
export declare class StorageService {
private static instance;
/** Connection buffers mapped by connection ID */
private connectionBuffers;
/** Cleanup interval timer */
private cleanupTimer;
/** Service configuration */
private config;
constructor(config?: Partial<SpeechStorageConfig>);
/**
* Get singleton instance of storage service
*/
static getInstance(config?: Partial<SpeechStorageConfig>): StorageService;
/**
* Create buffer for new connection
* @param connectionId - The connection ID to create buffer for
* @param allowExisting - If true, gracefully handle existing buffers instead of throwing error
*/
createConnectionBuffer(connectionId: string, allowExisting?: boolean): void;
/**
* Add audio chunk to connection buffer
*/
addAudioChunk(connectionId: string, audioData: Uint8Array): boolean;
/**
* Get merged audio data for connection
*/
getMergedAudioData(connectionId: string): Uint8Array;
/**
* Mark connection buffer as completed (no more data expected)
*/
markConnectionCompleted(connectionId: string): void;
/**
* Clean up connection buffer and free memory
*/
cleanupConnection(connectionId: string): boolean;
/**
* Get buffer info for connection
*/
getConnectionBufferInfo(connectionId: string): {
exists: boolean;
size: number;
chunkCount: number;
state: string;
lastActivity: Date | null;
};
/**
* Get memory usage statistics
*/
getMemoryStats(): MemoryStats;
/**
* Merge multiple audio chunks into single Uint8Array
*/
private mergeAudioChunks;
/**
* Start automatic cleanup interval
*/
private startCleanupInterval;
/**
* Perform automatic cleanup of stale connections
*/
private performAutomaticCleanup;
/**
* Validate audio data format and size
*/
validateAudioData(audioData: Uint8Array): boolean;
/**
* Estimate buffer size for streaming coordination
*/
estimateBufferSize(connectionId: string, additionalBytes?: number): {
currentSize: number;
estimatedSize: number;
remainingCapacity: number;
utilizationPercent: number;
};
/**
* Check if connection can accept more data
*/
canAcceptMoreData(connectionId: string, additionalBytes?: number): boolean;
/**
* Cleanup service and free all resources
*/
destroy(): void;
/**
* Initialize storage service (required by StateManager)
*/
initialize(): Promise<void>;
/**
* Cleanup storage service and free resources (required by StateManager)
*/
cleanup(): Promise<void>;
/**
* Register callback for connection state changes (required by StateManager)
*/
onConnectionStateChange(callback: (connectionId: string, state: any) => void): void;
/**
* Get active connection count (for StateManager)
*/
getActiveConnectionCount(): number;
}
export declare const getStorageService: (config?: Partial<SpeechStorageConfig>) => StorageService;
export {};
//# sourceMappingURL=storageService.d.ts.map