# Voice Input

> A microphone control with a live level meter and timer, plus a hook that records with getUserMedia and MediaRecorder.

Source: https://www.honestui.com/docs/ai/voice-input

```tsx
"use client"

import * as React from "react"

import { VoiceInput, type VoiceInputState } from "@/components/ai/voice-input"

const SAMPLE_TRANSCRIPT = "Summarize the failing billing tests and suggest a fix for the rounding one."

/** A speech-like level: syllables over a slow breath, with a little grain. */
function useSampleLevel(active: boolean) {
  const [level, setLevel] = React.useState(0)
  React.useEffect(() => {
    if (!active) return
    const started = performance.now()
    const timer = window.setInterval(() => {
      const t = (performance.now() - started) / 1000
      const syllable = Math.max(0, Math.sin(t * 9) * Math.sin(t * 2.3 + 1))
      const breath = 0.55 + 0.45 * Math.sin(t * 0.9)
      setLevel(Math.min(1, 0.08 + syllable * breath * 0.85 + Math.random() * 0.08))
    }, 60)
    return () => {
      window.clearInterval(timer)
      setLevel(0)
    }
  }, [active])
  return level
}

/**
 * No microphone is used. The states run on timers and the level is
 * generated, so the preview shows the whole flow without asking for access.
 */
export function VoiceInputDemo() {
  const [state, setState] = React.useState<VoiceInputState>("idle")
  const level = useSampleLevel(state === "recording")
  const timer = React.useRef<number | undefined>(undefined)
  React.useEffect(() => () => window.clearTimeout(timer.current), [])

  const later = (next: VoiceInputState, ms: number) => {
    window.clearTimeout(timer.current)
    timer.current = window.setTimeout(() => setState(next), ms)
  }

  return (
    <div className="flex w-full max-w-sm min-w-0 flex-col gap-[var(--hui-space-4)]">
      <VoiceInput
        state={state}
        level={level}
        onStart={() => {
          setState("requesting")
          later("recording", 700)
        }}
        onStop={() => {
          setState("transcribing")
          later("done", 1400)
        }}
        onCancel={() => {
          window.clearTimeout(timer.current)
          setState("idle")
        }}
      />
      <p
        className="m-0 min-h-10 px-[var(--hui-space-2)] text-center text-[var(--hui-color-foreground-base-secondary)] [font-size:var(--hui-font-size-regular)] [line-height:20px]"
      >
        {state === "done" ? `“${SAMPLE_TRANSCRIPT}”` : null}
      </p>
    </div>
  )
}

```

## Overview [#overview]

Voice Input is a microphone control. It moves through idle, waiting for microphone access, listening with a level meter and timer, transcribing, and done or failed. Stop sends the recording on; cancel throws it away.

The component shows the state you pass. Recording and transcription are your app's work. For the first, `useVoiceRecorder` records from the microphone and hands you the audio as a Blob. Send it to your transcription service and put the text in your composer.

The previews on this page don't use your microphone unless you press "Try with your microphone".

## Anatomy [#anatomy]

<Anatomy title="VoiceInput">
  <AnatomyItem name="Microphone" description="Starts recording, and shows a spinner while waiting or transcribing" />

  <AnatomyItem name="Level meter" description="Bars that scroll with the loudness while listening" />

  <AnatomyItem name="Timer" description="Time recorded, as m:ss" />

  <AnatomyItem name="Cancel and Stop" description="Discard the recording, or finish it" />

  <AnatomyItem name="Status" description="A line in words for each state" />
</Anatomy>

Installing `voice-input` brings [Button](/docs/components/button) along as a registry dependency.

## Behavior [#behavior]

### States [#states]

| State          | Shows                                              |
| -------------- | -------------------------------------------------- |
| `idle`         | The microphone button                              |
| `requesting`   | A spinner while the browser asks for access        |
| `recording`    | Cancel, the level meter, the timer, and Stop       |
| `transcribing` | A spinner until your service replies               |
| `done`         | A check and "Added to your message"                |
| `error`        | The reason, and the microphone button to try again |

