barmark 0.16.0 → 0.17.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.
@@ -8,3 +8,24 @@ export function memberShares(members) {
8
8
  return dotted === 0 ? [0.75, 0.25] : [0.25, 0.75];
9
9
  return members.map(() => 1 / members.length);
10
10
  }
11
+ /**
12
+ * Each simple slot of a bar in order, with its onset and length in whole
13
+ * notes: the bar split evenly among its slots (grammar 2.1), and a group's
14
+ * slot among its members (2.2). A lyric row's slots are timed as a bar
15
+ * line's (7.7).
16
+ */
17
+ export function slotTimes(slots, meter) {
18
+ const each = meter.n / meter.d / slots.length;
19
+ const out = [];
20
+ let at = 0;
21
+ for (const s of slots) {
22
+ const members = "members" in s ? s.members : [s];
23
+ const shares = memberShares(members);
24
+ members.forEach((slot, i) => {
25
+ const length = each * (shares[i] ?? 0);
26
+ out.push({ slot, at, length });
27
+ at += length;
28
+ });
29
+ }
30
+ return out;
31
+ }
@@ -0,0 +1,21 @@
1
+ import type { Syllable } from "./types.js";
2
+ /**
3
+ * A syllable token (grammar 4.3): `/` holds the syllable before, a
4
+ * trailing `-` continues the word, and `~` joins elided syllables on one
5
+ * note. Null when no syllable reads: the token is empty, a side of a `~`
6
+ * is, or it is a hold with a hyphen (`/-`), since a hold can't begin or
7
+ * continue a word.
8
+ */
9
+ export declare function readSyllable(text: string): Syllable | null;
10
+ /** The note part of a slot token and the syllable written after it, if any. */
11
+ export type Split = {
12
+ note: string;
13
+ syllable: string | null;
14
+ };
15
+ /**
16
+ * Split a group member at the colon after its note set's closing brace (and
17
+ * a rhythm's dot): `{C4}.:maz-` is `{C4}.` and `maz-`. A member with no
18
+ * brace splits at its first colon, so `G:la` is `G` and `la`, which the
19
+ * caller rejects: only a note set takes a syllable.
20
+ */
21
+ export declare function splitSyllable(member: string): Split;
@@ -0,0 +1,32 @@
1
+ /**
2
+ * A syllable token (grammar 4.3): `/` holds the syllable before, a
3
+ * trailing `-` continues the word, and `~` joins elided syllables on one
4
+ * note. Null when no syllable reads: the token is empty, a side of a `~`
5
+ * is, or it is a hold with a hyphen (`/-`), since a hold can't begin or
6
+ * continue a word.
7
+ */
8
+ export function readSyllable(text) {
9
+ if (text === "/")
10
+ return { kind: "held" };
11
+ if (text === "/-")
12
+ return null;
13
+ const continues = text.endsWith("-");
14
+ const parts = (continues ? text.slice(0, -1) : text).split("~");
15
+ if (parts.some((p) => p === ""))
16
+ return null;
17
+ return { kind: "sung", parts, continues };
18
+ }
19
+ /**
20
+ * Split a group member at the colon after its note set's closing brace (and
21
+ * a rhythm's dot): `{C4}.:maz-` is `{C4}.` and `maz-`. A member with no
22
+ * brace splits at its first colon, so `G:la` is `G` and `la`, which the
23
+ * caller rejects: only a note set takes a syllable.
24
+ */
25
+ export function splitSyllable(member) {
26
+ const brace = member.indexOf("}");
27
+ const after = member[brace + 1] === "." ? brace + 2 : brace + 1;
28
+ const colon = brace < 0 ? member.indexOf(":") : member[after] === ":" ? after : -1;
29
+ if (colon < 0)
30
+ return { note: member, syllable: null };
31
+ return { note: member.slice(0, colon), syllable: member.slice(colon + 1) };
32
+ }
@@ -36,16 +36,17 @@ const isLabelled = (l) => (l.kind === "bars" || l.kind === "invalid") && l.part
36
36
  /** The key a part's line is grouped and played by: its label, and its line's name if any (grammar 7.6). */
