{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "system-audio-settings",
  "title": "System Audio Settings",
  "description": "Turn system audio capture on, set its level, watch its meter and explain what the browser will capture.",
  "dependencies": [
    "@phosphor-icons/react"
  ],
  "registryDependencies": [
    "alert",
    "badge",
    "card",
    "field",
    "switch",
    "@audiocn/level-meter",
    "@audiocn/parameter-slider",
    "@audiocn/use-audio-analyser",
    "@audiocn/use-audio-context",
    "@audiocn/use-system-audio",
    "@audiocn/use-gain-node"
  ],
  "files": [
    {
      "path": "components/blocks/system-audio-settings/system-audio-settings.tsx",
      "content": "\"use client\";\n\nimport { DesktopIcon, InfoIcon, WarningIcon } from \"@phosphor-icons/react\";\nimport { useEffect, useEffectEvent, useId, useMemo, useState } from \"react\";\n\nimport { Alert, AlertDescription, AlertTitle } from \"@/components/ui/alert\";\nimport { Badge } from \"@/components/ui/badge\";\nimport {\n  Card,\n  CardContent,\n  CardDescription,\n  CardHeader,\n  CardTitle,\n} from \"@/components/ui/card\";\nimport {\n  Field,\n  FieldContent,\n  FieldDescription,\n  FieldGroup,\n  FieldLabel,\n} from \"@/components/ui/field\";\nimport { LevelMeter } from \"@/components/ui/level-meter\";\nimport {\n  ParameterSlider,\n  ParameterSliderControl,\n  ParameterSliderHeader,\n  ParameterSliderInput,\n  ParameterSliderLabel,\n  ParameterSliderReset,\n} from \"@/components/ui/parameter-slider\";\nimport { Switch } from \"@/components/ui/switch\";\nimport { useAudioAnalyser } from \"@/hooks/use-audio-analyser\";\nimport { useAudioContext } from \"@/hooks/use-audio-context\";\nimport { useGainNode } from \"@/hooks/use-gain-node\";\nimport { useSystemAudio } from \"@/hooks/use-system-audio\";\nimport type { SystemAudioStatus } from \"@/hooks/use-system-audio\";\nimport { dbToGain } from \"@/lib/audio/decibels\";\n\nconst DEFAULT_GAIN_DB = -6;\n\nconst STATE_LABELS: Record<SystemAudioStatus, string> = {\n  active: \"On\",\n  denied: \"Needs permission\",\n  ended: \"Ended\",\n  idle: \"Off\",\n  \"no-audio\": \"No audio shared\",\n  prompting: \"Choose what to share…\",\n  unsupported: \"Unsupported\",\n};\n\nexport interface SystemAudioSettingsProps {\n  enabled?: boolean;\n  onEnabledChange?: (enabled: boolean) => void;\n  gainDb?: number;\n  onGainChange?: (gainDb: number) => void;\n  /** The captured stream after the level control, for your own routing. */\n  onStreamChange?: (stream: MediaStream | null) => void;\n  className?: string;\n}\n\nexport const SystemAudioSettings = ({\n  enabled: enabledProp,\n  onEnabledChange,\n  gainDb: gainDbProp,\n  onGainChange,\n  onStreamChange,\n  className,\n}: SystemAudioSettingsProps) => {\n  const enabledId = useId();\n  const system = useSystemAudio();\n  const { context } = useAudioContext();\n  const [gainState, setGainState] = useState(DEFAULT_GAIN_DB);\n  const gainDb = gainDbProp ?? gainState;\n  const output = useMemo(\n    () => context?.createMediaStreamDestination() ?? null,\n    [context]\n  );\n  // The captured stream, through the level control, into a stream for the\n  // parent. Not routed to the speakers.\n  const gainNode = useGainNode({\n    destination: output,\n    gain: dbToGain(gainDb),\n    input: system.stream,\n  });\n  const analyser = useAudioAnalyser(system.stream ? gainNode : null, {\n    channels: \"stereo\",\n  });\n  const active = system.status === \"active\";\n\n  // A new callback each parent render must not re-emit the same stream.\n  const emitStream = useEffectEvent((stream: MediaStream | null) => {\n    onStreamChange?.(stream);\n  });\n  const processed =\n    context && gainNode && output && system.stream ? output.stream : null;\n\n  useEffect(() => {\n    emitStream(processed);\n  }, [processed]);\n\n  const { start: startCapture, stop: stopCapture } = system;\n  const isCapturing = useEffectEvent(\n    () => system.status === \"active\" || system.status === \"prompting\"\n  );\n\n  useEffect(() => {\n    if (enabledProp === true) {\n      if (!isCapturing()) {\n        startCapture();\n      }\n    } else if (enabledProp === false) {\n      stopCapture();\n    }\n  }, [enabledProp, startCapture, stopCapture]);\n\n  const setEnabled = (next: boolean) => {\n    onEnabledChange?.(next);\n    // Controlled, the effect above acts once the parent passes the new value.\n    // When the prop already says so (capture ended while `enabled` stayed\n    // true), nothing will change, so act here.\n    const propWillChange = enabledProp !== undefined && enabledProp !== next;\n    if (propWillChange) {\n      return;\n    }\n    if (next) {\n      system.start();\n    } else {\n      system.stop();\n    }\n  };\n\n  const setGain = (next: number) => {\n    setGainState(next);\n    onGainChange?.(next);\n  };\n\n  return (\n    <Card className={className}>\n      <CardHeader>\n        <CardTitle>\n          <span className=\"flex items-center gap-2\">\n            <DesktopIcon />\n            System audio\n          </span>\n        </CardTitle>\n        <CardDescription>\n          Add the sound from your computer to your recording or stream.\n        </CardDescription>\n      </CardHeader>\n      <CardContent>\n        <FieldGroup>\n          <Field orientation=\"horizontal\">\n            <FieldContent>\n              <div className=\"flex flex-wrap items-center gap-2\">\n                <FieldLabel htmlFor={enabledId}>\n                  Capture system audio\n                </FieldLabel>\n                <Badge variant={active ? \"default\" : \"secondary\"}>\n                  {STATE_LABELS[system.status]}\n                </Badge>\n              </div>\n              <FieldDescription>\n                Your browser asks what to share. Choose a screen or tab and turn\n                on its audio.\n              </FieldDescription>\n            </FieldContent>\n            <Switch\n              checked={active || system.status === \"prompting\"}\n              disabled={!system.isSupported}\n              id={enabledId}\n              onCheckedChange={setEnabled}\n            />\n          </Field>\n          <ParameterSlider\n            disabled={!active}\n            max={12}\n            min={-24}\n            onValueChange={setGain}\n            origin={0}\n            resetValue={DEFAULT_GAIN_DB}\n            unit=\"dB\"\n            value={gainDb}\n          >\n            <ParameterSliderHeader>\n              <ParameterSliderLabel>Level</ParameterSliderLabel>\n              <ParameterSliderReset />\n              <ParameterSliderInput />\n            </ParameterSliderHeader>\n            <ParameterSliderControl />\n          </ParameterSlider>\n          {active ? (\n            <LevelMeter\n              aria-label=\"System audio level\"\n              channelCount={2}\n              source={analyser.meter}\n            />\n          ) : null}\n          {system.isSupported ? (\n            <Alert>\n              <InfoIcon />\n              <AlertTitle>What gets captured</AlertTitle>\n              <AlertDescription>\n                Everything the shared screen, window or tab plays. Some browsers\n                only offer tab audio.\n              </AlertDescription>\n            </Alert>\n          ) : (\n            <Alert variant=\"destructive\">\n              <WarningIcon />\n              <AlertTitle>Not available in this browser</AlertTitle>\n              <AlertDescription>\n                This browser cannot capture system audio. Try a Chromium-based\n                browser.\n              </AlertDescription>\n            </Alert>\n          )}\n          {system.status === \"denied\" ? (\n            <Alert variant=\"destructive\">\n              <WarningIcon />\n              <AlertTitle>Capture was not started</AlertTitle>\n              <AlertDescription>\n                No screen or tab was shared. Turn capture on again and approve\n                the browser&apos;s sharing request. If access is blocked, allow\n                screen sharing in your browser or system settings.\n              </AlertDescription>\n            </Alert>\n          ) : null}\n          {system.status === \"no-audio\" ? (\n            <Alert variant=\"destructive\">\n              <WarningIcon />\n              <AlertTitle>No audio was shared</AlertTitle>\n              <AlertDescription>\n                Turn capture on again and tick the option to share audio.\n              </AlertDescription>\n            </Alert>\n          ) : null}\n        </FieldGroup>\n      </CardContent>\n    </Card>\n  );\n};\n",
      "type": "registry:block"
    }
  ],
  "categories": [
    "system-audio"
  ],
  "type": "registry:block"
}