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.
@@ -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
+ }
@@ -189,9 +189,25 @@ const labelsOf = (p) => [
189
189
  p.label ?? "",
190
190
  ...(p.lines ?? []).map((n) => (n ? `${p.label}.${n}` : (p.label ?? ""))),
191
191
  ];
192
+ /** The parts' places by label (the first of each) and their widest label, found once for every system printed. */
193
+ const partsKnown = new WeakMap();
194
+ function partsIndex(parts) {
195
+ const known = partsKnown.get(parts);
196
+ if (known)
197
+ return known;
198
+ const at = new Map();
199
+ parts.forEach((p, i) => {
200
+ if (!at.has(p.label))
201
+ at.set(p.label, i);
202
+ });
203
+ const width = Math.max(...parts.flatMap(labelsOf).map((label) => label.length));
204
+ const made = { at, width };
205
+ partsKnown.set(parts, made);
206
+ return made;
207
+ }
192
208
  /** Score order: by part, then by the part's line, a bare line first. */
193
209
  function scoreOf(l, parsed) {
194
- const at = parsed.parts.findIndex((p) => p.label === l.part);
210
+ const at = partsIndex(parsed.parts).at.get(l.part) ?? -1;
195
211
  const line = l.lineName === undefined ? -1 : (parsed.parts[at]?.lines ?? []).indexOf(l.lineName);
196
212
  return at * 10 + line + 1;
197
213
  }
