Listening to sound
A shader with an audio (waveform) or audioFFT (spectrum) input reads the sound as an image: one row of values per channel. IsfRenderer draws it, and listen() is what gives the input a sound to hear.
Click Play with sound: a page may not start audio before a click. It starts muted, the speaker in the bar unmutes it, and the inputs panel lets each audio input choose what it hears: the built-in sounds (Dark Beat, Dark Pad, Dark Sweep, Beat, Sweep, Pad…), or your microphone.
A bigger one (pipeline), with five audio inputs: a core that morphs through nine solids, from an extruded flower to a pyramid and a spinning top, lit and casting shadows, signed distance field (SDF) text, with a camera that orbits it. Its audio inputs hear a Dark Beat, a Dark Pad and a Dark Sweep by default; change them in the inputs panel.
The same scene with a drone behind it. A fourth audio input, drone, hears a slow sound (Drone Wide, by default) and lifts a faint haze behind the solid: the lows swell it, the mids make it drift and bring the accent colour in. Choose a drone that has something in the mids and the highs (Wide, Industrial, Glass, Air): one that is all low, like Drone Dark, only holds the haze still. The beat still moves the solid, the drone is the floor under it. The droneAmount slider sets how much of it you see.
Giving an input a sound
import { IsfRenderer, microphone } from '@isf-studio/isf-renderer';
const renderer = new IsfRenderer();
renderer.bind(canvas);
await renderer.load('/shaders/audio-bars.fs');
button.addEventListener('click', async () => {
// A URL, a Blob, an ArrayBuffer or a MediaStream.
const beat = await renderer.listen('spectrum', '/sounds/beat.ogg');
await renderer.listen('wave', '/sounds/beat.ogg', { muted: true });
beat.setMuted(true); // it feeds the shader either way
});
listen() takes the name of the input (renderer.inputs lists them) and what to hear. It must run from a click, a key press or a touch.
| You give | The input hears |
|---|---|
A URL, a Blob or an ArrayBuffer | The sound, in a loop, out loud. |
A MediaStream | A live source, such as microphone(). Never out loud: it would feed back. |
An audioFFT input asks for as many bins as its MAX says; an audio input, as many samples.
What it gives back
const listener = await renderer.listen('spectrum', '/sounds/beat.ogg', { loop: false, muted: true });
listener.pause();
listener.play();
listener.setLoop(true);
listener.setMuted(false);
listener.state; // { playing, loop, muted }
listener.stop(); // lets go of the sound; the input hears nothing again
A sound that does not loop stops at its end: state.playing is false then, and play() starts it again.
The microphone
const stream = await microphone(); // asks the user, without the clean-up made for voices
await renderer.listen('spectrum', stream);
Give each input its own stream.clone() when several listen to it: closing one stops the tracks of the others. pause() silences a microphone without turning its light off; stop() turns it off.
{ muted: true }), or they are heard together.dispose(), stops everything the renderer was listening to.With your own audio
Already have a <audio> or a Web Audio graph? The frames an input reads are made by @isf-studio/web-audio-source, which IsfRenderer builds on; its AudioGraphService turns any Web Audio source into the same frames.