UNPKG

@picovoice/orca-web

Version:

Orca Text-to-Speech engine for web browsers (via WebAssembly)

174 lines 8.58 kB
import { Mutex } from 'async-mutex'; import { pv_free_type } from '@picovoice/web-utils'; import { OrcaModel, OrcaStreamSynthesizeResult, OrcaSynthesizeParams, OrcaSynthesizeResult } from './types'; type pv_orca_pcm_delete_type = (object: number) => Promise<void>; type pv_orca_stream_synthesize_type = (object: number, text: number, numSamples: number, pcm: number) => Promise<number>; type pv_orca_stream_flush_type = (object: number, numSamples: number, pcm: number) => Promise<number>; type pv_orca_stream_close_type = (object: number) => Promise<void>; type pv_get_error_stack_type = (messageStack: number, messageStackDepth: number) => Promise<number>; type pv_free_error_stack_type = (messageStack: number) => Promise<void>; /** * OrcaStream object that converts a stream of text to a stream of audio. */ declare class Stream { private _wasmMemory; private readonly _alignedAlloc; private readonly _pvFree; private readonly _pvGetErrorStack; private readonly _pvFreeErrorStack; private readonly _messageStackAddressAddressAddress; private readonly _messageStackDepthAddress; private readonly _functionMutex; private readonly _streamPcmAddressAddress; private readonly _pvOrcaPcmDelete; private readonly _pvOrcaStreamSynthesize; private readonly _pvOrcaStreamFlush; private readonly _pvOrcaStreamClose; private readonly _streamAddress; private readonly _getMessageStack; constructor(wasmMemory: WebAssembly.Memory, alignedAlloc: CallableFunction, pvFree: pv_free_type, pvGetErrorStack: pv_get_error_stack_type, pvFreeErrorStack: pv_free_error_stack_type, messageStackAddressAddressAddress: number, messageStackDepthAddress: number, functionMutex: Mutex, streamPcmAddressAddress: number, pvOrcaPcmDelete: pv_orca_pcm_delete_type, pvOrcaStreamSynthesize: pv_orca_stream_synthesize_type, pvOrcaStreamFlush: pv_orca_stream_flush_type, pvOrcaStreamClose: pv_orca_stream_close_type, streamAddress: number, getMessageStack: any); /** * Adds a chunk of text to the Stream object and generates audio if enough text has been added. * This function is expected to be called multiple times with consecutive chunks of text from a text stream. * The incoming text is buffered as it arrives until there is enough context to convert a chunk of the * buffered text into audio. The caller needs to use `OrcaStream.flush()` to generate the audio chunk * for the remaining text that has not yet been synthesized. * * @param text A chunk of text from a text input stream, comprised of valid characters. * Valid characters can be retrieved by calling `validCharacters`. * Custom pronunciations can be embedded in the text via the syntax `{word|pronunciation}`. * They need to be added in a single call to this function. * The pronunciation is expressed in ARPAbet format, e.g.: `I {liv|L IH V} in {Sevilla|S EH V IY Y AH}`. * @return The generated audio as a sequence of 16-bit linearly-encoded integers, `null` if no * audio chunk has been produced. */ synthesize(text: string): Promise<OrcaStreamSynthesizeResult>; /** * Generates audio for all the buffered text that was added to the OrcaStream object * via `OrcaStream.synthesize()`. * * @return The generated audio as a sequence of 16-bit linearly-encoded integers, `null` if no * audio chunk has been produced. */ flush(): Promise<OrcaStreamSynthesizeResult>; /** * Releases the resources acquired by the OrcaStream object. */ close(): Promise<void>; } export type OrcaStream = Stream; /** * JavaScript/WebAssembly Binding for Orca */ export declare class Orca { private static _version; private static _sampleRate; private static _validCharacters; private static _maxCharacterLimit; private _wasmMemory?; private readonly _alignedAlloc; private readonly _pvFree; private readonly _pvGetErrorStack; private readonly _pvFreeErrorStack; private readonly _messageStackAddressAddressAddress; private readonly _messageStackDepthAddress; private readonly _objectAddress; private readonly _pvOrcaDelete; private readonly _pvOrcaSynthesize; private readonly _pvOrcaSynthesizeParamsInit; private readonly _pvOrcaSynthesizeParamsDelete; private readonly _pvOrcaSynthesizeParamsSetSpeechRate; private readonly _pvOrcaSynthesizeParamsSetRandomState; private readonly _pvOrcaPcmDelete; private readonly _pvOrcaWordAlignmentsDelete; private readonly _streamPcmAddressAddress; private readonly _pvOrcaStreamOpen; private readonly _pvOrcaStreamSynthesize; private readonly _pvOrcaStreamFlush; private readonly _pvOrcaStreamClose; private readonly _functionMutex; private static _wasm; private static _wasmSimd; private static _sdk; private static _orcaMutex; private constructor(); /** * Get Orca engine version. */ get version(): string; /** * Get sample rate. */ get sampleRate(): number; /** * Get valid characters. */ get validCharacters(): string[]; /** * Get maximum character limit. */ get maxCharacterLimit(): number; /** * Set base64 wasm file. * @param wasm Base64'd wasm file to use to initialize wasm. */ static setWasm(wasm: string): void; /** * Set base64 wasm file with SIMD feature. * @param wasmSimd Base64'd wasm file to use to initialize wasm. */ static setWasmSimd(wasmSimd: string): void; static setSdk(sdk: string): void; /** * Creates an instance of the Picovoice Orca Text-to-Speech engine. * Behind the scenes, it requires the WebAssembly code to load and initialize before * it can create an instance. * * @param accessKey AccessKey obtained from Picovoice Console (https://console.picovoice.ai/) * @param model Orca model options. * @param model.base64 The model in base64 string to initialize Orca. * @param model.publicPath The model path relative to the public directory. * @param model.customWritePath Custom path to save the model in storage. * Set to a different name to use multiple models across `orca` instances. * @param model.forceWrite Flag to overwrite the model in storage even if it exists. * @param model.version Version of the model file. Increment to update the model file in storage. * * @returns An instance of the Orca engine. */ static create(accessKey: string, model: OrcaModel): Promise<Orca>; static _init(accessKey: string, modelPath: string): Promise<Orca>; /** * Generates audio from text. The returned audio contains the speech representation of the text. * The maximum number of characters per call to `.synthesize()` is `.maxCharacterLimit`. * Allowed characters are lower-case and upper-case letters and punctuation marks that can be retrieved with `.validCharacters`. * Custom pronunciations can be embedded in the text via the syntax `{word|pronunciation}`. * The pronunciation is expressed in ARPAbet format, e.g.: "I {live|L IH V} in {Sevilla|S EH V IY Y AH}". * * @param text A string of text. * @param synthesizeParams Optional configuration arguments. * @param synthesizeParams.speechRate Configure the rate of speech of the synthesized speech. * @param synthesizeParams.randomState Configure the random seed for the synthesized speech. * * @return A result object containing the generated audio as a sequence of 16-bit linearly-encoded integers * and a sequence of OrcaAlignment objects representing the word alignments. */ synthesize(text: string, synthesizeParams?: OrcaSynthesizeParams): Promise<OrcaSynthesizeResult>; /** * Opens a stream for streaming text synthesis. * * @param synthesizeParams Optional configuration arguments. * @param synthesizeParams.speechRate Configure the rate of speech of the synthesized speech. * @param synthesizeParams.randomState Configure the random seed for the synthesized speech. * * @returns An instance of OrcaStream. */ streamOpen(synthesizeParams?: OrcaSynthesizeParams): Promise<OrcaStream>; /** * Releases resources acquired by WebAssembly module. */ release(): Promise<void>; private static initWasm; private static getMessageStack; } export {}; //# sourceMappingURL=orca.d.ts.map