# useBake

> Changes the samples of a sound as edited: renders what a track's clips play, hands the channels to an operation and puts the result back as one step of undo; process(operation) bakes one of the Process menu's operations, named after it.

```bash
npx shadcn@latest add @audio-ui/use-bake
```

```ts
import { renderEdited, useBake } from "@/hooks/use-bake";
```

- Files: `hooks/use-bake.ts`
- Depends on: `arrangement`, `arrangement-engine`, `clip-player`, `sound-operations`; npm: nothing
- Used by: `block-audio-editor`, `use-sound-report`

| Export | Kind | What it is |
| --- | --- | --- |
| `renderEdited` | function | What the track's clips play over the whole arrangement, as plain channels: the cuts, fades and gains made, not the track's devices. |
| `useBake` | function | Changes the samples of the sound as edited. |

## Usage

An edit of the arrangement (a cut, a fade, a gain) leaves the recording as it was. An operation on samples (a reverse, a filter, a resample) needs the sound as it plays. `useBake` renders what the track's clips play over the whole arrangement, gives its channels to your operation, and hands the result to `onBaked`, which puts it on the track as a sound of its own: one step of undo.

```tsx
import { useBake } from "@/hooks/use-bake";

const { bake, process } = useBake({
  doc, // the arrangement's document
  onBaked, // (sound, label) => void: puts the sound on the track as a step of the history
  recordings,
  sampleRate: 48_000, // the rate the edited sound is rendered at
  setStatus, // what the editor says while it works
  stop, // stops what plays
  track: "track-1",
});

// An operation of the Process menu, named after it: "Repeat ×4".
<ProcessMenuItems onProcess={process} />;
<MenuItem onClick={() => process({ times: 4, type: "repeat" })}>Repeat 4 times</MenuItem>;

// Any operation: channels in, channels out.
bake("Resample", ({ channels, sampleRate }) => ({
  channels: resampleChannels(channels, sampleRate, 44_100),
  sampleRate: 44_100,
}));
```

`process(operation)` bakes one of the operations of [`sound-operations`](/docs/lib/sound-operations), which the [Process menu](/docs/components/base/process-menu) lists: Reverse, Repeat, Add silence, Normalize, Pan, Dither, Filter, Stretch, Pitch, Speed and Repair, each over [`@/lib/engine/files`](/docs/engine/files) or [`@/lib/engine/repair`](/docs/engine/repair). A browser that cannot render offline says so through `setStatus`.
