Audio Visualizer

appinapp-audio gives widgets raw system-audio frames for visualizers — without shelling out to binaries the user may not have installed.
import { onAudioFrame } from "appinapp-audio";

Frame shape

cb receives { spectrum, waveform, rms } on every tick:
FieldTypeDescription
spectrumnumber[64]Frequency magnitudes, each ~0–1. Use directly as bar heights.
waveformnumber[512]Time-domain samples, each ~-1–1. Draw as an oscilloscope line.
rmsnumberOverall loudness (~0–1). Use for glow / scale pulsing.

Lifecycle

  • The first onAudioFrame call starts capture (Core Audio process tap — no screen-recording indicator). The OS may ask once for audio access.
  • The callback never fires while permission is denied or nothing is playing — render an idle state, not an error.
  • When the last subscriber unsubscribes, capture stops — zero CPU when no visualizer is mounted.
  • Returns an unsubscribe function — always return it from useEffect.
import { useEffect, useRef } from "appinapp-react";
import { onAudioFrame } from "appinapp-audio";

const AudioBars = () => {
  const canvasRef = useRef(null);
  const frameRef = useRef(null);

  useEffect(() => {
    const unsubscribe = onAudioFrame((frame) => {
      frameRef.current = frame;
    });
    return () => unsubscribe();
  }, []);

  return <canvas ref={canvasRef} width={300} height={120} />;
};

export default AudioBars;

Performance rule

Frames arrive at ~60 Hz. Do not setState on every frame — 60 re-renders per second kills performance. Store the latest frame in a ref and draw from requestAnimationFrame:
useEffect(() => {
  const unsubscribe = onAudioFrame((frame) => {
    frameRef.current = frame; // no setState here
  });

  let raf = 0;
  const draw = () => {
    raf = requestAnimationFrame(draw);
    const canvas = canvasRef.current;
    const frame = frameRef.current;
    if (!canvas || !frame) return;
    const ctx = canvas.getContext("2d");
    const { width, height } = canvas;
    ctx.clearRect(0, 0, width, height);
    const bw = width / frame.spectrum.length;
    frame.spectrum.forEach((v, i) => {
      const h = Math.max(2, v * height);
      ctx.fillStyle = "#4ade80";
      ctx.fillRect(i * bw + 1, height - h, bw - 2, h);
    });
  };
  raf = requestAnimationFrame(draw);

  return () => {
    cancelAnimationFrame(raf);
    unsubscribe();
  };
}, []);
rms is the cheapest way to add life to a static widget — pulse opacity, blur, or scale from it even if you do not draw a full spectrum.

Checklist

  1. Named imports only — import { onAudioFrame } from "appinapp-audio", never a default import or window.*.
  2. Cleanup — always return the unsubscribe from useEffect for onAudioFrame.
  3. No 60 Hz setState — stash audio frames in a ref, draw with requestAnimationFrame.
  4. Handle silence — onAudioFrame stays quiet when denied / idle. Render idle UI, not errors.
  5. No binaries — all audio data comes from appinapp-audio via the sandbox allowlist.