Pass `message` to replace the status line, such as the reason access was refused.

### Level [#level]

Pass `level`, from 0 to 1, from wherever you measure it. Or pass a `stream` and the component measures it with Web Audio. The meter samples the level at a steady pace, so it scrolls at the same speed however often the level changes.

When you leave `elapsed` out, the timer counts from the moment recording starts.

### Sizes [#sizes]

`default` stands on its own in a panel. `compact` fits a composer's toolbar: a small microphone that grows into a pill while listening.

### useVoiceRecorder [#usevoicerecorder]

The hook does nothing until you call `start`, so the browser asks for the microphone only when the person presses it. `stop` finishes the recording and calls `onRecorded` with the Blob; `cancel` discards it. The microphone is released when recording ends or the component unmounts.

## Accessibility [#accessibility]

Each button is named: "Start voice input", "Stop recording", and "Cancel recording". The status is a polite live region, so each change of state is announced. The timer has the timer role. The meter is decorative. Focus moves to the control that replaces the one pressed, and <kbd>Escape</kbd> cancels while focus is inside. With reduced motion, the spinner and the halo do not animate.

| Key                                  | Action                 |
| ------------------------------------ | ---------------------- |
| <kbd>Enter</kbd> or <kbd>Space</kbd> | Start, stop, or cancel |
| <kbd>Escape</kbd>                    | Cancel while recording |

## Installation [#installation]


  

  
    <CliBlock commands="[&#x22;@honestai/voice-input&#x22;]" />
  

  
    
      
        Add the components it is built from.
      

      <CliBlock commands="[&#x22;button&#x22;]" />

      
        Copy and paste this file into your project.
      

      ### components/ai/voice-input.tsx

