{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "wave-envelope",
  "title": "Wave Envelope",
  "description": "Wave player with interactive volume automation points.",
  "dependencies": [
    "wavesurfer.js",
    "lucide-react"
  ],
  "registryDependencies": [
    "@waves-cn/wave-cn",
    "button",
    "card",
    "slider"
  ],
  "files": [
    {
      "path": "src/registry/components/wave-envelope.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport EnvelopePlugin from \"wavesurfer.js/dist/plugins/envelope.esm.js\";\nimport { cn } from \"@/lib/utils\";\nimport { Card, CardContent } from \"@/components/ui/card\";\nimport { Button } from \"@/components/ui/button\";\nimport { Slider } from \"@/components/ui/slider\";\nimport { Play, Pause, Volume2, VolumeX, RotateCcw } from \"lucide-react\";\nimport WavesurferPlayer, {\n  formatTime,\n  useCssVar,\n  useWavePlayer,\n} from \"@/lib/wave-cn\";\nimport type WaveSurfer from \"wavesurfer.js\";\n\n// ─── Types ───────────────────────────────────────────────────────────────────\n\nexport interface EnvelopePoint {\n  time: number;\n  volume: number;\n}\n\nexport interface WaveEnvelopeProps {\n  /** Audio source URL */\n  src: string;\n  /** Optional title shown above the waveform */\n  title?: string;\n  /** Initial volume (0–1) */\n  defaultVolume?: number;\n  /** Envelope line color @default \"var(--primary)\" */\n  lineColor?: string;\n  /** Envelope line width in px @default 2 */\n  lineWidth?: number;\n  /** Drag point radius in px @default 10 */\n  dragPointSize?: number;\n  /** Drag point fill color @default \"var(--primary)\" */\n  dragPointFill?: string;\n  /** Drag point stroke color @default \"var(--background)\" */\n  dragPointStroke?: string;\n  /** Fade in end time in seconds */\n  fadeInEnd?: number;\n  /** Fade out start time in seconds */\n  fadeOutStart?: number;\n  /** Audio bar color @default \"var(--muted-foreground)\" */\n  waveColor?: string;\n  /** Progress bar color @default \"var(--primary)\" */\n  progressColor?: string;\n  /** Waveform bar width in px @default 3 */\n  barWidth?: number;\n  /** Waveform bar gap in px @default 2 */\n  barGap?: number;\n  /** Rounded borders for bars @default 2 */\n  barRadius?: number;\n  /** Waveform height in px @default 64 */\n  waveHeight?: number;\n  /** Called when envelope volume changes during playback */\n  onVolumeChange?: (volume: number) => void;\n  /** Called when playback starts */\n  onPlay?: () => void;\n  /** Called when playback pauses */\n  onPause?: () => void;\n  /** Called when playback finishes */\n  onFinish?: () => void;\n  /** Called with current time on every audio process tick */\n  onTimeUpdate?: (currentTime: number, duration: number) => void;\n  className?: string;\n}\n\ntype EnvelopePluginInstance = InstanceType<typeof EnvelopePlugin>;\n\n// ─── Component ───────────────────────────────────────────────────────────────\n\nexport function WaveEnvelope({\n  src,\n  title,\n  defaultVolume = 0.8,\n  lineColor = \"var(--primary)\",\n  lineWidth = 2,\n  dragPointSize = 10,\n  dragPointFill = \"var(--primary)\",\n  dragPointStroke = \"var(--background)\",\n  fadeInEnd,\n  fadeOutStart,\n  waveColor,\n  progressColor,\n  barWidth,\n  barGap,\n  barRadius,\n  waveHeight,\n  onVolumeChange,\n  onPlay,\n  onPause,\n  onFinish,\n  onTimeUpdate,\n  className,\n}: WaveEnvelopeProps) {\n  const [envelopePlugin, setEnvelopePlugin] =\n    React.useState<EnvelopePluginInstance | null>(null);\n  const [envelopeVolume, setEnvelopeVolume] = React.useState(defaultVolume);\n\n  // Latest callback readable from the plugin listener without re-subscribing.\n  const latest = React.useRef({ onVolumeChange });\n  React.useEffect(() => {\n    latest.current = { onVolumeChange };\n  });\n\n  // The envelope is drawn as SVG: resolve tokens to concrete colors.\n  const resolvedLineColor = useCssVar(lineColor);\n  const resolvedDragPointFill = useCssVar(dragPointFill);\n  const resolvedDragPointStroke = useCssVar(dragPointStroke);\n\n  // ── Build initial points from fade config ─────────────────────────────────\n  const initialPoints = React.useMemo(() => {\n    const pts: EnvelopePoint[] = [];\n    if (fadeInEnd !== undefined && fadeInEnd > 0) {\n      pts.push({ time: 0, volume: 0 });\n      pts.push({ time: fadeInEnd, volume: defaultVolume });\n    }\n    if (fadeOutStart !== undefined) {\n      pts.push({ time: fadeOutStart, volume: defaultVolume });\n    }\n    return pts;\n  }, [fadeInEnd, fadeOutStart, defaultVolume]);\n\n  // ── Memoized plugins ──────────────────────────────────────────────────────\n  const plugins = React.useMemo(\n    () =>\n      typeof document === \"undefined\"\n        ? []\n        : [\n            EnvelopePlugin.create({\n              volume: defaultVolume,\n              lineColor: resolvedLineColor,\n              lineWidth: `${lineWidth}px`,\n              dragPointSize,\n              dragPointFill: resolvedDragPointFill,\n              dragPointStroke: resolvedDragPointStroke,\n              points: initialPoints.length > 0 ? initialPoints : undefined,\n            }),\n          ],\n    [\n      defaultVolume,\n      resolvedLineColor,\n      lineWidth,\n      dragPointSize,\n      resolvedDragPointFill,\n      resolvedDragPointStroke,\n      initialPoints,\n    ],\n  );\n\n  // ── Player ────────────────────────────────────────────────────────────────\n  const player = useWavePlayer({\n    defaultVolume,\n    onPlay,\n    onPause,\n    onFinish,\n    onTimeUpdate,\n    onReady: (ws) => {\n      const live = ws\n        .getActivePlugins()\n        .find((p) => p instanceof EnvelopePlugin) as\n        | EnvelopePluginInstance\n        | undefined;\n      setEnvelopePlugin(live ?? null);\n    },\n  });\n\n  const handlers = React.useMemo(\n    () => ({\n      ...player.handlers,\n      onDestroy: (ws: WaveSurfer) => {\n        player.handlers.onDestroy?.(ws);\n        setEnvelopePlugin(null);\n      },\n    }),\n    [player.handlers],\n  );\n\n  // ── Envelope volume events ────────────────────────────────────────────────\n  React.useEffect(() => {\n    if (!envelopePlugin) return;\n    const unsub = envelopePlugin.on(\"volume-change\", (vol: number) => {\n      setEnvelopeVolume(vol);\n      latest.current.onVolumeChange?.(vol);\n    });\n    return () => unsub();\n  }, [envelopePlugin]);\n\n  // ── Controls ──────────────────────────────────────────────────────────────\n  const { seek } = player;\n  const handleSeek = React.useCallback(([v]: number[]) => seek(v), [seek]);\n\n  // ── Derived ───────────────────────────────────────────────────────────────\n  const { isReady, isPlaying, isMuted, currentTime, duration, progress } =\n    player;\n\n  // ── Render ────────────────────────────────────────────────────────────────\n  return (\n    <Card\n      className={cn(\n        \"w-full px-0 border-0 rounded-none bg-transparent\",\n        className,\n      )}\n    >\n      <CardContent className=\"border-0 px-0 space-y-3\">\n        {title && (\n          <p className=\"text-sm font-medium text-foreground truncate\">\n            {title}\n          </p>\n        )}\n\n        {/* Waveform with envelope overlay */}\n        <div className=\"relative w-full rounded-sm overflow-hidden bg-muted/40\">\n          <WavesurferPlayer\n            url={src}\n            waveColor={waveColor}\n            progressColor={progressColor}\n            height={waveHeight}\n            barWidth={barWidth}\n            barGap={barGap}\n            barRadius={barRadius}\n            dragToSeek\n            plugins={plugins}\n            {...handlers}\n          />\n        </div>\n\n        {/* Volume indicator */}\n        <p className=\"text-xs text-muted-foreground\" aria-live=\"polite\">\n          Envelope volume:{\" \"}\n          <span className=\"tabular-nums font-medium text-foreground\">\n            {Math.round(envelopeVolume * 100)}%\n          </span>\n          <span className=\"ml-2 opacity-60\">— drag points to adjust</span>\n        </p>\n\n        {/* Seek bar */}\n        <div className=\"flex items-center gap-2\">\n          <span className=\"text-[11px] tabular-nums text-muted-foreground w-10 text-right shrink-0\">\n            {formatTime(currentTime)}\n          </span>\n          <Slider\n            className=\"flex-1\"\n            value={[progress]}\n            min={0}\n            max={1}\n            step={0.001}\n            disabled={!isReady}\n            onValueChange={handleSeek}\n            aria-label=\"Seek\"\n          />\n          <span className=\"text-[11px] tabular-nums text-muted-foreground w-10 shrink-0\">\n            {formatTime(duration)}\n          </span>\n        </div>\n\n        {/* Controls */}\n        <div className=\"flex items-center justify-between gap-3\">\n          <div className=\"flex items-center gap-1.5\">\n            <Button\n              size=\"icon\"\n              variant=\"ghost\"\n              className=\"h-8 w-8 text-muted-foreground hover:text-foreground\"\n              disabled={!isReady}\n              onClick={player.restart}\n              aria-label=\"Restart\"\n            >\n              <RotateCcw size={15} />\n            </Button>\n            <Button\n              size=\"icon\"\n              variant=\"secondary\"\n              className=\"h-9 w-9\"\n              disabled={!isReady}\n              onClick={player.togglePlay}\n              aria-label={isPlaying ? \"Pause\" : \"Play\"}\n            >\n              {isPlaying ? <Pause size={17} /> : <Play size={17} />}\n            </Button>\n          </div>\n\n          {/* Volume / Mute */}\n          <div className=\"flex items-center gap-2\">\n            <Button\n              size=\"icon\"\n              variant=\"ghost\"\n              className=\"h-7 w-7 shrink-0 text-muted-foreground hover:text-foreground\"\n              onClick={player.toggleMute}\n              aria-label={isMuted ? \"Unmute\" : \"Mute\"}\n            >\n              {isMuted ? <VolumeX size={15} /> : <Volume2 size={15} />}\n            </Button>\n          </div>\n        </div>\n      </CardContent>\n    </Card>\n  );\n}\n\nexport default WaveEnvelope;\n",
      "type": "registry:component",
      "target": "components/waves-cn/wave-envelope.tsx"
    }
  ],
  "type": "registry:component"
}