@tonaljs/interval
Version:
Parse and manipulate music intervals
90 lines • 2.11 kB
JavaScript
// index.ts
import { distance as dist } from "@tonaljs/pitch-distance";
import {
coordToInterval,
interval as props
} from "@tonaljs/pitch-interval";
function names() {
return "1P 2M 3M 4P 5P 6m 7m".split(" ");
}
var get = props;
var name = (name2) => props(name2).name;
var semitones = (name2) => props(name2).semitones;
var quality = (name2) => props(name2).q;
var num = (name2) => props(name2).num;
function simplify(name2) {
const i = props(name2);
return i.empty ? "" : i.simple + i.q;
}
function invert(name2) {
const i = props(name2);
if (i.empty) {
return "";
}
const step = (7 - i.step) % 7;
const alt = i.type === "perfectable" ? -i.alt : -(i.alt + 1);
return props({ step, alt, oct: i.oct, dir: i.dir }).name;
}
var IN = [1, 2, 2, 3, 3, 4, 5, 5, 6, 6, 7, 7];
var IQ = "P m M m M P d P m M m M".split(" ");
function fromSemitones(semitones2) {
const d = semitones2 < 0 ? -1 : 1;
const n = Math.abs(semitones2);
const c = n % 12;
const o = Math.floor(n / 12);
return d * (IN[c] + 7 * o) + IQ[c];
}
var distance = dist;
var add = combinator((a, b) => [a[0] + b[0], a[1] + b[1]]);
var addTo = (interval) => (other) => add(interval, other);
var subtract = combinator((a, b) => [a[0] - b[0], a[1] - b[1]]);
function transposeFifths(interval, fifths) {
const ivl = get(interval);
if (ivl.empty) return "";
const [nFifths, nOcts, dir] = ivl.coord;
return coordToInterval([nFifths + fifths, nOcts, dir]).name;
}
var interval_default = {
names,
get,
name,
num,
semitones,
quality,
fromSemitones,
distance,
invert,
simplify,
add,
addTo,
subtract,
transposeFifths
};
function combinator(fn) {
return (a, b) => {
const coordA = props(a).coord;
const coordB = props(b).coord;
if (coordA && coordB) {
const coord = fn(coordA, coordB);
return coordToInterval(coord).name;
}
};
}
export {
add,
addTo,
interval_default as default,
distance,
fromSemitones,
get,
invert,
name,
names,
num,
quality,
semitones,
simplify,
subtract,
transposeFifths
};
//# sourceMappingURL=index.mjs.map