```tsx
"use client"

import * as React from "react"
import {
  Check as CheckIcon,
  CircleAlert as CircleAlertIcon,
  LoaderCircle as LoaderCircleIcon,
  Mic as MicIcon,
  Square as StopIcon,
  X as XIcon,
} from "honestui/icons"

import { cn } from "@/lib/utils"
import { Button } from "@/components/honest-ui/ui/button"

export type VoiceInputState = "idle" | "requesting" | "recording" | "transcribing" | "done" | "error"

export type VoiceInputLabels = {
  group: string
  start: string
  stop: string
  cancel: string
  retry: string
  idle: string
  requesting: string
  recording: string
  transcribing: string
  done: string
  error: string
}

export const defaultVoiceInputLabels: VoiceInputLabels = {
  group: "Voice input",
  start: "Start voice input",
  stop: "Stop recording",
  cancel: "Cancel recording",
  retry: "Try again",
  idle: "Tap to speak",
  requesting: "Waiting for microphone access",
  recording: "Listening",
  transcribing: "Transcribing",
  done: "Added to your message",
  error: "The microphone isn't available",
}

const metaText =
  "[font-size:var(--hui-font-size-small)] [letter-spacing:var(--hui-letter-spacing-small)] [line-height:var(--hui-line-height-small)]"
const rowText =
  "[font-size:var(--hui-font-size-regular)] [letter-spacing:var(--hui-letter-spacing-regular)] [line-height:var(--hui-line-height-regular)]"

/** m:ss, with hours when a recording runs that long. */
function formatElapsed(ms: number) {
  const total = Math.max(0, Math.floor(ms / 1000))
  const hours = Math.floor(total / 3600)
  const minutes = Math.floor((total % 3600) / 60)
  const seconds = String(total % 60).padStart(2, "0")
  return hours > 0 ? `${hours}:${String(minutes).padStart(2, "0")}:${seconds}` : `${minutes}:${seconds}`
}

/* -------------------------------------------------------------------------- */
/* Audio level                                                                 */
/* -------------------------------------------------------------------------- */

type AudioContextConstructor = typeof AudioContext

function getAudioContext(): AudioContextConstructor | undefined {
  if (typeof window === "undefined") return undefined
  return (
    window.AudioContext ??
    (window as unknown as { webkitAudioContext?: AudioContextConstructor }).webkitAudioContext
  )
}

/**
 * The loudness of a stream, 0 to 1, read with Web Audio. Returns 0 without a
 * stream. Nothing is recorded or sent anywhere.
 */
export function useStreamLevel(stream: MediaStream | null | undefined) {
  const [level, setLevel] = React.useState(0)

  React.useEffect(() => {
    const Context = getAudioContext()
    if (!stream || !Context || stream.getAudioTracks().length === 0) return

    const context = new Context()
    const source = context.createMediaStreamSource(stream)
    const analyser = context.createAnalyser()
    analyser.fftSize = 512
    analyser.smoothingTimeConstant = 0.6
    source.connect(analyser)
    const samples = new Uint8Array(analyser.fftSize)
    let frame = 0
    let smoothed = 0

    const tick = () => {
      analyser.getByteTimeDomainData(samples)
      let sum = 0
      for (const sample of samples) {
        const centered = (sample - 128) / 128
        sum += centered * centered
      }
      // Speech sits low in RMS terms; the curve lifts it into a readable range.
      const rms = Math.sqrt(sum / samples.length)
      const next = Math.min(1, Math.pow(rms * 3.2, 0.7))
      smoothed = smoothed * 0.65 + next * 0.35
      setLevel(smoothed)
      frame = requestAnimationFrame(tick)
    }
    frame = requestAnimationFrame(tick)

    return () => {
      cancelAnimationFrame(frame)
      source.disconnect()
      void context.close()
      setLevel(0)
    }
  }, [stream])

  return stream ? level : 0
}

/* -------------------------------------------------------------------------- */
/* Recorder hook                                                               */
/* -------------------------------------------------------------------------- */

export type VoiceRecorderState = "idle" | "requesting" | "recording" | "error"

export type UseVoiceRecorderOptions = {
  /** Called with the recording when it stops. Send it to your transcription service. */
  onRecorded?: (blob: Blob) => void
  /** Preferred container, such as "audio/webm". The browser's default otherwise. */
  mimeType?: string
  /** Passed to getUserMedia. */
  constraints?: MediaTrackConstraints
}

export type UseVoiceRecorderResult = {
  state: VoiceRecorderState
  /** Loudness while recording, 0 to 1. */
  level: number
  /** Milliseconds since recording started. */
  elapsed: number
  /** The last finished recording. */
  blob: Blob | null
  error: Error | null
  stream: MediaStream | null
  start: () => Promise<void>
  stop: () => void
  cancel: () => void
}

/**
 * Microphone recording with getUserMedia and MediaRecorder. Nothing happens
 * until you call start, so the browser asks for the microphone only then.
 */
export function useVoiceRecorder({
  onRecorded,
  mimeType,
  constraints,
}: UseVoiceRecorderOptions = {}): UseVoiceRecorderResult {
  const [state, setState] = React.useState<VoiceRecorderState>("idle")
  const [stream, setStream] = React.useState<MediaStream | null>(null)
  const [blob, setBlob] = React.useState<Blob | null>(null)
  const [error, setError] = React.useState<Error | null>(null)
  const [elapsed, setElapsed] = React.useState(0)
  const recorderRef = React.useRef<MediaRecorder | null>(null)
  const discardRef = React.useRef(false)
  const startedAt = React.useRef(0)
  const onRecordedRef = React.useRef(onRecorded)
  React.useEffect(() => {
    onRecordedRef.current = onRecorded
  }, [onRecorded])

  const level = useStreamLevel(state === "recording" ? stream : null)

  React.useEffect(() => {
    if (state !== "recording") return
    const timer = window.setInterval(() => setElapsed(Date.now() - startedAt.current), 250)
    return () => window.clearInterval(timer)
  }, [state])

  const release = React.useCallback((current: MediaStream | null) => {
    current?.getTracks().forEach((track) => track.stop())
    setStream(null)
  }, [])

  const start = React.useCallback(async () => {
    if (recorderRef.current) return
    if (typeof navigator === "undefined" || !navigator.mediaDevices?.getUserMedia || typeof MediaRecorder === "undefined") {
      setError(new Error("Recording is not supported in this browser."))
      setState("error")
      return
    }

    setError(null)
    setState("requesting")
    let media: MediaStream
    try {
      media = await navigator.mediaDevices.getUserMedia({ audio: constraints ?? true })
    } catch (cause) {
      setError(cause instanceof Error ? cause : new Error(String(cause)))
      setState("error")
      return
    }

    const type = mimeType && MediaRecorder.isTypeSupported(mimeType) ? mimeType : undefined
    const recorder = new MediaRecorder(media, type ? { mimeType: type } : undefined)
    const chunks: Blob[] = []
    discardRef.current = false
    recorder.ondataavailable = (event) => {
      if (event.data.size > 0) chunks.push(event.data)
    }
    recorder.onstop = () => {
      recorderRef.current = null
      release(media)
      setState("idle")
      if (discardRef.current) return
      const recording = new Blob(chunks, { type: recorder.mimeType || type || "audio/webm" })
      setBlob(recording)
      onRecordedRef.current?.(recording)
    }

    recorderRef.current = recorder
    startedAt.current = Date.now()
    setElapsed(0)
    setStream(media)
    recorder.start()
    setState("recording")
  }, [constraints, mimeType, release])

  const stop = React.useCallback(() => {
    if (recorderRef.current?.state === "recording") recorderRef.current.stop()
  }, [])

  const cancel = React.useCallback(() => {
    discardRef.current = true
    stop()
  }, [stop])

  // Let go of the microphone if the component goes away mid-recording.
  React.useEffect(
    () => () => {
      discardRef.current = true
      const recorder = recorderRef.current
      if (recorder?.state === "recording") recorder.stop()
      recorder?.stream.getTracks().forEach((track) => track.stop())
    },
    []
  )

  return { state, level, elapsed, blob, error, stream, start, stop, cancel }
}

/* -------------------------------------------------------------------------- */
/* Level bars                                                                  */
/* -------------------------------------------------------------------------- */

/** Samples the level at a steady pace, so the bars scroll at the same speed however often it changes. */
function useLevelHistory(level: number, count: number, active: boolean) {
  const [history, setHistory] = React.useState<number[]>(() => Array(count).fill(0))
  const latest = React.useRef(level)
  React.useEffect(() => {
    latest.current = level
  }, [level])

  React.useEffect(() => {
    if (!active) return
    const timer = window.setInterval(() => {
      setHistory((current) => [...current.slice(-(count - 1)), latest.current])
    }, 70)
    return () => {
      window.clearInterval(timer)
      setHistory(Array(count).fill(0))
    }
  }, [active, count])

  return history.length === count ? history : Array<number>(count).fill(0)
}

function LevelBars({ levels, className }: { levels: number[]; className?: string }) {
  return (
    <span
      aria-hidden="true"
      data-slot="voice-input-meter"
      className={cn("flex h-6 min-w-0 items-center gap-[2px]", className)}
    >
      {levels.map((value, index) => (
        <span
          key={index}
          className="w-[2px] shrink-0 rounded-full bg-[var(--hui-color-foreground-base-secondary)]"
          style={{ height: `${Math.round(12 + Math.min(1, Math.max(0, value)) * 88)}%` }}
        />
      ))}
    </span>
  )
}

/* -------------------------------------------------------------------------- */
/* Voice input                                                                 */
/* -------------------------------------------------------------------------- */

export type VoiceInputProps = Omit<React.ComponentProps<"div">, "children"> & {
  state: VoiceInputState
  /** Loudness 0 to 1 while recording. Takes precedence over stream. */
  level?: number
  /** A live stream to read the level from, when you don't pass level. */
  stream?: MediaStream | null
  /** Milliseconds recorded. Counted from the start of recording when you leave it out. */
  elapsed?: number
  /** Replaces the status line, such as the reason recording failed. */
  message?: string
  /** "compact" fits a composer's toolbar. "default" stands on its own. */
  size?: "default" | "compact"
  onStart?: () => void
  onStop?: () => void
  onCancel?: () => void
  disabled?: boolean
  labels?: Partial<VoiceInputLabels>
}

/**
 * A microphone control: start, a level meter and timer while listening, then
 * transcribing and done. It shows the state you pass; recording and
 * transcription are your app's work, or use useVoiceRecorder for the first.
 */
function VoiceInput({
  state,
  level: levelProp,
  stream,
  elapsed: elapsedProp,
  message,
  size = "default",
  onStart,
  onStop,
  onCancel,
  disabled,
  labels: labelsProp,
  className,
  ...props
}: VoiceInputProps) {
  const labels = React.useMemo(() => ({ ...defaultVoiceInputLabels, ...labelsProp }), [labelsProp])
  const recording = state === "recording"
  const streamLevel = useStreamLevel(levelProp === undefined && recording ? stream : null)
  const level = levelProp ?? streamLevel
  const compact = size === "compact"
  const history = useLevelHistory(level, compact ? 14 : 36, recording)

  // An internal clock for when the app does not count.
  const [ownElapsed, setOwnElapsed] = React.useState(0)
  React.useEffect(() => {
    if (!recording || elapsedProp !== undefined) return
    const startedAt = Date.now()
    const timer = window.setInterval(() => setOwnElapsed(Date.now() - startedAt), 250)
    return () => {
      window.clearInterval(timer)
      setOwnElapsed(0)
    }
  }, [recording, elapsedProp])
  const elapsed = elapsedProp ?? ownElapsed

  const status = message ?? labels[state]

  // Focus follows the control that replaces the one pressed, but only after a
  // press here, so state changes from elsewhere never pull focus in.
  const rootRef = React.useRef<HTMLDivElement>(null)
  const followFocus = React.useRef(false)
  const press = (handler?: () => void) => () => {
    followFocus.current = true
    handler?.()
  }
  React.useEffect(() => {
    if (!followFocus.current) return
    const slot = state === "recording" ? "voice-input-stop" : "voice-input-start"
    const target = rootRef.current?.querySelector<HTMLButtonElement>(`[data-slot="${slot}"]`)
    if (target && !target.disabled) {
      target.focus()
      followFocus.current = false
    }
  }, [state])

  React.useEffect(() => {
    if (!recording) return
    const onKey = (event: KeyboardEvent) => {
      if (event.key === "Escape" && rootRef.current?.contains(document.activeElement)) {
        followFocus.current = true
        onCancel?.()
      }
    }
    document.addEventListener("keydown", onKey)
    return () => document.removeEventListener("keydown", onKey)
  }, [recording, onCancel])

  const startButton = (
    <Button
      variant={compact ? "ghost" : "secondary"}
      size={compact ? "sm" : "default"}
      data-slot="voice-input-start"
      aria-label={state === "error" ? labels.retry : labels.start}
      disabled={disabled || state === "requesting" || state === "transcribing"}
      onClick={press(onStart)}
      className={cn(
        "shrink-0 rounded-full",
        compact ? "size-8 min-h-8 p-0" : "size-14 min-h-14 p-0 [&_svg]:size-5!"
      )}
    >
      {state === "requesting" || state === "transcribing" ? (
        <LoaderCircleIcon aria-hidden="true" className="motion-safe:animate-spin" />
      ) : (
        <MicIcon aria-hidden="true" />
      )}
    </Button>
  )

  const cancelButton = (
    <Button
      variant="ghost"
      size="sm"
      data-slot="voice-input-cancel"
      aria-label={labels.cancel}
      onClick={press(onCancel)}
      className="size-8 min-h-8 shrink-0 rounded-full p-0 text-[var(--hui-color-foreground-base-secondary)]"
    >
      <XIcon aria-hidden="true" />
    </Button>
  )

  const stopButton = (
    <Button
      variant="default"
      size="sm"
      data-slot="voice-input-stop"
      aria-label={labels.stop}
      onClick={press(onStop)}
      className="size-8 min-h-8 shrink-0 rounded-full p-0"
    >
      <StopIcon aria-hidden="true" className="size-3! fill-current" />
    </Button>
  )

  const timer = (
    <span
      role="timer"
      aria-label={formatElapsed(elapsed)}
      data-slot="voice-input-elapsed"
      className={cn("shrink-0 tabular-nums text-[var(--hui-color-foreground-base-primary)]", metaText)}
    >
      {formatElapsed(elapsed)}
    </span>
  )

  const statusIcon =
    state === "done" ? (
      <CheckIcon aria-hidden="true" className="size-3.5 shrink-0" />
    ) : state === "error" ? (
      <CircleAlertIcon aria-hidden="true" className="size-3.5 shrink-0 text-[var(--hui-color-foreground-danger-primary)]" />
    ) : null

  if (compact) {
    return (
      <div
        ref={rootRef}
        role="group"
        aria-label={labels.group}
        data-slot="voice-input"
        data-state={state}
        data-size="compact"
        className={cn(
          "inline-flex min-w-0 items-center gap-[var(--hui-space-2)] text-[var(--hui-color-foreground-base-secondary)]",
          recording &&
            "rounded-full border-[0.5px] border-[var(--hui-color-border-base-primary)] bg-[var(--hui-color-background-base-primary)] py-[2px] ps-[2px] pe-[var(--hui-space-2)] shadow-[var(--hui-shadow-feather)]",
          className
        )}
        {...props}
      >
        {recording ? (
          <>
            {cancelButton}
            <LevelBars levels={history} className="h-4 px-[var(--hui-space-2)]" />
            {timer}
            {stopButton}
          </>
        ) : (
          <>
            {(state === "transcribing" || state === "error" || state === "requesting") && (
              <span className={cn("inline-flex min-w-0 items-center gap-[var(--hui-space-2)] ps-[var(--hui-space-2)]", metaText)}>
                {statusIcon}
                <span className="truncate">{status}</span>
              </span>
            )}
            {startButton}
          </>
        )}
        <span aria-live="polite" className="sr-only">
          {state === "idle" ? "" : status}
        </span>
      </div>
    )
  }

  return (
    <div
      ref={rootRef}
      role="group"
      aria-label={labels.group}
      data-slot="voice-input"
      data-state={state}
      data-size="default"
      className={cn(
        "flex w-full min-w-0 flex-col items-center gap-[var(--hui-space-5)] rounded-[var(--hui-radius-4)] border-[0.5px] border-[var(--hui-color-border-base-primary)] bg-[var(--hui-color-background-base-primary)] px-[var(--hui-space-6)] py-[var(--hui-space-7)] text-[var(--hui-color-foreground-base-primary)] shadow-[var(--hui-shadow-feather)]",
        rowText,
        className
      )}
      {...props}
    >
      <div className="flex h-14 w-full min-w-0 items-center justify-center">
        {recording ? (
          <div className="flex w-full min-w-0 items-center justify-center gap-[var(--hui-space-4)]">
            {cancelButton}
            <LevelBars levels={history} className="h-8 justify-center" />
            {stopButton}
          </div>
        ) : (
          <span className="relative inline-flex">
            {/* A soft halo while the browser asks, so the wait reads as the app's, not a hang. */}
            {state === "requesting" && (
              <span
                aria-hidden="true"
                className="absolute inset-0 rounded-full bg-[var(--hui-color-background-neutral-secondary)] motion-safe:animate-ping"
              />
            )}
            {startButton}
          </span>
        )}
      </div>
      <div className="flex min-w-0 flex-col items-center gap-[var(--hui-space-1)] text-center">
        <p
          aria-live="polite"
          data-slot="voice-input-status"
          className={cn(
            "m-0 inline-flex min-w-0 items-center gap-[var(--hui-space-2)] [font-weight:var(--hui-font-weight-medium)]",
            state === "error" && "text-[var(--hui-color-foreground-base-primary)]"
          )}
        >
          {statusIcon}
          <span className="min-w-0">{status}</span>
        </p>
        <p className={cn("m-0 h-4 text-[var(--hui-color-foreground-base-secondary)] tabular-nums", metaText)}>
          {recording ? timer : state === "error" && onStart ? labels.retry : null}
        </p>
      </div>
    </div>
  )
}

export { VoiceInput }

```

      
        Update the import paths to match your project setup.
      
    
  


