visualizer
The sound of a page player made visible: live bars, a wave, a ring or a waveform.
Kite Frequency Sundown warm-up, a 20 s excerpt
Live preview needs WebGPU or WebGL 2. A player for a short electronic track with live spectrum bars that rise and fall with the music.
This example failed to draw.
Or use the player. With peaks, click or drag along the waveform to jump.
Usage
Put it on an empty decorative element next to an <audio> or <video>. start() registers visualizer, so the attribute alone puts it on an element. The tabs below are the example above, with the values you set.
To change its code, copy the source into your effects folder; the copy replaces the built-in.
npx @dioramajs/diorama add visualizer<div class="vz-player"> <div class="vz-head"> <p class="vz-title"> Kite Frequency <span>Sundown warm-up, a 20 s excerpt</span> </p> <audio src="/media/audio/kite-frequency.mp3" controls preload="metadata" > <track kind="captions" src="/media/audio/kite-frequency.vtt" srclang="en" label="English" /> </audio> </div> <div class="vz-bars" data-dio="visualizer" data-dio-peaks="/media/audio/kite-frequency.json" aria-hidden="true" ></div></div>import { start } from '@dioramajs/diorama'
const app = start()app.attach('.vz-bars', 'visualizer', { style: 'bars', bars: 48, color: '#ffffff', accent: '#ff5a36', gap: 0.3, radius: 2, range: [-90, -20], smooth: 0.75, mirror: false, seek: true, fps: 30,})import { start } from '@dioramajs/diorama'import visualizer from '@dioramajs/audio/visualizer'
const app = start()app.attach('.vz-bars', visualizer({ style: 'bars', bars: 48, color: '#ffffff', accent: '#ff5a36', gap: 0.3, radius: 2, range: [-90, -20], smooth: 0.75, mirror: false, seek: true, fps: 30,}))Page transitions
Fades with the page transition. How a flight flies.
Params
Every param is also an attribute, and both app.attach() and the handle take it by name.
| Param | Type | Default | Description |
|---|---|---|---|
forrebuildsdata-dio-for | string | '' | Selector of the audio or video; empty takes the closest one before it. |
stylerebuildsdata-dio-style | 'bars' | 'wave' | 'ring' | 'peaks' | 'bars' | bars, wave, ring, or peaks for a waveform of the whole track. |
peaksrebuildsdata-dio-peaks | string | '' | URL of the peaks JSON: audiowaveform v2 or an array of 0..1. |
barsdata-dio-bars | number8 to 128 | 48 | Number of bars.At most low 24 by quality tier. |
colordata-dio-color | color | '#ffffff' | Color of the bars, the wave and the unplayed part. |
accentdata-dio-accent | color | '#ff5a36' | Color of the played part and the position mark. |
gapdata-dio-gap | number0 to 0.9 | 0.3 | Gap between bars as a share of a step. |
radiusdata-dio-radius | number0 to 16 px | 2 | Corner radius of the bars. |
rangedata-dio-range | [number, number]-120 to 0 | [-90, -20] | Analyzer range in decibels. |
smoothdata-dio-smooth | number0 to 0.99 | 0.75 | Smoothing of the analyzer over time. |
mirrordata-dio-mirror | boolean | false | Bars grow both ways from the middle. |
seekdata-dio-seek | boolean | true | With peaks, a click, a tap or a drag seeks and hover shows the position; off, the waveform only shows progress, for a list where a click opens the episode. |
fpsdata-dio-fps | number1 to 60 | 30 | Frame cap while playing.At most low 20, medium 30 by quality tier. |
Params marked rebuilds re-run the effect's setup when they change, so set them before the element shows.
Inputs and frames
It reads delta, reducedMotion and size
. Draws at a steady rate while it is on screen and stops when it leaves. None while paused; at most fps while playing, then a 0.3 s fall. How a frame is decided.
Reduced motion and touch
With reduced motion it shows a still frame: motion inputs are zero and time stands still. bars, wave and ring hold a calm still shape; peaks works fully.
Runs on every device. A tap on peaks seeks; dragging needs touch-action: pan-y.
To change what reduced motion does, patch it in start().
import { start } from '@dioramajs/diorama'
start({ effects: { visualizer: { reducedMotion: 'off' } } })Cost and quality
- Download
- 5.4 KB min+gzip the first time a page uses it
- GPU
- One quad drawn by a fragment shader and a 128 x 1 data texture updated per frame.
- Memory
- A few kilobytes of analyzer data and peaks.
- Frames
- Draws while on screen
It runs from the low tier up. Each tier changes this: The quality plugin picks the tier for each device.
| Tier | What changes |
|---|---|
low | At most 20 fps and 24 bars. |
medium | At most 30 fps. |
Recipes
A podcast episode with a waveform
The peaks style draws the whole episode from a small JSON file and shows the played part in accent. A click, a tap or a drag along it seeks, so it doubles as a scrubber, and it needs no access to the sound itself. Make the file once per episode with audiowaveform: audiowaveform -i ep2.mp3 -o ep2.json -z 256 -b 8.
Signal Notes, episode 2
The Eagle has landed
Six seconds of radio that ended thirteen minutes of silence in Houston.
Live preview needs WebGPU or WebGL 2. A podcast episode card with a native player and a waveform that seeks on click.
This example failed to draw.
<article class="vz-episode"> <p class="vz-ep-show">Signal Notes, episode 2</p> <p class="vz-ep-title">The Eagle has landed</p> <p class="vz-ep-about">Six seconds of radio that ended thirteen minutes of silence in Houston.</p> <audio src="/media/audio/eagle-has-landed.mp3" controls preload="metadata" aria-label="Episode 2, The Eagle has landed" > <track kind="captions" src="/media/audio/eagle-has-landed.vtt" srclang="en" label="English" /> </audio> <div class="vz-wave" data-dio="visualizer" data-dio-style="peaks" data-dio-peaks="/media/audio/eagle-has-landed.json" data-dio-color="#3a5a8f" data-dio-accent="#7fd3ff" data-dio-gap="0.4" aria-hidden="true" style="touch-action: pan-y" ></div></article>touch-action: pan-y on the waveform lets a finger drag along it while vertical swipes still scroll the page.
A radio player with a ring
The ring style puts the bars around a circle, which frames a round play button or a cover. The <audio> can drop its native controls when your own button plays and pauses it.
Signal Radio Kite Frequency, sundown warm-up
Live preview needs WebGPU or WebGL 2. A round play button inside a ring of bars that pulse with the music.
This example failed to draw.
<div class="vz-radio"> <audio src="/media/audio/kite-frequency.mp3" preload="metadata" > <track kind="captions" src="/media/audio/kite-frequency.vtt" srclang="en" label="English" /> </audio> <div class="vz-ring" data-dio="visualizer" data-dio-style="ring" data-dio-bars="96" data-dio-color="#7fd3ff" data-dio-gap="0.4" aria-hidden="true" ></div> <button type="button" class="vz-play" aria-label="Play Kite Frequency" >Play</button> <p class="vz-now"> Signal Radio <span>Kite Frequency, sundown warm-up</span> </p></div>Audio from a CDN
The bars, the wave and the ring read the sound through Web Audio, and the browser lets a page read only sound it is allowed to. For a file on another origin, add crossorigin="anonymous" to the element and serve the file with an Access-Control-Allow-Origin header that allows your site.
<audio id="set" src="https://cdn.example.org/sets/sundown.mp3" crossorigin="anonymous" controls preload="metadata"></audio><div class="bars" data-dio="visualizer" data-dio-for="#set" aria-hidden="true"></div>Without both, the effect fails with EFFECT_CORS and leaves the element alone, because Web Audio would play an opaque source as silence. The sound keeps playing. The peaks style draws from the JSON file and works with any source.
A list of episodes
In a list where a click on the card opens the episode page, turn seek off: the waveform still shows how much has been played, and clicks reach the link.
<li class="episode"> <a href="/episodes/2">The Eagle has landed</a> <audio src="/audio/ep2.mp3" preload="none"></audio> <div data-dio="visualizer" data-dio-style="peaks" data-dio-peaks="/peaks/ep2.json" data-dio-seek="false" aria-hidden="true" ></div></li>Notes
The <audio> or <video> stays the player: its controls, keyboard and media keys work as usual, and the visualizer is decoration with aria-hidden. With for left empty it takes the closest <audio> or <video> before it in the same parent, so a player and its bars can sit side by side without an id.
The first play routes the element’s sound through the page’s AudioContext for the rest of the element’s life, as browsers allow one such connection per element.
It draws nothing while paused. While playing it draws at most fps frames a second, then the bars fall for 0.3 s and it stops. Under reduced motion the bars, the wave and the ring hold a calm still shape, and peaks works fully.
style, peaks and for rebuild the effect when they change. Colors, the bar count, the gap and the analyzer range change live.