tonal
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 (
"",[],nullor{ empty: true }) instead of an exception. - Written in TypeScript, with no runtime dependencies, and published as
tonaland 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
- @tonaljs/note: Note operations (simplify, transposeBy )
- @tonaljs/midi: Midi number conversions
- @tonaljs/interval: Interval operations (add, simplify, invert)
- @tonaljs/abc-notation: Parse ABC notation notes
Scales and chords
- @tonaljs/scale: Scales
- @tonaljs/scale-type: A dictionary of scales
- @tonaljs/chord: Chords
- @tonaljs/chord-type: A dictionary of chords
- @tonaljs/chord-detect: Detect chords from notes
- @tonaljs/pcset: Pitch class sets. Compare note groups.
Voicings
- @tonaljs/voicing: Voicings of any chord by rules and templates, for one chord or a whole progression
- @tonaljs/voice-leading: Voice-leading distance and the smoothest path between voicings
- @tonaljs/voicing-dictionary: Collections of chord voicings (deprecated: use
Voicing)
Keys, chord progressions, charts
- @tonaljs/chart: Chord charts: bars, sections and meters, the played form, and the chord at a position
- @tonaljs/key: Major and minor keys, their scales and chords
- @tonaljs/mode: A dictionary of Greek modes (ionian, dorian...)
- @tonaljs/progression: Chord progressions
- @tonaljs/roman-numeral: Parse roman numeral symbols
Time, rhythm, patterns
- @tonaljs/time: Musical time as exact fractions of quarter notes: positions, durations, meters and bar:beat
- @tonaljs/pattern: Note patterns: arpeggio orders and melodic sequences as lists of indexes
- @tonaljs/rhythm-pattern: Generate, read and measure rhythm patterns (Euclidean rhythms, evenness, oddity...)
- @tonaljs/time-signature: Parse time signatures
- @tonaljs/duration-value: Note duration values
Utilities
- @tonaljs/core: Core functions (note, interval, transpose and distance)
- @tonaljs/collection: Utility functions to work with collections (range, shuffle, permutations)
- @tonaljs/range: Create note ranges
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:
- Teoria: https://github.com/saebekassebil/teoria
- Impro-Visor: https://www.cs.hmc.edu/~keller/jazz/improvisor/
- MusicKit: https://github.com/benzguo/MusicKit
- Music21: https://www.music21.org/music21docs/
- Sharp11: https://github.com/jsrmath/sharp11
- python-mingus: https://github.com/bspaans/python-mingus
- Open Music Theory: https://viva.pressbooks.pub/openmusictheory/
Projects using tonal
Showcase of projects that are using Tonal:
- Solfej by Shayan Javadi
- EarBeater by Morten Vestergaard
- Sonid (play store, apple store) by martijnmichel
- Songcraft by Gabe G'Sell
- React Guitar by 4lejandrito
- Fretty.app by tfeldmann
- Chordify by ashleymays
- Chordal by kad1kad
- muted.io by thisisseb
- Midi Sandbox by jdlee022
- music, eternal by kousun12
- Chromatone.center by davay42
- Super Oscillator by lukehorvat
- StringScales by Ambewas
- Polychron by i1li
- MusicTrainer by zilongliu
- RiffScore by joekotvas
- tonal_rs rust port by TvrtkoM
Thank you all!
Add your project here by editing this file