{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "sound",
  "type": "registry:lib",
  "title": "sound",
  "description": "Zero-asset Web Audio API procedural sound synthesis engine for tactile micro-interactions.",
  "dependencies": [],
  "files": [
    {
      "path": "src/lib/sound.ts",
      "target": "lib/sound.ts",
      "type": "registry:lib",
      "content": "\"use client\";\n\nexport type SoundEffectName =\n  | \"tap\"\n  | \"type\"\n  | \"keyup\"\n  | \"toggle\"\n  | \"stamp\"\n  | \"downloadStart\"\n  | \"success\"\n  | \"pop\"\n  | \"bell\"\n  | \"bubble\"\n  | \"wood\";\n\nexport interface SoundOptions {\n  /** Pitch multiplier (default 1) */\n  pitch?: number;\n  /** Volume multiplier 0..1 (default 1) */\n  volume?: number;\n  /** Directional toggle state for \"toggle\" sound */\n  on?: boolean;\n}\n\nlet audioCtx: AudioContext | null = null;\nlet noiseBuffer: AudioBuffer | null = null;\nlet globalMuted = false;\n\nexport function setSoundMuted(muted: boolean) {\n  globalMuted = muted;\n}\n\nexport function isSoundMuted(): boolean {\n  return globalMuted;\n}\n\nfunction getContext(): AudioContext | null {\n  if (typeof window === \"undefined\") return null;\n  const Ctor =\n    window.AudioContext ||\n    (window as unknown as { webkitAudioContext?: typeof AudioContext }).webkitAudioContext;\n  if (!Ctor) return null;\n  if (!audioCtx) {\n    try {\n      audioCtx = new Ctor();\n    } catch {\n      return null;\n    }\n  }\n  if (audioCtx.state === \"suspended\") {\n    audioCtx.resume().catch(() => {});\n  }\n  return audioCtx;\n}\n\nfunction getNoiseBuffer(ctx: AudioContext): AudioBuffer {\n  if (noiseBuffer && noiseBuffer.sampleRate === ctx.sampleRate) {\n    return noiseBuffer;\n  }\n  const length = Math.floor(ctx.sampleRate * 0.12);\n  const buffer = ctx.createBuffer(1, length, ctx.sampleRate);\n  const data = buffer.getChannelData(0);\n  for (let i = 0; i < length; i++) {\n    data[i] = Math.random() * 2 - 1;\n  }\n  noiseBuffer = buffer;\n  return buffer;\n}\n\nfunction playNoiseBurst(\n  ctx: AudioContext,\n  now: number,\n  duration: number,\n  filterType: BiquadFilterType,\n  freq: number,\n  q: number,\n  gainPeak: number\n) {\n  const src = ctx.createBufferSource();\n  src.buffer = getNoiseBuffer(ctx);\n\n  const filter = ctx.createBiquadFilter();\n  filter.type = filterType;\n  filter.frequency.setValueAtTime(freq, now);\n  filter.Q.setValueAtTime(q, now);\n\n  const gain = ctx.createGain();\n  gain.gain.setValueAtTime(0.0001, now);\n  gain.gain.exponentialRampToValueAtTime(Math.max(0.0002, gainPeak), now + 0.002);\n  gain.gain.exponentialRampToValueAtTime(0.0001, now + duration);\n\n  src.connect(filter);\n  filter.connect(gain);\n  gain.connect(ctx.destination);\n\n  src.start(now);\n  src.stop(now + duration + 0.01);\n}\n\nfunction playTone(\n  ctx: AudioContext,\n  now: number,\n  type: OscillatorType,\n  startFreq: number,\n  endFreq: number,\n  duration: number,\n  gainPeak: number\n) {\n  const osc = ctx.createOscillator();\n  osc.type = type;\n  osc.frequency.setValueAtTime(startFreq, now);\n  if (endFreq !== startFreq) {\n    osc.frequency.exponentialRampToValueAtTime(Math.max(20, endFreq), now + duration);\n  }\n\n  const gain = ctx.createGain();\n  gain.gain.setValueAtTime(0.0001, now);\n  gain.gain.exponentialRampToValueAtTime(Math.max(0.0002, gainPeak), now + 0.004);\n  gain.gain.exponentialRampToValueAtTime(0.0001, now + duration);\n\n  osc.connect(gain);\n  gain.connect(ctx.destination);\n\n  osc.start(now);\n  osc.stop(now + duration + 0.015);\n}\n\n/**\n * Zero-asset Web Audio API procedural sound synthesis for tactile micro-interactions.\n */\nexport function playSound(name: SoundEffectName, options: SoundOptions = {}) {\n  if (globalMuted) return;\n  const ctx = getContext();\n  if (!ctx) return;\n\n  const pitch = options.pitch ?? 1;\n  const vol = Math.min(1, Math.max(0, options.volume ?? 1));\n  const now = ctx.currentTime;\n\n  switch (name) {\n    case \"tap\": {\n      playNoiseBurst(ctx, now, 0.022, \"bandpass\", 2100 * pitch, 2.2, 0.09 * vol);\n      playTone(ctx, now, \"sine\", 540 * pitch, 240 * pitch, 0.028, 0.06 * vol);\n      break;\n    }\n    case \"type\": {\n      // Tactile mechanical keycap thock + crisp top click\n      playNoiseBurst(ctx, now, 0.026, \"bandpass\", 2600 * pitch, 1.8, 0.13 * vol);\n      playTone(ctx, now, \"triangle\", 220 * pitch, 95 * pitch, 0.055, 0.14 * vol);\n      playTone(ctx, now, \"sine\", 980 * pitch, 420 * pitch, 0.018, 0.045 * vol);\n      break;\n    }\n    case \"keyup\": {\n      playNoiseBurst(ctx, now, 0.016, \"highpass\", 2900 * pitch, 1.4, 0.045 * vol);\n      playTone(ctx, now, \"sine\", 760 * pitch, 480 * pitch, 0.018, 0.025 * vol);\n      break;\n    }\n    case \"toggle\": {\n      const isOn = options.on !== false;\n      const base = isOn ? 620 : 440;\n      const end = isOn ? 880 : 300;\n      playNoiseBurst(ctx, now, 0.018, \"bandpass\", (isOn ? 2800 : 1900) * pitch, 2.5, 0.07 * vol);\n      playTone(ctx, now, \"sine\", base * pitch, end * pitch, 0.042, 0.07 * vol);\n      break;\n    }\n    case \"stamp\": {\n      // Resonant wooden seal thud on thick Xuan paper\n      playTone(ctx, now, \"sine\", 155 * pitch, 58 * pitch, 0.11, 0.22 * vol);\n      playNoiseBurst(ctx, now, 0.045, \"lowpass\", 720 * pitch, 1.1, 0.15 * vol);\n      break;\n    }\n    case \"downloadStart\": {\n      playNoiseBurst(ctx, now, 0.02, \"bandpass\", 2400 * pitch, 2.0, 0.08 * vol);\n      playTone(ctx, now, \"sine\", 480 * pitch, 640 * pitch, 0.048, 0.07 * vol);\n      break;\n    }\n    case \"success\": {\n      // Warm two-note glass chime (C6 -> E6) with soft harmonic shimmer\n      playTone(ctx, now, \"sine\", 1046.5 * pitch, 1046.5 * pitch, 0.19, 0.085 * vol);\n      playTone(ctx, now, \"triangle\", 2093 * pitch, 2093 * pitch, 0.11, 0.022 * vol);\n      playTone(ctx, now + 0.075, \"sine\", 1318.5 * pitch, 1318.5 * pitch, 0.28, 0.095 * vol);\n      playTone(ctx, now + 0.075, \"triangle\", 2637 * pitch, 2637 * pitch, 0.16, 0.024 * vol);\n      break;\n    }\n    case \"pop\": {\n      playTone(ctx, now, \"sine\", 340 * pitch, 780 * pitch, 0.055, 0.085 * vol);\n      break;\n    }\n    case \"bell\": {\n      // Crystalline metallic bell with inharmonic partials\n      playTone(ctx, now, \"sine\", 1174.66 * pitch, 1174.66 * pitch, 0.58, 0.1 * vol);\n      playTone(ctx, now, \"sine\", 2349.32 * pitch, 2349.32 * pitch, 0.36, 0.042 * vol);\n      playTone(ctx, now, \"sine\", 3135.96 * pitch, 3135.96 * pitch, 0.22, 0.022 * vol);\n      playNoiseBurst(ctx, now, 0.012, \"highpass\", 4200 * pitch, 1.5, 0.04 * vol);\n      break;\n    }\n    case \"bubble\": {\n      // Smooth upward resonant pitch-sweep water droplet\n      playTone(ctx, now, \"sine\", 310 * pitch, 920 * pitch, 0.085, 0.11 * vol);\n      playTone(ctx, now + 0.02, \"triangle\", 620 * pitch, 1280 * pitch, 0.05, 0.03 * vol);\n      break;\n    }\n    case \"wood\": {\n      // Hollow resonant wooden block / bamboo clack\n      playTone(ctx, now, \"triangle\", 680 * pitch, 490 * pitch, 0.075, 0.14 * vol);\n      playTone(ctx, now, \"sine\", 1360 * pitch, 1020 * pitch, 0.042, 0.055 * vol);\n      playNoiseBurst(ctx, now, 0.018, \"bandpass\", 1500 * pitch, 2.4, 0.09 * vol);\n      break;\n    }\n  }\n}\n"
    }
  ]
}
