apphouse
Version:
Component library for React that uses observable state management and theme-able components.
91 lines (90 loc) • 3.42 kB
TypeScript
import { List } from '../models/List';
import { Utterance } from './textToSpeech/Utterance';
type TextToSpeechStatusType = 'speaking' | 'paused' | 'cancelled' | 'idle';
/**
* A class that represents a text to speech synthesizer
* It is a wrapper around the SpeechSynthesis class, that handles multiple utterances
*/
export declare class TextToSpeech {
synthesizer: SpeechSynthesis | null;
selectedVoiceIndex: number;
utterances: List<Utterance>;
currentUtterance: Utterance | null;
textToSpeak: string | null;
status: TextToSpeechStatusType;
constructor();
get voiceList(): SpeechSynthesisVoice[] | undefined;
get voices(): {
label: string;
value: number;
details: SpeechSynthesisVoice;
}[];
setSelectedVoiceIndex: (voiceIndex: number) => void;
private init;
/**
* This method will immediately pause any utterances that are being spoken.
*/
pause: () => void;
/**
* This method will cause the browser to resume speaking an
* utterance that was previously paused.
*/
resume: () => void;
/**
* This method will immediately stop any utterances
* that are being spoken
*/
cancel: () => void;
/**
* Helper method to set a status so the status is observable
* @param status the status to set
*/
private setStatus;
/**
* Speaks the text passed in as an argument or the textToSpeak property.
* If there is a current utterance, it will be cancelled and a new one
* will be created. If it speaking, it will be ignored.
* If an id is passed, it will be used to identify the utterance and speak that utterance.
* @param text the text to speak
*/
speak: (text?: string, id?: string) => void;
private speakUtteranceId;
private speakUtterance;
restart: (utteranceId: string) => void;
setCurrentUtterance: (utterance: Utterance) => void;
/**
* Method to initialize an utterance.
* If the utterance already exists it sets it as the current utterance.
* If it doesn't exist, it creates a new utterance and sets it as the current utterance.
* @param {string} id the id of the utterance, must be unique
*/
initUtterance: (id: string) => void;
/**
* Method to set the rate of the utterance.
* When setting the rate, unfortunately, the speech synthesis API
* does not allow us to set the rate of an utterance on the fly. So we
* have to cancel the current utterance and create a new one with the new rate.
*/
setRate: (id: string, rate: number) => void;
/**
* A method that sets the voice of the speech synthesis.
* When setting a different voice, unfortunately, the speech synthesis API
* does not allow us to set the voice of an utterance on the fly. So we
* have to cancel the current utterance and create a new one with the new
* voice.
*/
setVoice: (id: string, voice: SpeechSynthesisVoice) => void;
/**
* Method to create a new utterance and add it to the list of utterances.
* In addition, it also sets the current utterance to the newly created one.
* It is used privately
* @param id
* @param text
* @param options
* @returns
*/
private createUtterance;
setTextToSpeak: (text: string) => void;
}
export declare const textToSpeech: TextToSpeech;
export {};