{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "voiceprint-dubbing-demo",
  "type": "registry:block",
  "title": "Voiceprint: Dubbing Preview",
  "description": "Audio Waveform Background example, Dubbing Preview: Each line plays in English in ink, then as an AI dub in orange on the same beats, so the same shape prints twice; the chips pick the language. 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-dubbing-demo",
  "registryDependencies": [
    "https://keyframeui.com/r/voiceprint.json"
  ],
  "files": [
    {
      "path": "components/voiceprint-dubbing-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 Lang = \"es\" | \"fr\" | \"de\" | \"ja\" | \"hi\";\n\nconst LANGS: { id: Lang; label: string; code: string }[] = [\n  { id: \"es\", label: \"Spanish\", code: \"ES\" },\n  { id: \"fr\", label: \"French\", code: \"FR\" },\n  { id: \"de\", label: \"German\", code: \"DE\" },\n  { id: \"ja\", label: \"Japanese\", code: \"JA\" },\n  { id: \"hi\", label: \"Hindi\", code: \"HI\" },\n];\n\nconst SCRIPT: { en: string; dub: Record<Lang, string> }[] = [\n  {\n    en: \"Welcome back to the workshop.\",\n    dub: {\n      es: \"Bienvenidos de nuevo al taller.\",\n      fr: \"Bon retour dans l'atelier.\",\n      de: \"Willkommen zurück in der Werkstatt.\",\n      ja: \"おかえりなさい、工房へようこそ。\",\n      hi: \"वर्कशॉप में फिर से स्वागत है।\",\n    },\n  },\n  {\n    en: \"Today we're turning one walnut board into a desk.\",\n    dub: {\n      es: \"Hoy convertimos una tabla de nogal en un escritorio.\",\n      fr: \"Aujourd'hui, on transforme une planche de noyer en bureau.\",\n      de: \"Heute machen wir aus einem Nussbaumbrett einen Schreibtisch.\",\n      ja: \"今日は一枚のウォルナット材からデスクを作ります。\",\n      hi: \"आज हम अखरोट की एक तख्ती से डेस्क बनाएंगे।\",\n    },\n  },\n  {\n    en: \"Measure twice, and let the saw do the work.\",\n    dub: {\n      es: \"Mide dos veces y deja que la sierra haga el trabajo.\",\n      fr: \"Mesurez deux fois, et laissez la scie travailler.\",\n      de: \"Zweimal messen, und dann die Säge arbeiten lassen.\",\n      ja: \"二度測って、あとはのこぎりに任せましょう。\",\n      hi: \"दो बार नापिए, और काम आरी को करने दीजिए।\",\n    },\n  },\n];\n\n// The reel's clock, built once. Each line is spoken in English, then dubbed\n// on exactly the same syllable beats: the dub keeps the speaker's timing,\n// which the print shows as the same shape coming out a second time in orange.\nconst SYLLABLE = 0.18; // s per syllable\nconst GAP = 0.06; // s between words\nconst COMMA = 0.26; // s after a comma\nconst LEAD = 0.5; // s before the first line\nconst PAUSE = 0.9; // s between the original and its dub\nconst NEXT = 1.6; // s after a dub, before the next line\nconst LOUD = 0.58; // peak loudness of the voice\n\ntype Beat = { start: number; end: number; amp: number };\ntype Take = { start: number; end: number };\ntype Cue = { original: Take; dubbed: Take; beats: Beat[] };\n\nfunction syllables(word: string) {\n  const letters = word.toLowerCase().replace(/[^a-z]/g, \"\");\n  const groups = letters.replace(/e$/, \"\").match(/[aeiouy]+/g);\n  return Math.max(1, groups ? groups.length : 1);\n}\n\nfunction buildReel() {\n  let seed = 5;\n  const random = () => (seed = (seed * 16807) % 2147483647) / 2147483647;\n  let t = LEAD;\n  const cues: Cue[] = SCRIPT.map(({ en }) => {\n    const beats: Beat[] = [];\n    let at = 0;\n    en.split(\" \").forEach((word, i, words) => {\n      for (let s = 0; s < syllables(word); s++) {\n        const length = SYLLABLE * (0.8 + 0.4 * random());\n        beats.push({ start: at, end: at + length, amp: LOUD * (0.7 + 0.3 * random()) });\n        at += length;\n      }\n      if (i < words.length - 1) at += GAP + (word.endsWith(\",\") ? COMMA : 0);\n    });\n    const original = { start: t, end: t + at };\n    const dubbed = { start: original.end + PAUSE, end: original.end + PAUSE + at };\n    t = dubbed.end + NEXT;\n    return { original, dubbed, beats };\n  });\n  return { cues, duration: t };\n}\n\nconst REEL = buildReel();\n\n/** A steady per-language wobble, so each dub has its own voice on the same beats. */\nconst wobble = (lang: Lang, i: number) => {\n  const x = Math.sin((LANGS.findIndex((l) => l.id === lang) + 1) * 12.9898 + i * 78.233) * 43758.5453;\n  return 0.88 + 0.24 * (x - Math.floor(x));\n};\n\nfunction levelAt(time: number, lang: Lang) {\n  for (const cue of REEL.cues) {\n    for (const take of [cue.original, cue.dubbed]) {\n      if (time < take.start || time > take.end) continue;\n      const local = time - take.start;\n      const dub = take === cue.dubbed;\n      for (let i = 0; i < cue.beats.length; i++) {\n        const beat = cue.beats[i];\n        if (local < beat.start || local > beat.end) continue;\n        const p = (local - beat.start) / (beat.end - beat.start);\n        const amp = beat.amp * (dub ? wobble(lang, i) : 1);\n        return 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/** The line on screen, whether the original or the dub has the floor, and how far through its take it is. */\nfunction cueAt(time: number) {\n  let index = 0;\n  REEL.cues.forEach((c, i) => {\n    if (time >= c.original.start - 0.6) index = i;\n  });\n  const cue = REEL.cues[index];\n  const dub = time >= cue.original.end + PAUSE / 2;\n  const take = dub ? cue.dubbed : cue.original;\n  const share = Math.min(1, Math.max(0, (time - take.start) / (take.end - take.start)));\n  return { index, dub, share: Math.round(share * 40) / 40 };\n}\n\n/** Words, or characters for Japanese, so subtitles can light up as they're said. */\nconst tokens = (text: string, lang: Lang | \"en\") => (lang === \"ja\" ? Array.from(text) : text.split(/(?<= )/));\n\nfunction Subtitle({\n  text,\n  lang,\n  shown,\n  state,\n}: {\n  text: string;\n  lang: Lang | \"en\";\n  shown: number;\n  state: \"speaking\" | \"said\" | \"waiting\";\n}) {\n  const parts = tokens(text, lang);\n  const lit = state === \"speaking\" ? Math.ceil(shown * parts.length) : state === \"said\" ? parts.length : 0;\n  return (\n    <p lang={lang} className=\"mt-1 text-lg/7 font-medium text-pretty\">\n      {parts.map((part, i) => (\n        <span\n          key={i}\n          className={cn(\n            \"transition-colors duration-150\",\n            i < lit\n              ? state === \"said\"\n                ? \"text-zinc-950/45 dark:text-zinc-50/45\"\n                : \"text-zinc-950 dark:text-zinc-50\"\n              : \"text-zinc-950/20 dark:text-zinc-50/20\",\n          )}\n        >\n          {part}\n        </span>\n      ))}\n    </p>\n  );\n}\n\nexport function VoiceprintDubbingDemo() {\n  const start = React.useRef(0);\n  const [lang, setLang] = React.useState<Lang>(\"es\");\n  const [cue, setCue] = React.useState({ index: 0, dub: false, share: 0 });\n\n  const reelTime = React.useCallback(() => {\n    if (!start.current) return 0;\n    return ((performance.now() - start.current) / 1000) % REEL.duration;\n  }, []);\n  const level = React.useCallback(() => levelAt(reelTime(), lang), [reelTime, lang]);\n  const seekTo = React.useCallback((time: number) => {\n    start.current = performance.now() - time * 1000;\n  }, []);\n\n  React.useEffect(() => {\n    start.current = performance.now();\n    const timer = window.setInterval(() => {\n      const next = cueAt(reelTime());\n      setCue((c) => (c.index === next.index && c.dub === next.dub && c.share === next.share ? c : next));\n    }, 50);\n    return () => window.clearInterval(timer);\n  }, [reelTime]);\n\n  /** A new language replays the line on screen from its original, so you hear the switch. */\n  function pick(next: Lang) {\n    if (next === lang) return;\n    setLang(next);\n    seekTo(REEL.cues[cue.index].original.start - 0.3);\n  }\n\n  const line = SCRIPT[cue.index];\n  const language = LANGS.find((l) => l.id === lang) ?? LANGS[0];\n\n  return (\n    <Voiceprint\n      level={level}\n      tone={cue.dub ? \"accent\" : \"ink\"}\n      reach={0.6}\n      accentClassName=\"text-[#ff4a17] dark:text-[#ff6a3d]\"\n      className=\"flex h-[var(--demo-height,36rem)] w-full flex-col items-center justify-between gap-6 rounded-2xl border border-zinc-950/10 bg-[#fafafa] px-5 py-8 text-zinc-950 sm:px-8 sm:py-10 dark:border-white/10 dark:bg-[#09090b] dark:text-zinc-50\"\n    >\n      <h2 className=\"max-w-[20ch] text-center text-4xl/[1.05] font-semibold tracking-[-0.03em] text-balance sm:text-[3.25rem]/[1.02]\">\n        Your voice in 32 languages, on the same beat\n      </h2>\n\n      <div className=\"flex w-full max-w-2xl flex-col items-center\">\n        <p className=\"sr-only\">\n          A looping preview: each line of a woodworking video plays in English, then as Mynah&apos;s {language.label} dub\n          in the same voice and rhythm.\n        </p>\n        <div aria-hidden className=\"grid w-full gap-3 text-left sm:grid-cols-2 sm:gap-6\">\n          <div>\n            <p className=\"flex items-center gap-2 text-[11px] font-medium tracking-[0.08em] text-zinc-500 uppercase\">\n              <span className=\"size-1.5 rounded-full bg-zinc-950 dark:bg-zinc-50\" />\n              EN · Theo, original\n            </p>\n            <Subtitle text={line.en} lang=\"en\" shown={cue.share} state={cue.dub ? \"said\" : \"speaking\"} />\n          </div>\n          <div>\n            <p className=\"flex items-center gap-2 text-[11px] font-medium tracking-[0.08em] text-zinc-500 uppercase\">\n              <span className=\"size-1.5 rounded-full bg-[#ff4a17] dark:bg-[#ff6a3d]\" />\n              {language.code} · Theo&apos;s voice, by Mynah\n            </p>\n            <Subtitle text={line.dub[lang]} lang={lang} shown={cue.share} state={cue.dub ? \"speaking\" : \"waiting\"} />\n          </div>\n        </div>\n\n        <div role=\"group\" aria-label=\"Dub language\" className=\"mt-6 flex flex-wrap justify-center gap-1.5\">\n          {LANGS.map((l) => (\n            <button\n              key={l.id}\n              type=\"button\"\n              onClick={() => pick(l.id)}\n              aria-pressed={l.id === lang}\n              className={cn(\n                \"h-9 rounded-full border px-3.5 text-sm font-medium transition-colors\",\n                l.id === lang\n                  ? \"border-[#ff4a17] bg-[#ff4a17] text-[#09090b] dark:border-[#ff6a3d] dark:bg-[#ff6a3d]\"\n                  : \"border-zinc-950/10 bg-[#fafafa] text-zinc-600 hover:text-zinc-950 dark:border-white/10 dark:bg-[#09090b] dark:text-zinc-400 dark:hover:text-zinc-50\",\n              )}\n            >\n              {l.label}\n            </button>\n          ))}\n        </div>\n      </div>\n    </Voiceprint>\n  );\n}\n"
    }
  ]
}