{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "prompt-voice-recorder",
  "type": "registry:component",
  "title": "Prompt Voice Recorder",
  "description": "A composer that turns into a live waveform with a timer while you dictate, then collapses into a transcribing state and types the transcript into the box. It can read a real microphone level or simulate speech.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "agent-kit",
    "voice",
    "dictation",
    "waveform",
    "prompt",
    "microphone",
    "transcript"
  ],
  "docs": "https://hyperblur.dev/effects/prompt-voice-recorder",
  "files": [
    {
      "path": "registry/hyperblur/prompt-voice-recorder.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/prompt-voice-recorder.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface PromptVoiceRecorderProps extends Omit<React.HTMLAttributes<HTMLFormElement>, \"onSubmit\"> {\n  transcript?: string;\n  placeholder?: string;\n  microphone?: boolean;\n  maxDuration?: number;\n  accent?: string;\n  autoPlay?: boolean;\n  onRecordingChange?: (recording: boolean) => void;\n  onTranscript?: (text: string) => void;\n  onSubmit?: (text: string) => void;\n}\n\ntype Phase = \"idle\" | \"recording\" | \"processing\";\n\nconst BARS = 44;\n\nfunction noise(n: number) {\n  let x = Math.imul((n | 0) ^ 0x27d4eb2d, 0x165667b1) >>> 0;\n  x = Math.imul(x ^ (x >>> 15), 0x85ebca77) >>> 0;\n  return ((x ^ (x >>> 13)) >>> 0) / 4294967295;\n}\n\nfunction clock(ms: number) {\n  const s = Math.floor(ms / 1000);\n  return `${Math.floor(s / 60)}:${String(s % 60).padStart(2, \"0\")}`;\n}\n\nconst css = `\n.hb-vl8 {\n  --hb-vl8-a: #ff4d5e;\n  position: relative;\n  display: flex;\n  flex-direction: column;\n  gap: 6px;\n  width: 100%;\n  padding: 10px;\n  border-radius: 20px;\n  background: linear-gradient(180deg, #19191c, #121214);\n  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.06), 0 0 0 1px rgba(255, 255, 255, 0.08), 0 24px 48px -28px rgba(0, 0, 0, 0.9);\n  color: #ededed;\n  font-family: inherit;\n  box-sizing: border-box;\n  transition: box-shadow 0.4s ease;\n}\n.hb-vl8 *, .hb-vl8 *::before, .hb-vl8 *::after { box-sizing: border-box; }\n.hb-vl8[data-phase=\"recording\"] {\n  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.06), 0 0 0 1px color-mix(in srgb, var(--hb-vl8-a) 50%, transparent), 0 0 0 5px color-mix(in srgb, var(--hb-vl8-a) 10%, transparent), 0 24px 48px -28px rgba(0, 0, 0, 0.9);\n}\n.hb-vl8-body { position: relative; min-height: 52px; }\n.hb-vl8-ta {\n  display: block;\n  width: 100%;\n  min-height: 52px;\n  max-height: 128px;\n  margin: 0;\n  padding: 8px 6px;\n  border: 0;\n  outline: none;\n  resize: none;\n  background: transparent;\n  color: #ededed;\n  font: inherit;\n  font-size: 14px;\n  line-height: 1.5;\n  transition: opacity 0.3s ease, filter 0.3s ease;\n}\n.hb-vl8-ta::placeholder { color: #6f6f75; }\n.hb-vl8[data-phase=\"recording\"] .hb-vl8-ta, .hb-vl8[data-phase=\"processing\"] .hb-vl8-ta { opacity: 0; filter: blur(4px); pointer-events: none; }\n.hb-vl8-wave {\n  position: absolute;\n  inset: 0;\n  display: flex;\n  align-items: center;\n  gap: 10px;\n  padding: 0 4px;\n  opacity: 0;\n  transform: scale(0.98);\n  pointer-events: none;\n  transition: opacity 0.3s ease, transform 0.4s cubic-bezier(0.22, 1, 0.36, 1);\n}\n.hb-vl8[data-phase=\"recording\"] .hb-vl8-wave, .hb-vl8[data-phase=\"processing\"] .hb-vl8-wave { opacity: 1; transform: none; pointer-events: auto; }\n.hb-vl8-rec {\n  display: flex;\n  flex: none;\n  align-items: center;\n  gap: 7px;\n  font-family: var(--font-mono, ui-monospace, monospace);\n  font-size: 12px;\n  font-variant-numeric: tabular-nums;\n  color: #ededed;\n}\n.hb-vl8-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--hb-vl8-a); box-shadow: 0 0 10px var(--hb-vl8-a); animation: hb-vl8-blink 1.2s ease-in-out infinite; }\n@keyframes hb-vl8-blink { 50% { opacity: 0.35; } }\n.hb-vl8-bars {\n  display: flex;\n  flex: 1 1 auto;\n  align-items: center;\n  justify-content: flex-end;\n  gap: 2px;\n  min-width: 0;\n  height: 40px;\n  overflow: hidden;\n  mask-image: linear-gradient(90deg, transparent, #000000 30%);\n  -webkit-mask-image: linear-gradient(90deg, transparent, #000000 30%);\n}\n.hb-vl8-bar {\n  flex: none;\n  width: 3px;\n  height: 100%;\n  border-radius: 3px;\n  background: linear-gradient(180deg, color-mix(in srgb, var(--hb-vl8-a) 60%, #ffffff), var(--hb-vl8-a) 50%, color-mix(in srgb, var(--hb-vl8-a) 60%, #ffffff));\n  transform: scaleY(0.08);\n  transition: transform 0.09s ease-out;\n}\n.hb-vl8[data-phase=\"processing\"] .hb-vl8-bar { transform: scaleY(0.06) !important; transition: transform 0.45s cubic-bezier(0.22, 1, 0.36, 1); background: #8f8f8f; }\n.hb-vl8-proc {\n  position: absolute;\n  left: 50%;\n  top: 50%;\n  font-size: 12px;\n  transform: translate(-50%, -50%);\n  background: linear-gradient(90deg, #8f8f8f 0%, #ededed 45%, #8f8f8f 70%) 0 0 / 200% 100%;\n  -webkit-background-clip: text;\n  background-clip: text;\n  color: transparent;\n  opacity: 0;\n  transition: opacity 0.3s ease;\n  animation: hb-vl8-sweep 1.4s linear infinite;\n  pointer-events: none;\n}\n@keyframes hb-vl8-sweep { to { background-position: -200% 0; } }\n.hb-vl8[data-phase=\"processing\"] .hb-vl8-proc { opacity: 1; }\n.hb-vl8[data-phase=\"processing\"] .hb-vl8-rec { opacity: 0.4; }\n.hb-vl8-row { display: flex; align-items: center; gap: 6px; }\n.hb-vl8-hint { flex: 1 1 auto; min-width: 0; overflow: hidden; padding-left: 4px; font-size: 11.5px; color: #77777d; white-space: nowrap; text-overflow: ellipsis; }\n.hb-vl8-kbd {\n  padding: 1px 5px;\n  border-radius: 5px;\n  background: #222226;\n  box-shadow: inset 0 -1px 0 rgba(0, 0, 0, 0.5), inset 0 0 0 1px rgba(255, 255, 255, 0.07);\n  font-family: var(--font-mono, ui-monospace, monospace);\n  font-size: 10px;\n  color: #a1a1a1;\n}\n.hb-vl8-btn {\n  position: relative;\n  display: grid;\n  flex: none;\n  place-items: center;\n  width: 34px;\n  height: 34px;\n  margin: 0;\n  padding: 0;\n  border: 0;\n  border-radius: 50%;\n  background: #232327;\n  color: #ededed;\n  cursor: pointer;\n  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.08);\n  transition: background 0.25s ease, transform 0.2s ease, color 0.25s ease, opacity 0.2s ease;\n}\n.hb-vl8-btn:hover { background: #2c2c31; }\n.hb-vl8-btn:active { transform: scale(0.92); }\n.hb-vl8-btn:focus-visible { outline: 2px solid var(--hb-vl8-a); outline-offset: 2px; }\n.hb-vl8-btn:disabled { opacity: 0.35; cursor: not-allowed; }\n.hb-vl8-mic::before {\n  content: \"\";\n  position: absolute;\n  inset: -4px;\n  border-radius: 50%;\n  box-shadow: 0 0 0 1px color-mix(in srgb, var(--hb-vl8-a) 55%, transparent);\n  opacity: 0;\n  transform: scale(0.85);\n  transition: opacity 0.3s ease, transform 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);\n}\n.hb-vl8-mic:hover::before { opacity: 1; transform: none; }\n.hb-vl8-mic:hover { color: var(--hb-vl8-a); }\n.hb-vl8-stop { background: var(--hb-vl8-a); color: #ffffff; box-shadow: 0 6px 18px -6px var(--hb-vl8-a); }\n.hb-vl8-stop:hover { background: color-mix(in srgb, var(--hb-vl8-a) 85%, #ffffff); }\n.hb-vl8-send { background: #ededed; color: #0a0a0a; }\n.hb-vl8-send:hover { background: #ffffff; }\n.hb-vl8-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }\n@media (prefers-reduced-motion: reduce) {\n  .hb-vl8 *, .hb-vl8 *::before, .hb-vl8 *::after { transition: none !important; animation: none !important; }\n}\n`;\n\nexport function PromptVoiceRecorder({\n  transcript = \"Draft a short note to the design team: the Thursday review moves to 3 pm, and everyone should bring two options for the onboarding screen.\",\n  placeholder = \"Type a message or hold a thought...\",\n  microphone = false,\n  maxDuration = 60,\n  accent = \"#ff4d5e\",\n  autoPlay = false,\n  onRecordingChange,\n  onTranscript,\n  onSubmit,\n  className,\n  style,\n  ...rest\n}: PromptVoiceRecorderProps) {\n  const [phase, setPhase] = React.useState<Phase>(\"idle\");\n  const [text, setText] = React.useState(\"\");\n  const [elapsed, setElapsed] = React.useState(0);\n  const barRefs = React.useRef<(HTMLSpanElement | null)[]>([]);\n  const levels = React.useRef<number[]>(Array.from({ length: BARS }, () => 0.08));\n  const raf = React.useRef(0);\n  const timers = React.useRef<number[]>([]);\n  const stream = React.useRef<MediaStream | null>(null);\n  const audio = React.useRef<AudioContext | null>(null);\n  const micBtn = React.useRef<HTMLButtonElement>(null);\n  const taRef = React.useRef<HTMLTextAreaElement>(null);\n  const uid = React.useId();\n  const auto = React.useRef(autoPlay);\n  const api = React.useRef<{ start: () => Promise<void>; finish: (keep: boolean) => void } | null>(null);\n\n  const release = React.useCallback(() => {\n    cancelAnimationFrame(raf.current);\n    stream.current?.getTracks().forEach((track) => track.stop());\n    stream.current = null;\n    audio.current?.close().catch(() => undefined);\n    audio.current = null;\n  }, []);\n\n  React.useEffect(\n    () => () => {\n      release();\n      timers.current.forEach((t) => window.clearTimeout(t));\n    },\n    [release],\n  );\n\n  const paint = () => {\n    levels.current.forEach((v, i) => {\n      const el = barRefs.current[i];\n      if (el) el.style.transform = `scaleY(${v.toFixed(3)})`;\n    });\n  };\n\n  const finish = (keep: boolean) => {\n    release();\n    onRecordingChange?.(false);\n    if (!keep) {\n      setPhase(\"idle\");\n      levels.current = levels.current.map(() => 0.08);\n      paint();\n      micBtn.current?.focus();\n      return;\n    }\n    setPhase(\"processing\");\n    const words = transcript.split(\" \");\n    const typing = window.setTimeout(() => {\n      setPhase(\"idle\");\n      let i = 0;\n      const step = () => {\n        i += 1;\n        setText(words.slice(0, i).join(\" \"));\n        if (i < words.length) timers.current.push(window.setTimeout(step, 38 + (i % 3) * 14));\n        else {\n          onTranscript?.(transcript);\n          if (auto.current) {\n            timers.current.push(\n              window.setTimeout(() => {\n                if (!auto.current) return;\n                setText(\"\");\n                void api.current?.start();\n              }, 2600),\n            );\n          } else taRef.current?.focus();\n        }\n      };\n      step();\n    }, 1100);\n    timers.current.push(typing);\n  };\n\n  const start = async () => {\n    timers.current.forEach((t) => window.clearTimeout(t));\n    timers.current = [];\n    setText(\"\");\n    setElapsed(0);\n    setPhase(\"recording\");\n    onRecordingChange?.(true);\n    let analyser: AnalyserNode | null = null;\n    if (microphone && navigator.mediaDevices?.getUserMedia) {\n      try {\n        const media = await navigator.mediaDevices.getUserMedia({ audio: true });\n        stream.current = media;\n        const ctx = new AudioContext();\n        audio.current = ctx;\n        analyser = ctx.createAnalyser();\n        analyser.fftSize = 512;\n        ctx.createMediaStreamSource(media).connect(analyser);\n      } catch {\n        analyser = null;\n      }\n    }\n    const reduce = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    const buffer = analyser ? new Uint8Array(analyser.fftSize) : null;\n    const begin = performance.now();\n    let lastPush = 0;\n    let lastClock = 0;\n    let tick = 0;\n    const loop = (now: number) => {\n      raf.current = requestAnimationFrame(loop);\n      const t = now - begin;\n      if (t - lastClock > 200) {\n        lastClock = t;\n        setElapsed(t);\n        if (t >= maxDuration * 1000) {\n          finish(true);\n          return;\n        }\n      }\n      if (t - lastPush < 70) return;\n      lastPush = t;\n      tick += 1;\n      let level: number;\n      if (analyser && buffer) {\n        analyser.getByteTimeDomainData(buffer);\n        let sum = 0;\n        for (let i = 0; i < buffer.length; i++) {\n          const v = (buffer[i] - 128) / 128;\n          sum += v * v;\n        }\n        level = Math.min(1, Math.sqrt(sum / buffer.length) * 4.5);\n      } else {\n        const syllable = Math.abs(Math.sin(tick * 0.55)) * Math.abs(Math.sin(tick * 0.13 + 1));\n        const pause = Math.floor(tick / 18) % 4 === 3 ? 0.15 : 1;\n        level = (0.15 + syllable * 0.75 + noise(tick) * 0.25) * pause;\n      }\n      const next = Math.max(0.08, Math.min(1, reduce ? 0.4 : level));\n      levels.current = [...levels.current.slice(1), next];\n      paint();\n    };\n    raf.current = requestAnimationFrame(loop);\n    if (auto.current) {\n      timers.current.push(\n        window.setTimeout(() => {\n          if (auto.current) api.current?.finish(true);\n        }, 4200),\n      );\n    }\n  };\n\n  React.useEffect(() => {\n    api.current = { start, finish };\n  });\n\n  React.useEffect(() => {\n    if (!autoPlay) return;\n    auto.current = true;\n    const t = window.setTimeout(() => {\n      void api.current?.start();\n    }, 700);\n    return () => {\n      window.clearTimeout(t);\n      auto.current = false;\n    };\n  }, [autoPlay]);\n\n  const send = () => {\n    const value = text.trim();\n    if (!value) return;\n    onSubmit?.(value);\n    setText(\"\");\n  };\n\n  return (\n    <form\n      className={[\"hb-vl8\", className].filter(Boolean).join(\" \")}\n      style={{ \"--hb-vl8-a\": accent, ...style } as React.CSSProperties}\n      data-phase={phase}\n      onSubmit={(event) => {\n        event.preventDefault();\n        send();\n      }}\n      onPointerDown={() => {\n        auto.current = false;\n      }}\n      onKeyDown={(event) => {\n        auto.current = false;\n        if (event.key === \"Escape\" && phase === \"recording\") {\n          event.preventDefault();\n          finish(false);\n        }\n      }}\n      {...rest}\n    >\n      <style href=\"hb-prompt-voice-recorder\" precedence=\"default\">{css}</style>\n      <div className=\"hb-vl8-body\">\n        <label htmlFor={`${uid}-ta`} className=\"hb-vl8-hidden\">\n          Message\n        </label>\n        <textarea\n          id={`${uid}-ta`}\n          ref={taRef}\n          className=\"hb-vl8-ta\"\n          rows={2}\n          value={text}\n          placeholder={placeholder}\n          readOnly={phase !== \"idle\"}\n          onChange={(event) => setText(event.target.value)}\n          onKeyDown={(event) => {\n            if (event.key === \"Enter\" && !event.shiftKey && !event.nativeEvent.isComposing) {\n              event.preventDefault();\n              send();\n            }\n          }}\n        />\n        <div className=\"hb-vl8-wave\" aria-hidden={phase === \"idle\"}>\n          <span className=\"hb-vl8-rec\">\n            <span className=\"hb-vl8-dot\" />\n            {clock(elapsed)}\n          </span>\n          <span className=\"hb-vl8-bars\">\n            {Array.from({ length: BARS }, (_, i) => (\n              <span\n                key={i}\n                className=\"hb-vl8-bar\"\n                ref={(node) => {\n                  barRefs.current[i] = node;\n                }}\n              />\n            ))}\n          </span>\n          <span className=\"hb-vl8-proc\">Transcribing</span>\n        </div>\n      </div>\n      <div className=\"hb-vl8-row\">\n        <span className=\"hb-vl8-hint\" role=\"status\" aria-live=\"polite\">\n          {phase === \"recording\" ? (\n            <>\n              Listening, <span className=\"hb-vl8-kbd\">Esc</span> to discard\n            </>\n          ) : phase === \"processing\" ? (\n            \"Turning speech into text\"\n          ) : (\n            <>\n              <span className=\"hb-vl8-kbd\">Enter</span> to send, mic to dictate\n            </>\n          )}\n        </span>\n        {phase === \"recording\" ? (\n          <>\n            <button type=\"button\" className=\"hb-vl8-btn\" aria-label=\"Discard recording\" onClick={() => finish(false)}>\n              <svg width=\"14\" height=\"14\" viewBox=\"0 0 14 14\" fill=\"none\" aria-hidden=\"true\">\n                <path d=\"M3.5 3.5l7 7M10.5 3.5l-7 7\" stroke=\"currentColor\" strokeWidth=\"1.6\" strokeLinecap=\"round\" />\n              </svg>\n            </button>\n            <button type=\"button\" className=\"hb-vl8-btn hb-vl8-stop\" aria-label=\"Stop and transcribe\" onClick={() => finish(true)}>\n              <svg width=\"14\" height=\"14\" viewBox=\"0 0 14 14\" fill=\"none\" aria-hidden=\"true\">\n                <path d=\"M3 7.3l2.6 2.6L11 4.4\" stroke=\"currentColor\" strokeWidth=\"1.9\" strokeLinecap=\"round\" strokeLinejoin=\"round\" />\n              </svg>\n            </button>\n          </>\n        ) : (\n          <>\n            <button\n              ref={micBtn}\n              type=\"button\"\n              className=\"hb-vl8-btn hb-vl8-mic\"\n              aria-label=\"Start dictation\"\n              disabled={phase === \"processing\"}\n              onClick={() => {\n                void start();\n              }}\n            >\n              <svg width=\"15\" height=\"15\" viewBox=\"0 0 16 16\" fill=\"none\" aria-hidden=\"true\">\n                <rect x=\"5.5\" y=\"1.8\" width=\"5\" height=\"8\" rx=\"2.5\" stroke=\"currentColor\" strokeWidth=\"1.4\" />\n                <path d=\"M3 7.8a5 5 0 0010 0M8 12.8v1.8\" stroke=\"currentColor\" strokeWidth=\"1.4\" strokeLinecap=\"round\" />\n              </svg>\n            </button>\n            <button type=\"submit\" className=\"hb-vl8-btn hb-vl8-send\" aria-label=\"Send\" disabled={phase !== \"idle\" || text.trim().length === 0}>\n              <svg width=\"14\" height=\"14\" viewBox=\"0 0 14 14\" fill=\"none\" aria-hidden=\"true\">\n                <path d=\"M7 11.5V2.8M3.2 6.4L7 2.6l3.8 3.8\" stroke=\"currentColor\" strokeWidth=\"1.8\" strokeLinecap=\"round\" strokeLinejoin=\"round\" />\n              </svg>\n            </button>\n          </>\n        )}\n      </div>\n    </form>\n  );\n}\n"
    }
  ]
}