@tonaljs/pitch-distance
Version:
Parse intervals in shorthand notation
69 lines (68 loc) • 2.89 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 name in all)
__defProp(target, name, { get: all[name], 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 pitch_distance_exports = {};
__export(pitch_distance_exports, {
distance: () => distance,
tonicIntervalsTransposer: () => tonicIntervalsTransposer,
transpose: () => transpose
});
module.exports = __toCommonJS(pitch_distance_exports);
var import_pitch_interval = require("@tonaljs/pitch-interval");
var import_pitch_note = require("@tonaljs/pitch-note");
function transpose(noteName, intervalName) {
const note = (0, import_pitch_note.note)(noteName);
const intervalCoord = Array.isArray(intervalName) ? intervalName : (0, import_pitch_interval.interval)(intervalName).coord;
if (note.empty || !intervalCoord || intervalCoord.length < 2) {
return "";
}
const noteCoord = note.coord;
const tr = noteCoord.length === 1 ? [noteCoord[0] + intervalCoord[0]] : [noteCoord[0] + intervalCoord[0], noteCoord[1] + intervalCoord[1]];
return (0, import_pitch_note.coordToNote)(tr).name;
}
function tonicIntervalsTransposer(intervals, tonic) {
const len = intervals.length;
return (normalized) => {
if (!tonic) return "";
const index = normalized < 0 ? (len - -normalized % len) % len : normalized % len;
const octaves = Math.floor(normalized / len);
const root = transpose(tonic, [0, octaves]);
return transpose(root, intervals[index]);
};
}
function distance(fromNote, toNote) {
const from = (0, import_pitch_note.note)(fromNote);
const to = (0, import_pitch_note.note)(toNote);
if (from.empty || to.empty) {
return "";
}
const fcoord = from.coord;
const tcoord = to.coord;
const fifths = tcoord[0] - fcoord[0];
const octs = fcoord.length === 2 && tcoord.length === 2 ? tcoord[1] - fcoord[1] : -Math.floor(fifths * 7 / 12);
const forceDescending = to.height === from.height && to.midi !== null && from.oct === to.oct && from.step > to.step;
return (0, import_pitch_interval.coordToInterval)([fifths, octs], forceDescending).name;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
distance,
tonicIntervalsTransposer,
transpose
});
//# sourceMappingURL=index.js.map