audio/ui
Toggle Mode D
Copy to Clipboardpnpm dlx shadcn@latest add @audio-ui/use-device-loop
Copy to Clipboardimport { 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 is on, so the play button belongs to the host, and it is silent outside one.
Copy to Clipboardimport { 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
Makes the engine for the state it starts with: an effect's
create<Name>Engine.Where the processed sound goes, such as the node of
useMasterBus.The sound to loop, its sidechain key, and their tempo.
The state of the device, followed by the engine.
Returns · 2
The engine while the loop plays: give it to
useDeviceSignal for the panel's live sound.Whether it plays, and the tempo of its sound.