@@ -236,9 +252,7 @@ function alignBlock(lines, rows, bars, width) {
236
252
  function alignSystem(block, parsed, beatsFor, edit) {
237
253
  const system = block.lines;
238
254
  const labelled = system[0]?.system !== undefined;
239
- const width = labelled
240
- ? Math.max(...parsed.parts.flatMap(labelsOf).map((label) => label.length))
241
- : 0;
255
+ const width = labelled ? partsIndex(parsed.parts).width : 0;
242
256
  const sorted = labelled
243
257
  ? [...system].sort((a, b) => scoreOf(a, parsed) - scoreOf(b, parsed))
244
258
  : system;
@@ -252,13 +266,37 @@ function alignSystem(block, parsed, beatsFor, edit) {
252
266
  const bars = (system[0]?.cells ?? []).flatMap((c, i) => (isBar(c) ? [i] : []));
253
267
  return alignBlock(pieces, rows, bars, width);
254
268
  }
255
- /** A row lies just under its bars unless a heading comes between: under a replay heading it doesn't. */
256
- const isUnder = (r, lines) => {
269
+ /**
270
+ * Whether a row lies just under its bars: not when a heading comes
271
+ * between (under a replay heading it doesn't). `headings` are the
272
+ * headings' line numbers in order, searched by halves, so many rows
273
+ * print in linear time.
274
+ */
275
+ const isUnder = (r, headings) => {
257
276
  if (!r.covers.length)
258
277
  return false;
259
- const last = Math.max(...r.covers);
260
- return !lines.some((l) => l.kind === "section" && l.line > last && l.line < r.line);
278
+ const last = lastOf(r.covers);
279
+ let lo = 0;
280
+ let hi = headings.length;
281
+ while (lo < hi) {
282
+ const mid = (lo + hi) >> 1;
283
+ if ((headings[mid] ?? 0) <= last)
284
+ lo = mid + 1;
285
+ else
286
+ hi = mid;
287
+ }
288
+ return !((headings[lo] ?? Number.POSITIVE_INFINITY) < r.line);
261
289
  };
290
+ /** The last line a row covers; rows share one list per unit, so it is found once for all of them. */
291
+ const lastKnown = new WeakMap();
292
+ function lastOf(covers) {
293
+ let last = lastKnown.get(covers);
294
+ if (last === undefined) {
295
+ last = Math.max(...covers);
296
+ lastKnown.set(covers, last);
297
+ }
298
+ return last;
299
+ }
262
300
  /** The block a bar line is in: its system's, or its own. */
263
301
  const blockKey = (l) => (l.kind === "bars" || l.kind === "invalid") && l.system !== undefined
264
302
  ? `system ${l.system}`
@@ -275,11 +313,19 @@ function blocksOf(lines) {
275
313
  for (const l of lines.filter(inSystem))
276
314
  blockFor(blocks, l).lines.push(l);
277
315
  const byLine = new Map(lines.map((l) => [l.line, l]));
316
+ const headings = lines.flatMap((l) => (l.kind === "section" ? [l.line] : []));
317
+ // Rows share one list of covered lines per unit: its first kept line is found once.
318
+ const firstOf = new Map();
319
+ const firstKept = (covers) => {
320
+ if (!firstOf.has(covers))
321
+ firstOf.set(covers, covers.map((n) => byLine.get(n)).find((l) => l !== undefined));
322
+ return firstOf.get(covers);
323
+ };
278
324
  for (const r of lines) {
279
- if (r.kind !== "lyrics" || !isUnder(r, lines))
325
+ if (r.kind !== "lyrics" || !isUnder(r, headings))
280
326
  continue;
281
327
  // An extracted part keeps only some of a system's lines.
282
- const first = r.covers.map((n) => byLine.get(n)).find((l) => l !== undefined);
328
+ const first = firstKept(r.covers);
283
329
  if (!first)
284
330
  continue;
285
331
  const block = blockFor(blocks, first);
@@ -312,16 +358,17 @@ function systemTexts(parsed, errored, beatsFor, edit) {
312
358
  }
313
359
  /** Each line's beat counts on its first play, every part's lines included. */
314
360
  function beatsOfLines(parsed) {
315
- const byLine = new Map(parsed.lines.map((l) => [l.line, l]));
361
+ // Each line's bar count, once: counting it again for every bar played costs the line's bars squared.
362
+ const cellsOf = new Map(parsed.lines.map((l) => [l.line, l.kind === "bars" ? l.cells.filter(isBar).length : 0]));
316
363
  const firstPlay = new Map();
317
364
  for (const b of parsed.bars) {
318
365
  const lines = new Set([b.line, ...(b.parts ?? []).flatMap((p) => (p ? [p.line] : []))]);
319
366
  for (const line of lines) {
320
- const l = byLine.get(line);
321
367
  const meters = firstPlay.get(line) ?? [];
322
- const cells = l?.kind === "bars" ? l.cells.filter(isBar).length : 0;
323
- if (meters.length < cells)
324
- firstPlay.set(line, [...meters, beatsOf(b.meter)]);
368
+ if (meters.length < (cellsOf.get(line) ?? 0)) {
369
+ meters.push(beatsOf(b.meter));
370
+ firstPlay.set(line, meters);
371
+ }
325
372
  }
326
373
  }
327
374
  return (l) => {
@@ -25,4 +25,11 @@ export declare function slotTimes<T extends Member>(slots: (T | {
25
25
  kind: "group";
26
26
  members: T[];
27
27
  })[], meter: Meter): Timed<T>[];
28
+ /**
29
+ * `make` remembered by its written data (compared by identity) and the
30
+ * meter it plays in: a bar played again shares what its first play made,
31
+ * so a chart's cost is its written size plus its plays, never their
32
+ * product. Callers never mutate what it returns.
33
+ */
34
+ export declare function perWritten<K extends object, T>(make: (written: K, meter: Meter) => T): (written: K, meter: Meter) => T;
28
35
  export {};
@@ -29,3 +29,23 @@ export function slotTimes(slots, meter) {
29
29
  }
30
30
  return out;
31
31
  }
32
+ /**
33
+ * `make` remembered by its written data (compared by identity) and the
34
+ * meter it plays in: a bar played again shares what its first play made,
35
+ * so a chart's cost is its written size plus its plays, never their
36
+ * product. Callers never mutate what it returns.
37
+ */
38
+ export function perWritten(make) {
39
+ const seen = new WeakMap();
40
+ return (written, meter) => {
41
+ let byMeter = seen.get(written);
42
+ if (!byMeter) {
43
+ byMeter = new Map();
44
+ seen.set(written, byMeter);
45
+ }
46
+ const key = `${meter.n}/${meter.d}`;
47
+ if (!byMeter.has(key))
48
+ byMeter.set(key, make(written, meter));
49
+ return byMeter.get(key);
50
+ };
51
+ }
@@ -73,14 +73,30 @@ function numberSystems(lines) {
73
73
  */
74
74
  function referenceLines(lines) {
75
75
  const bySystem = new Map();
76
- for (const l of lines)
77
- if (l.kind === "bars" && l.system !== undefined)
78
- bySystem.set(l.system, [...(bySystem.get(l.system) ?? []), l]);
76
+ for (const l of lines) {
77
+ if (l.kind !== "bars" || l.system === undefined)
78
+ continue;
79
+ const members = bySystem.get(l.system);
80
+ if (members)
81
+ members.push(l);
82
+ else
83
+ bySystem.set(l.system, [l]);
84
+ }
79
85
  const refs = new Map();
80
86
  for (const [system, members] of bySystem) {
81
- const votes = (l) => members.filter((m) => structureOf(m) === structureOf(l)).length;
82
- const best = members.reduce((a, b) => (votes(b) > votes(a) ? b : a));
83
- refs.set(system, best);
87
+ // Each structure counted once, not each line against every other.
88
+ const shapes = members.map(structureOf);
89
+ const votes = new Map();
90
+ for (const shape of shapes)
91
+ votes.set(shape, (votes.get(shape) ?? 0) + 1);
92
+ let best = 0;
93
+ shapes.forEach((shape, i) => {
94
+ if ((votes.get(shape) ?? 0) > (votes.get(shapes[best] ?? "") ?? 0))
95
+ best = i;
96
+ });
97
+ const ref = members[best];
98
+ if (ref)
99
+ refs.set(system, ref);
84
100
  }
85
101
  return refs;
86
102
  }
@@ -255,18 +255,30 @@ export type Line = {
255
255
  system?: number;
256
256
  };
257
257
  export type PlayedBar = {
258
- /** 1-based measure number in play order; fermatas are not counted. */
258
+ /**
259
+ * 1-based position in play order (not a bar number: a replay gives a bar
260
+ * a new position); fermatas are not counted.
261
+ */
259
262
  index: number;
260
263
  /**
261
- * 1-based number of the bar as written, counting each written bar once
262
- * (`%` included, fermatas not) in written order: a repeat or replay plays
263
- * its bars under the numbers they were written with.
264
+ * The bar's number as written, as `parseChart` and `extractPart` number
265
+ * it (`expand` counts from 1): 0 for a pickup (grammar 2.4), else
266
+ * counting from 1, each written bar once (`%` included, fermatas not) in
267
+ * written order: a repeat or replay plays its bars under the numbers they
268
+ * were written with.
264
269
  */
265
270
  written: number;
266
271
  line: number;
267
272
  /** Heading text of the section occurrence, as written. */
268
273
  section: string | null;
269
274
  meter: Meter;
275
+ /**
276
+ * The `time:` meter in force, present only when a one-bar meter gives
277
+ * this bar its own, a `%` copying a bracketed bar included (grammar
278
+ * 2.4): `tempo:` counts its beat, not the bar's, so a short or odd bar
279
+ * never changes speed.
280
+ */
281
+ time?: Meter;
270
282
  key: Key | null;
271
283
  /** The steady tempo: where a change starts from, and what `a tempo` returns to. */
272
284
  tempo: number | null;
@@ -390,6 +402,8 @@ export type ChartParse = {
390
402
  events: ChartEvent[];
391
403
  /** `events` flattened, with an open repeat's pass included once. */
392
404
  bars: PlayedBar[];
405
+ /** True when the chart's first written bar is a pickup (grammar 2.4); it is then written bar 0. */
406
+ pickup: boolean;
393
407
  sections: SectionOccurrence[];
394
408
  /** Sorted by line, then errors before warnings. */
395
409
  diagnostics: ChartDiagnostic[];
@@ -16,8 +16,15 @@ export type Singer = {
16
16
  };
17
17
  /** The parts as the parse reports them: each that sings with the line it sings on. */
18
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[]>;
19
+ /**
20
+ * The rows that sing: `over` gives those over a bar line under a replay
21
+ * heading (null for none), or undefined; `all` is every row over some
22
+ * line, in written order.
23
+ */
24
+ type Rows = {
25
+ over: (replay: number | null, line: number) => LyricsLine[] | undefined;
26
+ all: LyricsLine[];
27
+ };
21
28
  /**
22
29
  * The words each played bar sings (grammar 4.3, 7.7), for `expand` to
23
30
  * attach as it plays the line that sings: the syllables on its notes
@@ -27,11 +34,15 @@ type Rows = Map<string, LyricsLine[]>;
27
34
  * starts, a `:/` with nothing sung before it to hold, and a pass that
28
35
  * sings nothing where other passes sing, warn. `finish`, after playing,
29
36
  * warns of each numbered row never sung.
37
+ *
38
+ * A bar played again shares what its first play read (its notes' and its
39
+ * rows' syllables, by the written slots, the meter and what each row
40
+ * holds as it begins): a pass costs only the verses it sings.
30
41
  */
31
42
  declare function singing(rows: Rows, who: Singer | null): {
32
43
  sing: Sing;
33
44
  diagnostics: ChartDiagnostic[];
34
- finish: () => void;
45
+ finish: (stopped?: boolean) => void;
35
46
  };
36
47
  /** One voice that sings (grammar 7.7): its singer, null for the chart's words when no part sings, and its words. */
37
48
  export type Voice = {