@barmark/lyricsheet 0.0.0-stage → 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 +168 -2
- 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 +47 -5
|
@@ -0,0 +1,269 @@
|
|
|
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 { memberShares } from "barmark";
|
|
8
|
+
import { changeLines, comment } from "./changes.js";
|
|
9
|
+
import { groupBy, pushAll } from "./lists.js";
|
|
10
|
+
import { lyricLine } from "./merge.js";
|
|
11
|
+
import { directive } from "./meta.js";
|
|
12
|
+
import { isGap, spelled } from "./spell.js";
|
|
13
|
+
import { labelValue, plain, spaced, textChanged, unhashed, unjoined, untagged } from "./text.js";
|
|
14
|
+
const LOST = {
|
|
15
|
+
"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)',
|
|
16
|
+
"note-sets": "a note set is written as its notes' names, as text and not a chord; its octaves and root mark are lost",
|
|
17
|
+
voicing: "a voicing is written as its chord's name; its notes are lost",
|
|
18
|
+
marks: "pushes, accents, hits and pedal marks are not written; the chord is, unmarked",
|
|
19
|
+
"bar-meter": "lyric lines do not show a bar's own meter",
|
|
20
|
+
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",
|
|
21
|
+
"chord-moved": "a chord between two syllables of one word is written before the word's next syllable",
|
|
22
|
+
"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",
|
|
23
|
+
};
|
|
24
|
+
export const lose = (out, code, bar) => out.losses.add(code, LOST[code], bar);
|
|
25
|
+
/** The environment a heading's first word names (spec 4.6): verse, chorus or bridge, any case; else verse. */
|
|
26
|
+
function envOf(name) {
|
|
27
|
+
const word = /^[a-z]+/i.exec(name ?? "")?.[0]?.toLowerCase();
|
|
28
|
+
return word === "chorus" || word === "bridge" ? word : "verse";
|
|
29
|
+
}
|
|
30
|
+
/** `label="…"`: the heading as written, quoted as the label's value needs (`labelValue`); null for none. */
|
|
31
|
+
export function labelOf(name, out, bar) {
|
|
32
|
+
if (name === null)
|
|
33
|
+
return null;
|
|
34
|
+
const label = untagged(name);
|
|
35
|
+
if (label !== name || /[{}]|\\n/.test(name) || spaced(name) !== name)
|
|
36
|
+
textChanged(out.losses, bar);
|
|
37
|
+
return `label="${labelValue(label)}"`;
|
|
38
|
+
}
|
|
39
|
+
/** `{start_of_verse: label="…"}`, or with no label for the unnamed section. */
|
|
40
|
+
function startLine(env, name, out, bar) {
|
|
41
|
+
const label = labelOf(name, out, bar);
|
|
42
|
+
return label === null ? directive(`start_of_${env}`) : directive(`start_of_${env}`, label);
|
|
43
|
+
}
|
|
44
|
+
/** A note set's notes as text, octaves and root mark left out, each once. */
|
|
45
|
+
const noteNames = (pitches) => [...new Set(pitches.map((p) => p.replace(/-?\d+$/, "")))].join(" ");
|
|
46
|
+
/** What a chord or note set slot writes, and what it loses. */
|
|
47
|
+
function chordText(slot, bar, out) {
|
|
48
|
+
if (slot.push || slot.articulation || slot.pedal)
|
|
49
|
+
lose(out, "marks", bar);
|
|
50
|
+
if (slot.chord.kind === "notes") {
|
|
51
|
+
lose(out, "note-sets", bar);
|
|
52
|
+
return `*${noteNames(slot.chord.pitches)}`;
|
|
53
|
+
}
|
|
54
|
+
// With diagrams, the shapes decide which of these losses stand (defines.ts).
|
|
55
|
+
if (out.uses)
|
|
56
|
+
out.uses.push({ chord: slot.chord, voicing: slot.voicing ?? null, bar });
|
|
57
|
+
else {
|
|
58
|
+
if (slot.voicing)
|
|
59
|
+
lose(out, "voicing", bar);
|
|
60
|
+
if (isGap(slot.chord))
|
|
61
|
+
lose(out, "chord-names", bar);
|
|
62
|
+
}
|
|
63
|
+
return spelled(slot.chord);
|
|
64
|
+
}
|
|
65
|
+
/** N.C. or a stop, unless the slot before was the same (a repeat has no attack of its own). */
|
|
66
|
+
function restText(kind, st) {
|
|
67
|
+
if (st.last === kind)
|
|
68
|
+
return null;
|
|
69
|
+
st.last = kind;
|
|
70
|
+
return kind === "nc" ? "*N.C." : "*Stop";
|
|
71
|
+
}
|
|
72
|
+
/** What a slot writes in a lyric line (spec 4.3), or null for none (a hold, a repeated N.C.). */
|
|
73
|
+
export function slotText(slot, st, bar, out) {
|
|
74
|
+
if (slot.kind === "hold")
|
|
75
|
+
return null;
|
|
76
|
+
if ((slot.kind === "nc" || slot.kind === "silence") && slot.push)
|
|
77
|
+
lose(out, "marks", bar);
|
|
78
|
+
if (slot.kind === "nc" || slot.kind === "silence")
|
|
79
|
+
return restText(slot.kind, st);
|
|
80
|
+
st.last = null;
|
|
81
|
+
return slot.kind === "unknown" ? "*?" : chordText(slot, bar, out);
|
|
82
|
+
}
|
|
83
|
+
/** Each simple slot of a top-level slot with its onset, as `slotTimes` in barmark times them. */
|
|
84
|
+
export function membersAt(slot, at, each) {
|
|
85
|
+
const members = slot.kind === "group" ? slot.members : [slot];
|
|
86
|
+
const shares = memberShares(members);
|
|
87
|
+
let t = at;
|
|
88
|
+
return members.map((m, i) => {
|
|
89
|
+
const here = { slot: m, at: t };
|
|
90
|
+
t += each * (shares[i] ?? 0);
|
|
91
|
+
return here;
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
/** A bar's words, rehearsal mark and chords, from `offset` whole notes into the line. */
|
|
95
|
+
function harmonyPieces(bar, offset, st, out) {
|
|
96
|
+
const length = bar.meter.n / bar.meter.d;
|
|
97
|
+
const each = length / Math.max(1, bar.slots.length);
|
|
98
|
+
const pieces = bar.rehearsal ? [{ at: offset, chord: `*@${bar.rehearsal}` }] : [];
|
|
99
|
+
let changed = false;
|
|
100
|
+
// The bar's words by slot, once: a scan of every word for each slot is quadratic in a long bar.
|
|
101
|
+
const words = groupBy(bar.words ?? [], (w) => w.at);
|
|
102
|
+
const wordsAt = (k) => {
|
|
103
|
+
for (const w of words.get(k) ?? []) {
|
|
104
|
+
const text = plain(w.text);
|
|
105
|
+
changed ||= text !== w.text;
|
|
106
|
+
const at = offset + k * each;
|
|
107
|
+
pieces.push(k === bar.slots.length ? { at, chord: `*${text}`, alone: true } : { at, chord: `*${text}` });
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
bar.slots.forEach((slot, k) => {
|
|
111
|
+
wordsAt(k);
|
|
112
|
+
for (const m of membersAt(slot, offset + k * each, each)) {
|
|
113
|
+
const chord = slotText(m.slot, st, bar, out);
|
|
114
|
+
if (chord !== null)
|
|
115
|
+
pieces.push({ at: m.at, chord });
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
wordsAt(bar.slots.length);
|
|
119
|
+
// A pickup's short meter is how a pickup is written, not a bar's own meter.
|
|
120
|
+
if (bar.time && bar.written !== 0)
|
|
121
|
+
lose(out, "bar-meter", bar);
|
|
122
|
+
return { pieces, changed };
|
|
123
|
+
}
|
|
124
|
+
/** A syllable with brackets and braces as ChordPro shows them unchanged, or itself when it has none. */
|
|
125
|
+
function shown(s) {
|
|
126
|
+
if (s.kind !== "sung" || s.parts.every((p) => plain(p) === p))
|
|
127
|
+
return s;
|
|
128
|
+
return { ...s, parts: s.parts.map(plain) };
|
|
129
|
+
}
|
|
130
|
+
/** A bar's sung syllables, from `offset` whole notes into the line. */
|
|
131
|
+
function sungPieces(bar, offset) {
|
|
132
|
+
const pieces = [];
|
|
133
|
+
let changed = false;
|
|
134
|
+
for (const l of bar.lyrics ?? [])
|
|
135
|
+
for (const s of l.syllables) {
|
|
136
|
+
const syllable = shown(s.syllable);
|
|
137
|
+
changed ||= syllable !== s.syllable;
|
|
138
|
+
pieces.push({ at: offset + s.at, syllable });
|
|
139
|
+
}
|
|
140
|
+
return { pieces, changed };
|
|
141
|
+
}
|
|
142
|
+
/** `*…` at `at`, alone, when a fermata follows played bar `after` (0: before the first bar). */
|
|
143
|
+
function fermataAt(bar, after, at, fermatas, out) {
|
|
144
|
+
if (!fermatas.has(after))
|
|
145
|
+
return [];
|
|
146
|
+
lose(out, "fermata", bar);
|
|
147
|
+
return [{ at, chord: "*…", alone: true }];
|
|
148
|
+
}
|
|
149
|
+
/** One written line's bars as one lyric line, and its losses. */
|
|
150
|
+
function lyricText(bars, fermatas, out) {
|
|
151
|
+
const pieces = [];
|
|
152
|
+
const owner = [];
|
|
153
|
+
const st = { last: null };
|
|
154
|
+
let offset = 0;
|
|
155
|
+
let changed = false;
|
|
156
|
+
for (const bar of bars) {
|
|
157
|
+
const length = bar.meter.n / bar.meter.d;
|
|
158
|
+
const harmony = harmonyPieces(bar, offset, st, out);
|
|
159
|
+
const sung = sungPieces(bar, offset);
|
|
160
|
+
const own = [
|
|
161
|
+
...fermataAt(bar, bar.index === 1 ? 0 : -1, offset, fermatas, out),
|
|
162
|
+
...harmony.pieces,
|
|
163
|
+
...sung.pieces,
|
|
164
|
+
...fermataAt(bar, bar.index, offset + length, fermatas, out),
|
|
165
|
+
];
|
|
166
|
+
pushAll(pieces, own);
|
|
167
|
+
for (const _ of own)
|
|
168
|
+
owner.push(bar);
|
|
169
|
+
changed ||= harmony.changed || sung.changed;
|
|
170
|
+
offset += length;
|
|
171
|
+
}
|
|
172
|
+
const text = lyricLine(pieces, (i) => {
|
|
173
|
+
const bar = owner[i];
|
|
174
|
+
if (bar)
|
|
175
|
+
lose(out, "chord-moved", bar);
|
|
176
|
+
});
|
|
177
|
+
const written = unjoined(unhashed(text));
|
|
178
|
+
if (changed || written !== text)
|
|
179
|
+
textChanged(out.losses, bars[0]);
|
|
180
|
+
return written;
|
|
181
|
+
}
|
|
182
|
+
/** An occurrence's bars by written line (or system), a new line where play goes back (a repeat). */
|
|
183
|
+
export function byLine(bars) {
|
|
184
|
+
const out = [];
|
|
185
|
+
let prev;
|
|
186
|
+
for (const bar of bars) {
|
|
187
|
+
if (!prev || bar.line !== prev.line || bar.written <= prev.written)
|
|
188
|
+
out.push([]);
|
|
189
|
+
out.at(-1)?.push(bar);
|
|
190
|
+
prev = bar;
|
|
191
|
+
}
|
|
192
|
+
return out;
|
|
193
|
+
}
|
|
194
|
+
/** The run of bar lines a bar is written in. */
|
|
195
|
+
export const runOf = (layout, bar) => bar && layout.runs.get(bar.line);
|
|
196
|
+
/** A run's text lines on one side of it, as comments. */
|
|
197
|
+
export const texts = (run, side, bar, out) => (run?.[side] ?? []).map((t) => comment(t, bar, out));
|
|
198
|
+
/** The lyric line, and the end of an open repeat after it. */
|
|
199
|
+
function played(bars, last, out, layout) {
|
|
200
|
+
const text = lyricText(bars, layout.fermatas, out);
|
|
201
|
+
const lines = text === "" ? [] : [text];
|
|
202
|
+
if (layout.openEnds.has(last.index))
|
|
203
|
+
lines.push(directive("comment", "repeat ad lib."));
|
|
204
|
+
return lines;
|
|
205
|
+
}
|
|
206
|
+
/** One written line's lines: its run's text before it, changes, the lyric line, an open repeat's end, text after. */
|
|
207
|
+
function lineLines(bars, around, out, context) {
|
|
208
|
+
const { layout } = context;
|
|
209
|
+
const [first] = bars;
|
|
210
|
+
const last = bars.at(-1);
|
|
211
|
+
if (!first || !last)
|
|
212
|
+
return [];
|
|
213
|
+
const run = runOf(layout, first);
|
|
214
|
+
const fresh = around.opens || runOf(layout, around.prev) !== run;
|
|
215
|
+
return [
|
|
216
|
+
...(fresh ? texts(run, "before", first, out) : []),
|
|
217
|
+
...changeLines(around.prev, first, context.first, out),
|
|
218
|
+
...played(bars, last, out, layout),
|
|
219
|
+
...(runOf(layout, around.next) !== run ? texts(run, "after", last, out) : []),
|
|
220
|
+
];
|
|
221
|
+
}
|
|
222
|
+
/** The lines inside an occurrence's environment, written line by written line. */
|
|
223
|
+
function bodyOf(o, out, context) {
|
|
224
|
+
const groups = byLine(o.bars);
|
|
225
|
+
let prev = context.previous.at(-1)?.bars.at(-1);
|
|
226
|
+
return groups.flatMap((bars, g) => {
|
|
227
|
+
const around = { prev, next: groups[g + 1]?.[0], opens: g === 0 };
|
|
228
|
+
prev = bars.at(-1) ?? prev;
|
|
229
|
+
return lineLines(bars, around, out, context);
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
/** Whether `{chorus}` says the same: a replay of the written chorus last written, singing its lines, with its chords. */
|
|
233
|
+
function recalls(o, body, context) {
|
|
234
|
+
const before = context.chorus.last;
|
|
235
|
+
return o.replay && o.form !== null && before?.form === o.form && before.body === body;
|
|
236
|
+
}
|
|
237
|
+
/** A sung occurrence as a lyric environment, or `{chorus}` (spec 4.1, 4.5, 4.6). */
|
|
238
|
+
export function lyricSection(o, out, context) {
|
|
239
|
+
const lines = bodyOf(o, out, context);
|
|
240
|
+
if (o.stacked)
|
|
241
|
+
for (const bar of o.bars)
|
|
242
|
+
if (bar.lyrics?.length)
|
|
243
|
+
lose(out, "stacked-verses", bar);
|
|
244
|
+
const env = envOf(o.name);
|
|
245
|
+
const body = lines.join("\n");
|
|
246
|
+
const recalled = env === "chorus" && recalls(o, body, context);
|
|
247
|
+
if (env === "chorus")
|
|
248
|
+
context.chorus.last = { form: o.form, body };
|
|
249
|
+
if (out.lines.length)
|
|
250
|
+
out.lines.push("");
|
|
251
|
+
pushAll(out.lines, headingTexts(o, context, out));
|
|
252
|
+
if (recalled)
|
|
253
|
+
out.lines.push(directive("chorus"));
|
|
254
|
+
else {
|
|
255
|
+
out.lines.push(startLine(env, o.name, out, o.bars[0]));
|
|
256
|
+
pushAll(out.lines, lines);
|
|
257
|
+
out.lines.push(directive(`end_of_${env}`));
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* The text under a heading with no bar lines of its own (a replay), as
|
|
262
|
+
* comments before its first occurrence, outside any environment.
|
|
263
|
+
*/
|
|
264
|
+
export function headingTexts(o, context, out) {
|
|
265
|
+
const before = context.previous.at(-1);
|
|
266
|
+
const first = before?.heading !== o.heading || before?.form !== o.form;
|
|
267
|
+
const texts = o.heading === null || !first ? [] : (context.layout.headings.get(o.heading) ?? []);
|
|
268
|
+
return texts.map((t) => comment(t, o.bars[0], out));
|
|
269
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One written line's chords and sung syllables as one ChordPro lyric line
|
|
3
|
+
* (spec 4.2), merged by time. ChordPro keeps order, not duration, so the
|
|
4
|
+
* merge is exact about order and drops only timing.
|
|
5
|
+
*/
|
|
6
|
+
import type { Syllable } from "barmark";
|
|
7
|
+
/**
|
|
8
|
+
* A chord (its text as written between brackets: a name, or `*…` for an
|
|
9
|
+
* annotation) or a syllable, at `at` whole notes from the line's start.
|
|
10
|
+
* A chord marked `alone` (a fermata after its bar) never claims the
|
|
11
|
+
* syllable that starts at its time: it stands alone before it.
|
|
12
|
+
*/
|
|
13
|
+
export type Piece = {
|
|
14
|
+
at: number;
|
|
15
|
+
chord: string;
|
|
16
|
+
alone?: true;
|
|
17
|
+
} | {
|
|
18
|
+
at: number;
|
|
19
|
+
syllable: Syllable;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* One line's chords and syllables as ChordPro (spec 4.2). A chord that
|
|
23
|
+
* attacks where a syllable starts goes directly before it (`[C]riv`); one
|
|
24
|
+
* where none starts stands alone in time order (`waiting [D] for`). A
|
|
25
|
+
* chord that attacks between two syllables of one word (a design call the
|
|
26
|
+
* spec leaves open) cannot stand alone without splitting the word, so it
|
|
27
|
+
* is written before the word's next syllable (`wait[D]ing`), and `moved`
|
|
28
|
+
* is told its index in `pieces`. Continued syllables join with no hyphen;
|
|
29
|
+
* an elision (`~`) is a space; a held syllable writes nothing.
|
|
30
|
+
*/
|
|
31
|
+
export declare function lyricLine(pieces: Piece[], moved?: (index: number) => void): string;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/** Times equal within this many whole notes are a tie, as `@barmark/musicxml`'s lyrics compare them. */
|
|
2
|
+
const EPSILON = 1e-9;
|
|
3
|
+
/** A time as a whole number of EPSILONs, so ties sort and match exactly. */
|
|
4
|
+
const tick = (at) => Math.round(at / EPSILON);
|
|
5
|
+
const isSung = (p) => "syllable" in p && p.syllable.kind === "sung";
|
|
6
|
+
/** The pieces in time order, each with its index; on a tie a chord comes first, otherwise the order given. */
|
|
7
|
+
function inOrder(pieces) {
|
|
8
|
+
const rank = (p) => ("chord" in p ? 0 : 1);
|
|
9
|
+
return pieces
|
|
10
|
+
.map((p, i) => ({ p, i, t: tick(p.at) }))
|
|
11
|
+
.sort((a, b) => a.t - b.t || rank(a.p) - rank(b.p) || a.i - b.i);
|
|
12
|
+
}
|
|
13
|
+
const bracketed = (chord) => `[${chord}]`;
|
|
14
|
+
/** A chord that lands on no syllable: alone in time order, a space between it and any neighbour. */
|
|
15
|
+
function standAlone(s, chord) {
|
|
16
|
+
s.text += `${s.text === "" ? "" : " "}${bracketed(chord)}`;
|
|
17
|
+
}
|
|
18
|
+
/** A sung syllable, after the chords that land on it; a space before it unless it continues a word. */
|
|
19
|
+
function sing(s, { syllable }, last) {
|
|
20
|
+
const space = s.text === "" || s.inWord ? "" : " ";
|
|
21
|
+
const word = syllable.parts.join(" ");
|
|
22
|
+
// A word that runs on past the line's end keeps its hyphen, so it reads as unfinished.
|
|
23
|
+
const hyphen = syllable.continues && last ? "-" : "";
|
|
24
|
+
s.text += `${space}${s.pending.map(bracketed).join("")}${word}${hyphen}`;
|
|
25
|
+
s.pending = [];
|
|
26
|
+
s.inWord = syllable.continues;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* One line's chords and syllables as ChordPro (spec 4.2). A chord that
|
|
30
|
+
* attacks where a syllable starts goes directly before it (`[C]riv`); one
|
|
31
|
+
* where none starts stands alone in time order (`waiting [D] for`). A
|
|
32
|
+
* chord that attacks between two syllables of one word (a design call the
|
|
33
|
+
* spec leaves open) cannot stand alone without splitting the word, so it
|
|
34
|
+
* is written before the word's next syllable (`wait[D]ing`), and `moved`
|
|
35
|
+
* is told its index in `pieces`. Continued syllables join with no hyphen;
|
|
36
|
+
* an elision (`~`) is a space; a held syllable writes nothing.
|
|
37
|
+
*/
|
|
38
|
+
export function lyricLine(pieces, moved) {
|
|
39
|
+
const starts = new Set(pieces.filter(isSung).map((p) => tick(p.at)));
|
|
40
|
+
const ordered = inOrder(pieces);
|
|
41
|
+
const sungTotal = pieces.filter(isSung).length;
|
|
42
|
+
let sung = 0;
|
|
43
|
+
const s = { text: "", inWord: false, pending: [] };
|
|
44
|
+
for (const { p, i } of ordered) {
|
|
45
|
+
if (isSung(p)) {
|
|
46
|
+
sung += 1;
|
|
47
|
+
sing(s, p, sung === sungTotal);
|
|
48
|
+
}
|
|
49
|
+
else if ("chord" in p) {
|
|
50
|
+
const later = sung < sungTotal;
|
|
51
|
+
if (!p.alone && starts.has(tick(p.at)))
|
|
52
|
+
s.pending.push(p.chord);
|
|
53
|
+
else if (s.inWord && later) {
|
|
54
|
+
s.pending.push(p.chord);
|
|
55
|
+
moved?.(i);
|
|
56
|
+
}
|
|
57
|
+
else
|
|
58
|
+
standAlone(s, p.chord);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
for (const chord of s.pending)
|
|
62
|
+
standAlone(s, chord);
|
|
63
|
+
return s.text;
|
|
64
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { ChartParse, Key } from "barmark";
|
|
2
|
+
import type { Losses } from "./losses.js";
|
|
3
|
+
/** The one place a directive line is built: `{name: value}`, or `{name}` with no value. */
|
|
4
|
+
export declare function directive(name: string, value?: string): string;
|
|
5
|
+
/** The key as `{key}` writes it: ionian is major and aeolian minor (the same notes); the other modes have none. */
|
|
6
|
+
export declare function keyText(key: Key): string | null;
|
|
7
|
+
/** What the header leaves out or changes: a church mode in the key (spec 4.6), markup in its text. */
|
|
8
|
+
export declare function headerLosses(parse: ChartParse, losses: Losses, title?: string): void;
|
|
9
|
+
/** Title (always), credits, then the opening key, time and tempo (spec 4.6); nothing else is invented. */
|
|
10
|
+
export declare function headerLines(parse: ChartParse, title: string | undefined): string[];
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { spaced, textChanged, untagged } from "./text.js";
|
|
2
|
+
/**
|
|
3
|
+
* A value as one line of plain text: `{`, `}`, every control character (C0,
|
|
4
|
+
* DEL, C1, so tab, VT, NUL and NEL too) and U+2028/2029 become a space, runs
|
|
5
|
+
* of spaces collapse and the ends are trimmed. A non-breaking space and the
|
|
6
|
+
* other Unicode spaces stay: the output rules forbid tabs and controls only.
|
|
7
|
+
*/
|
|
8
|
+
const clean = (value) => spaced(value.replace(/[{}]/g, " ")).replace(/ {2,}/g, " ").trim();
|
|
9
|
+
/** The one place a directive line is built: `{name: value}`, or `{name}` with no value. */
|
|
10
|
+
export function directive(name, value) {
|
|
11
|
+
return value === undefined ? `{${name}}` : `{${name}: ${clean(value)}}`;
|
|
12
|
+
}
|
|
13
|
+
/** The key as `{key}` writes it: ionian is major and aeolian minor (the same notes); the other modes have none. */
|
|
14
|
+
export function keyText(key) {
|
|
15
|
+
if (key.mode === "major" || key.mode === "ionian")
|
|
16
|
+
return key.tonic;
|
|
17
|
+
if (key.mode === "minor" || key.mode === "aeolian")
|
|
18
|
+
return `${key.tonic}m`;
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
/** The chart's opening key, when it has one. */
|
|
22
|
+
const openingKey = (parse) => parse.bars[0]?.key ?? null;
|
|
23
|
+
/** The title the header writes: the chart's, else the option's, else none (written empty). */
|
|
24
|
+
const titleOf = (parse, title) => [parse.title, title].find((t) => t != null && clean(t) !== "") ?? "";
|
|
25
|
+
/** The header's free text: its title and credits. */
|
|
26
|
+
const headerTexts = (parse, title) => {
|
|
27
|
+
const { credits: c } = parse;
|
|
28
|
+
return [titleOf(parse, title), ...c.composer, ...c.lyricist, ...c.arranger, ...c.copyright];
|
|
29
|
+
};
|
|
30
|
+
/** Whether a header text is written otherwise: tag-like markup, braces or a control character. */
|
|
31
|
+
const changed = (t) => untagged(t) !== t || /[{}]/.test(t) || spaced(t) !== t;
|
|
32
|
+
/** What the header leaves out or changes: a church mode in the key (spec 4.6), markup in its text. */
|
|
33
|
+
export function headerLosses(parse, losses, title) {
|
|
34
|
+
if (headerTexts(parse, title).some(changed))
|
|
35
|
+
textChanged(losses);
|
|
36
|
+
const key = openingKey(parse);
|
|
37
|
+
const bar = parse.bars[0];
|
|
38
|
+
if (key && keyText(key) === null)
|
|
39
|
+
losses.add("key-mode", `ChordPro's {key} cannot name the mode ${key.mode}, so it is left out`, bar);
|
|
40
|
+
}
|
|
41
|
+
/** Title (always), credits, then the opening key, time and tempo (spec 4.6); nothing else is invented. */
|
|
42
|
+
export function headerLines(parse, title) {
|
|
43
|
+
// Always a title line: with none, the reference takes the first lyric line for the title.
|
|
44
|
+
const out = [directive("title", untagged(titleOf(parse, title)))];
|
|
45
|
+
const { credits } = parse;
|
|
46
|
+
const add = (name, text) => out.push(directive(name, untagged(text)));
|
|
47
|
+
for (const who of credits.composer)
|
|
48
|
+
add("composer", who);
|
|
49
|
+
for (const who of credits.lyricist)
|
|
50
|
+
add("lyricist", who);
|
|
51
|
+
for (const who of credits.arranger)
|
|
52
|
+
add("meta", `arranger ${who}`);
|
|
53
|
+
for (const text of credits.copyright)
|
|
54
|
+
add("copyright", text);
|
|
55
|
+
const first = parse.bars[0];
|
|
56
|
+
const key = openingKey(parse);
|
|
57
|
+
const keyed = key && keyText(key);
|
|
58
|
+
if (keyed)
|
|
59
|
+
out.push(directive("key", keyed));
|
|
60
|
+
if (first) {
|
|
61
|
+
const time = first.time ?? first.meter;
|
|
62
|
+
out.push(directive("time", `${time.n}/${time.d}`));
|
|
63
|
+
if (first.tempo !== null)
|
|
64
|
+
out.push(directive("tempo", String(first.tempo)));
|
|
65
|
+
}
|
|
66
|
+
return out;
|
|
67
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The played bars as the sheet writes them (spec 4.1, 4.5): section
|
|
3
|
+
* occurrences in play order, each one environment unless a repeat sings a
|
|
4
|
+
* new verse on bars it played (then one per verse sung), and stacked verses
|
|
5
|
+
* one pass each; and what sits between the bars (text lines, fermatas, the
|
|
6
|
+
* end of an open repeat) by where it is written.
|
|
7
|
+
*/
|
|
8
|
+
import type { ChartParse, PlayedBar } from "barmark";
|
|
9
|
+
export type Occurrence = {
|
|
10
|
+
name: string | null;
|
|
11
|
+
bars: PlayedBar[];
|
|
12
|
+
/** Some bar sings words: a lyric environment, else (the next task) a grid. */
|
|
13
|
+
sung: boolean;
|
|
14
|
+
/** A replay heading's occurrence, or a jump's return through a section. */
|
|
15
|
+
replay: boolean;
|
|
16
|
+
/** The one verse sung, when one is; null when none is, or several. */
|
|
17
|
+
verse: number | null;
|
|
18
|
+
/** Which written section this plays (`SectionOccurrence.form`, shared by its replays); null before the first heading. */
|
|
19
|
+
form: number | null;
|
|
20
|
+
/** The line of the heading it is played under (a replay's own heading); null before the first heading. */
|
|
21
|
+
heading: number | null;
|
|
22
|
+
/** The line of the heading its bars are written under (a replay's written section's); null before the first heading. */
|
|
23
|
+
source: number | null;
|
|
24
|
+
/** One pass of verses stacked on the same bars (spec 4.5): its bars hold that verse only. */
|
|
25
|
+
stacked?: true;
|
|
26
|
+
};
|
|
27
|
+
/** The played bars as section occurrences in play order; bars before the first heading are one unnamed occurrence. */
|
|
28
|
+
export declare function occurrences(parse: ChartParse): Occurrence[];
|
|
29
|
+
/** Text lines around a run of consecutive bar lines (a system, or several written together). */
|
|
30
|
+
export type Run = {
|
|
31
|
+
before: string[];
|
|
32
|
+
after: string[];
|
|
33
|
+
};
|
|
34
|
+
/** What the sheet writes between bars, found by where it is written. */
|
|
35
|
+
export type Layout = {
|
|
36
|
+
/** Text lines before the first section and the first bar: written after the header. */
|
|
37
|
+
header: string[];
|
|
38
|
+
/** Each bar line's run, by its line number. */
|
|
39
|
+
runs: Map<number, Run>;
|
|
40
|
+
/**
|
|
41
|
+
* Text lines under a heading with no bar lines of its own (a replay), by
|
|
42
|
+
* the heading's line: written once, outside any environment, before it.
|
|
43
|
+
*/
|
|
44
|
+
headings: Map<number, string[]>;
|
|
45
|
+
/** The played bars a fermata follows, by `PlayedBar.index`; 0 for one before the first bar. */
|
|
46
|
+
fermatas: Set<number>;
|
|
47
|
+
/** The last bar of each open repeat's pass, by `PlayedBar.index`. */
|
|
48
|
+
openEnds: Set<number>;
|
|
49
|
+
};
|
|
50
|
+
/** What the sheet writes between a chart's bars (spec 4.3, 4.5, 4.6). */
|
|
51
|
+
export declare function layout(parse: ChartParse): Layout;
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import { groupBy, pushAll } from "./lists.js";
|
|
2
|
+
const played = (s) => s.firstBar !== null && s.lastBar !== null;
|
|
3
|
+
/** The first section from `s` that has not ended before bar `index`. */
|
|
4
|
+
function advance(sections, s, index) {
|
|
5
|
+
let at = s;
|
|
6
|
+
while (at < sections.length && index > (sections[at]?.lastBar ?? 0))
|
|
7
|
+
at += 1;
|
|
8
|
+
return at;
|
|
9
|
+
}
|
|
10
|
+
/** The section bar `index` is in, if it has begun; null for a bar before it (before the first heading). */
|
|
11
|
+
const within = (section, index) => section && index >= section.firstBar ? section : null;
|
|
12
|
+
const spanOf = (section) => section === null
|
|
13
|
+
? { name: null, replay: false, form: null, heading: null, source: null, bars: [] }
|
|
14
|
+
: {
|
|
15
|
+
name: section.name,
|
|
16
|
+
replay: section.replayOf !== null,
|
|
17
|
+
form: section.form,
|
|
18
|
+
heading: section.line,
|
|
19
|
+
source: section.replayOf ?? section.line,
|
|
20
|
+
bars: [],
|
|
21
|
+
};
|
|
22
|
+
/** The bars of each section occurrence in play order; bars before the first heading are one unnamed span. */
|
|
23
|
+
function spans(parse) {
|
|
24
|
+
const sections = parse.sections.filter(played);
|
|
25
|
+
const out = [];
|
|
26
|
+
let s = 0;
|
|
27
|
+
let owner;
|
|
28
|
+
for (const bar of parse.bars) {
|
|
29
|
+
s = advance(sections, s, bar.index);
|
|
30
|
+
const section = within(sections[s], bar.index);
|
|
31
|
+
if (section !== owner)
|
|
32
|
+
out.push(spanOf(section));
|
|
33
|
+
owner = section;
|
|
34
|
+
out.at(-1)?.bars.push(bar);
|
|
35
|
+
}
|
|
36
|
+
return out;
|
|
37
|
+
}
|
|
38
|
+
/** A span's passes: a new one where the written bar number falls back (a repeat going round). */
|
|
39
|
+
function passes(bars) {
|
|
40
|
+
const out = [];
|
|
41
|
+
let prev;
|
|
42
|
+
for (const bar of bars) {
|
|
43
|
+
if (!prev || bar.written <= prev.written)
|
|
44
|
+
out.push([]);
|
|
45
|
+
out.at(-1)?.push(bar);
|
|
46
|
+
prev = bar;
|
|
47
|
+
}
|
|
48
|
+
return out;
|
|
49
|
+
}
|
|
50
|
+
/** Which verses a bar sings, as a key. */
|
|
51
|
+
const verseKey = (bar) => (bar.lyrics ?? []).map((l) => l.verse).join(",");
|
|
52
|
+
/**
|
|
53
|
+
* A span's environments: its passes together, except that a pass singing
|
|
54
|
+
* a different verse on a bar the environment already played starts a new
|
|
55
|
+
* one (pass 1 verse 1, pass 2 verse 2). A repeat singing the same row
|
|
56
|
+
* again stays in the one environment, its bars written again as played.
|
|
57
|
+
*/
|
|
58
|
+
function environments(bars) {
|
|
59
|
+
const out = [];
|
|
60
|
+
let sang = new Map();
|
|
61
|
+
for (const pass of passes(bars)) {
|
|
62
|
+
const differs = (b) => sang.has(b.written) && sang.get(b.written) !== verseKey(b);
|
|
63
|
+
if (!out.length || pass.some(differs)) {
|
|
64
|
+
out.push([]);
|
|
65
|
+
sang = new Map();
|
|
66
|
+
}
|
|
67
|
+
for (const b of pass)
|
|
68
|
+
sang.set(b.written, verseKey(b));
|
|
69
|
+
const last = out.at(-1);
|
|
70
|
+
if (last)
|
|
71
|
+
pushAll(last, pass);
|
|
72
|
+
}
|
|
73
|
+
return out;
|
|
74
|
+
}
|
|
75
|
+
const sings = (bar) => bar.lyrics?.some((l) => l.syllables.some((s) => s.syllable.kind === "sung")) ?? false;
|
|
76
|
+
const versesOf = (bar) => {
|
|
77
|
+
const { lyrics, ...rest } = bar;
|
|
78
|
+
return { rest, byVerse: groupBy(lyrics ?? [], (l) => l.verse) };
|
|
79
|
+
};
|
|
80
|
+
/** A bar with only verse `verse`'s words, or none. */
|
|
81
|
+
function withVerse({ rest, byVerse }, verse) {
|
|
82
|
+
const kept = byVerse.get(verse);
|
|
83
|
+
return kept ? { ...rest, lyrics: kept } : rest;
|
|
84
|
+
}
|
|
85
|
+
/** One environment's bars as occurrences: themselves, or with verses stacked, one per verse in verse order. */
|
|
86
|
+
function ofEnvironment(span, bars) {
|
|
87
|
+
const verses = [...new Set(bars.flatMap((b) => (b.lyrics ?? []).map((l) => l.verse)))].sort((a, b) => a - b);
|
|
88
|
+
const { bars: _, ...base } = span;
|
|
89
|
+
if (!bars.some((b) => (b.lyrics?.length ?? 0) > 1)) {
|
|
90
|
+
const verse = verses.length === 1 ? (verses[0] ?? null) : null;
|
|
91
|
+
return [{ ...base, bars, sung: bars.some(sings), verse }];
|
|
92
|
+
}
|
|
93
|
+
// A stacked verse that sings no syllable (`lyrics 2: | -- |`) writes nothing.
|
|
94
|
+
const indexed = bars.map(versesOf);
|
|
95
|
+
const sung = verses
|
|
96
|
+
.map((verse) => ({ verse, own: indexed.map((b) => withVerse(b, verse)) }))
|
|
97
|
+
.filter((v) => v.own.some(sings));
|
|
98
|
+
if (!sung.length)
|
|
99
|
+
return [{ ...base, bars, sung: false, verse: null }];
|
|
100
|
+
const stacked = sung.length > 1 ? { stacked: true } : {};
|
|
101
|
+
return sung.map(({ verse, own }) => ({ ...base, bars: own, sung: true, verse, ...stacked }));
|
|
102
|
+
}
|
|
103
|
+
/** The played bars as section occurrences in play order; bars before the first heading are one unnamed occurrence. */
|
|
104
|
+
export function occurrences(parse) {
|
|
105
|
+
return spans(parse).flatMap((span) => environments(span.bars).flatMap((bars) => ofEnvironment(span, bars)));
|
|
106
|
+
}
|
|
107
|
+
/** The text lines waiting, given to the heading with no bar lines, the last run, or the header. */
|
|
108
|
+
function settle(w) {
|
|
109
|
+
if (!w.pending.length)
|
|
110
|
+
return;
|
|
111
|
+
if (w.heading !== null && !w.hasRun)
|
|
112
|
+
w.headings.set(w.heading, w.pending);
|
|
113
|
+
else
|
|
114
|
+
pushAll(w.last ? w.last.after : w.header, w.pending);
|
|
115
|
+
w.pending = [];
|
|
116
|
+
}
|
|
117
|
+
/** A bar line: in the current run, or the first of a new one, taking the text before it. */
|
|
118
|
+
function barLine(w, line) {
|
|
119
|
+
w.current ??= { before: w.pending, after: [] };
|
|
120
|
+
w.pending = [];
|
|
121
|
+
w.runs.set(line, w.current);
|
|
122
|
+
w.last = w.current;
|
|
123
|
+
w.hasRun = true;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Text lines by the run of bar lines they belong to: those before a run
|
|
127
|
+
* are its `before`; those after a section's last run, before the next
|
|
128
|
+
* heading or the chart's end, its `after`; those under a heading with no
|
|
129
|
+
* bar lines its own; those before any bar and any heading the header's.
|
|
130
|
+
*/
|
|
131
|
+
function runsOf(parse) {
|
|
132
|
+
const w = {
|
|
133
|
+
...{ header: [], runs: new Map(), headings: new Map(), pending: [] },
|
|
134
|
+
...{ current: null, last: null, heading: null, hasRun: false },
|
|
135
|
+
};
|
|
136
|
+
for (const line of parse.lines) {
|
|
137
|
+
if (line.kind === "text") {
|
|
138
|
+
w.current = null;
|
|
139
|
+
w.pending.push(line.text);
|
|
140
|
+
}
|
|
141
|
+
else if (line.kind === "bars" || line.kind === "invalid")
|
|
142
|
+
barLine(w, line.line);
|
|
143
|
+
else if (line.kind === "section") {
|
|
144
|
+
settle(w);
|
|
145
|
+
Object.assign(w, { current: null, heading: line.line, hasRun: false });
|
|
146
|
+
}
|
|
147
|
+
else if (line.kind === "directive")
|
|
148
|
+
w.current = null;
|
|
149
|
+
}
|
|
150
|
+
settle(w);
|
|
151
|
+
return { header: w.header, runs: w.runs, headings: w.headings };
|
|
152
|
+
}
|
|
153
|
+
/** The bars fermatas follow and the bars that end an open repeat's pass, from the events. */
|
|
154
|
+
function eventMarks(events, into, before = null) {
|
|
155
|
+
let lastBar = before;
|
|
156
|
+
for (const e of events) {
|
|
157
|
+
if (e.kind === "bar")
|
|
158
|
+
lastBar = e.bar.index;
|
|
159
|
+
else if (e.kind === "fermata")
|
|
160
|
+
into.fermatas.add(lastBar ?? 0);
|
|
161
|
+
else {
|
|
162
|
+
const end = eventMarks(e.events, into, lastBar);
|
|
163
|
+
if (end !== null && end !== lastBar)
|
|
164
|
+
into.openEnds.add(end);
|
|
165
|
+
lastBar = end;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
return lastBar;
|
|
169
|
+
}
|
|
170
|
+
/** What the sheet writes between a chart's bars (spec 4.3, 4.5, 4.6). */
|
|
171
|
+
export function layout(parse) {
|
|
172
|
+
const marks = { fermatas: new Set(), openEnds: new Set() };
|
|
173
|
+
eventMarks(parse.events, marks);
|
|
174
|
+
return { ...runsOf(parse), ...marks };
|
|
175
|
+
}
|