barmark 0.21.0 → 0.23.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.
@@ -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",
@@ -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;
@@ -0,0 +1,41 @@
1
+ /** Whether meter `a` lasts less than `b`, by value (`[4/4]` and 4/4 are the same length). */
2
+ const shorter = (a, b) => a.n * b.d < b.n * a.d;
3
+ /**
4
+ * The rule of grammar 2.4 on bars numbered from 1, as `expand` plays them:
5
+ * the first written bar is in a one-bar meter shorter than the meter in
6
+ * force at the next written bar, and that bar is not itself in a one-bar
7
+ * meter shorter than its `time:`. A bar line dropped for an error writes
8
+ * bars no played bar has, so a chart whose first or second written bar is
9
+ * on one has no pickup.
10
+ */
11
+ function pickupRule(bars) {
12
+ const first = bars.find((b) => b.written === 1);
13
+ const next = bars.find((b) => b.written === 2);
14
+ if (!first?.time || !next)
15
+ return false;
16
+ if (next.time && shorter(next.meter, next.time))
17
+ return false;
18
+ return shorter(first.meter, next.meter);
19
+ }
20
+ /**
21
+ * Whether a chart's played bars open with a pickup (grammar 2.4), for a
22
+ * reader holding only the bars. Accepts both numberings: bars as
23
+ * `parseChart` and `extractPart` give them (the pickup is the bar written
24
+ * 0), and bars as `expand` gives them, counting from 1.
25
+ * `ChartParse.pickup` says the same.
26
+ */
27
+ export function opensWithPickup(bars) {
28
+ return bars.some((b) => b.written === 0) || pickupRule(bars);
29
+ }
30
+ /**
31
+ * Number a chart's bars from 0 when it opens with a pickup (grammar 2.4):
32
+ * every played bar's `written` drops by one (each played bar is its own
33
+ * object, so none drops twice). Returns whether it did.
34
+ */
35
+ export function numberFromPickup(bars) {
36
+ if (!pickupRule(bars))
37
+ return false;
38
+ for (const b of bars)
39
+ b.written -= 1;
40
+ return true;
41
+ }
@@ -1,4 +1,4 @@
1
- import type { Cell, ChartParse, Line, Slot } from "./types.js";
1
+ import type { Cell, ChartParse, Line, SimpleSlot, Slot } from "./types.js";
2
2
  /**
3
3
  * The fewest slots with the same timeline: `G / D /` is `G D`, `-- --` is `--`.
4
4
  * Only a slot with no attack of its own merges (`/`, a repeated `--` or
@@ -30,6 +30,16 @@ export declare function printChart(text: string): string;
30
30
  * text isn't read twice. `edit`, if given, changes each line before it prints.
31
31
  */
32
32
  export declare function printParsed(text: string, parsed: ChartParse, edit?: (l: Line) => Line): string;
33
+ /** The unreadable slots of a bar line, or of any cells (grammar 2.3): unknown harmony that keeps its text. */
34
+ export declare const unreadSlots: (l: {
35
+ cells: Cell[];
36
+ }) => SimpleSlot[];
37
+ /**
38
+ * The lines that print as written (spec §8): every line with an error,
39
+ * except a bar line whose only errors are its unreadable slots, one each,
40
+ * which prints in canonical form with each slot's text as written.
41
+ */
42
+ export declare function printedAsWritten(parsed: ChartParse): Set<number>;
33
43
  /**
34
44
  * One part of a chart in canonical form, as a single-part chart (grammar
35
45
  * 7.4): its `part:` line, the tutti lines, its own lines, and `N.C.` bars