Chord Magic
Chord Magic is a parser, disambiguator, transposer, and pretty-printer for musical chords. It runs in Node.js and the browser.
Parse a chord:
var chord = chordMagic; // { root: 'A', quality: 'Major', extended: 'Dominant7' }
Transpose it up 3 half-steps:
chord = chordMagic; // { root: 'C', quality: 'Major', extended: 'Dominant7' }
Pretty-print it:
chordMagic; // 'C7'
Pretty-print it in Do-Re-Mi format:
chordMagic; // 'Do7'
Install
Install with npm:
npm install chord-magic
Then require()
it:
var chordMagic =
ES modules are also supported:
You can also use a script tag:
This exposes a global chordMagic
object.
Usage
Overview
chordMagic.parse(string [, options])
chordMagic.transpose(chord, halfSteps)
chordMagic.prettyPrint(chord [, options])
chordMagic.parse(string [, options])
Parse a string with the given options
. Returns either a structured object describing the parsed chord, or undefined
if the parse failed.
options
takes only a single option, naming
, which can be either:
'English'
(A-B-C-D-E-F-G)'NorthernEuropean'
(A-B-H-C-C#-D-D#-E-F-F#-G)'SouthernEuropean'
(Do-Re-Mi-Fa-So-La-Ti-Do, and variants like Ré and Sol).
If no naming is specified, then 'English'
is the default.
// Parse in three different naming styles:chordMagic;chordMagic;chordMagic; // These all return the same thing:// { root: 'B', quality: 'Major' }
The object returned describes the chord. Some examples:
chordMagic// { root: 'C', quality: 'Major' } chordMagic// { root: 'C', quality: 'Minor' } chordMagic// { root: 'C', quality: 'Minor', extended: 'Minor7' } chordMagic// { root: 'C', quality: 'Major', suspended: 'Sus4' } chordMagic// { root: 'C', quality: 'Major', added: 'Add9' } chordMagic// { root: 'C', quality: 'Major', overridingRoot: 'G' }
This object has six possible fields root
, quality
, extended
, suspended
, added
, and overridingRoot
:
root (required)
The root note of the chord. It will be one of:
'A' 'Bb' 'B' 'C' 'Db' 'D' 'Eb' 'E' 'F' 'Gb' 'G' 'Ab'
Note that this value is always in English format, regardless of the format when you parsed it. Also, flats are always expressed, never sharps (e.g. always 'Bb'
, never 'A#'
). The goal here is that you never have to do the disambiguation yourself.
quality (required)
The chord quality. It will be one of:
'Major' 'Minor' 'Augmented' 'Diminished'
extended
If the chord is extended, then that will be expressed here. One of:
'Major7' 'Minor7' 'Dominant7' 'Diminished7' 'Major9' 'Major11' 'Major13' 'AugmentedDominant7' 'AugmentedMajor7' 'Minor9'
suspended
If the chord is suspended, then that will be expressed here. One of:
'Sus4' 'Sus2'
added
If the chord has an added note, then that will be expressed here. One of:
'Add9' 'Add11' 'Major6' 'SixNine' 'PowerChord'
The 'PowerChord'
is also known as an 'Add5'
, but I thought it was funny to call it a 'PowerChord'
.
overridingRoot
If the chord has a note that overrides the root note (e.g. 'D/F#'
, where F# overrides D), then that will be expressed here. The overridingRoot
can be any of the 12 notes listed above for the root
.
chordMagic.transpose(chord, halfSteps)
Transpose the given chord
object to a new chord by the number of halfSteps
.
This does not modify the input chord
. halfSteps
can be any valid integer.
Examples:
chordMagic// { root: 'Bb', quality: 'Major', extended: 'Major7' } chordMagic// { root: 'B', quality: 'Major', extended: 'Major7' } chordMagic// { root: 'C', quality: 'Major', extended: 'Major7' } chordMagic// { root: 'Db', quality: 'Major', extended: 'Major7' } chordMagic// { root: 'D', quality: 'Major', extended: 'Major7' } chordMagic// { root: 'Ab', quality: 'Major', extended: 'Major7' } chordMagic// { root: 'G', quality: 'Major', extended: 'Major7' } chordMagic// { root: 'Gb', quality: 'Major', extended: 'Major7' }
Of course, this also elegantly handles overriding roots:
chordMagic// { root: 'Eb', quality: 'Major', overridingRoot: 'Bb' }
chordMagic.prettyPrint(chord, [options])
Print a chord
object into a nice string for display.
options
takes only a single option, naming
, which supports some default built-in naming schemes:
English
(default)NorthernEuropean
(contains H)SouthernEuropean
("Do Re Mi" format)
Or custom naming (described below).
Examples:
chordMagic// 'C'chordMagic// 'Do'chordMagic// 'C'chordMagic// 'Cm'chordMagic// 'Cm7'chordMagic// 'C'chordMagic// 'Cmaj7'
By default this will choose a single representation for each note (e.g. always 'Bb'
and never 'A#'
) as well as a single common representation for every other attribute (e.g. always 'm7'
, never 'min7'
, 'minor7'
, etc.).
If you would like to customize the note names (e.g. to show sharps instead of flats), then you can pass an array of the 12 notes starting with A as the naming
member. For instance:
var sharpsOnly = 'A' 'A#' 'B' 'C' 'C#' 'D' 'D#' 'E' 'F' 'F#' 'G' 'G#' chordMagic// 'A#' chordMagic// 'A#' chordMagic// 'D/F#'
Build this project
npm run build
Test this project
npm run test
Or to check code coverage:
npm run coverage