# Redux

> A redux device: a number of bits, a downsample factor and a mix, a panel that draws the stairs the bits make, a Web Audio engine and tools for agents.

```tsx
import { useState } from "react";
import {
  type ReduxState,
  reduxDevice,
} from "@/components/audio-ui/devices/redux/device";
import { createReduxEngine } from "@/components/audio-ui/devices/redux/engine";
import { ReduxPanel } from "@/components/audio-ui/devices/redux/panel";
import { useDeviceLoop } from "@/hooks/use-device-loop";
import { useDeviceSignal } from "@/hooks/use-device-signal";
import { useMasterBus } from "@/hooks/use-master-bus";
import { useDemoSound } from "@/hooks/use-demo-source";

const MASTER_VOLUME = 0.7;

/**
 * A redux you can plug in: its state, a panel that draws the stairs the bits
 * make and an engine that plays it, each its own file and none knowing the
 * others. Play the loop and take the bits down: the stairs grow as the sound
 * roughens.
 */
export function BlockRedux() {
  const [state, setState] = useState<ReduxState>(() => reduxDevice.defaults());
  const master = useMasterBus(MASTER_VOLUME);
  const sound = useDemoSound("groove");
  const { engine } = useDeviceLoop({
    create: createReduxEngine,
    destination: master,
    sound,
    state,
  });
  const signal = useDeviceSignal(engine, reduxDevice);
  return (
    <div className="rounded-xl h-full w-full min-w-0">
      <ReduxPanel onValueChange={setState} signal={signal} value={state} />
    </div>
  );
}
```

Roughens the sound like an early sampler. It keeps only some bits of each sample and holds each sample for several. It is a [device](/docs/devices): a state, a panel, an engine and tools, each its own file.

## Installation

**CLI**

```bash
npx shadcn@latest add @audio-ui/redux
```

It installs the device's state, its engine and its panel to `components/audio-ui/devices/redux/`. The demo above, which wires them to a loop, is `@audio-ui/block-redux`.

**Manual**

**1. Install the packages:**

```bash
npm install @ui-sdk/core@0.1.0-beta.14 @ui-sdk/react@0.1.0-beta.14
```

**2. Add the pieces you want. All of them, or only the state and the panel if your engine is elsewhere:**

```bash
npx shadcn@latest add @audio-ui/redux-device @audio-ui/redux-panel @audio-ui/redux-engine
```

**3. Copy and paste the demo that wires them, if you want it.**

```tsx title="components/examples/block-redux.tsx"
import { useState } from "react";
import {
  type ReduxState,
  reduxDevice,
} from "@/components/audio-ui/devices/redux/device";
import { createReduxEngine } from "@/components/audio-ui/devices/redux/engine";
import { ReduxPanel } from "@/components/audio-ui/devices/redux/panel";
import { useDeviceLoop } from "@/hooks/use-device-loop";
import { useDeviceSignal } from "@/hooks/use-device-signal";
import { useMasterBus } from "@/hooks/use-master-bus";
import { useDemoSound } from "@/hooks/use-demo-source";

const MASTER_VOLUME = 0.7;

/**
 * A redux you can plug in: its state, a panel that draws the stairs the bits
 * make and an engine that plays it, each its own file and none knowing the
 * others. Play the loop and take the bits down: the stairs grow as the sound
 * roughens.
 */
export function BlockRedux() {
  const [state, setState] = useState<ReduxState>(() => reduxDevice.defaults());
  const master = useMasterBus(MASTER_VOLUME);
  const sound = useDemoSound("groove");
  const { engine } = useDeviceLoop({
    create: createReduxEngine,
    destination: master,
    sound,
    state,
  });
  const signal = useDeviceSignal(engine, reduxDevice);
  return (
    <div className="rounded-xl h-full w-full min-w-0">
      <ReduxPanel onValueChange={setState} signal={signal} value={state} />
    </div>
  );
}
```

**Agent**

Paste this prompt to your agent. It does the rest: it installs the skill, adds the component and wires it to your audio.

```text
Install the audio/ui skill with `npx skills add ouestlabs/audio-ui`, then follow it to add the redux from audio/ui to my project and wire it to my audio code. Read the docs at https://www.audio-ui.dev/llms.txt first.
```

To let the agent read the docs live, connect the [MCP server](/docs/mcp). More in [Agent Skill](/docs/agents).

## Controls

| Control | Range | Default | What it does |
| --- | --- | --- | --- |
| On | On or off | On | Turns the device on. Off, the signal passes through unchanged. |
| Bits | 1 to 16 | 8 | How many bits each sample keeps. At 8 the stairs are too fine to see: take it down to 3 or 4. |
| Quantize | Nearest, Shaped | Nearest | How each sample is rounded to its bits. Nearest, the default, takes the closest level. Shaped carries each sample's error to the next, so the rounding noise moves to the highs, where it is heard less: fewer bits sound cleaner. |
| Downsample | 1x to 64x | 4x | How many samples each one is held for. |
| Mix | 0 % to 100 % | 100 % | The balance of the roughened signal and the clean one. |

## Panel

The panel draws the stairs the bits make against the straight line of a clean sound. `quantize(input, bits)` is one step of the quantizer.

```tsx
import { ReduxPanel } from "@/components/audio-ui/devices/redux/panel";
import { reduxDevice } from "@/components/audio-ui/devices/redux/device";

const [state, setState] = useState(() => reduxDevice.defaults());

<ReduxPanel onValueChange={setState} value={state} />;
```

## Engine

Create it, connect it and update it as for any device: see [The engine](/docs/devices#the-engine).

Rounded to the nearest level, the bits are a quantizer table in a `WaveShaperNode`, rebuilt only when they change. The sample hold is an `AudioWorklet` (`REDUX_WORKLET` is its source): the bitcrusher of `@audio/effect-bitcrusher` at 24 bits, and for a shaped quantizer the error feedback of `@audio/effect-noiseshaper`, the table then out of the way. A worklet cannot import, so the device carries their code as published (`REDUX_KERNELS`, in its `kernels.ts`). Until it is ready the bits are already rounded to the nearest level, shaped or not, and the rate is not yet reduced, and `engine.ready` resolves to `false` in a context that cannot run worklets. The hold takes whole samples, not a filtered resampling, so it aliases, which is the sound.
