AI Voice
Voice input for agents: a waveform dictation panel with a live transcript, a face you talk with, and a push-to-talk composer row.
"use client"
import * as React from "react"
import { ArrowUp, Mic, X } from "lucide-react"
import {
AiKitStyles,
AiStyle,
cx,
Face,
Roll,
toneColor,
type AiMood,
type AiShape,
type AiTone,
} from "@/lib/ai-kit"
const CSS = `
.aik-vo-a{display:grid;gap:16px;width:min(100%,400px);padding:16px 18px 18px;border-radius:20px;background:var(--aik-card);box-shadow:var(--aik-sh-pop)}
.aik-vo-top{display:flex;align-items:center;gap:10px;min-width:0}
.aik-vo-dot{display:grid;place-items:center;inline-size:28px;block-size:28px;flex:none;border-radius:50%;color:var(--aik-red);background:color-mix(in oklab,var(--aik-red) 13%,transparent);transition:color var(--aik-d-state),background var(--aik-d-state)}
.aik-vo-a[data-s="done"] .aik-vo-dot{color:var(--aik-green);background:color-mix(in oklab,var(--aik-green) 13%,transparent)}
.aik-vo-top .aik-roll{font-weight:600;font-size:13.5px;line-height:18px}
.aik-vo-clock{margin-left:auto;font:12px/16px var(--aik-mono);color:var(--aik-text-3);font-variant-numeric:tabular-nums}
.aik-vo-bars{display:flex;align-items:center;justify-content:space-between;height:56px}
.aik-vo-bars i{display:block;inline-size:3px;block-size:56px;border-radius:2px;background:var(--aik-text);transform:scaleY(var(--v,.1));transition:transform 110ms linear,background var(--aik-d-state)}
.aik-vo-a[data-s="done"] .aik-vo-bars i{background:var(--aik-text-3)}
.aik-vo-said{min-height:44px;font-size:14.5px;line-height:22px;color:var(--aik-text);overflow-wrap:anywhere}
.aik-vo-said:empty::before{content:"Say what you want built.";color:var(--aik-text-3)}
.aik-vo-b{display:grid;gap:20px;justify-items:center;text-align:center;width:min(100%,340px)}
.aik-vo-orb{position:relative;display:grid;place-items:center;inline-size:132px;block-size:132px}
.aik-vo-orb .aik-face{--aik-fs:88px;--aik-fc:var(--aik-vo-c,var(--aik-f-teal));position:relative}
.aik-vo-lvl{position:absolute;inset:22px;border-radius:50%;background:color-mix(in oklab,var(--aik-vo-c,var(--aik-f-teal)) 16%,transparent);transform:scale(var(--l,1));transition:transform 130ms var(--aik-ease-out),opacity var(--aik-d-state)}
.aik-vo-lvl + .aik-vo-lvl{background:color-mix(in oklab,var(--aik-vo-c,var(--aik-f-teal)) 9%,transparent);transform:scale(calc(1 + (var(--l,1) - 1)*1.9))}
.aik-vo-b[data-s="talk"] .aik-vo-lvl{animation:aik-vo-pulse 1.5s var(--aik-ease-out) infinite}
.aik-vo-b[data-s="talk"] .aik-vo-lvl + .aik-vo-lvl{animation-delay:.5s}
@keyframes aik-vo-pulse{from{transform:scale(1);opacity:1}to{transform:scale(1.46);opacity:0}}
.aik-vo-b .aik-label{display:inline-grid}
.aik-vo-lines{display:grid;gap:6px;justify-items:center;min-height:72px;align-content:start}
.aik-vo-you{color:var(--aik-text-3);font-size:13px;line-height:18px;min-height:18px}
.aik-vo-say{font-size:15px;line-height:23px;max-width:30ch}
@media (prefers-reduced-motion: reduce){.aik-vo-b[data-s="talk"] .aik-vo-lvl{animation:none}}
.aik-vo-c{display:grid;gap:10px;width:min(100%,400px)}
.aik-vo-row{display:flex;align-items:center;gap:8px;height:52px;padding:0 9px 0 16px;border-radius:17px;background:var(--aik-card);box-shadow:var(--aik-sh-pop);min-width:0;transition:padding var(--aik-d-state)}
.aik-vo-c[data-s="rec"] .aik-vo-row{padding-left:9px}
.aik-vo-pane{display:none;align-items:center;gap:10px;flex:1;min-width:0}
.aik-vo-c[data-s="idle"] .aik-vo-pane.i,.aik-vo-c[data-s="rec"] .aik-vo-pane.r,.aik-vo-c[data-s="busy"] .aik-vo-pane.b,.aik-vo-c[data-s="ready"] .aik-vo-pane.t{display:flex;animation:aik-in var(--aik-d-enter) var(--aik-ease-out) both}
.aik-vo-pane.i{color:var(--aik-text-3);font-size:14.5px}
.aik-vo-pane.i .aik-kbd{height:22px;padding:0 7px;font-size:11px;transition:background var(--aik-d-press),color var(--aik-d-press),box-shadow var(--aik-d-press),transform var(--aik-d-press) var(--aik-ease-out)}
.aik-vo-pane.i .aik-kbd[data-hit]{background:var(--aik-pri);color:var(--aik-pri-ink);box-shadow:none;transform:scale(.94)}
.aik-vo-live{inline-size:8px;block-size:8px;flex:none;border-radius:50%;background:var(--aik-red);animation:aik-blink 1.1s steps(1) infinite}
.aik-vo-pane.r .aik-vo-clock{margin:0;color:var(--aik-text);font-size:13px}
.aik-vo-mini{display:flex;align-items:center;gap:3px;height:24px;flex:1;min-width:0;overflow:hidden}
.aik-vo-mini i{display:block;inline-size:3px;block-size:24px;flex:none;border-radius:2px;background:var(--aik-red);transform:scaleY(var(--v,.15));transition:transform 110ms linear}
.aik-vo-pane.b{font-size:14.5px;font-weight:500}
.aik-vo-text{flex:1;min-width:0;font-size:14.5px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
.aik-vo-btn{position:relative;display:grid;place-items:center;inline-size:34px;block-size:34px;flex:none;border-radius:11px;background:var(--aik-raised);color:var(--aik-text-2);
transition:background var(--aik-d-state) var(--aik-ease-out),color var(--aik-d-state) var(--aik-ease-out),transform var(--aik-d-press) var(--aik-ease-out)}
.aik-vo-btn:active,.aik-vo-btn.press{transform:scale(.92)}
.aik-vo-btn > *{grid-area:1/1;transition:opacity var(--aik-d-state) var(--aik-ease-out),transform var(--aik-d-state) var(--aik-ease-out)}
.aik-vo-btn .up{opacity:0;transform:scale(.4)}
.aik-vo-c[data-s="rec"] .aik-vo-btn{background:var(--aik-red);color:#fff}
.aik-vo-c[data-s="ready"] .aik-vo-btn{background:var(--aik-pri);color:var(--aik-pri-ink)}
.aik-vo-c[data-s="ready"] .aik-vo-btn .mic{opacity:0;transform:scale(.4)}
.aik-vo-c[data-s="ready"] .aik-vo-btn .up{opacity:1;transform:none}
.aik-vo-note{display:flex;justify-content:center;color:var(--aik-text-3);font-size:12px;line-height:16px;min-height:16px}
.aik-vo-note .aik-roll{justify-items:center}
@media (prefers-reduced-motion: reduce){.aik-vo-live{animation:none}}
@container (max-width:330px){.aik-vo-pane.i .aik-kbd{display:none}}
.aik-vo-a[data-s="idle"] .aik-vo-dot{color:var(--aik-text-2);background:var(--aik-raised)}
.aik-vo-a[data-s="idle"] .aik-vo-bars i{background:var(--aik-text-3)}
button.aik-vo-dot{cursor:pointer;transition:color var(--aik-d-state),background var(--aik-d-state),transform var(--aik-d-press) var(--aik-ease-out)}
button.aik-vo-dot:active{transform:scale(.92)}
.aik-vo-said:empty::before{content:attr(data-ph)}
button.aik-vo-orb{cursor:pointer;border-radius:50%;transition:transform var(--aik-d-press) var(--aik-ease-out)}
button.aik-vo-orb:active{transform:scale(.97)}
.aik-vo-orb .aik-face{position:relative}
.aik-vo-b[data-s="idle"] .aik-vo-lvl,.aik-vo-b[data-s="think"] .aik-vo-lvl{opacity:.55}
.aik-vo-c:focus-visible{outline:none}
.aik-vo-c:focus-visible .aik-vo-row{box-shadow:var(--aik-sh-pop),0 0 0 2px var(--aik-blue)}
.aik-vo-btn{cursor:pointer;touch-action:none}
input.aik-vo-text{height:100%;border:0;outline:0;background:none}
.aik-vo-mini{justify-content:flex-start}
`
const clamp = (value: number, min: number, max: number) =>
Math.min(max, Math.max(min, value))
/** Splits text into fading word spans, so words added later arrive on their own. */
function Words({ text }: { text: string }) {
if (!text) return null
return (
<>
{text.split(" ").map((word, at) => (
<span key={at} className="aik-w">
{word}{" "}
</span>
))}
</>
)
}
export interface AiWaveformProps extends Omit<
React.HTMLAttributes<HTMLDivElement>,
"onToggle"
> {
/** One level per bar, each from 0 to 1. */
levels: number[]
status?: "idle" | "recording" | "done"
/** The line beside the mic. Defaults to a label for the status. */
label?: string
/** Elapsed time, already formatted. */
time?: string
/** What has been heard so far. New words fade in as it grows. */
transcript?: string
placeholder?: string
/** Makes the mic a button. Called when it is pressed. */
onToggle?: () => void
}
const WAVE_LABEL = { idle: "Ready", recording: "Listening", done: "Got it" }
/** A dictation panel: bars that follow the voice and a transcript that streams in underneath. */
export function AiWaveform({
levels,
status = "recording",
label,
time,
transcript = "",
placeholder = "Say what you want built.",
onToggle,
className,
...rest
}: AiWaveformProps) {
const dot = <Mic size={14} />
return (
<div
{...rest}
className={cx("aik aik-vo-a", className)}
data-s={status === "recording" ? "rec" : status}
>
<AiKitStyles />
<AiStyle id="ai-voice" css={CSS} />
<div className="aik-vo-top">
{onToggle ? (
<button
type="button"
className="aik-vo-dot"
aria-label={
status === "recording" ? "Stop recording" : "Start recording"
}
aria-pressed={status === "recording"}
onClick={onToggle}
>
{dot}
</button>
) : (
<span className="aik-vo-dot">{dot}</span>
)}
<Roll>{label ?? WAVE_LABEL[status]}</Roll>
{time ? <span className="aik-vo-clock">{time}</span> : null}
</div>
<div className="aik-vo-bars" aria-hidden="true">
{levels.map((level, at) => (
<i
key={at}
style={{ "--v": clamp(level, 0.08, 1) } as React.CSSProperties}
/>
))}
</div>
<p className="aik-vo-said" data-ph={placeholder} aria-live="polite">
<Words text={transcript} />
</p>
</div>
)
}
export interface AiVoiceOrbProps extends Omit<
React.HTMLAttributes<HTMLDivElement>,
"onToggle"
> {
status?: "idle" | "listening" | "thinking" | "speaking"
/** How loud the voice is right now, from 0 to 1. Drives the halos while listening. */
level?: number
/** The small caption under the face. Defaults to a label for the status. */
label?: string
/** What the person said. */
heard?: string
/** What the agent is saying back. New words fade in as it grows. */
reply?: string
tone?: AiTone | (string & {})
shape?: AiShape
/** Makes the face a button. Called when it is pressed. */
onToggle?: () => void
}
const ORB: Record<
NonNullable<AiVoiceOrbProps["status"]>,
{ s: string; mood: AiMood; label: string }
> = {
idle: { s: "idle", mood: "happy", label: "Your turn" },
listening: { s: "listen", mood: "listen", label: "Listening" },
thinking: { s: "think", mood: "think", label: "Thinking" },
speaking: { s: "talk", mood: "talk", label: "Speaking" },
}
/** A conversation with a face. Halos follow your voice while it listens, then pulse outward as it talks. */
export function AiVoiceOrb({
status = "listening",
level = 0,
label,
heard = "",
reply = "",
tone = "teal",
shape = "orb",
onToggle,
className,
style,
...rest
}: AiVoiceOrbProps) {
const look = ORB[status]
const inner = (
<>
{[0, 1].map((at) => (
<i
key={at}
className="aik-vo-lvl"
style={
{
"--l": status === "listening" ? 1 + clamp(level, 0, 1) * 0.26 : 1,
} as React.CSSProperties
}
/>
))}
<Face size={88} shape={shape} tone={tone} mood={look.mood} />
</>
)
return (
<div
{...rest}
className={cx("aik aik-vo-b", className)}
data-s={look.s}
style={{ "--aik-vo-c": toneColor(tone), ...style } as React.CSSProperties}
>
<AiKitStyles />
<AiStyle id="ai-voice" css={CSS} />
{onToggle ? (
<button
type="button"
className="aik-vo-orb"
aria-label={
status === "listening" ? "Stop listening" : "Start listening"
}
aria-pressed={status === "listening"}
onClick={onToggle}
>
{inner}
</button>
) : (
<div className="aik-vo-orb">{inner}</div>
)}
<Roll className="aik-label">{label ?? look.label}</Roll>
<div className="aik-vo-lines" aria-live="polite">
<p className="aik-vo-you">{heard}</p>
<p className="aik-vo-say">
<Words text={reply} />
</p>
</div>
</div>
)
}
export interface AiPushToTalkProps extends Omit<
React.HTMLAttributes<HTMLDivElement>,
"onChange"
> {
status?: "idle" | "recording" | "transcribing" | "ready"
/** One level per bar while recording, each from 0 to 1. */
levels?: number[]
/** Elapsed recording time, already formatted. */
time?: string
/** The recognised sentence, shown and editable once `status` is `ready`. */
transcript?: string
onTranscriptChange?: (value: string) => void
/** Called when the mic or the hotkey goes down. */
onStart?: () => void
/** Called when the mic or the hotkey is released. */
onStop?: () => void
/** Called on Escape or the cancel button while recording. */
onCancel?: () => void
/** Called with the transcript on Enter or the send button. */
onSend?: (value: string) => void
/** The line under the row. Defaults to a hint for the status. */
note?: string
/** Shown in the idle hint. The component listens for the Space key. */
hotkey?: string
}
const PTT_NOTE = {
idle: "Works anywhere the composer is focused",
recording: "Release to send. Esc cancels.",
transcribing: "Turning speech into text",
ready: "Enter sends. Edit first if it misheard.",
}
const PTT_STATE = {
idle: "idle",
recording: "rec",
transcribing: "busy",
ready: "ready",
}
/** Voice inside a composer row. Hold the mic or Space to record; release and the row becomes your sentence. */
export function AiPushToTalk({
status = "idle",
levels = [],
time = "0:00",
transcript = "",
onTranscriptChange,
onStart,
onStop,
onCancel,
onSend,
note,
hotkey = "space",
className,
...rest
}: AiPushToTalkProps) {
const [keyDown, setKeyDown] = React.useState(false)
const held = React.useRef(false)
const start = () => {
if (status !== "idle" || held.current) return
held.current = true
onStart?.()
}
const stop = () => {
if (!held.current) return
held.current = false
onStop?.()
}
const fromField = (target: EventTarget) =>
target instanceof HTMLInputElement || target instanceof HTMLButtonElement
return (
<div
{...rest}
className={cx("aik aik-vo-c", className)}
data-s={PTT_STATE[status]}
role="group"
aria-label="Push to talk"
tabIndex={0}
onKeyDown={(event) => {
if (event.key === "Escape" && status === "recording") {
held.current = false
setKeyDown(false)
onCancel?.()
} else if (
event.key === " " &&
!event.repeat &&
!fromField(event.target)
) {
event.preventDefault()
setKeyDown(true)
start()
}
}}
onKeyUp={(event) => {
if (event.key !== " " || fromField(event.target)) return
setKeyDown(false)
stop()
}}
onBlur={(event) => {
if (event.currentTarget.contains(event.relatedTarget)) return
setKeyDown(false)
stop()
}}
>
<AiKitStyles />
<AiStyle id="ai-voice" css={CSS} />
<div className="aik-vo-row">
<div className="aik-vo-pane i">
<span>Hold</span>
<kbd className="aik-kbd" data-hit={keyDown ? "" : undefined}>
{hotkey}
</kbd>
<span>to talk</span>
</div>
<div className="aik-vo-pane r">
<button
type="button"
className="aik-btn icon sm ghost"
aria-label="Cancel"
onClick={() => {
held.current = false
onCancel?.()
}}
>
<X size={16} />
</button>
<i className="aik-vo-live" />
<span className="aik-vo-clock">{time}</span>
<div className="aik-vo-mini" aria-hidden="true">
{levels.map((level, at) => (
<i
key={at}
style={{ "--v": clamp(level, 0.1, 1) } as React.CSSProperties}
/>
))}
</div>
</div>
<div className="aik-vo-pane b">
<span className="aik-shimmer">Transcribing</span>
</div>
<div className="aik-vo-pane t">
<input
className="aik-vo-text"
value={transcript}
aria-label="Transcript"
readOnly={!onTranscriptChange}
onChange={(event) => onTranscriptChange?.(event.target.value)}
onKeyDown={(event) => {
if (event.key === "Enter" && transcript.trim()) {
event.preventDefault()
onSend?.(transcript.trim())
}
}}
/>
</div>
<button
type="button"
className="aik-vo-btn"
aria-label={
status === "ready"
? "Send"
: status === "recording"
? "Release to stop"
: "Hold to record"
}
disabled={status === "transcribing"}
onPointerDown={(event) => {
if (status !== "idle") return
event.currentTarget.setPointerCapture(event.pointerId)
start()
}}
onPointerUp={stop}
onPointerCancel={stop}
onKeyDown={(event) => {
if ((event.key === " " || event.key === "Enter") && !event.repeat)
start()
}}
onKeyUp={(event) => {
if (event.key === " " || event.key === "Enter") stop()
}}
onClick={() => {
if (status === "ready" && transcript.trim())
onSend?.(transcript.trim())
}}
>
<Mic size={16} className="mic" />
<ArrowUp size={16} className="up" />
</button>
</div>
<div className="aik-vo-note">
<Roll>{note ?? PTT_NOTE[status]}</Roll>
</div>
</div>
)
}
Installation
One command adds ai-voice and the shared AI Kit layer it is built on (tokens, faces, marks and brand icons). No animation library, light and dark built in.
$npx shadcn@latest add https://tweenui.dev/r/ai-voice.json
ai-voice-demo
"use client"
import * as React from "react"
import { AiDemo } from "@/lib/ai-demo"
import { paceToken, typeInto, useAiLoop } from "@/lib/ai-kit"
import { AiPushToTalk, AiVoiceOrb, AiWaveform } from "@/registry/tween/ai-voice"
const frame: React.CSSProperties = {
display: "grid",
justifyItems: "center",
width: "100%",
}
const clock = (seconds: number) =>
`0:${String(Math.floor(seconds)).padStart(2, "0")}`
/** A believable voice envelope: a slow swell, a fast flutter and a little noise. */
const speak = (count: number, tick: number, gain = 1) =>
Array.from({ length: count }, (_, at) => {
const swell = 0.5 + 0.5 * Math.sin(tick * 0.55 + at * 0.42)
const flutter = 0.5 + 0.5 * Math.sin(tick * 1.7 + at * 1.9)
const noise = 0.7 + Math.random() * 0.3
return gain * (0.12 + 0.88 * swell * (0.35 + 0.65 * flutter) * noise)
})
const hush = (count: number) => Array.from({ length: count }, () => 0.08)
const frozen = (count: number) =>
Array.from(
{ length: count },
(_, at) => 0.2 + 0.7 * Math.abs(Math.sin(at * 0.9))
)
const SAID =
"Make the cards fan out when the section scrolls in, and slow the last one down."
function Waveform() {
const root = React.useRef<HTMLDivElement>(null)
const [status, setStatus] = React.useState<"idle" | "recording" | "done">(
"recording"
)
const [levels, setLevels] = React.useState(() => frozen(28))
const [seconds, setSeconds] = React.useState(3)
const [said, setSaid] = React.useState(
"Make the cards fan out when the section scrolls in"
)
const [hands, setHands] = React.useState(false)
useAiLoop(
root,
async ({ sleep }) => {
setStatus("recording")
setSaid("")
setSeconds(0)
setLevels(hush(28))
await sleep(500)
let live = true
const meter = (async () => {
for (let tick = 0; live; tick++) {
setLevels(speak(28, tick))
setSeconds(tick / 9)
await sleep(110)
}
})()
const text = (async () => {
await sleep(400)
let line = ""
for (const word of SAID.split(" ")) {
line = line ? `${line} ${word}` : word
setSaid(line)
await sleep(paceToken(word, 150))
}
await sleep(500)
live = false
})()
try {
await Promise.all([meter, text])
} finally {
live = false
}
setLevels(hush(28))
setStatus("done")
await sleep(2200)
},
{ gap: 300, enabled: !hands }
)
// Once the visitor takes over, the mic button runs a live meter by hand.
React.useEffect(() => {
if (!hands || status !== "recording") return
let tick = 0
const timer = window.setInterval(() => {
tick += 1
setLevels(speak(28, tick))
setSeconds(tick / 9)
}, 110)
return () => window.clearInterval(timer)
}, [hands, status])
return (
<div ref={root} style={frame} onPointerDown={() => setHands(true)}>
<AiWaveform
levels={levels}
status={status}
time={clock(seconds)}
transcript={said}
onToggle={() => {
if (status === "recording") {
setStatus("done")
setLevels(hush(28))
} else {
setStatus("recording")
setSaid("")
}
}}
/>
</div>
)
}
const ASKED = "How do I slow down the card ring reveal?"
const ANSWER =
"Pass a longer duration, like 1.5 seconds. The stagger scales with it."
type OrbStatus = "idle" | "listening" | "thinking" | "speaking"
function Orb() {
const root = React.useRef<HTMLDivElement>(null)
const [status, setStatus] = React.useState<OrbStatus>("listening")
const [level, setLevel] = React.useState(0.5)
const [heard, setHeard] = React.useState(ASKED)
const [reply, setReply] = React.useState("")
const [hands, setHands] = React.useState(false)
useAiLoop(
root,
async ({ sleep }) => {
setStatus("listening")
setHeard("")
setReply("")
setLevel(0)
await sleep(500)
let live = true
const pulse = (async () => {
for (let tick = 0; live; tick++) {
setLevel(
(0.5 + 0.5 * Math.sin(tick * 0.9)) * (0.5 + Math.random() * 0.5)
)
await sleep(130)
}
})()
const text = (async () => {
await typeInto(setHeard, ASKED, sleep, 46)
await sleep(500)
live = false
})()
try {
await Promise.all([pulse, text])
} finally {
live = false
}
setLevel(0)
setStatus("thinking")
await sleep(1100)
setStatus("speaking")
let line = ""
for (const word of ANSWER.split(" ")) {
line = line ? `${line} ${word}` : word
setReply(line)
await sleep(paceToken(word, 120))
}
await sleep(500)
setStatus("idle")
await sleep(1800)
},
{ gap: 300, enabled: !hands, still: () => setLevel(0.5) }
)
React.useEffect(() => {
if (!hands || status !== "listening") return
let tick = 0
const timer = window.setInterval(() => {
tick += 1
setLevel((0.5 + 0.5 * Math.sin(tick * 0.9)) * (0.5 + Math.random() * 0.5))
}, 130)
return () => window.clearInterval(timer)
}, [hands, status])
return (
<div ref={root} style={frame} onPointerDown={() => setHands(true)}>
<AiVoiceOrb
status={status}
level={level}
heard={heard}
reply={reply}
label={status === "speaking" ? "Mira is speaking" : undefined}
onToggle={() => {
if (status === "listening") {
setStatus("idle")
} else {
setStatus("listening")
setHeard("Listening to you now")
setReply("")
}
}}
/>
</div>
)
}
const HEARD = "Swap the hero image for the dark one"
type TalkStatus = "idle" | "recording" | "transcribing" | "ready"
function PushToTalk() {
const root = React.useRef<HTMLDivElement>(null)
const [status, setStatus] = React.useState<TalkStatus>("idle")
const [levels, setLevels] = React.useState(() => hush(44))
const [seconds, setSeconds] = React.useState(0)
const [text, setText] = React.useState("")
const [hands, setHands] = React.useState(false)
useAiLoop(
root,
async ({ sleep }) => {
setStatus("idle")
setText("")
setLevels(hush(44))
await sleep(1500)
setStatus("recording")
for (let tick = 0; tick < 30; tick++) {
setLevels(speak(44, tick, 0.95))
setSeconds(tick / 9)
await sleep(110)
}
setLevels(hush(44))
setStatus("transcribing")
await sleep(900)
setStatus("ready")
await typeInto(setText, HEARD, sleep, 16)
await sleep(1900)
},
{
gap: 200,
enabled: !hands,
still: () => {
setStatus("recording")
setSeconds(2)
setLevels(frozen(44))
},
}
)
React.useEffect(() => {
if (!hands) return
if (status === "recording") {
let tick = 0
const timer = window.setInterval(() => {
tick += 1
setLevels(speak(44, tick, 0.95))
setSeconds(tick / 9)
}, 110)
return () => window.clearInterval(timer)
}
if (status === "transcribing") {
const timer = window.setTimeout(() => {
setText(HEARD)
setStatus("ready")
}, 900)
return () => window.clearTimeout(timer)
}
}, [hands, status])
return (
<div
ref={root}
style={frame}
onPointerDown={() => setHands(true)}
onKeyDown={() => setHands(true)}
>
<AiPushToTalk
status={status}
levels={levels}
time={clock(seconds)}
transcript={text}
onTranscriptChange={setText}
onStart={() => {
setSeconds(0)
setStatus("recording")
}}
onStop={() => setStatus("transcribing")}
onCancel={() => setStatus("idle")}
onSend={() => {
setText("")
setStatus("idle")
}}
/>
</div>
)
}
export function AiVoiceDemo({ only }: { only?: string }) {
return (
<AiDemo
only={only}
tabs={[
{ name: "Waveform", node: <Waveform /> },
{ name: "Orb", node: <Orb /> },
{ name: "Push to talk", node: <PushToTalk /> },
]}
/>
)
}
Installation
Usage
import { AiPushToTalk, AiVoiceOrb, AiWaveform } from "@/components/ui/ai-voice"<AiWaveform
levels={levels}
status={recording ? "recording" : "done"}
time="0:03"
transcript={transcript}
onToggle={() => setRecording(!recording)}
/>These components draw the voice UI and report gestures. They do not open the microphone. Feed levels from your own analyser (one number from 0 to 1 per bar) and transcript from your speech-to-text.
Versions
AiWaveformis a dictation panel. Bars scale withlevelsand the transcript streams in underneath, each new word fading in. When recording stops the bars settle and the mic turns green.AiVoiceOrbis a conversation with a face. Two halos followlevelwhile it listens, then pulse outward while it speaks and the reply streams.AiPushToTalkis a composer row. Hold the mic or Space and it becomes a recorder with a clock and a level strip. Release and it shows the sentence in an editable field, with send where the mic was. Escape cancels.
Props
AiWaveform
| Prop | Type | Default | Description |
|---|---|---|---|
levels | number[] | One level per bar, 0 to 1. | |
status | "idle" | "recording" | "done" | "recording" | Colours the mic and the bars. |
label | string | The line beside the mic. | |
time | string | Elapsed time, already formatted. | |
transcript | string | "" | What has been heard so far. |
placeholder | string | Shown while the transcript is empty. | |
onToggle | () => void | Makes the mic a button. |
AiVoiceOrb
| Prop | Type | Default | Description |
|---|---|---|---|
status | "idle" | "listening" | "thinking" | "speaking" | "listening" | Drives the face, the halos and the caption. |
level | number | 0 | Voice level from 0 to 1 while listening. |
heard | string | "" | What the person said. |
reply | string | "" | What the agent says back. |
label | string | Overrides the caption. | |
tone, shape | face look | "teal", "orb" | The face and the halo colour. |
onToggle | () => void | Makes the face a button. |
AiPushToTalk
| Prop | Type | Default | Description |
|---|---|---|---|
status | "idle" | "recording" | "transcribing" | "ready" | "idle" | Which pane the row shows. |
levels | number[] | [] | Level strip while recording. |
time | string | "0:00" | Elapsed recording time. |
transcript | string | "" | The recognised sentence. |
onTranscriptChange | (value: string) => void | Makes the transcript editable. | |
onStart, onStop | () => void | The mic or Space going down and coming up. | |
onCancel | () => void | Escape or the cancel button while recording. | |
onSend | (value: string) => void | Enter or the send button once ready. | |
note | string | Overrides the hint under the row. |