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.
package/CHANGELOG.md CHANGED
@@ -9,6 +9,156 @@ Releases follow the process in [RELEASING.md](RELEASING.md).
9
9
 
10
10
  ## [Unreleased]
11
11
 
12
+ ## [0.22.0] - 2026-11-09
13
+
14
+ A grammar change: a chart's pickup is bar 0 (2.4), which now defines a
15
+ pickup. Charts without a pickup read and number as before. Implements the
16
+ [grammar v0.1 draft](https://github.com/kevinpeckham/barmark/blob/main/docs/grammar-v0.1-draft.md).
17
+
18
+ ### Added
19
+
20
+ - **`ChartParse.pickup`**: true when the chart's first written bar is a
21
+ pickup (grammar 2.4: in a one-bar meter shorter, by value, than the
22
+ meter in force at the next written bar, and that bar not short itself).
23
+ **`opensWithPickup(bars)`** gives the same answer from played bars
24
+ alone. The grammar now defines a pickup.
25
+
26
+ ### Changed
27
+
28
+ - **A pickup is bar 0** (grammar 2.4): in a chart that opens with one,
29
+ every played bar's `written` is one less than before, so bar 1 is the
30
+ first full bar, as notation and the MusicXML export number it; a repeat,
31
+ replay or D.C. that plays the pickup again shows 0 too. Charts without
32
+ a pickup are numbered as before. `index`, the play position, does not
33
+ change, so times keyed by it stay valid.
34
+
35
+ - **A line's passes that sing nothing are one warning.** "pass 3 of this
36
+ repeat sings nothing: its bars have verses 1 and 2" stays as it was for
37
+ one pass; several are now listed together, a run of three or more as a
38
+ range ("passes 1 and 3–9 of this repeat sing nothing: …"), and a list
39
+ past five items shows four and counts the rest ("verses 1, 2, 3, 4 and
40
+ 2496 more"). 2,500 verses over a bar played 10,000 times gave 7,500
41
+ warnings of about 14 KB each (105 million characters, which the site
42
+ rendered as one list); now one of 92 characters.
43
+
44
+ - **The npm homepage is https://www.barmark.dev**, not the package's
45
+ folder on GitHub.
46
+
47
+ ### Fixed
48
+
49
+ - **A bar played many times no longer costs its size on every play.** A
50
+ shared chart of one bar with thousands of lyric syllables, notes or
51
+ slots, repeated thousands of times, made the parse copy every syllable
52
+ for every play (lyrics) and walk every slot of every play twice (the
53
+ pedal warnings, through a matrix of every slot of every played bar):
54
+ seconds to minutes and over a gigabyte from a few kilobytes. A played
55
+ bar now shares its written bar's data: two plays of one bar return the
56
+ same `slots`, `words` and `lyrics[].syllables` arrays (and the same
57
+ `PlayedLyrics` object), which a caller must not mutate; each written
58
+ bar's syllables, pushes and pedal effect are read once per meter it
59
+ plays in. The parse costs the written chart plus the played bars, never
60
+ their product. The parse's data is otherwise unchanged; `pedalStates`
61
+ still builds its matrix for callers that want it, but `parseChart`
62
+ doesn't call it.
63
+ - **A chart of many parts parses in time near linear in its size, not
64
+ cubic.** Each part's projection scanned the whole system for its own
65
+ line at every line it projected, and rebuilt the systems by copying;
66
+ choosing a system's reference line compared every line with every
67
+ other; reading a labelled line re-listed the declared parts; finding
68
+ who sings read every bar line once per part, and gathered the lyric
69
+ rows by copying their list for each row; folding a bar looked for each
70
+ part's streams among all of them. 600 one-bar parts in one system
71
+ (19 KB) took about 9 s; at 50 KB, 1,500 such parts took minutes (now
72
+ 0.9 s), 1,383 parts under 1,383 verses 18 s (now 1.3 s). And each part
73
+ read every line of the chart although it may play only its share of
74
+ the bars: 3,227 parts resting through 2,000 systems one part writes
75
+ took 19 s, 2,113 parts under 6,339 tutti lines 27 s (now 1.1 s each).
76
+ A part of a chart of several now reads at most ten times its share of
77
+ the bars in lines; one that stops there says so ("expansion stopped
78
+ after 0 bars: each of 1000 parts reads at most 100 lines"). Real charts
79
+ read every line: four parts may each read 25,000.
80
+ A third review, and an audit of every loop over parts, voices, lines,
81
+ systems, rows and played bars, found more of the same: each part's
82
+ voice was looked for among every voice and every stream (1,979 parts
83
+ each singing, 50 KB: 59 s, now 0.3 s); a part's own line was looked up
84
+ again at every line of its system, and every part walked every line of
85
+ the chart (800 parts of four lines: 3.3 s, now 0.7 s; 777 parts of two
86
+ lines in two systems: 1.9 s, now 0.6 s), and the parts' score order
87
+ read every system once per part; a lyric row under a replay heading
88
+ was copied onto every part's line of the replayed system (1,354 parts
89
+ and 1,354 replays, each with its own row: 3.8 s, now 0.3 s), and so was
90
+ each row's list of covered lines. Printing a 10,000-bar line counted
91
+ its bars again for every bar (2.2 s, now 0.5 s). A test now parses each
92
+ of these shapes at eight times the parts and fails if it takes twenty
93
+ times as long.
94
+ - **The verses a bar sings count toward the expansion's budget.** Outside
95
+ a repeat a bar sings every verse written over it, so 1,560 verses under
96
+ one bar replayed 5,000 times (55 KB) made 7.8 million verse references
97
+ (1.3 s, 61 MB). Each verse a written bar sings when it plays again now
98
+ counts as work (its first play's are as many as are written), and a
99
+ chart past the budget stops with an error that says so ("expansion
100
+ stopped after 64 bars: the bars played again sing too many verses
101
+ (98,280)"; both numbers count only the bars output, so a bar a
102
+ replay's `->` cuts is not counted, and a bar the budget stops is
103
+ neither sung nor checked): now 0.1 s and 2 MB. Once play stops at any
104
+ limit, the stop is the only report of what it cut short: no "the parts
105
+ fall out of step" for a part that stopped early, and no "verse N is
106
+ never sung" for verses the stop left unsung: a row play never reached,
107
+ or reached in a repeat with passes enough for its verse. A verse its
108
+ repeat can never reach is still reported (`lyrics 3:` under `x2`), stop
109
+ or not. A part whose play ended at `Fine`, after the jump, reports no
110
+ stop for the lines past its share, which it would never have played. Repeats that play too much with no
111
+ bars to count (fermatas) now say "expansion stopped after N bars: its
112
+ repeats play too many times", not the bar cap's "expansion stopped at
113
+ 10000 bars", which is kept for the bar cap itself. Real charts use a tiny share of it: the examples
114
+ and fixtures at most 153 of 100,000.
115
+
116
+ ## [0.21.0] - 2026-11-08
117
+
118
+ ### Added
119
+
120
+ - **`nameNotes(pitchClasses, bass, key)`** names a chord from the pitch
121
+ classes sounding over a bass, spelled for the key, or returns null when
122
+ none fits: the name grammar 4's linter may suggest for a note set, and
123
+ what `@barmark/midi`'s import will call. Every root is tried, with the
124
+ fifth supplied where it is missing. The fewest added tones wins, a
125
+ slash and a supplied fifth costing one each; ties go to triads, then
126
+ sevenths, sixths, sus, extended chords and added tones, then the root
127
+ in the bass, then `(9)`, `(11)`, `(13)` over `(2)`, `(4)`, `(6)`.
128
+ Suffixes no one writes are never suggested: a degree both plain and
129
+ altered (`b9` with `9`), `(7)` on a sixth chord, a tone on `dim` or
130
+ `dim7`, an altered tone on `5`, `6` or `6/9`. `{G B F}` over G is `G7`,
131
+ `{E G C}` over E is `C/E`, `Am7/C`'s notes are `C6`, `{G B F E}` is
132
+ `G7(13)`; two pitch classes name only a power chord (`E5`). The bass is
133
+ spelled by its interval from the root, or by pitch when that would
134
+ double (`E(#9)/G`); with no key, a black-key root takes the spelling
135
+ with fewer awkward notes (`Db`, `C#m`, `G#dim7`).
136
+ - **`time` on a played bar in a one-bar meter** (Kevin's decision,
137
+ 2026-11-08, grammar 2.4, A.7): the meter of the `time:` in force,
138
+ whose beat `tempo:` counts there, so a short or odd bar never changes
139
+ speed. In `time: 6/8, tempo: 60` a `[2/8]` pickup plays at 60 dotted
140
+ quarters a minute, not 60 eighths. Absent on any other bar, where
141
+ `meter` is the `time:` meter. No change to what any chart means
142
+ otherwise.
143
+
144
+ ### Fixed
145
+
146
+ - **`printChart` prints many lyric rows in linear time.** Deciding
147
+ whether a row lies just under its bar line scanned every line of the
148
+ chart for a heading in between, once a row: 100 verses under each of
149
+ 500 bar lines took 9 s to print. The headings are now found by halves;
150
+ the same chart prints in about a second, parse included.
151
+ - **A part's tacet stand-in for a `%` is as long as the bar it copies**
152
+ (grammar 7.4, 2.4). Where one part writes `| [2/8] G | % |` and another
153
+ has no line, the resting part's bar for the `%` was a bar of the
154
+ running meter (`| [2/8] N.C. | N.C. |` in 6/8), so an extracted part
155
+ and the score disagreed on the bar's length. It is now `[2/8] N.C.`,
156
+ across line breaks and after a tutti line too. A bar of several parts
157
+ also takes its `time` from the part its meter comes from, the first
158
+ that plays, so a `%` of a one-bar meter where the lead part rests no
159
+ longer loses it (it played at 30 quarter notes a minute in 6/8 at
160
+ `tempo: 60`, not 90).
161
+
12
162
  ## [0.20.0] - 2026-11-08
13
163
 
14
164
  ### Changed
@@ -610,7 +760,9 @@ The first working release: the reference parser and printer for the
610
760
 
611
761
  - **A placeholder package that holds the `barmark` name** while the spec and parser are written.
612
762
 
613
- [Unreleased]: https://github.com/kevinpeckham/barmark/compare/barmark-v0.20.0...HEAD
763
+ [Unreleased]: https://github.com/kevinpeckham/barmark/compare/barmark-v0.22.0...HEAD
764
+ [0.22.0]: https://github.com/kevinpeckham/barmark/compare/barmark-v0.21.0...barmark-v0.22.0
765
+ [0.21.0]: https://github.com/kevinpeckham/barmark/compare/barmark-v0.20.0...barmark-v0.21.0
614
766
  [0.20.0]: https://github.com/kevinpeckham/barmark/compare/barmark-v0.19.1...barmark-v0.20.0
615
767
  [0.19.1]: https://github.com/kevinpeckham/barmark/compare/barmark-v0.19.0...barmark-v0.19.1
616
768
  [0.19.0]: https://github.com/kevinpeckham/barmark/compare/barmark-v0.18.0...barmark-v0.19.0
package/README.md CHANGED
@@ -41,6 +41,7 @@ import {
41
41
  extractPart,
42
42
  harteToBarmark,
43
43
  keyFromText,
44
+ nameNotes,
44
45
  parseChart,
45
46
  parseChord,
46
47
  printChart,
@@ -56,7 +57,8 @@ const chart = parseChart(text);
56
57
  chart.title; // "Example Song"
57
58
  chart.parts; // [{ label: "Gtr", name: "Electric Guitar 1" }] from part: Gtr = Electric Guitar 1, in score order
58
59
  chart.credits; // { composer: ["Kevin Peckham"], lyricist: [], arranger: [], copyright: [] } from composer: Kevin Peckham
59
- chart.bars; // every bar in play order: its number as written, slots, section, key, meter, tempo, feel (and swingPercent)
60
+ chart.bars; // every bar in play order: its number as written, slots, section, key, meter (and in a one-bar meter the time: meter, whose beat tempo counts), tempo, feel (and swingPercent)
61
+ chart.pickup; // true when the first written bar is a pickup (time: 4/4 then | [1/4] G | C |): it is bar 0
60
62
  chart.sections; // each section as played, with its first and last bar
61
63
  chart.diagnostics; // errors and warnings, each with a line number
62
64
 
@@ -78,6 +80,7 @@ if (chord.ok) chord.value.pitches; // ["B", "D", "F", "A"]
78
80
  if (chord.ok) chord.value.harte; // "B:hdim7"
79
81
 
80
82
  harteToBarmark("A:sus4(b7,9)"); // "A9sus4"
83
+ nameNotes([7, 11, 2, 5], 11, null); // "G7/B": a chord symbol for sounding pitch classes (0 = C) over a bass, spelled for a key; null when none fits
81
84
  ```
82
85
 
83
86
  ### Several parts
@@ -193,9 +196,11 @@ an object. It has two kinds of field:
193
196
  - `events` is play order with fermatas and an open repeat kept as
194
197
  events.
195
198
  - `bars` is the same flattened to one `PlayedBar` per measure played,
196
- repeats, endings, jumps and replays spelled out. A bar's `index` counts
197
- in play order; `written` is the number a band reads on the page, the
198
- same on every pass. Each bar carries its slots, section, key, meter,
199
+ repeats, endings, jumps and replays spelled out. A bar's `index` is
200
+ its position in play order, not a bar number; `written` is the number
201
+ a band reads on the page, the same on every pass. A pickup (grammar
202
+ 2.4: a short first bar before a full one) is bar 0, and `pickup` says
203
+ the chart has one; otherwise bars count from 1. Each bar carries its slots, section, key, meter,
199
204
  tempo, feel and, when sung, each verse's syllables (`lyrics`).
200
205
  - `sections` is each section as played, with its first and last bar and
201
206
  `form` (which distinct section it is; replays share their section's
@@ -218,7 +223,8 @@ package's minor versions: 0.17.0, for example, added the `break` and
218
223
  diagnostics with `severity: "error"`; store `printChart(text)` to keep
219
224
  one spelling of each chart.
220
225
  - **Times in a recording** belong beside the chart, not in it: key them
221
- by `PlayedBar.index`, which names the same bar however the chart is
226
+ by `PlayedBar.index`, the play position (not the bar number, which
227
+ is `written`), which names the same bar however the chart is
222
228
  reformatted or its repeats are spelled (`|: :|` or `x2`), until the
223
229
  music itself changes. An open repeat (`x ...`) plays its bars once in
224
230
  `bars`, so its later passes have no index of their own.
@@ -24,6 +24,8 @@ function checkParts(lines, diagnostics) {
24
24
  return declared.map(partOf);
25
25
  const seen = new Map();
26
26
  const parts = [];
27
+ // Where each declaration is, found once: finding it again for every bad one costs the lines times the bad ones.
28
+ let at;
27
29
  for (const l of declared) {
28
30
  const problem = partProblem(l, seen);
29
31
  if (!problem) {
@@ -32,7 +34,10 @@ function checkParts(lines, diagnostics) {
32
34
  }
33
35
  const text = l.label ? `part: ${l.label} = ${l.name}` : `part: ${l.name}`;
34
36
  diagnostics.push({ line: l.line, severity: "error", token: text, ...problem });
35
- lines[lines.indexOf(l)] = { kind: "text", line: l.line, text };
37
+ at ??= new Map(lines.map((m, i) => [m, i]));
38
+ const i = at.get(l);
39
+ if (i !== undefined)
40
+ lines[i] = { kind: "text", line: l.line, text };
36
41
  }
37
42
  return parts;
38
43
  }
@@ -94,7 +99,7 @@ function readHeading(text, line, diagnostics) {
94
99
  }
95
100
  /** A declared label written before a bar line, exactly, or a part's label, a dot and a line's name (grammar 7.6). */
96
101
  function findLabel(word, declared) {
97
- const match = (w) => declared.find((p) => p.label?.toLowerCase() === w.toLowerCase())?.label;
102
+ const match = (w) => declared.get(w.toLowerCase());
98
103
  const exact = match(word);
99
104
  if (exact)
100
105
  return { part: exact };
@@ -234,6 +239,9 @@ export function readDocument(text) {
234
239
  const lines = [];
235
240
  const diagnostics = [];
236
241
  let title = null;
242
+ /** The parts declared so far, by lowercased label (the first of a label), and whether a bar line has been read. */
243
+ const labels = new Map();
244
+ let barred = false;
237
245
  /** The line kept as text, with why it isn't what it looks like. */
238
246
  const asText = (t, line, severity, message) => {
239
247
  diagnostics.push({ line, severity, message, token: t });
@@ -249,8 +257,11 @@ export function readDocument(text) {
249
257
  const partLine = (read, t, line) => {
250
258
  if (typeof read === "string")
251
259
  return asText(t, line, "error", read);
252
- if (lines.some((l) => l.kind === "bars" || l.kind === "invalid"))
260
+ if (barred)
253
261
  return asText(t, line, "error", "part: names the whole chart; put it before the first bar");
262
+ const label = read.label?.toLowerCase();
263
+ if (label !== undefined && !labels.has(label))
264
+ labels.set(label, read.label ?? "");
254
265
  return { kind: "part", line, ...read };
255
266
  };
256
267
  const otherLine = (t, line) => {
@@ -275,7 +286,6 @@ export function readDocument(text) {
275
286
  diagnostics.push(...r.diagnostics);
276
287
  return r.bars ?? { kind: "invalid", line, text: t, written: r.written };
277
288
  };
278
- const declaredParts = () => lines.filter((l) => l.kind === "part");
279
289
  /** A labelled line that can't be read: no space before its pipe, or a bad line name. */
280
290
  const badLabel = (t, line, word, rest, owner) => {
281
291
  const space = t.length > word.length + rest.length;
@@ -291,7 +301,7 @@ export function readDocument(text) {
291
301
  /** A declared part's bar line, or one of its lines (`Pno.RH`, grammar 7.6). */
292
302
  const labelledLine = (t, line) => {
293
303
  const split = splitLabel(t);
294
- const owner = split && findLabel(split.word, declaredParts());
304
+ const owner = split && findLabel(split.word, labels);
295
305
  if (!split || !owner)
296
306
  return null;
297
307
  const { word, space, rest } = split;
@@ -340,8 +350,12 @@ export function readDocument(text) {
340
350
  .split(/\r?\n/)
341
351
  .forEach((raw, i) => {
342
352
  const t = raw.trim();
343
- if (t && !t.startsWith("```"))
344
- lines.push(classify(t, i + 1));
353
+ if (!t || t.startsWith("```"))
354
+ return;
355
+ const l = classify(t, i + 1);
356
+ if (l.kind === "bars" || l.kind === "invalid")
357
+ barred = true;
358
+ lines.push(l);
345
359
  });
346
360
  const empty = noBarLines(lines);
347
361
  if (empty)
@@ -3,23 +3,40 @@ import type { ChartDiagnostic, ChartEvent, Line, PlayedBar, SectionOccurrence }
3
3
  * Where a played bar comes from, for its words (grammar 7.7): its bar
4
4
  * line, its place among that line's bars (from 0), the pass of the repeat
5
5
  * it plays in (null outside one, and in an open repeat, whose passes are
6
- * unknown), and the line of the replay heading it plays under, if any.
6
+ * unknown) and how many passes that repeat plays here, and the line of
7
+ * the replay heading it plays under, if any.
7
8
  */
8
9
  export type BarSource = {
9
10
  line: number;
10
11
  bar: number;
11
12
  pass: number | null;
13
+ passes: number | null;
12
14
  replay: number | null;
13
15
  };
14
- /** The words a played bar sings, given where it comes from; undefined when none. */
15
- export type Sing = (bar: PlayedBar, from: BarSource) => PlayedBar["lyrics"];
16
+ /**
17
+ * The words a played bar sings, given where it comes from; undefined when
18
+ * none. `verses` says how many it would sing, with no other effect, so the
19
+ * budget is checked before a bar is sung.
20
+ */
21
+ export type Sing = {
22
+ (bar: PlayedBar, from: BarSource): PlayedBar["lyrics"];
23
+ verses(bar: PlayedBar, from: BarSource): number;
24
+ };
16
25
  /** Nested repeats multiply; expansion stops here rather than run away. */
17
26
  export declare const MAX_EXPANDED_BARS = 10000;
27
+ /**
28
+ * The work one of `share` parts may do: ten times its bars. A part of a
29
+ * chart of several also reads at most this many lines, so the parts
30
+ * times the lines never multiply.
31
+ */
32
+ export declare const workFor: (share: number) => number;
18
33
  /** Every played bar in order, with an open repeat's pass included once. */
19
34
  export declare function flattenBars(events: ChartEvent[]): PlayedBar[];
20
35
  /**
21
36
  * Turn the written chart into the bars in play order: sections and their
22
- * replays, repeats, directive changes, `%` and elision.
37
+ * replays, repeats, directive changes, `%` and elision. Its bars count
38
+ * `written` from 1, a pickup included; `parseChart` and `extractPart`
39
+ * renumber a pickup to 0 (grammar 2.4).
23
40
  */
24
41
  /**
25
42
  * `label` names the part a projected chart is (grammar 7.4), for its
@@ -33,4 +50,6 @@ sing?: Sing): {
33
50
  events: ChartEvent[];
34
51
  sections: SectionOccurrence[];
35
52
  diagnostics: ChartDiagnostic[];
53
+ /** Whether play stopped at a limit: what follows from that alone goes unreported. */
54
+ stopped: boolean;
36
55
  };