barmark 0.15.0 → 0.16.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.
@@ -9,7 +9,10 @@ export declare function zipEvents(each: ChartEvent[][], report: (d: ChartDiagnos
9
9
  export declare function parseChart(text: string): ChartParse;
10
10
  /**
11
11
  * One part of a chart as a single-part chart (grammar 7.4): the tutti bars,
12
- * its own bars, and `N.C.` marked `tacet` where it rests. Null when the
13
- * chart declares no part with that label. Never throws.
12
+ * its own bars, and `N.C.` marked `tacet` where it rests. With `lineName`,
13
+ * one of the part's lines alone (grammar 7.6), as a converter writes each
14
+ * line: the part's first line also plays its bare and tutti lines, and a
15
+ * line rests where it isn't written. Null when the chart declares no part
16
+ * with that label, or the part no line of that name. Never throws.
14
17
  */
15
- export declare function extractPart(text: string, label: string): ChartParse | null;
18
+ export declare function extractPart(text: string, label: string, lineName?: string): ChartParse | null;
@@ -1,6 +1,7 @@
1
1
  import { readDocument } from "./document.js";
2
2
  import { expand, flattenBars } from "./expand.js";
3
- import { projectPart } from "./parts.js";
3
+ import { projectPart, projectPartLines } from "./parts.js";
4
+ import { pedalWarnings } from "./pedal.js";
4
5
  const RANK = { error: 0, warning: 1 };
5
6
  /** Sorted by line, errors before warnings. */
6
7
  const sortDiagnostics = (all) => all.sort((a, b) => a.line - b.line || RANK[a.severity] - RANK[b.severity]);
@@ -78,17 +79,80 @@ export function zipEvents(each, report) {
78
79
  return e;
79
80
  });
80
81
  }
