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.
@@ -32,11 +32,19 @@ function nameLines(lines, report) {
32
32
  });
33
33
  return { lines: out, names };
34
34
  }
35
+ /** Push `item` onto the list at `key`, starting it if new: a copy per item would cost the list squared. */
36
+ function pushAt(map, key, ...items) {
37
+ const known = map.get(key);
38
+ if (known)
39
+ known.push(...items);
40
+ else
41
+ map.set(key, items);
42
+ }
35
43
  /** A system's lines by part, in written order. */
36
44
  function byPart(system) {
37
45
  const out = new Map();
38
46
  for (const l of system)
39
- out.set(l.part ?? "", [...(out.get(l.part ?? "") ?? []), l]);
47
+ pushAt(out, l.part ?? "", l);
40
48
  return out;
41
49
  }
42
50
  /**
@@ -45,12 +53,17 @@ function byPart(system) {
45
53
  * they first appear.
46
54
  */
47
55
  function scoreOrder(names, systems) {
56
+ // Each part's first system writing two or more of its lines, found in one pass over the systems.
57
+ const full = new Map();
58
+ for (const system of systems)
59
+ for (const [part, own] of system) {
60
+ const named = own.flatMap((l) => l.lineName ?? []);
61
+ if (named.length > 1 && !full.has(part))
62
+ full.set(part, named);
63
+ }
48
64
  const out = new Map();
49
65
  for (const [part, all] of names) {
50
- const full = systems
51
- .map((s) => (byPart(s).get(part) ?? []).flatMap((l) => l.lineName ?? []))
52
- .find((n) => n.length > 1);
53
- const first = full ?? [];
66
+ const first = full.get(part) ?? [];
54
67
  out.set(part, [...first, ...all.filter((n) => !first.includes(n))]);
55
68
  }
56
69
  return out;
@@ -62,10 +75,10 @@ function scoreOrder(names, systems) {
62
75
  function bareBeside(systems) {
63
76
  const out = new Map();
64
77
  for (const system of systems)
65
- for (const [part, own] of byPart(system)) {
78
+ for (const [part, own] of system) {
66
79
  const bare = own.filter((l) => l.lineName === undefined);
67
80
  if (bare.length && bare.length < own.length)
68
- out.set(part, [...(out.get(part) ?? []), ...bare]);
81
+ pushAt(out, part, ...bare);
69
82
  }
70
83
  return out;
71
84
  }
@@ -116,9 +129,11 @@ export function checkLines(lines, parts) {
116
129
  const systems = new Map();
117
130
  for (const l of named.lines)
118
131
  if (isLabelled(l) && l.system !== undefined && !failed.has(l.line))
119
- systems.set(l.system, [...(systems.get(l.system) ?? []), l]);
120
- const bare = bareBeside([...systems.values()]);
121
- const order = withBare(scoreOrder(named.names, [...systems.values()]), bare, report);
132
+ pushAt(systems, l.system, l);
133
+ // Each system's lines by part, once: not once for every part.
134
+ const parted = [...systems.values()].map(byPart);
135
+ const bare = bareBeside(parted);
136
+ const order = withBare(scoreOrder(named.names, parted), bare, report);
122
137
  for (const system of systems.values())
123
138
  checkSystem(system.filter((l) => !failed.has(l.line)), order, report);
124
139
  const out = named.lines.map((l) => l.kind === "bars" && failed.has(l.line) ? { ...l, dropped: true } : l);
@@ -84,12 +84,22 @@ const isRow = (l) => l.kind === "lyrics" || (l.kind === "text" && !l.escaped &&
84
84
  const isCovered = (l) => l.kind === "bars" || l.kind === "invalid";
85
85
  const normalize = (name) => name.toLowerCase().replace(/\s+/g, " ").trim();
86
86
  const plural = (n, what) => `${n} ${what}${n === 1 ? "" : "s"}`;
87
+ /** Each unit's reference line and its bar count, found once however many rows lie under it. */
88
+ const referenceOf = new WeakMap();
89
+ function unitReference(unit) {
90
+ if (referenceOf.has(unit))
91
+ return referenceOf.get(unit) ?? null;
92
+ const ref = unit.find((l) => l.kind === "bars" && !l.dropped);
93
+ const found = ref ? { ref, n: ref.cells.filter(isBar).length } : null;
94
+ referenceOf.set(unit, found);
95
+ return found;
96
+ }
87
97
  /** Why a row can't lie under a unit, or null: its bar count must be the unit's. */
88
98
  function countProblem(row, unit) {
89
- const ref = unit.find((l) => l.kind === "bars" && !l.dropped);
90
- if (ref?.kind !== "bars")
99
+ const found = unitReference(unit);
100
+ if (!found)
91
101
  return null;
92
- const n = ref.cells.filter(isBar).length;
102
+ const { ref, n } = found;
93
103
  if (row.cells.length === n)
94
104
  return null;
95
105
  return `this lyric row has ${plural(row.cells.length, "bar")}; the bar line it covers has ${n} (line ${ref.line})`;
@@ -161,6 +171,8 @@ function placeRows(s, definitions) {
161
171
  export function attachLyrics(lines) {
162
172
  const diagnostics = [];
163
173
  const covers = new Map();
174
+ // One list of lines for each unit, shared by its rows: a copy per row costs the rows times the unit's lines.
175
+ const linesOf = new Map();
164
176
  const definitions = new Map();
165
177
  const placed = sectionsOf(lines).flatMap((s) => placeRows(s, definitions));
166
178
  // A row with an error of its own (kept as text) only holds its place.
@@ -170,8 +182,11 @@ export function attachLyrics(lines) {
170
182
  const why = unit ? countProblem(row, unit) : problem;
171
183
  if (why)
172
184
  diagnostics.push({ line: row.line, severity: "error", message: why, token: "lyrics" });
173
- if (unit)
174
- covers.set(row, unit.map((l) => l.line));
185
+ if (!unit)
186
+ continue;
187
+ const own = linesOf.get(unit) ?? unit.map((l) => l.line);
188
+ linesOf.set(unit, own);
189
+ covers.set(row, own);
175
190
  }
176
191
  const out = lines.map((l) => (l.kind === "lyrics" ? { ...l, covers: covers.get(l) ?? [] } : l));
177
192
  return { lines: out, diagnostics };
@@ -4,7 +4,9 @@ import type { ChartDiagnostic, ChartEvent, ChartParse } from "./types.js";
4
4
  * line of a system shares its structure. Where they don't, the lead's
5
5
  * shape is kept and the first place they part is an error.
6
6
  */
7
- export declare function zipEvents(each: ChartEvent[][], report: (d: ChartDiagnostic) => void): ChartEvent[];
7
+ export declare function zipEvents(each: ChartEvent[][], report: (d: ChartDiagnostic) => void,
8
+ /** A part that stopped at a limit falls out of step because it stopped: the stop says so. */
9
+ stopped?: boolean): ChartEvent[];
8
10
  /** Read a whole barmark chart: the document as written and the bars in play order. Never throws. */
9
11
  export declare function parseChart(text: string): ChartParse;
10
12
  /**
@@ -1,7 +1,8 @@
1
1
  import { readDocument } from "./document.js";
2
- import { expand, flattenBars } from "./expand.js";
2
+ import { expand, flattenBars, workFor } from "./expand.js";
3
3
  import { projectPart, projectPartLines } from "./parts.js";
4
4
  import { pedalWarnings } from "./pedal.js";
5
+ import { numberFromPickup } from "./pickup.js";
5
6
  import { together, voicesIn, voicesOf, withSings } from "./verses.js";
6
7
  const RANK = { error: 0, warning: 1 };
7
8
  /** Sorted by line, errors before warnings. */
@@ -12,8 +13,13 @@ function mergeReports(each) {
12
13
  const most = new Map();
13
14
  for (const list of each) {
14
15
  const mine = new Map();
15
- for (const d of list)
16
- mine.set(keyOf(d), [...(mine.get(keyOf(d)) ?? []), d]);
16
+ for (const d of list) {
17
+ const known = mine.get(keyOf(d));
18
+ if (known)
19
+ known.push(d);
20
+ else
21
+ mine.set(keyOf(d), [d]);
22
+ }
17
23
  for (const [k, ds] of mine)
18
24
  if (ds.length > (most.get(k)?.length ?? 0))
19
25
  most.set(k, ds);
@@ -68,11 +74,13 @@ function stepAt(each) {
68
74
  * line of a system shares its structure. Where they don't, the lead's
69
75
  * shape is kept and the first place they part is an error.
70
76
  */
71
- export function zipEvents(each, report) {
77
+ export function zipEvents(each, report,
78
+ /** A part that stopped at a limit falls out of step because it stopped: the stop says so. */
79
+ stopped = false) {
72
80
  const [lead = []] = each;
73
81
  const apart = stepAt(each);
74
82
  const at = lead[apart] ?? lead[lead.length - 1];
75
- if (apart >= 0 && at)
83
+ if (apart >= 0 && at && !stopped)
76
84
  report({
77
85
  line: lineOf(at),
78
86
  severity: "error",
@@ -89,16 +97,25 @@ export function zipEvents(each, report) {
89
97
  if (e.kind === "open")
90
98
  return {
91
99
  ...e,
92
- events: zipEvents(peers.map((p) => (p.kind === "open" ? p.events : e.events)), report),
100
+ events: zipEvents(peers.map((p) => (p.kind === "open" ? p.events : e.events)), report, stopped),
93
101
  };
94
102
  return e;
95
103
  });
96
104
  }
97
105
  /** How a stream's line is labelled in its messages: `Pno.RH`, or the part's label for its bare line. */
98
106
  const lineLabel = (label, name) => (name ? `${label}.${name}` : label);
99
- const streamsOf = (shown) => shown.flatMap((p, part) => p.lines?.length
107
+ const streamsOf = (shown) => shown
108
+ .flatMap((p, part) => p.lines?.length
100
109
  ? p.lines.map((name) => ({ part, label: p.label ?? "", name }))
101
- : [{ part, label: p.label ?? "", name: null }]);
110
+ : [{ part, label: p.label ?? "", name: null }])
111
+ .map((s, index) => ({ ...s, index }));
112
+ /** Each shown part's streams, gathered once rather than looked for in every bar. */
113
+ function byPart(streams, shown) {
114
+ const out = shown.map(() => []);
115
+ for (const s of streams)
116
+ out[s.part]?.push(s);
117
+ return out;
118
+ }
102
119
  /** A part's share of a bar from its streams' shares: its first playing line leads (grammar 7.6). */
103
120
  function partOf(p, own) {
104
121
  const playing = own.filter((e) => e[1] !== null);
@@ -118,7 +135,7 @@ function partOf(p, own) {
118
135
  /** A zipped bar of streams as a bar of parts; with one part shown, its lines sit on the bar itself. */
119
136
  function foldBar(bar, streams, shown) {
120
137
  const each = bar.parts ?? [];
121
- const parts = shown.map((p, i) => partOf(p, streams.flatMap((s, j) => (s.part === i ? [[s, each[j] ?? null]] : []))));
138
+ const parts = shown.map((p, i) => partOf(p, (streams[i] ?? []).map((s) => [s, each[s.index] ?? null])));
122
139
  const { parts: _p, words: _w, tacet: _t, ...shared } = bar;
123
140
  const lead = parts[0];
124
141
  const top = lead
@@ -150,18 +167,37 @@ function linedPedalWarnings(events, shown) {
150
167
  const bars = flattenBars(events);
151
168
  return shown.flatMap((p, i) => p.lines?.length ? pedalWarnings(bars.map((b) => partView(b, i, shown.length))) : []);
152
169
  }
170
+ /** Each part's streams, in order, by its label lower-cased. */
171
+ function streamsByLabel(streams) {
172
+ const by = new Map();
173
+ for (const s of streams) {
174
+ const label = s.label.toLowerCase();
175
+ const known = by.get(label);
176
+ if (known)
177
+ known.push(s);
178
+ else
179
+ by.set(label, [s]);
180
+ }
181
+ return by;
182
+ }
153
183
  /** The stream a voice sings on: its singing line, else its part's first; the first shown for the chart's words. */
154
- function singingStream(streams, singer) {
184
+ function singingStream(by, singer) {
155
185
  const label = singer?.label?.toLowerCase();
156
186
  if (label === undefined)
157
187
  return 0;
158
- const own = (s) => s.label.toLowerCase() === label;
159
- const at = streams.findIndex((s) => own(s) && (s.name ?? "").toLowerCase() === singer?.sings?.toLowerCase());
160
- return at >= 0 ? at : streams.findIndex(own);
188
+ const own = by.get(label) ?? [];
189
+ const sings = singer?.sings?.toLowerCase();
190
+ const line = own.find((s) => (s.name ?? "").toLowerCase() === sings);
191
+ return (line ?? own[0])?.index ?? -1;
161
192
  }
162
193
  /** Each stream's words: the voices singing on it, together. */
163
194
  function singsOn(streams, voices) {
164
- return streams.map((_, i) => together(voices.filter((v) => singingStream(streams, v.singer) === i).map((v) => v.sing)));
195
+ // Streams indexed once: finding each voice's stream by a scan costs streams times voices.
196
+ const by = streamsByLabel(streams);
197
+ const on = streams.map(() => []);
198
+ for (const v of voices)
199
+ on[singingStream(by, v.singer)]?.push(v.sing);
200
+ return on.map(together);
165
201
  }
166
202
  /**
167
203
  * Parts and their lines: each stream projected and played, zipped into one
@@ -171,18 +207,19 @@ function singsOn(streams, voices) {
171
207
  function playStreams(lines, parts, shown, voices) {
172
208
  const streams = streamsOf(shown);
173
209
  const sings = singsOn(streams, voices);
174
- const played = streams.map((s, i) => expand(projectPart(lines, parts, s.label, s.name), lineLabel(s.label, s.name), streams.length, s.name === null, sings[i]));
210
+ const played = streams.map((s, i) => expand(projectPart(lines, parts, s.label, s.name, workFor(streams.length)), lineLabel(s.label, s.name), streams.length, s.name === null, sings[i]));
175
211
  const diagnostics = mergeReports(played.map((p) => p.diagnostics));
176
- const zipped = zipEvents(played.map((p) => p.events), (d) => diagnostics.push(d));
177
- const events = foldEvents(zipped, streams, shown);
212
+ const stopped = played.some((p) => p.stopped);
213
+ const zipped = zipEvents(played.map((p) => p.events), (d) => diagnostics.push(d), stopped);
214
+ const events = foldEvents(zipped, byPart(streams, shown), shown);
178
215
  diagnostics.push(...linedPedalWarnings(events, shown));
179
- return { events, sections: played[0]?.sections ?? [], diagnostics };
216
+ return { events, sections: played[0]?.sections ?? [], diagnostics, stopped };
180
217
  }
181
218
  const lined = (parts) => parts.some((p) => p.lines?.length);
182
- /** What the voices found as they sang (grammar 7.7), once playing is done. */
183
- function sungChecks(voices) {
219
+ /** What the voices found as they sang (grammar 7.7), once playing is done; past a stop, not what it left unsung. */
220
+ function sungChecks(voices, stopped) {
184
221
  return voices.flatMap((v) => {
185
- v.finish();
222
+ v.finish(stopped);
186
223
  return v.diagnostics;
187
224
  });
188
225
  }
@@ -194,6 +231,8 @@ export function parseChart(text) {
194
231
  ? playStreams(doc.lines, doc.parts, doc.parts, voice.voices)
195
232
  : expand(doc.lines, undefined, 1, true, together(voice.voices.map((v) => v.sing)));
196
233
  const [part = null] = doc.part ? withSings([doc.part], voice.all) : [];
234
+ const bars = flattenBars(played.events);
235
+ const pickup = numberFromPickup(bars);
197
236
  return {
198
237
  title: doc.title,
199
238
  part,
@@ -201,13 +240,14 @@ export function parseChart(text) {
201
240
  credits: doc.credits,
202
241
  lines: doc.lines,
203
242
  events: played.events,
204
- bars: flattenBars(played.events),
243
+ bars,
244
+ pickup,
205
245
  sections: played.sections,
206
246
  diagnostics: sortDiagnostics([
207
247
  ...doc.diagnostics,
208
248
  ...voice.diagnostics,
209
249
  ...played.diagnostics,
210
- ...sungChecks(voice.voices),
250
+ ...sungChecks(voice.voices, played.stopped),
211
251
  ]),
212
252
  };
213
253
  }
@@ -249,6 +289,8 @@ export function extractPart(text, label, lineName) {
249
289
  if (!part || !extract)
250
290
  return null;
251
291
  const { played } = extract;
292
+ const bars = flattenBars(played.events);
293
+ const pickup = numberFromPickup(bars);
252
294
  // The lyric rows are their owner's, or the chart's when no part sings (grammar 7.7).
253
295
  const rows = voices.some((v) => v.singer === voice.owner);
254
296
  const lines = rows ? extract.lines : extract.lines.filter((l) => l.kind !== "lyrics");
@@ -264,12 +306,13 @@ export function extractPart(text, label, lineName) {
264
306
  credits: doc.credits,
265
307
  lines,
266
308
  events: played.events,
267
- bars: flattenBars(played.events),
309
+ bars,
310
+ pickup,
268
311
  sections: played.sections,
269
312
  diagnostics: sortDiagnostics([
270
313
  ...[...doc.diagnostics, ...voice.diagnostics].filter((d) => kept.has(d.line)),
271
314
  ...played.diagnostics,
272
- ...sungChecks(voices),
315
+ ...sungChecks(voices, played.stopped),
273
316
  ]),
274
317
  };
275
318
  }
@@ -7,6 +7,8 @@ import type { DeclaredPart, Line } from "./types.js";
7
7
  * its bare lines and the tutti lines, and a line rests (tacet) wherever
8
8
  * it isn't written. Never throws.
9
9
  */
10
- export declare function projectPart(lines: Line[], parts: DeclaredPart[], label: string, lineName?: string | null): Line[];
10
+ export declare function projectPart(lines: Line[], parts: DeclaredPart[], label: string, lineName?: string | null,
11
+ /** Past this many lines the part reads no more (`expand`'s share of the work), so none past it is built. */
12
+ limit?: number): Line[];
11
13
  /** One part's lines kept as written, labels and all, with a stand-in for each system it doesn't play: what an extract prints (grammar 7.6). */
12
14
  export declare function projectPartLines(lines: Line[], label: string): Line[];
@@ -11,13 +11,19 @@ function copyAlong(cells, before, out) {
11
11
  }
12
12
  return meter;
13
13
  }
14
+ /** Each chart's copied meters, worked out once however many parts are projected from it. */
15
+ const copiedOf = new WeakMap();
14
16
  /**
15
17
  * What each `%` copies its meter from: the bar before it on its part's
16
18
  * line (a part's line, or a tutti line, which every part plays), across
17
19
  * line breaks, so a stand-in for it is a bar of the same length.
18
20
  */
19
21
  function copiedMeters(lines) {
22
+ const known = copiedOf.get(lines);
23
+ if (known)
24
+ return known;
20
25
  const out = new Map();
26
+ copiedOf.set(lines, out);
21
27
  const last = new Map();
22
28
  let tutti = null;
23
29
  for (const l of lines) {
@@ -60,7 +66,7 @@ const referenceOf = (system) => system.find((l) => l.kind === "bars" && !l.dropp
60
66
  * or else the reference line, which has none.
61
67
  */
62
68
  function standIn(system, at, copied) {
63
- const ref = referenceOf(system);
69
+ const ref = system.reference;
64
70
  if (!ref)
65
71
  return unread(at);
66
72
  const { part: _p, system: _s, dropped: _d, ...rest } = ref;
@@ -77,28 +83,116 @@ function unlabelled(l) {
77
83
  }
78
84
  const plays = (l) => l?.kind === "bars" && !l.dropped;
79
85
  /** The stream's own line in a system, if written; a bare line dropped from a part's lines is none of them (grammar 7.6). */
80
- const ownLine = (system, s) => system.find((m) => m.part?.toLowerCase() === s.key &&
81
- (s.name === null
82
- ? true
83
- : m.lineName === undefined
84
- ? s.first && !(m.kind === "bars" && m.dropped)
85
- : m.lineName.toLowerCase() === s.name.toLowerCase()));
86
+ const findOwn = (system, s) => (system.byPart.get(s.key) ?? []).find((m) => s.name === null
87
+ ? true
88
+ : m.lineName === undefined
89
+ ? s.first && !(m.kind === "bars" && m.dropped)
90
+ : m.lineName.toLowerCase() === s.name);
91
+ function ownLine(system, s) {
92
+ if (s.own.has(system))
93
+ return s.own.get(system);
94
+ const own = findOwn(system, s);
95
+ s.own.set(system, own);
96
+ return own;
97
+ }
86
98
  /** A tutti line as a stream other than its part's first hears it: tacet. */
87
99
  const restingTutti = (l, copied) => l.kind === "bars" ? { ...l, cells: l.cells.map(standInCell(copied)) } : l;
88
100
  const inSystem = (l) => (l.kind === "bars" || l.kind === "invalid") && l.system !== undefined;
101
+ /** Each chart's systems, indexed once however many parts are projected from it. */
102
+ const systemsKnown = new WeakMap();
89
103
  function systemsOf(lines) {
104
+ const known = systemsKnown.get(lines);
105
+ if (known)
106
+ return known;
90
107
  const systems = new Map();
91
- for (const l of lines)
92
- if (inSystem(l))
93
- systems.set(l.system, [...(systems.get(l.system) ?? []), l]);
108
+ for (const l of lines) {
109
+ if (!inSystem(l))
110
+ continue;
111
+ let system = systems.get(l.system);
112
+ if (!system) {
113
+ system = { lines: [], byPart: new Map(), reference: undefined };
114
+ systems.set(l.system, system);
115
+ }
116
+ system.lines.push(l);
117
+ const key = l.part?.toLowerCase();
118
+ if (key === undefined)
119
+ continue;
120
+ const own = system.byPart.get(key);
121
+ if (own)
122
+ own.push(l);
123
+ else
124
+ system.byPart.set(key, [l]);
125
+ }
126
+ for (const system of systems.values())
127
+ system.reference = referenceOf(system.lines);
128
+ systemsKnown.set(lines, systems);
94
129
  return systems;
95
130
  }
131
+ const NO_SYSTEM = { lines: [], byPart: new Map(), reference: undefined };
132
+ /** The line a system's stand-ins are written at, for a part that has no line of its own there. */
133
+ const anchorOf = (system) => system.reference ?? system.lines[0];
134
+ const readingKnown = new WeakMap();
135
+ /** The part a line belongs to, by lowercased label; undefined for a line every stream reads. */
136
+ const partKey = (l) => l.kind === "part"
137
+ ? (l.label?.toLowerCase() ?? "")
138
+ : inSystem(l)
139
+ ? l.part?.toLowerCase()
140
+ : undefined;
141
+ function readingOf(lines, systems) {
142
+ const known = readingKnown.get(lines);
143
+ if (known)
144
+ return known;
145
+ const reading = { common: [], byPart: new Map() };
146
+ lines.forEach((l, i) => {
147
+ const anchored = inSystem(l) && anchorOf(systems.get(l.system) ?? NO_SYSTEM) === l;
148
+ if (anchored || (l.kind !== "part" && !inSystem(l)))
149
+ reading.common.push(i);
150
+ const key = partKey(l);
151
+ if (key === undefined)
152
+ return;
153
+ const own = reading.byPart.get(key);
154
+ if (own)
155
+ own.push(i);
156
+ else
157
+ reading.byPart.set(key, [i]);
158
+ });
159
+ readingKnown.set(lines, reading);
160
+ return reading;
161
+ }
162
+ /** Two ascending lists of indices merged, each index once. */
163
+ function* inOrder(a, b) {
164
+ let i = 0;
165
+ let j = 0;
166
+ while (i < a.length || j < b.length) {
167
+ const at = Math.min(a[i] ?? Number.POSITIVE_INFINITY, b[j] ?? Number.POSITIVE_INFINITY);
168
+ if (a[i] === at)
169
+ i++;
170
+ if (b[j] === at)
171
+ j++;
172
+ yield at;
173
+ }
174
+ }
175
+ const labelsKnown = new WeakMap();
176
+ /** The declared parts by lowercased label, the first of each; indexed once however many parts are projected. */
177
+ function partsByLabel(parts) {
178
+ const known = labelsKnown.get(parts);
179
+ if (known)
180
+ return known;
181
+ const by = new Map();
182
+ for (const p of parts) {
183
+ const key = p.label?.toLowerCase();
184
+ if (key !== undefined && !by.has(key))
185
+ by.set(key, p);
186
+ }
187
+ labelsKnown.set(parts, by);
188
+ return by;
189
+ }
96
190
  /** What one line becomes in the stream's chart: itself, its unlabelled self, a stand-in, or nothing. */
97
191
  function projectLine(l, s, systems) {
98
192
  if (l.kind === "part")
99
193
  return l.label?.toLowerCase() === s.key ? l : null;
100
194
  if (inSystem(l))
101
- return projectSystemLine(l, s, systems.get(l.system) ?? []);
195
+ return projectSystemLine(l, s, systems.get(l.system) ?? NO_SYSTEM);
102
196
  return s.first ? l : restingTutti(l, s.copied);
103
197
  }
104
198
  /** A system's line in one stream's chart: its own line, a stand-in where it rests, or nothing. */
@@ -106,7 +200,7 @@ function projectSystemLine(l, s, system) {
106
200
  const own = ownLine(system, s);
107
201
  if (own === l && plays(l))
108
202
  return unlabelled(l);
109
- const at = own ?? referenceOf(system) ?? system[0];
203
+ const at = own ?? anchorOf(system);
110
204
  return at === l ? standIn(system, l, s.copied) : null;
111
205
  }
112
206
  /**
@@ -117,20 +211,39 @@ function projectSystemLine(l, s, system) {
117
211
  * its bare lines and the tutti lines, and a line rests (tacet) wherever
118
212
  * it isn't written. Never throws.
119
213
  */
120
- export function projectPart(lines, parts, label, lineName = null) {
214
+ export function projectPart(lines, parts, label, lineName = null,
215
+ /** Past this many lines the part reads no more (`expand`'s share of the work), so none past it is built. */
216
+ limit = Number.POSITIVE_INFINITY) {
121
217
  if (!parts.length)
122
218
  return lines;
123
219
  const key = label.toLowerCase();
124
- const part = parts.find((p) => p.label?.toLowerCase() === key);
125
- const first = part?.lines?.[0];
220
+ const first = partsByLabel(parts).get(key)?.lines?.[0];
126
221
  const s = {
127
222
  key,
128
- name: lineName,
223
+ name: lineName?.toLowerCase() ?? null,
129
224
  first: lineName === null || lineName === first,
130
225
  copied: copiedMeters(lines),
226
+ own: new Map(),
131
227
  };
132
228
  const systems = systemsOf(lines);
133
- return lines.map((l) => projectLine(l, s, systems)).filter((l) => l !== null);
229
+ const { common, byPart } = readingOf(lines, systems);
230
+ const out = [];
231
+ for (const i of inOrder(common, byPart.get(key) ?? [])) {
232
+ const projected = projectLine(lines[i], s, systems);
233
+ if (projected)
234
+ out.push(projected);
235
+ // One past the limit, so the reader knows there was more.
236
+ if (out.length > limit)
237
+ break;
238
+ }
239
+ return out;
240
+ }
241
+ /** A system's line in a part's extract: its own lines as written, or one stand-in where it doesn't play. */
242
+ function extractSystemLine(l, key, system, copied) {
243
+ const own = system.byPart.get(key) ?? [];
244
+ if (own.length)
245
+ return own.includes(l) ? l : null;
246
+ return anchorOf(system) === l ? standIn(system, l, copied) : null;
134
247
  }
135
248
  /** One part's lines kept as written, labels and all, with a stand-in for each system it doesn't play: what an extract prints (grammar 7.6). */
136
249
  export function projectPartLines(lines, label) {
@@ -143,12 +256,7 @@ export function projectPartLines(lines, label) {
143
256
  return l.label?.toLowerCase() === key ? l : null;
144
257
  if (!inSystem(l))
145
258
  return l;
146
- const system = systems.get(l.system) ?? [];
147
- const own = system.filter((m) => m.part?.toLowerCase() === key);
148
- if (own.length)
149
- return own.includes(l) ? l : null;
150
- const at = referenceOf(system) ?? system[0];
151
- return at === l ? standIn(system, l, copied) : null;
259
+ return extractSystemLine(l, key, systems.get(l.system) ?? NO_SYSTEM, copied);
152
260
  })
153
261
  .filter((l) => l !== null);
154
262
  }
@@ -1,5 +1,9 @@
1
1
  import type { ChartDiagnostic, PlayedBar } from "./types.js";
2
2
  /** Whether the pedal is down at the end of each slot of each bar, in play order. The chart's end lifts it. */
3
3
  export declare function pedalStates(bars: PlayedBar[]): boolean[][];
4
- /** A warning for each `*` played with the pedal already up, once per line. */
4
+ /**
5
+ * A warning for each `*` played with the pedal already up, once per line.
6
+ * One pass over the bars with the pedal's state; each written bar's slots
7
+ * are walked at most twice however often it plays.
8
+ */
5
9
  export declare function pedalWarnings(bars: PlayedBar[]): ChartDiagnostic[];
@@ -1,40 +1,73 @@
1
1
  const pedalOf = (s) => (s.kind === "chord" ? s.pedal : undefined);
2
2
  const membersOf = (s) => (s.kind === "group" ? s.members : [s]);
3
3
  /**
4
- * Walk a part's bars in play order with the pedal (grammar 2.9): `+`
5
- * presses or changes it, `*` lifts it, and so do `--` and a tacet bar.
6
- * `onRelease` hears each `*`, with whether the pedal was down.
4
+ * One slot's effect on the pedal (grammar 2.9): `+` presses or changes it,
5
+ * `*` lifts it, and so does `--`. `onRelease` hears each `*`, with whether
6
+ * the pedal was down.
7
7
  */
8
- function walk(bars, onRelease = () => { }) {
8
+ function step(slot, down, onRelease) {
9
+ let now = down;
10
+ for (const m of membersOf(slot)) {
11
+ const pedal = pedalOf(m);
12
+ if (pedal === "release")
13
+ onRelease(now);
14
+ if (pedal)
15
+ now = pedal === "press";
16
+ else if (m.kind === "silence")
17
+ now = false;
18
+ }
19
+ return now;
20
+ }
21
+ /** Whether the pedal is down at the end of each slot of each bar, in play order. The chart's end lifts it. */
22
+ export function pedalStates(bars) {
9
23
  let down = false;
10
24
  return bars.map((bar) => {
25
+ // A tacet bar lifts the pedal.
11
26
  if (bar.tacet)
12
27
  down = false;
13
28
  return bar.slots.map((slot) => {
14
- for (const m of membersOf(slot)) {
15
- const pedal = pedalOf(m);
16
- if (pedal === "release")
17
- onRelease(bar, down);
18
- if (pedal)
19
- down = pedal === "press";
20
- else if (m.kind === "silence")
21
- down = false;
22
- }
29
+ down = step(slot, down, () => { });
23
30
  return down && !bar.tacet;
24
31
  });
25
32
  });
26
33
  }
27
- /** Whether the pedal is down at the end of each slot of each bar, in play order. The chart's end lifts it. */
28
- export function pedalStates(bars) {
29
- return walk(bars);
34
+ /** Each written bar's slots walked once from each state: a bar played again shares its walk. */
35
+ function effects() {
36
+ const seen = new WeakMap();
37
+ const walk = (slots, from) => {
38
+ let idle = false;
39
+ let down = from;
40
+ for (const slot of slots)
41
+ down = step(slot, down, (was) => {
42
+ if (!was)
43
+ idle = true;
44
+ });
45
+ return { end: down, idle };
46
+ };
47
+ return (slots, down) => {
48
+ let both = seen.get(slots);
49
+ if (!both) {
50
+ both = [walk(slots, false), walk(slots, true)];
51
+ seen.set(slots, both);
52
+ }
53
+ return both[down ? 1 : 0];
54
+ };
30
55
  }
31
- /** A warning for each `*` played with the pedal already up, once per line. */
56
+ /**
57
+ * A warning for each `*` played with the pedal already up, once per line.
58
+ * One pass over the bars with the pedal's state; each written bar's slots
59
+ * are walked at most twice however often it plays.
60
+ */
32
61
  export function pedalWarnings(bars) {
33
62
  const lines = new Set();
34
- walk(bars, (bar, wasDown) => {
35
- if (!wasDown)
63
+ const effect = effects();
64
+ let down = false;
65
+ for (const bar of bars) {
66
+ const e = effect(bar.slots, down && !bar.tacet);
67
+ if (e.idle)
36
68
  lines.add(bar.line);
37
- });
69
+ down = e.end;
70
+ }
38
71
  return [...lines].map((line) => ({
39
72
  line,
40
73
  severity: "warning",