Opal Music's visualizer is a separate window driven by the music you're playing. Open it with the cube button next to Lyrics and Up Next. Every scene is a small JavaScript package you can read, copy and edit, and making your own takes two files.
Install a scene
- In the visualizer, click the folder button. It opens your scenes folder:
- Windows:
%LOCALAPPDATA%\OpalMusic\visualizers - macOS:
~/Library/Application Support/OpalMusic/visualizers - Linux:
~/.local/share/OpalMusic/visualizers
- Windows:
- Create a new folder inside it, for example
my-scene. - Put
manifest.jsonandmain.jsin that folder. - Close and reopen the visualizer. Your scene appears in the scene dropdown.
Saving main.js reloads the scene automatically while it's open. If the script has an error, the visualizer shows it and keeps the last working version running. Press R or the reload button to restart a scene from scratch.
Spectrum, Vortex, Tide and Cloud Flight ship as examples in the same folder. Copy one to start from something that works.
manifest.json
{
"api": 2,
"name": "Pulse",
"description": "A circle that follows the bass.",
"author": "Your name",
"entry": "main.js"
}
All five fields are required and no others are allowed. api can be 1 or 2; use 2 for new scenes. entry is the script file, relative to the folder.
main.js
let radius = 40;
function render(frame, draw) {
const target = 40 + frame.audio.bass * 100 * frame.intensity;
radius = opal.smooth(radius, target, 12, frame.dt);
draw.circle(frame.width / 2, frame.height / 2, radius, opal.hsv(frame.time * 0.02, 0.7, 2));
}
The visualizer calls render(frame, draw) once per display refresh. It must be synchronous. Variables declared outside render keep their values between frames, which is how scenes animate smoothly. The screen is cleared to black before every frame, so draw everything you want to see each time.
Scripts run in a plain JavaScript engine (QuickJS). There is no DOM, canvas, WebGL, Node.js, fetch, timers, async, modules or file access. Use frame.time and frame.dt for animation.
Frame input
| Field | Meaning |
|---|---|
time |
Seconds since the visualizer opened. |
dt |
Seconds since the previous frame, at most 0.1. |
width, height |
Window size in logical pixels. |
intensity |
The user's intensity setting, 0.2 to 2.0 (the + and − keys). Multiply your reactions by it. |
theme.accent, theme.background |
Opal's theme colors as [r, g, b, a]. |
audio.spectrum |
64 frequency bands from 30 Hz to 20 kHz, low to high, each 0 to 1. Already smoothed. |
audio.waveform |
256 recent samples, roughly −1 to 1. |
audio.bass, audio.mid, audio.treble |
Average of spectrum bands 0–21, 22–44 and 45–63, each 0 to 1. |
audio.rms, audio.peak |
[left, right] loudness of the latest audio block. |
audio.playing |
Whether music is playing. |
audio.sampleRate |
Always 44100. |
audio.position |
Seconds since the audio feed started. Not the song position. |
Audio values stay at 0 when nothing is playing. Real music rarely pushes bands to 1; most sit between 0.1 and 0.6.
Drawing
Coordinates are in logical pixels with the origin at the top left. Later shapes draw over earlier ones.
draw.circle(x, y, radius, color);
draw.rect(centerX, centerY, width, height, color, (rotationRadians = 0));
draw.line(x1, y1, x2, y2, width, color);
draw.polyline(
[
[x1, y1],
[x2, y2] /* ... */,
],
width,
color,
);
draw.triangle(x1, y1, x2, y2, x3, y3, color);
draw.quad(x1, y1, x2, y2, x3, y3, x4, y4, color);
draw.polygon([[x1, y1] /* ... */], color); // convex shapes only
Rectangles are positioned by their center. Colors are linear [r, g, b, alpha] arrays: alpha is 0 to 1 and RGB is 0 to 8. Values up to 1 show exactly as written. Values above 1 glow with bloom, so use them for highlights.
Helpers
| Helper | Does |
|---|---|
opal.clamp(value, low = 0, high = 1) |
Limits a value to a range. |
opal.lerp(a, b, t) |
Blends from a to b. |
opal.smooth(previous, target, speed, dt) |
Eases toward a target independent of frame rate. Higher speed reacts faster; 4 is gentle, 20 is snappy. |
opal.hsv(hue, saturation = 1, value = 1, alpha = 1) |
Color from hue (wraps over 0–1), saturation and brightness. |
opal.rgb("#rrggbb", alpha = 1) |
Color from a hex code as design tools show it. |
Limits
- 262,144 triangle corners per frame, including instances. Excess geometry is dropped with a warning. A triangle costs three, a quad or rect six, and a circle 72.
- 50 ms per frame. A script that runs longer for several frames in a row is stopped. Aim for under 5 ms.
- 32 MiB of memory, 256 KiB of stack, and a 256 KiB source file.
- Coordinates must be finite and within ±32768. Sizes can't be negative.
Keyboard
| Key | Action |
|---|---|
| 1–9 | Pick a scene |
| Tab | Next scene |
| R | Reload |
| F11 | Fullscreen |
| Esc | Leave fullscreen, then close |
| H | Hide or show controls |
| + / − | Intensity |
Tips
- Smooth audio input with
opal.smoothbefore using it. Raw values flicker. Use a fast speed while the value rises and a slow one while it falls for a punchy look. - Multiply audio reactions by
frame.intensityso the user can tune them. - Spend your vertex budget where it shows: fewer, larger shapes beat thousands of tiny ones.
- To fake depth or masking, draw back to front. Things drawn later cover things drawn earlier.
- Precompute anything that doesn't change outside
render.
API 2 meshes and gradients
Use "api": 2 in new manifests. API 1 packages and all existing shapes continue working on the same renderer. A mesh submits a whole triangle batch, with an independent linear RGBA color at each vertex. The GPU interpolates colors and alpha, so gradients and feathered edges do not need stacks of shapes.
const positions = new Float32Array([0, 0, 1280, 0, 1280, 800, 0, 800]);
const colors = new Float32Array([
0.02, 0.05, 0.18, 1, 0.02, 0.05, 0.18, 1, 0.6, 0.3, 0.15, 1, 0.6, 0.3, 0.15, 1,
]);
const indices = new Uint16Array([0, 1, 2, 0, 2, 3]);
function render(frame, draw) {
positions[2] = positions[4] = frame.width;
positions[5] = positions[7] = frame.height;
draw.mesh(positions, colors, indices);
}
draw.mesh(positions, colors, indices?, options?) takes Float32Array xy pairs, four color values per vertex, and optional Uint16Array or Uint32Array triangle indices. Without indices, each three consecutive vertices form a triangle. Concave geometry must be triangulated by the author. Arrays can be reused and edited between frames; the host copies the submitted values. Invalid lengths, indices, coordinates and colors produce a script error.
Retained meshes and instances
const puff = opal.mesh(positions, colors, indices); // immutable native copy
const data = new Float32Array(5000 * 8);
// Fill each tuple: x, y, scale, rotationRadians, r, g, b, alpha.
function render(frame, draw) {
draw.instances(puff, data);
}
opal.mesh accepts the same arguments as draw.mesh, and returns an opaque numeric handle. Create it at load time or in init. Editing its original arrays does not change the retained mesh. draw.instances submits one reusable Float32Array of eight-value tuples. Scale must be nonnegative. Instance colors multiply vertex colors.
Retained geometry stays uploaded until reload. The GPU reads one copy of it and transforms each instance; JavaScript does not expand or project vertices. The renderer uses vertex pulling with a cached buffer of triangle-corner IDs, rather than a custom hardware-instanced draw command. Changing the instance count rebuilds that ID buffer; keeping counts stable avoids that upload. There is one draw batch per submission, with a safety limit of 4096 batches per frame.
Blend modes
draw.blend("normal"); // default, alpha over
draw.blend("add"); // light and glow
draw.blend("multiply");
draw.blend("screen");
A blend change affects subsequent commands. Blend, camera and fog settings reset each frame. Drawing still preserves command order. add and screen work well for overlapping particles; they do not depend on bloom.
GPU 3D, camera and fog
const mesh = opal.mesh3d(xyzPositions, colors, indices);
function render(frame, draw) {
draw.camera({ position: [0, 0, 8], yaw: 0, pitch: 0, roll: 0, fov: 60, near: 0.1 });
draw.fog([0.2, 0.3, 0.5, 1], 10, 100);
draw.instances3d(mesh, instanceData);
// Or draw.mesh3d(xyzPositions, colors, indices, options).
}
3D positions are Float32Array xyz triples. The right-handed camera looks along −Z, with Y up. Yaw rotates about Y, pitch about X, and roll about Z; angles are radians. fov is the vertical field of view in degrees (greater than 1 and less than 179). The projection has a positive near plane and an infinite far plane. Camera rotation, projection, clipping and linear distance fog run on the GPU.
draw.instances3d takes eleven-value tuples: x, y, z, scale, yaw, pitch, roll, r, g, b, alpha. Use opal.mesh3d handles for it; 2D handles are for draw.instances.
Normal 3D drawing tests and writes depth. Transparent pixels are discarded, but partially transparent geometry still needs back-to-front ordering for correct alpha blending. Add, multiply and screen test depth without writing it. These modes can layer translucent particles without one puff hiding everything behind it. 2D commands ignore depth and can form backgrounds or overlays. Fog requires 0 <= near < far, and uses RGB from its four-component color.
Generated textures
const pixels = new Uint8Array(64 * 64 * 4);
// Fill tightly packed RGBA bytes, 0..255.
const texture = opal.texture(64, 64, pixels);
const mesh = opal.mesh(positions, colors, indices, {
texture,
uvs: new Float32Array([0, 0, 1, 0, 1, 1, 0, 1]),
});
Texture creation makes an immutable copy. RGB bytes are sRGB and alpha is linear; filtering is linear with clamped edges. UVs are two floats per vertex, with [0,0] at the texture's top left. The sampled texture multiplies vertex and instance colors. The same options work with transient and 3D meshes. No file loading or network access is added.
Textures are limited to 1–2048 pixels per dimension and 16 MiB total per script. Scripts can retain up to 256 mesh handles, 256 texture handles, and 1,048,576 triangle corners of geometry until reload.
Lifecycle and richer audio
Optional synchronous hooks:
function init(info) {
/* once, before the first render; info is the full frame */
}
function resize(info) {
/* before render when logical width or height changes */
}
function trackChange(audio) {
/* first audio frame, then on track generation changes */
}
Audio adds these fields:
loudness: maximum channel RMS, in linear amplitude.loudnessShort,loudnessLong: native exponential averages with 3-second and 20-second time constants.spectralFlux: mean positive change in the unsmoothed FFT band magnitudes.onset: a heuristic transient detector with an adaptive threshold and 180 ms refractory period. This is not a BPM estimate.onsetCount: cumulative detected transients since the last track change. Compare this counter between renders; one audio block can span multiple display frames.track: an opaque track generation, not an Apple Music ID. Live Opal updates it when the current song changes. Legacy recordings and the generated test signal use 0.
opal.noise2(x,y) and opal.noise3(x,y,z) compute deterministic native gradient noise in −1..1. This implementation is gradient noise, not simplex. Coordinates must be finite and within ±32768. const random = opal.random(seed) returns an independent reproducible generator in [0,1).
Author tools and captures
Press D for script time, explicit GC time, JavaScript memory and vertex count. GC is explicitly sampled every 120 renders; automatic GC remains included in script time. Errors include entry filenames and QuickJS stack line numbers. Download the TypeScript declarations for editor completion; runtime scripts are still JavaScript.
The frame budget is 262,144 submitted triangle corners, including every instance. A quad costs six, a triangle three, and a convenience circle 72. Excess triangles or instances are dropped and a warning appears; a valid scene is not disabled for exceeding this budget. For a partially accepted mesh, only complete triangles are kept. Invalid geometry remains an error. The 32 MiB QuickJS heap, 256 KiB stack, 256 KiB source and 50 ms watchdog still apply. Native retained geometry and texture limits are separate from the QuickJS heap.
The companion has a core runner and a deterministic GPU capture path:
opal-visualizer --check --packages ./scenes
opal-visualizer --check --packages ./scenes --audio ./recording.pcm
opal-visualizer --packages ./scenes --preset cloud-flight \
--audio ./recording.pcm --capture ./frame-180.png --frames 180
Capture and recording modes advance at a fixed 60 Hz and wait for each script result. --frames is 1–10000; a capture saves one PNG and exits. Run captures at several frame numbers for a sequence. On Linux without a desktop, use xvfb-run -a with a working Vulkan driver, including Mesa software Vulkan. --check needs no window or GPU when built with --no-default-features.
Recordings are at most 64 MiB: the eight-byte OPALPCM1 header followed by little-endian clock/playing packets and 1024 stereo f32 samples at 44100 Hz. Bit 63 of the clock marks playing. OPALPCM2 adds a little-endian u64 track generation after each packet. Packet clocks must increase; truncated or invalid PCM is rejected. Opal's live private pipe uses version 2, and the companion accepts both formats. Custom shaders and PNG file loading remain outside this API.