{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "voiceprint-demo",
  "type": "registry:block",
  "title": "Voiceprint: Voice Agent Hero",
  "description": "Audio Waveform Background example, Voice Agent Hero: A clinic's voice agent books a same-day appointment on a looping call, agent in blue and patient in ink, and Talk to it prints your own voice from the microphone. A take on the animated background (backdrop) and audio visualizer (waveform). Fits hero and features sections.",
  "author": "KeyframeUI <https://keyframeui.com>",
  "categories": [
    "hero",
    "features",
    "animated-background",
    "audio-visualizer",
    "background",
    "ai",
    "canvas"
  ],
  "docs": "Live preview, examples and props: https://keyframeui.com/components/voiceprint#voiceprint-demo",
  "registryDependencies": [
    "https://keyframeui.com/r/voiceprint.json"
  ],
  "files": [
    {
      "path": "components/voiceprint-demo.tsx",
      "type": "registry:component",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nimport { cn } from \"@/lib/utils\";\nimport { Voiceprint } from \"@/components/ui/voiceprint\";\n\ntype Speaker = \"agent\" | \"caller\";\n\nconst CALL: { who: Speaker; text: string }[] = [\n  { who: \"agent\", text: \"Northside Dental, this is Marlow. How can I help?\" },\n  { who: \"caller\", text: \"Hi, I chipped a tooth last night. Can someone see me today?\" },\n  { who: \"agent\", text: \"Sorry to hear that. Dr. Osei has a 3:40 this afternoon. Shall I hold it for you?\" },\n  { who: \"caller\", text: \"Yes please, 3:40 is perfect.\" },\n  { who: \"agent\", text: \"Booked. I'm texting you the address and a two-minute intake form now.\" },\n  { who: \"caller\", text: \"Great, thank you so much.\" },\n];\n\n// The call's clock, built once: every word gets a start and its syllables\n// become loudness bumps, so the print and the transcript move together.\nconst SYLLABLE = 0.17; // s per syllable\nconst GAP = 0.06; // s between words\nconst COMMA = 0.28; // s after a comma\nconst STOP = 0.42; // s after a full stop or a question\nconst TURN = 0.75; // s between speakers\nconst LEAD = 0.6; // s of quiet before the first line\nconst REST = 1.8; // s of quiet before the call starts again\nconst LOUD = { agent: 0.62, caller: 0.5 }; // peak loudness of each voice\n\ntype Beat = { start: number; end: number; amp: number };\ntype Word = { text: string; start: number; end: number; beats: Beat[] };\ntype Line = { who: Speaker; start: number; end: number; words: Word[] };\n\nfunction syllables(word: string) {\n  const letters = word.toLowerCase().replace(/[^a-z]/g, \"\");\n  if (!letters) return Math.max(1, word.replace(/\\D/g, \"\").length); // \"3:40\" is three-forty\n  const groups = letters.replace(/e$/, \"\").match(/[aeiouy]+/g);\n  return Math.max(1, groups ? groups.length : 1);\n}\n\nfunction buildCall() {\n  let seed = 11;\n  const random = () => (seed = (seed * 16807) % 2147483647) / 2147483647;\n  let t = LEAD;\n  const lines: Line[] = CALL.map(({ who, text }) => {\n    const start = t;\n    const words = text.split(\" \").map((word) => {\n      const wordStart = t;\n      const beats: Beat[] = [];\n      for (let i = 0; i < syllables(word); i++) {\n        const length = SYLLABLE * (0.8 + 0.4 * random());\n        beats.push({ start: t, end: t + length, amp: LOUD[who] * (0.7 + 0.3 * random()) });\n        t += length;\n      }\n      const end = t;\n      t += GAP;\n      if (word.endsWith(\",\")) t += COMMA;\n      if (/[.?!]$/.test(word)) t += STOP;\n      return { text: word, start: wordStart, end, beats };\n    });\n    const end = t;\n    t += TURN;\n    return { who, start, end, words };\n  });\n  return { lines, duration: t + REST };\n}\n\nconst TIMELINE = buildCall();\n\nfunction levelAt(time: number) {\n  for (const line of TIMELINE.lines) {\n    if (time < line.start || time > line.end) continue;\n    for (const word of line.words) {\n      if (time < word.start || time > word.end) continue;\n      for (const beat of word.beats) {\n        if (time < beat.start || time > beat.end) continue;\n        const p = (time - beat.start) / (beat.end - beat.start);\n        // A rounded bump per syllable with a little flutter, over a floor so a word never drops to silence inside.\n        return Math.max(0.08, beat.amp * Math.pow(Math.sin(Math.PI * p), 0.7) * (0.92 + 0.08 * Math.sin(time * 53)));\n      }\n    }\n  }\n  return 0;\n}\n\n/** Which line the transcript shows, and how many of its words have been said. The next line takes over halfway through the pause before it. */\nfunction cueAt(time: number) {\n  let line = 0;\n  TIMELINE.lines.forEach((l, i) => {\n    if (time >= l.start - TURN / 2) line = i;\n  });\n  const words = TIMELINE.lines[line].words.filter((w) => time >= w.start).length;\n  return { line, words, second: Math.max(0, Math.floor(time - LEAD)) };\n}\n\nconst clock = (second: number) => `${String(Math.floor(second / 60)).padStart(2, \"0\")}:${String(second % 60).padStart(2, \"0\")}`;\n\ntype Mic =\n  | { status: \"off\" }\n  | { status: \"asking\" }\n  | { status: \"on\"; stream: MediaStream }\n  | { status: \"blocked\"; reason: string };\n\nfunction MicIcon() {\n  return (\n    <svg viewBox=\"0 0 16 16\" fill=\"none\" aria-hidden className=\"size-4\">\n      <rect x=\"5.5\" y=\"1.75\" width=\"5\" height=\"8\" rx=\"2.5\" stroke=\"currentColor\" strokeWidth=\"1.5\" />\n      <path d=\"M3 7.5a5 5 0 0 0 10 0M8 12.5v2\" stroke=\"currentColor\" strokeWidth=\"1.5\" strokeLinecap=\"round\" />\n    </svg>\n  );\n}\n\nexport function VoiceprintDemo() {\n  const start = React.useRef(0);\n  const alive = React.useRef(true);\n  const [cue, setCue] = React.useState({ line: 0, words: 0, second: 0 });\n  const [mic, setMic] = React.useState<Mic>({ status: \"off\" });\n\n  const callTime = React.useCallback(() => {\n    if (!start.current) return 0;\n    return ((performance.now() - start.current) / 1000) % TIMELINE.duration;\n  }, []);\n  // Read by the print every frame, so the call never re-renders the page at 60fps.\n  const level = React.useCallback(() => levelAt(callTime()), [callTime]);\n\n  React.useEffect(() => {\n    alive.current = true;\n    start.current = performance.now();\n    const timer = window.setInterval(() => {\n      const next = cueAt(callTime());\n      setCue((c) => (c.line === next.line && c.words === next.words && c.second === next.second ? c : next));\n    }, 50);\n    return () => {\n      alive.current = false;\n      window.clearInterval(timer);\n    };\n  }, [callTime]);\n\n  // Hang up the microphone when it's switched off or the demo goes away.\n  React.useEffect(() => {\n    if (mic.status !== \"on\") return;\n    const stream = mic.stream;\n    return () => stream.getTracks().forEach((track) => track.stop());\n  }, [mic]);\n\n  async function toggleMic() {\n    if (mic.status === \"on\") {\n      setMic({ status: \"off\" });\n      return;\n    }\n    if (mic.status === \"asking\") return;\n    if (!navigator.mediaDevices?.getUserMedia) {\n      setMic({ status: \"blocked\", reason: \"No microphone here, so the demo call keeps playing.\" });\n      return;\n    }\n    setMic({ status: \"asking\" });\n    try {\n      const stream = await navigator.mediaDevices.getUserMedia({\n        audio: { echoCancellation: true, noiseSuppression: true },\n      });\n      if (!alive.current) {\n        stream.getTracks().forEach((track) => track.stop());\n        return;\n      }\n      setMic({ status: \"on\", stream });\n    } catch {\n      if (alive.current) setMic({ status: \"blocked\", reason: \"The microphone is blocked, so the demo call keeps playing.\" });\n    }\n  }\n\n  const talking = mic.status === \"on\";\n  const line = TIMELINE.lines[cue.line];\n  const agent = !talking && line.who === \"agent\";\n\n  return (\n    <Voiceprint\n      level={level}\n      source={talking ? mic.stream : null}\n      tone={agent ? \"accent\" : \"ink\"}\n      reach={0.6}\n      className=\"flex h-[var(--demo-height,36rem)] w-full flex-col items-center justify-between rounded-2xl border border-zinc-950/10 bg-[#fafafa] px-6 py-8 text-center text-zinc-950 sm:py-10 dark:border-white/10 dark:bg-[#07080c] dark:text-zinc-50\"\n    >\n      <header className=\"flex flex-col items-center\">\n        <h2 className=\"max-w-[16ch] text-4xl/[1.05] font-semibold tracking-[-0.03em] text-balance sm:text-[3.25rem]/[1.02]\">\n          The front desk that picks up on the first ring\n        </h2>\n      </header>\n\n      <div className=\"flex w-full max-w-xl flex-col items-center\">\n        <p className=\"sr-only\">\n          A scripted call: Marlow, a voice agent, books a same-day appointment for a patient with a chipped tooth.\n        </p>\n        <div aria-hidden className=\"min-h-[5.5rem] w-full sm:min-h-[4.5rem]\">\n          <p className=\"flex items-center justify-center gap-2 text-[11px] font-medium tracking-[0.08em] text-zinc-500 uppercase\">\n            <span\n              className={cn(\n                \"size-1.5 rounded-full\",\n                agent ? \"bg-[#2747ff] dark:bg-[#6b84ff]\" : \"bg-zinc-950 dark:bg-zinc-50\",\n              )}\n            />\n            {talking ? \"You\" : agent ? \"Marlow\" : \"Patient\"}\n            <span className=\"text-zinc-400 tabular-nums dark:text-zinc-600\">\n              · {talking ? \"Live mic\" : `Northside Dental · ${clock(cue.second)}`}\n            </span>\n          </p>\n          <p className=\"mt-2 text-lg/7 font-medium text-balance\">\n            {talking\n              ? \"Listening. Say a few words and watch them print.\"\n              : line.words.map((word, i) => (\n                  <span\n                    key={`${cue.line}-${i}`}\n                    className={cn(\n                      \"transition-colors duration-150\",\n                      i < cue.words ? \"text-zinc-950 dark:text-zinc-50\" : \"text-zinc-950/20 dark:text-zinc-50/20\",\n                    )}\n                  >\n                    {word.text}{\" \"}\n                  </span>\n                ))}\n          </p>\n        </div>\n\n        <div className=\"mt-5 flex items-center gap-1\">\n          <button\n            type=\"button\"\n            onClick={toggleMic}\n            aria-pressed={talking}\n            className=\"inline-flex h-11 items-center gap-2 rounded-full bg-zinc-950 px-5 text-sm font-medium text-white transition-transform active:scale-[0.97] dark:bg-white dark:text-zinc-950\"\n          >\n            <MicIcon />\n            {talking ? \"Stop\" : mic.status === \"asking\" ? \"Allow the mic…\" : \"Talk to it\"}\n          </button>\n          <a\n            href=\"https://marlow.example/demo-line\"\n            className=\"inline-flex h-11 items-center rounded-full px-4 text-sm font-medium text-zinc-600 transition-colors hover:text-zinc-950 dark:text-zinc-400 dark:hover:text-zinc-50\"\n          >\n            Call the demo line\n          </a>\n        </div>\n        <p aria-live=\"polite\" className=\"mt-2 min-h-4 text-xs text-zinc-500\">\n          {mic.status === \"blocked\" ? mic.reason : \"\"}\n        </p>\n      </div>\n    </Voiceprint>\n  );\n}\n"
    }
  ]
}