@tonaljs/interval
Version:
Parse and manipulate music intervals
125 lines (124 loc) • 3.82 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name2 in all)
__defProp(target, name2, { get: all[name2], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// index.ts
var interval_exports = {};
__export(interval_exports, {
add: () => add,
addTo: () => addTo,
default: () => interval_default,
distance: () => distance,
fromSemitones: () => fromSemitones,
get: () => get,
invert: () => invert,
name: () => name,
names: () => names,
num: () => num,
quality: () => quality,
semitones: () => semitones,
simplify: () => simplify,
subtract: () => subtract,
transposeFifths: () => transposeFifths
});
module.exports = __toCommonJS(interval_exports);
var import_pitch_distance = require("@tonaljs/pitch-distance");
var import_pitch_interval = require("@tonaljs/pitch-interval");
function names() {
return "1P 2M 3M 4P 5P 6m 7m".split(" ");
}
var get = import_pitch_interval.interval;
var name = (name2) => (0, import_pitch_interval.interval)(name2).name;
var semitones = (name2) => (0, import_pitch_interval.interval)(name2).semitones;
var quality = (name2) => (0, import_pitch_interval.interval)(name2).q;
var num = (name2) => (0, import_pitch_interval.interval)(name2).num;
function simplify(name2) {
const i = (0, import_pitch_interval.interval)(name2);
return i.empty ? "" : i.simple + i.q;
}
function invert(name2) {
const i = (0, import_pitch_interval.interval)(name2);
if (i.empty) {
return "";
}
const step = (7 - i.step) % 7;
const alt = i.type === "perfectable" ? -i.alt : -(i.alt + 1);
return (0, import_pitch_interval.interval)({ 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 = import_pitch_distance.distance;
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 (0, import_pitch_interval.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 = (0, import_pitch_interval.interval)(a).coord;
const coordB = (0, import_pitch_interval.interval)(b).coord;
if (coordA && coordB) {
const coord = fn(coordA, coordB);
return (0, import_pitch_interval.coordToInterval)(coord).name;
}
};
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
add,
addTo,
distance,
fromSemitones,
get,
invert,
name,
names,
num,
quality,
semitones,
simplify,
subtract,
transposeFifths
});
//# sourceMappingURL=index.js.map