UNPKG

tonal-dictionary

Version:

Tonal key/value utilities

86 lines (77 loc) 2.36 kB
/** * [![npm version](https://img.shields.io/npm/v/tonal-dictionary.svg)](https://www.npmjs.com/package/tonal-dictionary) * * `tonal-dictionary` contains a dictionary of musical scales and chords * * This is part of [tonal](https://www.npmjs.com/package/tonal) music theory library. * * @example * // es6 * import * as Dictionary from "tonal-dictionary" * // es5 * const Dictionary = require("tonal-dictionary") * * @example * Dictionary.chord("Maj7") // => ["1P", "3M", "5P", "7M"] * * @module Dictionary */ import sdata from "./data/scales.json"; import cdata from "./data/chords.json"; import { chroma } from "tonal-pcset"; export var dictionary = function (raw) { var keys = Object.keys(raw).sort(); var data = []; var index = []; var add = function (name, ivls, chroma) { data[name] = ivls; index[chroma] = index[chroma] || []; index[chroma].push(name); }; keys.forEach(function (key) { var ivls = raw[key][0].split(" "); var alias = raw[key][1]; var chr = chroma(ivls); add(key, ivls, chr); if (alias) { alias.forEach(function (a) { return add(a, ivls, chr); }); } }); var allKeys = Object.keys(data).sort(); var dict = function (name) { return data[name]; }; dict.names = function (p) { if (typeof p === "string") { return (index[p] || []).slice(); } else { return (p === true ? allKeys : keys).slice(); } }; return dict; }; export var combine = function (a, b) { var dict = function (name) { return a(name) || b(name); }; dict.names = function (p) { return a.names(p).concat(b.names(p)); }; return dict; }; /** * A dictionary of scales: a function that given a scale name (without tonic) * returns an array of intervals * * @function * @param {string} name * @return {Array} intervals * @example * import { scale } from "tonal-dictionary" * scale("major") // => ["1P", "2M", ...] * scale.names(); // => ["major", ...] */ export var scale = dictionary(sdata); /** * A dictionary of chords: a function that given a chord type * returns an array of intervals * * @function * @param {string} type * @return {Array} intervals * @example * import { chord } from "tonal-dictionary" * chord("Maj7") // => ["1P", "3M", ...] * chord.names(); // => ["Maj3", ...] */ export var chord = dictionary(cdata); export var pcset = combine(scale, chord);