Pentatonic

Turn any array of integers into a fun little melody using the Web Audio API.
It has over 4,550 downloads on npm.

A simple pixel-art graphic of some piano keys where the keys of the C Major Pentatonic scale are highlighted

A snippet of a C Major Pentatonic scale.

Hey, psst!… You can jump straight down to the examples!

pentatonic on GitHub pentatonic Releases pentatonic License

What? Permalink

Pentatonic is a small JavaScript file that will play music generated from an array of integers from anywhere in your DOM.

Why? Permalink

Why not? No, but really though, there’s no serious reason for this. It’s just fun.

Installation Permalink

Available on npm:

npm install @chrisburnell/pentatonic --save-dev

You can also download it directly from GitHub:

https://github.com/chrisburnell/pentatonic/archive/master.zip

Usage Permalink

pentatonic.js gives you a function, pentatonic() to use, like so:

document.querySelectorAll(".pentatonic").forEach(target => {
	target.addEventListener("click", () => {
		pentatonic(target.dataset.values.split(","));
	});
});

The function takes six parameters:

Values Description
notes
(required)
an array of positive integers defines the melody in the scale defined by keyIntervals, beginning from keyStart
duration 4000 (default) the length of time to play the audio for, represented in milliseconds
volume 0.5 (default) controls the *gain* of the audio, represented by a 0–1 range
keyStart 29 (default: C♯3 / D♭3) the zero-index of the key on a standard keyboard from which the scale should start
keyIntervals [2, 2, 3, 2, 3] (default) an array of integers which represent half-steps in a loop which composes the desired scale
keyLimit 12 (default) represents the highest index in the desired scale by which notes are bound by

Examples Permalink

You can also send an anonymous reply (using Quill and Comment Parade).