{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "piano",
  "title": "Piano",
  "author": "AlgorizeTech",
  "description": "An interactive Playable Piano component featuring Web Audio API synthesizers, physical keyboard keybindings (A-S-D-F-G-H-J-K-L-; and W-E-T-Y-U-O-P), master volume slider, and toggleable key indicators.",
  "dependencies": [
    "lucide-react"
  ],
  "registryDependencies": [
    "utils"
  ],
  "files": [
    {
      "path": "src/components/custom/piano.tsx",
      "content": "\"use client\";\n\nimport { useState, useEffect, useCallback, useRef } from \"react\";\nimport { cn } from \"@/lib/utils\";\nimport { Volume2, VolumeX, Music, RotateCw } from \"lucide-react\";\n\nexport interface PianoProps {\n  className?: string;\n  defaultVolume?: number; // 0 to 1\n  defaultShowKeys?: boolean;\n  defaultRotate?: boolean;\n}\n\ninterface PianoKeyNote {\n  note: string;\n  keyTrigger: string;\n  freq: number;\n  isBlack?: boolean;\n  whiteIndex?: number; // position relative to white key\n}\n\nconst PIANO_NOTES: PianoKeyNote[] = [\n  // White keys\n  { note: \"C4\", keyTrigger: \"a\", freq: 261.63, isBlack: false, whiteIndex: 0 },\n  { note: \"D4\", keyTrigger: \"s\", freq: 293.66, isBlack: false, whiteIndex: 1 },\n  { note: \"E4\", keyTrigger: \"d\", freq: 329.63, isBlack: false, whiteIndex: 2 },\n  { note: \"F4\", keyTrigger: \"f\", freq: 349.23, isBlack: false, whiteIndex: 3 },\n  { note: \"G4\", keyTrigger: \"g\", freq: 392.0, isBlack: false, whiteIndex: 4 },\n  { note: \"A4\", keyTrigger: \"h\", freq: 440.0, isBlack: false, whiteIndex: 5 },\n  { note: \"B4\", keyTrigger: \"j\", freq: 493.88, isBlack: false, whiteIndex: 6 },\n  { note: \"C5\", keyTrigger: \"k\", freq: 523.25, isBlack: false, whiteIndex: 7 },\n  { note: \"D5\", keyTrigger: \"l\", freq: 587.33, isBlack: false, whiteIndex: 8 },\n  { note: \"E5\", keyTrigger: \";\", freq: 659.25, isBlack: false, whiteIndex: 9 },\n\n  // Black keys\n  { note: \"C#4\", keyTrigger: \"w\", freq: 277.18, isBlack: true, whiteIndex: 0 },\n  { note: \"D#4\", keyTrigger: \"e\", freq: 311.13, isBlack: true, whiteIndex: 1 },\n  { note: \"F#4\", keyTrigger: \"t\", freq: 369.99, isBlack: true, whiteIndex: 3 },\n  { note: \"G#4\", keyTrigger: \"y\", freq: 415.3, isBlack: true, whiteIndex: 4 },\n  { note: \"A#4\", keyTrigger: \"u\", freq: 466.16, isBlack: true, whiteIndex: 5 },\n  { note: \"C#5\", keyTrigger: \"o\", freq: 554.37, isBlack: true, whiteIndex: 7 },\n  { note: \"D#5\", keyTrigger: \"p\", freq: 622.25, isBlack: true, whiteIndex: 8 },\n];\n\nexport function Piano({\n  className,\n  defaultVolume = 0.8,\n  defaultShowKeys = true,\n  defaultRotate = false,\n}: PianoProps) {\n  const [volume, setVolume] = useState(defaultVolume);\n  const [showKeys, setShowKeys] = useState(defaultShowKeys);\n  const [isRotated, setIsRotated] = useState(defaultRotate);\n  const [activeKeys, setActiveKeys] = useState<Set<string>>(new Set());\n\n  const audioCtxRef = useRef<AudioContext | null>(null);\n\n  // Initialize Web Audio API Context\n  const getAudioContext = useCallback(() => {\n    if (!audioCtxRef.current) {\n      const AudioContextClass =\n        window.AudioContext ||\n        (window as typeof window & { webkitAudioContext?: typeof AudioContext })\n          .webkitAudioContext;\n      if (AudioContextClass) {\n        audioCtxRef.current = new AudioContextClass();\n      }\n    }\n    if (audioCtxRef.current && audioCtxRef.current.state === \"suspended\") {\n      audioCtxRef.current.resume();\n    }\n    return audioCtxRef.current;\n  }, []);\n\n  // Synthesize rich acoustic grand piano sound with multi-harmonic overtones & warm lowpass filter\n  const playNote = useCallback(\n    (freq: number, keyTrigger: string) => {\n      const ctx = getAudioContext();\n      if (!ctx || volume <= 0) return;\n\n      const now = ctx.currentTime;\n      const masterVol = Math.max(0.01, Math.min(1, volume));\n\n      // Master Gain Node for note decay envelope\n      const noteGain = ctx.createGain();\n      noteGain.gain.setValueAtTime(0.001, now);\n      noteGain.gain.linearRampToValueAtTime(masterVol * 0.8, now + 0.008); // Instant hammer strike\n      noteGain.gain.exponentialRampToValueAtTime(0.0001, now + 1.8); // Smooth acoustic tail sustain\n\n      // Warm Acoustic Wood Low-Pass Filter\n      const filter = ctx.createBiquadFilter();\n      filter.type = \"lowpass\";\n      filter.frequency.setValueAtTime(3200, now);\n      filter.frequency.exponentialRampToValueAtTime(1200, now + 1.2);\n      filter.Q.value = 1.2;\n\n      // Rich Harmonic Oscillators (Grand Piano Harmonics: 1x, 2x, 3x, 4x)\n      const harmonics = [\n        { mult: 1, gainRatio: 1.0, type: \"sine\" as const },\n        { mult: 2, gainRatio: 0.38, type: \"sine\" as const },\n        { mult: 3, gainRatio: 0.18, type: \"triangle\" as const },\n        { mult: 4, gainRatio: 0.08, type: \"sine\" as const },\n      ];\n\n      harmonics.forEach(({ mult, gainRatio, type }) => {\n        const osc = ctx.createOscillator();\n        const oscGain = ctx.createGain();\n\n        osc.type = type;\n        osc.frequency.setValueAtTime(freq * mult, now);\n        oscGain.gain.setValueAtTime(gainRatio, now);\n\n        osc.connect(oscGain);\n        oscGain.connect(filter);\n\n        osc.start(now);\n        osc.stop(now + 1.8);\n      });\n\n      filter.connect(noteGain);\n      noteGain.connect(ctx.destination);\n\n      // Visual active key state trigger\n      setActiveKeys((prev) => new Set(prev).add(keyTrigger));\n      setTimeout(() => {\n        setActiveKeys((prev) => {\n          const next = new Set(prev);\n          next.delete(keyTrigger);\n          return next;\n        });\n      }, 200);\n    },\n    [getAudioContext, volume]\n  );\n\n  // Keyboard Event Listeners for Physical Typing Play\n  useEffect(() => {\n    const handleKeyDown = (e: KeyboardEvent) => {\n      if (e.repeat || e.target instanceof HTMLInputElement) return;\n      const keyLower = e.key.toLowerCase();\n      const targetNote = PIANO_NOTES.find((n) => n.keyTrigger === keyLower);\n      if (targetNote) {\n        playNote(targetNote.freq, targetNote.keyTrigger);\n      }\n    };\n\n    window.addEventListener(\"keydown\", handleKeyDown);\n    return () => window.removeEventListener(\"keydown\", handleKeyDown);\n  }, [playNote]);\n\n  const whiteKeys = PIANO_NOTES.filter((n) => !n.isBlack);\n  const blackKeys = PIANO_NOTES.filter((n) => n.isBlack);\n\n  return (\n    <article\n      className={cn(\n        \"w-full max-w-5xl p-5 sm:p-8 rounded-3xl bg-card border border-border shadow-2xl flex flex-col gap-6 text-card-foreground select-none transition-all duration-300 origin-center max-sm:rotate-90 max-sm:w-[88vh] max-sm:h-[88vw] max-sm:max-w-none max-sm:my-16\",\n        isRotated && \"sm:rotate-90 sm:w-[88vh] sm:h-[88vw] sm:max-w-none sm:my-16\",\n        className\n      )}\n    >\n      {/* Top Header Controls Bar (Single Horizontal Row) */}\n      <header className=\"flex flex-row items-center justify-between gap-2 sm:gap-4 w-full\">\n        {/* Title */}\n        <div className=\"flex items-center gap-2 sm:gap-2.5 shrink-0\">\n          <div className=\"size-7 sm:size-9 rounded-2xl bg-primary border border-primary flex items-center justify-center shadow-sm\">\n            <Music className=\"size-3.5 sm:size-4 text-primary-foreground\" />\n          </div>\n          <h2 className=\"text-sm sm:text-lg font-bold tracking-widest text-primary uppercase\">\n            PIANO\n          </h2>\n        </div>\n\n        {/* Right Controls: Volume Slider & Show Keys Toggle */}\n        <div className=\"flex items-center gap-3 sm:gap-6 shrink-0\">\n          {/* Volume Control */}\n          <div className=\"flex items-center gap-2\">\n            <button\n              onClick={() => setVolume(volume > 0 ? 0 : 0.8)}\n              className=\"text-muted-foreground hover:text-foreground transition-colors\"\n              title=\"Toggle Mute\"\n            >\n              {volume > 0 ? (\n                <Volume2 className=\"size-4\" />\n              ) : (\n                <VolumeX className=\"size-4 text-red-400\" />\n              )}\n            </button>\n            <span className=\"text-[10px] sm:text-xs font-semibold uppercase text-muted-foreground\">\n              Volume\n            </span>\n            <div className=\"relative w-20 sm:w-28 h-4 flex items-center shrink-0\">\n              <div className=\"w-full h-2.5 rounded-full bg-muted border border-border overflow-hidden relative\">\n                <div\n                  className=\"h-full bg-primary\"\n                  style={{ width: `${volume * 100}%` }}\n                />\n              </div>\n              <input\n                type=\"range\"\n                min={0}\n                max={1}\n                step={0.05}\n                value={volume}\n                onChange={(e) => setVolume(parseFloat(e.target.value))}\n                className=\"absolute inset-0 w-full h-full opacity-0 cursor-pointer z-10\"\n              />\n              <div\n                className=\"absolute size-4 rounded-full bg-background border border-border ring-2 ring-foreground/20 pointer-events-none -translate-x-1/2 shadow-sm\"\n                style={{ left: `${volume * 100}%` }}\n              />\n            </div>\n          </div>\n\n          {/* Show Keys Toggle Switch (Height Matched to Volume Slider) */}\n          <div className=\"flex items-center gap-2\">\n            <span className=\"text-[10px] sm:text-xs font-semibold uppercase text-muted-foreground\">\n              Show Keys\n            </span>\n            <div className=\"h-4 flex items-center\">\n              <button\n                onClick={() => setShowKeys(!showKeys)}\n                className={cn(\n                  \"relative inline-flex h-4 w-9 shrink-0 cursor-pointer rounded-full border border-border p-0.5 transition-colors duration-200 ease-in-out focus:outline-none items-center\",\n                  showKeys ? \"bg-primary\" : \"bg-muted\"\n                )}\n                role=\"switch\"\n                aria-checked={showKeys}\n              >\n                <span\n                  className={cn(\n                    \"pointer-events-none inline-block size-3 rounded-full bg-background shadow-sm ring-0 transition-transform duration-200 ease-in-out\",\n                    showKeys ? \"translate-x-4\" : \"translate-x-0\"\n                  )}\n                />\n              </button>\n            </div>\n          </div>\n\n          {/* Rotate Toggle — turns the keyboard sideways on narrow screens */}\n          <button\n            onClick={() => setIsRotated((prev) => !prev)}\n            className={cn(\n              \"transition-colors\",\n              isRotated ? \"text-primary\" : \"text-muted-foreground hover:text-foreground\",\n            )}\n            title=\"Rotate keyboard\"\n            aria-pressed={isRotated}\n          >\n            <RotateCw className=\"size-4\" />\n          </button>\n        </div>\n      </header>\n\n      {/* Piano Keyboard Chassis (Playable Keys Section) */}\n      <div className=\"w-full flex-1 flex items-center justify-center transition-all duration-300 min-h-[180px]\">\n        <section\n          className={cn(\n            \"relative w-full bg-zinc-950 p-1.5 sm:p-3 rounded-2xl border border-zinc-800 flex justify-between gap-1 shadow-inner overflow-hidden transition-all duration-300 origin-center h-[90%] sm:h-64 lg:h-72 my-0\"\n          )}\n        >\n          {/* Render 10 White Keys */}\n          {whiteKeys.map((item) => {\n            const isActive = activeKeys.has(item.keyTrigger);\n            return (\n              <button\n                key={item.note}\n                onClick={() => playNote(item.freq, item.keyTrigger)}\n                className={cn(\n                  \"flex-1 h-full rounded-b-xl sm:rounded-b-2xl border border-zinc-300 shadow-md flex flex-col justify-end items-center pb-2 sm:pb-3 transition-all duration-75 relative z-0\",\n                  isActive\n                    ? \"bg-primary/25 translate-y-1 shadow-inner border-primary/30\"\n                    : \"bg-gradient-to-b from-zinc-100 via-white to-zinc-100 hover:bg-zinc-100 active:translate-y-1 active:bg-primary/25\"\n                )}\n              >\n                {showKeys && (\n                  <span\n                    className={cn(\n                      \"text-xs font-mono font-semibold uppercase tracking-wider transition-colors\",\n                      isActive ? \"text-primary font-bold\" : \"text-zinc-500\"\n                    )}\n                  >\n                    {item.keyTrigger === \";\" ? \";\" : item.keyTrigger.toUpperCase()}\n                  </span>\n                )}\n              </button>\n            );\n          })}\n\n          {/* Render Overlapping Black Keys */}\n          {blackKeys.map((item) => {\n            const isActive = activeKeys.has(item.keyTrigger);\n            const leftOffsetPercent = (item.whiteIndex! + 1) * 10 - 3.5;\n\n            return (\n              <button\n                key={item.note}\n                onClick={(e) => {\n                  e.stopPropagation();\n                  playNote(item.freq, item.keyTrigger);\n                }}\n                style={{ left: `${leftOffsetPercent}%` }}\n                className={cn(\n                  \"absolute top-2 sm:top-3 w-[7%] h-[60%] rounded-b-lg border-x border-b border-black shadow-xl z-10 flex flex-col justify-end items-center pb-2.5 transition-all duration-75\",\n                  isActive\n                    ? \"bg-primary border-primary/30 text-primary-foreground translate-y-1 shadow-inner\"\n                    : \"bg-gradient-to-b from-zinc-800 via-zinc-900 to-black hover:from-zinc-700 active:translate-y-1 active:bg-primary\"\n                )}\n              >\n                {showKeys && (\n                  <span\n                    className={cn(\n                      \"text-[10px] font-mono font-semibold uppercase tracking-wider transition-colors\",\n                      isActive ? \"text-primary-foreground\" : \"text-zinc-400\"\n                    )}\n                  >\n                    {item.keyTrigger.toUpperCase()}\n                  </span>\n                )}\n              </button>\n            );\n          })}\n        </section>\n      </div>\n    </article>\n  );\n}\n",
      "type": "registry:component",
      "target": "components/custom/piano.tsx"
    }
  ],
  "docs": "Named export: Piano. Supports `defaultVolume` (0 to 1), `defaultShowKeys` (boolean), and `className`. Synthesizes real acoustic piano notes using native Web Audio API.",
  "categories": [
    "media",
    "utilities"
  ],
  "type": "registry:component"
}
