@barmark/cli 0.7.0 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -6,6 +6,61 @@ package uses [semantic versioning](https://semver.org/).
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [0.9.0] - 2026-11-09
10
+
11
+ ### Added
12
+
13
+ - **`barmark render --score <chart>`** writes a standalone HTML page of
14
+ the full engraved score instead of the bar grid: the drawing
15
+ `<barmark-score>` shows (`@barmark/score`'s `engraveChart`, by Verovio
16
+ in-process, now built in), themed by `--theme`. Open it in a browser and
17
+ print to PDF from there. `--width <px>` (320 to 40000, default 900) sets
18
+ the width it is engraved at; `--part <label>` engraves one part alone.
19
+ The grid's own flags (`--numerals`, `--no-bar-numbers`,
20
+ `--rehearsal-letters`, `--no-wrap`, `--diagnostics`, `--font-url`) are
21
+ usage errors with `--score` (exit 2), as is `--width` without it. A
22
+ chart that won't engrave writes nothing: `file: reason` on stderr, exit
23
+ 1. In the standalone binaries Verovio is an embedded file loaded on the
24
+ first score, so the other commands start as fast as before.
25
+ - **`--version`** lists `@barmark/score` and Verovio.
26
+ - The binaries' release carries Verovio's licence (`Verovio-LGPL-3.0.txt`,
27
+ `Verovio-GPL-3.0.txt`) beside `Edwin-OFL.txt`.
28
+
29
+ ### Changed
30
+
31
+ - **Uses** @barmark/musicxml 0.7.2 and @barmark/score 0.1.1 (with Verovio
32
+ 6.4.1). `barmark export --musicxml` writes a pickup as an implicit
33
+ measure 0, a chord's degrees hidden when its kind text spells them, no
34
+ slash style on a keyboard's staves, the description in the top part
35
+ only, `×N` over the bar it ends, and an acoustic guitar as one.
36
+
37
+ ## [0.8.0] - 2026-11-08
38
+
39
+ ### Added
40
+
41
+ - **`barmark export --midi <chart> --out <file.mid>`** writes a Standard
42
+ MIDI File of the chart's play order with `@barmark/midi`. `--out` is
43
+ required (a usage error without it, as is giving `--midi` and
44
+ `--musicxml` together); `--part <label>` exports one part. What MIDI
45
+ can't hold goes to stderr as `file: bar N: warning: message`; a chart
46
+ with errors writes nothing.
47
+ - **`barmark import <file.mid|.midi>`** reads a MIDI file as a chart.
48
+ Its diagnostics go to stderr as `file: bar N (track T): severity:
49
+ message`, control characters shown as U+FFFD; it exits 1 when no chart
50
+ came out. `--tracks 2,Piano` names the tracks that carry the harmony,
51
+ by number (from 1) or name; `--tracks` with a MusicXML score, or
52
+ `--part` with a MIDI file, is a usage error.
53
+ - **`--version`** lists `@barmark/midi`.
54
+
55
+ ### Changed
56
+
57
+ - **Uses** barmark 0.21.0, @barmark/render 0.17.1, @barmark/musicxml
58
+ 0.7.1, @barmark/midi 0.2.0 and, through it, @barmark/sync 0.2.1.
59
+ - `barmark export` with no format now says `give a format: --musicxml or
60
+ --midi`.
61
+ - **The npm homepage is https://www.barmark.dev**, not the package's
62
+ folder on GitHub.
63
+
9
64
  ## [0.7.0] - 2026-11-08
10
65
 
11
66
  ### Changed
@@ -242,7 +297,9 @@ The first release of the `barmark` command. Uses `barmark` 0.8.0 and
242
297
  - **The `barmark` command:** `check`, `format`, `render` and `transpose`
243
298
  for `.chart.md` files, on npm and as standalone binaries.
244
299
 
245
- [Unreleased]: https://github.com/kevinpeckham/barmark/compare/cli-v0.7.0...HEAD
300
+ [Unreleased]: https://github.com/kevinpeckham/barmark/compare/cli-v0.9.0...HEAD
301
+ [0.9.0]: https://github.com/kevinpeckham/barmark/compare/cli-v0.8.0...cli-v0.9.0
302
+ [0.8.0]: https://github.com/kevinpeckham/barmark/compare/cli-v0.7.0...cli-v0.8.0
246
303
  [0.7.0]: https://github.com/kevinpeckham/barmark/compare/cli-v0.6.6...cli-v0.7.0
247
304
  [0.6.6]: https://github.com/kevinpeckham/barmark/compare/cli-v0.6.5...cli-v0.6.6
248
305
  [0.6.5]: https://github.com/kevinpeckham/barmark/compare/cli-v0.6.4...cli-v0.6.5
package/README.md CHANGED
@@ -3,8 +3,9 @@
3
3
  The `barmark` command: check, format, part, render, transpose, export and import
