use-speech-recognition
Transcribe speech with the Web Speech API.
| Package | @vizejs/composable/use-speech-recognition |
| Own the source | vize lib pull composable:use-speech-recognition |
| Runtime exports | useSpeechRecognition |
| Gzip budget | 2304 B |
Usage
import { useSpeechRecognition } from "@vizejs/composable/use-speech-recognition";
Runtime contract
| Utility | Category | Stability | SSR | Hydration | Cleanup | Targets | Host globals | Uses |
|---|---|---|---|---|---|---|---|---|
useSpeechRecognition |
media | experimental | deterministic-fallback | caller-managed | reactive-scope | web, desktop | window |
— |
API
useSpeechRecognition
Transcribe speech with the Web Speech API. result accumulates the final transcript of the current session and interimResult the text still being recognized. Options are applied each time listening starts, so a reactive lang takes effect on the next start. Errors are normalized to a closed SpeechRecognitionErrorCode union and exposed through error rather than thrown. The recognizer is aborted and its listeners removed when the owning reactive scope stops. Server rendering: no recognizer is created; supported and listening are false and the transcripts are empty.
function useSpeechRecognition( options: UseSpeechRecognitionOptions = {}, ): SpeechRecognitionControls
const speech = useSpeechRecognition({ lang: "ja-JP" });
speech.start();
watch(speech.result, (text) => console.log(text));
Types
SpeechRecognitionAlternativeLike
One recognition hypothesis.
| Member | Type | Description |
|---|---|---|
transcript |
string |
Recognized text. |
confidence |
number |
Confidence between 0 and 1. |
SpeechRecognitionResultLike
One recognition result: an array-like list of alternatives.
| Member | Type | Description |
|---|---|---|
length |
number |
Number of alternatives. |
isFinal |
boolean |
Whether the result is final (no longer interim). |
SpeechRecognitionResultListLike
Array-like list of every result of the current session.
| Member | Type | Description |
|---|---|---|
length |
number |
Number of results. |
SpeechRecognitionLike
Minimal SpeechRecognition instance consumed by useSpeechRecognition.
| Member | Type | Description |
|---|---|---|
lang |
string |
BCP 47 language tag. |
continuous |
boolean |
Keep listening after the first final result. |
interimResults |
boolean |
Report interim (non-final) results. |
maxAlternatives |
number |
Maximum alternatives per result. |
start |
() => void |
Start listening. Throws InvalidStateError when already started. |
stop |
() => void |
Stop listening and deliver a final result. |
abort |
() => void |
Stop listening without delivering a result. |
SpeechRecognitionFailure
Recognition failure reported by useSpeechRecognition.
| Member | Type | Description |
|---|---|---|
code |
SpeechRecognitionErrorCode |
Normalized error code. |
message |
string |
Implementation-provided message, possibly empty. |
UseSpeechRecognitionOptions
Options for useSpeechRecognition.
| Member | Type | Description |
|---|---|---|
lang? |
MaybeRefOrGetter<string> |
Recognition language, applied when listening starts. |
continuous? |
boolean |
Keep listening after the first final result. |
interimResults? |
boolean |
Report interim results through interimResult. |
maxAlternatives? |
number |
Maximum alternatives per result. |
host? |
MaybeRef<SpeechRecognitionHost | null | undefined> |
Recognizer constructor for alternate runtimes and tests. A ref (not a getter) because the host itself is a constructor function. |
SpeechRecognitionControls
Reactive state and actions returned by useSpeechRecognition.
| Member | Type | Description |
|---|---|---|
supported |
ComputedRef<boolean> |
Whether a speech recognizer is available. |
listening |
Readonly<Ref<boolean>> |
Whether the recognizer is listening. |
result |
Readonly<Ref<string>> |
Concatenated final transcript of the current session. |
interimResult |
Readonly<Ref<string>> |
Concatenated interim transcript not yet final. |
alternatives |
Readonly<ShallowRef<readonly SpeechRecognitionAlternativeLike[]>> |
Alternatives of the most recent result. |
error |
Readonly<ShallowRef<SpeechRecognitionFailure | undefined>> |
Most recent recognition failure, cleared when listening starts. |
start |
() => boolean |
Start listening with the current options, clearing previous results. |
stop |
() => void |
Stop listening and keep the final result. |
abort |
() => void |
Stop listening immediately without a final result. |
toggle |
(value?: boolean) => void |
Start or stop listening. |