schemem4lilypondtypesettingmusic-notation

What is the best way to automatically transpose a LilyPond source file into multiple keys?


problem

I'm using LilyPond to typeset sheet music for a church choir to perform. Depending on who is available on any given week, songs will be played in various keys. We have an amazing pianist who can play anything we throw at her and the guitarists will typically pencil in alternate chords, but I want to make things easier by having beautifully typeset sheet music available in any key we want.

So say we're going to sing our ABCs. First I'll take whatever source transcriptions available and enter it into a LilyPond script:

melody = \relative c' {
    c c g g
    a a g2
    f f e e
    d d c2 }

I want the ability to transpose this automatically, so if I want the whole thing in 'G' I wrap the song in a \transpose call like so:

melody = \transpose c g \relative c' {
    c c g g
    a a g2
    f f e e
    d d c2 }

What I really want is to substitute something for the 'g' and generate the output for melody multiple times. Simple LilyPond variables don't seem to work here, and so far I've been unsuccessful in defining a scheme function to do this.

What I've resorted to for the moment is taking the above file, call it twinkle.ly and turning it into an M4 script called twinkle.ly.m4, the contents of which look like this:

melody = \transpose c _key \relative c' {
c c g g
a a g2
f f e e
d d c2 }

I then compile the while thing by executing the following line:

> m4 -D _key=g twinkle.ly.m4 > twinkle_g.ly && lilypond twinkle_g.ly

I've written a Makefile to do this for me, defining rules for every song I have and every key I'm interested in.

question There's got to be a better way of going about this. Given that Lilypond supports embedded scheme, I would prefer to not use a macro preprocessed on it. Has anybody else come up with a solution to this same problem?


Solution

  • You can refer to the melody variable and have it transposed in the desired keys:

    melody = \relative c' { c8 e c e g4 g }
    \score {
      \transpose c d \melody
    }
    \score {
      \transpose c e \melody
    }
    % etc.
    

    If you want to output the transpositions as separate files, embed them into \book blocks.