@barmark/lyricsheet 0.0.0 → 0.1.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 +79 -0
- package/LICENSE +21 -0
- package/README.md +167 -1
- package/dist/export/cells.d.ts +22 -0
- package/dist/export/cells.js +64 -0
- package/dist/export/changes.d.ts +18 -0
- package/dist/export/changes.js +70 -0
- package/dist/export/defines.d.ts +7 -0
- package/dist/export/defines.js +97 -0
- package/dist/export/grid.d.ts +9 -0
- package/dist/export/grid.js +210 -0
- package/dist/export/index.d.ts +3 -0
- package/dist/export/index.js +118 -0
- package/dist/export/lists.d.ts +13 -0
- package/dist/export/lists.js +33 -0
- package/dist/export/losses.d.ts +8 -0
- package/dist/export/losses.js +41 -0
- package/dist/export/lyrics.d.ts +61 -0
- package/dist/export/lyrics.js +269 -0
- package/dist/export/merge.d.ts +31 -0
- package/dist/export/merge.js +64 -0
- package/dist/export/meta.d.ts +10 -0
- package/dist/export/meta.js +67 -0
- package/dist/export/plan.d.ts +51 -0
- package/dist/export/plan.js +175 -0
- package/dist/export/sheet.d.ts +11 -0
- package/dist/export/sheet.js +51 -0
- package/dist/export/spell.d.ts +18 -0
- package/dist/export/spell.js +77 -0
- package/dist/export/text.d.ts +60 -0
- package/dist/export/text.js +53 -0
- package/dist/export/types.d.ts +72 -0
- package/dist/export/types.js +1 -0
- package/dist/export/written.d.ts +29 -0
- package/dist/export/written.js +117 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +2 -0
- package/dist/version.d.ts +2 -0
- package/dist/version.js +2 -0
- package/package.json +48 -1
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A section occurrence with no words as a ChordPro grid (spec 4.1, 4.3,
|
|
3
|
+
* 4.5, 4.6): `{start_of_grid: label="Intro" shape="1+4x4+1"}`, then one
|
|
4
|
+
* grid line per written bar line, its bars as measures and a cell per beat
|
|
5
|
+
* (`cells.ts`).
|
|
6
|
+
*
|
|
7
|
+
* - An occurrence that plays its section's written lines straight through
|
|
8
|
+
* (`written.ts`) is written once, as written: `|:`, `:|`, a volta `|2`,
|
|
9
|
+
* and `x N` as right-margin text. Any other is written as played, bar
|
|
10
|
+
* by bar, with plain bar lines.
|
|
11
|
+
* - Rows: a rehearsal mark starts its own row, so it is left-margin text
|
|
12
|
+
* (`@B`) at the start of the bars it marks. A written line with `x N`
|
|
13
|
+
* stays whole (the count is the line's): its marks are written at its
|
|
14
|
+
* start, a `mark-moved` loss. Words are right-margin text, the row's
|
|
15
|
+
* words in order, then its `x N`; words that read as a count (`x3`) are
|
|
16
|
+
* written in double quotes. The shape gives a margin cell each side, or
|
|
17
|
+
* the reference drops the text and warns "No margin cell for trailing
|
|
18
|
+
* comment".
|
|
19
|
+
* - `%` is written where the chart wrote it and it plays the measure
|
|
20
|
+
* before it in the row: not after a time change or a fermata, and not
|
|
21
|
+
* first in a row.
|
|
22
|
+
* - The shape is the widest row's (most cells): its measures by its beats
|
|
23
|
+
* (a fermata is a measure of one cell, `*…`). The reference draws every
|
|
24
|
+
* cell of a grid the same width, draws a line with fewer cells than the
|
|
25
|
+
* shape short, left-aligned, and warns "Too few cells for grid content"
|
|
26
|
+
* only for a line with more (measured 2026-11-19 with `chordpro`
|
|
27
|
+
* 6.101.0), so an occurrence is always one grid, and a `|: :|` is never
|
|
28
|
+
* split across two.
|
|
29
|
+
* - An open repeat's line is followed by `{comment: repeat ad lib.}`, a
|
|
30
|
+
* line of the grid like the changes below.
|
|
31
|
+
* - Key, time, tempo and feel changes and text lines are comments (and
|
|
32
|
+
* directives) between grid lines, as in lyric lines: the reference takes
|
|
33
|
+
* them there and draws the comment.
|
|
34
|
+
*/
|
|
35
|
+
import { beatsOf } from "barmark";
|
|
36
|
+
import { barCells } from "./cells.js";
|
|
37
|
+
import { changeLines } from "./changes.js";
|
|
38
|
+
import { maxOf, pushAll } from "./lists.js";
|
|
39
|
+
import { byLine, headingTexts, labelOf, lose, runOf, texts } from "./lyrics.js";
|
|
40
|
+
import { directive } from "./meta.js";
|
|
41
|
+
import { plain, textChanged, unjoined } from "./text.js";
|
|
42
|
+
import { placeOf, straightLines, writtenSimile } from "./written.js";
|
|
43
|
+
/** Bars split before each rehearsal mark but the first bar's: a mark starts its own row. */
|
|
44
|
+
function atMarks(bars) {
|
|
45
|
+
const out = [];
|
|
46
|
+
for (const [i, bar] of bars.entries()) {
|
|
47
|
+
if (i === 0 || bar.rehearsal)
|
|
48
|
+
out.push([]);
|
|
49
|
+
out.at(-1)?.push(bar);
|
|
50
|
+
}
|
|
51
|
+
return out;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* A written line's rows: its bars' first plays, its repeat signs, its
|
|
55
|
+
* ending's volta and its `x N`; split at a rehearsal mark, the opening
|
|
56
|
+
* sign on the first row and the closing one on the last. A line with
|
|
57
|
+
* `x N` is kept whole (the count is the line's), its marks at its start.
|
|
58
|
+
*/
|
|
59
|
+
function writtenRows(l, bars, out) {
|
|
60
|
+
const times = typeof l.repeat === "number" ? `x${l.repeat}` : null;
|
|
61
|
+
const open = l.ending ? `|${l.ending[0]}` : l.opens ? "|:" : "|";
|
|
62
|
+
const close = l.closes ? ":|" : "|";
|
|
63
|
+
if (times !== null) {
|
|
64
|
+
const moved = bars.find((b, i) => i > 0 && b.rehearsal);
|
|
65
|
+
if (moved)
|
|
66
|
+
out.losses.add("mark-moved", MARK_MOVED, moved);
|
|
67
|
+
return [{ bars, open, close, times }];
|
|
68
|
+
}
|
|
69
|
+
const parts = atMarks(bars);
|
|
70
|
+
return parts.map((part, i) => ({
|
|
71
|
+
bars: part,
|
|
72
|
+
open: i === 0 ? open : "|",
|
|
73
|
+
close: i === parts.length - 1 ? close : "|",
|
|
74
|
+
times: null,
|
|
75
|
+
}));
|
|
76
|
+
}
|
|
77
|
+
const MARK_MOVED = "a rehearsal mark inside a repeated line is written at the line's start";
|
|
78
|
+
/** Each written line's bars, first plays only, by line number. */
|
|
79
|
+
function firstPlays(o, w) {
|
|
80
|
+
const seen = new Set();
|
|
81
|
+
const firsts = new Map();
|
|
82
|
+
for (const b of o.bars) {
|
|
83
|
+
const key = `${b.line}:${placeOf(w, b)}`;
|
|
84
|
+
if (seen.has(key))
|
|
85
|
+
continue;
|
|
86
|
+
seen.add(key);
|
|
87
|
+
const line = firsts.get(b.line) ?? [];
|
|
88
|
+
line.push(b);
|
|
89
|
+
firsts.set(b.line, line);
|
|
90
|
+
}
|
|
91
|
+
return firsts;
|
|
92
|
+
}
|
|
93
|
+
/** The rows of an occurrence: its section's written lines when it plays them straight through, else its bars as played. */
|
|
94
|
+
function rowsOf(o, w, out) {
|
|
95
|
+
const lines = straightLines(o, w);
|
|
96
|
+
if (!lines)
|
|
97
|
+
return byLine(o.bars)
|
|
98
|
+
.flatMap(atMarks)
|
|
99
|
+
.map((bars) => ({ bars, open: "|", close: "|", times: null }));
|
|
100
|
+
const firsts = firstPlays(o, w);
|
|
101
|
+
return lines.flatMap((l) => writtenRows(l, firsts.get(l.line) ?? [], out));
|
|
102
|
+
}
|
|
103
|
+
/** Words that read as a count (`x3`, `x 3`, `X3`): in the right margin they would read as the line's. */
|
|
104
|
+
const COUNT = /(?:^|\s)x\s*\d+$/i;
|
|
105
|
+
/** Words as right-margin text: plain, one line, spaces single; in double quotes when they end like a count. */
|
|
106
|
+
function wordsText(text, bar, out) {
|
|
107
|
+
const flat = plain(text).replace(/\s+/g, " ").trim();
|
|
108
|
+
const written = COUNT.test(flat) ? `"${flat}"` : flat;
|
|
109
|
+
if (written !== text.replace(/\s+/g, " ").trim())
|
|
110
|
+
textChanged(out.losses, bar);
|
|
111
|
+
return written;
|
|
112
|
+
}
|
|
113
|
+
/** A fermata as a measure of its own, `*…`, and its loss. */
|
|
114
|
+
function fermata(parts, bar, out) {
|
|
115
|
+
lose(out, "fermata", bar);
|
|
116
|
+
parts.measures.push(["*…"]);
|
|
117
|
+
}
|
|
118
|
+
/** The same meter: `%` across a time change plays its bar in another. */
|
|
119
|
+
const sameMeter = (a, b) => a.meter.n === b.meter.n &&
|
|
120
|
+
a.meter.d === b.meter.d &&
|
|
121
|
+
a.time?.n === b.time?.n &&
|
|
122
|
+
a.time?.d === b.time?.d;
|
|
123
|
+
/**
|
|
124
|
+
* Whether a bar is written `%`: the chart wrote it, and the measure
|
|
125
|
+
* before it in its row is the bar it copies, in the same meter, with no
|
|
126
|
+
* fermata between them. (A `%` never follows a pickup: two short bars in
|
|
127
|
+
* a row open none, grammar 2.4.)
|
|
128
|
+
*/
|
|
129
|
+
function simileHere(bar, before, context) {
|
|
130
|
+
return (before !== undefined &&
|
|
131
|
+
writtenSimile(context.written, bar) &&
|
|
132
|
+
before.written === bar.written - 1 &&
|
|
133
|
+
!context.layout.fermatas.has(before.index) &&
|
|
134
|
+
sameMeter(before, bar));
|
|
135
|
+
}
|
|
136
|
+
/** One bar into a row's parts: a fermata before the first bar, its mark, its cells, its words, a fermata after it. */
|
|
137
|
+
function barParts(bar, parts, before, out, context) {
|
|
138
|
+
const { fermatas } = context.layout;
|
|
139
|
+
if (bar.index === 1 && fermatas.has(0))
|
|
140
|
+
fermata(parts, bar, out);
|
|
141
|
+
if (bar.rehearsal)
|
|
142
|
+
parts.left.push(`@${bar.rehearsal}`);
|
|
143
|
+
parts.measures.push(barCells(bar, simileHere(bar, before, context), out));
|
|
144
|
+
for (const w of bar.words ?? [])
|
|
145
|
+
parts.right.push(wordsText(w.text, bar, out));
|
|
146
|
+
if (fermatas.has(bar.index))
|
|
147
|
+
fermata(parts, bar, out);
|
|
148
|
+
}
|
|
149
|
+
/** A row's parts. */
|
|
150
|
+
function partsOf(row, out, context) {
|
|
151
|
+
const parts = { measures: [], left: [], right: [] };
|
|
152
|
+
for (const [i, bar] of row.bars.entries())
|
|
153
|
+
barParts(bar, parts, row.bars[i - 1], out, context);
|
|
154
|
+
if (row.times)
|
|
155
|
+
parts.right.push(row.times);
|
|
156
|
+
return parts;
|
|
157
|
+
}
|
|
158
|
+
/** A row's grid line, its cells, and the shape for it: its measures by the most beats any bar or measure has. */
|
|
159
|
+
function drawRow(row, out, context) {
|
|
160
|
+
const { measures, left, right } = partsOf(row, out, context);
|
|
161
|
+
const body = measures.map((m) => m.join(" ")).join(" | ");
|
|
162
|
+
const text = [left.join(" "), row.open, body, row.close, right.join(" ")].filter((t) => t !== "");
|
|
163
|
+
const beats = Math.max(maxOf(row.bars.map((b) => beatsOf(b.time ?? b.meter))), maxOf(measures.map((m) => m.length)));
|
|
164
|
+
const cells = measures.reduce((n, m) => n + m.length, 0);
|
|
165
|
+
const written = unjoined(text.join(" "));
|
|
166
|
+
if (written !== text.join(" "))
|
|
167
|
+
textChanged(out.losses, row.bars.at(-1));
|
|
168
|
+
return { row, text: written, cells, shape: `1+${measures.length}x${beats}+1` };
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* The occurrence's grid line: its label, and the widest row's shape. The
|
|
172
|
+
* reference counts a line's cells against the shape's and draws a line
|
|
173
|
+
* with fewer short, so every row fits the widest one's shape and the
|
|
174
|
+
* occurrence is one grid.
|
|
175
|
+
*/
|
|
176
|
+
function startLine(drawn, label) {
|
|
177
|
+
const widest = drawn.reduce((a, b) => (b.cells > a.cells ? b : a));
|
|
178
|
+
const shape = `shape="${widest.shape}"`;
|
|
179
|
+
return directive("start_of_grid", label === null ? shape : `${label} ${shape}`);
|
|
180
|
+
}
|
|
181
|
+
/** A section occurrence with no words as one grid (spec 4.1, 4.3, 4.5, 4.6). */
|
|
182
|
+
export function gridSection(o, out, context) {
|
|
183
|
+
const { layout } = context;
|
|
184
|
+
const drawn = rowsOf(o, context.written, out)
|
|
185
|
+
.filter((row) => row.bars.length)
|
|
186
|
+
.map((row) => drawRow(row, out, context));
|
|
187
|
+
if (out.lines.length)
|
|
188
|
+
out.lines.push("");
|
|
189
|
+
pushAll(out.lines, headingTexts(o, context, out));
|
|
190
|
+
const label = labelOf(o.name, out, o.bars[0]);
|
|
191
|
+
if (!drawn.length)
|
|
192
|
+
return;
|
|
193
|
+
out.lines.push(startLine(drawn, label));
|
|
194
|
+
let prev = context.previous.at(-1)?.bars.at(-1);
|
|
195
|
+
drawn.forEach(({ row, text }, g) => {
|
|
196
|
+
const first = row.bars[0];
|
|
197
|
+
const end = row.bars.at(-1);
|
|
198
|
+
const run = runOf(layout, first);
|
|
199
|
+
if (g === 0 || runOf(layout, drawn[g - 1]?.row.bars[0]) !== run)
|
|
200
|
+
pushAll(out.lines, texts(run, "before", first, out));
|
|
201
|
+
pushAll(out.lines, changeLines(prev, first, context.first, out));
|
|
202
|
+
out.lines.push(text);
|
|
203
|
+
if (layout.openEnds.has(end.index))
|
|
204
|
+
out.lines.push(directive("comment", "repeat ad lib."));
|
|
205
|
+
if (runOf(layout, drawn[g + 1]?.row.bars[0]) !== run)
|
|
206
|
+
pushAll(out.lines, texts(run, "after", end, out));
|
|
207
|
+
prev = end;
|
|
208
|
+
});
|
|
209
|
+
out.lines.push(directive("end_of_grid"));
|
|
210
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { extractPart, parseChart } from "barmark";
|
|
2
|
+
import { comment } from "./changes.js";
|
|
3
|
+
import { defineLines } from "./defines.js";
|
|
4
|
+
import { gridSection } from "./grid.js";
|
|
5
|
+
import { pushAll } from "./lists.js";
|
|
6
|
+
import { Losses } from "./losses.js";
|
|
7
|
+
import { lyricSection } from "./lyrics.js";
|
|
8
|
+
import { headerLines, headerLosses } from "./meta.js";
|
|
9
|
+
import { layout, occurrences } from "./plan.js";
|
|
10
|
+
import { sheetOf } from "./sheet.js";
|
|
11
|
+
import { textChanged, unescapable, untagged } from "./text.js";
|
|
12
|
+
import { writtenOf } from "./written.js";
|
|
13
|
+
const refusal = (message, token = "") => ({
|
|
14
|
+
line: 0,
|
|
15
|
+
severity: "error",
|
|
16
|
+
message,
|
|
17
|
+
token,
|
|
18
|
+
});
|
|
19
|
+
/**
|
|
20
|
+
* The whole chart's diagnostics and what a part's add, in line order: the
|
|
21
|
+
* sheet takes the singer's words from the whole chart (sheet.ts), so a
|
|
22
|
+
* problem there is the sheet's too.
|
|
23
|
+
*/
|
|
24
|
+
function merged(whole, part) {
|
|
25
|
+
const key = (d) => JSON.stringify([d.line, d.severity, d.message, d.token]);
|
|
26
|
+
// All of the whole chart's, even two alike on one line; of the part's, only what the whole lacks.
|
|
27
|
+
const seen = new Set(whole.map(key));
|
|
28
|
+
const added = part.filter((d) => !seen.has(key(d)));
|
|
29
|
+
return [...whole, ...added].sort((a, b) => a.line - b.line);
|
|
30
|
+
}
|
|
31
|
+
/** Why there is nothing to export, or the parse to export (the part asked for, when one is). */
|
|
32
|
+
function parsed(chart, options) {
|
|
33
|
+
const { part } = options;
|
|
34
|
+
const whole = parseChart(chart);
|
|
35
|
+
const parse = part === undefined ? whole : extractPart(chart, part);
|
|
36
|
+
const refused = (diagnostics) => ({ ok: false, diagnostics });
|
|
37
|
+
if (parse === null)
|
|
38
|
+
return refused([refusal(`no part labelled ${part}`, part), ...whole.diagnostics]);
|
|
39
|
+
const diagnostics = parse === whole ? whole.diagnostics : merged(whole.diagnostics, parse.diagnostics);
|
|
40
|
+
if (diagnostics.some((d) => d.severity === "error"))
|
|
41
|
+
return refused(diagnostics);
|
|
42
|
+
return parse.bars.length === 0
|
|
43
|
+
? refused([refusal("the chart has no bars to export"), ...diagnostics])
|
|
44
|
+
: { ok: true, parse, whole, diagnostics };
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* The sheet after its header: text lines written before any bar, then
|
|
48
|
+
* each section occurrence in play order. A sung one is a lyric
|
|
49
|
+
* environment; one with no words a grid.
|
|
50
|
+
*/
|
|
51
|
+
function body(chart, p, options, out) {
|
|
52
|
+
const sheet = sheetOf(chart, p.parse, options, out, p.whole);
|
|
53
|
+
const first = sheet.bars[0];
|
|
54
|
+
if (!first)
|
|
55
|
+
return;
|
|
56
|
+
const lay = layout(sheet);
|
|
57
|
+
pushAll(out.lines, lay.header.map((text) => comment(text, undefined, out)));
|
|
58
|
+
const previous = [];
|
|
59
|
+
const context = {
|
|
60
|
+
previous,
|
|
61
|
+
layout: lay,
|
|
62
|
+
first,
|
|
63
|
+
chorus: { last: null },
|
|
64
|
+
written: writtenOf(sheet),
|
|
65
|
+
};
|
|
66
|
+
for (const o of occurrences(sheet)) {
|
|
67
|
+
if (o.sung)
|
|
68
|
+
lyricSection(o, out, context);
|
|
69
|
+
else
|
|
70
|
+
gridSection(o, out, context);
|
|
71
|
+
previous.push(o);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* The define block after the header (`headed` lines), a blank line on each
|
|
76
|
+
* side, before whatever the body wrote first; none when no chord has a shape.
|
|
77
|
+
*/
|
|
78
|
+
function defined(out, headed) {
|
|
79
|
+
const lines = defineLines(out.uses ?? [], out);
|
|
80
|
+
if (lines.length === 0)
|
|
81
|
+
return;
|
|
82
|
+
const block = ["", ...lines, ...(out.lines[headed] === "" ? [] : [""])];
|
|
83
|
+
out.lines = [...out.lines.slice(0, headed), ...block, ...out.lines.slice(headed)];
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* The sheet's text: every line through `untagged` and `unescapable`, here
|
|
87
|
+
* where the lines are joined so no line can miss them, one final newline.
|
|
88
|
+
* Each piece of a line was already untagged alone; two pieces can still
|
|
89
|
+
* make a tag between them (`<b` and `>loud`), which only the whole line
|
|
90
|
+
* shows. Nothing this package writes holds an angle bracket of its own.
|
|
91
|
+
*/
|
|
92
|
+
function joined(out) {
|
|
93
|
+
if (out.lines.length === 0)
|
|
94
|
+
return "";
|
|
95
|
+
const lines = out.lines.map((line) => unescapable(untagged(line)));
|
|
96
|
+
if (lines.some((line, i) => line !== out.lines[i]))
|
|
97
|
+
textChanged(out.losses);
|
|
98
|
+
return `${lines.join("\n")}\n`;
|
|
99
|
+
}
|
|
100
|
+
/** A chart as a ChordPro lyric sheet, and what it could not carry. Never throws. */
|
|
101
|
+
export function exportLyricsheet(chart, options = {}) {
|
|
102
|
+
const p = parsed(chart, options);
|
|
103
|
+
if (!p.ok)
|
|
104
|
+
return p;
|
|
105
|
+
const out = {
|
|
106
|
+
lines: [],
|
|
107
|
+
losses: new Losses(),
|
|
108
|
+
...(options.diagrams === "guitar" && { uses: [] }),
|
|
109
|
+
};
|
|
110
|
+
pushAll(out.lines, headerLines(p.parse, options.title));
|
|
111
|
+
const headed = out.lines.length;
|
|
112
|
+
headerLosses(p.parse, out.losses, options.title);
|
|
113
|
+
body(chart, p, options, out);
|
|
114
|
+
if (out.uses)
|
|
115
|
+
defined(out, headed);
|
|
116
|
+
const text = joined(out);
|
|
117
|
+
return { ok: true, text, diagnostics: p.diagnostics, losses: out.losses.list() };
|
|
118
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lists of any length. A spread into a call (`push(...x)`, `Math.max(...x)`)
|
|
3
|
+
* passes each item as an argument, and V8 throws a RangeError past about
|
|
4
|
+
* 120,000 of them (Bun's limit is far higher, so only a run under Node
|
|
5
|
+
* shows it); the export never throws, so none is used on a list a chart can
|
|
6
|
+
* make long.
|
|
7
|
+
*/
|
|
8
|
+
/** Adds every item to the end of `target`. */
|
|
9
|
+
export declare function pushAll<T>(target: T[], items: readonly T[]): void;
|
|
10
|
+
/** The largest of the numbers, or -Infinity for none (as `Math.max()`). */
|
|
11
|
+
export declare function maxOf(values: Iterable<number>): number;
|
|
12
|
+
/** The items by key, each key's in their order (`Map.groupBy`, which Node 20 lacks). */
|
|
13
|
+
export declare function groupBy<T, K>(items: readonly T[], key: (item: T) => K): Map<K, T[]>;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lists of any length. A spread into a call (`push(...x)`, `Math.max(...x)`)
|
|
3
|
+
* passes each item as an argument, and V8 throws a RangeError past about
|
|
4
|
+
* 120,000 of them (Bun's limit is far higher, so only a run under Node
|
|
5
|
+
* shows it); the export never throws, so none is used on a list a chart can
|
|
6
|
+
* make long.
|
|
7
|
+
*/
|
|
8
|
+
/** Adds every item to the end of `target`. */
|
|
9
|
+
export function pushAll(target, items) {
|
|
10
|
+
for (const item of items)
|
|
11
|
+
target.push(item);
|
|
12
|
+
}
|
|
13
|
+
/** The largest of the numbers, or -Infinity for none (as `Math.max()`). */
|
|
14
|
+
export function maxOf(values) {
|
|
15
|
+
let max = Number.NEGATIVE_INFINITY;
|
|
16
|
+
for (const v of values)
|
|
17
|
+
if (v > max)
|
|
18
|
+
max = v;
|
|
19
|
+
return max;
|
|
20
|
+
}
|
|
21
|
+
/** The items by key, each key's in their order (`Map.groupBy`, which Node 20 lacks). */
|
|
22
|
+
export function groupBy(items, key) {
|
|
23
|
+
const out = new Map();
|
|
24
|
+
for (const item of items) {
|
|
25
|
+
const k = key(item);
|
|
26
|
+
const list = out.get(k);
|
|
27
|
+
if (list)
|
|
28
|
+
list.push(item);
|
|
29
|
+
else
|
|
30
|
+
out.set(k, [item]);
|
|
31
|
+
}
|
|
32
|
+
return out;
|
|
33
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { PlayedBar } from "barmark";
|
|
2
|
+
import type { LyricsheetLoss, LyricsheetLossCode } from "./types.js";
|
|
3
|
+
/** Losses grouped by code: one each, at its lowest bar, saying how many bars. */
|
|
4
|
+
export declare class Losses {
|
|
5
|
+
private readonly groups;
|
|
6
|
+
add(code: LyricsheetLossCode, message: string, bar?: PlayedBar): void;
|
|
7
|
+
list(): LyricsheetLoss[];
|
|
8
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
const SHOWN = 10;
|
|
2
|
+
const name = (written) => (written === 0 ? "the pickup" : String(written));
|
|
3
|
+
/** Where a loss is, written in parentheses after it: "bar 5", "the pickup", or "3 bars: 2, 5, 9" (at most ten named). */
|
|
4
|
+
function barsText(written) {
|
|
5
|
+
const [only] = written;
|
|
6
|
+
if (written.length === 1 && only !== undefined)
|
|
7
|
+
return only === 0 ? "the pickup" : `bar ${only}`;
|
|
8
|
+
const shown = written.slice(0, SHOWN).map(name).join(", ");
|
|
9
|
+
return `${written.length} bars: ${shown}${written.length > SHOWN ? ", …" : ""}`;
|
|
10
|
+
}
|
|
11
|
+
/** Losses grouped by code: one each, at its lowest bar, saying how many bars. */
|
|
12
|
+
export class Losses {
|
|
13
|
+
groups = new Map();
|
|
14
|
+
add(code, message, bar) {
|
|
15
|
+
const group = this.groups.get(code) ?? { message, lowest: null, bars: new Map() };
|
|
16
|
+
this.groups.set(code, group);
|
|
17
|
+
if (!bar)
|
|
18
|
+
return;
|
|
19
|
+
const seen = group.bars.get(bar.written);
|
|
20
|
+
if (!seen || bar.index < seen.index)
|
|
21
|
+
group.bars.set(bar.written, bar);
|
|
22
|
+
if (!group.lowest || bar.index < group.lowest.index) {
|
|
23
|
+
group.lowest = bar;
|
|
24
|
+
group.message = message;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
list() {
|
|
28
|
+
return [...this.groups].map(([code, g]) => {
|
|
29
|
+
const first = g.lowest;
|
|
30
|
+
if (!first)
|
|
31
|
+
return { code, message: g.message };
|
|
32
|
+
const written = [...g.bars.keys()].sort((a, b) => a - b);
|
|
33
|
+
return {
|
|
34
|
+
code,
|
|
35
|
+
message: `${g.message} (${barsText(written)})`,
|
|
36
|
+
bar: first.index,
|
|
37
|
+
written: first.written,
|
|
38
|
+
};
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A sung section occurrence as a ChordPro lyric environment (spec 4.1 to
|
|
3
|
+
* 4.6): one line per written bar line, each chord before the syllable it
|
|
4
|
+
* lands on, and `{chorus}` for a replayed chorus that sings what the
|
|
5
|
+
* chorus before it sang.
|
|
6
|
+
*/
|
|
7
|
+
import { type PlayedBar, type SimpleSlot, type Slot } from "barmark";
|
|
8
|
+
import type { Layout, Occurrence, Run } from "./plan.js";
|
|
9
|
+
import type { Out } from "./types.js";
|
|
10
|
+
export type Context = {
|
|
11
|
+
/** Every occurrence before this one in play order, sung or not. */
|
|
12
|
+
previous: Occurrence[];
|
|
13
|
+
layout: Layout;
|
|
14
|
+
/** The chart's first played bar, whose key, time and tempo the header wrote. */
|
|
15
|
+
first: PlayedBar;
|
|
16
|
+
/** The last chorus the sheet wrote: what `{chorus}` would recall. */
|
|
17
|
+
chorus: {
|
|
18
|
+
last: {
|
|
19
|
+
form: number | null;
|
|
20
|
+
body: string;
|
|
21
|
+
} | null;
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
declare const LOST: {
|
|
25
|
+
readonly "chord-names": 'a chord name ChordPro\'s chord parser does not know is written as barmark spells it: ChordPro readers may show it as text (diagrams: "guitar" defines a shape for one that has a shape, which they then draw)';
|
|
26
|
+
readonly "note-sets": "a note set is written as its notes' names, as text and not a chord; its octaves and root mark are lost";
|
|
27
|
+
readonly voicing: "a voicing is written as its chord's name; its notes are lost";
|
|
28
|
+
readonly marks: "pushes, accents, hits and pedal marks are not written; the chord is, unmarked";
|
|
29
|
+
readonly "bar-meter": "lyric lines do not show a bar's own meter";
|
|
30
|
+
readonly fermata: "a fermata is written as the annotation *… (ChordPro has none): a reader sees an ellipsis over the words or in a grid's cell, not a fermata sign";
|
|
31
|
+
readonly "chord-moved": "a chord between two syllables of one word is written before the word's next syllable";
|
|
32
|
+
readonly "stacked-verses": "verses stacked on the same bars (not one verse per pass of a repeat) are written one pass per verse, each with its chords: ChordPro has no stacking";
|
|
33
|
+
};
|
|
34
|
+
export declare const lose: (out: Out, code: keyof typeof LOST, bar: PlayedBar) => void;
|
|
35
|
+
/** `label="…"`: the heading as written, quoted as the label's value needs (`labelValue`); null for none. */
|
|
36
|
+
export declare function labelOf(name: string | null, out: Out, bar: PlayedBar | undefined): string | null;
|
|
37
|
+
/** Where the line is: the repeated N.C. or stop that has no attack of its own. */
|
|
38
|
+
export type LineState = {
|
|
39
|
+
last: "nc" | "silence" | null;
|
|
40
|
+
};
|
|
41
|
+
/** What a slot writes in a lyric line (spec 4.3), or null for none (a hold, a repeated N.C.). */
|
|
42
|
+
export declare function slotText(slot: SimpleSlot, st: LineState, bar: PlayedBar, out: Out): string | null;
|
|
43
|
+
/** Each simple slot of a top-level slot with its onset, as `slotTimes` in barmark times them. */
|
|
44
|
+
export declare function membersAt(slot: Slot, at: number, each: number): {
|
|
45
|
+
slot: SimpleSlot;
|
|
46
|
+
at: number;
|
|
47
|
+
}[];
|
|
48
|
+
/** An occurrence's bars by written line (or system), a new line where play goes back (a repeat). */
|
|
49
|
+
export declare function byLine(bars: PlayedBar[]): PlayedBar[][];
|
|
50
|
+
/** The run of bar lines a bar is written in. */
|
|
51
|
+
export declare const runOf: (layout: Layout, bar: PlayedBar | undefined) => Run | undefined;
|
|
52
|
+
/** A run's text lines on one side of it, as comments. */
|
|
53
|
+
export declare const texts: (run: Run | undefined, side: keyof Run, bar: PlayedBar, out: Out) => string[];
|
|
54
|
+
/** A sung occurrence as a lyric environment, or `{chorus}` (spec 4.1, 4.5, 4.6). */
|
|
55
|
+
export declare function lyricSection(o: Occurrence, out: Out, context: Context): void;
|
|
56
|
+
/**
|
|
57
|
+
* The text under a heading with no bar lines of its own (a replay), as
|
|
58
|
+
* comments before its first occurrence, outside any environment.
|
|
59
|
+
*/
|
|
60
|
+
export declare function headingTexts(o: Occurrence, context: Context, out: Out): string[];
|
|
61
|
+
export {};
|