@dsebastien/music-notation 0.4.0 → 0.5.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 +20 -2
- package/dist/blocks/block-source.d.ts +5 -0
- package/dist/blocks/renderers.d.ts +3 -0
- package/dist/chord/guitar.d.ts +4 -0
- package/dist/chord/transitions.d.ts +46 -0
- package/dist/chord/triads.d.ts +43 -0
- package/dist/convert/chord-sheet.d.ts +2 -0
- package/dist/convert/text-tab.d.ts +5 -0
- package/dist/fretboard/fretboard.d.ts +2 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +1269 -24
- package/dist/index.js.map +16 -10
- package/dist/keyboard/keyboard.d.ts +2 -0
- package/dist/music-notation.css +216 -0
- package/dist/music-notation.publish.min.js +239 -13
- package/dist/music-notation.tab.min.js +527 -0
- package/dist/playback/controls.d.ts +21 -0
- package/dist/playback/player.d.ts +50 -0
- package/dist/publish/auto.d.ts +3 -0
- package/dist/publish/index.js +881 -24
- package/dist/publish/index.js.map +14 -10
- package/dist/tab/auto.d.ts +16 -0
- package/dist/tab/index.d.ts +97 -0
- package/dist/tab/index.js +1043 -0
- package/dist/tab/index.js.map +19 -0
- package/package.json +21 -2
package/dist/publish/index.js
CHANGED
|
@@ -220,18 +220,37 @@ function renderGuitarDiagram(doc, voicing, options = {}) {
|
|
|
220
220
|
}
|
|
221
221
|
const open = tuning?.[s];
|
|
222
222
|
const isRoot = options.root !== undefined && options.root !== null && open !== undefined && (open + fret) % 12 === options.root;
|
|
223
|
+
const midi = open !== undefined ? { "data-midi": open + fret } : {};
|
|
223
224
|
if (fret === 0) {
|
|
224
225
|
const [cx, cy] = mark;
|
|
225
|
-
root.appendChild(svg(doc, "circle", {
|
|
226
|
+
root.appendChild(svg(doc, "circle", {
|
|
227
|
+
class: `mn-open${isRoot ? " mn-root" : ""}`,
|
|
228
|
+
cx,
|
|
229
|
+
cy,
|
|
230
|
+
r: 4,
|
|
231
|
+
...midi
|
|
232
|
+
}));
|
|
226
233
|
return;
|
|
227
234
|
}
|
|
228
235
|
const center = at(s, fret - base + 0.5);
|
|
229
236
|
const [cx, cy] = center;
|
|
230
|
-
root.appendChild(svg(doc, "circle", {
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
237
|
+
root.appendChild(svg(doc, "circle", {
|
|
238
|
+
class: `mn-dot${isRoot ? " mn-root" : ""}${options.marks?.[s] ? ` mn-${options.marks[s]}` : ""}`,
|
|
239
|
+
cx,
|
|
240
|
+
cy,
|
|
241
|
+
r: 7,
|
|
242
|
+
...midi
|
|
243
|
+
}));
|
|
244
|
+
const text = options.labels ? options.labels[s] : voicing.fingers[s];
|
|
245
|
+
if (text)
|
|
246
|
+
root.appendChild(svgLabel(doc, text, center, "mn-dot-text"));
|
|
234
247
|
});
|
|
248
|
+
if (tuning) {
|
|
249
|
+
const pitches = voicing.frets.flatMap((f, s) => f === null ? [] : [tuning[s] + f]);
|
|
250
|
+
root.setAttribute("data-mn-pitches", pitches.join(" "));
|
|
251
|
+
root.setAttribute("data-mn-play", "chord");
|
|
252
|
+
root.setAttribute("data-mn-sound", "guitar");
|
|
253
|
+
}
|
|
235
254
|
return root;
|
|
236
255
|
}
|
|
237
256
|
|
|
@@ -757,6 +776,7 @@ function renderFretboard(doc, set, options = {}) {
|
|
|
757
776
|
for (let fret = first;fret <= to; fret++) {
|
|
758
777
|
root.appendChild(svgLine(doc, frame.at(acrossLength, wire(fret)), frame.at(0, wire(fret)), "mn-fret"));
|
|
759
778
|
}
|
|
779
|
+
const sounding = new Set;
|
|
760
780
|
for (let s = strings - 1;s >= 0; s--) {
|
|
761
781
|
const open = tuning[s];
|
|
762
782
|
root.appendChild(svgLine(doc, frame.at(across(s), 0), frame.at(across(s), wire(to)), "mn-string"));
|
|
@@ -767,19 +787,26 @@ function renderFretboard(doc, set, options = {}) {
|
|
|
767
787
|
const [cx, cy] = point;
|
|
768
788
|
const member = memberOf(set, open + fret);
|
|
769
789
|
const chroma = (open + fret) % 12;
|
|
770
|
-
|
|
771
|
-
|
|
790
|
+
const hidden = options.strings !== undefined && !options.strings.includes(s);
|
|
791
|
+
if (!member || hidden || played && !played.has(`${s}:${fret}`)) {
|
|
792
|
+
if (!member && !hidden && options.marks?.ghosts?.has(chroma)) {
|
|
772
793
|
root.appendChild(svg(doc, "circle", { class: "mn-ghost", cx, cy, r: 7 }));
|
|
773
794
|
}
|
|
774
795
|
continue;
|
|
775
796
|
}
|
|
776
797
|
const cls = `mn-dot${markClasses(set, member.chroma, options.marks)}`;
|
|
777
|
-
root.appendChild(svg(doc, "circle", { class: cls, cx, cy, r: 8 }));
|
|
798
|
+
root.appendChild(svg(doc, "circle", { class: cls, cx, cy, r: 8, "data-midi": open + fret }));
|
|
799
|
+
sounding.add(open + fret);
|
|
778
800
|
const text = labelOf(member, label);
|
|
779
801
|
if (text)
|
|
780
802
|
root.appendChild(svgLabel(doc, text, point, "mn-dot-text"));
|
|
781
803
|
}
|
|
782
804
|
}
|
|
805
|
+
root.setAttribute("data-mn-pitches", [...sounding].sort((a, b) => a - b).join(" "));
|
|
806
|
+
root.setAttribute("data-mn-play", "scale");
|
|
807
|
+
root.setAttribute("data-mn-sound", "guitar");
|
|
808
|
+
if (set.root !== null)
|
|
809
|
+
root.setAttribute("data-mn-root", String(set.root));
|
|
783
810
|
return root;
|
|
784
811
|
}
|
|
785
812
|
|
|
@@ -908,7 +935,8 @@ function renderKeyboard(doc, set, options = {}) {
|
|
|
908
935
|
root.appendChild(svg(doc, "rect", {
|
|
909
936
|
class: keyClass(midi, false),
|
|
910
937
|
...key(along, WHITE_W, WHITE_H),
|
|
911
|
-
rx: 2
|
|
938
|
+
rx: 2,
|
|
939
|
+
"data-midi": midi
|
|
912
940
|
}));
|
|
913
941
|
caption(midi, at(along + WHITE_W / 2, WHITE_H - 13), false);
|
|
914
942
|
});
|
|
@@ -920,10 +948,20 @@ function renderKeyboard(doc, set, options = {}) {
|
|
|
920
948
|
root.appendChild(svg(doc, "rect", {
|
|
921
949
|
class: keyClass(black, true),
|
|
922
950
|
...key(along, BLACK_W, BLACK_H),
|
|
923
|
-
rx: 2
|
|
951
|
+
rx: 2,
|
|
952
|
+
"data-midi": black
|
|
924
953
|
}));
|
|
925
954
|
caption(black, at(along + BLACK_W / 2, BLACK_H - 13), true);
|
|
926
955
|
});
|
|
956
|
+
const played = [];
|
|
957
|
+
for (let midi = from;midi <= to; midi++)
|
|
958
|
+
if (memberOf(set, midi))
|
|
959
|
+
played.push(midi);
|
|
960
|
+
root.setAttribute("data-mn-pitches", played.join(" "));
|
|
961
|
+
root.setAttribute("data-mn-play", options.playAs ?? "scale");
|
|
962
|
+
root.setAttribute("data-mn-sound", "piano");
|
|
963
|
+
if (set.root !== null)
|
|
964
|
+
root.setAttribute("data-mn-root", String(set.root));
|
|
927
965
|
return root;
|
|
928
966
|
}
|
|
929
967
|
|
|
@@ -1223,6 +1261,14 @@ function parseRange(text) {
|
|
|
1223
1261
|
const to = m[2] === undefined ? from : Number(m[2]);
|
|
1224
1262
|
return from <= to ? [from, to] : null;
|
|
1225
1263
|
}
|
|
1264
|
+
function parseStrings(text, count) {
|
|
1265
|
+
const range = parseRange(text);
|
|
1266
|
+
const numbers = range ? Array.from({ length: range[1] - range[0] + 1 }, (_, i) => range[0] + i) : text.split(/[\s,]+/).filter(Boolean).map(Number);
|
|
1267
|
+
if (!numbers.length || numbers.some((n) => !Number.isInteger(n) || n < 1 || n > count)) {
|
|
1268
|
+
throw new Error(`strings: use string numbers from 1 (the highest) to ${count}, e.g. 1-4`);
|
|
1269
|
+
}
|
|
1270
|
+
return numbers;
|
|
1271
|
+
}
|
|
1226
1272
|
var SEPARATOR = /^\s*-{3,}\s*$/;
|
|
1227
1273
|
function splitParts(source) {
|
|
1228
1274
|
const lines = source.split(/\r?\n/);
|
|
@@ -1434,7 +1480,8 @@ var SHARED_KEYS = {
|
|
|
1434
1480
|
"highlight",
|
|
1435
1481
|
"compare",
|
|
1436
1482
|
"position",
|
|
1437
|
-
"shape"
|
|
1483
|
+
"shape",
|
|
1484
|
+
"strings"
|
|
1438
1485
|
]
|
|
1439
1486
|
};
|
|
1440
1487
|
var field = (block, ...names) => {
|
|
@@ -1561,6 +1608,7 @@ var renderChord = (doc, block, options) => {
|
|
|
1561
1608
|
const range = field(block, "range");
|
|
1562
1609
|
handOf(block, options);
|
|
1563
1610
|
return renderKeyboard(doc, set, {
|
|
1611
|
+
playAs: "chord",
|
|
1564
1612
|
title,
|
|
1565
1613
|
label: labelField(block, fingers.size ? "fingers" : "notes", fingers.size > 0),
|
|
1566
1614
|
fingers,
|
|
@@ -1615,14 +1663,47 @@ var renderChord = (doc, block, options) => {
|
|
|
1615
1663
|
}
|
|
1616
1664
|
}
|
|
1617
1665
|
const root = symbol ? chordNoteSet(symbol)?.root ?? rootOf(symbol) : null;
|
|
1666
|
+
const labels = dotLabels(field(block, "labels"), symbol, voicing, tuning);
|
|
1667
|
+
const count = voicing.frets.length;
|
|
1668
|
+
const marked = (key) => {
|
|
1669
|
+
const text = field(block, key);
|
|
1670
|
+
return text ? parseStrings(text, count).map((n) => count - n) : [];
|
|
1671
|
+
};
|
|
1672
|
+
const anchors = marked("anchors");
|
|
1673
|
+
const guides = marked("guides");
|
|
1674
|
+
const marks = voicing.frets.map((_, s) => anchors.includes(s) ? "anchor" : guides.includes(s) ? "guide" : null);
|
|
1618
1675
|
return renderGuitarDiagram(doc, voicing, {
|
|
1619
1676
|
title,
|
|
1620
1677
|
root,
|
|
1621
1678
|
tuning,
|
|
1679
|
+
...labels ? { labels } : {},
|
|
1680
|
+
...anchors.length || guides.length ? { marks } : {},
|
|
1622
1681
|
orientation: orientationOf(block, options.orientation?.chord),
|
|
1623
1682
|
hand: handOf(block, options)
|
|
1624
1683
|
});
|
|
1625
1684
|
};
|
|
1685
|
+
function dotLabels(text, symbol, voicing, tuning) {
|
|
1686
|
+
const value = text?.toLowerCase();
|
|
1687
|
+
if (value === undefined || value === "fingers")
|
|
1688
|
+
return null;
|
|
1689
|
+
if (!["intervals", "notes", "none"].includes(value)) {
|
|
1690
|
+
throw new Error(`Unknown labels "${text}": use fingers, intervals, notes or none`);
|
|
1691
|
+
}
|
|
1692
|
+
if (value === "none")
|
|
1693
|
+
return voicing.frets.map(() => null);
|
|
1694
|
+
const set = symbol ? chordNoteSet(symbol) : null;
|
|
1695
|
+
const strings = tuning ?? (voicing.frets.length === 6 ? STANDARD_TUNING : null);
|
|
1696
|
+
if (!set || !strings)
|
|
1697
|
+
throw new Error(`labels: ${value} needs a chord the library knows`);
|
|
1698
|
+
return voicing.frets.map((fret, s) => {
|
|
1699
|
+
if (fret === null || fret === 0)
|
|
1700
|
+
return null;
|
|
1701
|
+
const member = set.members.find((m) => m.chroma === (strings[s] + fret) % 12);
|
|
1702
|
+
if (!member)
|
|
1703
|
+
return null;
|
|
1704
|
+
return value === "notes" || member.interval === null ? member.name : intervalLabel(member.interval);
|
|
1705
|
+
});
|
|
1706
|
+
}
|
|
1626
1707
|
function fingersFor(pitches, fingering) {
|
|
1627
1708
|
const sorted = [...pitches].sort((a, b) => a - b);
|
|
1628
1709
|
const fingers = new Map;
|
|
@@ -1647,6 +1728,7 @@ var renderKeyboardBlock = (doc, block, options) => {
|
|
|
1647
1728
|
fingers = fingersFor(set.pitches, fingering);
|
|
1648
1729
|
}
|
|
1649
1730
|
return renderKeyboard(doc, set, {
|
|
1731
|
+
playAs: !set.scaleType && set.root !== null ? "chord" : "scale",
|
|
1650
1732
|
title: field(block, "title"),
|
|
1651
1733
|
label: labelField(block, fingers ? "fingers" : "notes", fingers !== undefined),
|
|
1652
1734
|
fingers,
|
|
@@ -1674,8 +1756,12 @@ var renderFretboardBlock = (doc, block, options) => {
|
|
|
1674
1756
|
const named = { box: "box", caged: "CAGED", "3nps": "3nps" }[at.system];
|
|
1675
1757
|
cells = orFail(scalePosition(set, tuning ?? STANDARD_TUNING, at.system, at.n), `${set.title} has ${count} ${named} positions: use position 1 to ${count}`);
|
|
1676
1758
|
}
|
|
1759
|
+
const stringsText = field(block, "strings");
|
|
1760
|
+
const count = (tuning ?? STANDARD_TUNING).length;
|
|
1761
|
+
const strings = stringsText ? parseStrings(stringsText, count).map((n) => count - n) : undefined;
|
|
1677
1762
|
return renderFretboard(doc, set, {
|
|
1678
1763
|
title: field(block, "title"),
|
|
1764
|
+
...strings ? { strings } : {},
|
|
1679
1765
|
label: labelField(block, "notes"),
|
|
1680
1766
|
frets,
|
|
1681
1767
|
cells,
|
|
@@ -1699,6 +1785,345 @@ var RENDERERS = {
|
|
|
1699
1785
|
fretboard: fromSource(renderFretboardBlock)
|
|
1700
1786
|
};
|
|
1701
1787
|
|
|
1788
|
+
// src/playback/player.ts
|
|
1789
|
+
var DEFAULT_SEQUENCE = { bpm: 90, strum: 40 };
|
|
1790
|
+
var RING = 2.5;
|
|
1791
|
+
function sequenceOf(kind, pitches, options = DEFAULT_SEQUENCE, root = null) {
|
|
1792
|
+
if (!pitches.length)
|
|
1793
|
+
return [];
|
|
1794
|
+
if (kind === "chord") {
|
|
1795
|
+
const strum = Number.isFinite(options.strum) ? options.strum : DEFAULT_SEQUENCE.strum;
|
|
1796
|
+
const gap = Math.min(200, Math.max(0, strum)) / 1000;
|
|
1797
|
+
return pitches.map((midi, i) => ({
|
|
1798
|
+
midi,
|
|
1799
|
+
time: i * gap,
|
|
1800
|
+
duration: RING + (pitches.length - 1 - i) * gap
|
|
1801
|
+
}));
|
|
1802
|
+
}
|
|
1803
|
+
const bpm = Number.isFinite(options.bpm) ? options.bpm : DEFAULT_SEQUENCE.bpm;
|
|
1804
|
+
const beat = 60 / Math.min(240, Math.max(20, bpm));
|
|
1805
|
+
let up = [...pitches].sort((a, b) => a - b);
|
|
1806
|
+
if (root !== null) {
|
|
1807
|
+
const roots = up.filter((p) => (p % 12 + 12) % 12 === root);
|
|
1808
|
+
if (roots.length >= 2)
|
|
1809
|
+
up = up.filter((p) => p >= roots[0] && p <= roots[roots.length - 1]);
|
|
1810
|
+
}
|
|
1811
|
+
const order = [...up, ...up.slice(0, -1).reverse()];
|
|
1812
|
+
return order.map((midi, i) => ({ midi, time: i * beat, duration: beat * 0.95 }));
|
|
1813
|
+
}
|
|
1814
|
+
var lengthOf = (events) => events.reduce((end, e) => Math.max(end, e.time + e.duration), 0);
|
|
1815
|
+
var frequency = (midi) => 440 * Math.pow(2, (midi - 69) / 12);
|
|
1816
|
+
function pluckedString(midi, sampleRate, seconds, random = Math.random) {
|
|
1817
|
+
const out = new Float32Array(Math.round(sampleRate * seconds));
|
|
1818
|
+
const period = Math.max(2, Math.round(sampleRate / frequency(midi)));
|
|
1819
|
+
const line = new Float32Array(period);
|
|
1820
|
+
for (let i = 0;i < period; i++)
|
|
1821
|
+
line[i] = random() * 2 - 1;
|
|
1822
|
+
const decay = 0.996 + Math.min(0.0035, 0.00004 * (90 - midi));
|
|
1823
|
+
let at = 0;
|
|
1824
|
+
for (let i = 0;i < out.length; i++) {
|
|
1825
|
+
const next = (at + 1) % period;
|
|
1826
|
+
const value = line[at];
|
|
1827
|
+
line[at] = decay * 0.5 * (value + line[next]);
|
|
1828
|
+
out[i] = value * 0.5;
|
|
1829
|
+
at = next;
|
|
1830
|
+
}
|
|
1831
|
+
return out;
|
|
1832
|
+
}
|
|
1833
|
+
function pianoTone(midi, sampleRate, seconds) {
|
|
1834
|
+
const out = new Float32Array(Math.round(sampleRate * seconds));
|
|
1835
|
+
const f = frequency(midi);
|
|
1836
|
+
const harmonics = [1, 0.5, 0.3, 0.15, 0.08, 0.04];
|
|
1837
|
+
for (let i = 0;i < out.length; i++) {
|
|
1838
|
+
const t = i / sampleRate;
|
|
1839
|
+
const attack = Math.min(1, t / 0.005);
|
|
1840
|
+
let value = 0;
|
|
1841
|
+
harmonics.forEach((amp, k) => {
|
|
1842
|
+
const n = k + 1;
|
|
1843
|
+
if (f * n > sampleRate / 2)
|
|
1844
|
+
return;
|
|
1845
|
+
value += amp * Math.exp(-t * (1.2 + n * 0.8)) * Math.sin(2 * Math.PI * f * n * t);
|
|
1846
|
+
});
|
|
1847
|
+
out[i] = 0.3 * attack * value;
|
|
1848
|
+
}
|
|
1849
|
+
return out;
|
|
1850
|
+
}
|
|
1851
|
+
var contexts = new WeakMap;
|
|
1852
|
+
function contextFor(win) {
|
|
1853
|
+
const existing = contexts.get(win);
|
|
1854
|
+
if (existing)
|
|
1855
|
+
return existing;
|
|
1856
|
+
const Ctor = win.AudioContext;
|
|
1857
|
+
if (!Ctor)
|
|
1858
|
+
return null;
|
|
1859
|
+
const ctx = new Ctor;
|
|
1860
|
+
contexts.set(win, ctx);
|
|
1861
|
+
return ctx;
|
|
1862
|
+
}
|
|
1863
|
+
var STRING_SECONDS = 4;
|
|
1864
|
+
var PIANO_PARTIALS = [0, 1, 0.5, 0.3, 0.15, 0.08, 0.04];
|
|
1865
|
+
function lightPlayer(win) {
|
|
1866
|
+
const strings = new Map;
|
|
1867
|
+
const waves = new WeakMap;
|
|
1868
|
+
const stringBuffer = (ctx, midi) => {
|
|
1869
|
+
let buffer = strings.get(midi);
|
|
1870
|
+
if (!buffer) {
|
|
1871
|
+
const data = pluckedString(midi, ctx.sampleRate, STRING_SECONDS);
|
|
1872
|
+
buffer = ctx.createBuffer(1, data.length, ctx.sampleRate);
|
|
1873
|
+
buffer.getChannelData(0).set(data);
|
|
1874
|
+
strings.set(midi, buffer);
|
|
1875
|
+
}
|
|
1876
|
+
return buffer;
|
|
1877
|
+
};
|
|
1878
|
+
const pianoWave = (ctx) => {
|
|
1879
|
+
let wave = waves.get(ctx);
|
|
1880
|
+
if (!wave) {
|
|
1881
|
+
const real = new Float32Array(PIANO_PARTIALS.length);
|
|
1882
|
+
wave = ctx.createPeriodicWave(real, Float32Array.from(PIANO_PARTIALS));
|
|
1883
|
+
waves.set(ctx, wave);
|
|
1884
|
+
}
|
|
1885
|
+
return wave;
|
|
1886
|
+
};
|
|
1887
|
+
return {
|
|
1888
|
+
play(events, { sound, volume, onEnd }) {
|
|
1889
|
+
const ctx = contextFor(win);
|
|
1890
|
+
if (!ctx || !events.length)
|
|
1891
|
+
return { stop() {} };
|
|
1892
|
+
ctx.resume();
|
|
1893
|
+
if (sound === "guitar")
|
|
1894
|
+
for (const e of events)
|
|
1895
|
+
stringBuffer(ctx, e.midi);
|
|
1896
|
+
const gain = ctx.createGain();
|
|
1897
|
+
gain.gain.value = Number.isFinite(volume) ? Math.min(1, Math.max(0, volume)) : 0.8;
|
|
1898
|
+
gain.connect(ctx.destination);
|
|
1899
|
+
const start = ctx.currentTime + 0.05;
|
|
1900
|
+
const sources = events.map((e) => {
|
|
1901
|
+
const at = start + e.time;
|
|
1902
|
+
const end = at + e.duration;
|
|
1903
|
+
const env = ctx.createGain();
|
|
1904
|
+
env.connect(gain);
|
|
1905
|
+
let source;
|
|
1906
|
+
if (sound === "guitar") {
|
|
1907
|
+
const buffer = ctx.createBufferSource();
|
|
1908
|
+
buffer.buffer = stringBuffer(ctx, e.midi);
|
|
1909
|
+
source = buffer;
|
|
1910
|
+
env.gain.setValueAtTime(1, end);
|
|
1911
|
+
} else {
|
|
1912
|
+
const osc = ctx.createOscillator();
|
|
1913
|
+
osc.setPeriodicWave(pianoWave(ctx));
|
|
1914
|
+
osc.frequency.value = frequency(e.midi);
|
|
1915
|
+
source = osc;
|
|
1916
|
+
env.gain.setValueAtTime(0, at);
|
|
1917
|
+
env.gain.linearRampToValueAtTime(0.35, at + 0.005);
|
|
1918
|
+
env.gain.exponentialRampToValueAtTime(0.02, Math.max(at + 0.01, end));
|
|
1919
|
+
}
|
|
1920
|
+
env.gain.linearRampToValueAtTime(0, end + 0.08);
|
|
1921
|
+
source.connect(env);
|
|
1922
|
+
source.start(at);
|
|
1923
|
+
source.stop(end + 0.1);
|
|
1924
|
+
return source;
|
|
1925
|
+
});
|
|
1926
|
+
const finish = () => gain.disconnect();
|
|
1927
|
+
const timer = win.setTimeout(() => {
|
|
1928
|
+
finish();
|
|
1929
|
+
onEnd?.();
|
|
1930
|
+
}, (lengthOf(events) + 0.15) * 1000);
|
|
1931
|
+
return {
|
|
1932
|
+
stop() {
|
|
1933
|
+
win.clearTimeout(timer);
|
|
1934
|
+
for (const s of sources) {
|
|
1935
|
+
try {
|
|
1936
|
+
s.stop();
|
|
1937
|
+
} catch {}
|
|
1938
|
+
}
|
|
1939
|
+
finish();
|
|
1940
|
+
}
|
|
1941
|
+
};
|
|
1942
|
+
}
|
|
1943
|
+
};
|
|
1944
|
+
}
|
|
1945
|
+
|
|
1946
|
+
// src/playback/controls.ts
|
|
1947
|
+
var clamp = (n, lo, hi, fallback) => n === undefined || !Number.isFinite(n) ? fallback : Math.min(hi, Math.max(lo, n));
|
|
1948
|
+
var ICONS = {
|
|
1949
|
+
play: "M4 2.5v11l9-5.5z",
|
|
1950
|
+
stop: "M4 4h8v8H4z",
|
|
1951
|
+
loop: "M3 8a5 5 0 0 1 8.5-3.5L13 6V2.5M13 8a5 5 0 0 1-8.5 3.5L3 10v3.5",
|
|
1952
|
+
volume: "M2 6h3l4-3v10l-4-3H2zM11 5.5a3.5 3.5 0 0 1 0 5"
|
|
1953
|
+
};
|
|
1954
|
+
function icon(doc, name) {
|
|
1955
|
+
const root = svg(doc, "svg", {
|
|
1956
|
+
class: `mn-play-icon mn-play-icon-${name}`,
|
|
1957
|
+
viewBox: "0 0 16 16",
|
|
1958
|
+
width: 12,
|
|
1959
|
+
height: 12,
|
|
1960
|
+
"aria-hidden": "true"
|
|
1961
|
+
});
|
|
1962
|
+
root.appendChild(svg(doc, "path", { d: ICONS[name] }));
|
|
1963
|
+
return root;
|
|
1964
|
+
}
|
|
1965
|
+
var players = new WeakMap;
|
|
1966
|
+
function lightFor(win) {
|
|
1967
|
+
let player = players.get(win);
|
|
1968
|
+
if (!player) {
|
|
1969
|
+
player = lightPlayer(win);
|
|
1970
|
+
players.set(win, player);
|
|
1971
|
+
}
|
|
1972
|
+
return player;
|
|
1973
|
+
}
|
|
1974
|
+
function playerFor(win, options) {
|
|
1975
|
+
const given = typeof options.player === "function" ? options.player() : options.player;
|
|
1976
|
+
return given ?? lightFor(win);
|
|
1977
|
+
}
|
|
1978
|
+
function dataOf(diagram) {
|
|
1979
|
+
const pitches = (diagram.getAttribute("data-mn-pitches") ?? "").split(" ").filter(Boolean).map(Number).filter((n) => Number.isFinite(n));
|
|
1980
|
+
if (!pitches.length)
|
|
1981
|
+
return null;
|
|
1982
|
+
return {
|
|
1983
|
+
kind: diagram.getAttribute("data-mn-play") === "chord" ? "chord" : "scale",
|
|
1984
|
+
pitches,
|
|
1985
|
+
sound: diagram.getAttribute("data-mn-sound") === "piano" ? "piano" : "guitar",
|
|
1986
|
+
root: Number.isInteger(Number(diagram.getAttribute("data-mn-root") ?? "x")) ? Number(diagram.getAttribute("data-mn-root")) : null
|
|
1987
|
+
};
|
|
1988
|
+
}
|
|
1989
|
+
function attachPlayback(el, options = {}) {
|
|
1990
|
+
const doc = el.ownerDocument;
|
|
1991
|
+
const win = doc.defaultView;
|
|
1992
|
+
if (!win)
|
|
1993
|
+
return () => {};
|
|
1994
|
+
const stops = [];
|
|
1995
|
+
for (const diagram of [...el.querySelectorAll("svg[data-mn-pitches]")]) {
|
|
1996
|
+
const data = dataOf(diagram);
|
|
1997
|
+
if (!data)
|
|
1998
|
+
continue;
|
|
1999
|
+
stops.push(barFor(doc, win, diagram, data, options));
|
|
2000
|
+
}
|
|
2001
|
+
return () => stops.forEach((stop) => stop());
|
|
2002
|
+
}
|
|
2003
|
+
function barFor(doc, win, diagram, data, options) {
|
|
2004
|
+
const wrap = html(doc, "div", "mn-playable");
|
|
2005
|
+
diagram.parentNode?.insertBefore(wrap, diagram);
|
|
2006
|
+
wrap.appendChild(diagram);
|
|
2007
|
+
const bar = html(doc, "div", "mn-play");
|
|
2008
|
+
bar.setAttribute("role", "group");
|
|
2009
|
+
const name = diagram.getAttribute("aria-label");
|
|
2010
|
+
bar.setAttribute("aria-label", name ? `Play ${name}` : "Playback");
|
|
2011
|
+
wrap.appendChild(bar);
|
|
2012
|
+
const settings = {
|
|
2013
|
+
sound: options.sound ?? data.sound,
|
|
2014
|
+
volume: clamp(options.volume, 0, 1, 0.8),
|
|
2015
|
+
bpm: clamp(options.bpm, 20, 240, DEFAULT_SEQUENCE.bpm),
|
|
2016
|
+
strum: clamp(options.strum, 0, 200, DEFAULT_SEQUENCE.strum),
|
|
2017
|
+
loop: false
|
|
2018
|
+
};
|
|
2019
|
+
let playing = null;
|
|
2020
|
+
const toggle = html(doc, "button", "mn-play-toggle");
|
|
2021
|
+
toggle.type = "button";
|
|
2022
|
+
const show = () => {
|
|
2023
|
+
toggle.replaceChildren(icon(doc, playing ? "stop" : "play"));
|
|
2024
|
+
toggle.setAttribute("aria-label", playing ? "Stop" : "Play");
|
|
2025
|
+
toggle.title = playing ? "Stop" : "Play";
|
|
2026
|
+
};
|
|
2027
|
+
const stop = () => {
|
|
2028
|
+
playing?.stop();
|
|
2029
|
+
playing = null;
|
|
2030
|
+
show();
|
|
2031
|
+
};
|
|
2032
|
+
const play = (events, onEnd) => {
|
|
2033
|
+
const options_ = { sound: settings.sound, volume: settings.volume, onEnd };
|
|
2034
|
+
try {
|
|
2035
|
+
return playerFor(win, options).play(events, options_);
|
|
2036
|
+
} catch {
|
|
2037
|
+
return lightFor(win).play(events, options_);
|
|
2038
|
+
}
|
|
2039
|
+
};
|
|
2040
|
+
const start = () => {
|
|
2041
|
+
stop();
|
|
2042
|
+
if (!wrap.isConnected)
|
|
2043
|
+
return;
|
|
2044
|
+
playing = play(sequenceOf(data.kind, data.pitches, settings, data.root), () => {
|
|
2045
|
+
playing = null;
|
|
2046
|
+
if (settings.loop && wrap.isConnected)
|
|
2047
|
+
start();
|
|
2048
|
+
else
|
|
2049
|
+
show();
|
|
2050
|
+
});
|
|
2051
|
+
show();
|
|
2052
|
+
};
|
|
2053
|
+
toggle.addEventListener("click", () => playing ? stop() : start());
|
|
2054
|
+
show();
|
|
2055
|
+
bar.appendChild(toggle);
|
|
2056
|
+
const loop = html(doc, "button", "mn-play-loop");
|
|
2057
|
+
loop.type = "button";
|
|
2058
|
+
loop.appendChild(icon(doc, "loop"));
|
|
2059
|
+
loop.title = "Loop";
|
|
2060
|
+
loop.setAttribute("aria-label", "Loop");
|
|
2061
|
+
loop.setAttribute("aria-pressed", "false");
|
|
2062
|
+
loop.addEventListener("click", () => {
|
|
2063
|
+
settings.loop = !settings.loop;
|
|
2064
|
+
loop.setAttribute("aria-pressed", String(settings.loop));
|
|
2065
|
+
loop.classList.toggle("mn-on", settings.loop);
|
|
2066
|
+
});
|
|
2067
|
+
bar.appendChild(loop);
|
|
2068
|
+
const slider = (label, min, max, value, set, mark) => {
|
|
2069
|
+
const field = html(doc, "label", "mn-play-field");
|
|
2070
|
+
field.title = `${label}: ${value}`;
|
|
2071
|
+
const tag = html(doc, "span", "mn-play-label");
|
|
2072
|
+
tag.setAttribute("aria-hidden", "true");
|
|
2073
|
+
tag.append(mark);
|
|
2074
|
+
const input = html(doc, "input");
|
|
2075
|
+
input.type = "range";
|
|
2076
|
+
input.min = String(min);
|
|
2077
|
+
input.max = String(max);
|
|
2078
|
+
input.value = String(value);
|
|
2079
|
+
input.setAttribute("aria-label", label);
|
|
2080
|
+
input.addEventListener("input", () => {
|
|
2081
|
+
set(Number(input.value));
|
|
2082
|
+
field.title = `${label}: ${input.value}`;
|
|
2083
|
+
});
|
|
2084
|
+
field.append(tag, input);
|
|
2085
|
+
bar.appendChild(field);
|
|
2086
|
+
};
|
|
2087
|
+
if (data.pitches.length > 1) {
|
|
2088
|
+
if (data.kind === "scale")
|
|
2089
|
+
slider("Tempo (BPM)", 20, 240, settings.bpm, (n) => settings.bpm = n, "♩");
|
|
2090
|
+
else
|
|
2091
|
+
slider("Strum (ms between strings)", 0, 200, settings.strum, (n) => settings.strum = n, "≋");
|
|
2092
|
+
}
|
|
2093
|
+
slider("Volume", 0, 100, Math.round(settings.volume * 100), (n) => settings.volume = n / 100, icon(doc, "volume"));
|
|
2094
|
+
const sound = html(doc, "select", "mn-play-sound");
|
|
2095
|
+
sound.setAttribute("aria-label", "Sound");
|
|
2096
|
+
for (const value of ["guitar", "piano"]) {
|
|
2097
|
+
const option = html(doc, "option", undefined, value === "guitar" ? "Guitar" : "Piano");
|
|
2098
|
+
option.value = value;
|
|
2099
|
+
option.selected = value === settings.sound;
|
|
2100
|
+
sound.appendChild(option);
|
|
2101
|
+
}
|
|
2102
|
+
sound.addEventListener("change", () => {
|
|
2103
|
+
settings.sound = sound.value === "piano" ? "piano" : "guitar";
|
|
2104
|
+
});
|
|
2105
|
+
bar.appendChild(sound);
|
|
2106
|
+
diagram.addEventListener("click", (event) => {
|
|
2107
|
+
const node = event.target;
|
|
2108
|
+
const target = typeof node?.closest === "function" ? node.closest("[data-midi]") : null;
|
|
2109
|
+
const midi = Number(target?.getAttribute("data-midi"));
|
|
2110
|
+
if (!target || !Number.isFinite(midi))
|
|
2111
|
+
return;
|
|
2112
|
+
stop();
|
|
2113
|
+
playing = play([{ midi, time: 0, duration: 1.5 }], () => {
|
|
2114
|
+
playing = null;
|
|
2115
|
+
show();
|
|
2116
|
+
});
|
|
2117
|
+
show();
|
|
2118
|
+
});
|
|
2119
|
+
wrap.addEventListener("pointerdown", (event) => {
|
|
2120
|
+
if (event.pointerType !== "touch" || bar.contains(event.target))
|
|
2121
|
+
return;
|
|
2122
|
+
wrap.classList.toggle("mn-play-open");
|
|
2123
|
+
});
|
|
2124
|
+
return stop;
|
|
2125
|
+
}
|
|
2126
|
+
|
|
1702
2127
|
// src/blocks/properties.ts
|
|
1703
2128
|
var DEFAULT_PROPERTY_NAMES = {
|
|
1704
2129
|
chordSymbol: "chord_symbol",
|
|
@@ -2228,6 +2653,211 @@ function generateVoicings(symbol, options = {}) {
|
|
|
2228
2653
|
return [...results.values()].sort((a, b) => a.difficulty - b.difficulty || sounding(b) - sounding(a) || a.baseFret - b.baseFret || a.frets.join(",").localeCompare(b.frets.join(",")));
|
|
2229
2654
|
}
|
|
2230
2655
|
|
|
2656
|
+
// src/chord/transitions.ts
|
|
2657
|
+
var FINGER_NAMES = {
|
|
2658
|
+
"1": "index finger",
|
|
2659
|
+
"2": "middle finger",
|
|
2660
|
+
"3": "ring finger",
|
|
2661
|
+
"4": "pinky",
|
|
2662
|
+
T: "thumb"
|
|
2663
|
+
};
|
|
2664
|
+
var ordinal = (n) => `${n}${n % 100 >= 11 && n % 100 <= 13 ? "th" : ["th", "st", "nd", "rd"][n % 10] ?? "th"}`;
|
|
2665
|
+
var MAX_SLIDE = 3;
|
|
2666
|
+
function stringName(tuning, s) {
|
|
2667
|
+
const name = noteOf(tuning[s]).replace(/-?\d+$/, "");
|
|
2668
|
+
const same = tuning.filter((p) => noteOf(p).replace(/-?\d+$/, "") === name).length;
|
|
2669
|
+
if (same < 2)
|
|
2670
|
+
return `the ${name} string`;
|
|
2671
|
+
if (same > 2)
|
|
2672
|
+
return `the ${ordinal(tuning.length - s)} string (${name})`;
|
|
2673
|
+
const lowest = tuning.findIndex((p) => noteOf(p).replace(/-?\d+$/, "") === name);
|
|
2674
|
+
return `the ${s === lowest ? "low" : "high"} ${name} string`;
|
|
2675
|
+
}
|
|
2676
|
+
var lowestFret = (frets) => {
|
|
2677
|
+
const fretted = frets.filter((f) => f !== null && f > 0);
|
|
2678
|
+
return fretted.length ? Math.min(...fretted) : 0;
|
|
2679
|
+
};
|
|
2680
|
+
function move(from, fromFingers, to, tuning) {
|
|
2681
|
+
const anchors = [];
|
|
2682
|
+
const guides = [];
|
|
2683
|
+
let movement = 0;
|
|
2684
|
+
const tips = [];
|
|
2685
|
+
to.frets.forEach((fret, s) => {
|
|
2686
|
+
const finger = to.fingers[s];
|
|
2687
|
+
if (fret === null || fret === 0 || !finger)
|
|
2688
|
+
return;
|
|
2689
|
+
const before = from[s];
|
|
2690
|
+
if (before === fret && fromFingers[s] === finger) {
|
|
2691
|
+
anchors.push(s);
|
|
2692
|
+
return;
|
|
2693
|
+
}
|
|
2694
|
+
if (before !== null && before !== undefined && before > 0 && fromFingers[s] === finger && Math.abs(fret - before) <= MAX_SLIDE) {
|
|
2695
|
+
guides.push(s);
|
|
2696
|
+
movement += 0.5 + 0.1 * Math.abs(fret - before);
|
|
2697
|
+
return;
|
|
2698
|
+
}
|
|
2699
|
+
movement += 1;
|
|
2700
|
+
});
|
|
2701
|
+
const placed = new Set(to.frets.flatMap((f, s) => f && to.fingers[s] && !anchors.includes(s) && !guides.includes(s) ? [to.fingers[s]] : []));
|
|
2702
|
+
const strings = new Set(to.frets.flatMap((f, s) => f && to.fingers[s] && !anchors.includes(s) && !guides.includes(s) ? [s] : []));
|
|
2703
|
+
movement -= strings.size - placed.size;
|
|
2704
|
+
const shift = Math.abs(lowestFret(to.frets) - lowestFret(from));
|
|
2705
|
+
movement += 0.3 * shift;
|
|
2706
|
+
const fingerName = (f) => FINGER_NAMES[f ?? ""] ?? "finger";
|
|
2707
|
+
const seen = new Set;
|
|
2708
|
+
for (const s of anchors) {
|
|
2709
|
+
const f = to.fingers[s];
|
|
2710
|
+
if (seen.has(f))
|
|
2711
|
+
continue;
|
|
2712
|
+
seen.add(f);
|
|
2713
|
+
tips.push(`Keep your ${fingerName(f)} on ${stringName(tuning, s)}'s ${ordinal(to.frets[s])} fret.`);
|
|
2714
|
+
}
|
|
2715
|
+
for (const s of guides) {
|
|
2716
|
+
const f = to.fingers[s];
|
|
2717
|
+
if (seen.has(f))
|
|
2718
|
+
continue;
|
|
2719
|
+
seen.add(f);
|
|
2720
|
+
tips.push(`Slide your ${fingerName(f)} along ${stringName(tuning, s)} from fret ${from[s]} to ${to.frets[s]}.`);
|
|
2721
|
+
}
|
|
2722
|
+
if (shift >= 2) {
|
|
2723
|
+
const up = lowestFret(to.frets) > lowestFret(from);
|
|
2724
|
+
tips.push(`Move your hand ${up ? "up" : "down"} ${shift} frets.`);
|
|
2725
|
+
}
|
|
2726
|
+
if (!anchors.length && !guides.length && shift < 2) {
|
|
2727
|
+
tips.push("Lift all your fingers together and place them as one shape.");
|
|
2728
|
+
}
|
|
2729
|
+
return { anchors, guides, shift, movement: Math.round(movement * 10) / 10, tips };
|
|
2730
|
+
}
|
|
2731
|
+
var score = (step, voicing) => step.movement + 0.4 * voicing.difficulty;
|
|
2732
|
+
function transition(from, toChord, options = {}) {
|
|
2733
|
+
const tuning = options.tuning ?? STANDARD_TUNING;
|
|
2734
|
+
if (from.length !== tuning.length)
|
|
2735
|
+
return [];
|
|
2736
|
+
const fromFingers = options.fromFingers ?? fingerVoicing(from) ?? from.map(() => null);
|
|
2737
|
+
const candidates = generateVoicings(toChord, { tuning }).slice(0, options.candidates ?? 30);
|
|
2738
|
+
return candidates.map((voicing) => ({ voicing, ...move(from, fromFingers, voicing, tuning) })).sort((a, b) => score(a, a.voicing) - score(b, b.voicing));
|
|
2739
|
+
}
|
|
2740
|
+
function smoothProgression(chords, options = {}) {
|
|
2741
|
+
const tuning = options.tuning ?? STANDARD_TUNING;
|
|
2742
|
+
const lists = chords.map((c) => generateVoicings(c, { tuning }).slice(0, options.candidates ?? 12));
|
|
2743
|
+
if (!chords.length || lists.some((l) => !l.length))
|
|
2744
|
+
return [];
|
|
2745
|
+
let costs = lists[0].map((v) => 0.4 * v.difficulty);
|
|
2746
|
+
const back = [];
|
|
2747
|
+
for (let i = 1;i < lists.length; i++) {
|
|
2748
|
+
const prev = lists[i - 1];
|
|
2749
|
+
const fingersOf = prev.map((v) => v.fingers);
|
|
2750
|
+
const next = lists[i].map((v) => {
|
|
2751
|
+
let best = Infinity;
|
|
2752
|
+
let from = 0;
|
|
2753
|
+
prev.forEach((p, j) => {
|
|
2754
|
+
const step = move(p.frets, fingersOf[j], v, tuning);
|
|
2755
|
+
const total = costs[j] + score(step, v);
|
|
2756
|
+
if (total < best) {
|
|
2757
|
+
best = total;
|
|
2758
|
+
from = j;
|
|
2759
|
+
}
|
|
2760
|
+
});
|
|
2761
|
+
return { best, from };
|
|
2762
|
+
});
|
|
2763
|
+
back.push(next.map((n) => n.from));
|
|
2764
|
+
costs = next.map((n) => n.best);
|
|
2765
|
+
}
|
|
2766
|
+
const picks = [costs.indexOf(Math.min(...costs))];
|
|
2767
|
+
for (let i = back.length - 1;i >= 0; i--)
|
|
2768
|
+
picks.unshift(back[i][picks[0]]);
|
|
2769
|
+
return picks.map((pick, i) => {
|
|
2770
|
+
const voicing = lists[i][pick];
|
|
2771
|
+
if (i === 0)
|
|
2772
|
+
return { chord: chords[0], voicing };
|
|
2773
|
+
const previous = lists[i - 1][picks[i - 1]];
|
|
2774
|
+
return {
|
|
2775
|
+
chord: chords[i],
|
|
2776
|
+
voicing,
|
|
2777
|
+
...move(previous.frets, previous.fingers, voicing, tuning)
|
|
2778
|
+
};
|
|
2779
|
+
});
|
|
2780
|
+
}
|
|
2781
|
+
|
|
2782
|
+
// src/chord/triads.ts
|
|
2783
|
+
import { Key } from "tonal";
|
|
2784
|
+
var inversionName = (inversion) => inversion === 0 ? "root position" : inversion === 1 ? "1st inversion" : "2nd inversion";
|
|
2785
|
+
function triadVoicings(symbol, options) {
|
|
2786
|
+
const tuning = options.tuning ?? STANDARD_TUNING;
|
|
2787
|
+
const set = chordNoteSet(symbol);
|
|
2788
|
+
const members = set?.members.filter((m) => m.interval !== null) ?? [];
|
|
2789
|
+
if (!set || members.length !== 3)
|
|
2790
|
+
return [];
|
|
2791
|
+
const strings = [...new Set(options.strings)].sort((a, b) => a - b);
|
|
2792
|
+
const adjacent = strings.length === 3 && strings[2] - strings[0] === 2;
|
|
2793
|
+
if (!adjacent || strings[2] > tuning.length)
|
|
2794
|
+
return [];
|
|
2795
|
+
const voicings = generateVoicings(symbol, {
|
|
2796
|
+
tuning,
|
|
2797
|
+
strings,
|
|
2798
|
+
minStrings: 3,
|
|
2799
|
+
inversion: options.inversion ?? "any",
|
|
2800
|
+
...options.maxFret !== undefined ? { maxFret: options.maxFret } : {}
|
|
2801
|
+
});
|
|
2802
|
+
const lowest = (v) => Math.min(...v.frets.filter((f) => f !== null));
|
|
2803
|
+
const close = (v) => {
|
|
2804
|
+
const played = v.frets.filter((f) => f !== null);
|
|
2805
|
+
return played.length === 3 && Math.max(...played) - Math.min(...played) <= 3;
|
|
2806
|
+
};
|
|
2807
|
+
return voicings.filter((v) => close(v) && v.inversion !== null).map((v) => {
|
|
2808
|
+
let top = -1;
|
|
2809
|
+
let pitch = -Infinity;
|
|
2810
|
+
v.frets.forEach((f, s) => {
|
|
2811
|
+
if (f !== null && tuning[s] + f > pitch) {
|
|
2812
|
+
pitch = tuning[s] + f;
|
|
2813
|
+
top = s;
|
|
2814
|
+
}
|
|
2815
|
+
});
|
|
2816
|
+
const chroma = (tuning[top] + v.frets[top]) % 12;
|
|
2817
|
+
const member = members.find((m) => m.chroma === chroma);
|
|
2818
|
+
return {
|
|
2819
|
+
...v,
|
|
2820
|
+
inversion: v.inversion,
|
|
2821
|
+
top: intervalLabel(member.interval)
|
|
2822
|
+
};
|
|
2823
|
+
}).sort((a, b) => lowest(a) - lowest(b) || a.inversion - b.inversion);
|
|
2824
|
+
}
|
|
2825
|
+
function parseKey(text) {
|
|
2826
|
+
const m = /^\s*([A-Ga-g][#b♯♭]?)\s+(major|minor|maj|min)\s*$/i.exec(text);
|
|
2827
|
+
if (!m)
|
|
2828
|
+
return null;
|
|
2829
|
+
const tonic = m[1][0].toUpperCase() + (m[1][1] ?? "").replace("♯", "#").replace("♭", "b");
|
|
2830
|
+
return { tonic, minor: /^min/i.test(m[2]) };
|
|
2831
|
+
}
|
|
2832
|
+
function keyTriads(key, options) {
|
|
2833
|
+
const parsed = parseKey(key);
|
|
2834
|
+
if (!parsed)
|
|
2835
|
+
return null;
|
|
2836
|
+
const scale = parsed.minor ? Key.minorKey(parsed.tonic).natural : Key.majorKey(parsed.tonic);
|
|
2837
|
+
if (!scale.triads.length)
|
|
2838
|
+
return null;
|
|
2839
|
+
const inversion = options.inversion ?? 0;
|
|
2840
|
+
const out = [];
|
|
2841
|
+
let floor = 0;
|
|
2842
|
+
scale.triads.forEach((symbol, i) => {
|
|
2843
|
+
const list = triadVoicings(symbol, { maxFret: 20, ...options, inversion });
|
|
2844
|
+
if (!list.length)
|
|
2845
|
+
return;
|
|
2846
|
+
const lowest = (t) => Math.min(...t.frets.filter((f) => f !== null));
|
|
2847
|
+
const triad = list.find((t) => lowest(t) >= floor) ?? list[0];
|
|
2848
|
+
floor = lowest(triad);
|
|
2849
|
+
const grade = ["I", "II", "III", "IV", "V", "VI", "VII"][i];
|
|
2850
|
+
const quality = symbol.endsWith("dim") ? "°" : "";
|
|
2851
|
+
const minor = /m(?!aj)/.test(symbol.replace(/^[A-G][#b]?/, "")) || quality;
|
|
2852
|
+
out.push({
|
|
2853
|
+
numeral: (minor ? grade.toLowerCase() : grade) + quality,
|
|
2854
|
+
symbol,
|
|
2855
|
+
triad
|
|
2856
|
+
});
|
|
2857
|
+
});
|
|
2858
|
+
return out;
|
|
2859
|
+
}
|
|
2860
|
+
|
|
2231
2861
|
// src/render/render-block.ts
|
|
2232
2862
|
var LANGUAGES = Object.keys(RENDERERS);
|
|
2233
2863
|
var isNotationLanguage = (language) => Object.prototype.hasOwnProperty.call(RENDERERS, language);
|
|
@@ -2254,7 +2884,7 @@ function renderBlock(language, source, host, given = {}) {
|
|
|
2254
2884
|
}
|
|
2255
2885
|
let parts = null;
|
|
2256
2886
|
try {
|
|
2257
|
-
parts = isDiagramLanguage(language) ? splitParts(text) ?? modeParts(language, text) ?? positionParts(language, text) ?? voicingParts(language, text) : null;
|
|
2887
|
+
parts = isDiagramLanguage(language) ? splitParts(text) ?? modeParts(language, text) ?? positionParts(language, text) ?? transitionParts(language, text) ?? triadParts(language, text) ?? voicingParts(language, text) : null;
|
|
2258
2888
|
} catch (e) {
|
|
2259
2889
|
return failed(el, host, language, messageOf(e));
|
|
2260
2890
|
}
|
|
@@ -2273,7 +2903,16 @@ function renderBlock(language, source, host, given = {}) {
|
|
|
2273
2903
|
return failed(el, host, language, messageOf(e));
|
|
2274
2904
|
}
|
|
2275
2905
|
host.appendChild(el);
|
|
2276
|
-
|
|
2906
|
+
const stopPlayback = options.play ? attachPlayback(el, typeof options.play === "object" ? options.play : {}) : null;
|
|
2907
|
+
return {
|
|
2908
|
+
ok: error === null,
|
|
2909
|
+
el,
|
|
2910
|
+
error,
|
|
2911
|
+
destroy: () => {
|
|
2912
|
+
stopPlayback?.();
|
|
2913
|
+
el.remove();
|
|
2914
|
+
}
|
|
2915
|
+
};
|
|
2277
2916
|
}
|
|
2278
2917
|
function failed(el, host, language, error) {
|
|
2279
2918
|
el.replaceChildren();
|
|
@@ -2363,14 +3002,6 @@ var yes = (text, key) => {
|
|
|
2363
3002
|
return false;
|
|
2364
3003
|
throw new Error(`${key} must be yes or no, got "${text}"`);
|
|
2365
3004
|
};
|
|
2366
|
-
var stringList = (text, count) => {
|
|
2367
|
-
const range = parseRange(text);
|
|
2368
|
-
const numbers = range ? Array.from({ length: range[1] - range[0] + 1 }, (_, i) => range[0] + i) : text.split(/[\s,]+/).filter(Boolean).map(Number);
|
|
2369
|
-
if (!numbers.length || numbers.some((n) => !Number.isInteger(n) || n < 1 || n > count)) {
|
|
2370
|
-
throw new Error(`strings: use string numbers from 1 (the highest) to ${count}, e.g. 1-4`);
|
|
2371
|
-
}
|
|
2372
|
-
return numbers;
|
|
2373
|
-
};
|
|
2374
3005
|
var voicingCache = new Map;
|
|
2375
3006
|
function cachedVoicings(symbol, options) {
|
|
2376
3007
|
const key = JSON.stringify([symbol, options]);
|
|
@@ -2406,6 +3037,8 @@ function voicingParts(language, source) {
|
|
|
2406
3037
|
if (value === undefined)
|
|
2407
3038
|
return null;
|
|
2408
3039
|
const fields = block.fields;
|
|
3040
|
+
if (fields.has("triads"))
|
|
3041
|
+
throw new Error("Use triads or voicings, not both");
|
|
2409
3042
|
const instrument = fields.get("instrument")?.toLowerCase();
|
|
2410
3043
|
if (instrument === "piano" || instrument === "keyboard" || fields.has("pitches") || fields.has("notes")) {
|
|
2411
3044
|
throw new Error("voicings are for guitar and other fretted instruments");
|
|
@@ -2459,7 +3092,7 @@ function voicingParts(language, source) {
|
|
|
2459
3092
|
}
|
|
2460
3093
|
const strings = fields.get("strings");
|
|
2461
3094
|
if (strings)
|
|
2462
|
-
options.strings =
|
|
3095
|
+
options.strings = parseStrings(strings, count);
|
|
2463
3096
|
const inversion = fields.get("inversion")?.trim().toLowerCase();
|
|
2464
3097
|
if (inversion) {
|
|
2465
3098
|
const n = inversion === "root" ? 0 : Number(inversion);
|
|
@@ -2512,6 +3145,220 @@ function voicingParts(language, source) {
|
|
|
2512
3145
|
return [...header.length ? [header.join(`
|
|
2513
3146
|
`)] : [], ...diagrams];
|
|
2514
3147
|
}
|
|
3148
|
+
function triadParts(language, source) {
|
|
3149
|
+
if (language !== "chord")
|
|
3150
|
+
return null;
|
|
3151
|
+
let block;
|
|
3152
|
+
try {
|
|
3153
|
+
block = filled(parseBlockSource(source));
|
|
3154
|
+
} catch {
|
|
3155
|
+
return null;
|
|
3156
|
+
}
|
|
3157
|
+
const value = block.fields.get("triads")?.trim();
|
|
3158
|
+
if (value === undefined)
|
|
3159
|
+
return null;
|
|
3160
|
+
const fields = block.fields;
|
|
3161
|
+
if (fields.has("voicings"))
|
|
3162
|
+
throw new Error("Use triads or voicings, not both");
|
|
3163
|
+
const refused = NOT_WITH_VOICINGS.find((key) => fields.has(key));
|
|
3164
|
+
if (refused)
|
|
3165
|
+
throw new Error(`triads finds the frets itself: remove "${refused}"`);
|
|
3166
|
+
const instrument = fields.get("instrument")?.toLowerCase();
|
|
3167
|
+
if (instrument && instrument !== "guitar" && !fields.get("tuning")) {
|
|
3168
|
+
throw new Error("triads are for guitar and other fretted instruments: give a tuning");
|
|
3169
|
+
}
|
|
3170
|
+
const tuningText = fields.get("tuning");
|
|
3171
|
+
const tuning = tuningText ? parseTuning(tuningText) : null;
|
|
3172
|
+
if (tuningText && !tuning)
|
|
3173
|
+
throw new Error(`Unknown tuning "${tuningText}"`);
|
|
3174
|
+
const count = tuning?.length ?? 6;
|
|
3175
|
+
const strings = parseStrings(value, count);
|
|
3176
|
+
const sorted = [...strings].sort((a, b) => a - b);
|
|
3177
|
+
if (sorted.length !== 3 || sorted[2] - sorted[0] !== 2) {
|
|
3178
|
+
throw new Error(`triads: give three strings next to each other, e.g. 1-3 or 2-4`);
|
|
3179
|
+
}
|
|
3180
|
+
const inversionText = fields.get("inversion")?.trim().toLowerCase();
|
|
3181
|
+
let inversion;
|
|
3182
|
+
if (inversionText) {
|
|
3183
|
+
const n = inversionText === "root" ? 0 : Number(inversionText);
|
|
3184
|
+
if (inversionText === "any")
|
|
3185
|
+
inversion = "any";
|
|
3186
|
+
else if (n === 0 || n === 1 || n === 2)
|
|
3187
|
+
inversion = n;
|
|
3188
|
+
else
|
|
3189
|
+
throw new Error(`inversion: use 0 (root position), 1, 2 or any, got "${inversionText}"`);
|
|
3190
|
+
}
|
|
3191
|
+
const options = { strings: sorted, ...tuning ? { tuning } : {} };
|
|
3192
|
+
const keyText = fields.get("key");
|
|
3193
|
+
let diagrams;
|
|
3194
|
+
const head = fields.get("title");
|
|
3195
|
+
if (keyText) {
|
|
3196
|
+
if (inversion === "any")
|
|
3197
|
+
throw new Error("With a key, pick one inversion: 0, 1 or 2");
|
|
3198
|
+
const triads = keyTriads(keyText, {
|
|
3199
|
+
...options,
|
|
3200
|
+
...inversion !== undefined ? { inversion } : {}
|
|
3201
|
+
});
|
|
3202
|
+
if (!triads)
|
|
3203
|
+
throw new Error(`Unknown key "${keyText}": use e.g. key: G major or key: E minor`);
|
|
3204
|
+
diagrams = triads.map((t) => ({
|
|
3205
|
+
symbol: t.symbol,
|
|
3206
|
+
title: `${t.numeral} · ${t.symbol}`,
|
|
3207
|
+
triad: t.triad
|
|
3208
|
+
}));
|
|
3209
|
+
} else {
|
|
3210
|
+
const written = fields.get("symbol") ?? fields.get("chord") ?? block.head ?? fields.get("name");
|
|
3211
|
+
if (!written)
|
|
3212
|
+
throw new Error("triads needs a chord (e.g. G) or a key (key: G major)");
|
|
3213
|
+
const chord = readChord(written);
|
|
3214
|
+
if (!chord)
|
|
3215
|
+
throw new Error(`Didn't recognize "${written}": try ${suggestChords(written)}`);
|
|
3216
|
+
const notes = chordNoteSet(chord.symbol)?.members.filter((m) => m.interval !== null).length;
|
|
3217
|
+
if (notes !== 3) {
|
|
3218
|
+
throw new Error(`${chord.symbol} isn't a triad (three notes): try voicings: all`);
|
|
3219
|
+
}
|
|
3220
|
+
const list = triadVoicings(chord.symbol, {
|
|
3221
|
+
...options,
|
|
3222
|
+
...inversion !== undefined ? { inversion } : {}
|
|
3223
|
+
});
|
|
3224
|
+
diagrams = list.slice(0, MAX_DIAGRAMS).map((t) => ({
|
|
3225
|
+
symbol: chord.symbol,
|
|
3226
|
+
title: `${head ?? written}, ${inversionName(t.inversion)}`,
|
|
3227
|
+
triad: t
|
|
3228
|
+
}));
|
|
3229
|
+
}
|
|
3230
|
+
if (!diagrams.length)
|
|
3231
|
+
throw new Error("No triad fits on these strings: try another string set");
|
|
3232
|
+
const shared = SHARED_KEYS.chord;
|
|
3233
|
+
const others = [...fields].filter(([key]) => !["triads", "key", "inversion", "symbol", "chord", "name", "title"].includes(key));
|
|
3234
|
+
const header = others.filter(([key]) => key === "columns" || shared.includes(key)).map(([key, v]) => `${key}: ${v}`);
|
|
3235
|
+
if (!fields.has("labels"))
|
|
3236
|
+
header.push("labels: intervals");
|
|
3237
|
+
const own = others.filter(([key]) => key !== "columns" && !shared.includes(key)).map(([key, v]) => `${key}: ${v}`);
|
|
3238
|
+
const frets = (list) => list.some((f) => f !== null && f > 9) ? list.map((f) => f === null ? "x" : f).join("-") : list.map((f) => f === null ? "x" : f).join("");
|
|
3239
|
+
const parts = diagrams.map((d) => [
|
|
3240
|
+
`symbol: ${d.symbol}`,
|
|
3241
|
+
`title: ${d.title}`,
|
|
3242
|
+
`frets: ${frets(d.triad.frets)}`,
|
|
3243
|
+
`fingers: ${d.triad.fingers.map((f) => f ?? "x").join("")}`,
|
|
3244
|
+
...own
|
|
3245
|
+
].join(`
|
|
3246
|
+
`));
|
|
3247
|
+
return [header.join(`
|
|
3248
|
+
`), ...parts];
|
|
3249
|
+
}
|
|
3250
|
+
var fretsText = (list) => list.some((f) => f !== null && f > 9) ? list.map((f) => f === null ? "x" : f).join("-") : list.map((f) => f === null ? "x" : f).join("");
|
|
3251
|
+
var stringNumbers = (indexes, count) => indexes.map((s) => count - s).join(" ");
|
|
3252
|
+
function heldChord(text) {
|
|
3253
|
+
const tokens = text.trim().split(/\s+/);
|
|
3254
|
+
const at = tokens.findIndex((t, i) => i > 0 && parseFrets(t) !== null);
|
|
3255
|
+
if (at < 0)
|
|
3256
|
+
return { written: text.trim(), frets: null, fingers: null };
|
|
3257
|
+
return {
|
|
3258
|
+
written: tokens.slice(0, at).join(" "),
|
|
3259
|
+
frets: parseFrets(tokens[at]),
|
|
3260
|
+
fingers: tokens[at + 1] ?? null
|
|
3261
|
+
};
|
|
3262
|
+
}
|
|
3263
|
+
function transitionParts(language, source) {
|
|
3264
|
+
if (language !== "chord")
|
|
3265
|
+
return null;
|
|
3266
|
+
let block;
|
|
3267
|
+
try {
|
|
3268
|
+
block = filled(parseBlockSource(source));
|
|
3269
|
+
} catch {
|
|
3270
|
+
return null;
|
|
3271
|
+
}
|
|
3272
|
+
const fields = block.fields;
|
|
3273
|
+
const fromText = fields.get("from");
|
|
3274
|
+
const progressionText = fields.get("progression");
|
|
3275
|
+
if (fromText === undefined && progressionText === undefined) {
|
|
3276
|
+
if (fields.has("to"))
|
|
3277
|
+
throw new Error("Add the chord you hold: from: C (with its frets: from: C x32010)");
|
|
3278
|
+
return null;
|
|
3279
|
+
}
|
|
3280
|
+
const instrument = fields.get("instrument")?.toLowerCase();
|
|
3281
|
+
if (instrument && instrument !== "guitar" && !fields.get("tuning")) {
|
|
3282
|
+
throw new Error("Transitions are for guitar and other fretted instruments: give a tuning");
|
|
3283
|
+
}
|
|
3284
|
+
if (fromText !== undefined && progressionText !== undefined) {
|
|
3285
|
+
throw new Error("Use from and to, or progression, not both");
|
|
3286
|
+
}
|
|
3287
|
+
const refused = [...NOT_WITH_VOICINGS, "voicings", "triads"].find((key) => fields.has(key));
|
|
3288
|
+
if (refused)
|
|
3289
|
+
throw new Error(`"${refused}" can't be used with ${fromText ? "from" : "progression"}`);
|
|
3290
|
+
const tuningText = fields.get("tuning");
|
|
3291
|
+
const tuning = tuningText ? parseTuning(tuningText) : null;
|
|
3292
|
+
if (tuningText && !tuning)
|
|
3293
|
+
throw new Error(`Unknown tuning "${tuningText}"`);
|
|
3294
|
+
const count = tuning?.length ?? 6;
|
|
3295
|
+
if (count > MAX_GENERATED_STRINGS) {
|
|
3296
|
+
throw new Error(`Transitions work on up to ${MAX_GENERATED_STRINGS} strings`);
|
|
3297
|
+
}
|
|
3298
|
+
const read = (written) => {
|
|
3299
|
+
const chord = readChord(written);
|
|
3300
|
+
if (!chord)
|
|
3301
|
+
throw new Error(`Didn't recognize "${written}": try ${suggestChords(written)}`);
|
|
3302
|
+
return chord.symbol;
|
|
3303
|
+
};
|
|
3304
|
+
const shared = SHARED_KEYS.chord;
|
|
3305
|
+
const header = [...fields].filter(([key]) => key === "columns" || shared.includes(key)).map(([key, v]) => `${key}: ${v}`);
|
|
3306
|
+
const diagram = (symbol, title, frets, fingers, anchors = [], guides = []) => [
|
|
3307
|
+
`symbol: ${symbol}`,
|
|
3308
|
+
`title: ${title}`,
|
|
3309
|
+
`frets: ${fretsText(frets)}`,
|
|
3310
|
+
...fingers ? [
|
|
3311
|
+
`fingers: ${typeof fingers === "string" ? fingers : fingers.map((f) => f ?? "x").join("")}`
|
|
3312
|
+
] : [],
|
|
3313
|
+
...anchors.length ? [`anchors: ${stringNumbers(anchors, count)}`] : [],
|
|
3314
|
+
...guides.length ? [`guides: ${stringNumbers(guides, count)}`] : []
|
|
3315
|
+
].join(`
|
|
3316
|
+
`);
|
|
3317
|
+
const options = tuning ? { tuning } : {};
|
|
3318
|
+
if (progressionText !== undefined) {
|
|
3319
|
+
const names = (/[,|]/.test(progressionText) ? progressionText.split(/[,|]/) : progressionText.split(/\s+/)).map((n) => n.trim()).filter(Boolean);
|
|
3320
|
+
if (names.length < 2)
|
|
3321
|
+
throw new Error("progression: give two chords or more, e.g. G D Em C");
|
|
3322
|
+
if (names.length > MAX_DIAGRAMS)
|
|
3323
|
+
throw new Error(`progression: at most ${MAX_DIAGRAMS} chords`);
|
|
3324
|
+
const steps = smoothProgression(names.map(read), options);
|
|
3325
|
+
if (!steps.length)
|
|
3326
|
+
throw new Error("No playable voicings for this progression");
|
|
3327
|
+
return [
|
|
3328
|
+
...header.length ? [header.join(`
|
|
3329
|
+
`)] : [],
|
|
3330
|
+
...steps.map((s, i) => diagram(s.chord, names[i], s.voicing.frets, s.voicing.fingers, s.anchors, s.guides))
|
|
3331
|
+
];
|
|
3332
|
+
}
|
|
3333
|
+
const toText = fields.get("to");
|
|
3334
|
+
if (!toText)
|
|
3335
|
+
throw new Error("Add the next chord: to: D");
|
|
3336
|
+
const held = heldChord(fromText);
|
|
3337
|
+
const fromSymbol = read(held.written);
|
|
3338
|
+
const fromFrets = held.frets ?? generateVoicings(fromSymbol, options)[0]?.frets ?? null;
|
|
3339
|
+
if (!fromFrets)
|
|
3340
|
+
throw new Error(`No playable voicing of ${held.written}: add its frets`);
|
|
3341
|
+
if (fromFrets.length !== count) {
|
|
3342
|
+
throw new Error(`"${fretsText(fromFrets)}" has ${fromFrets.length} strings, the tuning ${count}`);
|
|
3343
|
+
}
|
|
3344
|
+
const fromFingers = held.fingers ? parseFingers(held.fingers) : fingerVoicing(fromFrets);
|
|
3345
|
+
const howMany = Number(fields.get("count") ?? 3);
|
|
3346
|
+
if (!Number.isInteger(howMany) || howMany < 1 || howMany > MAX_DIAGRAMS - 1) {
|
|
3347
|
+
throw new Error(`count: use a number from 1 to ${MAX_DIAGRAMS - 1}`);
|
|
3348
|
+
}
|
|
3349
|
+
const steps = transition(fromFrets, read(toText), {
|
|
3350
|
+
...options,
|
|
3351
|
+
...fromFingers ? { fromFingers } : {}
|
|
3352
|
+
}).slice(0, howMany);
|
|
3353
|
+
if (!steps.length)
|
|
3354
|
+
throw new Error(`No playable voicing of ${toText}`);
|
|
3355
|
+
return [
|
|
3356
|
+
...header.length ? [header.join(`
|
|
3357
|
+
`)] : [],
|
|
3358
|
+
diagram(fromSymbol, held.written, fromFrets, held.fingers ?? fromFingers),
|
|
3359
|
+
...steps.map((s, i) => diagram(read(toText), howMany > 1 ? `${toText}, ${i + 1}` : toText, s.voicing.frets, s.voicing.fingers, s.anchors, s.guides))
|
|
3360
|
+
];
|
|
3361
|
+
}
|
|
2515
3362
|
function noVoicing(written, symbol, options) {
|
|
2516
3363
|
const strings = options.strings;
|
|
2517
3364
|
const contiguous = strings ? [...strings].sort((a, b) => a - b).every((n, i, all) => i === 0 || n === all[i - 1] + 1) : true;
|
|
@@ -2628,7 +3475,14 @@ function renderRow(doc, language, parts, el, options) {
|
|
|
2628
3475
|
if (block.fields.has("columns")) {
|
|
2629
3476
|
throw new Error('"columns" goes in the shared settings, before the first "---"');
|
|
2630
3477
|
}
|
|
2631
|
-
const directive = [
|
|
3478
|
+
const directive = [
|
|
3479
|
+
"positions",
|
|
3480
|
+
"modes",
|
|
3481
|
+
"voicings",
|
|
3482
|
+
"triads",
|
|
3483
|
+
"from",
|
|
3484
|
+
"progression"
|
|
3485
|
+
].find((key) => block.fields.has(key));
|
|
2632
3486
|
if (directive) {
|
|
2633
3487
|
throw new Error(`"${directive}" draws its own row: use it in a block of its own`);
|
|
2634
3488
|
}
|
|
@@ -2653,6 +3507,7 @@ function renderRow(doc, language, parts, el, options) {
|
|
|
2653
3507
|
var SELECTOR = LANGUAGES.map((l) => `pre > code.language-${l}`).join(", ");
|
|
2654
3508
|
var MARK = "mpRendered";
|
|
2655
3509
|
var SOURCE = "mpSource";
|
|
3510
|
+
var results = new WeakMap;
|
|
2656
3511
|
var languageOf = (code) => LANGUAGES.find((l) => code.classList.contains(`language-${l}`));
|
|
2657
3512
|
function stamp(properties) {
|
|
2658
3513
|
if (!properties || typeof properties !== "object")
|
|
@@ -2686,6 +3541,7 @@ function renderAll(root, options = {}) {
|
|
|
2686
3541
|
const previous = renderingOf(pre);
|
|
2687
3542
|
if (pre.dataset[MARK] && pre.dataset[SOURCE] === print && previous)
|
|
2688
3543
|
return;
|
|
3544
|
+
results.get(pre)?.destroy();
|
|
2689
3545
|
previous?.remove();
|
|
2690
3546
|
pre.style.removeProperty("display");
|
|
2691
3547
|
pre.dataset[SOURCE] = print;
|
|
@@ -2696,6 +3552,7 @@ function renderAll(root, options = {}) {
|
|
|
2696
3552
|
properties: properties ?? null
|
|
2697
3553
|
});
|
|
2698
3554
|
pre.insertAdjacentElement("afterend", result.el);
|
|
3555
|
+
results.set(pre, result);
|
|
2699
3556
|
if (!result.ok) {
|
|
2700
3557
|
pre.dataset[MARK] = "error";
|
|
2701
3558
|
return;
|
|
@@ -2736,5 +3593,5 @@ export {
|
|
|
2736
3593
|
renderAll
|
|
2737
3594
|
};
|
|
2738
3595
|
|
|
2739
|
-
//# debugId=
|
|
3596
|
+
//# debugId=AB197890A3E6D45364756E2164756E21
|
|
2740
3597
|
//# sourceMappingURL=index.js.map
|