@dsebastien/music-notation 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +20 -2
- package/dist/blocks/block-source.d.ts +5 -0
- package/dist/blocks/renderers.d.ts +3 -0
- package/dist/chord/guitar.d.ts +4 -0
- package/dist/chord/transitions.d.ts +46 -0
- package/dist/chord/triads.d.ts +43 -0
- package/dist/convert/chord-sheet.d.ts +2 -0
- package/dist/convert/text-tab.d.ts +5 -0
- package/dist/fretboard/fretboard.d.ts +2 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +1269 -24
- package/dist/index.js.map +16 -10
- package/dist/keyboard/keyboard.d.ts +2 -0
- package/dist/music-notation.css +216 -0
- package/dist/music-notation.publish.min.js +239 -13
- package/dist/music-notation.tab.min.js +527 -0
- package/dist/playback/controls.d.ts +21 -0
- package/dist/playback/player.d.ts +50 -0
- package/dist/publish/auto.d.ts +3 -0
- package/dist/publish/index.js +881 -24
- package/dist/publish/index.js.map +14 -10
- package/dist/tab/auto.d.ts +16 -0
- package/dist/tab/index.d.ts +97 -0
- package/dist/tab/index.js +1043 -0
- package/dist/tab/index.js.map +19 -0
- package/package.json +21 -2
|
@@ -0,0 +1,1043 @@
|
|
|
1
|
+
// src/render/dom.ts
|
|
2
|
+
var SVG_NS = "http://www.w3.org/2000/svg";
|
|
3
|
+
var apply = (el, attrs) => {
|
|
4
|
+
for (const [name, value] of Object.entries(attrs)) {
|
|
5
|
+
if (value !== undefined)
|
|
6
|
+
el.setAttribute(name, String(value));
|
|
7
|
+
}
|
|
8
|
+
};
|
|
9
|
+
function svg(doc, tag, attrs = {}, children = []) {
|
|
10
|
+
const el = doc.createElementNS(SVG_NS, tag);
|
|
11
|
+
apply(el, attrs);
|
|
12
|
+
for (const child of children)
|
|
13
|
+
if (child)
|
|
14
|
+
el.appendChild(child);
|
|
15
|
+
return el;
|
|
16
|
+
}
|
|
17
|
+
function svgText(doc, text, attrs = {}) {
|
|
18
|
+
const el = svg(doc, "text", attrs);
|
|
19
|
+
el.textContent = text;
|
|
20
|
+
return el;
|
|
21
|
+
}
|
|
22
|
+
function svgLabel(doc, text, [x, y], cls, anchor = "middle") {
|
|
23
|
+
return svgText(doc, text, { class: cls, x, y: y + 4, "text-anchor": anchor });
|
|
24
|
+
}
|
|
25
|
+
function svgLine(doc, [x1, y1], [x2, y2], cls) {
|
|
26
|
+
return svg(doc, "line", { class: cls, x1, y1, x2, y2 });
|
|
27
|
+
}
|
|
28
|
+
function html(doc, tag, cls, text) {
|
|
29
|
+
const el = doc.createElement(tag);
|
|
30
|
+
if (cls)
|
|
31
|
+
el.className = cls;
|
|
32
|
+
if (text !== undefined)
|
|
33
|
+
el.textContent = text;
|
|
34
|
+
return el;
|
|
35
|
+
}
|
|
36
|
+
function svgRoot(doc, width, height, cls, label) {
|
|
37
|
+
return svg(doc, "svg", {
|
|
38
|
+
xmlns: SVG_NS,
|
|
39
|
+
viewBox: `0 0 ${width} ${height}`,
|
|
40
|
+
width,
|
|
41
|
+
class: `mn-svg ${cls}`,
|
|
42
|
+
role: "img",
|
|
43
|
+
"aria-label": label
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// src/theory/pitch.ts
|
|
48
|
+
var SHARP_NAMES = [
|
|
49
|
+
"C",
|
|
50
|
+
"C#",
|
|
51
|
+
"D",
|
|
52
|
+
"D#",
|
|
53
|
+
"E",
|
|
54
|
+
"F",
|
|
55
|
+
"F#",
|
|
56
|
+
"G",
|
|
57
|
+
"G#",
|
|
58
|
+
"A",
|
|
59
|
+
"A#",
|
|
60
|
+
"B"
|
|
61
|
+
];
|
|
62
|
+
var FLAT_NAMES = ["C", "Db", "D", "Eb", "E", "F", "Gb", "G", "Ab", "A", "Bb", "B"];
|
|
63
|
+
var LETTER_CHROMA = { C: 0, D: 2, E: 4, F: 5, G: 7, A: 9, B: 11 };
|
|
64
|
+
function chromaOf(name) {
|
|
65
|
+
const m = /^([A-Ga-g])([#♯b♭]*)$/.exec(name.trim());
|
|
66
|
+
if (!m)
|
|
67
|
+
return null;
|
|
68
|
+
let chroma = LETTER_CHROMA[m[1].toUpperCase()];
|
|
69
|
+
for (const accidental of m[2])
|
|
70
|
+
chroma += accidental === "#" || accidental === "♯" ? 1 : -1;
|
|
71
|
+
return (chroma % 12 + 12) % 12;
|
|
72
|
+
}
|
|
73
|
+
function midiOf(note) {
|
|
74
|
+
const m = /^([A-Ga-g][#♯b♭]*)(-?\d)$/.exec(note.trim());
|
|
75
|
+
if (!m)
|
|
76
|
+
return null;
|
|
77
|
+
const chroma = chromaOf(m[1]);
|
|
78
|
+
if (chroma === null)
|
|
79
|
+
return null;
|
|
80
|
+
const letter = LETTER_CHROMA[m[1][0].toUpperCase()];
|
|
81
|
+
const shift = Math.round((chroma - letter) / 12);
|
|
82
|
+
return (Number(m[2]) + 1) * 12 + chroma - shift * 12;
|
|
83
|
+
}
|
|
84
|
+
var nameOf = (chroma, preferFlats = false) => (preferFlats ? FLAT_NAMES : SHARP_NAMES)[((Math.round(chroma) || 0) % 12 + 12) % 12];
|
|
85
|
+
function splitChordSymbol(symbol) {
|
|
86
|
+
const normalized = symbol.trim().replace(/♯/g, "#").replace(/♭/g, "b");
|
|
87
|
+
const m = /^([A-G][#b]?)(.*?)(?:\/([A-G][#b]?))?$/.exec(normalized);
|
|
88
|
+
return m ? { root: m[1], suffix: m[2], bass: m[3] ?? null } : null;
|
|
89
|
+
}
|
|
90
|
+
var noteOf = (midi, preferFlats = false) => `${nameOf(midi, preferFlats)}${Math.floor(midi / 12) - 1}`;
|
|
91
|
+
var isFlat = (name) => /^[A-Ga-g][b♭]/.test(name.trim());
|
|
92
|
+
|
|
93
|
+
// src/fretboard/tuning.ts
|
|
94
|
+
var NAMED = new Map(Object.entries({
|
|
95
|
+
standard: "E2 A2 D3 G3 B3 E4",
|
|
96
|
+
"drop d": "D2 A2 D3 G3 B3 E4",
|
|
97
|
+
dadgad: "D2 A2 D3 G3 A3 D4",
|
|
98
|
+
"open g": "D2 G2 D3 G3 B3 D4",
|
|
99
|
+
"open d": "D2 A2 D3 F#3 A3 D4",
|
|
100
|
+
"open e": "E2 B2 E3 G#3 B3 E4",
|
|
101
|
+
"half step down": "Eb2 Ab2 Db3 Gb3 Bb3 Eb4",
|
|
102
|
+
bass: "E1 A1 D2 G2",
|
|
103
|
+
ukulele: "G4 C4 E4 A4"
|
|
104
|
+
}));
|
|
105
|
+
function parseTuning(text) {
|
|
106
|
+
const trimmed = text.trim();
|
|
107
|
+
const named = NAMED.get(trimmed.toLowerCase().replace(/\s*\(.*\)$/, ""));
|
|
108
|
+
if (named)
|
|
109
|
+
return parseTuning(named);
|
|
110
|
+
const spaced = trimmed.split(/[\s,]+/).filter(Boolean);
|
|
111
|
+
if (spaced.length > 1 && spaced.every((t) => midiOf(t) !== null)) {
|
|
112
|
+
return spaced.map((t) => midiOf(t));
|
|
113
|
+
}
|
|
114
|
+
const names = spaced.length > 1 ? spaced : trimmed.match(/[A-G][#♯b♭]?|[a-g][#♯♭]?/g) ?? [];
|
|
115
|
+
if (names.length < 2 || names.join("") !== spaced.join(""))
|
|
116
|
+
return null;
|
|
117
|
+
const midis = [];
|
|
118
|
+
for (const name of names) {
|
|
119
|
+
const chroma = chromaOf(name);
|
|
120
|
+
if (chroma === null)
|
|
121
|
+
return null;
|
|
122
|
+
const previous = midis[midis.length - 1];
|
|
123
|
+
if (previous === undefined) {
|
|
124
|
+
const candidates = [28, 40, 52].map((c) => c - c % 12 + chroma);
|
|
125
|
+
midis.push(candidates.reduce((a, b) => Math.abs(b - 40) < Math.abs(a - 40) ? b : a));
|
|
126
|
+
} else {
|
|
127
|
+
let midi = previous - previous % 12 + chroma;
|
|
128
|
+
while (midi <= previous)
|
|
129
|
+
midi += 12;
|
|
130
|
+
midis.push(midi);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return midis;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// src/convert/text-tab.ts
|
|
137
|
+
var STRING_LINE = /^\s*([A-Ga-g][#b]?)?\s*[|:]?[-0-9xX|:hpbrsvt/\\~^().*<>\s]*$/;
|
|
138
|
+
var isStringLine = (line) => STRING_LINE.test(line) && (line.match(/-/g)?.length ?? 0) >= 4 && (line.includes("|") || /^\s*[A-Ga-g][#b]?\s*[-|:]/.test(line));
|
|
139
|
+
function systemsOf(text) {
|
|
140
|
+
const systems = [];
|
|
141
|
+
let current = null;
|
|
142
|
+
for (const raw of text.split(/\r?\n/)) {
|
|
143
|
+
if (isStringLine(raw)) {
|
|
144
|
+
const m = /^\s*([A-Ga-g][#b]?)?\s*[|:]?/.exec(raw);
|
|
145
|
+
current ??= { names: [], lines: [] };
|
|
146
|
+
current.names.push(m[1] ?? null);
|
|
147
|
+
current.lines.push(raw.slice(m[0].length));
|
|
148
|
+
} else if (current) {
|
|
149
|
+
if (current.lines.length >= 4)
|
|
150
|
+
systems.push(current);
|
|
151
|
+
current = null;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
if (current && current.lines.length >= 4)
|
|
155
|
+
systems.push(current);
|
|
156
|
+
return systems;
|
|
157
|
+
}
|
|
158
|
+
function barsOf(lines) {
|
|
159
|
+
const width = Math.max(...lines.map((l) => l.length));
|
|
160
|
+
const padded = lines.map((l) => l.padEnd(width, "-"));
|
|
161
|
+
const bars = [[]];
|
|
162
|
+
for (let col = 0;col < width; col++) {
|
|
163
|
+
if (padded.every((l) => l[col] === "|" || l[col] === ":")) {
|
|
164
|
+
if (bars[bars.length - 1].length)
|
|
165
|
+
bars.push([]);
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
const beat = [];
|
|
169
|
+
let wide = false;
|
|
170
|
+
padded.forEach((line, i) => {
|
|
171
|
+
const c = line[col];
|
|
172
|
+
if (!/\d/.test(c) || /\d/.test(line[col - 1] ?? ""))
|
|
173
|
+
return;
|
|
174
|
+
const two = /\d/.test(line[col + 1] ?? "") ? line.slice(col, col + 2) : c;
|
|
175
|
+
if (two.length === 2)
|
|
176
|
+
wide = true;
|
|
177
|
+
beat.push({ fret: Number(two), string: i + 1 });
|
|
178
|
+
});
|
|
179
|
+
if (wide)
|
|
180
|
+
padded.forEach((line, i) => {
|
|
181
|
+
if (beat.some((n) => n.string === i + 1))
|
|
182
|
+
return;
|
|
183
|
+
const c = line[col + 1] ?? "";
|
|
184
|
+
if (!/\d/.test(c) || /\d/.test(line[col] ?? ""))
|
|
185
|
+
return;
|
|
186
|
+
const two = /\d/.test(line[col + 2] ?? "") ? line.slice(col + 1, col + 3) : c;
|
|
187
|
+
beat.push({ fret: Number(two), string: i + 1 });
|
|
188
|
+
});
|
|
189
|
+
if (beat.length)
|
|
190
|
+
bars[bars.length - 1].push(beat);
|
|
191
|
+
if (wide)
|
|
192
|
+
col++;
|
|
193
|
+
}
|
|
194
|
+
return bars.filter((b) => b.length);
|
|
195
|
+
}
|
|
196
|
+
var BY_COUNT = {
|
|
197
|
+
4: [28, 33, 38, 43],
|
|
198
|
+
5: [23, 28, 33, 38, 43],
|
|
199
|
+
7: [35, 40, 45, 50, 55, 59, 64]
|
|
200
|
+
};
|
|
201
|
+
var BASS = ["E A D G", "B E A D G"];
|
|
202
|
+
function tuningOf(names) {
|
|
203
|
+
const strings = names.length;
|
|
204
|
+
if (names.every((n) => n !== null)) {
|
|
205
|
+
const lowFirst = [...names].reverse();
|
|
206
|
+
if (strings === 6 && lowFirst.join(" ").toLowerCase() === "e a d g b e")
|
|
207
|
+
return null;
|
|
208
|
+
const tuning = parseTuning(lowFirst.join(" "));
|
|
209
|
+
if (!tuning)
|
|
210
|
+
return BY_COUNT[strings] ?? null;
|
|
211
|
+
const bass = BASS.includes(lowFirst.map((n) => n.toUpperCase()).join(" "));
|
|
212
|
+
return bass ? tuning.map((m) => m - 12) : tuning;
|
|
213
|
+
}
|
|
214
|
+
return strings === 6 ? null : BY_COUNT[strings] ?? null;
|
|
215
|
+
}
|
|
216
|
+
function textTabToAlphaTex(text) {
|
|
217
|
+
const systems = systemsOf(text);
|
|
218
|
+
if (!systems.length)
|
|
219
|
+
return null;
|
|
220
|
+
const strings = systems[0].lines.length;
|
|
221
|
+
const names = systems[0].names;
|
|
222
|
+
const bars = systems.flatMap((s) => s.lines.length === strings ? barsOf(s.lines) : []);
|
|
223
|
+
if (!bars.length)
|
|
224
|
+
return null;
|
|
225
|
+
const header = [];
|
|
226
|
+
const tuning = tuningOf(names);
|
|
227
|
+
if (tuning)
|
|
228
|
+
header.push(`\\tuning (${[...tuning].reverse().map((m) => noteOf(m)).join(" ")})`);
|
|
229
|
+
const beat = (notes) => notes.length === 1 ? `${notes[0].fret}.${notes[0].string}` : `(${notes.map((n) => `${n.fret}.${n.string}`).join(" ")})`;
|
|
230
|
+
const body = bars.map((bar) => bar.map(beat).join(" ")).join(` |
|
|
231
|
+
`);
|
|
232
|
+
return [...header, `:8 ${body}`].join(`
|
|
233
|
+
`);
|
|
234
|
+
}
|
|
235
|
+
// src/theory/note-set.ts
|
|
236
|
+
import { Chord, Scale as Scale2 } from "tonal";
|
|
237
|
+
|
|
238
|
+
// src/theory/suffix.ts
|
|
239
|
+
var CANONICAL = new Map([
|
|
240
|
+
["M", ""],
|
|
241
|
+
["maj", ""],
|
|
242
|
+
["min", "m"],
|
|
243
|
+
["-", "m"],
|
|
244
|
+
["M7", "maj7"],
|
|
245
|
+
["Δ", "maj7"],
|
|
246
|
+
["Δ7", "maj7"],
|
|
247
|
+
["M9", "maj9"],
|
|
248
|
+
["Δ9", "maj9"],
|
|
249
|
+
["min6", "m6"],
|
|
250
|
+
["-6", "m6"],
|
|
251
|
+
["min7", "m7"],
|
|
252
|
+
["-7", "m7"],
|
|
253
|
+
["min9", "m9"],
|
|
254
|
+
["-9", "m9"],
|
|
255
|
+
["ø", "m7b5"],
|
|
256
|
+
["ø7", "m7b5"],
|
|
257
|
+
["m7-5", "m7b5"],
|
|
258
|
+
["mmaj7", "mMaj7"],
|
|
259
|
+
["mM7", "mMaj7"],
|
|
260
|
+
["m(maj7)", "mMaj7"],
|
|
261
|
+
["-(maj7)", "mMaj7"],
|
|
262
|
+
["°", "dim"],
|
|
263
|
+
["°7", "dim7"],
|
|
264
|
+
["+", "aug"],
|
|
265
|
+
["sus", "sus4"],
|
|
266
|
+
["add2", "add9"],
|
|
267
|
+
["69", "6/9"],
|
|
268
|
+
["alt", "7alt"]
|
|
269
|
+
]);
|
|
270
|
+
var canonicalSuffix = (suffix) => CANONICAL.get(suffix) ?? suffix;
|
|
271
|
+
|
|
272
|
+
// src/theory/scales.ts
|
|
273
|
+
import { Scale } from "tonal";
|
|
274
|
+
var ALIASES = new Map([
|
|
275
|
+
["natural minor", "minor"],
|
|
276
|
+
["blues", "minor blues"],
|
|
277
|
+
["whole-tone", "whole tone"],
|
|
278
|
+
["pentatonic major", "major pentatonic"],
|
|
279
|
+
["pentatonic minor", "minor pentatonic"]
|
|
280
|
+
]);
|
|
281
|
+
function scaleName(text) {
|
|
282
|
+
const m = /^\s*([A-Ga-g](?:#|b|♯|♭)?)\s+(.+?)\s*$/.exec(text);
|
|
283
|
+
const [tonic, type] = m ? [m[1], m[2]] : ["", text.trim()];
|
|
284
|
+
const lower = type.toLowerCase().replace(/\s+/g, " ");
|
|
285
|
+
const name = ALIASES.get(lower) ?? lower;
|
|
286
|
+
return tonic ? `${tonic[0].toUpperCase()}${tonic.slice(1).replace("♯", "#").replace("♭", "b")} ${name}` : name;
|
|
287
|
+
}
|
|
288
|
+
var isScaleWithoutRoot = (text) => {
|
|
289
|
+
const scale = Scale.get(scaleName(text));
|
|
290
|
+
return !scale.empty && !scale.tonic;
|
|
291
|
+
};
|
|
292
|
+
var CHARACTERISTIC = {
|
|
293
|
+
lydian: ["4A"],
|
|
294
|
+
dorian: ["6M"],
|
|
295
|
+
mixolydian: ["7m"],
|
|
296
|
+
phrygian: ["2m"],
|
|
297
|
+
locrian: ["5d"],
|
|
298
|
+
minor: ["6m"],
|
|
299
|
+
"minor blues": ["5d"],
|
|
300
|
+
"major blues": ["3m"],
|
|
301
|
+
"harmonic minor": ["7M"],
|
|
302
|
+
"melodic minor": ["6M", "7M"],
|
|
303
|
+
"phrygian dominant": ["2m", "3M"]
|
|
304
|
+
};
|
|
305
|
+
var MODE_WORDS = /\b(dorian|phrygian|lydian|mixolydian|aeolian|locrian|blues)\b/i;
|
|
306
|
+
var characteristicIntervals = (type) => CHARACTERISTIC[type] ?? [];
|
|
307
|
+
var marksByDefault = (written) => MODE_WORDS.test(written);
|
|
308
|
+
function modesOf(text) {
|
|
309
|
+
const scale = Scale.get(scaleName(text));
|
|
310
|
+
if (scale.empty || !scale.tonic)
|
|
311
|
+
return null;
|
|
312
|
+
const modes = Scale.modeNames(scale.name);
|
|
313
|
+
if (new Set(modes.map(([, mode]) => mode)).size < 2)
|
|
314
|
+
return null;
|
|
315
|
+
return modes.map(([tonic, mode]) => `${tonic} ${mode}`);
|
|
316
|
+
}
|
|
317
|
+
var scaleTypes = () => Scale.names();
|
|
318
|
+
|
|
319
|
+
// src/theory/note-set.ts
|
|
320
|
+
function markClasses(set, chroma, marks = {}) {
|
|
321
|
+
const classes = [];
|
|
322
|
+
if (set.root === chroma)
|
|
323
|
+
classes.push("mn-root");
|
|
324
|
+
else if (marks.characteristic?.has(chroma))
|
|
325
|
+
classes.push("mn-char");
|
|
326
|
+
if (marks.different?.has(chroma))
|
|
327
|
+
classes.push("mn-diff");
|
|
328
|
+
return classes.length ? ` ${classes.join(" ")}` : "";
|
|
329
|
+
}
|
|
330
|
+
function describeMarks(set, marks = {}) {
|
|
331
|
+
const names = (chromas) => set.members.filter((m) => chromas?.has(m.chroma)).map((m) => m.name);
|
|
332
|
+
const characteristic = names(marks.characteristic);
|
|
333
|
+
const different = names(marks.different);
|
|
334
|
+
return (characteristic.length ? `, characteristic notes: ${characteristic.join(" ")}` : "") + (different.length ? `, different from the compared scale: ${different.join(" ")}` : "");
|
|
335
|
+
}
|
|
336
|
+
var memberFor = (set, chroma) => set.members.find((m) => m.chroma === chroma);
|
|
337
|
+
var memberOf = (set, midi) => set.pitches && !set.pitches.includes(midi) ? undefined : memberFor(set, (midi % 12 + 12) % 12);
|
|
338
|
+
function intervalLabel(interval) {
|
|
339
|
+
const m = /^(\d+)([PMmdA]+)$/.exec(interval);
|
|
340
|
+
if (!m)
|
|
341
|
+
return interval;
|
|
342
|
+
const [, number, quality] = m;
|
|
343
|
+
if (number === "1" && quality === "P")
|
|
344
|
+
return "R";
|
|
345
|
+
const perfect = ["1", "4", "5", "8", "11", "12"].includes(String((Number(number) - 1) % 7 + 1));
|
|
346
|
+
if (quality === "P" || quality === "M")
|
|
347
|
+
return number;
|
|
348
|
+
if (quality === "m")
|
|
349
|
+
return `b${number}`;
|
|
350
|
+
if (quality.startsWith("A"))
|
|
351
|
+
return `${"#".repeat(quality.length)}${number}`;
|
|
352
|
+
return `${"b".repeat(quality.length + (perfect ? 0 : 1))}${number}`;
|
|
353
|
+
}
|
|
354
|
+
function labelOf(member, label, finger) {
|
|
355
|
+
switch (label) {
|
|
356
|
+
case "none":
|
|
357
|
+
return "";
|
|
358
|
+
case "degrees":
|
|
359
|
+
return member.degree === null ? member.name : String(member.degree);
|
|
360
|
+
case "intervals":
|
|
361
|
+
return member.interval === null ? member.name : intervalLabel(member.interval);
|
|
362
|
+
case "fingers":
|
|
363
|
+
return finger ?? "";
|
|
364
|
+
default:
|
|
365
|
+
return member.name;
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
var fromNames = (title, names, intervals, chord = false) => {
|
|
369
|
+
const members = [];
|
|
370
|
+
names.forEach((name, i) => {
|
|
371
|
+
const chroma = chromaOf(name);
|
|
372
|
+
if (chroma === null || members.some((m) => m.chroma === chroma))
|
|
373
|
+
return;
|
|
374
|
+
members.push({
|
|
375
|
+
chroma,
|
|
376
|
+
name,
|
|
377
|
+
degree: intervals ? chord ? Number(/^\d+/.exec(intervals[i] ?? "")?.[0] ?? i + 1) : i + 1 : null,
|
|
378
|
+
interval: intervals?.[i] ?? null
|
|
379
|
+
});
|
|
380
|
+
});
|
|
381
|
+
if (!members.length)
|
|
382
|
+
return null;
|
|
383
|
+
return {
|
|
384
|
+
title,
|
|
385
|
+
members,
|
|
386
|
+
root: intervals ? members[0].chroma : null,
|
|
387
|
+
pitches: null,
|
|
388
|
+
preferFlats: names.some(isFlat)
|
|
389
|
+
};
|
|
390
|
+
};
|
|
391
|
+
function chordNoteSet(symbol) {
|
|
392
|
+
const parts = splitChordSymbol(symbol);
|
|
393
|
+
if (!parts)
|
|
394
|
+
return null;
|
|
395
|
+
const chord = Chord.get(parts.root + canonicalSuffix(parts.suffix));
|
|
396
|
+
if (chord.empty)
|
|
397
|
+
return null;
|
|
398
|
+
const bass = parts.bass;
|
|
399
|
+
const set = fromNames(symbol.trim(), chord.notes, chord.intervals, true);
|
|
400
|
+
const bassChroma = bass ? chromaOf(bass) : null;
|
|
401
|
+
if (set && bass && bassChroma !== null && !memberFor(set, bassChroma)) {
|
|
402
|
+
set.members.push({ chroma: bassChroma, name: bass, degree: null, interval: null });
|
|
403
|
+
}
|
|
404
|
+
return set;
|
|
405
|
+
}
|
|
406
|
+
function scaleNoteSet(name) {
|
|
407
|
+
const scale = Scale2.get(scaleName(name));
|
|
408
|
+
if (scale.empty || !scale.tonic)
|
|
409
|
+
return null;
|
|
410
|
+
const set = fromNames(name.trim(), scale.notes, scale.intervals);
|
|
411
|
+
if (set)
|
|
412
|
+
set.scaleType = scale.type;
|
|
413
|
+
return set;
|
|
414
|
+
}
|
|
415
|
+
function listNoteSet(text) {
|
|
416
|
+
const tokens = text.split(/[\s,]+/).filter(Boolean);
|
|
417
|
+
if (!tokens.length)
|
|
418
|
+
return null;
|
|
419
|
+
const withOctaves = tokens.every((t) => midiOf(t) !== null);
|
|
420
|
+
const names = withOctaves ? tokens.map((t) => t.replace(/-?\d$/, "")) : tokens;
|
|
421
|
+
if (!withOctaves && names.some((n) => chromaOf(n) === null))
|
|
422
|
+
return null;
|
|
423
|
+
const set = fromNames(tokens.join(" "), names, null);
|
|
424
|
+
if (set && withOctaves)
|
|
425
|
+
set.pitches = tokens.map((t) => midiOf(t));
|
|
426
|
+
return set;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
// src/theory/chord-symbol.ts
|
|
430
|
+
var QUALITY_SUFFIXES = [
|
|
431
|
+
[/^(m7b5|m7-5|ø7?)$/, "Minor 7 flat 5"],
|
|
432
|
+
[/^(dim7|°7)$/, "Diminished 7"],
|
|
433
|
+
[/^(maj9|M9|Δ9)$/, "Major 9"],
|
|
434
|
+
[/^(maj7|M7|Δ7?)$/, "Major 7"],
|
|
435
|
+
[/^(7sus4)$/, "7sus4"],
|
|
436
|
+
[/^(m9|min9|-9)$/, "Minor 9"],
|
|
437
|
+
[/^(m7|min7|-7)$/, "Minor 7"],
|
|
438
|
+
[/^(m6|min6|-6)$/, "Minor 6"],
|
|
439
|
+
[/^(m|min|-)$/, "Minor"],
|
|
440
|
+
[/^(dim|°)$/, "Diminished"],
|
|
441
|
+
[/^(aug|\+)$/, "Augmented"],
|
|
442
|
+
[/^(sus2)$/, "Sus2"],
|
|
443
|
+
[/^(sus4|sus)$/, "Sus4"],
|
|
444
|
+
[/^(add9|add2)$/, "Add9"],
|
|
445
|
+
[/^5$/, "5 (Power)"],
|
|
446
|
+
[/^(6\/9|69)$/, "6/9"],
|
|
447
|
+
[/^(mM7|mMaj7|mmaj7|m\(maj7\)|-\(maj7\))$/, "Minor major 7"],
|
|
448
|
+
[/^(13|11|9|7|6)$/, "$1"],
|
|
449
|
+
[/^(maj|M)?$/, "Major"]
|
|
450
|
+
];
|
|
451
|
+
var spell = (note) => note[0] + (note[1] === "#" ? " Sharp" : note[1] === "b" ? " Flat" : "");
|
|
452
|
+
var plain = (quality) => quality.replace(/\s*\([^)]*\)/g, "").trim();
|
|
453
|
+
function parseChord(symbol) {
|
|
454
|
+
const parts = splitChordSymbol(symbol);
|
|
455
|
+
if (!parts)
|
|
456
|
+
return null;
|
|
457
|
+
const trimmed = symbol.trim();
|
|
458
|
+
const { root, suffix, bass } = parts;
|
|
459
|
+
let quality = null;
|
|
460
|
+
for (const [re, label] of QUALITY_SUFFIXES) {
|
|
461
|
+
const hit = re.exec(suffix);
|
|
462
|
+
if (hit) {
|
|
463
|
+
quality = label.replace("$1", hit[1] ?? "");
|
|
464
|
+
break;
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
if (!quality && chordNoteSet(trimmed))
|
|
468
|
+
quality = suffix;
|
|
469
|
+
if (!quality)
|
|
470
|
+
return null;
|
|
471
|
+
return {
|
|
472
|
+
symbol: trimmed,
|
|
473
|
+
root,
|
|
474
|
+
quality,
|
|
475
|
+
bass,
|
|
476
|
+
name: `${spell(root)} ${plain(quality)}${bass ? ` over ${spell(bass)}` : ""}`
|
|
477
|
+
};
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
// src/convert/chord-sheet.ts
|
|
481
|
+
function chordsOf(line) {
|
|
482
|
+
const found = [];
|
|
483
|
+
const re = /\S+/g;
|
|
484
|
+
let m;
|
|
485
|
+
while (m = re.exec(line)) {
|
|
486
|
+
if (/^(\|:?|:?\||:|%|\/|-|\(?\d+x\)?|\(?x\d+\)?|n\.?c\.?)$/i.test(m[0]))
|
|
487
|
+
continue;
|
|
488
|
+
if (!parseChord(m[0]))
|
|
489
|
+
return null;
|
|
490
|
+
found.push({ chord: m[0], at: m.index });
|
|
491
|
+
}
|
|
492
|
+
return found.length ? found : null;
|
|
493
|
+
}
|
|
494
|
+
function sectionOf(line) {
|
|
495
|
+
const m = /^\s*\[?\s*((?:intro|verse|pre-?chorus|chorus|bridge|solo|outro|interlude|refrain|coda)(?:\s*\d+)?)\s*\]?\s*:?\s*$/i.exec(line);
|
|
496
|
+
return m ? m[1].trim() : null;
|
|
497
|
+
}
|
|
498
|
+
var lyricsOf = (line) => line.trimEnd().replace(/\[/g, "(").replace(/\]/g, ")");
|
|
499
|
+
function chordSheetToChordPro(text) {
|
|
500
|
+
const lines = text.replace(/\t/g, " ").split(/\r?\n/);
|
|
501
|
+
const out = [];
|
|
502
|
+
for (let i = 0;i < lines.length; i++) {
|
|
503
|
+
const line = lines[i];
|
|
504
|
+
const section = sectionOf(line);
|
|
505
|
+
if (section) {
|
|
506
|
+
out.push(`{comment: ${section}}`);
|
|
507
|
+
continue;
|
|
508
|
+
}
|
|
509
|
+
const labelled = /^\s*([A-Za-z][\w -]{0,20}):\s+(\S.*)$/.exec(line);
|
|
510
|
+
const labelledChords = labelled ? chordsOf(labelled[2]) : null;
|
|
511
|
+
if (labelled && labelledChords) {
|
|
512
|
+
out.push(`{comment: ${labelled[1].trim()}}`);
|
|
513
|
+
out.push(labelledChords.map((c) => `[${c.chord}]`).join(" "));
|
|
514
|
+
continue;
|
|
515
|
+
}
|
|
516
|
+
const chords = chordsOf(line);
|
|
517
|
+
if (!chords) {
|
|
518
|
+
out.push(lyricsOf(line));
|
|
519
|
+
continue;
|
|
520
|
+
}
|
|
521
|
+
const next = lines[i + 1];
|
|
522
|
+
const lyrics = next !== undefined && next.trim() && !chordsOf(next) && !sectionOf(next);
|
|
523
|
+
if (!lyrics) {
|
|
524
|
+
out.push(chords.map((c) => `[${c.chord}]`).join(" "));
|
|
525
|
+
continue;
|
|
526
|
+
}
|
|
527
|
+
let merged = lyricsOf(next);
|
|
528
|
+
const width = Math.max(merged.length, chords[chords.length - 1].at);
|
|
529
|
+
merged = merged.padEnd(width, " ");
|
|
530
|
+
for (const c of [...chords].reverse()) {
|
|
531
|
+
merged = `${merged.slice(0, c.at)}[${c.chord}]${merged.slice(c.at)}`;
|
|
532
|
+
}
|
|
533
|
+
out.push(merged.trimEnd());
|
|
534
|
+
i++;
|
|
535
|
+
}
|
|
536
|
+
return out.join(`
|
|
537
|
+
`).replace(/\n{3,}/g, `
|
|
538
|
+
|
|
539
|
+
`).trim();
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
// src/tab/index.ts
|
|
543
|
+
var ALPHATAB_VERSION = "1.8.4";
|
|
544
|
+
var alphaTabCdn = (version = ALPHATAB_VERSION) => {
|
|
545
|
+
const base = `https://cdn.jsdelivr.net/npm/@coderline/alphatab@${version}/dist`;
|
|
546
|
+
return {
|
|
547
|
+
script: `${base}/alphaTab.min.js`,
|
|
548
|
+
font: `${base}/font/Bravura.woff2`,
|
|
549
|
+
soundFont: `${base}/soundfont/sonivox.sf3`
|
|
550
|
+
};
|
|
551
|
+
};
|
|
552
|
+
var SETTING = /^\s*(staves|file|tracks?)\s*:\s*(.*)$/i;
|
|
553
|
+
function parseTabSource(language, source) {
|
|
554
|
+
const lines = source.split(/\r?\n/);
|
|
555
|
+
const out = { staves: language, file: null, tracks: null, tex: "", texLine: 0 };
|
|
556
|
+
let i = 0;
|
|
557
|
+
for (;i < lines.length; i++) {
|
|
558
|
+
const line = lines[i];
|
|
559
|
+
if (!line.trim() || /^\s*\/\//.test(line))
|
|
560
|
+
continue;
|
|
561
|
+
const m = SETTING.exec(line);
|
|
562
|
+
if (!m)
|
|
563
|
+
break;
|
|
564
|
+
const key = m[1].toLowerCase();
|
|
565
|
+
const value = m[2].trim();
|
|
566
|
+
if (key === "staves") {
|
|
567
|
+
const v = value.toLowerCase();
|
|
568
|
+
if (v !== "tab" && v !== "score" && v !== "both") {
|
|
569
|
+
throw new Error(`staves: use tab, score or both, got "${value}"`);
|
|
570
|
+
}
|
|
571
|
+
out.staves = v;
|
|
572
|
+
} else if (key === "file") {
|
|
573
|
+
out.file = value.replace(/^!?\[\[|\]\]$/g, "").split(/[|#]/)[0].trim() || null;
|
|
574
|
+
} else {
|
|
575
|
+
const tracks = value.split(/[\s,]+/).filter(Boolean).map(Number);
|
|
576
|
+
if (!tracks.length || tracks.some((t) => !Number.isInteger(t) || t < 1)) {
|
|
577
|
+
throw new Error(`tracks: use track numbers from 1, got "${value}"`);
|
|
578
|
+
}
|
|
579
|
+
out.tracks = tracks;
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
while (i < lines.length && !lines[i].trim())
|
|
583
|
+
i++;
|
|
584
|
+
out.texLine = i;
|
|
585
|
+
out.tex = lines.slice(i).join(`
|
|
586
|
+
`).trimEnd();
|
|
587
|
+
if (!out.file && !out.tex)
|
|
588
|
+
throw new Error("Write the music in alphaTex, or give a file: [[Song.gp]]");
|
|
589
|
+
return out;
|
|
590
|
+
}
|
|
591
|
+
function messageOf(error, lineOffset) {
|
|
592
|
+
const cause = error?.cause ?? error;
|
|
593
|
+
for (const kind of ["lexerDiagnostics", "parserDiagnostics", "semanticDiagnostics"]) {
|
|
594
|
+
const items = cause?.[kind]?.items;
|
|
595
|
+
if (!Array.isArray(items))
|
|
596
|
+
continue;
|
|
597
|
+
const first = items.find((d) => d.severity === 2 && d.message);
|
|
598
|
+
if (first) {
|
|
599
|
+
const at = first.start ? `line ${first.start.line + lineOffset}, column ${first.start.col}: ` : "";
|
|
600
|
+
return `${at}${first.message}`;
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
return error instanceof Error ? error.message : String(error);
|
|
604
|
+
}
|
|
605
|
+
var PROFILE = { tab: "Tab", score: "Score", both: "ScoreTab" };
|
|
606
|
+
function rgbaOf(doc, color, alpha = 1) {
|
|
607
|
+
const canvas = doc.createElement("canvas");
|
|
608
|
+
canvas.width = 1;
|
|
609
|
+
canvas.height = 1;
|
|
610
|
+
const ctx = canvas.getContext("2d", { willReadFrequently: true });
|
|
611
|
+
if (!ctx)
|
|
612
|
+
return null;
|
|
613
|
+
ctx.fillStyle = "#000";
|
|
614
|
+
ctx.fillStyle = color;
|
|
615
|
+
ctx.fillRect(0, 0, 1, 1);
|
|
616
|
+
const data = ctx.getImageData(0, 0, 1, 1).data;
|
|
617
|
+
if (data.length < 4)
|
|
618
|
+
return null;
|
|
619
|
+
return `rgba(${data[0]}, ${data[1]}, ${data[2]}, ${(data[3] / 255 * alpha).toFixed(3)})`;
|
|
620
|
+
}
|
|
621
|
+
function colorsOf(el) {
|
|
622
|
+
const doc = el.ownerDocument;
|
|
623
|
+
const raw = doc.defaultView?.getComputedStyle(el).color || "";
|
|
624
|
+
const fg = raw && rgbaOf(doc, raw) || (/^rgba?\(/.test(raw) ? raw : "rgba(0, 0, 0, 1)");
|
|
625
|
+
const muted = raw && rgbaOf(doc, raw, 0.6) || fg;
|
|
626
|
+
return {
|
|
627
|
+
mainGlyphColor: fg,
|
|
628
|
+
secondaryGlyphColor: muted,
|
|
629
|
+
scoreInfoColor: fg,
|
|
630
|
+
staffLineColor: muted,
|
|
631
|
+
barSeparatorColor: fg,
|
|
632
|
+
barNumberColor: muted
|
|
633
|
+
};
|
|
634
|
+
}
|
|
635
|
+
var PRINT_COLORS = {
|
|
636
|
+
mainGlyphColor: "rgba(0, 0, 0, 1)",
|
|
637
|
+
secondaryGlyphColor: "rgba(0, 0, 0, 0.6)",
|
|
638
|
+
scoreInfoColor: "rgba(0, 0, 0, 1)",
|
|
639
|
+
staffLineColor: "rgba(0, 0, 0, 0.6)",
|
|
640
|
+
barSeparatorColor: "rgba(0, 0, 0, 1)",
|
|
641
|
+
barNumberColor: "rgba(0, 0, 0, 0.6)"
|
|
642
|
+
};
|
|
643
|
+
function printTab(result) {
|
|
644
|
+
if (!result.api)
|
|
645
|
+
throw new Error("The score is not drawn yet");
|
|
646
|
+
result.api.print(undefined, { display: { resources: PRINT_COLORS } });
|
|
647
|
+
}
|
|
648
|
+
var local = (doc) => {
|
|
649
|
+
const win = doc.defaultView ?? globalThis;
|
|
650
|
+
return {
|
|
651
|
+
map: (entries) => new win.Map(entries),
|
|
652
|
+
bytes: (data) => {
|
|
653
|
+
const copy = new win.Uint8Array(data.length);
|
|
654
|
+
copy.set(data);
|
|
655
|
+
return copy;
|
|
656
|
+
}
|
|
657
|
+
};
|
|
658
|
+
};
|
|
659
|
+
var ICONS = {
|
|
660
|
+
play: "M4 2.5v11l9-5.5z",
|
|
661
|
+
pause: "M4 3h3v10H4zM9 3h3v10H9z",
|
|
662
|
+
stop: "M4 4h8v8H4z",
|
|
663
|
+
loop: "M3 8a5 5 0 0 1 8.5-3.5L13 6V2.5M13 8a5 5 0 0 1-8.5 3.5L3 10v3.5",
|
|
664
|
+
metronome: "M6 2h4l3 12H3zM8 11l3-7",
|
|
665
|
+
countIn: "M3 13h10M5 13V9M8 13V6M11 13V3",
|
|
666
|
+
volume: "M2 6h3l4-3v10l-4-3H2zM11 5.5a3.5 3.5 0 0 1 0 5"
|
|
667
|
+
};
|
|
668
|
+
function icon(doc, name) {
|
|
669
|
+
const filled = name === "play" || name === "pause" || name === "stop";
|
|
670
|
+
const root = svg(doc, "svg", {
|
|
671
|
+
class: `mn-play-icon${filled ? "" : " mn-play-icon-loop"}`,
|
|
672
|
+
viewBox: "0 0 16 16",
|
|
673
|
+
width: 12,
|
|
674
|
+
height: 12,
|
|
675
|
+
"aria-hidden": "true"
|
|
676
|
+
});
|
|
677
|
+
root.appendChild(svg(doc, "path", { d: ICONS[name] }));
|
|
678
|
+
return root;
|
|
679
|
+
}
|
|
680
|
+
var time = (ms) => {
|
|
681
|
+
const s = Math.max(0, Math.round(ms / 1000));
|
|
682
|
+
return `${Math.floor(s / 60)}:${String(s % 60).padStart(2, "0")}`;
|
|
683
|
+
};
|
|
684
|
+
function playerBar(doc, api, alphaTab, assets) {
|
|
685
|
+
const bar = html(doc, "div", "mn-play mn-tab-bar");
|
|
686
|
+
bar.setAttribute("role", "group");
|
|
687
|
+
bar.setAttribute("aria-label", "Playback");
|
|
688
|
+
const state = { loop: false, metronome: false, countIn: false, speed: 1, volume: 1 };
|
|
689
|
+
let started = null;
|
|
690
|
+
let cantStart = null;
|
|
691
|
+
const start = () => {
|
|
692
|
+
started ??= new Promise((resolve) => {
|
|
693
|
+
api.playerReady.on(() => resolve(true));
|
|
694
|
+
cantStart = () => resolve(false);
|
|
695
|
+
api.settings.player.playerMode = alphaTab.PlayerMode.EnabledSynthesizer;
|
|
696
|
+
api.settings.player.enableCursor = true;
|
|
697
|
+
if (typeof assets.soundFont === "string")
|
|
698
|
+
api.settings.player.soundFont = assets.soundFont;
|
|
699
|
+
api.updateSettings();
|
|
700
|
+
if (assets.soundFont instanceof Uint8Array)
|
|
701
|
+
api.loadSoundFont(local(doc).bytes(assets.soundFont), false);
|
|
702
|
+
}).then((ok) => {
|
|
703
|
+
if (ok) {
|
|
704
|
+
api.isLooping = state.loop;
|
|
705
|
+
api.metronomeVolume = state.metronome ? 1 : 0;
|
|
706
|
+
api.countInVolume = state.countIn ? 1 : 0;
|
|
707
|
+
api.playbackSpeed = state.speed;
|
|
708
|
+
api.masterVolume = state.volume;
|
|
709
|
+
}
|
|
710
|
+
return ok;
|
|
711
|
+
});
|
|
712
|
+
return started;
|
|
713
|
+
};
|
|
714
|
+
const button = (name, label, run) => {
|
|
715
|
+
const b = html(doc, "button");
|
|
716
|
+
b.type = "button";
|
|
717
|
+
b.title = label;
|
|
718
|
+
b.setAttribute("aria-label", label);
|
|
719
|
+
b.appendChild(icon(doc, name));
|
|
720
|
+
b.addEventListener("click", run);
|
|
721
|
+
bar.appendChild(b);
|
|
722
|
+
return b;
|
|
723
|
+
};
|
|
724
|
+
const live = (apply) => {
|
|
725
|
+
if (api.player)
|
|
726
|
+
apply();
|
|
727
|
+
};
|
|
728
|
+
const toggle = (name, label, key) => {
|
|
729
|
+
const b = button(name, label, () => {
|
|
730
|
+
state[key] = !state[key];
|
|
731
|
+
live(() => {
|
|
732
|
+
api.isLooping = state.loop;
|
|
733
|
+
api.metronomeVolume = state.metronome ? 1 : 0;
|
|
734
|
+
api.countInVolume = state.countIn ? 1 : 0;
|
|
735
|
+
});
|
|
736
|
+
b.classList.toggle("mn-on", state[key]);
|
|
737
|
+
b.setAttribute("aria-pressed", String(state[key]));
|
|
738
|
+
});
|
|
739
|
+
b.setAttribute("aria-pressed", "false");
|
|
740
|
+
};
|
|
741
|
+
const play = button("play", "Play", () => {
|
|
742
|
+
start().then((ok) => {
|
|
743
|
+
if (ok)
|
|
744
|
+
api.playPause();
|
|
745
|
+
});
|
|
746
|
+
});
|
|
747
|
+
button("stop", "Stop", () => live(() => api.stop()));
|
|
748
|
+
toggle("loop", "Loop", "loop");
|
|
749
|
+
toggle("metronome", "Metronome", "metronome");
|
|
750
|
+
toggle("countIn", "Count-in", "countIn");
|
|
751
|
+
const slider = (label, mark, min, max, value, set, unit = "") => {
|
|
752
|
+
const field = html(doc, "label", "mn-play-field");
|
|
753
|
+
const tag = html(doc, "span", "mn-play-label");
|
|
754
|
+
tag.setAttribute("aria-hidden", "true");
|
|
755
|
+
tag.append(mark);
|
|
756
|
+
const input = html(doc, "input");
|
|
757
|
+
input.type = "range";
|
|
758
|
+
input.min = String(min);
|
|
759
|
+
input.max = String(max);
|
|
760
|
+
input.value = String(value);
|
|
761
|
+
input.setAttribute("aria-label", label);
|
|
762
|
+
field.title = `${label}: ${value}${unit}`;
|
|
763
|
+
input.addEventListener("input", () => {
|
|
764
|
+
set(Number(input.value));
|
|
765
|
+
field.title = `${label}: ${input.value}${unit}`;
|
|
766
|
+
});
|
|
767
|
+
field.append(tag, input);
|
|
768
|
+
bar.appendChild(field);
|
|
769
|
+
};
|
|
770
|
+
slider("Speed", "%", 25, 150, 100, (n) => {
|
|
771
|
+
state.speed = n / 100;
|
|
772
|
+
live(() => api.playbackSpeed = state.speed);
|
|
773
|
+
}, "%");
|
|
774
|
+
slider("Volume", icon(doc, "volume"), 0, 100, 100, (n) => {
|
|
775
|
+
state.volume = n / 100;
|
|
776
|
+
live(() => api.masterVolume = state.volume);
|
|
777
|
+
});
|
|
778
|
+
const position = html(doc, "span", "mn-tab-time", "");
|
|
779
|
+
bar.appendChild(position);
|
|
780
|
+
api.playerStateChanged.on((e) => {
|
|
781
|
+
const playing = Number(e.state) === Number(alphaTab.synth.PlayerState.Playing);
|
|
782
|
+
play.replaceChildren(icon(doc, playing ? "pause" : "play"));
|
|
783
|
+
play.title = playing ? "Pause" : "Play";
|
|
784
|
+
play.setAttribute("aria-label", play.title);
|
|
785
|
+
});
|
|
786
|
+
api.playerPositionChanged.on((e) => {
|
|
787
|
+
position.textContent = `${time(e.currentTime)} / ${time(e.endTime)}`;
|
|
788
|
+
});
|
|
789
|
+
return {
|
|
790
|
+
bar,
|
|
791
|
+
failed: (message) => {
|
|
792
|
+
cantStart?.();
|
|
793
|
+
play.disabled = true;
|
|
794
|
+
play.title = `Can't play: ${message}`;
|
|
795
|
+
play.setAttribute("aria-label", play.title);
|
|
796
|
+
}
|
|
797
|
+
};
|
|
798
|
+
}
|
|
799
|
+
function renderTab(language, source, host, options) {
|
|
800
|
+
const doc = host.ownerDocument;
|
|
801
|
+
const el = html(doc, "div", `mn-block mn-block-${language} mn-tab`);
|
|
802
|
+
host.appendChild(el);
|
|
803
|
+
let api = null;
|
|
804
|
+
let gone = false;
|
|
805
|
+
let settled = false;
|
|
806
|
+
let settle = () => {};
|
|
807
|
+
const ready = new Promise((resolve) => {
|
|
808
|
+
settle = (result) => {
|
|
809
|
+
if (settled)
|
|
810
|
+
return;
|
|
811
|
+
settled = true;
|
|
812
|
+
resolve(result);
|
|
813
|
+
};
|
|
814
|
+
});
|
|
815
|
+
const fail = (message) => {
|
|
816
|
+
el.replaceChildren(html(doc, "div", "mn-error-message", `${language}: ${message}`));
|
|
817
|
+
el.classList.add("mn-error");
|
|
818
|
+
settle({ ok: false, error: message });
|
|
819
|
+
};
|
|
820
|
+
if (!source.trim()) {
|
|
821
|
+
el.classList.add("mn-empty");
|
|
822
|
+
settle({ ok: true, error: null });
|
|
823
|
+
} else {
|
|
824
|
+
(async () => {
|
|
825
|
+
await Promise.resolve();
|
|
826
|
+
const parsed = parseTabSource(language, source);
|
|
827
|
+
const at = options.alphaTab;
|
|
828
|
+
if (!at?.AlphaTabApi)
|
|
829
|
+
throw new Error("alphaTab isn't loaded");
|
|
830
|
+
const surface = html(doc, "div", "mn-tab-surface");
|
|
831
|
+
el.appendChild(surface);
|
|
832
|
+
const { assets } = options;
|
|
833
|
+
const made = local(doc);
|
|
834
|
+
const settings = {
|
|
835
|
+
core: {
|
|
836
|
+
useWorkers: Boolean(assets.scriptFile),
|
|
837
|
+
...assets.scriptFile ? { scriptFile: assets.scriptFile } : {},
|
|
838
|
+
smuflFontSources: made.map([[at.FontFileFormat.Woff2, assets.font]]),
|
|
839
|
+
logLevel: at.LogLevel.Warning
|
|
840
|
+
},
|
|
841
|
+
display: {
|
|
842
|
+
staveProfile: PROFILE[parsed.staves],
|
|
843
|
+
resources: colorsOf(el)
|
|
844
|
+
},
|
|
845
|
+
player: { playerMode: at.PlayerMode.Disabled, enableUserInteraction: true }
|
|
846
|
+
};
|
|
847
|
+
const current = new at.AlphaTabApi(surface, settings);
|
|
848
|
+
api = current;
|
|
849
|
+
if (gone) {
|
|
850
|
+
current.destroy();
|
|
851
|
+
return;
|
|
852
|
+
}
|
|
853
|
+
options.onApi?.(current);
|
|
854
|
+
let bar = null;
|
|
855
|
+
current.renderFinished.on(() => {
|
|
856
|
+
if (settled || gone)
|
|
857
|
+
return;
|
|
858
|
+
if (options.player !== false) {
|
|
859
|
+
bar = playerBar(doc, current, at, assets);
|
|
860
|
+
el.appendChild(bar.bar);
|
|
861
|
+
el.addEventListener("pointerdown", (event) => {
|
|
862
|
+
if (event.pointerType !== "touch" || bar?.bar.contains(event.target))
|
|
863
|
+
return;
|
|
864
|
+
el.classList.toggle("mn-play-open");
|
|
865
|
+
});
|
|
866
|
+
}
|
|
867
|
+
settle({ ok: true, error: null });
|
|
868
|
+
});
|
|
869
|
+
current.error.on((e) => {
|
|
870
|
+
const message = messageOf(e, parsed.texLine);
|
|
871
|
+
if (settled)
|
|
872
|
+
bar?.failed(message);
|
|
873
|
+
else
|
|
874
|
+
fail(message);
|
|
875
|
+
});
|
|
876
|
+
const tracks = parsed.tracks?.map((t) => t - 1);
|
|
877
|
+
if (parsed.file) {
|
|
878
|
+
let bytes;
|
|
879
|
+
try {
|
|
880
|
+
bytes = options.resolveFile ? await options.resolveFile(parsed.file) : null;
|
|
881
|
+
} catch (e) {
|
|
882
|
+
throw new Error(`Couldn't read "${parsed.file}": ${e instanceof Error ? e.message : String(e)}`);
|
|
883
|
+
}
|
|
884
|
+
if (gone)
|
|
885
|
+
return;
|
|
886
|
+
if (!bytes)
|
|
887
|
+
throw new Error(`Couldn't find the file "${parsed.file}"`);
|
|
888
|
+
const loaded = current.load(made.bytes(bytes), tracks);
|
|
889
|
+
if (!loaded)
|
|
890
|
+
throw new Error(`Couldn't read "${parsed.file}": not a score file alphaTab knows`);
|
|
891
|
+
} else {
|
|
892
|
+
current.tex(parsed.tex, tracks ?? "all");
|
|
893
|
+
}
|
|
894
|
+
})().catch((e) => {
|
|
895
|
+
if (!gone)
|
|
896
|
+
fail(e instanceof Error ? e.message : String(e));
|
|
897
|
+
});
|
|
898
|
+
}
|
|
899
|
+
return {
|
|
900
|
+
el,
|
|
901
|
+
ready,
|
|
902
|
+
get api() {
|
|
903
|
+
return api;
|
|
904
|
+
},
|
|
905
|
+
destroy() {
|
|
906
|
+
gone = true;
|
|
907
|
+
try {
|
|
908
|
+
api?.destroy();
|
|
909
|
+
} catch {}
|
|
910
|
+
el.remove();
|
|
911
|
+
settle({ ok: false, error: null });
|
|
912
|
+
}
|
|
913
|
+
};
|
|
914
|
+
}
|
|
915
|
+
var PROGRAMS = { guitar: 25, piano: 0 };
|
|
916
|
+
function soundfontPlayer(alphaTab, api) {
|
|
917
|
+
return {
|
|
918
|
+
play(events, { sound, volume, onEnd }) {
|
|
919
|
+
const current = api();
|
|
920
|
+
const player = current?.player;
|
|
921
|
+
if (!current || !player)
|
|
922
|
+
throw new Error("The soundfont player is not ready");
|
|
923
|
+
const midi = new alphaTab.midi.MidiFile;
|
|
924
|
+
const handler = new alphaTab.midi.AlphaSynthMidiFileHandler(midi, true);
|
|
925
|
+
const ticks = (seconds) => Math.round(seconds * midi.division);
|
|
926
|
+
handler.addTempo(0, 60);
|
|
927
|
+
handler.addProgramChange(0, 0, 0, PROGRAMS[sound]);
|
|
928
|
+
const level = Number.isFinite(volume) ? Math.min(1, Math.max(0, volume)) : 0.8;
|
|
929
|
+
current.masterVolume = level;
|
|
930
|
+
for (const e of events) {
|
|
931
|
+
handler.addNote(0, ticks(e.time), Math.max(1, ticks(e.duration)), e.midi, 100, 0);
|
|
932
|
+
}
|
|
933
|
+
const end = Math.max(...events.map((e) => e.time + e.duration), 0);
|
|
934
|
+
current.playbackSpeed = 1;
|
|
935
|
+
player.playOneTimeMidiFile(midi);
|
|
936
|
+
const timer = setTimeout(() => onEnd?.(), (end + 0.2) * 1000);
|
|
937
|
+
return {
|
|
938
|
+
stop() {
|
|
939
|
+
clearTimeout(timer);
|
|
940
|
+
player.stop();
|
|
941
|
+
}
|
|
942
|
+
};
|
|
943
|
+
}
|
|
944
|
+
};
|
|
945
|
+
}
|
|
946
|
+
function createSoundfontPlayer(alphaTab, assets, doc, fallback) {
|
|
947
|
+
let api = null;
|
|
948
|
+
let host = null;
|
|
949
|
+
let ready = null;
|
|
950
|
+
const start = () => {
|
|
951
|
+
if (ready)
|
|
952
|
+
return ready;
|
|
953
|
+
host = html(doc, "div", "mn-tab-hidden");
|
|
954
|
+
host.setAttribute("aria-hidden", "true");
|
|
955
|
+
doc.body.appendChild(host);
|
|
956
|
+
const made = local(doc);
|
|
957
|
+
api = new alphaTab.AlphaTabApi(host, {
|
|
958
|
+
core: {
|
|
959
|
+
useWorkers: Boolean(assets.scriptFile),
|
|
960
|
+
...assets.scriptFile ? { scriptFile: assets.scriptFile } : {},
|
|
961
|
+
smuflFontSources: made.map([[alphaTab.FontFileFormat.Woff2, assets.font]])
|
|
962
|
+
},
|
|
963
|
+
player: {
|
|
964
|
+
playerMode: alphaTab.PlayerMode.EnabledSynthesizer,
|
|
965
|
+
...typeof assets.soundFont === "string" ? { soundFont: assets.soundFont } : {}
|
|
966
|
+
}
|
|
967
|
+
});
|
|
968
|
+
const current = api;
|
|
969
|
+
ready = new Promise((resolve) => {
|
|
970
|
+
current.soundFontLoaded.on(() => resolve(true));
|
|
971
|
+
current.error.on(() => resolve(false));
|
|
972
|
+
});
|
|
973
|
+
if (assets.soundFont instanceof Uint8Array)
|
|
974
|
+
current.loadSoundFont(made.bytes(assets.soundFont), false);
|
|
975
|
+
current.tex("r");
|
|
976
|
+
return ready;
|
|
977
|
+
};
|
|
978
|
+
const inner = soundfontPlayer(alphaTab, () => api);
|
|
979
|
+
return {
|
|
980
|
+
play(events, options) {
|
|
981
|
+
let handle = null;
|
|
982
|
+
let stopped = false;
|
|
983
|
+
start().then((ok) => {
|
|
984
|
+
if (stopped)
|
|
985
|
+
return;
|
|
986
|
+
try {
|
|
987
|
+
if (ok) {
|
|
988
|
+
handle = inner.play(events, options);
|
|
989
|
+
return;
|
|
990
|
+
}
|
|
991
|
+
} catch {}
|
|
992
|
+
if (fallback)
|
|
993
|
+
handle = fallback.play(events, options);
|
|
994
|
+
});
|
|
995
|
+
return {
|
|
996
|
+
stop() {
|
|
997
|
+
stopped = true;
|
|
998
|
+
handle?.stop();
|
|
999
|
+
}
|
|
1000
|
+
};
|
|
1001
|
+
},
|
|
1002
|
+
destroy() {
|
|
1003
|
+
try {
|
|
1004
|
+
api?.destroy();
|
|
1005
|
+
} catch {}
|
|
1006
|
+
host?.remove();
|
|
1007
|
+
api = null;
|
|
1008
|
+
ready = null;
|
|
1009
|
+
}
|
|
1010
|
+
};
|
|
1011
|
+
}
|
|
1012
|
+
function scoreToAlphaTex(alphaTab, bytes) {
|
|
1013
|
+
const score = alphaTab.importer.ScoreLoader.loadScoreFromBytes(bytes);
|
|
1014
|
+
return new alphaTab.exporter.AlphaTexExporter().exportToString(score);
|
|
1015
|
+
}
|
|
1016
|
+
var scoreOf = (alphaTab, music) => typeof music === "string" ? alphaTab.importer.ScoreLoader.loadAlphaTex(music) : alphaTab.importer.ScoreLoader.loadScoreFromBytes(music);
|
|
1017
|
+
function exportGp7(alphaTab, music) {
|
|
1018
|
+
return new alphaTab.exporter.Gp7Exporter().export(scoreOf(alphaTab, music));
|
|
1019
|
+
}
|
|
1020
|
+
function exportMidi(alphaTab, music) {
|
|
1021
|
+
const score = scoreOf(alphaTab, music);
|
|
1022
|
+
const midi = new alphaTab.midi.MidiFile;
|
|
1023
|
+
const settings = new alphaTab.Settings;
|
|
1024
|
+
new alphaTab.midi.MidiFileGenerator(score, settings, new alphaTab.midi.AlphaSynthMidiFileHandler(midi, true)).generate();
|
|
1025
|
+
return midi.toBinary();
|
|
1026
|
+
}
|
|
1027
|
+
export {
|
|
1028
|
+
ALPHATAB_VERSION,
|
|
1029
|
+
alphaTabCdn,
|
|
1030
|
+
chordSheetToChordPro,
|
|
1031
|
+
createSoundfontPlayer,
|
|
1032
|
+
exportGp7,
|
|
1033
|
+
exportMidi,
|
|
1034
|
+
parseTabSource,
|
|
1035
|
+
printTab,
|
|
1036
|
+
renderTab,
|
|
1037
|
+
scoreToAlphaTex,
|
|
1038
|
+
soundfontPlayer,
|
|
1039
|
+
textTabToAlphaTex
|
|
1040
|
+
};
|
|
1041
|
+
|
|
1042
|
+
//# debugId=47D14E391D38F89264756E2164756E21
|
|
1043
|
+
//# sourceMappingURL=index.js.map
|