barmark 0.21.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.
@@ -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
- const sung = (p) => linesOf(p, bars, alone).some(hasSyllables);
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, linesOf(part, bars, alone));
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 = singers.find((x) => x.part === p)?.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, group, sungOnNotes, seen) {
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 && group.rows.some((r) => r.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
- for (const group of groupsOf(lines)) {
117
- const covered = (group.rows[0]?.covers ?? []).flatMap((n) => bars.get(n) ?? []);
118
- const sungOnNotes = covered.some((l) => owners.has(l.line) && hasSyllables(l));
119
- for (const row of singingRows(group, sungOnNotes, diagnostics))
120
- for (const l of covered)
121
- if (l.cells.filter(isBar).length === row.cells.length) {
122
- const key = rowKey(group.replay, l.line);
123
- rows.set(key, [...(rows.get(key) ?? []), row]);
124
- }
125
- }
126
- return { rows, diagnostics };
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, group, sungOnNotes, seen);
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(bar) {
145
- return slotTimes(bar.slots, bar.meter).flatMap(({ slot, at }) => slot.kind === "chord" && slot.syllable ? [{ at, syllable: slot.syllable, note: true }] : []);
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 = (bar) => slotTimes(bar.slots, bar.meter).flatMap(({ slot, at }) => (isNote(slot) ? [at] : []));
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, bar, onsets, held) {
237
+ function onBeats(cell, meter, onsets, held) {
159
238
  let state = held;
160
239
  let orphan = false;
161
- const syllables = slotTimes(cell, bar.meter).flatMap(({ slot, at }) => {
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 cell `i` begins from its own cells: its last syllable or breath before, or "start" with none. */
176
- function heldAt(row, i) {
177
- const before = row.cells
178
- .slice(0, i)
179
- .flat()
180
- .flatMap((s) => (s.kind === "group" ? s.members : [s]))
181
- .filter((s) => s.kind !== "hold");
182
- const last = before.at(-1);
183
- return last === undefined ? "start" : last.kind === "breath" ? "breath" : "sung";
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
- /** "1 and 2", "1, 2 and 3". */
200
- const listOf = (ns) => ns.length < 2 ? `${ns[0] ?? ""}` : `${ns.slice(0, -1).join(", ")} and ${ns.at(-1)}`;
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
- /** A row's verse in a bar, its syllables on the beats. */
237
- const fromRow = (row, bar, from, onsets) => {
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 own = heldAt(row, from.bar);
240
- const held = own === "start" ? (ends.get(verse) ?? "start") : own;
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
- return {
247
- verse,
248
- row,
249
- lyrics: { verse, ...part, syllables },
250
- ends: cell.ends,
251
- orphan: cell.orphan,
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
- /** The bar's verses before a pass picks: its notes' and its rows'. */
255
- const candidates = (bar, from) => {
256
- const replayed = from.replay === null ? undefined : rows.get(rowKey(from.replay, from.line));
257
- const own = replayed ?? rows.get(rowKey(null, from.line)) ?? [];
258
- const onsets = melody ? noteOnsets(bar) : [];
259
- const out = own.map((row) => fromRow(row, bar, from, onsets));
260
- const notes = replayed || !who ? [] : onNotes(bar);
261
- if (notes.length)
262
- out.unshift({ verse: 1, row: null, lyrics: { verse: 1, ...part, syllables: notes } });
263
- return out.sort((a, b) => a.verse - b.verse);
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
- for (const s of notes?.lyrics.syllables ?? []) {
268
- if (s.syllable.kind === "sung")
269
- heard = true;
270
- else if (!heard)
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 = (all, from) => warn(from.line, `pass ${from.pass} of this repeat sings nothing: its bars have ${versesNamed(all.map((c) => c.verse))}`, "lyrics");
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, all, chosen) => {
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
- const sungHere = chosen.includes(own) || notes.get(key)?.sung === true;
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
- const sing = (bar, from) => {
297
- const all = candidates(bar, from);
298
- const chosen = forPass(all, from.pass);
299
- if (all.length && !chosen.length)
300
- silent(all, from);
301
- noteNotes(bar, from, all, chosen);
302
- hold(bar, chosen.find((c) => c.row === null));
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
- const finish = () => {
308
- for (const n of notes.values())
309
- if (!n.sung)
310
- warn(n.line, "verse 1 is never sung: no pass of its repeat is pass 1", "lyrics");
311
- const unsung = new Set([...rows.values()].flat().filter((r) => r.verse !== null && !sung.has(r)));
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 : new Map(), singer),
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
- return (bar, from) => {
348
- const all = sings.flatMap((sing) => sing(bar, from) ?? []);
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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "barmark",
3
- "version": "0.21.0",
3
+ "version": "0.22.0",
4
4
  "description": "Markdown-inspired plain-text notation for chord charts: a parser, a canonical printer and Harte-label conversion.",
5
5
  "type": "module",
6
6
  "exports": {
@@ -31,7 +31,7 @@
31
31
  ],
32
32
  "author": "Kevin Peckham",
33
33
  "license": "MIT",
34
- "homepage": "https://github.com/kevinpeckham/barmark/tree/main/packages/barmark#readme",
34
+ "homepage": "https://www.barmark.dev",
35
35
  "repository": {
36
36
  "type": "git",
37
37
  "url": "git+https://github.com/kevinpeckham/barmark.git",