# useDeviceLoop

> A sound of the app's looped through an effect's engine and out to a destination: it plays while the session's play switch is on, the engine is made for it and follows the state (use-device-engines), and the engine is returned for the panel's live sound (use-device-signal).

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

```ts
import { useDeviceLoop } from "@/hooks/use-device-loop";
```

- Files: `hooks/use-device-loop.ts`
- Depends on: `use-device-engines`, `use-play-switch`, `web-audio`; npm: nothing
- Used by: `block-auto-pan`, `block-bass-enhancer`, `block-binaural-panner`, `block-channel-eq`, `block-chorus`, `block-compander`, `block-compressor`, `block-deesser`, `block-delay`, `block-distortion`, `block-ducker`, `block-equalizer`, `block-exciter`, `block-expander`, `block-filter`, `block-frequency-shifter`, `block-gate`, `block-graphic-eq`, `block-limiter`, `block-lofi`, `block-mid-side`, `block-multiband`, `block-overdrive`, `block-phaser`, `block-redux`, `block-reverb`, `block-rotary`, `block-saturator`, `block-stutter`, `block-tilt-eq`, `block-transient`, `block-unlimiter`, `block-utility`, `block-vibe`, `block-wah`, `use-demo-source`, `use-pulses-through-compressor`

| Export | Kind | What it is |
| --- | --- | --- |
| `DeviceLoopSound` | interface | A sound to loop through a device: the buffer that is heard, the one that feeds its sidechain, and their tempo. |
| `UseDeviceLoopOptions` | interface |  |
| `useDeviceLoop` | function | A sound looped through an effect's engine and out to a destination. |

## Usage

A sound looped through an effect's engine and out to a destination: what a device does, heard. The sound is the app's: an `AudioBuffer` it decoded, with its tempo. It plays while the play switch of its [session](/docs/hooks/use-audio-session) is on, so the play button belongs to the host, and it is silent outside one.

```tsx
import { compressorDevice } from "@/components/audio-ui/devices/compressor/device";
import { createCompressorEngine } from "@/components/audio-ui/devices/compressor/engine";
import { CompressorPanel } from "@/components/audio-ui/devices/compressor/panel";
import { useDeviceLoop } from "@/hooks/use-device-loop";
import { useDeviceSignal } from "@/hooks/use-device-signal";
import { useMasterBus } from "@/hooks/use-master-bus";

const output = useMasterBus(0.7);
// `buffer` is a loop the app decoded, such as with `decodeAudioData`.
const sound = buffer ? { buffer, tempo: { beatsPerBar: 4, beatUnit: 4, bpm: 90 } } : null;
const { engine } = useDeviceLoop({ create: createCompressorEngine, destination: output, sound, state });
const signal = useDeviceSignal(engine, compressorDevice); // what the panel draws of its sound

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

The engine is made when the loop starts and disposed when it stops ([`useDeviceEngines`](/docs/hooks/use-device-engines)), following `state` and the tempo of the sound (120 BPM in 4/4 until one is given), for a time parameter synced to it. `sound.key` feeds the engine's `sidechain` on the same beat, as a ducker needs: it is listened to, never heard. Nothing plays while `sound` is null. The blocks of this site pass the demo sounds through [`useDemoSound`](/docs/hooks/use-demo-source).

**Options**

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `create` | `EngineFactory<State, Engine>` |  | Makes the engine for the state it starts with: an effect's `create<Name>Engine`. |
| `destination` | `AudioNode \| null` |  | Where the processed sound goes, such as the node of [`useMasterBus`](/docs/hooks/use-master-bus). |
| `sound` | `{ buffer: AudioBuffer; key?: AudioBuffer \| null; tempo: Tempo } \| null` |  | The sound to loop, its sidechain key, and their tempo. |
| `state` | `State` |  | The state of the device, followed by the engine. |

**Returns**

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `engine` | `Engine \| null` |  | The engine while the loop plays: give it to [`useDeviceSignal`](/docs/hooks/use-device-signal) for the panel's live sound. |
| `playing, tempo` | `boolean, Tempo` |  | Whether it plays, and the tempo of its sound. |
