Chord JS is a music theory package that identifies notes, chords, and key signatures on an 88-key piano.
To install run
npm install @patrady/chord-js
or if you're using yarn
yarn add @patrady/chord-js
To translate a series of notes into a chord, use
import { Chord } from '@patrady/chord-js';
const chord = new Chord.for('C E G');
chord?.getName(); // C
This table shows the type of supported chords with examples
Chord | Example |
---|---|
Major | Chord.for("C E G"); // C |
Minor | Chord.for("C Eb G"); // Cm |
Suspended | Chord.for("C F G"); // Csus |
Suspended Second | Chord.for("C D G"); // Csus2 |
Augmented | Chord.for("C E G#"); // Caug |
Diminished | Chord.for("C Eb Gb"); // Cdim |
Inverted | Chord.for("E G C"); // C/E |
Sixth | Chord.for("C E G A"); // C6 |
Minor Sixth | Chord.for('C Eb G A'); // Cm6 |
Dominant Seventh | Chord.for("C E G Bb"); // C7 |
Diminished Seventh | Chord.for("C Eb Gb A"); // Cdim7 |
Augmented Seventh | Chord.for("C E G# Bb"); // C+7 |
Major Seventh | Chord.for("C E G B"); // Cmaj7 |
Augmented Major Seventh | Chord.for("C E G# B"); // Cmaj+7 |
Minor Seventh | Chord.for("C Eb G Bb"); // Cm7 |
Minor Major Seventh | Chord.for("C Eb G B"); // Cm7+ |
Half-Diminished Seventh | Chord.for("C Eb Gb Bb"); // Cø7 |
A Key Signature is a combination of sharps and flats at the beginning of each stave.
import { Note, KeySignatureOfD } from '@patrady/chord-js';
new KeySignatureOfD().getNotes(); // D, E, F#, G, A, B, C#, D
new KeySignatureOfD().normalize(new Note('Gb')); // F#
new KeySignatureOfD().isInKey(new Note('Gb')); // false
new KeySignatureOfD().isInKey(new Note('F#')); // true
Attribute | Description | Example |
---|---|---|
getNotes() |
Returns an array of eight notes from the base to the octave. | new KeySignatureOfD().getNotes(); // D, E, F#, G, A, B, C#, D |
normalize(note) |
Normalizes a note from one key signature to the current key signature. | new KeySignatureOfD().normalize(new Note("Gb")); // F# |
isInKey(note) |
Returns whether a note is in the key signature | new KeySignatureOfD().isInKey(new Note("Gb")); // false |
The major key signatures are supported but less popular ones are not. Check this table to see if the one you need is supported:
Key Signature | Supported | Name |
---|---|---|
C | ✅ | KeySignatureOfC |
Cb | ❌ | |
C# | ❌ | |
D | ✅ | KeySignatureOfD |
Db | ✅ | KeySignatureOfDb |
D# | ❌ | |
E | ✅ | KeySignatureOfE |
Eb | ✅ | KeySignatureOfEb |
E# | ❌ | |
F | ✅ | KeySignatureOfF |
Fb | ❌ | |
F# | ✅ | KeySignatureOfFsharp |
G | ✅ | KeySignatureOfG |
Gb | ✅ | KeySignatureOfGb |
G# | ❌ | |
A | ✅ | KeySignatureOfA |
Ab | ✅ | KeySignatureOfAb |
A# | ❌ | |
B | ✅ | KeySignatureOfB |
Bb | ✅ | KeySignatureOfBb |
B# | ❌ |
A Note is the fundamental element of music. Notes are simply frequencies and are used to create chords and key signatures.
import { Note } from '@patrady/chord-js';
const note = new Note('Eb4');
Attribute | Description | Example |
---|---|---|
getName() |
The name of the note and accidental | note.getName(); // Eb |
getScientificName() |
The name of the note, accidental, and octave | note.getScientificName(); // Eb4 |
getOctave() |
The octave between 0 and 8 | note.getOctave(); // 4 |
getFrequency() |
The frequency in Hz, up to 5 decimal places | note.getFrequency(); // 311.12698 |
getKeyNumber() |
The number of the key on an 88-key piano | note.getKeyNumber(); // 43 |
getMidiValue() |
The MIDI note of the key on an 88-key piano | note.getMidiValue(); // 63 |
When interacting with a MIDI keyboard and you want to convert a MIDI value to a note, use
import { Note } from '@patrady/chord-js';
const note = Note.fromMidi(24);
note.getScientificName(); // C1
For enharmonic notes, the MIDI value will be the same. For example, C# and Db in the 1st octave will have the same MIDI value of 25. To choose a specific enharmonic, normalize the note to a key signature:
import { Note, KeySignatureOfD, KeySignatureOfDb } from '@patrady/chord-js';
const note = Note.fromMidi(25);
new KeySignatureOfD().normalize(note); // C#
new KeySignatureOfDb().normalize(note); // Db
A chord can also be determined from the MIDI notes like so
import { Note } from '@patrady/chord-js';
const C = Note.fromMidi(60);
const E = Note.fromMidi(64);
const G = Note.fromMidi(67);
Chord.for([C, E, G])?.getName(); // C