barmark 0.15.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.
- package/CHANGELOG.md +145 -1
- package/README.md +49 -0
- package/dist/chart/barLine.js +130 -18
- package/dist/chart/credits.d.ts +1 -1
- package/dist/chart/credits.js +5 -1
- package/dist/chart/document.d.ts +3 -3
- package/dist/chart/document.js +95 -51
- package/dist/chart/expand.d.ts +19 -1
- package/dist/chart/expand.js +51 -16
- package/dist/chart/index.d.ts +5 -0
- package/dist/chart/index.js +5 -0
- package/dist/chart/instruments.d.ts +27 -0
- package/dist/chart/instruments.js +48 -0
- package/dist/chart/lines.d.ts +14 -0
- package/dist/chart/lines.js +130 -0
- package/dist/chart/lyricRows.d.ts +25 -0
- package/dist/chart/lyricRows.js +178 -0
- package/dist/chart/parse.d.ts +8 -3
- package/dist/chart/parse.js +158 -20
- package/dist/chart/partLine.d.ts +3 -0
- package/dist/chart/partLine.js +73 -0
- package/dist/chart/parts.d.ts +8 -3
- package/dist/chart/parts.js +50 -14
- package/dist/chart/pedal.d.ts +5 -0
- package/dist/chart/pedal.js +44 -0
- package/dist/chart/print.js +151 -26
- package/dist/chart/repeats.js +3 -1
- package/dist/chart/shares.d.ts +28 -0
- package/dist/chart/shares.js +31 -0
- package/dist/chart/syllable.d.ts +21 -0
- package/dist/chart/syllable.js +32 -0
- package/dist/chart/systems.d.ts +2 -0
- package/dist/chart/systems.js +19 -3
- package/dist/chart/types.d.ts +116 -8
- package/dist/chart/verses.d.ts +55 -0
- package/dist/chart/verses.js +351 -0
- package/package.json +1 -1
|
@@ -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
|
+
}
|