Transposition with Enharmonic Changes (Part 2)

In Part 1 of this tutorial  I showed you how to tame a transposition that wreaks havoc by running into absurd keys. This already was a nice example how “cleanly” you can realize such things with LilyPond – but we can do even better.

Suppose you need that song in yet another key, say transpose it from a flat major down a full step because the singer is somewhat tired today. That needs a different handling of the middle section, namely no enharmonic change because the e major will nicely be transposed to d major. In this case you can simply remove the enharmonic change of the middle section and update the transposition in the score block, which results in this version of the second half of the example:

music = {
\beginning
\middlesection
\end
}

\score {
\new Staff \transpose as ges \music
\layout {}
}

That wasn’t hard, was it? But I’m still not happy with having to apply changes in the middle of the music expressions. You would actually have to discard your previous version of the song or make a copy of the whole file, both of which aren’t acceptable hacks.

We already made use of variables to organize the structure of our file, and now we’ll take that a step further by refactoring our file a little bit. The first step will be to rewrite our current transposition (from a flat to g) in a more flexible and generic way.

We have written the music in the three variables beginning, middlesection and end.
LilyPond allows us to redefine variables, so

middlesection = \transpose e fes \middlesection

will use the enharmonically changed/transposed version of the variable and reassigns it to itself (or a variable with the same name). middlesection now refers to the modified version of the original variable.
Now you can simply write

music = {
\beginning
\middlesection
\end
}

and have a variable holding the complete music with the middle section enharmonically changed.
If you now transpose the music variable, again using the redefinition technique with

music = \transpose as g \music

you can use music in your score block in a generic way, without the score itself having a notion of key, transpositions or enharmonic change.

The complete file would now look like this:

beginning = \relative c'' {
\key as \major
% music 1
}
middlesection = \relative gis'{
\key e \major
% music 2
}
end = \relative c'' {
\key as \major
% music 3
}

middlesection = \transpose e fes \middlesection

music = {
\beginning
\middlesection
\end
}

music = \transpose as g \music

\score {
\new Staff \music
\layout {}
}

The music variable and the score block are now generic expressions, any new tranpositions can exclusively be handled in the two top-level variable assignments. In our initial problem (transposing to g flat major) we would remove the enharmonic change completely (by commenting it out) and update the overall transposition.

This actually isn’t the end of the line. The example as it is now actually doesn’t seem to be much more useful than the one from part 1: you still have to make updates in two places in the file if you need to produce another transposition. But factoring out the central parts of the transposition cleared the way for a truly generic version that we’ll see in part 3.

7 thoughts on “Transposition with Enharmonic Changes (Part 2)”

1. Michael Dunlap

Shouldn’t \transpose as g be \transpose as ges in the first example of this page (transposing down a whole step)?

2. Michael Dunlap

Urs, you changed my comment to what would seem more logical, but I get a funny result with transposing as to gs. The output puts each section on a new staff. Don’t know why. “as to fis”, which is equivalent enharmonically, does not. Fsharp major is less accidentals anyway.

3. Urs Liska

Hi Michael,
investigating this somewhat further I realized that I did not supply any complete, i.e. compilable example. So I don’t actually know what you tried to compile.
Therefore I wrote such an example for Download. If you play around with the commenting out you will hopefully see that/how it works.