Starter modules
Audio features
prefab-audio-features@0.1.1
Audio-reactive features from a native AnalyserNode: rms, energy, spectral centroid + raw bins. Mic (with consent) or any AudioNode/MediaStream.
- Version
- 0.1.1
- License
- MIT
- Size
- 2 KB
- Asks the player for
- microphone
- Runs on
- Desktop and mobile browsers. The microphone needs the "microphone" permission in prototir.json; the player is asked first.
Add it
Declare it in prototir.json at the root of your ZIP. Prototir checks the
version at upload and adds it to the page's import map.
prototir.json
{
"modules": [
"prefab-audio-features@0.1.1"
],
"permissions": [
"microphone"
]
}Then import it by name, from 'prefab-audio-features', as you would from npm.
Smallest example
Example
import { createAudioFeatures } from 'prefab-audio-features';
const af = await createAudioFeatures({
onFeatures({ rms, energy, centroid }) {
blob.scale = 1 + rms * 4;
}
});API
createAudioFeatures(options)
source: 'microphone' | MediaStream | AudioNode- What to listen to. Defaults to the microphone. Pass an AudioNode (for example from prefab-audio) to react to your own soundtrack.
context- An AudioContext to use; must match an AudioNode source's context.
fftSize = 1024, smoothing = 0.8- AnalyserNode settings.
onFeatures({ rms, energy, centroid, freq })- Called every animation frame. rms is loudness, energy is overall level 0..1, centroid is brightness 0..1, freq is the raw frequency bins.
The returned object
features- The latest values, updated every frame.
analyser, ctx, stream- The underlying Web Audio objects.
destroy()- Stops the loop. Stops the microphone and closes the context only if it created them; yours stay alive.
Versions
Every version stays available, so a prototype keeps working exactly as published. Declare the one you tested with.
prefab-audio-features@0.1.1prefab-audio-features@0.1.0
As JSON: /modules/prefab-audio-features.json. Source: prototir.