More ways to play: options, game setup, tree-shaking, your own sound
By name (every sound included, 23.8 kB gzipped):
import { play, volume, muted } from "plinkfx";
play("laser");
play("coin", { volume: 0.5, rate: 1.2 }); // quieter, a bit higher
play("step-wood", { variant: 2 }); // a specific take (0-3)
play("step-wood", { pan: -0.6 }); // to the left
play("coin", { delay: 0.1 }); // 100 ms later
play("combo-chain", { rate: 2 ** (n / 12) }); // n-th combo step, a semitone higher each
volume(0.8); // master volume, 0..1
muted(true); // mute everything, muted() reads it
The player protects your mix. The same sound started again within 30 ms is skipped (cooldown),
so 30 coin pickups in one frame make one coin, not one deafening stack. At most 4 copies of a sound
play at once (voices); one more stops the oldest with a short fade. Both are per call:
play("step-grass", { cooldown: 0.08 }); // footsteps: at most one every 80 ms
play("explosion", { voices: 2 }); // keep the mix clear
play("click", { cooldown: 0, voices: 16 }); // off
Loops: rain-loop, wind-loop, engine-idle and force-field are seamless and loop until you stop
them. play() returns a voice you can stop (with a fade) or bend:
const engine = play("engine-idle");
engine?.rate(1.6); // revs up, gliding
engine?.stop(0.5); // fade out over 0.5 s
play("coin", { loop: true }); // any sound can loop; loop: false plays a loop sound once
play() returns undefined when it skips a sound (on the server, inside the cooldown, before the first
click), hence the ?.. The voice's node is the AudioBufferSourceNode if you need more.
muted() is not saved. To remember it, one line at startup and one where you toggle it:
muted(localStorage.muted == "1");
localStorage.muted = muted(!muted()) ? "1" : "0";
In a game: browsers keep audio locked until the first click or key press. unlockAudio() resumes
it on that first gesture (and on iOS lets sounds play with the silent switch on). Sounds triggered
before that are skipped, not saved up into one loud burst. preload() renders sounds in idle time so
the first play has no delay; stopAll() is for pause screens.
import { play, preload, stopAll, unlockAudio } from "plinkfx";
unlockAudio();
preload(["jump", "coin", "hit", "explosion"]);
// ...
stopAll();
Everything is safe to import on the server (Next.js, Nuxt, SvelteKit): play() and volume() do
nothing there.
Only what you use (player plus one sound: 3 kB gzipped). Importing anything from "plinkfx"
includes every sound, so use the subpaths when size matters:
import { play } from "plinkfx/core";
import { coin, jump } from "plinkfx/sounds";
play(coin);
Your own sound is a plain object. Tweak any sound in the gallery, open the "params" tab and paste:
import { play } from "plinkfx/core";
const blip = {
layers: [
{ wave: "square", duty: 0.25, freq: 988, arp: [0, 5], arpRate: 0.07, h: 0.05, d: 0.3, lp: 7000 },
{ wave: "sine", freq: 1976, arp: [0, 5], arpRate: 0.07, d: 0.22, gain: 0.5 },
],
verb: [0.2, 0.12],
};
play(blip);
All fields are documented in skills/plinkfx/reference.md.
React, a two-line hook is enough: