Skip to content

Music composition compiler

DSCH

A compiler for an expression-oriented music DSL: concatenative on the surface, functional underneath, lowered to Standard MIDI.

100 bpm

-- bb 1-3
@3 d1 C# d2 B# @3 E d1 D#

-- bb 4-11
{
  (d3.5/1 ~ @4 d2 C# B# E d1 D# d2 C# d3/4 F#)
  (@3 d1 G# d2 Fx B d1 A# d2 G# d3/4 A d8 G# F#)
  (@3 d4 C# D# d3/4 E d8 D# C# d4 D# G# C#)
}

DSCH (the name is Shostakovich's musical signature) reads .dsch files, compiles them into a tree of scoped musical contexts, schedules those contexts as time-stamped MIDI events and writes a Standard MIDI File.

The language

  • Concatenative at the surface: putting expressions next to each other threads a musical context from left to right, as in Forth or Joy. @3 sets the register, d4 the duration, and they hold until something changes them.
  • Functional inside: AST nodes compose through a Monad<Exp>::bind, and the grammar already has imports and function declarations on the way to the surface.
  • ( … ) is a sequence, { … } is a stack of voices played together, a:b:c divides time in proportion, and dynamics and durations can ramp across several points.

Pipeline

A pest PEG grammar → AST → a fold over the AST into an arena of scoped contexts → a merge pass that sequences and combines them → MIDI ticks.

There's also dsch-lsp, a language server built on tower-lsp, so the scores get editor support.

The lab has Bach's Fugue in C♯ minor, BWV 849, from Book I of the Well-Tempered Clavier, transcribed into DSCH: it plays the MIDI file the compiler wrote, and the score lights up as each note sounds.

kolang is new tooling for building the family of languages DSCH belongs to.