import { Buffer } from 'buffer'; import { APIResource } from "../resource.js"; export interface Timestamp { /** * The text segment */ text: string; /** * The time at which the text starts, in seconds */ start: number; /** * The spoken duration of the text segment, in seconds */ duration: number; } export interface ErrorBody { /** * Slug identifying the error category. */ type: 'invalid_request_error' | 'authentication_error' | 'permission_error' | 'not_found_error' | 'payment_required_error' | 'rate_limit_error' | 'internal_server_error'; /** * Human-readable error message. */ message: string; } export type SpeechSessionMessage = SpeechSessionAudio | SpeechSessionReady | SpeechSessionTimestamps | SpeechSessionFlushComplete | SpeechSessionResetComplete | SpeechSessionError; /** * Binary audio data returned from the server */ export interface SpeechSessionAudio { type: 'audio'; audio: ArrayBuffer | Buffer; } /** * First message sent by the server, confirming the session is established. */ export interface SpeechSessionReady { type: 'ready'; request_id: string; } /** * Timestamps for the audio chunk that was just streamed, if requested in `init`. */ export interface SpeechSessionTimestamps { type: 'timestamps'; timestamps?: Array; } /** * Acknowledgement that a flush command has been completed. * * The `nonce` matches the one carried by the original flush, allowing you to determine when it has completed. */ export interface SpeechSessionFlushComplete { type: 'flush_complete'; nonce: number; } /** * Acknowledgement that a reset command has been completed. * * The `nonce` matches the one carried by the original reset, allowing you to discard any remaining in-flight speech before the reset. */ export interface SpeechSessionResetComplete { type: 'reset_complete'; nonce: number; } /** * Error envelope returned by the server. Connection closes immediately afterward. */ export interface SpeechSessionError { type: 'error'; error: ErrorBody; request_id: string; } export declare class SpeechSession { private socket; private outMessages; private inMessages; private nextNonce; /** Per-session request id, populated when the server emits its `ready` frame. */ request_id: string | null; constructor(apiKey: string, options: SpeechSessionParams, baseURL?: string); private setupWebSocket; private onError; private onOpen; private onClose; private onMessage; /** * Close the SpeechSession. */ close(): void; /** * Send text to the server to append into the text stream. */ sendText(text: string): void; /** * Force the server to generate speech for all buffered text in the stream. * * @returns The nonce used for this flush command */ sendFlush(): number; /** * Drop the server's buffered text without generating speech for it. * * @returns The nonce used for this reset command */ sendReset(): number; /** * Inform the server you're done appending text to this session and want it to close when the server has finished dispatching speech. */ sendFinish(): void; /** * Iterate over messages from the server. * * @returns AsyncIterator */ [Symbol.asyncIterator](): AsyncIterator; streamAudioToFile(path: string): Promise; private parseTextMessage; private sendMessage; private flushMessages; } export declare class Sessions extends APIResource { /** * Stream text to our servers and receive generated speech in real-time. Great for latency-sensitive applications and situations where you don't have all the text upfront. */ create(body: SpeechSessionParams): SpeechSession; } export interface SpeechSessionParams { /** * The voice ID to use for speech generation, obtained from 'List voices' API */ voice: string; /** * The desired output format of the audio. * - `mp3`: 96kbps MP3 audio. * - `pcm_s16le`: PCM signed 16-bit little-endian audio. * - `pcm_f32le`: PCM 32-bit floating-point little-endian audio. * - `ulaw`: 8-bit G711 ยต-law audio with a WAV header. * - `webm`: WebM format with Opus audio codec. */ format?: 'mp3' | 'pcm_s16le' | 'pcm_f32le' | 'ulaw' | 'webm'; language?: 'auto' | 'ar' | 'as' | 'bn' | 'cs' | 'da' | 'de' | 'en' | 'es' | 'fi' | 'fr' | 'hi' | 'id' | 'it' | 'ja' | 'ko' | 'ml' | 'mr' | 'nl' | 'pl' | 'pt' | 'ru' | 'sk' | 'sv' | 'ta' | 'te' | 'th' | 'tr' | 'uk' | 'ur' | 'vi' | 'zh'; /** * Controls whether the server will return timestamps for the generated speech */ return_timestamps?: boolean; /** * The desired output audio sample rate */ sample_rate?: 24000 | 16000 | 8000; } export declare namespace Sessions { export { type SpeechSessionParams as SpeechSessionParams }; export { SpeechSession as SpeechSession }; } //# sourceMappingURL=sessions.d.ts.map