barmark 0.20.0 → 0.22.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -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);
@@ -25,14 +31,15 @@ const meterText = (b) => `${b.meter.n}/${b.meter.d}`;
25
31
  /** Every stream's words in a bar, in verse order, then score order (grammar 7.7). */
26
32
  const lyricsOf = (bars) => bars.flatMap((b) => b.lyrics ?? []).sort((a, b) => a.verse - b.verse);
27
33
  /**
28
- * One bar of every part: shared fields from the first, the meter from the
29
- * first that plays, the words of every stream that sings.
34
+ * One bar of every part: shared fields from the first, the meter and the
35
+ * `time:` meter (grammar 2.4) together from the first that plays, the
36
+ * words of every stream that sings.
30
37
  */
31
38
  function zipBar(bars, report) {
32
39
  const [first, ...rest] = bars;
33
40
  const lyrics = lyricsOf(bars);
34
41
  const playing = bars.filter((b) => !b.tacet);
35
- const meter = playing[0]?.meter ?? first.meter;
42
+ const lead = playing[0] ?? first;
36
43
  const meters = [...new Set(playing.map(meterText))];
37
44
  // A `%` copies its bar's one-bar meter, so parts can disagree: say so on every line that plays the bar.
38
45
  if (meters.length > 1)
@@ -43,10 +50,11 @@ function zipBar(bars, report) {
43
50
  message: `the parts disagree on this bar's meter (${meters.join(", ")})`,
44
51
  token: "|",
45
52
  });
46
- const { lyrics: _l, ...shared } = first;
53
+ const { lyrics: _l, time: _t, ...shared } = first;
47
54
  return {
48
55
  ...shared,
49
- meter,
56
+ meter: lead.meter,
57
+ ...(lead.time && { time: lead.time }),
50
58
  parts: [first, ...rest].map(partBar),
51
59
  ...(lyrics.length && { lyrics }),
52
60
  };
@@ -66,11 +74,13 @@ function stepAt(each) {
66
74
  * line of a system shares its structure. Where they don't, the lead's
67
75
  * shape is kept and the first place they part is an error.
68
76
  */
69
- 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) {
70
80
  const [lead = []] = each;
71
81
  const apart = stepAt(each);
72
82
  const at = lead[apart] ?? lead[lead.length - 1];
73
- if (apart >= 0 && at)
83
+ if (apart >= 0 && at && !stopped)
74
84
  report({
75
85
  line: lineOf(at),
76
86
  severity: "error",
@@ -87,16 +97,25 @@ export function zipEvents(each, report) {
87
97
  if (e.kind === "open")
88
98
  return {
89
99
  ...e,
90
- 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),
91
101
  };
92
102
  return e;
93
103
  });
94
104
  }
95
105
  /** How a stream's line is labelled in its messages: `Pno.RH`, or the part's label for its bare line. */
96
106
  const lineLabel = (label, name) => (name ? `${label}.${name}` : label);
