UNPKG

@coze/uniapp-api

Version:

Official Coze UniApp SDK for seamless AI integration into your applications | 扣子官方 UniApp SDK,助您轻松集成 AI 能力到应用中

120 lines (119 loc) 3.63 kB
import { type ClientOptions as WsToolsOptions, type CreateSpeechWsReq, type CreateSpeechWsRes, type WebSocketAPI } from '@coze/api'; /** * WsSpeechClient for UniApp/WeChat Mini Program * Handles text-to-speech streaming through WebSockets * @class */ declare class WsSpeechClient { ws: WebSocketAPI<CreateSpeechWsReq, CreateSpeechWsRes> | null; private listeners; private pcmStreamPlayer; private trackId; private api; private totalDuration; private playbackStartTime; private playbackPauseTime; private playbackTimeout; private elapsedBeforePause; private audioDeltaList; private sampleRate; private config; /** * Creates a new WsSpeechClient instance * @param {WsToolsOptions} config - Configuration options */ constructor(config: WsToolsOptions); /** * Initialize the WebSocket connection * @returns {Promise<WebSocketAPI>} - The WebSocket API instance */ init(): Promise<WebSocketAPI<CreateSpeechWsReq, CreateSpeechWsRes>>; /** * Connect to the speech service and configure audio output * @param {Object} options - Connection options * @param {string} [options.voiceId] - Voice ID to use * @param {number} [options.speechRate] - Speech rate (-50 to 100, default 0) * @returns {Promise<void>} */ connect({ voiceId, speechRate, }?: { /** Voice ID */ voiceId?: string; /** Speech rate from -50 (0.5x) to 100 (2x), default 0 */ speechRate?: number; }): Promise<void>; /** * Disconnect from the speech service and stop audio playback * @returns {Promise<void>} */ disconnect(): Promise<void>; /** * Append text to the speech buffer * @param {string} message - Text message to convert to speech */ append(message: string): void; /** * Complete the speech buffer and start processing */ complete(): void; /** * Append text and complete in a single call * @param {string} message - Text message to convert to speech */ appendAndComplete(message: string): void; /** * Interrupt playback and disconnect * @returns {Promise<void>} */ interrupt(): Promise<void>; /** * Pause audio playback * @returns {Promise<void>} */ pause(): Promise<void>; /** * Resume audio playback * @returns {Promise<void>} */ resume(): Promise<void>; /** * Toggle between play and pause states * @returns {Promise<void>} */ togglePlay(): Promise<void>; /** * Check if audio is currently playing * @returns {boolean} */ isPlaying(): boolean; /** * Register an event listener * @param {string} event - Event name to listen for * @param {Function} callback - Callback function */ on(event: string, callback: (data: CreateSpeechWsRes | undefined) => void): void; /** * Remove an event listener * @param {string} event - Event name * @param {Function} callback - Callback function to remove */ off(event: string, callback: (data: CreateSpeechWsRes | undefined) => void): void; /** * Close the WebSocket connection * @private */ private closeWs; /** * Emit an event to all registered listeners * @param {string} event - Event name * @param {CreateSpeechWsRes|undefined} data - Event data * @private */ private emit; /** * Process audio data from the queue * @private */ private handleAudioMessage; } export { WsSpeechClient }; export default WsSpeechClient;