{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "feedback-sound",
  "title": "Feedback sounds",
  "description": "UI sounds synthesized with the Web Audio API: no audio files.",
  "files": [
    {
      "path": "lib/feedback-sound.ts",
      "content": "export type FeedbackSound =\n  | \"click\"\n  | \"tick\"\n  | \"toggle-on\"\n  | \"toggle-off\"\n  | \"success\"\n  | \"error\"\n  | \"swoosh\";\n\nlet context: AudioContext | null = null;\nlet noise: AudioBuffer | null = null;\n\nfunction getContext() {\n  if (typeof AudioContext === \"undefined\") return null;\n  context ??= new AudioContext();\n  // Autoplay policy: a context created or resumed outside a user gesture\n  // stays suspended, so the first play() must come from a click/keypress.\n  if (context.state === \"suspended\") void context.resume();\n  return context;\n}\n\nfunction getNoise(ctx: AudioContext) {\n  if (!noise) {\n    noise = ctx.createBuffer(1, ctx.sampleRate, ctx.sampleRate);\n    const data = noise.getChannelData(0);\n    for (let i = 0; i < data.length; i++) data[i] = Math.random() * 2 - 1;\n  }\n  return noise;\n}\n\nfunction envelope(\n  ctx: AudioContext,\n  start: number,\n  peak: number,\n  attack: number,\n  decay: number,\n) {\n  const gain = ctx.createGain();\n  // Ramping from silence avoids the audible pop of a hard start/stop.\n  gain.gain.setValueAtTime(0.0001, start);\n  gain.gain.linearRampToValueAtTime(peak, start + attack);\n  gain.gain.exponentialRampToValueAtTime(0.0001, start + attack + decay);\n  return gain;\n}\n\nfunction noiseBurst(\n  ctx: AudioContext,\n  out: AudioNode,\n  {\n    start,\n    duration,\n    filter,\n    frequency,\n    to,\n    q = 1,\n    peak,\n    attack = 0.001,\n  }: {\n    start: number;\n    duration: number;\n    filter: BiquadFilterType;\n    frequency: number;\n    to?: number;\n    q?: number;\n    peak: number;\n    attack?: number;\n  },\n) {\n  const source = ctx.createBufferSource();\n  source.buffer = getNoise(ctx);\n  const biquad = ctx.createBiquadFilter();\n  biquad.type = filter;\n  biquad.Q.value = q;\n  biquad.frequency.setValueAtTime(frequency, start);\n  if (to) biquad.frequency.exponentialRampToValueAtTime(to, start + duration);\n  const gain = envelope(ctx, start, peak, attack, duration - attack);\n  source.connect(biquad).connect(gain).connect(out);\n  source.start(start, Math.random() * 0.5, duration);\n}\n\nfunction tone(\n  ctx: AudioContext,\n  out: AudioNode,\n  {\n    start,\n    duration,\n    type = \"sine\",\n    frequency,\n    to,\n    peak,\n    attack = 0.004,\n  }: {\n    start: number;\n    duration: number;\n    type?: OscillatorType;\n    frequency: number;\n    to?: number;\n    peak: number;\n    attack?: number;\n  },\n) {\n  const osc = ctx.createOscillator();\n  osc.type = type;\n  osc.frequency.setValueAtTime(frequency, start);\n  if (to) osc.frequency.exponentialRampToValueAtTime(to, start + duration);\n  const gain = envelope(ctx, start, peak, attack, duration - attack);\n  osc.connect(gain).connect(out);\n  osc.start(start);\n  osc.stop(start + duration + 0.01);\n}\n\nexport function playFeedbackSound(sound: FeedbackSound, volume = 0.5) {\n  const ctx = getContext();\n  if (!ctx) return;\n  const out = ctx.createGain();\n  out.gain.value = volume;\n  out.connect(ctx.destination);\n  const t = ctx.currentTime;\n\n  switch (sound) {\n    case \"click\":\n      noiseBurst(ctx, out, {\n        start: t,\n        duration: 0.025,\n        filter: \"bandpass\",\n        frequency: 3200,\n        q: 1.5,\n        peak: 0.9,\n      });\n      break;\n    case \"tick\":\n      noiseBurst(ctx, out, {\n        start: t,\n        duration: 0.012,\n        filter: \"highpass\",\n        frequency: 5000,\n        peak: 0.5,\n      });\n      break;\n    case \"toggle-on\":\n      tone(ctx, out, {\n        start: t,\n        duration: 0.08,\n        frequency: 600,\n        to: 950,\n        peak: 0.35,\n      });\n      break;\n    case \"toggle-off\":\n      tone(ctx, out, {\n        start: t,\n        duration: 0.08,\n        frequency: 950,\n        to: 600,\n        peak: 0.3,\n      });\n      break;\n    case \"success\":\n      tone(ctx, out, {\n        start: t,\n        duration: 0.12,\n        frequency: 1046.5,\n        peak: 0.25,\n      });\n      tone(ctx, out, {\n        start: t + 0.09,\n        duration: 0.22,\n        frequency: 1568,\n        peak: 0.25,\n      });\n      break;\n    case \"error\":\n      tone(ctx, out, {\n        start: t,\n        duration: 0.09,\n        type: \"triangle\",\n        frequency: 220,\n        to: 190,\n        peak: 0.45,\n      });\n      tone(ctx, out, {\n        start: t + 0.12,\n        duration: 0.14,\n        type: \"triangle\",\n        frequency: 200,\n        to: 160,\n        peak: 0.45,\n      });\n      break;\n    case \"swoosh\":\n      noiseBurst(ctx, out, {\n        start: t,\n        duration: 0.28,\n        filter: \"bandpass\",\n        frequency: 400,\n        to: 3500,\n        q: 2,\n        peak: 1.5,\n        attack: 0.09,\n      });\n      break;\n  }\n}\n",
      "type": "registry:lib"
    }
  ],
  "type": "registry:lib"
}