97
- const streamsOf = (shown) => shown.flatMap((p, part) => p.lines?.length
107
+ const streamsOf = (shown) => shown
108
+ .flatMap((p, part) => p.lines?.length
98
109
  ? p.lines.map((name) => ({ part, label: p.label ?? "", name }))
99
- : [{ 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
+ }
100
119
  /** A part's share of a bar from its streams' shares: its first playing line leads (grammar 7.6). */
101
120
  function partOf(p, own) {
102
121
  const playing = own.filter((e) => e[1] !== null);
@@ -116,7 +135,7 @@ function partOf(p, own) {
116
135
  /** A zipped bar of streams as a bar of parts; with one part shown, its lines sit on the bar itself. */
117
136
  function foldBar(bar, streams, shown) {
118
137
  const each = bar.parts ?? [];
119
- 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])));
120
139
  const { parts: _p, words: _w, tacet: _t, ...shared } = bar;
121
140
  const lead = parts[0];
122
141
  const top = lead
@@ -148,18 +167,37 @@ function linedPedalWarnings(events, shown) {
148
167
  const bars = flattenBars(events);
149
168
  return shown.flatMap((p, i) => p.lines?.length ? pedalWarnings(bars.map((b) => partView(b, i, shown.length))) : []);
150
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
+ }
151
183
  /** The stream a voice sings on: its singing line, else its part's first; the first shown for the chart's words. */
152
- function singingStream(streams, singer) {
184
+ function singingStream(by, singer) {
153
185
  const label = singer?.label?.toLowerCase();
154
186
  if (label === undefined)
155
187
  return 0;
156
- const own = (s) => s.label.toLowerCase() === label;
157
- const at = streams.findIndex((s) => own(s) && (s.name ?? "").toLowerCase() === singer?.sings?.toLowerCase());
158
- 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;
159
192
  }
160
193
  /** Each stream's words: the voices singing on it, together. */
161
194
  function singsOn(streams, voices) {
162
- 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);
163
201
  }
164
202
  /**
165
203
  * Parts and their lines: each stream projected and played, zipped into one
@@ -169,18 +207,19 @@ function singsOn(streams, voices) {
169
207
  function playStreams(lines, parts, shown, voices) {
170
208
  const streams = streamsOf(shown);
171
209
  const sings = singsOn(streams, voices);
172
- 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]));
173
211
  const diagnostics = mergeReports(played.map((p) => p.diagnostics));
174
- const zipped = zipEvents(played.map((p) => p.events), (d) => diagnostics.push(d));
175
- 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);
176
215
  diagnostics.push(...linedPedalWarnings(events, shown));
177
- return { events, sections: played[0]?.sections ?? [], diagnostics };
216
+ return { events, sections: played[0]?.sections ?? [], diagnostics, stopped };
178
217
  }
179
218
  const lined = (parts) => parts.some((p) => p.lines?.length);
180
- /** What the voices found as they sang (grammar 7.7), once playing is done. */
181
- 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) {
182
221
  return voices.flatMap((v) => {
183
- v.finish();
222
+ v.finish(stopped);
184
223
  return v.diagnostics;
185
224
  });
186
225
  }
@@ -192,6 +231,8 @@ export function parseChart(text) {
192
231
  ? playStreams(doc.lines, doc.parts, doc.parts, voice.voices)
193
232
  : expand(doc.lines, undefined, 1, true, together(voice.voices.map((v) => v.sing)));
194
233
  const [part = null] = doc.part ? withSings([doc.part], voice.all) : [];
234
+ const bars = flattenBars(played.events);
235
+ const pickup = numberFromPickup(bars);
195
236
  return {
196
237
  title: doc.title,
197
238
  part,
@@ -199,13 +240,14 @@ export function parseChart(text) {
199
240
  credits: doc.credits,
200
241
  lines: doc.lines,
201
242
  events: played.events,
202
- bars: flattenBars(played.events),
243
+ bars,
244
+ pickup,
203
245
  sections: played.sections,
204
246
  diagnostics: sortDiagnostics([
205
247
  ...doc.diagnostics,
206
248
  ...voice.diagnostics,
207
249
  ...played.diagnostics,
208
- ...sungChecks(voice.voices),
250
+ ...sungChecks(voice.voices, played.stopped),
209
251
  ]),
210
252
  };
211
253
  }
@@ -247,6 +289,8 @@ export function extractPart(text, label, lineName) {
247
289
  if (!part || !extract)
248
290
  return null;
249
291
  const { played } = extract;
292
+ const bars = flattenBars(played.events);
293
+ const pickup = numberFromPickup(bars);
250
294
  // The lyric rows are their owner's, or the chart's when no part sings (grammar 7.7).
251
295
  const rows = voices.some((v) => v.singer === voice.owner);
252
296
  const lines = rows ? extract.lines : extract.lines.filter((l) => l.kind !== "lyrics");
@@ -262,12 +306,13 @@ export function extractPart(text, label, lineName) {
262
306
  credits: doc.credits,
263
307
  lines,
264
308
  events: played.events,
265
- bars: flattenBars(played.events),
309
+ bars,
310
+ pickup,
266
311
  sections: played.sections,
267
312
  diagnostics: sortDiagnostics([
268
313
  ...[...doc.diagnostics, ...voice.diagnostics].filter((d) => kept.has(d.line)),
269
314
  ...played.diagnostics,
270
- ...sungChecks(voices),
315
+ ...sungChecks(voices, played.stopped),
271
316
  ]),
272
317
  };
273
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[];
@@ -1,8 +1,49 @@
1
- /** A bar of `N.C.` where the part rests; the system's marks stay (grammar 7.4). */
2
- const standInCell = (c) => c.kind === "slots" || c.kind === "simile"
1
+ /** The stream a labelled line plays (its part, and its line's name), or null for a tutti line. */
2
+ const streamOf = (l) => l.part === undefined ? null : `${l.part}\u0000${l.lineName ?? ""}`.toLowerCase();
3
+ /** Each `%` on a line gets the meter of the bar before it; returns the meter of the line's last bar. */
4
+ function copyAlong(cells, before, out) {
5
+ let meter = before;
6
+ for (const c of cells) {
7
+ if (c.kind === "simile")
8
+ out.set(c, meter);
9
+ else if (c.kind === "slots")
10
+ meter = c.meter;
11
+ }
12
+ return meter;
13
+ }
14
+ /** Each chart's copied meters, worked out once however many parts are projected from it. */
15
+ const copiedOf = new WeakMap();
16
+ /**
17
+ * What each `%` copies its meter from: the bar before it on its part's
18
+ * line (a part's line, or a tutti line, which every part plays), across
19
+ * line breaks, so a stand-in for it is a bar of the same length.
20
+ */
21
+ function copiedMeters(lines) {
22
+ const known = copiedOf.get(lines);
23
+ if (known)
24
+ return known;
25
+ const out = new Map();
26
+ copiedOf.set(lines, out);
27
+ const last = new Map();
28
+ let tutti = null;
29
+ for (const l of lines) {
30
+ if (l.kind !== "bars")
31
+ continue;
32
+ const key = streamOf(l);
33
+ if (key === null) {
34
+ tutti = copyAlong(l.cells, tutti, out);
35
+ last.clear();
36
+ }
37
+ else
38
+ last.set(key, copyAlong(l.cells, last.has(key) ? (last.get(key) ?? null) : tutti, out));
39
+ }
40
+ return out;
41
+ }
42
+ /** A bar of `N.C.` where the part rests, as long as the bar it stands for; the system's marks stay (grammar 7.4). */
43
+ const standInCell = (copied) => (c) => c.kind === "slots" || c.kind === "simile"
3
44
  ? {
4
45
  kind: "slots",
5
- meter: c.kind === "slots" ? c.meter : null,
46
+ meter: c.kind === "slots" ? c.meter : (copied.get(c) ?? null),
6
47
  slots: [{ kind: "nc" }],
7
48
  tacet: true,
8
49
  }
@@ -24,12 +65,12 @@ const referenceOf = (system) => system.find((l) => l.kind === "bars" && !l.dropp
24
65
  * part's own line when it has one that errs, so its error stays its own,
25
66
  * or else the reference line, which has none.
26
67
  */
27
- function standIn(system, at) {
28
- const ref = referenceOf(system);
68
+ function standIn(system, at, copied) {
69
+ const ref = system.reference;
29
70
  if (!ref)
30
71
  return unread(at);
31
72
  const { part: _p, system: _s, dropped: _d, ...rest } = ref;
32
- return { ...rest, line: at.line, cells: ref.cells.map(standInCell) };
73
+ return { ...rest, line: at.line, cells: ref.cells.map(standInCell(copied)) };
33
74
  }
34
75
  /** A part's own line, as a single-part chart writes it. */
35
76
  function unlabelled(l) {
@@ -42,37 +83,125 @@ function unlabelled(l) {
42
83
  }
43
84
  const plays = (l) => l?.kind === "bars" && !l.dropped;
44
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). */
45
- const ownLine = (system, s) => system.find((m) => m.part?.toLowerCase() === s.key &&
46
- (s.name === null
47
- ? true
48
- : m.lineName === undefined
49
- ? s.first && !(m.kind === "bars" && m.dropped)
50
- : 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
+ }
51
98
  /** A tutti line as a stream other than its part's first hears it: tacet. */
52
- const restingTutti = (l) => l.kind === "bars" ? { ...l, cells: l.cells.map(standInCell) } : l;
99
+ const restingTutti = (l, copied) => l.kind === "bars" ? { ...l, cells: l.cells.map(standInCell(copied)) } : l;
53
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();
54
103
  function systemsOf(lines) {
104
+ const known = systemsKnown.get(lines);
105
+ if (known)
106
+ return known;
55
107
  const systems = new Map();
56
- for (const l of lines)
57
- if (inSystem(l))
58
- 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);
59
129
  return systems;
60
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
+ }
61
190
  /** What one line becomes in the stream's chart: itself, its unlabelled self, a stand-in, or nothing. */
62
191
  function projectLine(l, s, systems) {
63
192
  if (l.kind === "part")
64
193
  return l.label?.toLowerCase() === s.key ? l : null;
65
194
  if (inSystem(l))
66
- return projectSystemLine(l, s, systems.get(l.system) ?? []);
67
- return s.first ? l : restingTutti(l);
195
+ return projectSystemLine(l, s, systems.get(l.system) ?? NO_SYSTEM);
196
+ return s.first ? l : restingTutti(l, s.copied);
68
197
  }
69
198
  /** A system's line in one stream's chart: its own line, a stand-in where it rests, or nothing. */
70
199
  function projectSystemLine(l, s, system) {
71
200
  const own = ownLine(system, s);
72
201
  if (own === l && plays(l))
73
202
  return unlabelled(l);
74
- const at = own ?? referenceOf(system) ?? system[0];
75
- return at === l ? standIn(system, l) : null;
203
+ const at = own ?? anchorOf(system);
204
+ return at === l ? standIn(system, l, s.copied) : null;
76
205
  }
77
206
  /**
78
207
  * One part of a multi-part chart as a single-part chart's lines (grammar
@@ -82,32 +211,52 @@ function projectSystemLine(l, s, system) {
82
211
  * its bare lines and the tutti lines, and a line rests (tacet) wherever
83
212
  * it isn't written. Never throws.
84
213
  */
85
- 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) {
86
217
  if (!parts.length)
87
218
  return lines;
88
219
  const key = label.toLowerCase();
89
- const part = parts.find((p) => p.label?.toLowerCase() === key);
90
- const first = part?.lines?.[0];
91
- const s = { key, name: lineName, first: lineName === null || lineName === first };
220
+ const first = partsByLabel(parts).get(key)?.lines?.[0];
221
+ const s = {
222
+ key,
223
+ name: lineName?.toLowerCase() ?? null,
224
+ first: lineName === null || lineName === first,
225
+ copied: copiedMeters(lines),
226
+ own: new Map(),
227
+ };
92
228
  const systems = systemsOf(lines);
93
- 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;
94
247
  }
95
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). */
96
249
  export function projectPartLines(lines, label) {
97
250
  const key = label.toLowerCase();
98
251
  const systems = systemsOf(lines);
252
+ const copied = copiedMeters(lines);
99
253
  return lines
100
254
  .map((l) => {
101
255
  if (l.kind === "part")
102
256
  return l.label?.toLowerCase() === key ? l : null;
103
257
  if (!inSystem(l))
104
258
  return l;
105
- const system = systems.get(l.system) ?? [];
106
- const own = system.filter((m) => m.part?.toLowerCase() === key);
107
- if (own.length)
108
- return own.includes(l) ? l : null;
109
- const at = referenceOf(system) ?? system[0];
110
- return at === l ? standIn(system, l) : null;
259
+ return extractSystemLine(l, key, systems.get(l.system) ?? NO_SYSTEM, copied);
111
260
  })
112
261
  .filter((l) => l !== null);
113
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",
@@ -0,0 +1,15 @@
1
+ import type { PlayedBar } from "./types.js";
2
+ /**
3
+ * Whether a chart's played bars open with a pickup (grammar 2.4), for a
4
+ * reader holding only the bars. Accepts both numberings: bars as
5
+ * `parseChart` and `extractPart` give them (the pickup is the bar written
6
+ * 0), and bars as `expand` gives them, counting from 1.
7
+ * `ChartParse.pickup` says the same.
8
+ */
9
+ export declare function opensWithPickup(bars: PlayedBar[]): boolean;
10
+ /**
11
+ * Number a chart's bars from 0 when it opens with a pickup (grammar 2.4):
12
+ * every played bar's `written` drops by one (each played bar is its own
13
+ * object, so none drops twice). Returns whether it did.
14
+ */
15
+ export declare function numberFromPickup(bars: PlayedBar[]): boolean;