LabDAW

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).

pnpm dlx shadcn@latest add @audio-ui/use-device-loop
import { useDeviceLoop } from "@/hooks/use-device-loop";
Fileshooks/use-device-loop.ts
Depends onuse-device-engines, use-play-switch, web-audio
npmNothing
Used byblock-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
ExportKindWhat it is
DeviceLoopSoundinterfaceA sound to loop through a device: the buffer that is heard, the one that feeds its sidechain, and their tempo.
UseDeviceLoopOptionsinterface
useDeviceLoopfunctionA 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 is on, so the play button belongs to the host, and it is silent outside one.

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), 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.

Options · 4

Returns · 2

Built by Ouest Labs, the source code is available on GitHub.
RSS