Skip to content
🌏 Translated with the assistance of DeepSeek and ChatGPT

PromptSpeech ​

The speech input of Prompt is powered by the built-in speech module; speechConfig configures it, PromptSpeechTrigger toggles the recording and PromptSpeechPanel renders the recording panel.

Basic Usage ​

Pass speechConfig to Prompt and place PromptSpeechTrigger in the toolbar to start recording. With mode set to 'click', click the trigger to start, then submit with the confirm entry or discard with the cancel entry of the panel; with 'hold', the panel records while it is pressed and submits on release. The recognition result is written into the input content of Prompt.

Built-in browser speech recognition may have compatibility issues and require a suitable network environment.

Speech Mode
Ask anything
Hide source code

Custom Recognition ​

customRecognize is used to take over the built-in speech recognition. You can plug in your own speech recognition model, or send the audio directly to a server for processing.

The following example uses Transformers.js to run the Whisper tiny model locally in the browser and perform local transcription of the recording. The model and runtime are downloaded from a CDN when the page loads (about 45 MB of traffic on the first load; afterwards they are served from the browser cache), and the audio never leaves the browser.

The example sets acceptTimeout to -1, and the custom recognizer calls end() after transcription is complete.

Ask anything
Hide source code

API ​

PromptSpeechTriggerSlots ​

SlotParameterDescriptionVersion
triggertoggle: () => void, recording: boolean, disabled: booleanThe speech toggle area, replacing the internal button.0.2.1
iconrecording: booleanThe icon of the speech toggle.0.2.1

PromptSpeechPanelSlots ​

SlotParameterDescriptionVersion
cancelspeech: PromptSpeechScopeThe cancel entry of the speech recording panel.0.2.1
visualizerspeech: PromptSpeechScopeThe visualizer area of the speech recording panel.0.2.1
textspeech: PromptSpeechScopeThe live text area of the speech recognition.0.2.1
acceptspeech: PromptSpeechScopeThe confirm entry of the speech recording panel.0.2.1

PromptSpeechConfig is the type of speechConfig, covering the following properties and nested configuration types.

PromptSpeechSpectrum ​

AttributeTypeOptionalDefaultDescriptionVersion
minHznumberTrue400The lowest frequency of the spectrum analysis.0.2.1
maxHznumberTrue8000The highest frequency of the spectrum analysis.0.2.1
minDbnumberTrue-80The decibel value mapped to the minimum height.0.2.1
maxDbnumberTrue-25The decibel value mapped to the maximum height.0.2.1
smoothingnumberTrue0.5The smoothing factor of the spectrum changes.0.2.1
fftSizenumberTrue2048The sampling window size used by the spectrum analysis.0.2.1

PromptSpeechBarConfig ​

AttributeTypeOptionalDefaultDescriptionVersion
countnumberTrue36The number of the visualizer bars.0.2.1
minHeightnumberTrue4The minimum height of the visualizer bars.0.2.1
maxHeightnumberTrue48The maximum height of the visualizer bars.0.2.1

PromptSpeechConfig ​

AttributeTypeOptionalDefaultDescriptionVersion
modePromptSpeechModeTrue'click'The interaction mode of the speech recognition.0.2.1
langstringTrueThe language setting of the built-in browser speech recognition, given as a BCP 47 language tag. See BCP 47 language tag for the valid values.0.2.1
barConfigPromptSpeechBarConfigTrueThe configuration of the visualizer bars.0.2.1
spectrumConfigPromptSpeechSpectrumTrueThe configuration of the audio spectrum.0.2.1
customRecognizeCustomRecognizeTrueThe custom recognition process, taking over the internal speech recognition.0.2.1
acceptTimeoutnumberTrue3000The longest waiting time for the recognition result after the recording is confirmed.0.2.1

PromptSpeechScope ​

AttributeTypeOptionalDefaultDescriptionVersion
modePromptSpeechModeFalseThe current speech interaction mode.0.2.1
recordingbooleanFalseWhether a recording session is active.0.2.1
livebooleanFalseWhether the recording is live; false in hold mode before it is pressed.0.2.1
committingbooleanFalseWhether the recording is confirmed and the recognition result is pending.0.2.1
disabledbooleanFalseWhether the speech input is unavailable.0.2.1
supportedbooleanFalseWhether the browser supports the built-in speech recognition.0.2.1
textstringFalseThe current transcript.0.2.1
pendingbooleanFalseWhether the transcript is a pending interim result.0.2.1
levelsnumber[]FalseThe live levels of the visualizer bars, from 0 to 1.0.2.1
source'microphone' | 'simulated' | nullFalseThe source of the bar levels. microphone means the levels come from the live microphone spectrum, simulated means the levels are generated by the animation, and null means there is no level source.0.2.1
barConfigRequired<PromptSpeechBarConfig>FalseThe effective bar configuration after defaults are applied.0.2.1
enter() => voidFalseStarts recording.0.2.1
cancel() => voidFalseDiscards the current recording.0.2.1
accept() => voidFalseConfirms the recording and submits the recognition result.0.2.1
press() => voidFalseHandles the press in hold mode.0.2.1
release() => voidFalseHandles the release in hold mode.0.2.1
ts
export type PromptSpeechScope = {
	mode: PromptSpeechMode
	recording: boolean
	live: boolean
	committing: boolean
	disabled: boolean
	supported: boolean
	text: string
	pending: boolean
	levels: number[]
	source: 'microphone' | 'simulated' | null
	barConfig: Required<PromptSpeechBarConfig>
	enter: () => void
	cancel: () => void
	accept: () => void
	press: () => void
	release: () => void
}

PromptSpeechMode ​

ts
export type PromptSpeechMode = 'click' | 'hold'

CustomRecognizeOptions ​

ts
export interface CustomRecognizeOptions {
	stream: MediaStream | null
	audio: () => Promise<Blob | null>
	setText: (text: string, final?: boolean) => void
	end: () => void
	onAccept: (handler: () => void) => void
	onClose: (handler: (committed: boolean) => void) => void
}

export type CustomRecognize = (options: CustomRecognizeOptions) => void