## Usage [#usage]

```tsx
import { useVoiceRecorder, VoiceInput } from "@/components/ai/voice-input"

const transcribe = useTranscription()
const recorder = useVoiceRecorder({
  onRecorded: (blob) => transcribe.mutate(blob, { onSuccess: (text) => appendToComposer(text) }),
})

<VoiceInput
  size="compact"
  state={transcribe.isPending ? "transcribing" : recorder.state}
  level={recorder.level}
  elapsed={recorder.elapsed}
  message={recorder.error?.message}
  onStart={recorder.start}
  onStop={recorder.stop}
  onCancel={recorder.cancel}
/>
```

## Examples [#examples]

### In a composer [#in-a-composer]

The compact size beside a send button. It is simulated until you choose "Try with your microphone"; then it records for real and plays the recording back, since transcription is your app's step.

```tsx
"use client"

import * as React from "react"
import { ArrowUp as ArrowUpIcon } from "honestui/icons"

import { Button } from "@/components/honest-ui/ui/button"
import {
  useVoiceRecorder,
  VoiceInput,
  type VoiceInputState,
} from "@/components/ai/voice-input"

const SAMPLE_TRANSCRIPT = "Which suppliers have orders due this week?"

/**
 * Voice input in a composer's toolbar. By default it is simulated. "Try with
 * your microphone" switches to useVoiceRecorder, which asks for access only
 * then; the recording stays in the page, since transcription is your app's step.
 */
export function VoiceInputComposer() {
  const [useMic, setUseMic] = React.useState(false)
  const [text, setText] = React.useState("")
  const [simState, setSimState] = React.useState<VoiceInputState>("idle")
  const [simLevel, setSimLevel] = React.useState(0)
  const [recordingUrl, setRecordingUrl] = React.useState<string | null>(null)
  const [transcribing, setTranscribing] = React.useState(false)
  const timer = React.useRef<number | undefined>(undefined)

  const recorder = useVoiceRecorder({
    onRecorded: (blob) => {
      setTranscribing(true)
      setRecordingUrl(URL.createObjectURL(blob))
      // Where your app would send the blob to a transcription service.
      timer.current = window.setTimeout(() => setTranscribing(false), 600)
    },
  })

  React.useEffect(() => {
    if (simState !== "recording") return
    const started = performance.now()
    const interval = window.setInterval(() => {
      const t = (performance.now() - started) / 1000
      setSimLevel(Math.min(1, 0.1 + Math.max(0, Math.sin(t * 8) * Math.sin(t * 1.7)) * 0.8 + Math.random() * 0.1))
    }, 60)
    return () => window.clearInterval(interval)
  }, [simState])

  React.useEffect(() => () => window.clearTimeout(timer.current), [])
  React.useEffect(() => () => {
    if (recordingUrl) URL.revokeObjectURL(recordingUrl)
  }, [recordingUrl])

  const micState: VoiceInputState =
    recorder.state === "error" ? "error" : transcribing ? "transcribing" : recorder.state
  const state = useMic ? micState : simState

  return (
    <div className="flex w-full max-w-lg min-w-0 flex-col gap-[var(--hui-space-4)]">
      <div className="flex min-w-0 flex-col gap-[var(--hui-space-3)] rounded-[var(--hui-radius-5)] border-[0.5px] border-[var(--hui-color-border-base-primary)] bg-[var(--hui-color-background-base-primary)] p-[var(--hui-space-4)] shadow-[var(--hui-shadow-soft)]">
        <textarea
          aria-label="Message"
          rows={2}
          value={text}
          placeholder="Ask about your orders"
          onChange={(event) => setText(event.target.value)}
          className="w-full resize-none border-0 bg-transparent px-[var(--hui-space-2)] text-[var(--hui-color-foreground-base-primary)] outline-none placeholder:text-[var(--hui-color-foreground-base-secondary)] [font-size:var(--hui-font-size-regular)] [line-height:20px]"
        />
        <div className="flex min-w-0 items-center justify-end gap-[var(--hui-space-2)]">
          <VoiceInput
            size="compact"
            state={state}
            level={useMic ? recorder.level : simLevel}
            elapsed={useMic ? recorder.elapsed : undefined}
            message={useMic && recorder.error ? recorder.error.message : undefined}
            onStart={() => {
              if (useMic) {
                void recorder.start()
                return
              }
              setSimState("recording")
            }}
            onStop={() => {
              if (useMic) {
                recorder.stop()
                return
              }
              setSimState("transcribing")
              timer.current = window.setTimeout(() => {
                setSimState("idle")
                setText((current) => (current ? `${current} ${SAMPLE_TRANSCRIPT}` : SAMPLE_TRANSCRIPT))
              }, 1200)
            }}
            onCancel={() => (useMic ? recorder.cancel() : setSimState("idle"))}
          />
          <Button size="sm" aria-label="Send" disabled={!text.trim()} className="size-8 min-h-8 rounded-full p-0">
            <ArrowUpIcon aria-hidden="true" />
          </Button>
        </div>
      </div>
      <div className="flex min-w-0 flex-wrap items-center gap-[var(--hui-space-4)]">
        <Button
          variant="ghost"
          size="sm"
          aria-pressed={useMic}
          disabled={state === "recording" || state === "requesting"}
          onClick={() => setUseMic((current) => !current)}
        >
          {useMic ? "Back to the simulation" : "Try with your microphone"}
        </Button>
        {useMic && recordingUrl && (
          // The recording, so you can hear what useVoiceRecorder captured.
          <audio controls src={recordingUrl} aria-label="Your recording" className="h-8 max-w-full" />
        )}
      </div>
    </div>
  )
}

```