37
37
  export const streamKey = (part, lineName) => (lineName === undefined ? part : `${part}.${lineName}`).toLowerCase();
38
38
  /**
39
- * What a line takes in its system, and what it can't share it with: its
40
- * own stream, and its part written bare or as lines, which can't share a
41
- * system (grammar 7.6).
39
+ * What a line takes in its system, and what it can't follow there (grammar
40
+ * 7.6): each line once, and a part's named lines may follow its bare line
41
+ * but a bare line can't follow them, so a bare line after its part's
42
+ * lines starts a new system, as it did before bare lines could join.
42
43
  */
43
44
  function claimsOf(l) {
44
45
  const part = (l.part ?? "").toLowerCase();
45
- if (l.lineName === undefined)
46
- return { takes: [part, `${part}#bare`], clashes: [part, `${part}#lines`] };
47
46
  const own = streamKey(part, l.lineName);
48
- return { takes: [own, `${part}#lines`], clashes: [own, `${part}#bare`] };
47
+ if (l.lineName === undefined)
48
+ return { takes: [own], clashes: [own, `${part}#lines`] };
49
+ return { takes: [own, `${part}#lines`], clashes: [own] };
49
50
  }
50
51
  /** Number each run of labelled lines with no label twice (grammar 7.4). */
51
52
  function numberSystems(lines) {
@@ -60,12 +60,36 @@ export type Articulation = "accent" | "hit";
60
60
  export type Push = "eighth" | "sixteenth";
61
61
  /** `+` presses the sustain pedal, or changes it when it is down; `*` lifts it (grammar 2.9). */
62
62
  export type Pedal = "press" | "release";
63
+ /** A syllable sung on a note (grammar 4.3). `held` is `:/`. `parts` holds elided syllables (`the~end`). */
64
+ export type Syllable = {
65
+ kind: "sung";
66
+ parts: string[];
67
+ continues: boolean;
68
+ } | {
69
+ kind: "held";
70
+ };
63
71
  /**
64
72
  * A chord slot holds a chord symbol or a note set; `chord.kind` tells
65
73
  * which. `voicing` is the exact notes written after a chord's name
66
- * (`Cmaj7{E3 G3 B3}`, grammar 4.2). The optional fields are present only
67
- * when written.
74
+ * (`Cmaj7{E3 G3 B3}`, grammar 4.2). `syllable` is sung on a note set
75
+ * (`{G4}:maz-`, grammar 4.3), never on a chord name or a voicing. The
76
+ * optional fields are present only when written.
68
77
  */
78
+ /**
79
+ * A slot of a lyric row (grammar 7.7): a syllable, `/` holding the one
80
+ * before, `--` a breath (nothing sung), or `_` splitting the slot.
81
+ */
82
+ export type LyricSlot = {
83
+ kind: "syllable";
84
+ syllable: Syllable;
85
+ } | {
86
+ kind: "hold";
87
+ } | {
88
+ kind: "breath";
89
+ } | {
90
+ kind: "group";
91
+ members: LyricSlot[];
92
+ };
69
93
  export type SimpleSlot = ({
70
94
  kind: "chord";
71
95
  chord: ChordRecord;
@@ -73,6 +97,7 @@ export type SimpleSlot = ({
73
97
  pedal?: Pedal;
74
98
  articulation?: Articulation;
75
99
  push?: Push;
100
+ syllable?: Syllable;
76
101
  } | {
77
102
  kind: "hold";
78
103
  } | {
@@ -182,10 +207,32 @@ export type Line = {
182
207
  system?: number;
183
208
  /** Set when the line's bars or structure differ from its system's (an error): its part is tacet there. */
184
209
  dropped?: true;
185
- } | {
210
+ }
211
+ /** A text line; `escaped` when written with a leading `\` (grammar 1), which `text` leaves off. */
212
+ | {
186
213
  kind: "text";
187
214
  line: number;
188
215
  text: string;
216
+ escaped?: true;
217
+ }
218
+ /** `---` alone (grammar 1, 7.4): ends a system, and means nothing else. */
219
+ | {
220
+ kind: "break";
221
+ line: number;
222
+ }
223
+ /**
224
+ * `lyrics:` or `lyrics N:` then a bar line (grammar 7.7): syllables on the
225
+ * beats, one cell per bar. `verse` is N, null when unnumbered. `covers`
226
+ * holds the source lines of the bar lines it lies under, in order: one,
227
+ * a system's lines, or under a replay heading the replayed section's
228
+ * line; empty when it covers none (an error).
229
+ */
230
+ | {
231
+ kind: "lyrics";
232
+ line: number;
233
+ verse: number | null;
234
+ cells: LyricSlot[][];
235
+ covers: number[];
189
236
  }
190
237
  /** `composer: …` and the other credit lines (grammar 7.5). */
191
238
  | {
@@ -248,6 +295,29 @@ export type PlayedBar = {
248
295
  parts?: PartBar[];
249
296
  /** A single part's lines (grammar 7.6), as on `PartBar`: in a single-part chart or an extracted part that writes lines. */
250
297
  lines?: LineBar[];
298
+ /**
299
+ * The words sung in this bar (grammar 4.3, 7.7), one entry per verse in
300
+ * verse order: on a repeat's pass N only verse N; absent when none.
301
+ */
302
+ lyrics?: PlayedLyrics[];
303
+ };
304
+ /**
305
+ * A syllable as played: `at` is its onset in whole notes from the bar's
306
+ * start; `note` is true when a note of the singing line starts there.
307
+ */
308
+ export type PlayedSyllable = {
309
+ at: number;
310
+ syllable: Syllable;
311
+ note: boolean;
312
+ };
313
+ /**
314
+ * One verse's syllables in a played bar; `part` is the label of the part
315
+ * singing (absent in a chart with no voice part).
316
+ */
317
+ export type PlayedLyrics = {
318
+ verse: number;
319
+ part?: string;
320
+ syllables: PlayedSyllable[];
251
321
  };
252
322
  /** One part's line's share of a played bar (grammar 7.6): its name, the line it is written on, its slots and words. */
253
323
  export type LineBar = {
@@ -294,9 +364,15 @@ export type SectionOccurrence = {
294
364
  */
295
365
  form: number;
296
366
  };
297
- /** A declared part as the parse reports it: `lines` names its lines in score order (grammar 7.6), present only when it writes some. */
367
+ /**
368
+ * A declared part as the parse reports it: `lines` names its lines in score
369
+ * order (grammar 7.6), present only when it writes some. `sings`, on the
370
+ * part that sings (grammar 7.7), names the line whose notes carry the
371
+ * lyrics, or is "" for its bare line; absent when it writes no notes.
372
+ */
298
373
  export type DeclaredPart = Part & {
299
374
  lines?: string[];
375
+ sings?: string;
300
376
  };
301
377
  export type ChartParse = {
302
378
  title: string | null;
@@ -0,0 +1,55 @@
1
+ import type { Sing } from "./expand.js";
2
+ import type { ChartDiagnostic, DeclaredPart, Line } from "./types.js";
3
+ type LyricsLine = Extract<Line, {
4
+ kind: "lyrics";
5
+ }>;
6
+ /**
7
+ * Who sings a chart's words (grammar 7.7): a part, by its label, and the
8
+ * line whose notes carry them (`sings`, "" for the bare line; absent when
9
+ * the part writes no notes). A chart with no parts whose notes carry
10
+ * syllables sings with no label.
11
+ */
12
+ export type Singer = {
13
+ part: DeclaredPart | null;
14
+ label?: string;
15
+ sings?: string;
16
+ };
17
+ /** The parts as the parse reports them: each that sings with the line it sings on. */
18
+ export declare function withSings(parts: DeclaredPart[], singers: Singer[]): DeclaredPart[];
19
+ /** Rows by what they cover: the replay heading they are written under ("" for none) and a covered line. */
20
+ type Rows = Map<string, LyricsLine[]>;
21
+ /**
22
+ * The words each played bar sings (grammar 4.3, 7.7), for `expand` to
23
+ * attach as it plays the line that sings: the syllables on its notes
24
+ * (verse 1) and the rows over it, or under a replay heading with rows for
25
+ * the line, that replay's rows alone; on a repeat's pass N, verse N. The
26
+ * singer's melody is checked as it plays: a row's syllable where no note
27
+ * starts, a `:/` with nothing sung before it to hold, and a pass that
28
+ * sings nothing where other passes sing, warn. `finish`, after playing,
29
+ * warns of each numbered row never sung.
30
+ */
31
+ declare function singing(rows: Rows, who: Singer | null): {
32
+ sing: Sing;
33
+ diagnostics: ChartDiagnostic[];
34
+ finish: () => void;
35
+ };
36
+ /** One voice that sings (grammar 7.7): its singer, null for the chart's words when no part sings, and its words. */
37
+ export type Voice = {
38
+ singer: Singer | null;
39
+ } & ReturnType<typeof singing>;
40
+ /**
41
+ * Every voice of a chart: each part that sings, the owner with the lyric
42
+ * rows; or, when no part sings, the chart's words alone. `diagnostics`
43
+ * are the rows' errors, `all` the parts that sing.
44
+ */
45
+ export declare function voicesOf(lines: Line[], parts: DeclaredPart[]): {
46
+ voices: Voice[];
47
+ all: Singer[];
48
+ owner: Singer | null;
49
+ diagnostics: ChartDiagnostic[];
50
+ };
51
+ /** The voices an extract of a part, or of one of its lines, sings: its own, and the chart's words when no part sings. */
52
+ export declare function voicesIn(voices: Voice[], part: DeclaredPart, lineName?: string): Voice[];
53
+ /** Several voices' words on one line (verse order, then the voices' order), or none. */
54
+ export declare function together(sings: Sing[]): Sing | undefined;
55
+ export {};
@@ -0,0 +1,351 @@
1
+ import { isBar } from "./barLine.js";
2
+ import { slotTimes } from "./shares.js";
3
+ /** Each simple slot of a bar line's cells. */
4
+ const simpleSlots = (l) => l.cells.flatMap((c) => c.kind === "slots" ? c.slots.flatMap((s) => (s.kind === "group" ? s.members : [s])) : []);
5
+ const isNote = (s) => s.kind === "chord" && s.chord.kind === "notes";
6
+ const hasSyllables = (l) => simpleSlots(l).some((s) => s.kind === "chord" && s.syllable);
7
+ const hasNotes = (l) => simpleSlots(l).some(isNote);
8
+ const same = (a, b) => a.toLowerCase() === b.toLowerCase();
9
+ /** A part's bar lines: those labelled with it, and in a single-part chart the unlabelled ones too. */
10
+ function linesOf(part, bars, alone) {
11
+ return bars.filter((l) => (l.part === undefined ? alone : same(l.part, part.label ?? "\u0000")));
12
+ }
13
+ /**
14
+ * The line a part sings on: its first line, in score order, whose notes
15
+ * carry syllables, else its first with notes. A bare line plays as the
16
+ * first line (grammar 7.6); a part with no lines sings on "".
17
+ */
18
+ function singingLine(part, own) {
19
+ const names = part.lines?.length ? part.lines : [""];
20
+ const first = names[0] ?? "";
21
+ const on = (name, test) => own.some((l) => same(l.lineName ?? first, name) && test(l));
22
+ return names.find((n) => on(n, hasSyllables)) ?? names.find((n) => on(n, hasNotes));
23
+ }
24
+ /**
25
+ * Who sings a chart (grammar 7.7). `owner` sings its lyric rows: the first
26
+ * part with `instrument: voice`, else the first part with syllables on its
27
+ * notes; in a chart with no parts, the chart itself when its notes carry
28
+ * syllables; null when no one sings, so a row's words are the chart's.
29
+ * `all` is every part that sings, in score order: the owner and each part
30
+ * with syllables on its notes, which are its own (a choir's parts).
31
+ */
32
+ function singersOf(lines, parts) {
33
+ const bars = lines.filter((l) => l.kind === "bars");
34
+ if (!parts.length) {
35
+ const owner = bars.some(hasSyllables) ? { part: null, sings: "" } : null;
36
+ return { owner, all: owner ? [owner] : [] };
37
+ }
38
+ const alone = parts.length === 1;
39
+ const sung = (p) => linesOf(p, bars, alone).some(hasSyllables);
40
+ const first = parts.find((p) => p.instrument === "voice") ?? parts.find(sung);
41
+ const all = parts
42
+ .filter((p) => p === first || sung(p))
43
+ .map((part) => {
44
+ const sings = singingLine(part, linesOf(part, bars, alone));
45
+ return {
46
+ part,
47
+ ...(part.label && { label: part.label }),
48
+ ...(sings !== undefined && { sings }),
49
+ };
50
+ });
51
+ return { owner: all.find((x) => x.part === first) ?? null, all };
52
+ }
53
+ /** The bar lines of what a singer sings on: its part's, or with no part every one. */
54
+ function singerLines(singer, bars, parts) {
55
+ if (!singer)
56
+ return [];
57
+ return singer.part ? linesOf(singer.part, bars, parts === 1) : bars;
58
+ }
59
+ /** The parts as the parse reports them: each that sings with the line it sings on. */
60
+ export function withSings(parts, singers) {
61
+ return parts.map((p) => {
62
+ const sings = singers.find((x) => x.part === p)?.sings;
63
+ return sings === undefined ? p : { ...p, sings };
64
+ });
65
+ }
66
+ const rowKey = (replay, line) => `${replay ?? ""}:${line}`;
67
+ /** Each row's group: a row under a replay heading covers lines before that heading. */
68
+ function groupsOf(lines) {
69
+ const groups = new Map();
70
+ let heading = null;
71
+ for (const l of lines) {
72
+ if (l.kind === "section")
73
+ heading = l.line;
74
+ const first = l.kind === "lyrics" ? l.covers[0] : undefined;
75
+ if (l.kind !== "lyrics" || first === undefined)
76
+ continue;
77
+ const replay = heading !== null && first < heading ? heading : null;
78
+ const key = rowKey(replay, first);
79
+ const group = groups.get(key) ?? { replay, rows: [] };
80
+ group.rows.push(l);
81
+ groups.set(key, group);
82
+ }
83
+ return [...groups.values()];
84
+ }
85
+ 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
+ const MIXED = "an unnumbered lyric row among numbered ones over the same bars; number every verse (lyrics 1:, lyrics 2:)";
87
+ /** 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) {
89
+ const verse = row.verse ?? 1;
90
+ if (sungOnNotes && verse === 1)
91
+ return NOTES_SING;
92
+ if (row.verse === null && group.rows.some((r) => r.verse !== null))
93
+ return MIXED;
94
+ const before = seen.get(verse);
95
+ if (before === undefined)
96
+ return null;
97
+ return row.verse === null
98
+ ? `these bars already have their words (line ${before}); number each verse`
99
+ : `verse ${verse} is already written over these bars (line ${before})`;
100
+ }
101
+ /**
102
+ * The lyric rows each bar line sings (grammar 7.7), by the replay heading
103
+ * they are written under and the line they cover, with an error for each
104
+ * row that would write a syllable twice: `lyrics 1:` or an unnumbered row
105
+ * under notes that carry syllables, an unnumbered row among numbered ones,
106
+ * or a verse written twice over the same bars. A row with an error, or a
107
+ * bar count other than its line's, sings nothing. Never throws.
108
+ */
109
+ function versesOf(lines, owner, parts) {
110
+ const diagnostics = [];
111
+ const rows = new Map();
112
+ const all = lines.filter((l) => l.kind === "bars");
113
+ const bars = new Map(all.map((l) => [l.line, l]));
114
+ // Only the owner's syllables are the rows' verse 1; another part's are its own.
115
+ 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 };
127
+ }
128
+ /** A group's rows that sing, each other with its error. */
129
+ function singingRows(group, notes, diagnostics) {
130
+ const sungOnNotes = group.replay === null && notes;
131
+ const seen = new Map();
132
+ return group.rows.filter((row) => {
133
+ const problem = rowProblem(row, group, sungOnNotes, seen);
134
+ if (problem)
135
+ diagnostics.push({ line: row.line, severity: "error", message: problem, token: "lyrics" });
136
+ else
137
+ seen.set(row.verse ?? 1, row.line);
138
+ return !problem;
139
+ });
140
+ }
141
+ /** Within a rounding error: onsets are sums of fractions of a bar. */
142
+ const near = (a, b) => Math.abs(a - b) < 1e-9;
143
+ /** 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 }] : []);
146
+ }
147
+ /** Where the bar's notes start. */
148
+ const noteOnsets = (bar) => slotTimes(bar.slots, bar.meter).flatMap(({ slot, at }) => (isNote(slot) ? [at] : []));
149
+ const HELD = { kind: "held" };
150
+ /**
151
+ * A row's cell as played: each syllable at its beat, on a note when one
152
+ * starts there; a `/` holding a syllable over a new note of the melody is
153
+ * held there, as `:/` is (4.3). Over a tie, after a breath, before its
154
+ * verse has sung anything, or where no note starts, a `/` sings nothing;
155
+ * `orphan` says a `/` over a new note found nothing to hold. `ends` is
156
+ * what the cell leaves held.
157
+ */
158
+ function onBeats(cell, bar, onsets, held) {
159
+ let state = held;
160
+ let orphan = false;
161
+ const syllables = slotTimes(cell, bar.meter).flatMap(({ slot, at }) => {
162
+ const note = onsets.some((o) => near(o, at));
163
+ if (slot.kind === "syllable") {
164
+ state = "sung";
165
+ return [{ at, syllable: slot.syllable, note }];
166
+ }
167
+ if (slot.kind === "breath")
168
+ state = "breath";
169
+ else if (state === "start" && note)
170
+ orphan = true;
171
+ return slot.kind === "hold" && state === "sung" && note ? [{ at, syllable: HELD, note }] : [];
172
+ });
173
+ return { syllables, orphan, ends: state };
174
+ }
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 : [];
197
+ }
198
+ 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)}`;
201
+ /** "verse 2", "verses 1 and 2": each verse once. */
202
+ const versesNamed = (ns) => {
203
+ const each = [...new Set(ns)];
204
+ return `${each.length === 1 ? "verse" : "verses"} ${listOf(each)}`;
205
+ };
206
+ const HOLDS_NOTHING = ":/ holds the syllable before it, and none is sung before it";
207
+ /** A warning reported once per line and message. */
208
+ function warner(diagnostics) {
209
+ const warned = new Set();
210
+ return (line, message, token) => {
211
+ if (warned.has(`${line}\u0000${message}`))
212
+ return;
213
+ warned.add(`${line}\u0000${message}`);
214
+ diagnostics.push({ line, severity: "warning", message, token });
215
+ };
216
+ }
217
+ /**
218
+ * The words each played bar sings (grammar 4.3, 7.7), for `expand` to
219
+ * attach as it plays the line that sings: the syllables on its notes
220
+ * (verse 1) and the rows over it, or under a replay heading with rows for
221
+ * the line, that replay's rows alone; on a repeat's pass N, verse N. The
222
+ * singer's melody is checked as it plays: a row's syllable where no note
223
+ * starts, a `:/` with nothing sung before it to hold, and a pass that
224
+ * sings nothing where other passes sing, warn. `finish`, after playing,
225
+ * warns of each numbered row never sung.
226
+ */
227
+ function singing(rows, who) {
228
+ const diagnostics = [];
229
+ const warn = warner(diagnostics);
230
+ const melody = who?.sings !== undefined;
231
+ const part = who?.label === undefined ? {} : { part: who.label };
232
+ const sung = new Set();
233
+ /** What each verse's rows left held, in play order: a row starting with `/` holds on from it. */
234
+ const ends = new Map();
235
+ let heard = false;
236
+ /** A row's verse in a bar, its syllables on the beats. */
237
+ const fromRow = (row, bar, from, onsets) => {
238
+ 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;
244
+ if (missed)
245
+ 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
+ };
253
+ };
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);
264
+ };
265
+ /** A `:/` holds the syllable sung before it, in play order. */
266
+ 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
+ }
273
+ };
274
+ /** 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");
276
+ /** Each written bar's notes' syllables, by their line, and whether a pass sang them. */
277
+ 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;
282
+ 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 });
285
+ };
286
+ /** 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
+ const sungRow = ({ row, verse, ends: left, orphan }) => {
288
+ if (!row)
289
+ return;
290
+ sung.add(row);
291
+ if (left)
292
+ ends.set(verse, left);
293
+ if (orphan)
294
+ warn(row.line, HOLDS_NOTHING, "/");
295
+ };
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));
303
+ for (const c of chosen)
304
+ sungRow(c);
305
+ return chosen.length ? chosen.map((c) => c.lyrics) : undefined;
306
+ };
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)));
312
+ for (const r of unsung)
313
+ warn(r.line, `verse ${r.verse} is never sung: no pass of its repeat is pass ${r.verse}`, "lyrics");
314
+ };
315
+ return { sing, diagnostics, finish };
316
+ }
317
+ /**
318
+ * Every voice of a chart: each part that sings, the owner with the lyric
319
+ * rows; or, when no part sings, the chart's words alone. `diagnostics`
320
+ * are the rows' errors, `all` the parts that sing.
321
+ */
322
+ export function voicesOf(lines, parts) {
323
+ const { owner, all } = singersOf(lines, parts);
324
+ const verses = versesOf(lines, owner, parts.length);
325
+ const singers = owner ? all : [null];
326
+ const voices = singers.map((singer) => ({
327
+ singer,
328
+ ...singing(singer === owner ? verses.rows : new Map(), singer),
329
+ }));
330
+ return { voices, all, owner, diagnostics: verses.diagnostics };
331
+ }
332
+ /** The voices an extract of a part, or of one of its lines, sings: its own, and the chart's words when no part sings. */
333
+ export function voicesIn(voices, part, lineName) {
334
+ return voices.filter(({ singer }) => {
335
+ if (!singer)
336
+ return true;
337
+ if (singer.part !== part)
338
+ return false;
339
+ const line = singer.sings ?? part.lines?.[0] ?? "";
340
+ return lineName === undefined || same(lineName, line);
341
+ });
342
+ }
343
+ /** Several voices' words on one line (verse order, then the voices' order), or none. */
344
+ export function together(sings) {
345
+ if (sings.length < 2)
346
+ return sings[0];
347
+ return (bar, from) => {
348
+ const all = sings.flatMap((sing) => sing(bar, from) ?? []);
349
+ return all.length ? all.sort((a, b) => a.verse - b.verse) : undefined;
350
+ };
351
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "barmark",
3
- "version": "0.16.0",
3
+ "version": "0.17.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": {