@tonaljs/abc-notation
Version:
Parse musical notes in abc notation
86 lines (85 loc) • 3.02 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 abc_notation_exports = {};
__export(abc_notation_exports, {
abcToScientificNotation: () => abcToScientificNotation,
default: () => abc_notation_default,
distance: () => distance,
scientificToAbcNotation: () => scientificToAbcNotation,
tokenize: () => tokenize,
transpose: () => transpose
});
module.exports = __toCommonJS(abc_notation_exports);
var import_pitch_distance = require("@tonaljs/pitch-distance");
var import_pitch_note = require("@tonaljs/pitch-note");
var fillStr = (character, times) => Array(times + 1).join(character);
var REGEX = /^(_{1,}|=|\^{1,}|)([abcdefgABCDEFG])([,']*)$/;
function tokenize(str) {
const m = REGEX.exec(str);
if (!m) {
return ["", "", ""];
}
return [m[1], m[2], m[3]];
}
function abcToScientificNotation(str) {
const [acc, letter, oct] = tokenize(str);
if (letter === "") {
return "";
}
let o = 4;
for (let i = 0; i < oct.length; i++) {
o += oct.charAt(i) === "," ? -1 : 1;
}
const a = acc[0] === "_" ? acc.replace(/_/g, "b") : acc[0] === "^" ? acc.replace(/\^/g, "#") : "";
return letter.charCodeAt(0) > 96 ? letter.toUpperCase() + a + (o + 1) : letter + a + o;
}
function scientificToAbcNotation(str) {
const n = (0, import_pitch_note.note)(str);
if (n.empty || !n.oct && n.oct !== 0) {
return "";
}
const { letter, acc, oct } = n;
const a = acc[0] === "b" ? acc.replace(/b/g, "_") : acc.replace(/#/g, "^");
const l = oct > 4 ? letter.toLowerCase() : letter;
const o = oct === 5 ? "" : oct > 4 ? fillStr("'", oct - 5) : fillStr(",", 4 - oct);
return a + l + o;
}
function transpose(note2, interval) {
return scientificToAbcNotation((0, import_pitch_distance.transpose)(abcToScientificNotation(note2), interval));
}
function distance(from, to) {
return (0, import_pitch_distance.distance)(abcToScientificNotation(from), abcToScientificNotation(to));
}
var abc_notation_default = {
abcToScientificNotation,
scientificToAbcNotation,
tokenize,
transpose,
distance
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
abcToScientificNotation,
distance,
scientificToAbcNotation,
tokenize,
transpose
});
//# sourceMappingURL=index.js.map
;