## API reference [#api-reference]

### VoiceInput [#voiceinput]

Accepts native div props beyond those listed.

| Prop       | Type                        | Default           | Description                                      |
| ---------- | --------------------------- | ----------------- | ------------------------------------------------ |
| `state`    | `VoiceInputState`           | Required          | The state to show.                               |
| `level`    | `number`                    | None              | Loudness from 0 to 1 while recording.            |
| `stream`   | `MediaStream \| null`       | None              | A stream to measure when you don't pass `level`. |
| `elapsed`  | `number`                    | Counted           | Milliseconds recorded.                           |
| `message`  | `string`                    | The state's words | Replaces the status line.                        |
| `size`     | `"default" \| "compact"`    | `"default"`       | A standalone panel, or a toolbar control.        |
| `onStart`  | `() => void`                | None              | Called by the microphone button.                 |
| `onStop`   | `() => void`                | None              | Called by Stop.                                  |
| `onCancel` | `() => void`                | None              | Called by Cancel and <kbd>Escape</kbd>.          |
| `disabled` | `boolean`                   | `false`           | Disables the microphone button.                  |
| `labels`   | `Partial<VoiceInputLabels>` | English           | Button names and status words.                   |

### useVoiceRecorder [#usevoicerecorder-1]

| Option        | Type                    | Description                                                                  |
| ------------- | ----------------------- | ---------------------------------------------------------------------------- |
| `onRecorded`  | `(blob: Blob) => void`  | Called with the recording when it stops.                                     |
| `mimeType`    | `string`                | A preferred container, such as `"audio/webm"`, when the browser supports it. |
| `constraints` | `MediaTrackConstraints` | Passed to getUserMedia.                                                      |

It returns `state` (`"idle"`, `"requesting"`, `"recording"`, or `"error"`), `level`, `elapsed`, `blob`, `error`, `stream`, and `start`, `stop`, and `cancel`.

### useStreamLevel [#usestreamlevel]

`useStreamLevel(stream)` returns the stream's loudness from 0 to 1, read with Web Audio, or 0 without a stream.
