@dsebastien/music-notation 0.3.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 +26 -2
- package/dist/blocks/block-source.d.ts +5 -0
- package/dist/blocks/renderers.d.ts +3 -0
- package/dist/chord/generate.d.ts +71 -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 +7 -0
- package/dist/index.js +1884 -17
- package/dist/index.js.map +18 -11
- package/dist/keyboard/keyboard.d.ts +2 -0
- package/dist/music-notation.css +216 -0
- package/dist/music-notation.publish.min.js +239 -11
- 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 +1491 -16
- package/dist/publish/index.js.map +16 -11
- 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/dist/voicings/index.js +3 -2
- package/dist/voicings/index.js.map +3 -3
- package/package.json +21 -2
package/dist/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
|
|
|
@@ -534,7 +553,8 @@ var CANONICAL = new Map([
|
|
|
534
553
|
["+", "aug"],
|
|
535
554
|
["sus", "sus4"],
|
|
536
555
|
["add2", "add9"],
|
|
537
|
-
["69", "6/9"]
|
|
556
|
+
["69", "6/9"],
|
|
557
|
+
["alt", "7alt"]
|
|
538
558
|
]);
|
|
539
559
|
var canonicalSuffix = (suffix) => CANONICAL.get(suffix) ?? suffix;
|
|
540
560
|
|
|
@@ -756,6 +776,7 @@ function renderFretboard(doc, set, options = {}) {
|
|
|
756
776
|
for (let fret = first;fret <= to; fret++) {
|
|
757
777
|
root.appendChild(svgLine(doc, frame.at(acrossLength, wire(fret)), frame.at(0, wire(fret)), "mn-fret"));
|
|
758
778
|
}
|
|
779
|
+
const sounding = new Set;
|
|
759
780
|
for (let s = strings - 1;s >= 0; s--) {
|
|
760
781
|
const open = tuning[s];
|
|
761
782
|
root.appendChild(svgLine(doc, frame.at(across(s), 0), frame.at(across(s), wire(to)), "mn-string"));
|
|
@@ -766,19 +787,26 @@ function renderFretboard(doc, set, options = {}) {
|
|
|
766
787
|
const [cx, cy] = point;
|
|
767
788
|
const member = memberOf(set, open + fret);
|
|
768
789
|
const chroma = (open + fret) % 12;
|
|
769
|
-
|
|
770
|
-
|
|
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)) {
|
|
771
793
|
root.appendChild(svg(doc, "circle", { class: "mn-ghost", cx, cy, r: 7 }));
|
|
772
794
|
}
|
|
773
795
|
continue;
|
|
774
796
|
}
|
|
775
797
|
const cls = `mn-dot${markClasses(set, member.chroma, options.marks)}`;
|
|
776
|
-
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);
|
|
777
800
|
const text = labelOf(member, label);
|
|
778
801
|
if (text)
|
|
779
802
|
root.appendChild(svgLabel(doc, text, point, "mn-dot-text"));
|
|
780
803
|
}
|
|
781
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));
|
|
782
810
|
return root;
|
|
783
811
|
}
|
|
784
812
|
|
|
@@ -907,7 +935,8 @@ function renderKeyboard(doc, set, options = {}) {
|
|
|
907
935
|
root.appendChild(svg(doc, "rect", {
|
|
908
936
|
class: keyClass(midi, false),
|
|
909
937
|
...key(along, WHITE_W, WHITE_H),
|
|
910
|
-
rx: 2
|
|
938
|
+
rx: 2,
|
|
939
|
+
"data-midi": midi
|
|
911
940
|
}));
|
|
912
941
|
caption(midi, at(along + WHITE_W / 2, WHITE_H - 13), false);
|
|
913
942
|
});
|
|
@@ -919,10 +948,20 @@ function renderKeyboard(doc, set, options = {}) {
|
|
|
919
948
|
root.appendChild(svg(doc, "rect", {
|
|
920
949
|
class: keyClass(black, true),
|
|
921
950
|
...key(along, BLACK_W, BLACK_H),
|
|
922
|
-
rx: 2
|
|
951
|
+
rx: 2,
|
|
952
|
+
"data-midi": black
|
|
923
953
|
}));
|
|
924
954
|
caption(black, at(along + BLACK_W / 2, BLACK_H - 13), true);
|
|
925
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));
|
|
926
965
|
return root;
|
|
927
966
|
}
|
|
928
967
|
|
|
@@ -1222,6 +1261,14 @@ function parseRange(text) {
|
|
|
1222
1261
|
const to = m[2] === undefined ? from : Number(m[2]);
|
|
1223
1262
|
return from <= to ? [from, to] : null;
|
|
1224
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
|
+
}
|
|
1225
1272
|
var SEPARATOR = /^\s*-{3,}\s*$/;
|
|
1226
1273
|
function splitParts(source) {
|
|
1227
1274
|
const lines = source.split(/\r?\n/);
|
|
@@ -1433,7 +1480,8 @@ var SHARED_KEYS = {
|
|
|
1433
1480
|
"highlight",
|
|
1434
1481
|
"compare",
|
|
1435
1482
|
"position",
|
|
1436
|
-
"shape"
|
|
1483
|
+
"shape",
|
|
1484
|
+
"strings"
|
|
1437
1485
|
]
|
|
1438
1486
|
};
|
|
1439
1487
|
var field = (block, ...names) => {
|
|
@@ -1560,6 +1608,7 @@ var renderChord = (doc, block, options) => {
|
|
|
1560
1608
|
const range = field(block, "range");
|
|
1561
1609
|
handOf(block, options);
|
|
1562
1610
|
return renderKeyboard(doc, set, {
|
|
1611
|
+
playAs: "chord",
|
|
1563
1612
|
title,
|
|
1564
1613
|
label: labelField(block, fingers.size ? "fingers" : "notes", fingers.size > 0),
|
|
1565
1614
|
fingers,
|
|
@@ -1614,14 +1663,47 @@ var renderChord = (doc, block, options) => {
|
|
|
1614
1663
|
}
|
|
1615
1664
|
}
|
|
1616
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);
|
|
1617
1675
|
return renderGuitarDiagram(doc, voicing, {
|
|
1618
1676
|
title,
|
|
1619
1677
|
root,
|
|
1620
1678
|
tuning,
|
|
1679
|
+
...labels ? { labels } : {},
|
|
1680
|
+
...anchors.length || guides.length ? { marks } : {},
|
|
1621
1681
|
orientation: orientationOf(block, options.orientation?.chord),
|
|
1622
1682
|
hand: handOf(block, options)
|
|
1623
1683
|
});
|
|
1624
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
|
+
}
|
|
1625
1707
|
function fingersFor(pitches, fingering) {
|
|
1626
1708
|
const sorted = [...pitches].sort((a, b) => a - b);
|
|
1627
1709
|
const fingers = new Map;
|
|
@@ -1646,6 +1728,7 @@ var renderKeyboardBlock = (doc, block, options) => {
|
|
|
1646
1728
|
fingers = fingersFor(set.pitches, fingering);
|
|
1647
1729
|
}
|
|
1648
1730
|
return renderKeyboard(doc, set, {
|
|
1731
|
+
playAs: !set.scaleType && set.root !== null ? "chord" : "scale",
|
|
1649
1732
|
title: field(block, "title"),
|
|
1650
1733
|
label: labelField(block, fingers ? "fingers" : "notes", fingers !== undefined),
|
|
1651
1734
|
fingers,
|
|
@@ -1673,8 +1756,12 @@ var renderFretboardBlock = (doc, block, options) => {
|
|
|
1673
1756
|
const named = { box: "box", caged: "CAGED", "3nps": "3nps" }[at.system];
|
|
1674
1757
|
cells = orFail(scalePosition(set, tuning ?? STANDARD_TUNING, at.system, at.n), `${set.title} has ${count} ${named} positions: use position 1 to ${count}`);
|
|
1675
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;
|
|
1676
1762
|
return renderFretboard(doc, set, {
|
|
1677
1763
|
title: field(block, "title"),
|
|
1764
|
+
...strings ? { strings } : {},
|
|
1678
1765
|
label: labelField(block, "notes"),
|
|
1679
1766
|
frets,
|
|
1680
1767
|
cells,
|
|
@@ -1698,6 +1785,345 @@ var RENDERERS = {
|
|
|
1698
1785
|
fretboard: fromSource(renderFretboardBlock)
|
|
1699
1786
|
};
|
|
1700
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
|
+
|
|
1701
2127
|
// src/blocks/properties.ts
|
|
1702
2128
|
var DEFAULT_PROPERTY_NAMES = {
|
|
1703
2129
|
chordSymbol: "chord_symbol",
|
|
@@ -1797,6 +2223,641 @@ function blockFromProperties(language, properties, options = {}) {
|
|
|
1797
2223
|
return null;
|
|
1798
2224
|
}
|
|
1799
2225
|
|
|
2226
|
+
// src/chord/generate.ts
|
|
2227
|
+
var MAX_GENERATED_STRINGS = 8;
|
|
2228
|
+
var MAX_SPAN = 6;
|
|
2229
|
+
function chordInfo(symbol) {
|
|
2230
|
+
const set = chordNoteSet(symbol);
|
|
2231
|
+
const parts = splitChordSymbol(symbol);
|
|
2232
|
+
if (!set || !parts)
|
|
2233
|
+
return null;
|
|
2234
|
+
const bass = parts.bass ? chromaOf(parts.bass) : null;
|
|
2235
|
+
const members = set.members.filter((m) => m.interval !== null);
|
|
2236
|
+
const parsed = members.map((m) => {
|
|
2237
|
+
const hit = /^(\d+)([PMmdA]+)$/.exec(m.interval);
|
|
2238
|
+
return { member: m, number: Number(hit?.[1] ?? 0), quality: hit?.[2] ?? "" };
|
|
2239
|
+
});
|
|
2240
|
+
const natural = (p) => p.number > 7 && (p.quality === "M" || p.quality === "P");
|
|
2241
|
+
const highest = Math.max(0, ...parsed.filter(natural).map((p) => p.number));
|
|
2242
|
+
const eleventh = parsed.some((p) => p.number === 11 && p.quality === "P");
|
|
2243
|
+
const tones = parsed.map((p) => {
|
|
2244
|
+
let required = true;
|
|
2245
|
+
if (p.number === 5 && p.quality === "P")
|
|
2246
|
+
required = members.length === 2;
|
|
2247
|
+
else if (natural(p) && p.number < highest)
|
|
2248
|
+
required = false;
|
|
2249
|
+
else if (p.number === 3 && p.quality === "M" && eleventh)
|
|
2250
|
+
required = false;
|
|
2251
|
+
return { chroma: p.member.chroma, number: p.number, required };
|
|
2252
|
+
});
|
|
2253
|
+
if (bass !== null && !tones.some((t) => t.chroma === bass)) {
|
|
2254
|
+
tones.push({ chroma: bass, number: 0, required: true });
|
|
2255
|
+
}
|
|
2256
|
+
return {
|
|
2257
|
+
tones,
|
|
2258
|
+
members,
|
|
2259
|
+
bass,
|
|
2260
|
+
root: chromaOf(parts.root),
|
|
2261
|
+
suffix: canonicalSuffix(parts.suffix)
|
|
2262
|
+
};
|
|
2263
|
+
}
|
|
2264
|
+
function groupingsAt(fret, strings, frets) {
|
|
2265
|
+
const result = [];
|
|
2266
|
+
const walk = (i, acc) => {
|
|
2267
|
+
if (i === strings.length) {
|
|
2268
|
+
result.push(acc);
|
|
2269
|
+
return;
|
|
2270
|
+
}
|
|
2271
|
+
for (let j = i;j < strings.length; j++) {
|
|
2272
|
+
const from = strings[i];
|
|
2273
|
+
const to = strings[j];
|
|
2274
|
+
let ok = true;
|
|
2275
|
+
let under = false;
|
|
2276
|
+
for (let s = from;s <= to; s++) {
|
|
2277
|
+
const f = frets[s];
|
|
2278
|
+
if (f === null || f === undefined || f < fret) {
|
|
2279
|
+
ok = false;
|
|
2280
|
+
break;
|
|
2281
|
+
}
|
|
2282
|
+
if (f > fret)
|
|
2283
|
+
under = true;
|
|
2284
|
+
}
|
|
2285
|
+
if (!ok)
|
|
2286
|
+
break;
|
|
2287
|
+
walk(j + 1, [...acc, { fret, from, to, strings: strings.slice(i, j + 1), under }]);
|
|
2288
|
+
}
|
|
2289
|
+
};
|
|
2290
|
+
walk(0, []);
|
|
2291
|
+
return result;
|
|
2292
|
+
}
|
|
2293
|
+
function fingerChoices(count) {
|
|
2294
|
+
const out = [];
|
|
2295
|
+
const walk = (start, acc) => {
|
|
2296
|
+
if (acc.length === count) {
|
|
2297
|
+
out.push(acc);
|
|
2298
|
+
return;
|
|
2299
|
+
}
|
|
2300
|
+
for (let f = start;f <= 4 - (count - acc.length - 1); f++)
|
|
2301
|
+
walk(f + 1, [...acc, f]);
|
|
2302
|
+
};
|
|
2303
|
+
walk(1, []);
|
|
2304
|
+
return out;
|
|
2305
|
+
}
|
|
2306
|
+
function bestFingering(frets) {
|
|
2307
|
+
const empty = frets.map(() => null);
|
|
2308
|
+
const byFret = new Map;
|
|
2309
|
+
frets.forEach((fret, s) => {
|
|
2310
|
+
if (fret !== null && fret > 0)
|
|
2311
|
+
byFret.set(fret, [...byFret.get(fret) ?? [], s]);
|
|
2312
|
+
});
|
|
2313
|
+
if (!byFret.size)
|
|
2314
|
+
return { fingers: empty, cost: 0, awkward: false, fullBarre: false };
|
|
2315
|
+
const fretList = [...byFret.keys()].sort((a, b) => a - b);
|
|
2316
|
+
const lowest = fretList[0];
|
|
2317
|
+
const top = frets.reduce((t, f, s) => f === null ? t : s, -1);
|
|
2318
|
+
const options = fretList.map((f) => groupingsAt(f, byFret.get(f), frets));
|
|
2319
|
+
let best = null;
|
|
2320
|
+
const consider = (groups) => {
|
|
2321
|
+
if (groups.length > 4)
|
|
2322
|
+
return;
|
|
2323
|
+
const sorted = [...groups].sort((a, b) => a.fret - b.fret || a.from - b.from);
|
|
2324
|
+
for (const choice of fingerChoices(sorted.length)) {
|
|
2325
|
+
let ok = true;
|
|
2326
|
+
for (let i = 0;i < sorted.length && ok; i++) {
|
|
2327
|
+
const g = sorted[i];
|
|
2328
|
+
if (g.under && (choice[i] !== 1 || g.fret !== lowest))
|
|
2329
|
+
ok = false;
|
|
2330
|
+
for (let j = i + 1;j < sorted.length && ok; j++) {
|
|
2331
|
+
if (sorted[j].fret - g.fret > choice[j] - choice[i] + 1)
|
|
2332
|
+
ok = false;
|
|
2333
|
+
}
|
|
2334
|
+
}
|
|
2335
|
+
if (!ok)
|
|
2336
|
+
continue;
|
|
2337
|
+
let cost = sorted.length;
|
|
2338
|
+
let awkward = false;
|
|
2339
|
+
sorted.forEach((g, i) => {
|
|
2340
|
+
const finger = choice[i];
|
|
2341
|
+
if (g.strings.length > 1) {
|
|
2342
|
+
cost += 2.5;
|
|
2343
|
+
if (finger === 1 && g.fret === lowest && g.to === top && !g.under)
|
|
2344
|
+
cost -= 2;
|
|
2345
|
+
}
|
|
2346
|
+
cost += 0.1 * Math.abs(g.fret - lowest - (finger - 1));
|
|
2347
|
+
});
|
|
2348
|
+
for (const a of sorted)
|
|
2349
|
+
for (const b of sorted)
|
|
2350
|
+
if (a !== b && a.fret === b.fret && b.from - a.to >= 3) {
|
|
2351
|
+
if (sorted.some((c) => c.fret > a.fret && c.from > a.to && c.to < b.from))
|
|
2352
|
+
awkward = true;
|
|
2353
|
+
}
|
|
2354
|
+
if (awkward)
|
|
2355
|
+
cost += 1.5;
|
|
2356
|
+
if (!best || cost < best.cost - 0.000000001) {
|
|
2357
|
+
const fingers = [...empty];
|
|
2358
|
+
sorted.forEach((g, i) => {
|
|
2359
|
+
for (const s of g.strings)
|
|
2360
|
+
fingers[s] = String(choice[i]);
|
|
2361
|
+
});
|
|
2362
|
+
const fullBarre = sorted.some((g) => g.strings.length > 1 && (g.under || g.strings.length > 3));
|
|
2363
|
+
best = { fingers, cost, awkward, fullBarre };
|
|
2364
|
+
}
|
|
2365
|
+
}
|
|
2366
|
+
};
|
|
2367
|
+
const walk = (i, acc) => {
|
|
2368
|
+
if (acc.length > 4)
|
|
2369
|
+
return;
|
|
2370
|
+
if (i === options.length) {
|
|
2371
|
+
consider(acc);
|
|
2372
|
+
return;
|
|
2373
|
+
}
|
|
2374
|
+
for (const grouping of options[i])
|
|
2375
|
+
walk(i + 1, [...acc, ...grouping]);
|
|
2376
|
+
};
|
|
2377
|
+
walk(0, []);
|
|
2378
|
+
return best;
|
|
2379
|
+
}
|
|
2380
|
+
function fingerVoicing(frets) {
|
|
2381
|
+
return bestFingering(frets)?.fingers ?? null;
|
|
2382
|
+
}
|
|
2383
|
+
var CAGED_ROOT_STRING = {
|
|
2384
|
+
C: 1,
|
|
2385
|
+
A: 1,
|
|
2386
|
+
G: 0,
|
|
2387
|
+
E: 0,
|
|
2388
|
+
D: 2
|
|
2389
|
+
};
|
|
2390
|
+
var OPEN_CHORDS = new Set([
|
|
2391
|
+
"x32010 x02220 320003 320033 022100 xx0232",
|
|
2392
|
+
"x02210 022000 xx0231",
|
|
2393
|
+
"x32310 x02020 x02223 320001 020100 022130 xx0212 x21202",
|
|
2394
|
+
"x02010 x02213 020000 022030 022033 xx0211",
|
|
2395
|
+
"x32000 x02120 320002 xx0222 021100 xx3210",
|
|
2396
|
+
"x02200 x02230 xx0230 xx0233 022200",
|
|
2397
|
+
"x32030 x32033 x02222 x32210 xx0202 022120 320000 x02212 022020 xx0201",
|
|
2398
|
+
"x20003 332010 200232 032010 330013 330033"
|
|
2399
|
+
].join(" ").split(" "));
|
|
2400
|
+
var compact = (frets) => frets.some((f) => f !== null && f > 9) ? frets.map((f) => f === null ? "x" : f).join("-") : frets.map((f) => f === null ? "x" : f).join("");
|
|
2401
|
+
var sameTuning = (a, b) => a.length === b.length && a.every((v, i) => v === b[i]);
|
|
2402
|
+
var hasBarre = (frets, fingers) => {
|
|
2403
|
+
const counts = new Map;
|
|
2404
|
+
frets.forEach((fret, s) => {
|
|
2405
|
+
const finger = fingers[s];
|
|
2406
|
+
if (fret && finger) {
|
|
2407
|
+
const key = `${finger}@${fret}`;
|
|
2408
|
+
counts.set(key, (counts.get(key) ?? 0) + 1);
|
|
2409
|
+
}
|
|
2410
|
+
});
|
|
2411
|
+
return [...counts.values()].some((n) => n >= 2);
|
|
2412
|
+
};
|
|
2413
|
+
function cagedTargets(info) {
|
|
2414
|
+
const targets = new Map;
|
|
2415
|
+
for (const letter of ["E", "A", "C", "G", "D"]) {
|
|
2416
|
+
const set = chordNoteSet(letter + info.suffix);
|
|
2417
|
+
targets.set(letter, set ? new Set(set.members.map((m) => m.chroma)) : null);
|
|
2418
|
+
}
|
|
2419
|
+
return targets;
|
|
2420
|
+
}
|
|
2421
|
+
function shapeOf(frets, info, targets, tuning, fingers) {
|
|
2422
|
+
const sounding = frets.flatMap((f, s) => f === null ? [] : [s]);
|
|
2423
|
+
if (!sounding.length || tuning.length !== frets.length)
|
|
2424
|
+
return null;
|
|
2425
|
+
const chromas = new Set(sounding.map((s) => (tuning[s] + frets[s]) % 12));
|
|
2426
|
+
if (info.members.length === 2 && info.tones.every((t) => t.number === 1 || t.number === 5)) {
|
|
2427
|
+
return "Power chord";
|
|
2428
|
+
}
|
|
2429
|
+
const shell = sounding.length <= 4 && info.tones.some((t) => t.number === 7 || t.number === 6) && [...chromas].every((c) => info.tones.some((t) => t.chroma === c && [1, 3, 6, 7].includes(t.number)));
|
|
2430
|
+
if (shell)
|
|
2431
|
+
return "Shell";
|
|
2432
|
+
const adjacent = sounding[sounding.length - 1] - sounding[0] === sounding.length - 1;
|
|
2433
|
+
if (sounding.length === 3 && adjacent && chromas.size === 3)
|
|
2434
|
+
return "Triad";
|
|
2435
|
+
const fretted = sounding.map((s) => frets[s]).filter((f) => f > 0);
|
|
2436
|
+
const open = sounding.some((s) => frets[s] === 0);
|
|
2437
|
+
const top = sounding[sounding.length - 1] === frets.length - 1;
|
|
2438
|
+
if (open && top && sounding.length >= 4 && (!fretted.length || Math.max(...fretted) <= 4)) {
|
|
2439
|
+
return "Open";
|
|
2440
|
+
}
|
|
2441
|
+
const barre = fingers ? hasBarre(frets, fingers) : false;
|
|
2442
|
+
if (sameTuning(tuning, STANDARD_TUNING) && !open && fretted.length) {
|
|
2443
|
+
const lowest = Math.min(...fretted);
|
|
2444
|
+
for (const letter of ["E", "A", "C", "G", "D"]) {
|
|
2445
|
+
if (sounding[0] !== CAGED_ROOT_STRING[letter])
|
|
2446
|
+
continue;
|
|
2447
|
+
const target = targets.get(letter);
|
|
2448
|
+
if (!target)
|
|
2449
|
+
continue;
|
|
2450
|
+
const base = ((info.root - chromaOf(letter)) % 12 + 12) % 12;
|
|
2451
|
+
for (const shift of [base, base + 12]) {
|
|
2452
|
+
if (shift === 0 || lowest < shift)
|
|
2453
|
+
continue;
|
|
2454
|
+
const moved = frets.map((f) => f === null ? null : f - shift);
|
|
2455
|
+
if (moved.some((f) => f !== null && f > 4))
|
|
2456
|
+
continue;
|
|
2457
|
+
if (!sounding.every((s) => target.has((tuning[s] + moved[s]) % 12)))
|
|
2458
|
+
continue;
|
|
2459
|
+
if (letter === "E" || letter === "A")
|
|
2460
|
+
return barre ? `${letter}-shape barre` : `CAGED ${letter}`;
|
|
2461
|
+
return `CAGED ${letter}`;
|
|
2462
|
+
}
|
|
2463
|
+
}
|
|
2464
|
+
}
|
|
2465
|
+
return barre ? "Other barre" : null;
|
|
2466
|
+
}
|
|
2467
|
+
function voicingShape(frets, symbol, tuning = STANDARD_TUNING, fingers = fingerVoicing(frets)) {
|
|
2468
|
+
const info = chordInfo(symbol);
|
|
2469
|
+
return info ? shapeOf(frets, info, cagedTargets(info), tuning, fingers) : null;
|
|
2470
|
+
}
|
|
2471
|
+
var KNOWN_SHAPES = new Set([
|
|
2472
|
+
"CAGED C",
|
|
2473
|
+
"CAGED A",
|
|
2474
|
+
"CAGED G",
|
|
2475
|
+
"CAGED E",
|
|
2476
|
+
"CAGED D",
|
|
2477
|
+
"Power chord"
|
|
2478
|
+
]);
|
|
2479
|
+
var indexOf = (number, count) => count - number;
|
|
2480
|
+
var bits = (n) => {
|
|
2481
|
+
let count = 0;
|
|
2482
|
+
for (let v = n;v; v &= v - 1)
|
|
2483
|
+
count++;
|
|
2484
|
+
return count;
|
|
2485
|
+
};
|
|
2486
|
+
function generateVoicings(symbol, options = {}) {
|
|
2487
|
+
const tuning = options.tuning ?? STANDARD_TUNING;
|
|
2488
|
+
const count = tuning.length;
|
|
2489
|
+
if (count < 2 || count > MAX_GENERATED_STRINGS)
|
|
2490
|
+
return [];
|
|
2491
|
+
const info = chordInfo(symbol);
|
|
2492
|
+
if (!info)
|
|
2493
|
+
return [];
|
|
2494
|
+
const { tones, members, bass } = info;
|
|
2495
|
+
const targets = cagedTargets(info);
|
|
2496
|
+
const standard = sameTuning(tuning, STANDARD_TUNING);
|
|
2497
|
+
const allowedChromas = new Set(tones.map((t) => t.chroma));
|
|
2498
|
+
const [lo, hi] = options.area === "open" ? [1, 4] : options.area ?? [1, options.maxFret ?? 15];
|
|
2499
|
+
const maxFret = Math.min(24, Math.max(1, options.maxFret ?? 15, hi));
|
|
2500
|
+
const maxSpan = Math.min(MAX_SPAN, Math.max(1, options.maxSpan ?? 4));
|
|
2501
|
+
const allowOpen = options.allowOpen ?? true;
|
|
2502
|
+
const allowed = new Set((options.strings ?? Array.from({ length: count }, (_, i) => i + 1)).filter((n) => n >= 1 && n <= count).map((n) => indexOf(n, count)));
|
|
2503
|
+
const minStrings = Math.min(allowed.size, options.minStrings ?? (members.length === 2 ? 2 : 4));
|
|
2504
|
+
const ascending = tuning.every((p, i) => i === 0 || p > tuning[i - 1]);
|
|
2505
|
+
const inversion = options.inversion ?? (ascending ? 0 : "any");
|
|
2506
|
+
const power = members.length === 2 && tones.every((t) => t.number === 1 || t.number === 5);
|
|
2507
|
+
const wantedBass = bass !== null ? bass : inversion === "any" ? null : members[inversion]?.chroma ?? -1;
|
|
2508
|
+
if (wantedBass === -1)
|
|
2509
|
+
return [];
|
|
2510
|
+
const required = tones.reduce((mask, t) => t.required ? mask | 1 << t.chroma : mask, 0);
|
|
2511
|
+
const allowedAfter = tuning.map((_, s) => [...allowed].filter((a) => a > s).length);
|
|
2512
|
+
const choices = tuning.map((open, s) => {
|
|
2513
|
+
const list = [null];
|
|
2514
|
+
if (!allowed.has(s))
|
|
2515
|
+
return list;
|
|
2516
|
+
if (allowOpen && allowedChromas.has(open % 12))
|
|
2517
|
+
list.push(0);
|
|
2518
|
+
for (let f = Math.max(1, lo);f <= Math.min(hi, maxFret); f++) {
|
|
2519
|
+
if (allowedChromas.has((open + f) % 12))
|
|
2520
|
+
list.push(f);
|
|
2521
|
+
}
|
|
2522
|
+
return list;
|
|
2523
|
+
});
|
|
2524
|
+
const evaluate = (candidate) => {
|
|
2525
|
+
const sounding = candidate.flatMap((f, s) => f === null ? [] : [s]);
|
|
2526
|
+
const first = sounding[0];
|
|
2527
|
+
const last = sounding[sounding.length - 1];
|
|
2528
|
+
const inner = last - first + 1 - sounding.length;
|
|
2529
|
+
const pitches = sounding.map((s) => tuning[s] + candidate[s]);
|
|
2530
|
+
const chromas = new Set(pitches.map((p) => p % 12));
|
|
2531
|
+
if (sounding.length <= 3 && chromas.size < Math.min(sounding.length, tones.length)) {
|
|
2532
|
+
return null;
|
|
2533
|
+
}
|
|
2534
|
+
const low = Math.min(...pitches);
|
|
2535
|
+
const bassAt = sounding[pitches.indexOf(low)];
|
|
2536
|
+
if (wantedBass !== null && low % 12 !== wantedBass)
|
|
2537
|
+
return null;
|
|
2538
|
+
const bassString = count - bassAt;
|
|
2539
|
+
if (options.bassString !== undefined && bassString !== options.bassString)
|
|
2540
|
+
return null;
|
|
2541
|
+
const fingering = bestFingering(candidate);
|
|
2542
|
+
if (!fingering)
|
|
2543
|
+
return null;
|
|
2544
|
+
const { fingers } = fingering;
|
|
2545
|
+
const shape = shapeOf(candidate, info, targets, tuning, fingers);
|
|
2546
|
+
const fretted = sounding.map((s) => candidate[s]).filter((f) => f > 0);
|
|
2547
|
+
const span = fretted.length ? Math.max(...fretted) - Math.min(...fretted) + 1 : 0;
|
|
2548
|
+
const used = new Set(fingers.filter(Boolean)).size;
|
|
2549
|
+
const barre = hasBarre(candidate, fingers);
|
|
2550
|
+
const base = baseFretOf(candidate);
|
|
2551
|
+
const reasons = [];
|
|
2552
|
+
const usual = standard && OPEN_CHORDS.has(compact(candidate));
|
|
2553
|
+
let difficulty = used + Math.max(0, span - 1) + base * 0.15;
|
|
2554
|
+
if (barre) {
|
|
2555
|
+
difficulty += shape === "E-shape barre" || shape === "A-shape barre" || !fingering.fullBarre ? 1 : 3;
|
|
2556
|
+
reasons.push("barre");
|
|
2557
|
+
}
|
|
2558
|
+
const notes = sounding.filter((s) => candidate[s] > 0);
|
|
2559
|
+
const reach = notes.some((a) => notes.some((b) => b - a >= 2 && Math.abs(candidate[a] - candidate[b]) >= 3));
|
|
2560
|
+
if (reach) {
|
|
2561
|
+
difficulty += 1.5;
|
|
2562
|
+
reasons.push("reach");
|
|
2563
|
+
}
|
|
2564
|
+
if (fingering.awkward) {
|
|
2565
|
+
difficulty += 1.5;
|
|
2566
|
+
reasons.push("awkward fingering");
|
|
2567
|
+
}
|
|
2568
|
+
if (span >= 5) {
|
|
2569
|
+
difficulty += 2;
|
|
2570
|
+
reasons.push("stretch");
|
|
2571
|
+
}
|
|
2572
|
+
if (inner > 0) {
|
|
2573
|
+
difficulty += 2 * inner;
|
|
2574
|
+
reasons.push("muted inner string");
|
|
2575
|
+
}
|
|
2576
|
+
if (!power) {
|
|
2577
|
+
const strings = [...allowed];
|
|
2578
|
+
difficulty += strings.filter((s) => s < first).length;
|
|
2579
|
+
difficulty += 1.5 * strings.filter((s) => s > last).length;
|
|
2580
|
+
}
|
|
2581
|
+
if (sounding.length < 4 && !power)
|
|
2582
|
+
reasons.push("thin");
|
|
2583
|
+
const open = sounding.filter((s) => candidate[s] === 0);
|
|
2584
|
+
if (open.length && fretted.length && !usual) {
|
|
2585
|
+
if (Math.min(...fretted) > 4) {
|
|
2586
|
+
difficulty += 0.5 * (Math.min(...fretted) - 3);
|
|
2587
|
+
reasons.push("open strings far from the frets");
|
|
2588
|
+
}
|
|
2589
|
+
const inside = open.filter((s) => notes.some((n) => n < s) && notes.some((n) => n > s)).length;
|
|
2590
|
+
if (inside) {
|
|
2591
|
+
difficulty += inside;
|
|
2592
|
+
reasons.push("open string inside the shape");
|
|
2593
|
+
}
|
|
2594
|
+
}
|
|
2595
|
+
const fifth = tones.find((t) => t.number === 5);
|
|
2596
|
+
if (fifth && !chromas.has(fifth.chroma)) {
|
|
2597
|
+
difficulty += 0.5;
|
|
2598
|
+
reasons.push("no fifth");
|
|
2599
|
+
}
|
|
2600
|
+
if (usual)
|
|
2601
|
+
difficulty -= 3;
|
|
2602
|
+
else if (shape === "E-shape barre" || shape === "A-shape barre")
|
|
2603
|
+
difficulty -= 2;
|
|
2604
|
+
else if (shape && KNOWN_SHAPES.has(shape))
|
|
2605
|
+
difficulty -= 1.5;
|
|
2606
|
+
const lowChroma = low % 12;
|
|
2607
|
+
const inBass = members.findIndex((m) => m.chroma === lowChroma);
|
|
2608
|
+
return {
|
|
2609
|
+
frets: [...candidate],
|
|
2610
|
+
fingers,
|
|
2611
|
+
baseFret: base,
|
|
2612
|
+
bassString,
|
|
2613
|
+
inversion: inBass < 0 ? null : inBass,
|
|
2614
|
+
shape,
|
|
2615
|
+
difficulty: Math.max(0, Math.round(difficulty * 10) / 10),
|
|
2616
|
+
reasons
|
|
2617
|
+
};
|
|
2618
|
+
};
|
|
2619
|
+
const results = new Map;
|
|
2620
|
+
const frets = new Array(count).fill(null);
|
|
2621
|
+
const visit = (s, min, max, have, sounded, closed) => {
|
|
2622
|
+
if (s === count) {
|
|
2623
|
+
if (sounded < minStrings || (required & ~have) !== 0)
|
|
2624
|
+
return;
|
|
2625
|
+
const voicing = evaluate(frets);
|
|
2626
|
+
if (voicing)
|
|
2627
|
+
results.set(frets.join(","), voicing);
|
|
2628
|
+
return;
|
|
2629
|
+
}
|
|
2630
|
+
const left = closed ? 0 : (allowed.has(s) ? 1 : 0) + allowedAfter[s];
|
|
2631
|
+
if (bits(required & ~have) > left || sounded + left < minStrings)
|
|
2632
|
+
return;
|
|
2633
|
+
for (const fret of choices[s]) {
|
|
2634
|
+
if (fret === null) {
|
|
2635
|
+
frets[s] = null;
|
|
2636
|
+
visit(s + 1, min, max, have, sounded, closed || sounded > 0 && !options.gaps);
|
|
2637
|
+
continue;
|
|
2638
|
+
}
|
|
2639
|
+
if (closed)
|
|
2640
|
+
continue;
|
|
2641
|
+
const fretted = fret > 0;
|
|
2642
|
+
const nextMin = fretted ? Math.min(min, fret) : min;
|
|
2643
|
+
const nextMax = fretted ? Math.max(max, fret) : max;
|
|
2644
|
+
if (fretted && nextMax - nextMin + 1 > maxSpan)
|
|
2645
|
+
continue;
|
|
2646
|
+
frets[s] = fret;
|
|
2647
|
+
visit(s + 1, nextMin, nextMax, have | 1 << (tuning[s] + fret) % 12, sounded + 1, false);
|
|
2648
|
+
}
|
|
2649
|
+
frets[s] = null;
|
|
2650
|
+
};
|
|
2651
|
+
visit(0, Infinity, -Infinity, 0, 0, false);
|
|
2652
|
+
const sounding = (v) => v.frets.filter((f) => f !== null).length;
|
|
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(",")));
|
|
2654
|
+
}
|
|
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
|
+
|
|
1800
2861
|
// src/render/render-block.ts
|
|
1801
2862
|
var LANGUAGES = Object.keys(RENDERERS);
|
|
1802
2863
|
var isNotationLanguage = (language) => Object.prototype.hasOwnProperty.call(RENDERERS, language);
|
|
@@ -1823,7 +2884,7 @@ function renderBlock(language, source, host, given = {}) {
|
|
|
1823
2884
|
}
|
|
1824
2885
|
let parts = null;
|
|
1825
2886
|
try {
|
|
1826
|
-
parts = isDiagramLanguage(language) ? splitParts(text) ?? modeParts(language, text) ?? positionParts(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;
|
|
1827
2888
|
} catch (e) {
|
|
1828
2889
|
return failed(el, host, language, messageOf(e));
|
|
1829
2890
|
}
|
|
@@ -1842,7 +2903,16 @@ function renderBlock(language, source, host, given = {}) {
|
|
|
1842
2903
|
return failed(el, host, language, messageOf(e));
|
|
1843
2904
|
}
|
|
1844
2905
|
host.appendChild(el);
|
|
1845
|
-
|
|
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
|
+
};
|
|
1846
2916
|
}
|
|
1847
2917
|
function failed(el, host, language, error) {
|
|
1848
2918
|
el.replaceChildren();
|
|
@@ -1912,6 +2982,401 @@ function positionParts(language, source) {
|
|
|
1912
2982
|
return [...header.length ? [header.join(`
|
|
1913
2983
|
`)] : [], ...diagrams];
|
|
1914
2984
|
}
|
|
2985
|
+
var VOICING_FILTERS = [
|
|
2986
|
+
"voicings",
|
|
2987
|
+
"area",
|
|
2988
|
+
"root-string",
|
|
2989
|
+
"strings",
|
|
2990
|
+
"inversion",
|
|
2991
|
+
"stretch",
|
|
2992
|
+
"open",
|
|
2993
|
+
"gaps"
|
|
2994
|
+
];
|
|
2995
|
+
var yes = (text, key) => {
|
|
2996
|
+
if (text === undefined || text === "")
|
|
2997
|
+
return;
|
|
2998
|
+
const value = text.trim().toLowerCase();
|
|
2999
|
+
if (["yes", "true", "on"].includes(value))
|
|
3000
|
+
return true;
|
|
3001
|
+
if (["no", "false", "off"].includes(value))
|
|
3002
|
+
return false;
|
|
3003
|
+
throw new Error(`${key} must be yes or no, got "${text}"`);
|
|
3004
|
+
};
|
|
3005
|
+
var voicingCache = new Map;
|
|
3006
|
+
function cachedVoicings(symbol, options) {
|
|
3007
|
+
const key = JSON.stringify([symbol, options]);
|
|
3008
|
+
const hit = voicingCache.get(key);
|
|
3009
|
+
if (hit)
|
|
3010
|
+
return hit;
|
|
3011
|
+
const list = generateVoicings(symbol, options);
|
|
3012
|
+
voicingCache.set(key, list);
|
|
3013
|
+
if (voicingCache.size > 32)
|
|
3014
|
+
voicingCache.delete(voicingCache.keys().next().value);
|
|
3015
|
+
return list;
|
|
3016
|
+
}
|
|
3017
|
+
var NOT_WITH_VOICINGS = [
|
|
3018
|
+
"frets",
|
|
3019
|
+
"fingers",
|
|
3020
|
+
"voicing",
|
|
3021
|
+
"position",
|
|
3022
|
+
"base",
|
|
3023
|
+
"pitches",
|
|
3024
|
+
"notes",
|
|
3025
|
+
"fingering"
|
|
3026
|
+
];
|
|
3027
|
+
function voicingParts(language, source) {
|
|
3028
|
+
if (language !== "chord")
|
|
3029
|
+
return null;
|
|
3030
|
+
let block;
|
|
3031
|
+
try {
|
|
3032
|
+
block = filled(parseBlockSource(source));
|
|
3033
|
+
} catch {
|
|
3034
|
+
return null;
|
|
3035
|
+
}
|
|
3036
|
+
const value = block.fields.get("voicings")?.trim();
|
|
3037
|
+
if (value === undefined)
|
|
3038
|
+
return null;
|
|
3039
|
+
const fields = block.fields;
|
|
3040
|
+
if (fields.has("triads"))
|
|
3041
|
+
throw new Error("Use triads or voicings, not both");
|
|
3042
|
+
const instrument = fields.get("instrument")?.toLowerCase();
|
|
3043
|
+
if (instrument === "piano" || instrument === "keyboard" || fields.has("pitches") || fields.has("notes")) {
|
|
3044
|
+
throw new Error("voicings are for guitar and other fretted instruments");
|
|
3045
|
+
}
|
|
3046
|
+
const refused = NOT_WITH_VOICINGS.find((key) => fields.has(key));
|
|
3047
|
+
if (refused)
|
|
3048
|
+
throw new Error(`voicings finds the frets itself: remove "${refused}"`);
|
|
3049
|
+
const frettish = block.head?.split(/\s+/).find((t, i) => i > 0 && parseFrets(t) !== null);
|
|
3050
|
+
if (frettish)
|
|
3051
|
+
throw new Error(`voicings finds the frets itself: remove "${frettish}"`);
|
|
3052
|
+
const written = fields.get("symbol") ?? fields.get("chord") ?? block.head ?? fields.get("name");
|
|
3053
|
+
if (!written)
|
|
3054
|
+
throw new Error("voicings needs a chord: e.g. A Major");
|
|
3055
|
+
const chord = readChord(written);
|
|
3056
|
+
if (!chord)
|
|
3057
|
+
throw new Error(`Didn't recognize "${written}": try ${suggestChords(written)}`);
|
|
3058
|
+
const tuningText = fields.get("tuning");
|
|
3059
|
+
if (instrument && instrument !== "guitar" && !tuningText) {
|
|
3060
|
+
throw new Error(`Unknown instrument "${instrument}": use guitar, or give a tuning`);
|
|
3061
|
+
}
|
|
3062
|
+
const tuning = tuningText ? parseTuning(tuningText) : null;
|
|
3063
|
+
if (tuningText && !tuning)
|
|
3064
|
+
throw new Error(`Unknown tuning "${tuningText}"`);
|
|
3065
|
+
const count = tuning?.length ?? 6;
|
|
3066
|
+
if (count > MAX_GENERATED_STRINGS) {
|
|
3067
|
+
throw new Error(`voicings works on up to ${MAX_GENERATED_STRINGS} strings`);
|
|
3068
|
+
}
|
|
3069
|
+
const all = value.toLowerCase() === "all";
|
|
3070
|
+
const wanted = all ? MAX_DIAGRAMS : Number(value);
|
|
3071
|
+
if (!Number.isInteger(wanted) || wanted < 1 || wanted > MAX_DIAGRAMS) {
|
|
3072
|
+
throw new Error(`voicings: use all, or a number from 1 to ${MAX_DIAGRAMS}`);
|
|
3073
|
+
}
|
|
3074
|
+
const options = {};
|
|
3075
|
+
if (tuning)
|
|
3076
|
+
options.tuning = tuning;
|
|
3077
|
+
const area = fields.get("area");
|
|
3078
|
+
if (area) {
|
|
3079
|
+
const range = area.trim().toLowerCase() === "open" ? "open" : parseRange(area);
|
|
3080
|
+
if (!range || range !== "open" && range[1] > 24) {
|
|
3081
|
+
throw new Error(`area: use open, or frets like 5-9 (up to 24), got "${area}"`);
|
|
3082
|
+
}
|
|
3083
|
+
options.area = range === "open" ? "open" : [Math.max(1, range[0]), Math.max(1, range[1])];
|
|
3084
|
+
}
|
|
3085
|
+
const rootString = fields.get("root-string");
|
|
3086
|
+
if (rootString) {
|
|
3087
|
+
const n = Number(rootString);
|
|
3088
|
+
if (!Number.isInteger(n) || n < 1 || n > count) {
|
|
3089
|
+
throw new Error(`root-string: use a string number from 1 (the highest) to ${count}`);
|
|
3090
|
+
}
|
|
3091
|
+
options.bassString = n;
|
|
3092
|
+
}
|
|
3093
|
+
const strings = fields.get("strings");
|
|
3094
|
+
if (strings)
|
|
3095
|
+
options.strings = parseStrings(strings, count);
|
|
3096
|
+
const inversion = fields.get("inversion")?.trim().toLowerCase();
|
|
3097
|
+
if (inversion) {
|
|
3098
|
+
const n = inversion === "root" ? 0 : Number(inversion);
|
|
3099
|
+
if (inversion === "any")
|
|
3100
|
+
options.inversion = "any";
|
|
3101
|
+
else if (Number.isInteger(n) && n >= 0 && n <= 3)
|
|
3102
|
+
options.inversion = n;
|
|
3103
|
+
else
|
|
3104
|
+
throw new Error(`inversion: use 0 (root), 1, 2, 3 or any, got "${inversion}"`);
|
|
3105
|
+
}
|
|
3106
|
+
if (yes(fields.get("stretch"), "stretch"))
|
|
3107
|
+
options.maxSpan = 5;
|
|
3108
|
+
const open = yes(fields.get("open"), "open");
|
|
3109
|
+
if (open === false)
|
|
3110
|
+
options.allowOpen = false;
|
|
3111
|
+
if (yes(fields.get("gaps"), "gaps"))
|
|
3112
|
+
options.gaps = true;
|
|
3113
|
+
if (options.strings)
|
|
3114
|
+
options.minStrings = Math.min(options.strings.length, 4);
|
|
3115
|
+
if (count > 6) {
|
|
3116
|
+
if (options.inversion === "any" && (options.strings?.length ?? count) > 6) {
|
|
3117
|
+
throw new Error(`On ${count} strings, inversion: any needs six strings or fewer: add e.g. strings: 1-6`);
|
|
3118
|
+
}
|
|
3119
|
+
const wide = [
|
|
3120
|
+
options.gaps ? "gaps" : null,
|
|
3121
|
+
options.maxSpan ? "stretch" : null,
|
|
3122
|
+
Array.isArray(options.area) && options.area[1] - options.area[0] > 11 ? "a wide area" : null
|
|
3123
|
+
].filter((w) => w !== null);
|
|
3124
|
+
if (wide.length > 1) {
|
|
3125
|
+
throw new Error(`On ${count} strings, use one of ${wide.join(", ")} at a time`);
|
|
3126
|
+
}
|
|
3127
|
+
}
|
|
3128
|
+
const voicings = cachedVoicings(chord.symbol, options).slice(0, wanted);
|
|
3129
|
+
if (!voicings.length)
|
|
3130
|
+
throw new Error(noVoicing(written, chord.symbol, options));
|
|
3131
|
+
const shared = SHARED_KEYS.chord;
|
|
3132
|
+
const others = [...fields].filter(([key]) => !VOICING_FILTERS.includes(key) && !["symbol", "chord", "name", "title"].includes(key));
|
|
3133
|
+
const header = others.filter(([key]) => key === "columns" || shared.includes(key)).map(([key, v]) => `${key}: ${v}`);
|
|
3134
|
+
const own = others.filter(([key]) => key !== "columns" && !shared.includes(key)).map(([key, v]) => `${key}: ${v}`);
|
|
3135
|
+
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("");
|
|
3136
|
+
const title = fields.get("title") ?? written;
|
|
3137
|
+
const diagrams = voicings.map((v) => [
|
|
3138
|
+
`symbol: ${chord.symbol}`,
|
|
3139
|
+
`title: ${title}${v.shape ? `, ${v.shape}` : ""}`,
|
|
3140
|
+
`frets: ${frets(v.frets)}`,
|
|
3141
|
+
`fingers: ${v.fingers.map((f) => f ?? "x").join("")}`,
|
|
3142
|
+
...own
|
|
3143
|
+
].join(`
|
|
3144
|
+
`));
|
|
3145
|
+
return [...header.length ? [header.join(`
|
|
3146
|
+
`)] : [], ...diagrams];
|
|
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
|
+
}
|
|
3362
|
+
function noVoicing(written, symbol, options) {
|
|
3363
|
+
const strings = options.strings;
|
|
3364
|
+
const contiguous = strings ? [...strings].sort((a, b) => a - b).every((n, i, all) => i === 0 || n === all[i - 1] + 1) : true;
|
|
3365
|
+
if (strings && !contiguous && !options.gaps) {
|
|
3366
|
+
return `strings ${strings.join(" ")} aren't next to each other: add gaps: yes`;
|
|
3367
|
+
}
|
|
3368
|
+
if (strings && options.bassString !== undefined && !strings.includes(options.bassString)) {
|
|
3369
|
+
return `root-string ${options.bassString} isn't among strings ${strings.join(" ")}`;
|
|
3370
|
+
}
|
|
3371
|
+
const notes = chordNoteSet(symbol)?.members.filter((m) => m.interval !== null).length ?? 0;
|
|
3372
|
+
if (typeof options.inversion === "number" && options.inversion >= notes) {
|
|
3373
|
+
return `${symbol} has ${notes} notes: inversion goes up to ${notes - 1}`;
|
|
3374
|
+
}
|
|
3375
|
+
if (options.bassString !== undefined) {
|
|
3376
|
+
return `No voicing of ${written} has its bass on string ${options.bassString}: try another root-string`;
|
|
3377
|
+
}
|
|
3378
|
+
return `No playable voicing of ${written} fits: widen the area, allow open strings or add stretch: yes`;
|
|
3379
|
+
}
|
|
1915
3380
|
function modeParts(language, source) {
|
|
1916
3381
|
if (language === "chord")
|
|
1917
3382
|
return null;
|
|
@@ -2010,7 +3475,14 @@ function renderRow(doc, language, parts, el, options) {
|
|
|
2010
3475
|
if (block.fields.has("columns")) {
|
|
2011
3476
|
throw new Error('"columns" goes in the shared settings, before the first "---"');
|
|
2012
3477
|
}
|
|
2013
|
-
const directive = [
|
|
3478
|
+
const directive = [
|
|
3479
|
+
"positions",
|
|
3480
|
+
"modes",
|
|
3481
|
+
"voicings",
|
|
3482
|
+
"triads",
|
|
3483
|
+
"from",
|
|
3484
|
+
"progression"
|
|
3485
|
+
].find((key) => block.fields.has(key));
|
|
2014
3486
|
if (directive) {
|
|
2015
3487
|
throw new Error(`"${directive}" draws its own row: use it in a block of its own`);
|
|
2016
3488
|
}
|
|
@@ -2039,6 +3511,7 @@ var styles_default = `/* Music notation. Colors follow Obsidian's theme variable
|
|
|
2039
3511
|
--mn-muted: var(--text-muted, #8a8a8a);
|
|
2040
3512
|
--mn-faint: var(--background-modifier-border, #d0d0d0);
|
|
2041
3513
|
--mn-accent: var(--interactive-accent, #7c3aed);
|
|
3514
|
+
--mn-bg: var(--background-primary, #fff);
|
|
2042
3515
|
--mn-on-accent: var(--text-on-accent, #fff);
|
|
2043
3516
|
/* Dark enough for white labels in light and dark themes. */
|
|
2044
3517
|
--mn-root: #c2410c;
|
|
@@ -2099,6 +3572,221 @@ var styles_default = `/* Music notation. Colors follow Obsidian's theme variable
|
|
|
2099
3572
|
stroke-width: 2;
|
|
2100
3573
|
}
|
|
2101
3574
|
|
|
3575
|
+
/* Playback: a compact strip at the bottom of the diagram, shown on hover, keyboard focus or a
|
|
3576
|
+
tap. Host styles (Obsidian's buttons, selects and sliders) are reset inside it. */
|
|
3577
|
+
.mn-playable {
|
|
3578
|
+
position: relative;
|
|
3579
|
+
display: inline-block;
|
|
3580
|
+
max-width: 100%;
|
|
3581
|
+
}
|
|
3582
|
+
|
|
3583
|
+
.mn-play {
|
|
3584
|
+
position: absolute;
|
|
3585
|
+
left: 2px;
|
|
3586
|
+
right: 2px;
|
|
3587
|
+
bottom: 2px;
|
|
3588
|
+
display: flex;
|
|
3589
|
+
flex-wrap: wrap;
|
|
3590
|
+
align-items: center;
|
|
3591
|
+
justify-content: center;
|
|
3592
|
+
gap: 2px 6px;
|
|
3593
|
+
padding: 2px 4px;
|
|
3594
|
+
font-size: 10px;
|
|
3595
|
+
line-height: 1;
|
|
3596
|
+
color: var(--mn-fg);
|
|
3597
|
+
background: color-mix(in srgb, var(--mn-bg) 85%, transparent);
|
|
3598
|
+
border: 1px solid var(--mn-faint);
|
|
3599
|
+
border-radius: 6px;
|
|
3600
|
+
opacity: 0;
|
|
3601
|
+
pointer-events: none;
|
|
3602
|
+
transition: opacity 120ms ease;
|
|
3603
|
+
}
|
|
3604
|
+
|
|
3605
|
+
.mn-playable:hover .mn-play,
|
|
3606
|
+
.mn-playable:focus-within .mn-play,
|
|
3607
|
+
.mn-playable.mn-play-open .mn-play {
|
|
3608
|
+
opacity: 1;
|
|
3609
|
+
pointer-events: auto;
|
|
3610
|
+
}
|
|
3611
|
+
|
|
3612
|
+
.mn-play button,
|
|
3613
|
+
.mn-play select {
|
|
3614
|
+
all: unset;
|
|
3615
|
+
box-sizing: border-box;
|
|
3616
|
+
display: inline-flex;
|
|
3617
|
+
align-items: center;
|
|
3618
|
+
justify-content: center;
|
|
3619
|
+
height: 18px;
|
|
3620
|
+
min-width: 18px;
|
|
3621
|
+
padding: 0 3px;
|
|
3622
|
+
font-size: 10px;
|
|
3623
|
+
color: inherit;
|
|
3624
|
+
border-radius: 4px;
|
|
3625
|
+
cursor: pointer;
|
|
3626
|
+
}
|
|
3627
|
+
|
|
3628
|
+
.mn-play button:hover,
|
|
3629
|
+
.mn-play select:hover,
|
|
3630
|
+
.mn-play button:focus-visible,
|
|
3631
|
+
.mn-play select:focus-visible {
|
|
3632
|
+
background: color-mix(in srgb, var(--mn-fg) 12%, transparent);
|
|
3633
|
+
}
|
|
3634
|
+
|
|
3635
|
+
.mn-play button.mn-on {
|
|
3636
|
+
background: var(--mn-accent);
|
|
3637
|
+
color: var(--mn-on-accent);
|
|
3638
|
+
}
|
|
3639
|
+
|
|
3640
|
+
.mn-play-icon {
|
|
3641
|
+
fill: currentColor;
|
|
3642
|
+
stroke: none;
|
|
3643
|
+
}
|
|
3644
|
+
|
|
3645
|
+
.mn-play-icon-loop,
|
|
3646
|
+
.mn-play-icon-volume {
|
|
3647
|
+
fill: none;
|
|
3648
|
+
stroke: currentColor;
|
|
3649
|
+
stroke-width: 1.6;
|
|
3650
|
+
stroke-linecap: round;
|
|
3651
|
+
stroke-linejoin: round;
|
|
3652
|
+
}
|
|
3653
|
+
|
|
3654
|
+
.mn-play-field {
|
|
3655
|
+
display: inline-flex;
|
|
3656
|
+
align-items: center;
|
|
3657
|
+
gap: 2px;
|
|
3658
|
+
}
|
|
3659
|
+
|
|
3660
|
+
.mn-play-label {
|
|
3661
|
+
display: inline-flex;
|
|
3662
|
+
opacity: 0.8;
|
|
3663
|
+
}
|
|
3664
|
+
|
|
3665
|
+
.mn-play input[type='range'] {
|
|
3666
|
+
all: unset;
|
|
3667
|
+
appearance: none;
|
|
3668
|
+
width: 44px;
|
|
3669
|
+
/* The input is the track: WebKit then centers the thumb on it. */
|
|
3670
|
+
height: 3px;
|
|
3671
|
+
margin: 0;
|
|
3672
|
+
border-radius: 2px;
|
|
3673
|
+
background: var(--mn-faint);
|
|
3674
|
+
cursor: pointer;
|
|
3675
|
+
}
|
|
3676
|
+
|
|
3677
|
+
.mn-play input[type='range']::-webkit-slider-runnable-track {
|
|
3678
|
+
height: 3px;
|
|
3679
|
+
border: none;
|
|
3680
|
+
background: transparent;
|
|
3681
|
+
}
|
|
3682
|
+
|
|
3683
|
+
.mn-play input[type='range']::-webkit-slider-thumb {
|
|
3684
|
+
appearance: none;
|
|
3685
|
+
width: 9px;
|
|
3686
|
+
height: 9px;
|
|
3687
|
+
margin-top: 0;
|
|
3688
|
+
border: none;
|
|
3689
|
+
border-radius: 50%;
|
|
3690
|
+
background: var(--mn-accent);
|
|
3691
|
+
box-shadow: none;
|
|
3692
|
+
}
|
|
3693
|
+
|
|
3694
|
+
.mn-play input[type='range']::-moz-range-track {
|
|
3695
|
+
height: 3px;
|
|
3696
|
+
border: none;
|
|
3697
|
+
border-radius: 2px;
|
|
3698
|
+
background: var(--mn-faint);
|
|
3699
|
+
}
|
|
3700
|
+
|
|
3701
|
+
.mn-play input[type='range']::-moz-range-thumb {
|
|
3702
|
+
width: 9px;
|
|
3703
|
+
height: 9px;
|
|
3704
|
+
border: none;
|
|
3705
|
+
border-radius: 50%;
|
|
3706
|
+
background: var(--mn-accent);
|
|
3707
|
+
}
|
|
3708
|
+
|
|
3709
|
+
.mn-playable [data-midi] {
|
|
3710
|
+
cursor: pointer;
|
|
3711
|
+
}
|
|
3712
|
+
|
|
3713
|
+
/* A finger number or note name over a dot or key lets the click reach it. */
|
|
3714
|
+
.mn-playable .mn-svg text {
|
|
3715
|
+
pointer-events: none;
|
|
3716
|
+
}
|
|
3717
|
+
|
|
3718
|
+
/* The bar's empty space lets clicks through to the dots under it; its controls take them. */
|
|
3719
|
+
.mn-playable:hover .mn-play,
|
|
3720
|
+
.mn-playable:focus-within .mn-play,
|
|
3721
|
+
.mn-playable.mn-play-open .mn-play {
|
|
3722
|
+
pointer-events: none;
|
|
3723
|
+
}
|
|
3724
|
+
|
|
3725
|
+
.mn-playable:hover .mn-play > *,
|
|
3726
|
+
.mn-playable:focus-within .mn-play > *,
|
|
3727
|
+
.mn-playable.mn-play-open .mn-play > * {
|
|
3728
|
+
pointer-events: auto;
|
|
3729
|
+
}
|
|
3730
|
+
|
|
3731
|
+
/* Touch screens: hover sticks after a tap, so only a tap opens the bar. */
|
|
3732
|
+
@media (hover: none) {
|
|
3733
|
+
.mn-playable:hover:not(.mn-play-open) .mn-play {
|
|
3734
|
+
opacity: 0;
|
|
3735
|
+
}
|
|
3736
|
+
|
|
3737
|
+
.mn-playable:hover:not(.mn-play-open) .mn-play > * {
|
|
3738
|
+
pointer-events: none;
|
|
3739
|
+
}
|
|
3740
|
+
}
|
|
3741
|
+
|
|
3742
|
+
/* Tabs and scores: the player bar stays at the bottom of the screen while the score scrolls. */
|
|
3743
|
+
.mn-tab {
|
|
3744
|
+
position: relative;
|
|
3745
|
+
}
|
|
3746
|
+
|
|
3747
|
+
.mn-tab .mn-tab-bar {
|
|
3748
|
+
position: sticky;
|
|
3749
|
+
left: auto;
|
|
3750
|
+
right: auto;
|
|
3751
|
+
bottom: 6px;
|
|
3752
|
+
width: fit-content;
|
|
3753
|
+
max-width: 100%;
|
|
3754
|
+
margin: 4px auto 0;
|
|
3755
|
+
}
|
|
3756
|
+
|
|
3757
|
+
.mn-tab:hover .mn-tab-bar,
|
|
3758
|
+
.mn-tab:focus-within .mn-tab-bar,
|
|
3759
|
+
.mn-tab.mn-play-open .mn-tab-bar {
|
|
3760
|
+
opacity: 1;
|
|
3761
|
+
pointer-events: auto;
|
|
3762
|
+
}
|
|
3763
|
+
|
|
3764
|
+
.mn-tab-time {
|
|
3765
|
+
font-variant-numeric: tabular-nums;
|
|
3766
|
+
opacity: 0.8;
|
|
3767
|
+
}
|
|
3768
|
+
|
|
3769
|
+
.mn-tab-hidden {
|
|
3770
|
+
position: absolute;
|
|
3771
|
+
width: 1px;
|
|
3772
|
+
height: 1px;
|
|
3773
|
+
overflow: hidden;
|
|
3774
|
+
left: -9999px;
|
|
3775
|
+
}
|
|
3776
|
+
|
|
3777
|
+
/* Chord transitions: a finger that stays (anchor) gets a ring, one that slides (guide) a dashed
|
|
3778
|
+
ring, so the marks don't rest on color alone. */
|
|
3779
|
+
.mn-svg .mn-dot.mn-anchor {
|
|
3780
|
+
stroke: var(--mn-fg);
|
|
3781
|
+
stroke-width: 3;
|
|
3782
|
+
}
|
|
3783
|
+
|
|
3784
|
+
.mn-svg .mn-dot.mn-guide {
|
|
3785
|
+
stroke: var(--mn-fg);
|
|
3786
|
+
stroke-width: 2.5;
|
|
3787
|
+
stroke-dasharray: 3 2;
|
|
3788
|
+
}
|
|
3789
|
+
|
|
2102
3790
|
/* Notes that differ from a compared scale: outlined. */
|
|
2103
3791
|
.mn-svg .mn-dot.mn-diff,
|
|
2104
3792
|
.mn-svg .mn-key-white.mn-diff,
|
|
@@ -2352,32 +4040,202 @@ function injectStyles(doc) {
|
|
|
2352
4040
|
style.textContent = STYLES;
|
|
2353
4041
|
doc.head.appendChild(style);
|
|
2354
4042
|
}
|
|
4043
|
+
// src/convert/text-tab.ts
|
|
4044
|
+
var STRING_LINE = /^\s*([A-Ga-g][#b]?)?\s*[|:]?[-0-9xX|:hpbrsvt/\\~^().*<>\s]*$/;
|
|
4045
|
+
var isStringLine = (line) => STRING_LINE.test(line) && (line.match(/-/g)?.length ?? 0) >= 4 && (line.includes("|") || /^\s*[A-Ga-g][#b]?\s*[-|:]/.test(line));
|
|
4046
|
+
function systemsOf(text) {
|
|
4047
|
+
const systems = [];
|
|
4048
|
+
let current = null;
|
|
4049
|
+
for (const raw of text.split(/\r?\n/)) {
|
|
4050
|
+
if (isStringLine(raw)) {
|
|
4051
|
+
const m = /^\s*([A-Ga-g][#b]?)?\s*[|:]?/.exec(raw);
|
|
4052
|
+
current ??= { names: [], lines: [] };
|
|
4053
|
+
current.names.push(m[1] ?? null);
|
|
4054
|
+
current.lines.push(raw.slice(m[0].length));
|
|
4055
|
+
} else if (current) {
|
|
4056
|
+
if (current.lines.length >= 4)
|
|
4057
|
+
systems.push(current);
|
|
4058
|
+
current = null;
|
|
4059
|
+
}
|
|
4060
|
+
}
|
|
4061
|
+
if (current && current.lines.length >= 4)
|
|
4062
|
+
systems.push(current);
|
|
4063
|
+
return systems;
|
|
4064
|
+
}
|
|
4065
|
+
function barsOf(lines) {
|
|
4066
|
+
const width = Math.max(...lines.map((l) => l.length));
|
|
4067
|
+
const padded = lines.map((l) => l.padEnd(width, "-"));
|
|
4068
|
+
const bars = [[]];
|
|
4069
|
+
for (let col = 0;col < width; col++) {
|
|
4070
|
+
if (padded.every((l) => l[col] === "|" || l[col] === ":")) {
|
|
4071
|
+
if (bars[bars.length - 1].length)
|
|
4072
|
+
bars.push([]);
|
|
4073
|
+
continue;
|
|
4074
|
+
}
|
|
4075
|
+
const beat = [];
|
|
4076
|
+
let wide = false;
|
|
4077
|
+
padded.forEach((line, i) => {
|
|
4078
|
+
const c = line[col];
|
|
4079
|
+
if (!/\d/.test(c) || /\d/.test(line[col - 1] ?? ""))
|
|
4080
|
+
return;
|
|
4081
|
+
const two = /\d/.test(line[col + 1] ?? "") ? line.slice(col, col + 2) : c;
|
|
4082
|
+
if (two.length === 2)
|
|
4083
|
+
wide = true;
|
|
4084
|
+
beat.push({ fret: Number(two), string: i + 1 });
|
|
4085
|
+
});
|
|
4086
|
+
if (wide)
|
|
4087
|
+
padded.forEach((line, i) => {
|
|
4088
|
+
if (beat.some((n) => n.string === i + 1))
|
|
4089
|
+
return;
|
|
4090
|
+
const c = line[col + 1] ?? "";
|
|
4091
|
+
if (!/\d/.test(c) || /\d/.test(line[col] ?? ""))
|
|
4092
|
+
return;
|
|
4093
|
+
const two = /\d/.test(line[col + 2] ?? "") ? line.slice(col + 1, col + 3) : c;
|
|
4094
|
+
beat.push({ fret: Number(two), string: i + 1 });
|
|
4095
|
+
});
|
|
4096
|
+
if (beat.length)
|
|
4097
|
+
bars[bars.length - 1].push(beat);
|
|
4098
|
+
if (wide)
|
|
4099
|
+
col++;
|
|
4100
|
+
}
|
|
4101
|
+
return bars.filter((b) => b.length);
|
|
4102
|
+
}
|
|
4103
|
+
var BY_COUNT = {
|
|
4104
|
+
4: [28, 33, 38, 43],
|
|
4105
|
+
5: [23, 28, 33, 38, 43],
|
|
4106
|
+
7: [35, 40, 45, 50, 55, 59, 64]
|
|
4107
|
+
};
|
|
4108
|
+
var BASS = ["E A D G", "B E A D G"];
|
|
4109
|
+
function tuningOf2(names) {
|
|
4110
|
+
const strings = names.length;
|
|
4111
|
+
if (names.every((n) => n !== null)) {
|
|
4112
|
+
const lowFirst = [...names].reverse();
|
|
4113
|
+
if (strings === 6 && lowFirst.join(" ").toLowerCase() === "e a d g b e")
|
|
4114
|
+
return null;
|
|
4115
|
+
const tuning = parseTuning(lowFirst.join(" "));
|
|
4116
|
+
if (!tuning)
|
|
4117
|
+
return BY_COUNT[strings] ?? null;
|
|
4118
|
+
const bass = BASS.includes(lowFirst.map((n) => n.toUpperCase()).join(" "));
|
|
4119
|
+
return bass ? tuning.map((m) => m - 12) : tuning;
|
|
4120
|
+
}
|
|
4121
|
+
return strings === 6 ? null : BY_COUNT[strings] ?? null;
|
|
4122
|
+
}
|
|
4123
|
+
function textTabToAlphaTex(text) {
|
|
4124
|
+
const systems = systemsOf(text);
|
|
4125
|
+
if (!systems.length)
|
|
4126
|
+
return null;
|
|
4127
|
+
const strings = systems[0].lines.length;
|
|
4128
|
+
const names = systems[0].names;
|
|
4129
|
+
const bars = systems.flatMap((s) => s.lines.length === strings ? barsOf(s.lines) : []);
|
|
4130
|
+
if (!bars.length)
|
|
4131
|
+
return null;
|
|
4132
|
+
const header = [];
|
|
4133
|
+
const tuning = tuningOf2(names);
|
|
4134
|
+
if (tuning)
|
|
4135
|
+
header.push(`\\tuning (${[...tuning].reverse().map((m) => noteOf(m)).join(" ")})`);
|
|
4136
|
+
const beat = (notes) => notes.length === 1 ? `${notes[0].fret}.${notes[0].string}` : `(${notes.map((n) => `${n.fret}.${n.string}`).join(" ")})`;
|
|
4137
|
+
const body = bars.map((bar) => bar.map(beat).join(" ")).join(` |
|
|
4138
|
+
`);
|
|
4139
|
+
return [...header, `:8 ${body}`].join(`
|
|
4140
|
+
`);
|
|
4141
|
+
}
|
|
4142
|
+
// src/convert/chord-sheet.ts
|
|
4143
|
+
function chordsOf(line) {
|
|
4144
|
+
const found = [];
|
|
4145
|
+
const re = /\S+/g;
|
|
4146
|
+
let m;
|
|
4147
|
+
while (m = re.exec(line)) {
|
|
4148
|
+
if (/^(\|:?|:?\||:|%|\/|-|\(?\d+x\)?|\(?x\d+\)?|n\.?c\.?)$/i.test(m[0]))
|
|
4149
|
+
continue;
|
|
4150
|
+
if (!parseChord(m[0]))
|
|
4151
|
+
return null;
|
|
4152
|
+
found.push({ chord: m[0], at: m.index });
|
|
4153
|
+
}
|
|
4154
|
+
return found.length ? found : null;
|
|
4155
|
+
}
|
|
4156
|
+
function sectionOf(line) {
|
|
4157
|
+
const m = /^\s*\[?\s*((?:intro|verse|pre-?chorus|chorus|bridge|solo|outro|interlude|refrain|coda)(?:\s*\d+)?)\s*\]?\s*:?\s*$/i.exec(line);
|
|
4158
|
+
return m ? m[1].trim() : null;
|
|
4159
|
+
}
|
|
4160
|
+
var lyricsOf = (line) => line.trimEnd().replace(/\[/g, "(").replace(/\]/g, ")");
|
|
4161
|
+
function chordSheetToChordPro(text) {
|
|
4162
|
+
const lines = text.replace(/\t/g, " ").split(/\r?\n/);
|
|
4163
|
+
const out = [];
|
|
4164
|
+
for (let i = 0;i < lines.length; i++) {
|
|
4165
|
+
const line = lines[i];
|
|
4166
|
+
const section = sectionOf(line);
|
|
4167
|
+
if (section) {
|
|
4168
|
+
out.push(`{comment: ${section}}`);
|
|
4169
|
+
continue;
|
|
4170
|
+
}
|
|
4171
|
+
const labelled = /^\s*([A-Za-z][\w -]{0,20}):\s+(\S.*)$/.exec(line);
|
|
4172
|
+
const labelledChords = labelled ? chordsOf(labelled[2]) : null;
|
|
4173
|
+
if (labelled && labelledChords) {
|
|
4174
|
+
out.push(`{comment: ${labelled[1].trim()}}`);
|
|
4175
|
+
out.push(labelledChords.map((c) => `[${c.chord}]`).join(" "));
|
|
4176
|
+
continue;
|
|
4177
|
+
}
|
|
4178
|
+
const chords = chordsOf(line);
|
|
4179
|
+
if (!chords) {
|
|
4180
|
+
out.push(lyricsOf(line));
|
|
4181
|
+
continue;
|
|
4182
|
+
}
|
|
4183
|
+
const next = lines[i + 1];
|
|
4184
|
+
const lyrics = next !== undefined && next.trim() && !chordsOf(next) && !sectionOf(next);
|
|
4185
|
+
if (!lyrics) {
|
|
4186
|
+
out.push(chords.map((c) => `[${c.chord}]`).join(" "));
|
|
4187
|
+
continue;
|
|
4188
|
+
}
|
|
4189
|
+
let merged = lyricsOf(next);
|
|
4190
|
+
const width = Math.max(merged.length, chords[chords.length - 1].at);
|
|
4191
|
+
merged = merged.padEnd(width, " ");
|
|
4192
|
+
for (const c of [...chords].reverse()) {
|
|
4193
|
+
merged = `${merged.slice(0, c.at)}[${c.chord}]${merged.slice(c.at)}`;
|
|
4194
|
+
}
|
|
4195
|
+
out.push(merged.trimEnd());
|
|
4196
|
+
i++;
|
|
4197
|
+
}
|
|
4198
|
+
return out.join(`
|
|
4199
|
+
`).replace(/\n{3,}/g, `
|
|
4200
|
+
|
|
4201
|
+
`).trim();
|
|
4202
|
+
}
|
|
2355
4203
|
export {
|
|
2356
4204
|
CAGED_SHAPES,
|
|
2357
4205
|
DEFAULT_PROPERTY_NAMES,
|
|
4206
|
+
DEFAULT_SEQUENCE,
|
|
2358
4207
|
FLAT_NAMES,
|
|
2359
4208
|
LANGUAGES,
|
|
2360
4209
|
MAX_DIAGRAMS,
|
|
4210
|
+
MAX_GENERATED_STRINGS,
|
|
2361
4211
|
SHARP_NAMES,
|
|
2362
4212
|
STANDARD_TUNING,
|
|
2363
4213
|
STYLES,
|
|
4214
|
+
attachPlayback,
|
|
2364
4215
|
baseFretOf,
|
|
2365
4216
|
blockFromProperties,
|
|
2366
4217
|
characteristicIntervals,
|
|
2367
4218
|
chordNoteSet,
|
|
4219
|
+
chordSheetToChordPro,
|
|
2368
4220
|
chromaOf,
|
|
2369
4221
|
detectChordFromFrets,
|
|
2370
4222
|
detectChordFromPitches,
|
|
4223
|
+
fingerVoicing,
|
|
4224
|
+
generateVoicings,
|
|
2371
4225
|
injectStyles,
|
|
2372
4226
|
instrumentOf,
|
|
2373
4227
|
intervalLabel,
|
|
4228
|
+
inversionName,
|
|
2374
4229
|
isFlat,
|
|
2375
4230
|
isNotationLanguage,
|
|
2376
4231
|
isScaleWithoutRoot,
|
|
2377
4232
|
keyPrefersFlats,
|
|
2378
4233
|
keyRangeOf,
|
|
4234
|
+
keyTriads,
|
|
2379
4235
|
labelOf,
|
|
2380
4236
|
layoutDefaults,
|
|
4237
|
+
lengthOf,
|
|
4238
|
+
lightPlayer,
|
|
2381
4239
|
listNoteSet,
|
|
2382
4240
|
memberOf,
|
|
2383
4241
|
midiOf,
|
|
@@ -2390,10 +4248,13 @@ export {
|
|
|
2390
4248
|
parseFingers,
|
|
2391
4249
|
parseFrets,
|
|
2392
4250
|
parseHand,
|
|
4251
|
+
parseKey,
|
|
2393
4252
|
parseKeyRange,
|
|
2394
4253
|
parseLyrics,
|
|
2395
4254
|
parseOrientation,
|
|
2396
4255
|
parseTuning,
|
|
4256
|
+
pianoTone,
|
|
4257
|
+
pluckedString,
|
|
2397
4258
|
positionCount,
|
|
2398
4259
|
readChord,
|
|
2399
4260
|
renderBlock,
|
|
@@ -2405,10 +4266,16 @@ export {
|
|
|
2405
4266
|
scaleNoteSet,
|
|
2406
4267
|
scalePosition,
|
|
2407
4268
|
scaleTypes,
|
|
4269
|
+
sequenceOf,
|
|
4270
|
+
smoothProgression,
|
|
2408
4271
|
suggestChords,
|
|
4272
|
+
textTabToAlphaTex,
|
|
4273
|
+
transition,
|
|
2409
4274
|
transposeChord,
|
|
2410
|
-
transposeKey
|
|
4275
|
+
transposeKey,
|
|
4276
|
+
triadVoicings,
|
|
4277
|
+
voicingShape
|
|
2411
4278
|
};
|
|
2412
4279
|
|
|
2413
|
-
//# debugId=
|
|
4280
|
+
//# debugId=38B8A991D9E8CE0564756E2164756E21
|
|
2414
4281
|
//# sourceMappingURL=index.js.map
|