barmark 0.20.0 → 0.22.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/CHANGELOG.md +153 -1
- package/README.md +11 -5
- package/dist/chart/document.js +21 -7
- package/dist/chart/expand.d.ts +23 -4
- package/dist/chart/expand.js +156 -42
- package/dist/chart/index.d.ts +1 -0
- package/dist/chart/index.js +1 -0
- package/dist/chart/keys.d.ts +2 -0
- package/dist/chart/keys.js +2 -0
- package/dist/chart/lines.js +25 -10
- package/dist/chart/lyricRows.js +20 -5
- package/dist/chart/parse.d.ts +3 -1
- package/dist/chart/parse.js +75 -30
- package/dist/chart/parts.d.ts +3 -1
- package/dist/chart/parts.js +180 -31
- package/dist/chart/pedal.d.ts +5 -1
- package/dist/chart/pedal.js +53 -20
- package/dist/chart/pickup.d.ts +15 -0
- package/dist/chart/pickup.js +41 -0
- package/dist/chart/print.js +62 -15
- package/dist/chart/shares.d.ts +7 -0
- package/dist/chart/shares.js +20 -0
- package/dist/chart/systems.js +22 -6
- package/dist/chart/types.d.ts +18 -4
- package/dist/chart/verses.d.ts +14 -3
- package/dist/chart/verses.js +363 -101
- package/dist/chords/index.d.ts +1 -0
- package/dist/chords/index.js +1 -0
- package/dist/chords/name.d.ts +8 -0
- package/dist/chords/name.js +177 -0
- package/dist/chords/suffixes.d.ts +2 -0
- package/dist/chords/suffixes.js +1 -1
- package/package.json +2 -2
package/dist/chart/verses.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { isBar } from "./barLine.js";
|
|
2
|
-
import { slotTimes } from "./shares.js";
|
|
2
|
+
import { perWritten, slotTimes } from "./shares.js";
|
|
3
3
|
/** Each simple slot of a bar line's cells. */
|
|
4
4
|
const simpleSlots = (l) => l.cells.flatMap((c) => c.kind === "slots" ? c.slots.flatMap((s) => (s.kind === "group" ? s.members : [s])) : []);
|
|
5
5
|
const isNote = (s) => s.kind === "chord" && s.chord.kind === "notes";
|
|
@@ -36,12 +36,25 @@ function singersOf(lines, parts) {
|
|
|
36
36
|
return { owner, all: owner ? [owner] : [] };
|
|
37
37
|
}
|
|
38
38
|
const alone = parts.length === 1;
|
|
39
|
-
|
|
39
|
+
// Each part's lines found once, not by reading every bar line for every part.
|
|
40
|
+
const byPart = new Map();
|
|
41
|
+
for (const l of bars) {
|
|
42
|
+
const key = l.part?.toLowerCase();
|
|
43
|
+
if (key === undefined)
|
|
44
|
+
continue;
|
|
45
|
+
const own = byPart.get(key);
|
|
46
|
+
if (own)
|
|
47
|
+
own.push(l);
|
|
48
|
+
else
|
|
49
|
+
byPart.set(key, [l]);
|
|
50
|
+
}
|
|
51
|
+
const ownLines = (p) => alone ? linesOf(p, bars, true) : (byPart.get((p.label ?? "\u0000").toLowerCase()) ?? []);
|
|
52
|
+
const sung = (p) => ownLines(p).some(hasSyllables);
|
|
40
53
|
const first = parts.find((p) => p.instrument === "voice") ?? parts.find(sung);
|
|
41
54
|
const all = parts
|
|
42
55
|
.filter((p) => p === first || sung(p))
|
|
43
56
|
.map((part) => {
|
|
44
|
-
const sings = singingLine(part,
|
|
57
|
+
const sings = singingLine(part, ownLines(part));
|
|
45
58
|
return {
|
|
46
59
|
part,
|
|
47
60
|
...(part.label && { label: part.label }),
|
|
@@ -58,11 +71,16 @@ function singerLines(singer, bars, parts) {
|
|
|
58
71
|
}
|
|
59
72
|
/** The parts as the parse reports them: each that sings with the line it sings on. */
|
|
60
73
|
export function withSings(parts, singers) {
|
|
74
|
+
const by = new Map();
|
|
75
|
+
for (const x of singers)
|
|
76
|
+
if (!by.has(x.part))
|
|
77
|
+
by.set(x.part, x);
|
|
61
78
|
return parts.map((p) => {
|
|
62
|
-
const sings =
|
|
79
|
+
const sings = by.get(p)?.sings;
|
|
63
80
|
return sings === undefined ? p : { ...p, sings };
|
|
64
81
|
});
|
|
65
82
|
}
|
|
83
|
+
const NO_SINGING_ROWS = { over: () => undefined, all: [] };
|
|
66
84
|
const rowKey = (replay, line) => `${replay ?? ""}:${line}`;
|
|
67
85
|
/** Each row's group: a row under a replay heading covers lines before that heading. */
|
|
68
86
|
function groupsOf(lines) {
|
|
@@ -85,11 +103,11 @@ function groupsOf(lines) {
|
|
|
85
103
|
const NOTES_SING = "the notes above sing verse 1 on their syllables, so a lyric row under them is verse 2 or later (lyrics 2:)";
|
|
86
104
|
const MIXED = "an unnumbered lyric row among numbered ones over the same bars; number every verse (lyrics 1:, lyrics 2:)";
|
|
87
105
|
/** Why a row can't sing in its group, or null (grammar 7.7): every syllable is written once. */
|
|
88
|
-
function rowProblem(row,
|
|
106
|
+
function rowProblem(row, numbered, sungOnNotes, seen) {
|
|
89
107
|
const verse = row.verse ?? 1;
|
|
90
108
|
if (sungOnNotes && verse === 1)
|
|
91
109
|
return NOTES_SING;
|
|
92
|
-
if (row.verse === null &&
|
|
110
|
+
if (row.verse === null && numbered)
|
|
93
111
|
return MIXED;
|
|
94
112
|
const before = seen.get(verse);
|
|
95
113
|
if (before === undefined)
|
|
@@ -108,29 +126,89 @@ function rowProblem(row, group, sungOnNotes, seen) {
|
|
|
108
126
|
*/
|
|
109
127
|
function versesOf(lines, owner, parts) {
|
|
110
128
|
const diagnostics = [];
|
|
111
|
-
const rows = new Map();
|
|
112
129
|
const all = lines.filter((l) => l.kind === "bars");
|
|
113
130
|
const bars = new Map(all.map((l) => [l.line, l]));
|
|
114
131
|
// Only the owner's syllables are the rows' verse 1; another part's are its own.
|
|
115
132
|
const owners = new Set(singerLines(owner, all, parts).map((l) => l.line));
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
133
|
+
const units = coveredUnits(bars, owners);
|
|
134
|
+
const singingAt = new Map();
|
|
135
|
+
const stored = [];
|
|
136
|
+
groupsOf(lines).forEach((group, order) => {
|
|
137
|
+
const unit = units(group.rows[0]?.covers ?? []);
|
|
138
|
+
const rows = singingRows(group, unit.sungOnNotes(), diagnostics);
|
|
139
|
+
for (const row of rows)
|
|
140
|
+
if (unit.counts.has(row.cells.length))
|
|
141
|
+
stored.push(row);
|
|
142
|
+
singingAt.set(rowKey(group.replay, unit.id), { order, unit, rows });
|
|
143
|
+
});
|
|
144
|
+
// Looked up as played, once each: a row is never copied onto every line it covers,
|
|
145
|
+
// which would cost the rows under replay headings times the lines replayed.
|
|
146
|
+
const known = new Map();
|
|
147
|
+
const over = (replay, line) => {
|
|
148
|
+
const key = rowKey(replay, line);
|
|
149
|
+
if (!known.has(key))
|
|
150
|
+
known.set(key, rowsAt(singingAt, units.at(line), replay, line));
|
|
151
|
+
return known.get(key);
|
|
152
|
+
};
|
|
153
|
+
return { rows: { over, all: stored }, diagnostics };
|
|
154
|
+
}
|
|
155
|
+
/** The units rows cover, each built once, and those covering each line. */
|
|
156
|
+
function coveredUnits(bars, owners) {
|
|
157
|
+
const byFirst = new Map();
|
|
158
|
+
const atLine = new Map();
|
|
159
|
+
let made = 0;
|
|
160
|
+
const make = (covers) => {
|
|
161
|
+
const covered = covers.flatMap((n) => bars.get(n) ?? []);
|
|
162
|
+
const barsAt = new Map(covered.map((l) => [l.line, l.cells.filter(isBar).length]));
|
|
163
|
+
let notes;
|
|
164
|
+
const unit = {
|
|
165
|
+
id: made++,
|
|
166
|
+
barsAt,
|
|
167
|
+
counts: new Set(barsAt.values()),
|
|
168
|
+
sungOnNotes: () => {
|
|
169
|
+
notes ??= covered.some((l) => owners.has(l.line) && hasSyllables(l));
|
|
170
|
+
return notes;
|
|
171
|
+
},
|
|
172
|
+
};
|
|
173
|
+
for (const line of barsAt.keys()) {
|
|
174
|
+
const known = atLine.get(line);
|
|
175
|
+
if (known)
|
|
176
|
+
known.push(unit);
|
|
177
|
+
else
|
|
178
|
+
atLine.set(line, [unit]);
|
|
179
|
+
}
|
|
180
|
+
return unit;
|
|
181
|
+
};
|
|
182
|
+
const units = (covers) => {
|
|
183
|
+
const first = covers[0] ?? -1;
|
|
184
|
+
const same = byFirst.get(first) ?? [];
|
|
185
|
+
const found = same.find((u) => u.covers === covers || sameLines(u.covers, covers));
|
|
186
|
+
if (found)
|
|
187
|
+
return found.unit;
|
|
188
|
+
const unit = make(covers);
|
|
189
|
+
same.push({ covers, unit });
|
|
190
|
+
byFirst.set(first, same);
|
|
191
|
+
return unit;
|
|
192
|
+
};
|
|
193
|
+
return Object.assign(units, { at: (line) => atLine.get(line) ?? [] });
|
|
194
|
+
}
|
|
195
|
+
const sameLines = (a, b) => a.length === b.length && a.every((n, i) => n === b[i]);
|
|
196
|
+
/** The rows over a line under a replay heading: each covering unit's, in written order, those with its bar count. */
|
|
197
|
+
function rowsAt(singingAt, units, replay, line) {
|
|
198
|
+
const groups = units
|
|
199
|
+
.flatMap((u) => singingAt.get(rowKey(replay, u.id)) ?? [])
|
|
200
|
+
.sort((a, b) => a.order - b.order);
|
|
201
|
+
const rows = groups.flatMap((g) => g.rows.filter((r) => r.cells.length === g.unit.barsAt.get(line)));
|
|
202
|
+
return rows.length ? rows : undefined;
|
|
127
203
|
}
|
|
128
204
|
/** A group's rows that sing, each other with its error. */
|
|
129
205
|
function singingRows(group, notes, diagnostics) {
|
|
130
206
|
const sungOnNotes = group.replay === null && notes;
|
|
131
207
|
const seen = new Map();
|
|
208
|
+
// Asked once for the group, not again for each of its rows.
|
|
209
|
+
const numbered = group.rows.some((r) => r.verse !== null);
|
|
132
210
|
return group.rows.filter((row) => {
|
|
133
|
-
const problem = rowProblem(row,
|
|
211
|
+
const problem = rowProblem(row, numbered, sungOnNotes, seen);
|
|
134
212
|
if (problem)
|
|
135
213
|
diagnostics.push({ line: row.line, severity: "error", message: problem, token: "lyrics" });
|
|
136
214
|
else
|
|
@@ -141,12 +219,13 @@ function singingRows(group, notes, diagnostics) {
|
|
|
141
219
|
/** Within a rounding error: onsets are sums of fractions of a bar. */
|
|
142
220
|
const near = (a, b) => Math.abs(a - b) < 1e-9;
|
|
143
221
|
/** The syllables sung on a bar's notes (grammar 4.3), at their notes' onsets. */
|
|
144
|
-
function onNotes(
|
|
145
|
-
return slotTimes(
|
|
222
|
+
function onNotes(slots, meter) {
|
|
223
|
+
return slotTimes(slots, meter).flatMap(({ slot, at }) => slot.kind === "chord" && slot.syllable ? [{ at, syllable: slot.syllable, note: true }] : []);
|
|
146
224
|
}
|
|
147
225
|
/** Where the bar's notes start. */
|
|
148
|
-
const noteOnsets = (
|
|
226
|
+
const noteOnsets = (slots, meter) => slotTimes(slots, meter).flatMap(({ slot, at }) => (isNote(slot) ? [at] : []));
|
|
149
227
|
const HELD = { kind: "held" };
|
|
228
|
+
const HELD_INDEX = { sung: 0, breath: 1, start: 2 };
|
|
150
229
|
/**
|
|
151
230
|
* A row's cell as played: each syllable at its beat, on a note when one
|
|
152
231
|
* starts there; a `/` holding a syllable over a new note of the melody is
|
|
@@ -155,10 +234,10 @@ const HELD = { kind: "held" };
|
|
|
155
234
|
* `orphan` says a `/` over a new note found nothing to hold. `ends` is
|
|
156
235
|
* what the cell leaves held.
|
|
157
236
|
*/
|
|
158
|
-
function onBeats(cell,
|
|
237
|
+
function onBeats(cell, meter, onsets, held) {
|
|
159
238
|
let state = held;
|
|
160
239
|
let orphan = false;
|
|
161
|
-
const syllables = slotTimes(cell,
|
|
240
|
+
const syllables = slotTimes(cell, meter).flatMap(({ slot, at }) => {
|
|
162
241
|
const note = onsets.some((o) => near(o, at));
|
|
163
242
|
if (slot.kind === "syllable") {
|
|
164
243
|
state = "sung";
|
|
@@ -172,37 +251,87 @@ function onBeats(cell, bar, onsets, held) {
|
|
|
172
251
|
});
|
|
173
252
|
return { syllables, orphan, ends: state };
|
|
174
253
|
}
|
|
175
|
-
/** What a row holds as its
|
|
176
|
-
function
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
.flatMap((
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
}
|
|
185
|
-
/**
|
|
186
|
-
* The verses a pass sings: outside a repeat every verse; on pass N verse
|
|
187
|
-
* N, or a lone verse 1 on every pass. A lone `lyrics N:` (N ≥ 2) sings on
|
|
188
|
-
* pass N only (decided by Kevin, 2026-11-06).
|
|
189
|
-
*/
|
|
190
|
-
function forPass(verses, pass) {
|
|
191
|
-
if (pass === null)
|
|
192
|
-
return verses;
|
|
193
|
-
const own = verses.filter((v) => v.verse === pass);
|
|
194
|
-
if (own.length)
|
|
195
|
-
return own;
|
|
196
|
-
return verses.every((v) => v.verse === 1) ? verses : [];
|
|
254
|
+
/** What a row holds as each of its cells begins, from its own cells: its last syllable or breath before, or "start" with none. */
|
|
255
|
+
function heldBefore(row) {
|
|
256
|
+
let state = "start";
|
|
257
|
+
return row.cells.map((cell) => {
|
|
258
|
+
const at = state;
|
|
259
|
+
for (const s of cell.flatMap((x) => (x.kind === "group" ? x.members : [x])))
|
|
260
|
+
if (s.kind !== "hold")
|
|
261
|
+
state = s.kind === "breath" ? "breath" : "sung";
|
|
262
|
+
return at;
|
|
263
|
+
});
|
|
197
264
|
}
|
|
265
|
+
/** Whether a cell sings the same whatever is held as it begins: it opens with a syllable or a breath. */
|
|
266
|
+
const opensFresh = (cell) => {
|
|
267
|
+
const first = cell[0];
|
|
268
|
+
const head = first?.kind === "group" ? first.members[0] : first;
|
|
269
|
+
return head?.kind === "syllable" || head?.kind === "breath";
|
|
270
|
+
};
|
|
198
271
|
const textOf = (s) => s.syllable.kind === "sung" ? s.syllable.parts.join("~") : "/";
|
|
199
|
-
/**
|
|
200
|
-
const
|
|
272
|
+
/** A list cut short: past five items, four and how many more (`more` counts the rest). */
|
|
273
|
+
const SHOWN = 4;
|
|
274
|
+
/** "1 and 2", "1, 2 and 3"; past five, "1, 2, 3, 4 and 7 more". */
|
|
275
|
+
function listOf(items, more = (rest) => rest.length) {
|
|
276
|
+
if (items.length > SHOWN + 1)
|
|
277
|
+
return `${items.slice(0, SHOWN).join(", ")} and ${more(items.slice(SHOWN))} more`;
|
|
278
|
+
return items.length < 2
|
|
279
|
+
? (items[0] ?? "")
|
|
280
|
+
: `${items.slice(0, -1).join(", ")} and ${items.at(-1)}`;
|
|
281
|
+
}
|
|
201
282
|
/** "verse 2", "verses 1 and 2": each verse once. */
|
|
202
283
|
const versesNamed = (ns) => {
|
|
203
|
-
const each = [...new Set(ns)];
|
|
284
|
+
const each = [...new Set(ns)].map(String);
|
|
204
285
|
return `${each.length === 1 ? "verse" : "verses"} ${listOf(each)}`;
|
|
205
286
|
};
|
|
287
|
+
/** Passes in order, a run of three or more as a range: "1, 3 and 5\u20139". */
|
|
288
|
+
function passesNamed(passes) {
|
|
289
|
+
const runs = [];
|
|
290
|
+
for (const p of passes) {
|
|
291
|
+
const last = runs.at(-1);
|
|
292
|
+
if (last && p === last[1] + 1)
|
|
293
|
+
last[1] = p;
|
|
294
|
+
else
|
|
295
|
+
runs.push([p, p]);
|
|
296
|
+
}
|
|
297
|
+
const items = runs.flatMap(([a, b]) => b - a >= 2 ? [`${a}\u2013${b}`] : a === b ? [`${a}`] : [`${a}`, `${b}`]);
|
|
298
|
+
const count = (item) => {
|
|
299
|
+
const [a = 0, b = a] = item.split("\u2013").map(Number);
|
|
300
|
+
return b - a + 1;
|
|
301
|
+
};
|
|
302
|
+
const list = listOf(items, (rest) => rest.reduce((n, item) => n + count(item), 0));
|
|
303
|
+
return passes.length === 1
|
|
304
|
+
? `pass ${list} of this repeat sings`
|
|
305
|
+
: `passes ${list} of this repeat sing`;
|
|
306
|
+
}
|
|
307
|
+
function versesIn(rows) {
|
|
308
|
+
const verse = (r) => r.verse ?? 1;
|
|
309
|
+
const ordered = [...rows].sort((a, b) => verse(a) - verse(b));
|
|
310
|
+
const byVerse = new Map();
|
|
311
|
+
for (const r of ordered) {
|
|
312
|
+
const known = byVerse.get(verse(r));
|
|
313
|
+
if (known)
|
|
314
|
+
known.push(r);
|
|
315
|
+
else
|
|
316
|
+
byVerse.set(verse(r), [r]);
|
|
317
|
+
}
|
|
318
|
+
return { ordered, byVerse, allOne: rows.every((r) => verse(r) === 1), named: [] };
|
|
319
|
+
}
|
|
320
|
+
/**
|
|
321
|
+
* Which of a bar's verses a pass sings: outside a repeat every verse; on
|
|
322
|
+
* pass N verse N, or a lone verse 1 on every pass. A lone `lyrics N:`
|
|
323
|
+
* (N ≥ 2) sings on pass N only (decided by Kevin, 2026-11-06). `notes`
|
|
324
|
+
* is whether the bar's notes sing verse 1; the rows chosen follow them.
|
|
325
|
+
*/
|
|
326
|
+
function forPass(verses, notes, pass) {
|
|
327
|
+
const every = { notes, rows: verses.ordered };
|
|
328
|
+
if (pass === null)
|
|
329
|
+
return every;
|
|
330
|
+
const own = { notes: notes && pass === 1, rows: verses.byVerse.get(pass) ?? [] };
|
|
331
|
+
if (own.notes || own.rows.length)
|
|
332
|
+
return own;
|
|
333
|
+
return verses.allOne ? every : { notes: false, rows: [] };
|
|
334
|
+
}
|
|
206
335
|
const HOLDS_NOTHING = ":/ holds the syllable before it, and none is sung before it";
|
|
207
336
|
/** A warning reported once per line and message. */
|
|
208
337
|
function warner(diagnostics) {
|
|
@@ -214,6 +343,7 @@ function warner(diagnostics) {
|
|
|
214
343
|
diagnostics.push({ line, severity: "warning", message, token });
|
|
215
344
|
};
|
|
216
345
|
}
|
|
346
|
+
const NO_ROWS = [];
|
|
217
347
|
/**
|
|
218
348
|
* The words each played bar sings (grammar 4.3, 7.7), for `expand` to
|
|
219
349
|
* attach as it plays the line that sings: the syllables on its notes
|
|
@@ -223,6 +353,10 @@ function warner(diagnostics) {
|
|
|
223
353
|
* starts, a `:/` with nothing sung before it to hold, and a pass that
|
|
224
354
|
* sings nothing where other passes sing, warn. `finish`, after playing,
|
|
225
355
|
* warns of each numbered row never sung.
|
|
356
|
+
*
|
|
357
|
+
* A bar played again shares what its first play read (its notes' and its
|
|
358
|
+
* rows' syllables, by the written slots, the meter and what each row
|
|
359
|
+
* holds as it begins): a pass costs only the verses it sings.
|
|
226
360
|
*/
|
|
227
361
|
function singing(rows, who) {
|
|
228
362
|
const diagnostics = [];
|
|
@@ -230,58 +364,138 @@ function singing(rows, who) {
|
|
|
230
364
|
const melody = who?.sings !== undefined;
|
|
231
365
|
const part = who?.label === undefined ? {} : { part: who.label };
|
|
232
366
|
const sung = new Set();
|
|
367
|
+
/** The rows over each played bar, with the most passes their repeat played there. */
|
|
368
|
+
const reached = new Map();
|
|
233
369
|
/** What each verse's rows left held, in play order: a row starting with `/` holds on from it. */
|
|
234
370
|
const ends = new Map();
|
|
235
371
|
let heard = false;
|
|
236
|
-
/**
|
|
237
|
-
|
|
372
|
+
/**
|
|
373
|
+
* What a written bar's slots, in one meter, give every play of it: its
|
|
374
|
+
* notes' verse (null when they carry no syllables), where its notes
|
|
375
|
+
* start, each row's cell (by the row, then its cell and what it holds
|
|
376
|
+
* as it begins), and the rows already read over it.
|
|
377
|
+
*/
|
|
378
|
+
const writtenBar = perWritten((slots, meter) => {
|
|
379
|
+
const syllables = onNotes(slots, meter);
|
|
380
|
+
const notes = syllables.length
|
|
381
|
+
? {
|
|
382
|
+
verse: 1,
|
|
383
|
+
row: null,
|
|
384
|
+
lyrics: { verse: 1, ...part, syllables },
|
|
385
|
+
opensHeld: syllables[0]?.syllable.kind === "held",
|
|
386
|
+
sings: syllables.some((s) => s.syllable.kind === "sung"),
|
|
387
|
+
}
|
|
388
|
+
: null;
|
|
389
|
+
return {
|
|
390
|
+
meter,
|
|
391
|
+
notes,
|
|
392
|
+
onsets: melody ? noteOnsets(slots, meter) : [],
|
|
393
|
+
cells: new Map(),
|
|
394
|
+
checked: new Map(),
|
|
395
|
+
};
|
|
396
|
+
});
|
|
397
|
+
const held = new WeakMap();
|
|
398
|
+
const heldAt = (row, i) => {
|
|
399
|
+
let each = held.get(row);
|
|
400
|
+
if (!each) {
|
|
401
|
+
each = heldBefore(row);
|
|
402
|
+
held.set(row, each);
|
|
403
|
+
}
|
|
404
|
+
return each[i] ?? "start";
|
|
405
|
+
};
|
|
406
|
+
const verses = new WeakMap();
|
|
407
|
+
const versesOver = (own) => {
|
|
408
|
+
let v = verses.get(own);
|
|
409
|
+
if (!v) {
|
|
410
|
+
v = versesIn(own);
|
|
411
|
+
verses.set(own, v);
|
|
412
|
+
}
|
|
413
|
+
return v;
|
|
414
|
+
};
|
|
415
|
+
/** A row's cell read over a written bar, from what it holds as it begins: a syllable where no note starts warns. */
|
|
416
|
+
const readRow = (row, w, at, begins) => {
|
|
238
417
|
const verse = row.verse ?? 1;
|
|
239
|
-
const
|
|
240
|
-
const
|
|
241
|
-
const cell = onBeats(row.cells[from.bar] ?? [], bar, onsets, held);
|
|
242
|
-
const syllables = cell.syllables;
|
|
243
|
-
const missed = melody ? syllables.find((s) => !s.note) : undefined;
|
|
418
|
+
const cell = onBeats(row.cells[at] ?? [], w.meter, w.onsets, begins);
|
|
419
|
+
const missed = melody ? cell.syllables.find((s) => !s.note) : undefined;
|
|
244
420
|
if (missed)
|
|
245
421
|
warn(row.line, `"${textOf(missed)}" falls where the voice starts no note`, textOf(missed));
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
422
|
+
const lyrics = { verse, ...part, syllables: cell.syllables };
|
|
423
|
+
return { verse, row, lyrics, ends: cell.ends, orphan: cell.orphan };
|
|
424
|
+
};
|
|
425
|
+
/** The cells already read of a row over a written bar. */
|
|
426
|
+
const readOver = (row, w) => {
|
|
427
|
+
const found = w.cells.get(row);
|
|
428
|
+
if (found)
|
|
429
|
+
return found;
|
|
430
|
+
const made = new Map();
|
|
431
|
+
w.cells.set(row, made);
|
|
432
|
+
return made;
|
|
253
433
|
};
|
|
254
|
-
/**
|
|
255
|
-
const
|
|
256
|
-
const
|
|
257
|
-
const
|
|
258
|
-
const
|
|
259
|
-
const
|
|
260
|
-
const
|
|
261
|
-
if (
|
|
262
|
-
|
|
263
|
-
|
|
434
|
+
/** A row's verse in a bar, its syllables on the beats: read once for each thing it can begin holding. */
|
|
435
|
+
const fromRow = (row, w, at) => {
|
|
436
|
+
const own = heldAt(row, at);
|
|
437
|
+
const begins = own === "start" ? (ends.get(row.verse ?? 1) ?? "start") : own;
|
|
438
|
+
const key = at * 4 + (opensFresh(row.cells[at] ?? []) ? 3 : HELD_INDEX[begins]);
|
|
439
|
+
const read = readOver(row, w);
|
|
440
|
+
const found = read.get(key);
|
|
441
|
+
if (found)
|
|
442
|
+
return found;
|
|
443
|
+
const made = readRow(row, w, at, begins);
|
|
444
|
+
read.set(key, made);
|
|
445
|
+
return made;
|
|
446
|
+
};
|
|
447
|
+
/** The first time a bar line's bar plays over these slots, every row's cell is read, sung or not, for its warnings. */
|
|
448
|
+
const checkRows = (own, w, at) => {
|
|
449
|
+
const seen = w.checked.get(own) ?? new Set();
|
|
450
|
+
w.checked.set(own, seen);
|
|
451
|
+
if (seen.has(at))
|
|
452
|
+
return;
|
|
453
|
+
seen.add(at);
|
|
454
|
+
for (const row of own)
|
|
455
|
+
fromRow(row, w, at);
|
|
264
456
|
};
|
|
265
457
|
/** A `:/` holds the syllable sung before it, in play order. */
|
|
266
458
|
const hold = (bar, notes) => {
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
warn(bar.line, HOLDS_NOTHING, ":/");
|
|
272
|
-
}
|
|
459
|
+
if (notes?.opensHeld && !heard)
|
|
460
|
+
warn(bar.line, HOLDS_NOTHING, ":/");
|
|
461
|
+
if (notes?.sings)
|
|
462
|
+
heard = true;
|
|
273
463
|
};
|
|
464
|
+
/** Each line's passes that sing none of its verses: one warning, pushed at the first and worded once playing is done. */
|
|
465
|
+
const silences = new Map();
|
|
274
466
|
/** On a pass that sings none of a bar's several verses, say so. */
|
|
275
|
-
const silent = (
|
|
467
|
+
const silent = (over, notes, from) => {
|
|
468
|
+
const i = notes ? 1 : 0;
|
|
469
|
+
over.named[i] ??= versesNamed([
|
|
470
|
+
...(notes ? [1] : []),
|
|
471
|
+
...over.ordered.map((r) => r.verse ?? 1),
|
|
472
|
+
]);
|
|
473
|
+
const named = over.named[i];
|
|
474
|
+
const key = `${from.line}\u0000${named}`;
|
|
475
|
+
let found = silences.get(key);
|
|
476
|
+
if (!found) {
|
|
477
|
+
const warning = {
|
|
478
|
+
line: from.line,
|
|
479
|
+
severity: "warning",
|
|
480
|
+
message: "",
|
|
481
|
+
token: "lyrics",
|
|
482
|
+
};
|
|
483
|
+
diagnostics.push(warning);
|
|
484
|
+
found = { passes: new Set(), named, warning };
|
|
485
|
+
silences.set(key, found);
|
|
486
|
+
}
|
|
487
|
+
found.passes.add(from.pass ?? 0);
|
|
488
|
+
};
|
|
489
|
+
/** Word each silent-pass warning from all its passes. */
|
|
490
|
+
const wordSilences = () => {
|
|
491
|
+
for (const { passes, named, warning } of silences.values())
|
|
492
|
+
warning.message = `${passesNamed([...passes].sort((a, b) => a - b))} nothing: its bars have ${named}`;
|
|
493
|
+
};
|
|
276
494
|
/** Each written bar's notes' syllables, by their line, and whether a pass sang them. */
|
|
277
495
|
const notes = new Map();
|
|
278
|
-
const noteNotes = (bar, from,
|
|
279
|
-
const own = all.find((c) => c.row === null);
|
|
280
|
-
if (!own)
|
|
281
|
-
return;
|
|
496
|
+
const noteNotes = (bar, from, sungHere) => {
|
|
282
497
|
const key = `${from.line}:${from.bar}`;
|
|
283
|
-
|
|
284
|
-
notes.set(key, { line: bar.line, sung: sungHere });
|
|
498
|
+
notes.set(key, { line: bar.line, sung: sungHere || notes.get(key)?.sung === true });
|
|
285
499
|
};
|
|
286
500
|
/** A row's verse as a pass sings it: the row is sung, leaves its state held, and warns of a hold with nothing to hold. */
|
|
287
501
|
const sungRow = ({ row, verse, ends: left, orphan }) => {
|
|
@@ -293,22 +507,68 @@ function singing(rows, who) {
|
|
|
293
507
|
if (orphan)
|
|
294
508
|
warn(row.line, HOLDS_NOTHING, "/");
|
|
295
509
|
};
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
const
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
510
|
+
/** The rows over a played bar: under its replay heading when that writes rows for its line, else as written. */
|
|
511
|
+
const rowsOver = (from) => {
|
|
512
|
+
const replayed = from.replay === null ? undefined : rows.over(from.replay, from.line);
|
|
513
|
+
return { own: replayed ?? rows.over(null, from.line) ?? NO_ROWS, replayed: !!replayed };
|
|
514
|
+
};
|
|
515
|
+
/** The verses a pass sings: the notes' first, then the rows'. */
|
|
516
|
+
const choose = (pick, mine, w, at) => {
|
|
517
|
+
const chosen = pick.rows.map((row) => fromRow(row, w, at));
|
|
518
|
+
return pick.notes && mine ? [mine, ...chosen] : chosen;
|
|
519
|
+
};
|
|
520
|
+
/** What a pass leaves behind: a silent pass warns, the notes' verse is marked sung or not, holds and rows move on. */
|
|
521
|
+
const played = (bar, from, mine, chosen) => {
|
|
522
|
+
const notesSung = mine !== null && chosen[0] === mine;
|
|
523
|
+
if (mine)
|
|
524
|
+
noteNotes(bar, from, notesSung);
|
|
525
|
+
hold(bar, notesSung ? mine : null);
|
|
303
526
|
for (const c of chosen)
|
|
304
527
|
sungRow(c);
|
|
528
|
+
};
|
|
529
|
+
const singBar = (bar, from) => {
|
|
530
|
+
const { own, replayed } = rowsOver(from);
|
|
531
|
+
reached.set(own, Math.max(reached.get(own) ?? 0, from.passes ?? 1));
|
|
532
|
+
const w = writtenBar(bar.slots, bar.meter);
|
|
533
|
+
checkRows(own, w, from.bar);
|
|
534
|
+
const mine = replayed || !who ? null : w.notes;
|
|
535
|
+
const over = versesOver(own);
|
|
536
|
+
const chosen = choose(forPass(over, mine !== null, from.pass), mine, w, from.bar);
|
|
537
|
+
if ((mine || own.length) && !chosen.length)
|
|
538
|
+
silent(over, mine !== null, from);
|
|
539
|
+
played(bar, from, mine, chosen);
|
|
305
540
|
return chosen.length ? chosen.map((c) => c.lyrics) : undefined;
|
|
306
541
|
};
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
const
|
|
542
|
+
/** How many verses a bar would sing here, with no effect on what is sung or warned. */
|
|
543
|
+
const versesHere = (bar, from) => {
|
|
544
|
+
const { own, replayed } = rowsOver(from);
|
|
545
|
+
const mine = replayed || !who ? null : writtenBar(bar.slots, bar.meter).notes;
|
|
546
|
+
const pick = forPass(versesOver(own), mine !== null, from.pass);
|
|
547
|
+
return (pick.notes && mine ? 1 : 0) + pick.rows.length;
|
|
548
|
+
};
|
|
549
|
+
const sing = Object.assign(singBar, { verses: versesHere });
|
|
550
|
+
/**
|
|
551
|
+
* The rows a stop left unsung: those never reached, and those reached in
|
|
552
|
+
* a repeat with passes enough for their verse, whose pass the stop cut off.
|
|
553
|
+
*/
|
|
554
|
+
const cutOff = (stopped) => {
|
|
555
|
+
if (!stopped)
|
|
556
|
+
return () => false;
|
|
557
|
+
const most = new Map();
|
|
558
|
+
for (const [own, passes] of reached)
|
|
559
|
+
for (const r of own)
|
|
560
|
+
most.set(r, Math.max(most.get(r) ?? 0, passes));
|
|
561
|
+
return (r) => (most.get(r) ?? Number.POSITIVE_INFINITY) >= (r.verse ?? 1);
|
|
562
|
+
};
|
|
563
|
+
/** After playing: word the silent passes, then warn of verses never sung, unless a stop left them unsung. */
|
|
564
|
+
const finish = (stopped = false) => {
|
|
565
|
+
wordSilences();
|
|
566
|
+
if (!stopped)
|
|
567
|
+
for (const n of notes.values())
|
|
568
|
+
if (!n.sung)
|
|
569
|
+
warn(n.line, "verse 1 is never sung: no pass of its repeat is pass 1", "lyrics");
|
|
570
|
+
const cut = cutOff(stopped);
|
|
571
|
+
const unsung = new Set(rows.all.filter((r) => r.verse !== null && !sung.has(r) && !cut(r)));
|
|
312
572
|
for (const r of unsung)
|
|
313
573
|
warn(r.line, `verse ${r.verse} is never sung: no pass of its repeat is pass ${r.verse}`, "lyrics");
|
|
314
574
|
};
|
|
@@ -325,7 +585,7 @@ export function voicesOf(lines, parts) {
|
|
|
325
585
|
const singers = owner ? all : [null];
|
|
326
586
|
const voices = singers.map((singer) => ({
|
|
327
587
|
singer,
|
|
328
|
-
...singing(singer === owner ? verses.rows :
|
|
588
|
+
...singing(singer === owner ? verses.rows : NO_SINGING_ROWS, singer),
|
|
329
589
|
}));
|
|
330
590
|
return { voices, all, owner, diagnostics: verses.diagnostics };
|
|
331
591
|
}
|
|
@@ -344,8 +604,10 @@ export function voicesIn(voices, part, lineName) {
|
|
|
344
604
|
export function together(sings) {
|
|
345
605
|
if (sings.length < 2)
|
|
346
606
|
return sings[0];
|
|
347
|
-
|
|
348
|
-
const all = sings.flatMap((
|
|
607
|
+
const sing = (bar, from) => {
|
|
608
|
+
const all = sings.flatMap((s) => s(bar, from) ?? []);
|
|
349
609
|
return all.length ? all.sort((a, b) => a.verse - b.verse) : undefined;
|
|
350
610
|
};
|
|
611
|
+
const verses = (bar, from) => sings.reduce((n, s) => n + s.verses(bar, from), 0);
|
|
612
|
+
return Object.assign(sing, { verses });
|
|
351
613
|
}
|
package/dist/chords/index.d.ts
CHANGED
package/dist/chords/index.js
CHANGED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Key } from "../chart/types.js";
|
|
2
|
+
/**
|
|
3
|
+
* A chord symbol for sounding pitch classes over a bass, spelled for the key;
|
|
4
|
+
* null when none fits (spec 5.6). Pitch classes are taken mod 12 and
|
|
5
|
+
* deduplicated; the bass is added when missing. Two pitch classes name only a
|
|
6
|
+
* power chord. The suggestion grammar 4's linter may make for a note set.
|
|
7
|
+
*/
|
|
8
|
+
export declare function nameNotes(pitchClasses: number[], bass: number, key: Key | null): string | null;
|