Getting Started
MIDI Sketch Bach is an algorithmic composition engine that generates Bach-style instrumental MIDI. Built on Baroque music theory and counterpoint rules, it produces editable MIDI data featuring fugues, toccatas, passacaglias, and other Baroque forms.
New to Baroque or notation terms?
You do not need to read staff notation to use the library. If terms such as voice, bar, key, fugue, or counterpoint are new, start with the Music Primer for Engineers.
Install
Alpha — not yet published to npm
This package is in alpha and has not been published to the npm registry yet. The commands below will not work until the first public release. Until then, try it in the live demo on this site.
npm install @libraz/midi-sketch-bachyarn add @libraz/midi-sketch-bachpnpm add @libraz/midi-sketch-bachbun add @libraz/midi-sketch-bachQuick Example
import { init, BachGenerator } from '@libraz/midi-sketch-bach'
import { writeFileSync } from 'fs'
// Initialize the WASM module
await init()
// Create a generator and produce a fugue in D minor
const generator = new BachGenerator()
generator.generate({
form: 'fugue',
key: 2,
isMinor: true,
character: 'severe',
bpm: 80
})
// Get MIDI file data and save
const midi = generator.getMidi()
writeFileSync('bach-fugue.mid', midi)
// Get structured event data
const events = generator.getEvents()
console.log(events.description)
console.log(`${events.total_bars} bars, ${events.tracks.length} tracks`)
// Clean up
generator.destroy()
// Note: examples use ESM imports — set "type": "module" in package.json (or use .mjs)CLI Quick Start
Generate a MIDI file directly from the command line:
npx @libraz/midi-sketch-bach --form fugue --key d_minor -o output.midGenerate a Toccata and Fugue in D minor:
npx @libraz/midi-sketch-bach --form toccata_and_fugue --key d_minor -o toccata.midDefaults differ between the JS API and the CLI
When bpm is omitted, the JS API defaults to 100 while the CLI defaults to 72. The CLI also falls back to --scale medium for fugue when --scale is omitted (the JS API default is always short). See the CLI reference for the full list of CLI defaults.
What You Can Generate
MIDI Sketch Bach supports 10 musical forms across three systems:
Organ System -- Fugue, Prelude and Fugue, Trio Sonata, Chorale Prelude, Toccata and Fugue, Passacaglia, Fantasia and Fugue
Solo Instrument System -- Cello Prelude, Chaconne
Variation System -- Goldberg Variations
Each form follows authentic Baroque compositional rules for voice leading, counterpoint, and harmonic structure.
Next Steps
- Music Primer for Engineers -- Minimal music vocabulary used by the docs
- Features -- Overview of all capabilities
- Forms -- Choose the composition template you want
- Counterpoint Course -- Why the validator accepts or rejects notes
- Installation -- Detailed installation options
- JavaScript API or CLI Reference -- Use the package from code or the terminal
- Architecture -- How it works under the hood