Skip to content

CLI Reference

MIDI Sketch Bach includes a command-line tool for generating Bach-style MIDI files directly from the terminal.

Installation

bash
# Run without installing
npx @libraz/midi-sketch-bach [options]

# Or install globally
npm install -g @libraz/midi-sketch-bach
@libraz/midi-sketch-bach [options]

Usage

@libraz/midi-sketch-bach [options]

Options

OptionAliasTypeDefaultDescription
--form <value>-fstring/number0Musical form (name or 0--8)
--key <value>-knumber0Key as pitch class (0=C, 1=C#, ... 11=B)
--minor-mbooleanfalseUse minor key
--voices <value>-vnumberForm defaultNumber of voices (2--5)
--bpm <value>-bnumber100Tempo in BPM (40--200)
--seed <value>-snumber0Random seed (0 = random)
--instrument <value>-istring/numberForm defaultInstrument (name or 0--5)
--character <value>-cnumber0Subject character type (0--3)
--scale <value>string/number1Duration scale (short/medium/long/full or 0--3)
--target-bars <value>number--Target number of bars
--output <path>-ostringoutput.midOutput file path
--jsonbooleanfalseOutput event data as JSON to stdout

Form Names

Use these names with the --form option:

NameNumber
fugue0
prelude-and-fugue1
trio-sonata2
chorale-prelude3
toccata-and-fugue4
passacaglia5
fantasia-and-fugue6
cello-prelude7
chaconne8

Instrument Names

NameNumber
organ0
harpsichord1
piano2
violin3
cello4
guitar5

Examples

Basic Generation

Generate a default piece (Fugue in C major):

bash
@libraz/midi-sketch-bach -o fugue.mid

Fugue in D Minor

bash
@libraz/midi-sketch-bach --form fugue --key 2 --minor --voices 4 --bpm 76 -o fugue-dm.mid

Prelude and Fugue in C Major

bash
@libraz/midi-sketch-bach --form prelude-and-fugue --key 0 --voices 4 -o prelude-fugue.mid

Trio Sonata in F Major

bash
@libraz/midi-sketch-bach --form trio-sonata --key 5 --bpm 90 -o trio-sonata.mid

Chorale Prelude in A Major

bash
@libraz/midi-sketch-bach --form chorale-prelude --key 9 --voices 4 --bpm 66 -o chorale.mid

Toccata and Fugue in D Minor

bash
@libraz/midi-sketch-bach --form toccata-and-fugue --key 2 --minor --voices 4 -o toccata-fugue.mid

Passacaglia in C Minor

bash
@libraz/midi-sketch-bach --form passacaglia --key 0 --minor --voices 4 --scale long -o passacaglia.mid

Fantasia and Fugue in G Minor

bash
@libraz/midi-sketch-bach --form fantasia-and-fugue --key 7 --minor --voices 4 -o fantasia-fugue.mid

Cello Prelude in G Major

bash
@libraz/midi-sketch-bach --form cello-prelude --key 7 --instrument cello -o cello-prelude.mid

Chaconne in D Minor

bash
@libraz/midi-sketch-bach --form chaconne --key 2 --minor --instrument violin -o chaconne.mid

Deterministic Output with Seed

bash
@libraz/midi-sketch-bach --form fugue --key 7 --minor --seed 42 -o fugue-seed42.mid

Full-Scale Passacaglia

bash
@libraz/midi-sketch-bach --form passacaglia --key 2 --minor --scale full --voices 5 -o passacaglia-full.mid

Target a Specific Bar Count

bash
@libraz/midi-sketch-bach --form invention --key 0 --target-bars 24 -o invention-24bars.mid

Output JSON Event Data

bash
@libraz/midi-sketch-bach --form fugue --key 2 --minor --json > fugue-events.json

Generate with npx

No installation required:

bash
npx @libraz/midi-sketch-bach --form fugue --key 2 --minor -o fugue.mid

JSON Output Format

When using --json, the output follows the EventData structure:

json
{
  "form": "Fugue",
  "key": "D minor",
  "bpm": 80,
  "seed": 12345,
  "total_ticks": 30720,
  "total_bars": 32,
  "description": "Fugue in D minor, 4 voices",
  "tracks": [
    {
      "name": "Soprano",
      "channel": 0,
      "program": 19,
      "note_count": 128,
      "notes": [
        {
          "pitch": 74,
          "velocity": 80,
          "start_tick": 0,
          "duration": 480,
          "voice": 0
        }
      ]
    }
  ]
}

Exit Codes

CodeMeaning
0Success
1Invalid arguments or generation error

Released under the Apache-2.0 License.