Loops and patterns are the backbone of electronic music. Tone.js
provides powerful tools like Loop
and Pattern
for scheduling repeated sounds and sequences.
The Loop
object calls a callback at a fixed interval, great for steady beats.
The Pattern
lets you define sequences to play notes in various orders: "up", "down", "random", etc.
// Create a synth for sounds
const synth = new Tone.Synth().toDestination();
// Loop every quarter note
const loop = new Tone.Loop(time => {
synth.triggerAttackRelease("C4", "8n", time);
}, "4n");
// Pattern playing a sequence of notes in order
const patternNotes = ["C4", "E4", "G4", "B4"];
const pattern = new Tone.Pattern((time, note) => {
synth.triggerAttackRelease(note, "8n", time);
}, patternNotes, "up");