(define the-dream (+ 'scheme 'lilypond))

Eric Bailey

Written on 7 December, 2014
Updated on 17 December, 2023
Tags: , ,

nil

Lately I've gotten increasingly obsessed with Lisp family of programming languages and am determined to write as little non-Lisp code as possible.

As such, I've started experimenting with writing more Scheme in Lilypond. Note to self: Dive deeper into these pertinent blog posts ASAP. This is an entirely impractical example, but it gives a glimpse of what's possible. I can't wait to start map-ping lists of musical expressions and representing music as trees.

Scheme

Create a new hash table with minimum vector size 5.

(define riffs
  (make-hash-table 5))

Associate a LilyPond snippet with the key 'I in the riffs hash table.

(hashq-set! riffs 'I
  #{
    \mark \markup { I }
    f4 a c d | f d c a |
    f4 a c d | ees d c b |
  #})

Do the same for the 'IV, 'I', 'V, and 'turnaround snippets.

(hashq-set! riffs 'IV
  #{
    \mark \markup { IV }
    bes4 d f g | bes g f d |
  #})

(hashq-set! riffs 'I'
  #{
    \mark \markup { "I'" }
    f,4 a c d | f d c a |
  #})

(hashq-set! riffs 'V
  #{
    \mark \markup { V }
    g a bes b | c bes a g |
  #})

(hashq-set! riffs 'turnaround
  #{
    f4 f aes aes | g g ges c |
  #})

LilyPond

\version "2.24.1"
\paper { oddFooterMarkup = ##f }

Load the Scheme code from above.

#(load "the_dream.scm")

Lookup the riffs from the hash table and add them to the score.

\score {
  \relative c, {
    \clef bass
    #(hashq-ref riffs 'I)
    \break
    #(hashq-ref riffs 'IV)
    #(hashq-ref riffs 'I')
    \break
    #(hashq-ref riffs 'V)
    #(hashq-ref riffs 'turnaround)
    \bar "|."
  }
  \layout { indent = 0 }
}

Generate the PNG

N.B. The order of the flags matters.

lilypond --include $PWD -dcrop -dresolution=200 --png the_dream.ly