Skip to Content
Tonal
Time and Rhythm

Rhythm Patterns

@tonaljs/rhythm-pattern on npm

Generate and manipulate rhythmic patterns

A rhythmic pattern is an array of 1 and 0 indicating beats or rests (respectively). They are dimension-less:

import { RhythmPattern } from "tonal";

RhythmPattern.euclid(8, 3); // => [1, 0, 0, 1, 0, 0, 1, 0]

Generate patterns

RhythmPattern.binary

binary(...numbers: number[]) => number[]

Create a rhythm pattern from a number or concatenation of numbers in binary form: first the numbers are converted to binary, and the result is concatenated:

RhythmPattern.binary(13); // => [1, 1, 0, 1]
RhythmPattern.binary(12, 13); // => [1, 1, 0, 0, 1, 1, 0, 1]

RhythmPattern.hex

hex(hexNumber: string) => number[]

Create a rhythmic pattern using an hexadecimal numbers. Same as before, but using hexadecimal numbers:

RhythmPattern.hex("8f"); // => [1, 0, 0, 0, 1, 1, 1, 1]

RhythmPattern.onsets

onsets(...numbers: number[]) => number[]

Create a rhythm pattern from the onsets. The onsets is the space between beats:

RhythmPattern.onsets(1, 2, 2, 1); // => [1, 0, 1, 0, 0, 1, 0, 0, 1, 0]

RhythmPattern.random

random(length: number, probability = 0.5, rnd = Math.random) => number[]

Create a random rhythm pattern with a specified length

ParamDescription
lengthlength of the pattern
probabilityThreshold where random number is considered a beat (defaults to 0.5)
rndA random function (Math.random by default)
RhythmPattern.random(4); // => for example [1, 0, 0, 1]

RhythmPattern.probability

probability(probabilities: number[], rnd = Math.random) => number[]

Create a rhythm pattern based on the given probability thresholds

ParamDescription
probabilitiesAn array with the probability of each step to be a beat
rndA random function (Math.random by default)
RhythmPattern.probability([0.6, 0, 0.2, 0.5]); // => for example [0, 0, 0, 1]

RhythmPattern.euclid

euclid(steps: number, beats: number) => number[]

Generates an euclidean rhythm pattern

ParamDescription
stepsThe length of the pattern
beatsThe number of beats
RhythmPattern.euclid(8, 3); // => [1, 0, 0, 1, 0, 0, 1, 0]

Manipulate

RhythmPattern.rotate

rotate(pattern: number[], rotations: number) => number[]

Rotate a pattern to the right:

ParamDescription
patternthe pattern to rotate
rotationsthe number of steps to rotate
RhythmPattern.rotate([1, 0, 0, 1], 2); // => [0, 1, 1, 0]

References

On this page