4
4
  [Barmark](https://www.barmark.dev) chord charts (`.chart.md` files) in a
5
5
  terminal or in CI. It runs the same `barmark` parser and `@barmark/render`
6
- renderer as barmark.dev, and `@barmark/musicxml` for MusicXML, at exact
7
- versions, so a chart means the same thing everywhere.
6
+ renderer as barmark.dev, `@barmark/musicxml` for MusicXML,
7
+ `@barmark/score` (with Verovio) for full scores and `@barmark/midi` for
8
+ MIDI files, at exact versions, so a chart means the same thing everywhere.
8
9
 
9
10
  ## Install
10
11
 
@@ -35,12 +36,15 @@ dot-folders and symlinks. `-` reads from stdin.
35
36
  barmark check songs # errors and warnings, file:line
36
37
  barmark export --musicxml song.chart.md --out song.musicxml # for MuseScore, Dorico…
37
38
  barmark import song.musicxml --out song.chart.md # or a compressed song.mxl
39
+ barmark export --midi song.chart.md --out song.mid # for a DAW or a practice player
40
+ barmark import song.mid --out song.chart.md # chords named from the notes
38
41
  barmark check --strict --format github songs # warnings fail too; PR annotations
39
42
  barmark format song.chart.md # print it in standard form
40
43
  barmark format --write songs # rewrite the files that change
41
44
  barmark format --check songs # list them and fail, for CI
42
45
  barmark part Bass song.chart.md # one part of a chart of several
43
46
  barmark render song.chart.md --out song.html # a standalone page, font included
47
+ barmark render --score song.chart.md --out song.html # the full engraved score
44
48
  barmark transpose song.chart.md --to E # or --semitones -2
45
49
  ```
46
50
 
@@ -49,8 +53,25 @@ takes the renderer's options as flags: `--theme auto|light|dark`,
49
53
  `--numerals nashville|roman`, `--no-bar-numbers`, `--rehearsal-letters`,
50
54
  `--no-wrap`, `--diagnostics`, `--part <label>` to draw one part, and `--font-url <url>` to link the font
51
55
  instead of embedding it. `barmark <command> --help` lists each command's
52
- flags; `barmark --version` prints the versions of the CLI and its
53
- libraries.
56
+ flags; `barmark --version` prints the versions of the CLI, its
57
+ libraries and Verovio.
58
+
59
+ `render --score` writes a standalone page of the full engraved score
60
+ instead of the bar grid: the drawing `<barmark-score>` shows, the chart
61
+ exported to MusicXML and engraved by [Verovio](https://www.verovio.org),
62
+ which the CLI carries (nothing else to install). The output is HTML: open
63
+ it in a browser, and print it to PDF from there. `--theme` colours it as
64
+ it does the grid, `--part <label>` engraves that part alone, and
65
+ `--width <px>` sets the width it is laid out for (320 to 40000; default
66
+ 900, a comfortable reading width: the page scales the score to fit the
67
+ window or the paper, so the width decides how many bars go on a line,
68
+ not how big the page is). The grid's own flags (`--numerals`,
69
+ `--no-bar-numbers`, `--rehearsal-letters`, `--no-wrap`, `--diagnostics`,
70
+ `--font-url`) are usage errors with `--score`, and `--width` is one
71
+ without it. A chart that won't engrave (one with errors, or no bars)
72
+ writes nothing: the reason goes to stderr as `song.chart.md: Line 5:
73
+ message`, and the exit code is 1. The score's text is set in Verovio's own
74
+ fonts, so Edwin is not embedded.
54
75
 
55
76
  `export --musicxml` writes MusicXML 4.0 that notation programs open and
56
77
  play in the chart's order; `--part <label>` exports one part. A chart
@@ -66,6 +87,20 @@ one part alone, its notes as note sets. It exits 0 when a chart came out,
66
87
  even with warnings, and 1 when none did or the `.mxl` is bad. It does not
67
88
  read stdin.
68
89
 
90
+ `export --midi` writes a Standard MIDI File of the chart's play order
91
+ (chords voiced on piano, the tempo map, markers and lyrics); `--out` is
92
+ required, and `--part <label>` exports one part. What MIDI can't hold is
93
+ listed on stderr as `song.chart.md: bar 9: warning: message`, such as an
94
+ open repeat (`x ...`) played once. A chart with errors is not exported.
95
+
96
+ `import` reads a MIDI file (`.mid` or `.midi`) as a chart too, naming a
97
+ chord from the notes of the tracks that carry the harmony and turning
98
+ lyric events into lyric rows. Its messages are listed on stderr as
99
+ `song.mid: bar 12 (track 2): warning: message`. `--tracks 2,Piano`
100
+ names the harmony's tracks by number or name (ignoring case) instead of
101
+ letting the import choose. As for MusicXML, it exits 1 when no chart came
102
+ out.
103
+
69
104
  ## Exit codes
70
105
 
71
106
  | Code | Meaning |
@@ -92,3 +127,13 @@ version turns that into a pull request you review.
92
127
  MIT. `render` embeds Edwin, which `@barmark/render` ships under the SIL
93
128
  Open Font License (`fonts/OFL.txt` there; the binaries' release carries it
94
129
  as `Edwin-OFL.txt`).
130
+
131
+ `render --score` uses [Verovio](https://github.com/rism-digital/verovio),
132
+ which is LGPL-3.0-only. From npm, Verovio is an ordinary dependency
133
+ (through `@barmark/score`), installed and loaded unchanged. The standalone
134
+ binaries bundle it unmodified, at the version this package pins; the
135
+ release carries
136
+ its licence as `Verovio-LGPL-3.0.txt` and the GPL it builds on as
137
+ `Verovio-GPL-3.0.txt`. The binaries are built from this repository's
138
+ source with `bun scripts/compile.ts`, so they can be rebuilt with another
139
+ Verovio. barmark's own code stays MIT.
package/dist/args.js CHANGED
@@ -20,3 +20,9 @@ export function oneOf(flag, value, allowed) {
20
20
  const list = `${allowed.slice(0, -1).join(", ")} or ${allowed.at(-1)}`;
21
21
  throw new UsageError(`--${flag} must be ${list}, not "${value}"`);
22
22
  }
23
+ /** `--semitones -2`: join a negative number to its flag, which parseArgs would read as a flag. */
24
+ export const joinNegative = (args, flag) => args.flatMap((arg, i) => arg === flag && /^-\d/.test(args[i + 1] ?? "")
25
+ ? [`${flag}=${args[i + 1]}`]
26
+ : i > 0 && args[i - 1] === flag && /^-\d/.test(arg)
27
+ ? []
28
+ : [arg]);
package/dist/export.js CHANGED
@@ -1,30 +1,59 @@
1
- /** barmark export: a chart in another format; MusicXML for now. */
1
+ /** barmark export: a chart in another format: MusicXML, or a MIDI file. */
2
2
  import { basename } from "node:path";
3
+ import { exportMidi } from "@barmark/midi";
3
4
  import { exportMusicXml } from "@barmark/musicxml";
4
5
  import { parseFlags } from "./args.js";
5
6
  import { readCharts, STDIN } from "./inputs.js";
6
7
  import { UsageError } from "./io.js";
7
- import { onlyChart, writeOutput } from "./output.js";
8
+ import { onlyChart, writeFile, writeOutput } from "./output.js";
8
9
  import { textLine } from "./report.js";
10
+ /** `file: bar N: warning: message`, as `check` prints its findings; a loss with no bar has no place. */
11
+ export function lossLine(path, loss) {
12
+ return `${path}: ${loss.bar === undefined ? "" : `bar ${loss.bar}: `}warning: ${loss.message}`;
13
+ }
14
+ const diagnosticLine = (path, d) => d.line ? textLine(path, d) : `${path}: ${d.message}`;
15
+ /** The format the flags name: exactly one; --midi writes bytes, so it needs --out. */
16
+ function formatOf(values) {
17
+ if (values.musicxml && values.midi)
18
+ throw new UsageError("give one format: --musicxml or --midi");
19
+ if (values.midi) {
20
+ if (values.out === undefined)
21
+ throw new UsageError("--midi writes a file: give --out <file.mid>");
22
+ return "midi";
23
+ }
24
+ if (values.musicxml)
25
+ return "musicxml";
26
+ throw new UsageError("give a format: --musicxml or --midi");
27
+ }
9
28
  export async function exportCommand(args, io) {
10
29
  const { values, positionals } = parseFlags(args, {
11
30
  musicxml: { type: "boolean" },
31
+ midi: { type: "boolean" },
12
32
  out: { type: "string" },
13
33
  part: { type: "string" },
14
34
  });
15
- if (!values.musicxml)
16
- throw new UsageError("give a format: --musicxml");
35
+ const format = formatOf(values);
17
36
  const { path, text } = onlyChart(await readCharts(positionals, io, { folders: false }), "export");
18
- const result = exportMusicXml(text, {
37
+ const options = {
19
38
  ...(values.part === undefined ? {} : { part: values.part }),
20
39
  ...(path === STDIN ? {} : { title: basename(path).replace(/(\.chart)?\.[^.]*$/, "") }),
21
- });
40
+ };
41
+ const result = format === "midi" ? exportMidi(text, options) : exportMusicXml(text, options);
22
42
  // As transpose does: problems to stderr, and no file written from a chart with errors.
23
43
  for (const d of result.diagnostics)
24
- io.stderr(`${d.line ? textLine(path, d) : `${path}: ${d.message}`}\n`);
25
- if (result.ok)
44
+ io.stderr(`${diagnosticLine(path, d)}\n`);
45
+ if (!result.ok) {
46
+ if (values.out !== undefined)
47
+ io.stderr(`${values.out} not written: the chart has errors\n`);
48
+ return 1;
49
+ }
50
+ if ("bytes" in result) {
51
+ for (const loss of result.losses)
52
+ io.stderr(`${lossLine(path, loss)}\n`);
53
+ // formatOf made --out required for --midi.
54
+ writeFile(values.out, result.bytes, io);
55
+ }
56
+ else
26
57
  writeOutput(result.xml, values.out, io);
27
- else if (values.out !== undefined)
28
- io.stderr(`${values.out} not written: the chart has errors\n`);
29
- return result.ok ? 0 : 1;
58
+ return 0;
30
59
  }
package/dist/help.js CHANGED
@@ -3,11 +3,11 @@ export const HELP = `Usage: barmark <command> [options]
3
3
 
4
4
  Commands:
5
5
  check <paths…> Report each chart's errors and warnings
6
- export <file> Write a chart as MusicXML (--musicxml)
6
+ export <file> Write a chart as MusicXML or MIDI (--musicxml, --midi)
7
7
  format <paths…> Print or rewrite charts in standard form
8
- import <file> Read a MusicXML score as a chart
8
+ import <file> Read a MusicXML score or a MIDI file as a chart
9
9
  part <label> <file> Print one part of a chart of several
10
- render <file> Write a chart as a standalone HTML page
10
+ render <file> Write a chart as a standalone HTML page (--score: a full score)
11
11
  transpose <file> Print a chart in another key
12
12
 
13
13
  Options:
@@ -29,13 +29,16 @@ inside them; - reads stdin.
29
29
  github: annotations on a pull request
30
30
  json: an array of { file, diagnostics }
31
31
  `,
32
- export: `Usage: barmark export --musicxml <file> [options]
32
+ export: `Usage: barmark export (--musicxml | --midi) <file> [options]
33
33
 
34
34
  The chart as MusicXML 4.0, for MuseScore, Dorico, Sibelius or Finale: one
35
- part per declared part, playing in the chart's order. A chart with errors
36
- is not exported; problems go to stderr.
35
+ part per declared part, playing in the chart's order. Or as a MIDI file
36
+ of its play order, for a DAW or a practice player; what MIDI can't hold
37
+ goes to stderr as file: bar N: warning: message. A chart with errors is
38
+ not exported; problems go to stderr.
37
39
 
38
- --musicxml Export MusicXML (the one format for now)
40
+ --musicxml Export MusicXML
41
+ --midi Export a Standard MIDI File (needs --out)
39
42
  --out <file> Write to a file instead of stdout
40
43
  --part <label> Export only this part
41
44
  `,
@@ -48,16 +51,24 @@ Charts in standard form. With one chart and no option, prints it.
48
51
  `,
49
52
  render: `Usage: barmark render <file> [options]
50
53
 
51
- A standalone HTML page of the chart, with the Edwin font embedded.
54
+ A standalone HTML page of the chart's bar grid, with the Edwin font
55
+ embedded. With --score, a page of the full engraved score instead
56
+ (Verovio, built in): open it in a browser, and print to PDF from there.
57
+ A chart that won't engrave writes nothing; the reason goes to stderr.
52
58
 
53
59
  --out <file> Write to a file instead of stdout
54
60
  --theme auto|light|dark Colors (default auto: follows the reader's system)
61
+ --part <label> Draw only this part
62
+ --score The full score instead of the grid
63
+ --width <px> With --score: the width to engrave at, 320 to
64
+ 40000 (default 900)
65
+
66
+ The grid only:
55
67
  --numerals nashville|roman
56
68
  Show chords as degrees of the key
57
69
  --no-bar-numbers Hide bar numbers
58
70
  --rehearsal-letters Letter each section A, B, C…
59
71
  --no-wrap Keep each row on one line
60
- --part <label> Draw only this part
61
72
  --diagnostics List the chart's problems below it
62
73
  --font-url <url> Link the font instead of embedding it
63
74
  `,
@@ -65,11 +76,15 @@ A standalone HTML page of the chart, with the Edwin font embedded.
65
76
 
66
77
  A MusicXML score (.musicxml, .xml or compressed .mxl; from MuseScore, Dorico, Sibelius, Finale…) as a chart.
67
78
  Chord symbols become chords; what the chart can't carry over goes to
68
- stderr as file: measure N (Part): message. Exits 0 when a chart came out,
69
- even with warnings; 1 when none did or the .mxl is bad. Does not read stdin.
79
+ stderr as file: measure N (Part): message. A MIDI file (.mid or .midi)
80
+ too: chords named from its notes, lyrics as lyric rows; its messages as
81
+ file: bar N (track T): message. Exits 0 when a chart came out, even with
82
+ warnings; 1 when none did or the file is bad. Does not read stdin.
70
83
 
71
84
  --out <file> Write to a file instead of stdout
72
- --part <name> Import one part alone, its notes as note sets
85
+ --part <name> MusicXML: import one part alone, its notes as note sets
86
+ --tracks <list> MIDI: the tracks that carry the harmony, by number
87
+ or name, comma-separated (default: chosen)
73
88
  `,
74
89
  part: `Usage: barmark part <label> <file> [options]
75
90
 
package/dist/import.js CHANGED
@@ -1,54 +1,95 @@
1
- /** barmark import: a MusicXML score (.musicxml, .xml or .mxl) as a chart. */
1
+ /** barmark import: a MusicXML score (.musicxml, .xml or .mxl) or a MIDI file (.mid or .midi) as a chart. */
2
+ import { importMidi } from "@barmark/midi";
2
3
  import { decodeXmlBytes, importMusicXml, readMxl } from "@barmark/musicxml";
3
- import { parseFlags } from "./args.js";
4
+ import { joinNegative, parseFlags } from "./args.js";
4
5
  import { readBytes } from "./inputs.js";
5
6
  import { UsageError } from "./io.js";
6
7
  import { writeOutput } from "./output.js";
7
8
  /** Control characters shown as U+FFFD: nothing from a file reaches the terminal as an escape sequence. */
8
- const printable = (text) => text.replace(/\p{Cc}/gu, "\uFFFD");
9
+ const printable = (text) => text.replace(/\p{Cc}/gu, "�");
10
+ /** `path: place: severity: message`, the place left out when there is none. */
11
+ const line = (path, where, d) => {
12
+ const place = where.filter(Boolean).join(" ");
13
+ return printable(`${path}: ${place ? `${place}: ` : ""}${d.severity}: ${d.message}`);
14
+ };
9
15
  /**
10
16
  * `file: measure N (Part): severity: message`, as `check` prints its
11
17
  * `file:line: severity: message`; each piece of the place left out when the
12
18
  * diagnostic has none, or when the message names its measures itself.
13
19
  */
14
20
  export function importLine(path, d) {
15
- const where = [
21
+ return line(path, [
16
22
  // A diagnostic with `measures` names them in its message; the message is never searched.
17
23
  d.measure === undefined || d.measures !== undefined ? "" : `measure ${d.measure}`,
18
24
  d.part === undefined ? "" : `(${d.part})`,
19
- ].filter(Boolean);
20
- const place = where.length ? `${where.join(" ")}: ` : "";
21
- return printable(`${path}: ${place}${d.severity}: ${d.message}`);
25
+ ], d);
26
+ }
27
+ /** `file: bar N (track T): severity: message`, as `importLine` prints a score's. */
28
+ export function midiLine(path, d) {
29
+ return line(path, [d.bar === undefined ? "" : `bar ${d.bar}`, d.track === undefined ? "" : `(track ${d.track})`], d);
30
+ }
31
+ /** A `--tracks` entry that looks like a number: a track number, 1-based. */
32
+ function trackNumber(entry) {
33
+ const n = Number(entry);
34
+ if (!Number.isSafeInteger(n) || n < 1)
35
+ throw new UsageError(`--tracks: "${entry}" is not a track number (they start at 1)`);
36
+ return n;
37
+ }
38
+ /** `--tracks 2,Piano`: each entry a 1-based track number, or else a track's name. */
39
+ export function tracksOf(list) {
40
+ const entries = list.split(",").map((entry) => entry.trim());
41
+ if (entries.some((entry) => entry === ""))
42
+ throw new UsageError("--tracks takes track numbers or names, separated by commas");
43
+ return entries.map((entry) => (/^[+-]?\d+$/.test(entry) ? trackNumber(entry) : entry));
22
44
  }
23
45
  /** The score's XML text: a .mxl is unpacked. Rejects with a message naming the problem. */
24
46
  async function scoreText(bytes, path) {
25
47
  return /\.mxl$/i.test(path) ? readMxl(bytes) : decodeXmlBytes(bytes);
26
48
  }
49
+ function midiImport(bytes, path, tracks) {
50
+ const result = importMidi(new Uint8Array(bytes), tracks === undefined ? {} : { tracks });
51
+ return { text: result.text, lines: result.diagnostics.map((d) => midiLine(path, d)) };
52
+ }
53
+ async function scoreImport(bytes, path, part) {
54
+ let xml;
55
+ try {
56
+ xml = await scoreText(bytes, path);
57
+ }
58
+ catch (error) {
59
+ return {
60
+ text: "",
61
+ lines: [printable(`${path}: ${error instanceof Error ? error.message : String(error)}`)],
62
+ };
63
+ }
64
+ const result = importMusicXml(xml, part === undefined ? {} : { part });
65
+ return { text: result.text, lines: result.diagnostics.map((d) => importLine(path, d)) };
66
+ }
27
67
  export async function importCommand(args, io) {
28
- const { values, positionals } = parseFlags(args, {
68
+ const { values, positionals } = parseFlags(joinNegative(args, "--tracks"), {
29
69
  out: { type: "string" },
30
70
  part: { type: "string" },
71
+ tracks: { type: "string" },
31
72
  });
32
73
  const [path, ...rest] = positionals;
33
74
  if (path === undefined)
34
75
  throw new UsageError("no file given");
35
76
  if (rest.length)
36
77
  throw new UsageError("import takes one file");
78
+ const midi = /\.midi?$/i.test(path);
79
+ if (midi && values.part !== undefined)
80
+ throw new UsageError("--part is for MusicXML; a MIDI file takes --tracks");
81
+ if (!midi && values.tracks !== undefined)
82
+ throw new UsageError("--tracks is for MIDI; a MusicXML score takes --part");
83
+ const tracks = values.tracks === undefined ? undefined : tracksOf(values.tracks);
37
84
  const bytes = readBytes(path, io);
38
- let xml;
39
- try {
40
- xml = await scoreText(bytes, path);
41
- }
42
- catch (error) {
43
- io.stderr(`${printable(`${path}: ${error instanceof Error ? error.message : String(error)}`)}\n`);
44
- return 1;
45
- }
46
- const result = importMusicXml(xml, values.part === undefined ? {} : { part: values.part });
47
- for (const d of result.diagnostics)
48
- io.stderr(`${importLine(path, d)}\n`);
85
+ const { text, lines } = midi
86
+ ? midiImport(bytes, path, tracks)
87
+ : await scoreImport(bytes, path, values.part);
88
+ for (const l of lines)
89
+ io.stderr(`${l}\n`);
49
90
  // As export does: no file written from an import that gave nothing.
50
- if (result.text === "")
91
+ if (text === "")
51
92
  return 1;
52
- writeOutput(result.text, values.out, io);
93
+ writeOutput(text, values.out, io);
53
94
  return 0;
54
95
  }
package/dist/main.js CHANGED
@@ -21,8 +21,8 @@ export const COMMANDS = {
21
21
  export async function main(argv, io) {
22
22
  const [name, ...args] = argv;
23
23
  if (name === "--version" || name === "-v") {
24
- const { cli, barmark, render, musicxml } = io.versions;
25
- io.stdout(`@barmark/cli ${cli} (barmark ${barmark}, @barmark/render ${render}, @barmark/musicxml ${musicxml})\n`);
24
+ const { cli, barmark, render, musicxml, score, midi, verovio } = io.versions;
25
+ io.stdout(`@barmark/cli ${cli} (barmark ${barmark}, @barmark/render ${render}, @barmark/musicxml ${musicxml}, @barmark/score ${score}, @barmark/midi ${midi}, verovio ${verovio})\n`);
26
26
  return 0;
27
27
  }
28
28
  if (name === "--help" || name === "-h") {
package/dist/node-io.js CHANGED
@@ -13,6 +13,9 @@ function versions() {
13
13
  barmark: versionAt("barmark"),
14
14
  render: versionAt("@barmark/render"),
15
15
  musicxml: versionAt("@barmark/musicxml"),
16
+ score: versionAt("@barmark/score"),
17
+ midi: versionAt("@barmark/midi"),
18
+ verovio: versionAt("verovio"),
16
19
  };
17
20
  }
18
21
  async function readStdin() {
@@ -43,6 +46,7 @@ export function processIo() {
43
46
  export function nodeIo() {
44
47
  return {
45
48
  ...processIo(),
49
+ verovio: async () => (await import("verovio/wasm")).default,
46
50
  font: () => readFileSync(fileURLToPath(import.meta.resolve("@barmark/render/fonts/Edwin-Roman.otf"))),
47
51
  versions: versions(),
48
52
  };
package/dist/output.js CHANGED
@@ -4,12 +4,15 @@ import { renameSync, rmSync, statSync, writeFileSync } from "node:fs";
4
4
  import { basename, dirname, join, resolve } from "node:path";
5
5
  import { UsageError } from "./io.js";
6
6
  export function writeOutput(text, out, io) {
7
- if (out === undefined) {
7
+ if (out === undefined)
8
8
  io.stdout(text);
9
- return;
10
- }
9
+ else
10
+ writeFile(out, text, io);
11
+ }
12
+ /** Text or bytes to the file named by --out; `Io` stays text-only, so bytes never reach stdout. */
13
+ export function writeFile(out, data, io) {
11
14
  try {
12
- writeFileSync(resolve(io.cwd, out), text);
15
+ writeFileSync(resolve(io.cwd, out), data);
13
16
  }
14
17
  catch {
15
18
  throw new UsageError(`cannot write ${out}`);
package/dist/render.js CHANGED
@@ -1,10 +1,15 @@
1
- /** barmark render: a chart as a standalone HTML page, with Edwin embedded unless linked. */
1
+ /**
2
+ * barmark render: a chart as a standalone HTML page: the bar grid, with Edwin
3
+ * embedded unless linked, or with --score the full engraved score.
4
+ */
2
5
  import { basename } from "node:path";
3
6
  import { renderChart } from "@barmark/render";
4
7
  import { extractPart, parseChart } from "barmark";
5
8
  import { oneOf, parseFlags } from "./args.js";
6
9
  import { readCharts } from "./inputs.js";
10
+ import { UsageError } from "./io.js";
7
11
  import { onlyChart, writeOutput } from "./output.js";
12
+ import { DEFAULT_WIDTH, MAX_WIDTH, MIN_WIDTH, scoreBody } from "./score.js";
8
13
  const THEMES = ["auto", "light", "dark"];
9
14
  const NUMERALS = ["nashville", "roman"];
10
15
  const escapeHtml = (text) => text.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
@@ -22,22 +27,44 @@ ${body}
22
27
  </html>
23
28
  `;
24
29
  }
25
- export async function render(args, io) {
26
- const { values, positionals } = parseFlags(args, {
27
- out: { type: "string" },
28
- theme: { type: "string", default: "auto" },
29
- numerals: { type: "string" },
30
- "no-bar-numbers": { type: "boolean", default: false },
31
- "rehearsal-letters": { type: "boolean", default: false },
32
- "no-wrap": { type: "boolean", default: false },
33
- diagnostics: { type: "boolean", default: false },
34
- "font-url": { type: "string" },
35
- part: { type: "string" },
36
- });
30
+ const FLAGS = {
31
+ out: { type: "string" },
32
+ theme: { type: "string", default: "auto" },
33
+ numerals: { type: "string" },
34
+ "no-bar-numbers": { type: "boolean", default: false },
35
+ "rehearsal-letters": { type: "boolean", default: false },
36
+ "no-wrap": { type: "boolean", default: false },
37
+ diagnostics: { type: "boolean", default: false },
38
+ "font-url": { type: "string" },
39
+ part: { type: "string" },
40
+ score: { type: "boolean", default: false },
41
+ width: { type: "string" },
42
+ };
43
+ /** The grid's own flags: a score has no numerals, bar-number switch, letters, rows, problem list or Edwin. */
44
+ const GRID_ONLY = [
45
+ "numerals",
46
+ "no-bar-numbers",
47
+ "rehearsal-letters",
48
+ "no-wrap",
49
+ "diagnostics",
50
+ "font-url",
51
+ ];
52
+ /** The engraving width --width gives, refusing what Verovio cannot lay out. */
53
+ function widthOf(value) {
54
+ if (value === undefined)
55
+ return DEFAULT_WIDTH;
56
+ const width = /^\d+$/.test(value) ? Number(value) : Number.NaN;
57
+ if (!(width >= MIN_WIDTH && width <= MAX_WIDTH))
58
+ throw new UsageError(`--width must be a whole number of pixels from ${MIN_WIDTH} to ${MAX_WIDTH}, not "${value}"`);
59
+ return width;
60
+ }
61
+ /** The flags as `renderChart`'s options. */
62
+ function gridOptions(values, theme, io) {
63
+ if (values.width !== undefined)
64
+ throw new UsageError("--width needs --score");
37
65
  const options = {
38
66
  fontUrl: values["font-url"] ?? `data:font/otf;base64,${Buffer.from(io.font()).toString("base64")}`,
39
67
  };
40
- const theme = oneOf("theme", values.theme, THEMES);
41
68
  if (theme !== "auto")
42
69
  options.theme = theme;
43
70
  if (values.numerals !== undefined)
@@ -50,15 +77,42 @@ export async function render(args, io) {
50
77
  options.wrap = false;
51
78
  if (values.diagnostics)
52
79
  options.diagnostics = true;
80
+ return options;
81
+ }
82
+ /** --score's width, refusing the grid's own flags. */
83
+ function scoreWidth(values) {
84
+ const given = GRID_ONLY.find((flag) => values[flag] !== undefined && values[flag] !== false);
85
+ if (given)
86
+ throw new UsageError(`--${given} does not apply to --score`);
87
+ return widthOf(values.width);
88
+ }
89
+ export async function render(args, io) {
90
+ const { values, positionals } = parseFlags(args, FLAGS);
91
+ const theme = oneOf("theme", values.theme, THEMES);
92
+ const options = values.score ? null : gridOptions(values, theme, io);
93
+ const width = values.score ? scoreWidth(values) : 0;
53
94
  const { path, text } = onlyChart(await readCharts(positionals, io, { folders: false }), "render");
54
- if (values.part !== undefined) {
55
- if (!extractPart(text, values.part)) {
56
- io.stderr(`${path}: no part labelled ${values.part}\n`);
57
- return 1;
58
- }
59
- options.part = values.part;
95
+ if (values.part !== undefined && !extractPart(text, values.part)) {
96
+ io.stderr(`${path}: no part labelled ${values.part}\n`);
97
+ return 1;
60
98
  }
61
99
  const title = parseChart(text).title ?? basename(path).replace(/\.chart\.md$|\.md$/, "");
62
- writeOutput(htmlDocument(title, renderChart(text, options)), values.out, io);
100
+ if (options) {
101
+ if (values.part !== undefined)
102
+ options.part = values.part;
103
+ writeOutput(htmlDocument(title, renderChart(text, options)), values.out, io);
104
+ return 0;
105
+ }
106
+ const score = await scoreBody(text, {
107
+ theme,
108
+ width,
109
+ label: title,
110
+ ...(values.part === undefined ? {} : { part: values.part }),
111
+ }, io);
112
+ if (!score.ok) {
113
+ io.stderr(`${path}: ${score.reason}\n`);
114
+ return 1;
115
+ }
116
+ writeOutput(htmlDocument(title, score.html), values.out, io);
63
117
  return 0;
64
118
  }
package/dist/score.js ADDED
@@ -0,0 +1,62 @@
1
+ /**
2
+ * barmark render --score: the chart engraved as a full score, the drawing
3
+ * `<barmark-score>` shows, by Verovio in this process (no worker). Verovio's
4
+ * module (about 7 MB) comes from `io.verovio` on the first score only, so the
5
+ * other commands never load it.
6
+ */
7
+ import { exportMusicXml } from "@barmark/musicxml";
8
+ import { stylesheet } from "@barmark/render";
9
+ import { engraveChart, engraveXml, scorePane, scoreStyles } from "@barmark/score";
10
+ /** The width a score is engraved at unless --width says otherwise, in CSS pixels. */
11
+ export const DEFAULT_WIDTH = 900;
12
+ /** `@barmark/score` engraves nothing narrower; Verovio's page width (2.5 units a pixel) stops at 100000. */
13
+ export const MIN_WIDTH = 320;
14
+ export const MAX_WIDTH = 40_000;
15
+ let toolkit;
16
+ /** One toolkit for the process, its log off: Verovio would otherwise print to stdout, into the page. */
17
+ function loadToolkit(verovio) {
18
+ toolkit ??= (async () => {
19
+ const esm = await import("verovio/esm");
20
+ const module = await (await verovio())();
21
+ // Verovio's typings omit the log switch; it is exported all the same (as the score worker uses it).
22
+ const { enableLog, LOG_OFF } = esm;
23
+ enableLog(LOG_OFF, module);
24
+ return new esm.VerovioToolkit(module);
25
+ })();
26
+ return toolkit;
27
+ }
28
+ /** Why the export failed, worded as `engraveChart` words it: the first error, with its line when it has one. */
29
+ function exportReason(diagnostics) {
30
+ const first = diagnostics.find((d) => d.severity === "error") ?? diagnostics[0];
31
+ if (first === undefined)
32
+ return "the chart could not be exported";
33
+ return first.line > 0 ? `Line ${first.line}: ${first.message}` : first.message;
34
+ }
35
+ /** One part alone: its MusicXML (`@barmark/musicxml`'s `part`), engraved as `engraveChart` engraves the whole. */
36
+ function engravePart(vrv, text, part, width) {
37
+ const exported = exportMusicXml(text, { part });
38
+ if (!exported.ok)
39
+ return { ok: false, reason: exportReason(exported.diagnostics) };
40
+ try {
41
+ return { ok: true, svg: engraveXml(vrv, exported.xml, { width }).svg };
42
+ }
43
+ catch (e) {
44
+ return { ok: false, reason: e instanceof Error ? e.message : String(e) };
45
+ }
46
+ }
47
+ /** The score's markup and styles for a page body, or why the chart cannot be engraved. */
48
+ export async function scoreBody(text, options, io) {
49
+ const vrv = await loadToolkit(io.verovio);
50
+ const engraved = options.part === undefined
51
+ ? engraveChart(vrv, text, { width: options.width })
52
+ : engravePart(vrv, text, options.part, options.width);
53
+ if (!engraved.ok)
54
+ return { ok: false, reason: engraved.reason };
55
+ try {
56
+ const pane = scorePane(engraved.svg, options.theme, options.label);
57
+ return { ok: true, html: `<style>${stylesheet}${scoreStyles}</style>\n${pane}` };
58
+ }
59
+ catch (e) {
60
+ return { ok: false, reason: e instanceof Error ? e.message : String(e) };
61
+ }
62
+ }
package/dist/transpose.js CHANGED
@@ -1,16 +1,10 @@
1
1
  /** barmark transpose: a chart in another key, by key name or by semitones. */
2
2
  import { transposeChart } from "barmark";
3
- import { parseFlags } from "./args.js";
3
+ import { joinNegative, parseFlags } from "./args.js";
4
4
  import { readCharts } from "./inputs.js";
5
5
  import { UsageError } from "./io.js";
6
6
  import { onlyChart, writeOutput } from "./output.js";
7
7
  import { textLine } from "./report.js";
8
- /** `--semitones -2`: join a negative number to its flag, which parseArgs would read as a flag. */
9
- const joinNegative = (args) => args.flatMap((arg, i) => arg === "--semitones" && /^-\d/.test(args[i + 1] ?? "")
10
- ? [`--semitones=${args[i + 1]}`]
11
- : i > 0 && args[i - 1] === "--semitones" && /^-\d/.test(arg)
12
- ? []
13
- : [arg]);
14
8
  function moveOf(to, semitones) {
15
9
  if (to !== undefined && semitones !== undefined)
16
10
  throw new UsageError("give --to or --semitones, not both");
@@ -23,7 +17,7 @@ function moveOf(to, semitones) {
23
17
  return { semitones: Number(semitones) };
24
18
  }
25
19
  export async function transpose(args, io) {
26
- const { values, positionals } = parseFlags(joinNegative(args), {
20
+ const { values, positionals } = parseFlags(joinNegative(args, "--semitones"), {
27
21
  to: { type: "string" },
28
22
  semitones: { type: "string" },
29
23
  out: { type: "string" },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@barmark/cli",
3
- "version": "0.7.0",
3
+ "version": "0.9.0",
4
4
  "description": "The barmark command: check, format, render, transpose and export barmark chord charts (.chart.md) from the terminal and in CI.",
5
5
  "type": "module",
6
6
  "bin": {
@@ -27,7 +27,7 @@
27
27
  ],
28
28
  "author": "Kevin Peckham",
29
29
  "license": "MIT",
30
- "homepage": "https://github.com/kevinpeckham/barmark/tree/main/packages/cli#readme",
30
+ "homepage": "https://www.barmark.dev",
31
31
  "repository": {
32
32
  "type": "git",
33
33
  "url": "git+https://github.com/kevinpeckham/barmark.git",
@@ -37,9 +37,12 @@
37
37
  "url": "https://github.com/kevinpeckham/barmark/issues"
38
38
  },
39
39
  "dependencies": {
40
- "barmark": "0.20.0",
41
- "@barmark/render": "0.17.0",
42
- "@barmark/musicxml": "0.7.0"
40
+ "barmark": "0.21.0",
41
+ "@barmark/render": "0.17.1",
42
+ "@barmark/musicxml": "0.7.2",
43
+ "@barmark/midi": "0.2.0",
44
+ "@barmark/score": "0.1.1",
45
+ "verovio": "6.4.1"
43
46
  },
44
47
  "scripts": {
45
48
  "build": "rm -rf dist && tsc -p tsconfig.build.json",