2.2.2 • Published 9 months ago

@patrady/chord-js v2.2.2

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

Chord JS

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

Chords

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

ChordExample
MajorChord.for("C E G"); // C
MinorChord.for("C Eb G"); // Cm
SuspendedChord.for("C F G"); // Csus
Suspended SecondChord.for("C D G"); // Csus2
AugmentedChord.for("C E G#"); // Caug
DiminishedChord.for("C Eb Gb"); // Cdim
InvertedChord.for("E G C"); // C/E
SixthChord.for("C E G A"); // C6
Minor SixthChord.for('C Eb G A'); // Cm6
Dominant SeventhChord.for("C E G Bb"); // C7
Diminished SeventhChord.for("C Eb Gb A"); // Cdim7
Augmented SeventhChord.for("C E G# Bb"); // C+7
Major SeventhChord.for("C E G B"); // Cmaj7
Augmented Major SeventhChord.for("C E G# B"); // Cmaj+7
Minor SeventhChord.for("C Eb G Bb"); // Cm7
Minor Major SeventhChord.for("C Eb G B"); // Cm7+
Half-Diminished SeventhChord.for("C Eb Gb Bb"); // Cø7

Key Signatures

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
AttributeDescriptionExample
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 signaturenew KeySignatureOfD().isInKey(new Note("Gb")); // false

Supported Key Signatures

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 SignatureSupportedName
CKeySignatureOfC
Cb
C#
DKeySignatureOfD
DbKeySignatureOfDb
D#
EKeySignatureOfE
EbKeySignatureOfEb
E#
FKeySignatureOfF
Fb
F#KeySignatureOfFsharp
GKeySignatureOfG
GbKeySignatureOfGb
G#
AKeySignatureOfA
AbKeySignatureOfAb
A#
BKeySignatureOfB
BbKeySignatureOfBb
B#

Notes

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');
AttributeDescriptionExample
getName()The name of the note and accidentalnote.getName(); // Eb
getScientificName()The name of the note, accidental, and octavenote.getScientificName(); // Eb4
getOctave()The octave between 0 and 8note.getOctave(); // 4
getFrequency()The frequency in Hz, up to 5 decimal placesnote.getFrequency(); // 311.12698
getKeyNumber()The number of the key on an 88-key pianonote.getKeyNumber(); // 43
getMidiValue()The MIDI note of the key on an 88-key pianonote.getMidiValue(); // 63

MIDI Keyboard

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
2.2.1

9 months ago

2.2.0

9 months ago

2.2.2

9 months ago

2.1.0

10 months ago

1.4.0

10 months ago

2.0.0

10 months ago

1.3.0

10 months ago

1.2.0

2 years ago

1.1.0

2 years ago

1.0.0

2 years ago