81
- /** Several parts: each projected and played, then zipped into one timeline (grammar 7.4). */
82
- function playParts(lines, parts) {
83
- const played = parts.map((p) => expand(projectPart(lines, parts, p.label ?? ""), p.label, parts.length));
82
+ const streamsOf = (shown) => shown.flatMap((p, part) => p.lines?.length
83
+ ? p.lines.map((name) => ({ part, label: p.label ?? "", name }))
84
+ : [{ part, label: p.label ?? "", name: null }]);
85
+ /** A part's share of a bar from its streams' shares: its first playing line leads (grammar 7.6). */
86
+ function partOf(p, own) {
87
+ const playing = own.filter((e) => e[1] !== null);
88
+ const lead = playing[0]?.[1];
89
+ if (!lead)
90
+ return null;
91
+ if (!p.lines?.length)
92
+ return lead;
93
+ const lines = playing.map(([s, b]) => ({
94
+ name: s.name ?? "",
95
+ line: b.line,
96
+ slots: b.slots,
97
+ ...(b.words && { words: b.words }),
98
+ }));
99
+ return { ...lead, lines };
100
+ }
101
+ /** A zipped bar of streams as a bar of parts; with one part shown, its lines sit on the bar itself. */
102
+ function foldBar(bar, streams, shown) {
103
+ const each = bar.parts ?? [];
104
+ const parts = shown.map((p, i) => partOf(p, streams.flatMap((s, j) => (s.part === i ? [[s, each[j] ?? null]] : []))));
105
+ const { parts: _p, words: _w, tacet: _t, ...shared } = bar;
106
+ const lead = parts[0];
107
+ const top = lead
108
+ ? { ...shared, line: lead.line, slots: lead.slots, ...(lead.words && { words: lead.words }) }
109
+ : bar;
110
+ if (shown.length > 1)
111
+ return { ...top, parts };
112
+ const { parts: _q, ...alone } = top;
113
+ return lead?.lines ? { ...alone, lines: lead.lines } : alone;
114
+ }
115
+ function foldEvents(events, streams, shown) {
116
+ return events.map((e) => {
117
+ if (e.kind === "bar")
118
+ return { kind: "bar", bar: foldBar(e.bar, streams, shown) };
119
+ if (e.kind === "open")
120
+ return { ...e, events: foldEvents(e.events, streams, shown) };
121
+ return e;
122
+ });
123
+ }
124
+ /** A part's own bars from a folded bar: tacet where none of its lines plays. */
125
+ function partView(b, i, shown) {
126
+ if (shown === 1)
127
+ return b;
128
+ const own = b.parts?.[i];
129
+ return own ? { ...b, line: own.line, slots: own.slots } : { ...b, slots: [], tacet: true };
130
+ }
131
+ /** The pedal warnings of each part that writes lines, walked on the part, not one line (grammar 2.9, 7.6). */
132
+ function linedPedalWarnings(events, shown) {
133
+ const bars = flattenBars(events);
134
+ return shown.flatMap((p, i) => p.lines?.length ? pedalWarnings(bars.map((b) => partView(b, i, shown.length))) : []);
135
+ }
136
+ /**
137
+ * Parts and their lines: each stream projected and played, zipped into one
138
+ * timeline, then folded into the parts shown (grammar 7.4, 7.6).
139
+ */
140
+ function playStreams(lines, parts, shown) {
141
+ const streams = streamsOf(shown);
142
+ const played = streams.map((s) => expand(projectPart(lines, parts, s.label, s.name), s.name === null ? s.label : `${s.label}.${s.name}`, streams.length, s.name === null));
84
143
  const diagnostics = mergeReports(played.map((p) => p.diagnostics));
85
- const events = zipEvents(played.map((p) => p.events), (d) => diagnostics.push(d));
144
+ const zipped = zipEvents(played.map((p) => p.events), (d) => diagnostics.push(d));
145
+ const events = foldEvents(zipped, streams, shown);
146
+ diagnostics.push(...linedPedalWarnings(events, shown));
86
147
  return { events, sections: played[0]?.sections ?? [], diagnostics };
87
148
  }
149
+ const lined = (parts) => parts.some((p) => p.lines?.length);
88
150
  /** Read a whole barmark chart: the document as written and the bars in play order. Never throws. */
89
151
  export function parseChart(text) {
90
152
  const doc = readDocument(text);
91
- const played = doc.parts.length > 1 ? playParts(doc.lines, doc.parts) : expand(doc.lines);
153
+ const played = doc.parts.length > 1 || lined(doc.parts)
154
+ ? playStreams(doc.lines, doc.parts, doc.parts)
155
+ : expand(doc.lines);
92
156
  return {
93
157
  title: doc.title,
94
158
  part: doc.part,
@@ -101,18 +165,39 @@ export function parseChart(text) {
101
165
  diagnostics: sortDiagnostics([...doc.diagnostics, ...played.diagnostics]),
102
166
  };
103
167
  }
168
+ /** What an extract holds and plays: one of the part's lines, the part with its lines, or the part. */
169
+ function extractOf(doc, part, lineName) {
170
+ const label = part.label ?? "";
171
+ if (lineName !== undefined) {
172
+ const name = part.lines?.find((n) => n.toLowerCase() === lineName.toLowerCase());
173
+ if (!name)
174
+ return null;
175
+ const lines = projectPart(doc.lines, doc.parts, label, name);
176
+ return { lines, played: expand(lines, `${label}.${name}`) };
177
+ }
178
+ if (part.lines?.length)
179
+ return {
180
+ lines: projectPartLines(doc.lines, label),
181
+ played: playStreams(doc.lines, doc.parts, [part]),
182
+ };
183
+ const lines = doc.parts.length > 1 ? projectPart(doc.lines, doc.parts, label) : doc.lines;
184
+ return { lines, played: expand(lines, part.label) };
185
+ }
104
186
  /**
105
187
  * One part of a chart as a single-part chart (grammar 7.4): the tutti bars,
106
- * its own bars, and `N.C.` marked `tacet` where it rests. Null when the
107
- * chart declares no part with that label. Never throws.
188
+ * its own bars, and `N.C.` marked `tacet` where it rests. With `lineName`,
189
+ * one of the part's lines alone (grammar 7.6), as a converter writes each
190
+ * line: the part's first line also plays its bare and tutti lines, and a
191
+ * line rests where it isn't written. Null when the chart declares no part
192
+ * with that label, or the part no line of that name. Never throws.
108
193
  */
109
- export function extractPart(text, label) {
194
+ export function extractPart(text, label, lineName) {
110
195
  const doc = readDocument(text);
111
196
  const part = doc.parts.find((p) => p.label?.toLowerCase() === label.toLowerCase());
112
- if (!part)
197
+ const extract = part ? extractOf(doc, part, lineName) : null;
198
+ if (!part || !extract)
113
199
  return null;
114
- const lines = doc.parts.length > 1 ? projectPart(doc.lines, doc.parts, label) : doc.lines;
115
- const played = expand(lines, part.label);
200
+ const { lines, played } = extract;
116
201
  const kept = new Set(lines.map((l) => l.line));
117
202
  // A stand-in may sit on another part's line; that part's problems are not this one's.
118
203
  for (const l of doc.lines)
@@ -0,0 +1,3 @@
1
+ import type { Part } from "./types.js";
2
+ /** A `part:` line's label, name and attributes, or why it has none (grammar 7.3). */
3
+ export declare function readPart(text: string): Part | string | null;
@@ -0,0 +1,73 @@
1
+ import { DIRECTIVE_AFTER } from "./directives.js";
2
+ import { CLEF_NAMES, instrumentInfo, nearInstruments, } from "./instruments.js";
3
+ /** A `part:` line's label, name and attributes, or why it has none (grammar 7.3). */
4
+ export function readPart(text) {
5
+ const m = /^part\s*:(.*)$/i.exec(text);
6
+ if (!m)
7
+ return null;
8
+ const found = splitAttributes((m[1] ?? "").trim().replace(/\s+/g, " "));
9
+ if (typeof found === "string")
10
+ return found;
11
+ const attributes = readAttributes(found.attributes);
12
+ if (typeof attributes === "string")
13
+ return attributes;
14
+ const named = readName(found.value);
15
+ return typeof named === "string" ? named : { ...named, ...attributes };
16
+ }
17
+ /** The label and name before any attributes. */
18
+ function readName(value) {
19
+ if (DIRECTIVE_AFTER.test(value))
20
+ return "part: goes on a line of its own; the name runs to the end of the line";
21
+ const labelled = /^([^\s=]*)\s*=\s*(.*)$/.exec(value);
22
+ if (!labelled)
23
+ return value ? { name: value } : "part: needs a name";
24
+ const [, label, name] = labelled;
25
+ if (!label)
26
+ return "part: needs a label before =";
27
+ if (/^[|#@0-9]/.test(label))
28
+ return "a part label can't start with |, #, @ or a digit";
29
+ return name ? { label, name } : "part: needs a name after the label";
30
+ }
31
+ const ATTRIBUTE = /,\s*(instrument|clef)\s*:\s*([^,]*)$/i;
32
+ /** `, instrument: …` and `, clef: …` peeled off the end of the line, in either order. */
33
+ function splitAttributes(text) {
34
+ let value = text;
35
+ const attributes = new Map();
36
+ for (let m = ATTRIBUTE.exec(value); m; m = ATTRIBUTE.exec(value)) {
37
+ const name = (m[1] ?? "").toLowerCase();
38
+ if (attributes.has(name))
39
+ return `part: names its ${name} once`;
40
+ attributes.set(name, (m[2] ?? "").trim());
41
+ value = value.slice(0, m.index).trim();
42
+ }
43
+ return { value, attributes };
44
+ }
45
+ /** The instrument and clef written, each checked against its list. */
46
+ function readAttributes(written) {
47
+ const instrument = readInstrument(written.get("instrument"));
48
+ if (typeof instrument === "string")
49
+ return instrument;
50
+ const clef = written.get("clef")?.toLowerCase();
51
+ if (clef === undefined)
52
+ return instrument ? { instrument: instrument.name } : {};
53
+ const problem = clefProblem(clef, instrument?.staves ?? 1);
54
+ if (problem)
55
+ return problem;
56
+ return { ...(instrument && { instrument: instrument.name }), clef: clef };
57
+ }
58
+ /** The listed instrument written, null when none is, or why it isn't one. */
59
+ function readInstrument(name) {
60
+ if (name === undefined)
61
+ return null;
62
+ return instrumentInfo(name) ?? unknownInstrument(name);
63
+ }
64
+ function clefProblem(clef, staves) {
65
+ if (!CLEF_NAMES.includes(clef))
66
+ return `${clef || "an empty clef"} is not a clef; write ${CLEF_NAMES.join(", ")}`;
67
+ return staves === 2 ? "a keyboard has a treble and a bass staff; leave clef: out" : null;
68
+ }
69
+ function unknownInstrument(name) {
70
+ const near = nearInstruments(name);
71
+ const hint = near.length ? `: did you mean ${near.join(" or ")}?` : "";
72
+ return `${name || "an empty name"} is not an instrument barmark knows${hint}`;
73
+ }
@@ -1,7 +1,12 @@
1
- import type { Line, Part } from "./types.js";
1
+ import type { DeclaredPart, Line } from "./types.js";
2
2
  /**
3
3
  * One part of a multi-part chart as a single-part chart's lines (grammar
4
4
  * 7.4): the tutti lines, the part's own lines, and a bar of `N.C.` marked
5
- * `tacet` for each bar of a system it doesn't play. Never throws.
5
+ * `tacet` for each bar of a system it doesn't play. With `lineName`, one
6
+ * of the part's lines (grammar 7.6): the part's first line also plays
7
+ * its bare lines and the tutti lines, and a line rests (tacet) wherever
8
+ * it isn't written. Never throws.
6
9
  */
7
- export declare function projectPart(lines: Line[], parts: Part[], label: string): Line[];
10
+ export declare function projectPart(lines: Line[], parts: DeclaredPart[], label: string, lineName?: string | null): Line[];
11
+ /** 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
+ export declare function projectPartLines(lines: Line[], label: string): Line[];
@@ -34,13 +34,22 @@ function standIn(system, at) {
34
34
  /** A part's own line, as a single-part chart writes it. */
35
35
  function unlabelled(l) {
36
36
  if (l.kind === "invalid") {
37
- const { part: _p, system: _s, ...rest } = l;
37
+ const { part: _p, lineName: _n, system: _s, ...rest } = l;
38
38
  return rest;
39
39
  }
40
- const { part: _p, system: _s, dropped: _d, ...rest } = l;
40
+ const { part: _p, lineName: _n, system: _s, dropped: _d, ...rest } = l;
41
41
  return rest;
42
42
  }
43
- const plays = (l, key) => l.part?.toLowerCase() === key && l.kind === "bars" && !l.dropped;
43
+ const plays = (l) => l?.kind === "bars" && !l.dropped;
44
+ /** The stream's own line in a system, if written. */
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
50
+ : m.lineName.toLowerCase() === s.name.toLowerCase()));
51
+ /** 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;
44
53
  const inSystem = (l) => (l.kind === "bars" || l.kind === "invalid") && l.system !== undefined;
45
54
  function systemsOf(lines) {
46
55
  const systems = new Map();
@@ -49,16 +58,18 @@ function systemsOf(lines) {
49
58
  systems.set(l.system, [...(systems.get(l.system) ?? []), l]);
50
59
  return systems;
51
60
  }
52
- /** What one line becomes in the part's chart: itself, its unlabelled self, a stand-in, or nothing. */
53
- function projectLine(l, key, systems) {
61
+ /** What one line becomes in the stream's chart: itself, its unlabelled self, a stand-in, or nothing. */
62
+ function projectLine(l, s, systems) {
54
63
  if (l.kind === "part")
55
- return l.label?.toLowerCase() === key ? l : null;
56
- return inSystem(l) ? projectSystemLine(l, key, systems.get(l.system) ?? []) : l;
64
+ return l.label?.toLowerCase() === s.key ? l : null;
65
+ if (inSystem(l))
66
+ return projectSystemLine(l, s, systems.get(l.system) ?? []);
67
+ return s.first ? l : restingTutti(l);
57
68
  }
58
- /** A system's line in one part's chart: the part's own line, a stand-in where it rests, or nothing. */
59
- function projectSystemLine(l, key, system) {
60
- const own = system.find((m) => m.part?.toLowerCase() === key);
61
- if (own === l && plays(l, key))
69
+ /** A system's line in one stream's chart: its own line, a stand-in where it rests, or nothing. */
70
+ function projectSystemLine(l, s, system) {
71
+ const own = ownLine(system, s);
72
+ if (own === l && plays(l))
62
73
  return unlabelled(l);
63
74
  const at = own ?? referenceOf(system) ?? system[0];
64
75
  return at === l ? standIn(system, l) : null;
@@ -66,12 +77,37 @@ function projectSystemLine(l, key, system) {
66
77
  /**
67
78
  * One part of a multi-part chart as a single-part chart's lines (grammar
68
79
  * 7.4): the tutti lines, the part's own lines, and a bar of `N.C.` marked
69
- * `tacet` for each bar of a system it doesn't play. Never throws.
80
+ * `tacet` for each bar of a system it doesn't play. With `lineName`, one
81
+ * of the part's lines (grammar 7.6): the part's first line also plays
82
+ * its bare lines and the tutti lines, and a line rests (tacet) wherever
83
+ * it isn't written. Never throws.
70
84
  */
71
- export function projectPart(lines, parts, label) {
85
+ export function projectPart(lines, parts, label, lineName = null) {
72
86
  if (!parts.length)
73
87
  return lines;
74
88
  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 };
75
92
  const systems = systemsOf(lines);
76
- return lines.map((l) => projectLine(l, key, systems)).filter((l) => l !== null);
93
+ return lines.map((l) => projectLine(l, s, systems)).filter((l) => l !== null);
94
+ }
95
+ /** 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
+ export function projectPartLines(lines, label) {
97
+ const key = label.toLowerCase();
98
+ const systems = systemsOf(lines);
99
+ return lines
100
+ .map((l) => {
101
+ if (l.kind === "part")
102
+ return l.label?.toLowerCase() === key ? l : null;
103
+ if (!inSystem(l))
104
+ 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;
111
+ })
112
+ .filter((l) => l !== null);
77
113
  }
@@ -0,0 +1,5 @@
1
+ import type { ChartDiagnostic, PlayedBar } from "./types.js";
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
+ export declare function pedalStates(bars: PlayedBar[]): boolean[][];
4
+ /** A warning for each `*` played with the pedal already up, once per line. */
5
+ export declare function pedalWarnings(bars: PlayedBar[]): ChartDiagnostic[];
@@ -0,0 +1,44 @@
1
+ const pedalOf = (s) => (s.kind === "chord" ? s.pedal : undefined);
2
+ const membersOf = (s) => (s.kind === "group" ? s.members : [s]);
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.
7
+ */
8
+ function walk(bars, onRelease = () => { }) {
9
+ let down = false;
10
+ return bars.map((bar) => {
11
+ if (bar.tacet)
12
+ down = false;
13
+ 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
+ }
23
+ return down && !bar.tacet;
24
+ });
25
+ });
26
+ }
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);
30
+ }
31
+ /** A warning for each `*` played with the pedal already up, once per line. */
32
+ export function pedalWarnings(bars) {
33
+ const lines = new Set();
34
+ walk(bars, (bar, wasDown) => {
35
+ if (!wasDown)
36
+ lines.add(bar.line);
37
+ });
38
+ return [...lines].map((line) => ({
39
+ line,
40
+ severity: "warning",
41
+ message: "* lifts the pedal, and it is already up here",
42
+ token: "*",
43
+ }));
44
+ }
@@ -44,14 +44,16 @@ export function reduceSlots(slots, beats) {
44
44
  const SYMBOLS = { hold: "/", nc: "N.C.", silence: "--", unknown: "?" };
45
45
  const PUSHES = { eighth: "<", sixteenth: "<<" };
46
46
  const ARTICULATIONS = { accent: ">", hit: "^" };
47
+ const PEDALS = { press: "+", release: "*" };
47
48
  /** A slot's leading marks: its push, then its articulation (grammar 2.7, 2.8). */
48
49
  function printMarks(s) {
49
50
  const push = "push" in s && s.push ? PUSHES[s.push] : "";
50
- return push + (s.kind === "chord" && s.articulation ? ARTICULATIONS[s.articulation] : "");
51
+ const pedal = s.kind === "chord" && s.pedal ? PEDALS[s.pedal] : "";
52
+ return pedal + push + (s.kind === "chord" && s.articulation ? ARTICULATIONS[s.articulation] : "");
51
53
  }
52
54
  function printSimple(s) {
53
55
  const body = s.kind === "chord" ? s.chord.canonical + (s.voicing?.canonical ?? "") : SYMBOLS[s.kind];
54
- return printMarks(s) + body;
56
+ return printMarks(s) + body + (s.dotted ? "." : "");
55
57
  }
56
58
  /** A slot in canonical spelling; a group's members are joined with `_`. */
57
59
  export function printSlot(s) {
@@ -147,7 +149,7 @@ export function printLine(l, beats = []) {
147
149
  case "directive":
148
150
  return printDirectives(l.set);
149
151
  case "part":
150
- return `part: ${l.label ? `${l.label} = ` : ""}${l.name}`;
152
+ return `part: ${l.label ? `${l.label} = ` : ""}${l.name}${l.instrument ? `, instrument: ${l.instrument}` : ""}${l.clef ? `, clef: ${l.clef}` : ""}`;
151
153
  case "credit":
152
154
  return `${l.role}: ${l.text}`;
153
155
  case "bars":
@@ -164,13 +166,24 @@ export function printLine(l, beats = []) {
164
166
  export function printChart(text) {
165
167
  return printParsed(text, parseChart(text));
166
168
  }
169
+ /** A labelled line's label as printed: the part's, and its line's name (`Pno.RH`, grammar 7.6). */
170
+ const labelOf = (l) => `${l.part ?? ""}${l.lineName === undefined ? "" : `.${l.lineName}`}`;
171
+ const labelsOf = (p) => [
172
+ p.label ?? "",
173
+ ...(p.lines ?? []).map((n) => `${p.label}.${n}`),
174
+ ];
175
+ /** Score order: by part, then by the part's line, a bare line first. */
176
+ function scoreOf(l, parsed) {
177
+ const at = parsed.parts.findIndex((p) => p.label === l.part);
178
+ const line = l.lineName === undefined ? -1 : (parsed.parts[at]?.lines ?? []).indexOf(l.lineName);
179
+ return at * 10 + line + 1;
180
+ }
167
181
  /** A system's lines in score order, labels padded and pipes lined up (section 8, grammar 7.4). */
168
182
  function alignSystem(system, parsed, beatsFor, edit) {
169
- const score = (l) => parsed.parts.findIndex((p) => p.label === l.part);
170
- const width = Math.max(...parsed.parts.map((p) => p.label?.length ?? 0));
183
+ const width = Math.max(...parsed.parts.flatMap(labelsOf).map((label) => label.length));
171
184
  const pieces = [...system]
172
- .sort((a, b) => score(a) - score(b))
173
- .map((l) => ({ label: l.part ?? "", ...barPieces(edit(l), beatsFor(l)) }));
185
+ .sort((a, b) => scoreOf(a, parsed) - scoreOf(b, parsed))
186
+ .map((l) => ({ label: labelOf(l), ...barPieces(edit(l), beatsFor(l)) }));
174
187
  const columns = pieces[0]?.cells.map((_, i) => Math.max(...pieces.map((p) => p.cells[i]?.length ?? 0)));
175
188
  return pieces.map((p) => `${p.label.padEnd(width)} ${joinPieces({ ...p, cells: p.cells.map((c, i) => c.padEnd(columns?.[i] ?? 0)) })}`.trimEnd());
176
189
  }
@@ -189,7 +202,7 @@ function systemTexts(parsed, errored, beatsFor, edit) {
189
202
  // A system with an error is neither sorted nor padded, so the error's line stays where it was written.
190
203
  const printed = valid.length === lines.length
191
204
  ? alignSystem(valid, parsed, beatsFor, edit)
192
- : valid.map((l) => `${l.part} ${printLine(edit(l), beatsFor(l))}`);
205
+ : valid.map((l) => `${labelOf(l)} ${printLine(edit(l), beatsFor(l))}`);
193
206
  for (const [i, l] of valid.entries())
194
207
  texts.set(l.line, printed[i] ?? "");
195
208
  }
@@ -0,0 +1,6 @@
1
+ import type { SimpleSlot } from "./types.js";
2
+ /**
3
+ * How a group divides its slot (grammar 2.2), as fractions summing to 1:
4
+ * equally, or 3:1 (1:3) when one member of a pair is dotted.
5
+ */
6
+ export declare function memberShares(members: SimpleSlot[]): number[];
@@ -0,0 +1,10 @@
1
+ /**
2
+ * How a group divides its slot (grammar 2.2), as fractions summing to 1:
3
+ * equally, or 3:1 (1:3) when one member of a pair is dotted.
4
+ */
5
+ export function memberShares(members) {
6
+ const dotted = members.findIndex((m) => m.dotted);
7
+ if (members.length === 2 && dotted >= 0)
8
+ return dotted === 0 ? [0.75, 0.25] : [0.25, 0.75];
9
+ return members.map(() => 1 / members.length);
10
+ }
@@ -4,6 +4,8 @@ type BarsLine = Extract<Line, {
4
4
  }>;
5
5
  /** The marks of a bar line that shape the timeline, which every line of a system shares (grammar 7.4). */
6
6
  export declare function structureOf(l: BarsLine): string;
7
+ /** The key a part's line is grouped and played by: its label, and its line's name if any (grammar 7.6). */
8
+ export declare const streamKey: (part: string, lineName?: string) => string;
7
9
  /**
8
10
  * Group a multi-part chart's labelled bar lines into systems and check that
9
11
  * each system's lines share their bars and structure. A line that differs is
@@ -33,6 +33,20 @@ function differs(l, ref) {
33
33
  return null;
34
34
  }
35
35
  const isLabelled = (l) => (l.kind === "bars" || l.kind === "invalid") && l.part !== undefined;
36
+ /** The key a part's line is grouped and played by: its label, and its line's name if any (grammar 7.6). */
37
+ export const streamKey = (part, lineName) => (lineName === undefined ? part : `${part}.${lineName}`).toLowerCase();
38
+ /**
39
+ * What a line takes in its system, and what it can't share it with: its
40
+ * own stream, and its part written bare or as lines, which can't share a
41
+ * system (grammar 7.6).
42
+ */
43
+ function claimsOf(l) {
44
+ const part = (l.part ?? "").toLowerCase();
45
+ if (l.lineName === undefined)
46
+ return { takes: [part, `${part}#bare`], clashes: [part, `${part}#lines`] };
47
+ const own = streamKey(part, l.lineName);
48
+ return { takes: [own, `${part}#lines`], clashes: [own, `${part}#bare`] };
49
+ }
36
50
  /** Number each run of labelled lines with no label twice (grammar 7.4). */
37
51
  function numberSystems(lines) {
38
52
  let system = -1;
@@ -42,12 +56,13 @@ function numberSystems(lines) {
42
56
  seen = null;
43
57
  return l;
44
58
  }
45
- const label = l.part ?? "";
46
- if (!seen || seen.has(label)) {
59
+ const { takes, clashes } = claimsOf(l);
60
+ if (!seen || clashes.some((c) => seen?.has(c))) {
47
61
  system++;
48
62
  seen = new Set();
49
63
  }
50
- seen.add(label);
64
+ for (const c of takes)
65
+ seen.add(c);
51
66
  return { ...l, system };
52
67
  });
53
68
  }
@@ -1,4 +1,5 @@
1
1
  import type { ChordRecord } from "../chords/types.js";
2
+ import type { ClefName, InstrumentName } from "./instruments.js";
2
3
  export type Severity = "error" | "warning";
3
4
  export type ChartDiagnostic = {
4
5
  line: number;
@@ -28,11 +29,14 @@ export type TempoChange = {
28
29
  /** `tempo:` gives either `tempo` (a steady tempo) or `tempoChange`, never both. */
29
30
  /**
30
31
  * The part a chart is written for (grammar 7.3): `part: Electric Guitar 1`,
31
- * or with a short label, `part: Gtr = Electric Guitar 1`.
32
+ * or with a short label, `part: Gtr = Electric Guitar 1`; `instrument:`
33
+ * and `clef:` may follow, and are present only when written.
32
34
  */
33
35
  export type Part = {
34
36
  name: string;
35
37
  label?: string;
38
+ instrument?: InstrumentName;
39
+ clef?: ClefName;
36
40
  };
37
41
  export type Directives = {
38
42
  key?: Key;
@@ -54,16 +58,19 @@ export type Articulation = "accent" | "hit";
54
58
  * sixteenth, taken from the slot played just before it.
55
59
  */
56
60
  export type Push = "eighth" | "sixteenth";
61
+ /** `+` presses the sustain pedal, or changes it when it is down; `*` lifts it (grammar 2.9). */
62
+ export type Pedal = "press" | "release";
57
63
  /**
58
64
  * A chord slot holds a chord symbol or a note set; `chord.kind` tells
59
65
  * which. `voicing` is the exact notes written after a chord's name
60
66
  * (`Cmaj7{E3 G3 B3}`, grammar 4.2). The optional fields are present only
61
67
  * when written.
62
68
  */
63
- export type SimpleSlot = {
69
+ export type SimpleSlot = ({
64
70
  kind: "chord";
65
71
  chord: ChordRecord;
66
72
  voicing?: ChordRecord;
73
+ pedal?: Pedal;
67
74
  articulation?: Articulation;
68
75
  push?: Push;
69
76
  } | {
@@ -76,6 +83,9 @@ export type SimpleSlot = {
76
83
  push?: Push;
77
84
  } | {
78
85
  kind: "unknown";
86
+ }) & {
87
+ /** A dotted member of a two-member group (grammar 2.2): three quarters of its slot. */
88
+ dotted?: true;
79
89
  };
80
90
  export type Slot = SimpleSlot | {
81
91
  kind: "group";
@@ -166,6 +176,8 @@ export type Line = {
166
176
  ending: number[] | null;
167
177
  /** The declared label of the part whose line this is (grammar 7.4); absent on a tutti line and in a single-part chart. */
168
178
  part?: string;
179
+ /** The part's line this is (`RH` in `Pno.RH`, grammar 7.6), as first spelled; absent on a part's bare line. */
180
+ lineName?: string;
169
181
  /** Which system a labelled line is in, from 0 in chart order. */
170
182
  system?: number;
171
183
  /** Set when the line's bars or structure differ from its system's (an error): its part is tacet there. */
@@ -192,6 +204,7 @@ export type Line = {
192
204
  text: string;
193
205
  written: number;
194
206
  part?: string;
207
+ lineName?: string;
195
208
  system?: number;
196
209
  };
197
210
  export type PlayedBar = {
@@ -233,12 +246,27 @@ export type PlayedBar = {
233
246
  * `slots` and `words` are the first part's.
234
247
  */
235
248
  parts?: PartBar[];
249
+ /** A single part's lines (grammar 7.6), as on `PartBar`: in a single-part chart or an extracted part that writes lines. */
250
+ lines?: LineBar[];
236
251
  };
237
- /** One part's share of a played bar (grammar 7.4): the line it is written on, its slots and words. */
252
+ /** One part's line's share of a played bar (grammar 7.6): its name, the line it is written on, its slots and words. */
253
+ export type LineBar = {
254
+ name: string;
255
+ line: number;
256
+ slots: Slot[];
257
+ words?: Words[];
258
+ };
259
+ /**
260
+ * One part's share of a played bar (grammar 7.4): the line it is written
261
+ * on, its slots and words. A part that writes lines (7.6) has `lines`, one
262
+ * per line playing there in score order, and its first playing line's
263
+ * `line`, `slots` and `words`.
264
+ */
238
265
  export type PartBar = {
239
266
  line: number;
240
267
  slots: Slot[];
241
268
  words?: Words[];
269
+ lines?: LineBar[];
242
270
  } | null;
243
271
  /** An open repeat shows one pass; the number of passes is unknown. */
244
272
  export type ChartEvent = {
@@ -266,6 +294,10 @@ export type SectionOccurrence = {
266
294
  */
267
295
  form: number;
268
296
  };
297
+ /** A declared part as the parse reports it: `lines` names its lines in score order (grammar 7.6), present only when it writes some. */
298
+ export type DeclaredPart = Part & {
299
+ lines?: string[];
300
+ };
269
301
  export type ChartParse = {
270
302
  title: string | null;
271
303
  /**
@@ -273,9 +305,9 @@ export type ChartParse = {
273
305
  * none or several.
274
306
  * @deprecated use `parts`.
275
307
  */
276
- part: Part | null;
308
+ part: DeclaredPart | null;
277
309
  /** Every part the chart declares, in score order (grammar 7.4); empty when none. */
278
- parts: Part[];
310
+ parts: DeclaredPart[];
279
311
  /** The credit lines (grammar 7.5); each list empty when none is written. */
280
312
  credits: Credits;
281
313
  lines: Line[];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "barmark",
3
- "version": "0.15.0",
3
+ "version": "0.16.0",
4
4
  "description": "Markdown-inspired plain-text notation for chord charts: a parser, a canonical printer and Harte-label conversion.",
5
5
  "type": "module",
6
6
  "exports": {