npm.io
6.16.0 • Published 1h ago

tonal

Licence
MIT
Version
6.16.0
Deps
26
Size
838 kB
Vulns
0
Weekly
0
Stars
4.3K

tonal

npm version

tonal is a music theory library for building composition tools: notes, intervals, chords, scales, keys, voicings, progressions, chord charts, note patterns and rhythm patterns.

  • Pure functions, data in and out: no mutation, no classes. Invalid input gives an empty value ("", [], null or { empty: true }) instead of an exception.
  • Written in TypeScript, with no runtime dependencies, and published as tonal and as a collection of smaller @tonaljs/* packages.
  • It describes music, it doesn't play it: notes, chords and where things are in musical time (when they play, with tempo and seconds, comes step by step), never sound or scheduling. Use an audio library for that.

Example

import {
  Chart,
  Chord,
  Interval,
  Note,
  Pattern,
  RhythmPattern,
  Scale,
  Time,
  Voicing,
} from "tonal";

Note.midi("C4"); // => 60
Note.freq("a4"); // => 440
Note.accidentals("c#2"); // => '#'
Note.transpose("C4", "5P"); // => "G4"
Interval.semitones("5P"); // => 7
Interval.distance("C4", "G4"); // => "5P"

// Scales
Scale.get("C major").notes; // => ["C", "D", "E", "F", "G", "A", "B"];
[1, 3, 5, 7].map(Scale.degrees("C major")); // => ["C", "E", "G", "B"]

Chord.get("Cmaj7").name; // => "C major seventh"
Chord.detect(["D", "F", "A", "C"]); // => ["Dm7", "F6/D"]
Scale.chords("D dorian", { size: 4 }).map((c) => c.symbol);
// => ["Dm7", "Em7", "Fmaj7", "G7", "Am7", "Bm7b5", "Cmaj7"]

// Chord inversions
const triad = Chord.degrees("Cm");
[1, 2, 3].map(triad); // => ["C", "Eb", "G"];
[2, 3, 1].map(triad); // => ["Eb", "G", "C"];
[3, 1, 2].map(triad); // => ["G", "C", "Eb"];

// Voicings with smooth voice leading
Voicing.sequence(["Dm7", "G7", "Cmaj7"]);
// => [["D3", "F3", "A3", "C4"], ["D3", "F3", "G3", "B3"], ["C3", "E3", "G3", "B3"]]

// Note patterns: arpeggios and melodic sequences as indexes
Pattern.arpeggio("up-down", 4).map(Chord.steps(["C4", "maj7"]));
// => ["C4", "E4", "G4", "B4", "G4", "E4"]

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

// Chord charts: sections, repeats and endings, the chord at a position
const chart = Chart.parse(
  "[A] | Dm7 G7 | Cmaj7 | % |\n[B] |: Fmaj7 |1 E7 :|2 E7 Am7 |",
);
const chords = Chart.chords(chart);
chords.map((c) => c.value);
// => ["Dm7", "G7", "Cmaj7", "Cmaj7", "Fmaj7", "E7", "Fmaj7", "E7", "Am7"]
Chart.chordAt(chords, Time.fromBarBeat("1:3", "4/4"))?.value; // => "G7"

// Time: exact positions and durations, in quarter notes
Time.add(Time.duration("q."), [1, 3]); // => [11, 6]

Install

Install all packages at once:

npm install --save tonal

You can read CHANGELOG here.

Usage

Tonal works with ES modules (import), CommonJS (require) and in the browser.

ES modules (import)
import { Note, Scale } from "tonal";
CommonJS (require)
const { Note, Scale } = require("tonal");
Browser

You can use the browser version from jsdelivr CDN directly in your html:

<script src="https://cdn.jsdelivr.net/npm/tonal/browser/tonal.min.js"></script>
<script>
  console.log(Tonal.Key.minorKey("Ab"));
</script>

Or if you prefer, grab the minified browser ready version from the repository.

Bundle size

tonal includes all published modules.

The browser build is about 27 KB gzipped. You can reduce bundle sizes even more by installing the modules individually, and importing only the functions you need.

Note that individual modules are prefixed with @tonaljs/. For example:

npm i @tonaljs/note
import { transpose } from "@tonaljs/note";
transpose("A4", "P5");

Documentation

Visit the documentation site or the README.md of each module

To see what tonal can do, try the apps: a chord finder, scale chords, a key explorer, voicings and more, each showing the tonal calls behind it.

Using a coding agent? Point it to llms.txt: it links to a guide for agents and to every function with its signature, which is also in the npm package as node_modules/tonal/llms-api.txt.

Notes and intervals
Scales and chords
Voicings
Keys, chord progressions, charts
Time, rhythm, patterns
Utilities

Contributing

Read contributing document. To contribute open a PR and ensure:

  • If it's a music theory change (like the name of a scale), link to reliable references.
  • If it's a new feature, add documentation: changes to the README of the affected module(s) are expected.
  • Add tests: changes to the test.ts file of the affected module(s) are expected.
  • If it changes a published package, add a changeset (npx changeset).
  • If it changes a public function or its JSDoc, regenerate the API list for agents (npm run docs:api).
  • All tests are green (npm run test:all)

Inspiration

This library takes inspiration from other music theory libraries:

Projects using tonal

Showcase of projects that are using Tonal:

Thank you all!

Add your project here by editing this file

License

MIT License

Keywords