tonal-detect
Version:
Find the name of pitch class sets
73 lines (68 loc) • 2.13 kB
JavaScript
/**
* [](https://www.npmjs.com/package/tonal-detect)
*
* Find chord and scale names from a collection of notes or pitch classes
*
* This is part of [tonal](https://www.npmjs.com/package/tonal) music theory library.
*
* @example
* import { chord } from "tonal-detect"
* chord(["C", "E", "G", "A"]) // => ["CM6", "Am7"]
*
* @example
* const Detect = require("tonal-detect")
* Detect.chord(["C", "E", "G", "A"]) // => ["CM6", "Am7"]
*
* @module Detect
*/
import { name, pc } from "tonal-note";
import * as Dictionary from "tonal-dictionary";
import { sort, compact } from "tonal-array";
import { modes } from "tonal-pcset";
export function detector(dictionary, defaultBuilder) {
defaultBuilder = defaultBuilder || (function (tonic, names) { return [tonic, names]; });
return function(notes, builder) {
builder = builder || defaultBuilder;
notes = sort(notes.map(pc));
return modes(notes)
.map(function (mode, i) {
var tonic = name(notes[i]);
var names = dictionary.names(mode);
return names.length ? builder(tonic, names) : null;
})
.filter(function (x) { return x; });
};
}
/**
* Given a collection of notes or pitch classes, try to find the chord name
* @function
* @param {Array<String>} notes
* @return {Array<String>} chord names or empty array
* @example
* Detect.chord(["C", "E", "G", "A"]) // => ["CM6", "Am7"]
*/
export var chord = detector(
Dictionary.chord,
function (tonic, names) { return tonic + names[0]; }
);
/**
* Given a collection of notes or pitch classes, try to find the scale names
* @function
* @param {Array<String>} notes
* @return {Array<String>} scale names or empty array
* @example
* Detect.scale(["f3", "a", "c5", "e2", "d", "g2", "b6"]) // => [
* "C major",
* "D dorian",
* "E phrygian",
* "F lydian",
* "G mixolydian",
* "A aeolian",
* "B locrian"
* ]
*/
export var scale = detector(
Dictionary.scale,
function (tonic, names) { return tonic + " " + names[0]; }
);
export var pcset = detector(Dictionary.pcset);