SpeechToText class

SpeechToText enables voice captured by the microphone to be transcribe into a stream of text in real time.

Signature:

export declare class SpeechToText extends ExternalObject 

Extends: ExternalObject

Methods

MethodModifiersDescription
cancelTranscribing()Immediately cancel speech to text processing.
isAvailable()Indicate if speech to text will work on the current device
isTranscribing()
setExpectedSpeechType(type)Tune the speech recognition to expect a certain kind of speech. See SpeechType for available options.
setTargetPhrases(phrases)Specify a list of words or phrases to prioritize it over similar sounding words and be more likely to be returned by the recognition.
startTranscribing(onDeviceRecognition)Start converting voice detected by the microphone input into a stream of text wrapped in a Subject.
stopTranscribing()Stop transcribing the microphone input and finishes processing any existing input.

SpeechToText.cancelTranscribing() method

Immediately cancel speech to text processing.

Signature:

cancelTranscribing(): Promise<void>;

Returns:

Promise<void>

Remarks

Only use this method if you need to immediately stop recognition. Do not call this method frequently. Prefer SpeechToText.stopTranscribing(). stopTranscribing or cancelTranscribing must be called when you are finished with the Subject returned by SpeechToText.startTranscribing()

SpeechToText.isAvailable() method

Indicate if speech to text will work on the current device

Signature:

isAvailable(): Promise<boolean>;

Returns:

Promise<boolean>

Remarks

True if the device supports the functionality and permissions have been granted to use it. If this is false transcribing will never return any results.

Must be checked if speech to text is an optional feature of your module.

SpeechToText.isTranscribing() method

Signature:

isTranscribing(): Promise<boolean>;

Returns:

Promise<boolean>

A promise that resolves to true if there is a currently active transcription in progress; false if not.

SpeechToText.setExpectedSpeechType() method

Tune the speech recognition to expect a certain kind of speech. See SpeechType for available options.

Signature:

setExpectedSpeechType(type?: SpeechType): void;

Parameters

ParameterTypeDescription
typeSpeechType(Optional) The kind of speech expected (Default: General)

Returns:

void

SpeechToText.setTargetPhrases() method

Specify a list of words or phrases to prioritize it over similar sounding words and be more likely to be returned by the recognition.

Signature:

setTargetPhrases(phrases: readonly string[] | null): void;

Parameters

ParameterTypeDescription
phrasesreadonly string[] | nullPhrases to prioritize in speech to text results

Returns:

void

Remarks

Target phrases will persist between recognition requests. Set it to null to clear it.

SpeechToText.startTranscribing() method

Start converting voice detected by the microphone input into a stream of text wrapped in a Subject.

Signature:

startTranscribing(onDeviceRecognition?: boolean): Subject<string>;

Parameters

ParameterTypeDescription
onDeviceRecognitionboolean(Optional) True to let the device to transcribe offline (on the device). When false it uses the server. (Default: false)

Returns:

Subject<string>

Every word that's detected is returned in the value returned in the onNext event.

Remarks

You must listen to the onError handler of this subject to be notified of any problems. If an error occurs, you will need to stop transcription and try again before you get any results.

If you do not explicitly configure the locale, the system default will be used. If the user's default language is not supported for speech recognition, the method attempts to fall back to the language used by the keyboard for dictation.

Use the server transcription for the highest quality speech to text results; note however there will be a slight delay when compared to on device recognition. On device recognition is less accurate but faster. If the requested locale is unavailable, an error will be raised.

Example

For example if you utter the phrase "I am 22", it could return the following string in each onNext call as time passes:

"I"
"I am"
"I am 20"
"I am 22"

Notice how the string grows and can change as the context.

Another example: Utter "Eye of the beholder."

"I"
"I of"
"Eye of"
"Eye of the"
"Eye of the beholder"

You can stop transcribing at any time which cancels any outstanding transcription.

Considerations:

  • If transcribing on device there is no time limit to the transcription.
  • If transcribing using the server there is a 60 second time limit per transcription.

SpeechToText.stopTranscribing() method

Stop transcribing the microphone input and finishes processing any existing input.

Signature:

stopTranscribing(): Promise<void>;

Returns:

Promise<void>

Remarks

You must await this method before starting a new transcription task. stopTranscribing or cancelTranscribing must be called when you are finished with the Subject returned by SpeechToText.startTranscribing()