Tutorial · updated November 2026
Tone.js for game audio: sound effects and music in code.
Tone.js is a JavaScript library on top of the Web Audio API with synths, samplers, effects and a musical clock. It's ideal for browser games: procedural sound effects with no files, and music that reacts to the game. These examples are short, copy-ready and explained.
- 8 examples
- SFX with no files
- Adaptive music
- Mobile audio fixes
What is Tone.js?
Tone.js is an open-source JavaScript framework for making interactive music and sound in the browser. It wraps the Web Audio API with synthesizers, samplers, effects and a transport (a musical clock), so you can play notes, schedule loops and trigger sound effects with a few lines of code.
1. Unlock audio on the first tap
Browsers block sound until the player interacts. Start Tone on the first click or tap, or nothing will play (especially on iPhone).
import * as Tone from "tone";
window.addEventListener("pointerdown", async () => {
await Tone.start(); // resumes the AudioContext
console.log("audio ready");
}, { once: true });2. A coin sound with no audio file
const coin = new Tone.Synth({
oscillator: { type: "square" },
envelope: { attack: 0.001, decay: 0.1, sustain: 0, release: 0.05 },
}).toDestination();
export function playCoin() {
const now = Tone.now();
coin.triggerAttackRelease("B5", 0.05, now);
coin.triggerAttackRelease("E6", 0.12, now + 0.06);
}3. An explosion from filtered noise
const boom = new Tone.NoiseSynth({
noise: { type: "brown" },
envelope: { attack: 0.005, decay: 0.6, sustain: 0 },
});
const filter = new Tone.Filter(800, "lowpass").toDestination();
boom.connect(filter);
export function playExplosion() {
filter.frequency.setValueAtTime(1200, Tone.now());
filter.frequency.exponentialRampToValueAtTime(80, Tone.now() + 0.6);
boom.triggerAttackRelease(0.6);
}4. Play recorded sounds without lag
For footsteps, voices or anything recorded, load once into Tone.Players and trigger by name.
const sfx = new Tone.Players({
jump: "/audio/jump.wav",
hit: "/audio/hit.wav",
}).toDestination();
await Tone.loaded();
export const play = (name: "jump" | "hit") => sfx.player(name).start();5. A looping soundtrack on the transport
Tone.Transport.bpm.value = 120;
const bass = new Tone.MonoSynth({ oscillator: { type: "sawtooth" } }).toDestination();
const bassLine = new Tone.Sequence((time, note) => {
bass.triggerAttackRelease(note, "8n", time);
}, ["C2", "C2", "G2", "C2", "A#1", "A#1", "F2", "G2"], "8n").start(0);
const kick = new Tone.MembraneSynth().toDestination();
new Tone.Loop((time) => kick.triggerAttackRelease("C1", "8n", time), "4n").start(0);
Tone.Transport.start();6. Adaptive music: add layers when danger rises
Keep every layer playing in sync and fade volumes. Switching on the next bar keeps it musical.
const calm = new Tone.Volume(0).toDestination();
const danger = new Tone.Volume(-Infinity).toDestination();
// connect your calm instruments to calm, drums/lead to danger ...
export function setDanger(on: boolean) {
Tone.Transport.scheduleOnce((time) => {
danger.volume.rampTo(on ? 0 : -60, 1.5, time);
}, "@1m"); // at the start of the next bar
}7. Vary pitch so repeats don't get annoying
export function playStep() {
const player = sfx.player("jump");
player.playbackRate = 0.92 + Math.random() * 0.16; // ±8%
player.start();
}8. Mute and volume settings
export function setMasterVolume(db: number) { Tone.Destination.volume.rampTo(db, 0.1); }
export function setMuted(muted: boolean) { Tone.Destination.mute = muted; }
document.addEventListener("visibilitychange", () => {
Tone.Destination.mute = document.hidden; // silence when the tab is hidden
});Game audio checklist
- Start audio on the first user gesture
- Load samples before the level starts, not on first use
- Limit how many copies of one sound can play at once
- Randomise pitch and volume slightly on repeated sounds
- Duck music under important sounds and dialogue
- Give players separate music and SFX volume sliders
- Mute when the tab or app goes to the background
Frequently asked questions
Is Tone.js free?
Yes, Tone.js is open source under the MIT licence and free for commercial games.
Why doesn't my Tone.js sound play on iPhone?
Audio must be started by a user gesture: call Tone.start() inside a tap or click handler. Also check the phone's silent switch, which mutes Web Audio on some iOS versions.
Should I use Tone.js or plain Web Audio?
Tone.js saves a lot of code for music and synth effects. For a handful of sample-based sounds, plain Web Audio or your engine's audio system is enough and lighter.
Can Tone.js export audio files?
Yes, Tone.Offline renders audio faster than real time, and you can encode the result as WAV. Our sound effect and music generators do exactly this.
EvonX AI game maker
Got the asset? Now make the game.
Describe your game in one sentence and EvonX builds it: code, art, sound and a link you can share. Free to start, no download, plays on any phone.