Code Monkey home page Code Monkey logo

chord-js's Introduction

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

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

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
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

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 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#

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');
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

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

chord-js's People

Contributors

patrady avatar danielparvin avatar

Stargazers

 avatar E avatar abjutus avatar  avatar Alexey Strelkov avatar

Watchers

 avatar

Forkers

danielparvin

chord-js's Issues

Publish Package to NPM via GitHub Actions

Hi Patrick,
Would you be interested in implementing a GitHub pipeline to automatically publish the newest versions of this to npm? If so, I'd be interested in helping with that.

a minified js version

Hi, really love the lib.
Could you please add a minified js package so it will be easier to use it in pure javascript web projects?

Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.