@dsebastien/music-notation 0.6.0 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/blocks/renderers.d.ts +2 -0
- package/dist/index.js +122 -18
- package/dist/index.js.map +9 -9
- package/dist/music-notation.publish.min.js +16 -16
- package/dist/music-notation.tab.min.js +4 -4
- package/dist/playback/player.d.ts +3 -5
- package/dist/publish/index.js +122 -18
- package/dist/publish/index.js.map +9 -9
- package/dist/tab/index.d.ts +2 -1
- package/dist/tab/index.js +20 -3
- package/dist/tab/index.js.map +3 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -131,7 +131,7 @@ Add this to your site's `publish.js`. It loads the bundle only on pages that hav
|
|
|
131
131
|
|
|
132
132
|
```js
|
|
133
133
|
;(() => {
|
|
134
|
-
const VERSION = '0.
|
|
134
|
+
const VERSION = '0.6.0'
|
|
135
135
|
const CDN = `https://cdn.jsdelivr.net/npm/@dsebastien/music-notation@${VERSION}/dist`
|
|
136
136
|
const selector = (languages) => languages.map((l) => `pre > code.language-${l}`).join(', ')
|
|
137
137
|
const BLOCKS = selector(['chord', 'chords', 'keyboard', 'fretboard'])
|
|
@@ -28,6 +28,8 @@ export type DiagramRenderer = (doc: Document, block: BlockSource, options: Rende
|
|
|
28
28
|
export type DiagramLanguage = 'chord' | 'keyboard' | 'fretboard';
|
|
29
29
|
/** Keys a row's shared settings may hold, per language (plus `columns`, for the row itself). */
|
|
30
30
|
export declare const SHARED_KEYS: Record<DiagramLanguage, readonly string[]>;
|
|
31
|
+
/** root: in a chord block (or a row it expands into): it only applies to note lists. */
|
|
32
|
+
export declare const ROOT_FOR_LISTS_ONLY = "root only applies to lists of notes on fretboards and keyboards (notes: C E G with root: C): a chord has its own root";
|
|
31
33
|
/** Diagram languages and their renderers. */
|
|
32
34
|
export declare const DIAGRAMS: Record<DiagramLanguage, DiagramRenderer>;
|
|
33
35
|
/** Block languages (code fence names) and their renderers. */
|
package/dist/index.js
CHANGED
|
@@ -900,9 +900,9 @@ function renderFretboard(doc, set, options = {}) {
|
|
|
900
900
|
root.setAttribute("data-mn-play", "scale");
|
|
901
901
|
root.setAttribute("data-mn-sound", "guitar");
|
|
902
902
|
root.setAttribute("data-mn-set", set.title);
|
|
903
|
+
root.setAttribute("data-mn-labels", label);
|
|
904
|
+
root.setAttribute("data-mn-orientation", orientation);
|
|
903
905
|
root.setAttribute("data-mn-frets", `${from}-${to}`);
|
|
904
|
-
if (set.root !== null && !set.list)
|
|
905
|
-
root.setAttribute("data-mn-root", String(set.root));
|
|
906
906
|
return root;
|
|
907
907
|
}
|
|
908
908
|
|
|
@@ -1073,8 +1073,8 @@ function renderKeyboard(doc, set, options = {}) {
|
|
|
1073
1073
|
root.setAttribute("data-mn-play", options.playAs ?? "scale");
|
|
1074
1074
|
root.setAttribute("data-mn-sound", "piano");
|
|
1075
1075
|
root.setAttribute("data-mn-set", set.title);
|
|
1076
|
-
|
|
1077
|
-
|
|
1076
|
+
root.setAttribute("data-mn-labels", label);
|
|
1077
|
+
root.setAttribute("data-mn-orientation", vertical ? "vertical" : "horizontal");
|
|
1078
1078
|
return root;
|
|
1079
1079
|
}
|
|
1080
1080
|
|
|
@@ -1347,8 +1347,16 @@ function namesOf(pitches, flats) {
|
|
|
1347
1347
|
}
|
|
1348
1348
|
return names;
|
|
1349
1349
|
}
|
|
1350
|
+
function isStepwiseRun(pitches) {
|
|
1351
|
+
let sorted = [...new Set(pitches)].sort((a, b) => a - b);
|
|
1352
|
+
if (sorted.length > 1 && sorted[sorted.length - 1] - sorted[0] === 12)
|
|
1353
|
+
sorted = sorted.slice(0, -1);
|
|
1354
|
+
if (sorted.length < 5 || sorted[sorted.length - 1] - sorted[0] >= 12)
|
|
1355
|
+
return false;
|
|
1356
|
+
return sorted.every((pitch, i) => i === 0 || pitch - sorted[i - 1] <= 2);
|
|
1357
|
+
}
|
|
1350
1358
|
function detectChordFromPitches(pitches) {
|
|
1351
|
-
if (pitches.length < 2)
|
|
1359
|
+
if (pitches.length < 2 || isStepwiseRun(pitches))
|
|
1352
1360
|
return null;
|
|
1353
1361
|
const readings = new Set;
|
|
1354
1362
|
for (const flats of [false, true]) {
|
|
@@ -1764,7 +1772,10 @@ function pianoPitches(symbol) {
|
|
|
1764
1772
|
pitches.unshift(48 + bass);
|
|
1765
1773
|
return { ...set, pitches };
|
|
1766
1774
|
}
|
|
1775
|
+
var ROOT_FOR_LISTS_ONLY = "root only applies to lists of notes on fretboards and keyboards (notes: C E G with root: C): a chord has its own root";
|
|
1767
1776
|
var renderChord = (doc, block, options) => {
|
|
1777
|
+
if (block.fields.get("root")?.trim())
|
|
1778
|
+
throw new Error(ROOT_FOR_LISTS_ONLY);
|
|
1768
1779
|
const short = shorthand(block.head);
|
|
1769
1780
|
const name = field(block, "name");
|
|
1770
1781
|
const written = field(block, "symbol", "chord") ?? short.symbol;
|
|
@@ -1940,13 +1951,40 @@ var renderKeyboardBlock = (doc, block, options) => {
|
|
|
1940
1951
|
fingers = fingersFor(octave, fingering);
|
|
1941
1952
|
if (!keys)
|
|
1942
1953
|
set.pitches = octave;
|
|
1954
|
+
} else if (fingering && set.list) {
|
|
1955
|
+
const fingerList = fingering.split(/[\s,-]+/).filter(Boolean);
|
|
1956
|
+
let pitches = set.pitches;
|
|
1957
|
+
if (!pitches) {
|
|
1958
|
+
const chromas = set.title.split(/[\s,]+/).filter(Boolean).map((name) => chromaOf(name) ?? 0);
|
|
1959
|
+
const first = chromas[0] ?? 0;
|
|
1960
|
+
let at = keys ? keys[0] + ((first - keys[0]) % 12 + 12) % 12 : 60 + first;
|
|
1961
|
+
pitches = chromas.map((chroma, i) => {
|
|
1962
|
+
if (i > 0) {
|
|
1963
|
+
const up = ((chroma - at) % 12 + 12) % 12;
|
|
1964
|
+
at += up === 0 ? 12 : up <= 6 ? up : up - 12;
|
|
1965
|
+
}
|
|
1966
|
+
return at;
|
|
1967
|
+
});
|
|
1968
|
+
if (!keys)
|
|
1969
|
+
set.pitches = pitches;
|
|
1970
|
+
}
|
|
1971
|
+
if (fingerList.length !== pitches.length) {
|
|
1972
|
+
throw new Error(`fingering has ${fingerList.length} fingers for ${pitches.length} notes: one finger per note, in the order written`);
|
|
1973
|
+
}
|
|
1974
|
+
if (new Set(pitches).size !== pitches.length) {
|
|
1975
|
+
throw new Error("A key is listed twice: one finger per key");
|
|
1976
|
+
}
|
|
1977
|
+
if (keys && pitches.some((midi) => midi < keys[0] || midi > keys[1])) {
|
|
1978
|
+
throw new Error("The fingered notes go past the range: widen range: or leave it out");
|
|
1979
|
+
}
|
|
1980
|
+
fingers = new Map(pitches.map((midi, i) => [midi, fingerList[i]]));
|
|
1943
1981
|
}
|
|
1944
1982
|
return renderKeyboard(doc, set, {
|
|
1945
1983
|
playAs: !set.scaleType && !set.list && set.root !== null ? "chord" : "scale",
|
|
1946
1984
|
title: field(block, "title"),
|
|
1947
1985
|
label: labelField(block, fingers ? "fingers" : "notes", fingers !== undefined),
|
|
1948
1986
|
fingers,
|
|
1949
|
-
range: keyRange(keys ? keys : fingers && set.pitches ? [set.pitches
|
|
1987
|
+
range: keyRange(keys ? keys : fingers && set.pitches ? [Math.min(...set.pitches), Math.max(...set.pitches)] : keyRangeOf(set)),
|
|
1950
1988
|
orientation: orientationOf(block, options.orientation?.keyboard),
|
|
1951
1989
|
marks: marksOf(block, set)
|
|
1952
1990
|
});
|
|
@@ -2013,7 +2051,7 @@ var RENDERERS = {
|
|
|
2013
2051
|
// src/playback/player.ts
|
|
2014
2052
|
var DEFAULT_SEQUENCE = { bpm: 90, strum: 40 };
|
|
2015
2053
|
var RING = 2.5;
|
|
2016
|
-
function sequenceOf(kind, pitches, options = DEFAULT_SEQUENCE
|
|
2054
|
+
function sequenceOf(kind, pitches, options = DEFAULT_SEQUENCE) {
|
|
2017
2055
|
if (!pitches.length)
|
|
2018
2056
|
return [];
|
|
2019
2057
|
if (kind === "chord") {
|
|
@@ -2027,12 +2065,7 @@ function sequenceOf(kind, pitches, options = DEFAULT_SEQUENCE, root = null) {
|
|
|
2027
2065
|
}
|
|
2028
2066
|
const bpm = Number.isFinite(options.bpm) ? options.bpm : DEFAULT_SEQUENCE.bpm;
|
|
2029
2067
|
const beat = 60 / Math.min(240, Math.max(20, bpm));
|
|
2030
|
-
|
|
2031
|
-
if (root !== null) {
|
|
2032
|
-
const roots = up.filter((p) => (p % 12 + 12) % 12 === root);
|
|
2033
|
-
if (roots.length >= 2)
|
|
2034
|
-
up = up.filter((p) => p >= roots[0] && p <= roots[roots.length - 1]);
|
|
2035
|
-
}
|
|
2068
|
+
const up = [...pitches].sort((a, b) => a - b);
|
|
2036
2069
|
const order = [...up, ...up.slice(0, -1).reverse()];
|
|
2037
2070
|
return order.map((midi, i) => ({ midi, time: i * beat, duration: beat * 0.95 }));
|
|
2038
2071
|
}
|
|
@@ -2207,10 +2240,60 @@ function dataOf(diagram) {
|
|
|
2207
2240
|
return {
|
|
2208
2241
|
kind: diagram.getAttribute("data-mn-play") === "chord" ? "chord" : "scale",
|
|
2209
2242
|
pitches,
|
|
2210
|
-
sound: diagram.getAttribute("data-mn-sound") === "piano" ? "piano" : "guitar"
|
|
2211
|
-
root: Number.isInteger(Number(diagram.getAttribute("data-mn-root") ?? "x")) ? Number(diagram.getAttribute("data-mn-root")) : null
|
|
2243
|
+
sound: diagram.getAttribute("data-mn-sound") === "piano" ? "piano" : "guitar"
|
|
2212
2244
|
};
|
|
2213
2245
|
}
|
|
2246
|
+
var LABELS2 = new Set(["notes", "degrees", "intervals", "none"]);
|
|
2247
|
+
function spelledNote(name, midi) {
|
|
2248
|
+
const octave = Math.floor(midi / 12) - 1;
|
|
2249
|
+
for (const o of [octave, octave + 1, octave - 1])
|
|
2250
|
+
if (midiOf(`${name}${o}`) === midi)
|
|
2251
|
+
return `${name}${o}`;
|
|
2252
|
+
return `${nameOf(midi)}${octave}`;
|
|
2253
|
+
}
|
|
2254
|
+
function redrawFor(doc, diagram, data) {
|
|
2255
|
+
const toGuitar = data.sound === "piano";
|
|
2256
|
+
const pitches = [...new Set(data.pitches)].sort((a, b) => a - b).filter((midi) => !toGuitar || midi >= 40 && midi <= 88);
|
|
2257
|
+
if (!pitches.length)
|
|
2258
|
+
return null;
|
|
2259
|
+
const chord = diagram.getAttribute("data-mn-chord");
|
|
2260
|
+
const named = diagram.getAttribute("data-mn-set");
|
|
2261
|
+
let set = chord && chordNoteSet(chord) || named && (scaleNoteSet(named) ?? (named.includes(" ") ? null : chordNoteSet(named))) || null;
|
|
2262
|
+
if (set && !pitches.every((midi) => set.members.some((m) => m.chroma === midi % 12)))
|
|
2263
|
+
set = null;
|
|
2264
|
+
if (set)
|
|
2265
|
+
set.pitches = pitches;
|
|
2266
|
+
else {
|
|
2267
|
+
const flats = [...diagram.querySelectorAll("[data-mn-note]")].some((el) => /^[A-G]b/.test(el.getAttribute("data-mn-note") ?? ""));
|
|
2268
|
+
const nameAt = (midi) => diagram.querySelector(`[data-midi="${midi}"][data-mn-note]`)?.getAttribute("data-mn-note") ?? nameOf(midi, flats);
|
|
2269
|
+
const rootEl = diagram.querySelector(".mn-root[data-midi]");
|
|
2270
|
+
const rootMidi = rootEl ? Number(rootEl.getAttribute("data-midi")) : null;
|
|
2271
|
+
set = listNoteSet(pitches.map((midi) => spelledNote(nameAt(midi), midi)).join(" "), rootMidi === null ? undefined : nameAt(rootMidi));
|
|
2272
|
+
if (!set)
|
|
2273
|
+
return null;
|
|
2274
|
+
}
|
|
2275
|
+
const written = diagram.getAttribute("data-mn-labels");
|
|
2276
|
+
const label = written && LABELS2.has(written) && (set.root !== null || written === "notes" || written === "none") ? written : "notes";
|
|
2277
|
+
const title = diagram.querySelector(".mn-title")?.textContent ?? undefined;
|
|
2278
|
+
const orientation = diagram.getAttribute("data-mn-orientation") === "vertical" ? "vertical" : undefined;
|
|
2279
|
+
const drawn = toGuitar ? renderFretboard(doc, set, {
|
|
2280
|
+
label,
|
|
2281
|
+
...title ? { title } : {},
|
|
2282
|
+
...orientation ? { orientation } : {},
|
|
2283
|
+
frets: [0, Math.max(12, Math.min(24, pitches[pitches.length - 1] - 64))]
|
|
2284
|
+
}) : renderKeyboard(doc, set, {
|
|
2285
|
+
label,
|
|
2286
|
+
...title ? { title } : {},
|
|
2287
|
+
...orientation ? { orientation } : {},
|
|
2288
|
+
range: keyRangeOf(set),
|
|
2289
|
+
playAs: data.kind
|
|
2290
|
+
});
|
|
2291
|
+
const name = diagram.getAttribute("aria-label");
|
|
2292
|
+
if (name)
|
|
2293
|
+
drawn.setAttribute("aria-label", `${name}, drawn for ${toGuitar ? "guitar" : "piano"}`);
|
|
2294
|
+
drawn.classList.add("mn-redrawn");
|
|
2295
|
+
return drawn;
|
|
2296
|
+
}
|
|
2214
2297
|
function attachPlayback(el, options = {}) {
|
|
2215
2298
|
const doc = el.ownerDocument;
|
|
2216
2299
|
const win = doc.defaultView;
|
|
@@ -2242,6 +2325,8 @@ function barFor(doc, win, diagram, data, options) {
|
|
|
2242
2325
|
loop: false
|
|
2243
2326
|
};
|
|
2244
2327
|
let playing = null;
|
|
2328
|
+
let shown = diagram;
|
|
2329
|
+
const pitchesShown = () => shown === diagram ? data.pitches : dataOf(shown)?.pitches ?? data.pitches;
|
|
2245
2330
|
const toggle = html(doc, "button", "mn-play-toggle");
|
|
2246
2331
|
toggle.type = "button";
|
|
2247
2332
|
const show = () => {
|
|
@@ -2267,7 +2352,7 @@ function barFor(doc, win, diagram, data, options) {
|
|
|
2267
2352
|
stop();
|
|
2268
2353
|
if (!wrap.isConnected)
|
|
2269
2354
|
return;
|
|
2270
|
-
playing = play(sequenceOf(data.kind,
|
|
2355
|
+
playing = play(sequenceOf(data.kind, pitchesShown(), settings), () => {
|
|
2271
2356
|
playing = null;
|
|
2272
2357
|
if (settings.loop && wrap.isConnected)
|
|
2273
2358
|
start();
|
|
@@ -2325,11 +2410,24 @@ function barFor(doc, win, diagram, data, options) {
|
|
|
2325
2410
|
option.selected = value === settings.sound;
|
|
2326
2411
|
sound.appendChild(option);
|
|
2327
2412
|
}
|
|
2413
|
+
let other;
|
|
2414
|
+
const drawFor = () => {
|
|
2415
|
+
if (settings.sound !== data.sound)
|
|
2416
|
+
other ??= redrawFor(doc, diagram, data);
|
|
2417
|
+
const next = settings.sound === data.sound ? diagram : other ?? diagram;
|
|
2418
|
+
if (next !== shown && shown.parentNode === wrap) {
|
|
2419
|
+
wrap.replaceChild(next, shown);
|
|
2420
|
+
shown = next;
|
|
2421
|
+
}
|
|
2422
|
+
};
|
|
2328
2423
|
sound.addEventListener("change", () => {
|
|
2424
|
+
stop();
|
|
2329
2425
|
settings.sound = sound.value === "piano" ? "piano" : "guitar";
|
|
2426
|
+
drawFor();
|
|
2330
2427
|
});
|
|
2428
|
+
drawFor();
|
|
2331
2429
|
bar.appendChild(sound);
|
|
2332
|
-
|
|
2430
|
+
wrap.addEventListener("click", (event) => {
|
|
2333
2431
|
const node = event.target;
|
|
2334
2432
|
const target = typeof node?.closest === "function" ? node.closest("[data-midi]") : null;
|
|
2335
2433
|
const midi = Number(target?.getAttribute("data-midi"));
|
|
@@ -3281,6 +3379,8 @@ function voicingParts(language, source) {
|
|
|
3281
3379
|
if (instrument === "piano" || instrument === "keyboard" || fields.has("pitches") || fields.has("notes")) {
|
|
3282
3380
|
throw new Error("voicings are for guitar and other fretted instruments");
|
|
3283
3381
|
}
|
|
3382
|
+
if (fields.get("root")?.trim())
|
|
3383
|
+
throw new Error(ROOT_FOR_LISTS_ONLY);
|
|
3284
3384
|
const refused = NOT_WITH_VOICINGS.find((key) => fields.has(key));
|
|
3285
3385
|
if (refused)
|
|
3286
3386
|
throw new Error(`voicings finds the frets itself: remove "${refused}"`);
|
|
@@ -3398,6 +3498,8 @@ function triadParts(language, source) {
|
|
|
3398
3498
|
const fields = block.fields;
|
|
3399
3499
|
if (fields.has("voicings"))
|
|
3400
3500
|
throw new Error("Use triads or voicings, not both");
|
|
3501
|
+
if (fields.get("root")?.trim())
|
|
3502
|
+
throw new Error(ROOT_FOR_LISTS_ONLY);
|
|
3401
3503
|
const refused = NOT_WITH_VOICINGS.find((key) => fields.has(key));
|
|
3402
3504
|
if (refused)
|
|
3403
3505
|
throw new Error(`triads finds the frets itself: remove "${refused}"`);
|
|
@@ -3522,6 +3624,8 @@ function transitionParts(language, source) {
|
|
|
3522
3624
|
if (fromText !== undefined && progressionText !== undefined) {
|
|
3523
3625
|
throw new Error("Use from and to, or progression, not both");
|
|
3524
3626
|
}
|
|
3627
|
+
if (fields.get("root")?.trim())
|
|
3628
|
+
throw new Error(ROOT_FOR_LISTS_ONLY);
|
|
3525
3629
|
const refused = [...NOT_WITH_VOICINGS, "voicings", "triads"].find((key) => fields.has(key));
|
|
3526
3630
|
if (refused)
|
|
3527
3631
|
throw new Error(`"${refused}" can't be used with ${fromText ? "from" : "progression"}`);
|
|
@@ -4538,5 +4642,5 @@ export {
|
|
|
4538
4642
|
voicingShape
|
|
4539
4643
|
};
|
|
4540
4644
|
|
|
4541
|
-
//# debugId=
|
|
4645
|
+
//# debugId=F64C39781E3B43C164756E2164756E21
|
|
4542
4646
|
//# sourceMappingURL=index.js.map
|