Composing Music π΅ with an F# Internal DSL

Troels Lund

Troels Lund
I've been working on an internal DSL in F# that makes it incredibly easy to compose simple music. The goal is to provide a declarative, expressive way to create melodies while handling the underlying sound generation.
Here's an example of composing and playing the Super Mario Bros. theme song using the DSL:
1open DSL.Generator 2open DSL.MusicBuilder 3open DSL.Player 4 5let music = MusicBuilder() 6 7let song = 8 music { 9 note "E5" 0.5 10 note "E5" 0.5 11 rest 0.25 12 note "E5" 0.5 13 rest 0.25 14 note "C5" 0.5 15 note "E5" 0.5 16 rest 0.25 17 note "G5" 0.5 18 rest 0.25 19 note "G4" 0.5 20 // ... more notes ... 21 } 22 23 24let songName = "mario_bros_theme.wav" 25// Generate a WAV file and play the song 26createWav songName song 27// play the generated song 28playSound songName
F#
With just a few lines, you can define melodies using note names and durations while the DSL takes care of the rest. π
I'm considering extending it with chords, loops, and instrument variations.
The source code can be found here.