Monday, 30 August 2010

Fret diagrams with LilyPond

If you are playing (or learning to play) guitar, you may have wondered if LilyPond can help to produce the cute fret diagrams alongside (or instead) the classic notation. Indeed it can! For illustration purposes, I chose And I Love Her by The Beatles:

```% ****************************************************************
% And I Love Her (Lennon/McCartney)
% ****************************************************************
\version "2.12.3"
verseI = \lyricmode {
\set stanza = #"1."
I give her all my love,
that’s all I do. __ _
And if you saw my love
you’d love her too.
And I love her.
}
verseII = \lyricmode {
\set stanza = #"2."
She gives me ev -- ’ry -- thing
and ten -- der -- ly. __ _
The kiss my lov -- er brings
she brings to me.
And I love her.
}
verseIII = \lyricmode {
\set stanza = #"3."
Bright are the stars that shine,
dark is the sky. __ _
I know this love of mine
will ne -- ver die.
And I love her.
}
theChords = \chordmode {
fis1:m cis1:m fis1:m cis1:m fis1:m cis1:m a1 b1:7 e1:6
}
staffMelody = {
\key cis \minor
r4 fis'4 gis'8 a'4 dis''8(
dis''8) cis''4 e''8( e''2)
r4 fis'4 gis'8 a'4 dis''8\((
dis''8) cis''8( cis''2.)\)
r4 fis'4 gis'8 a'4 dis''8(
dis''8) cis''4 e''8( e''2)
r4 e''4 cis''8 a'4 gis'8(
fis'2) r8 cis'8 b8 cis'8(
cis'8) gis'8( gis'2.)
}
\score {
<<
\context ChordNames { \theChords }
\context FretBoards { \theChords }
\new Staff {
\context Voice = "voiceMelody" { \staffMelody }
}
\new Lyrics = "lyricsI" {
\lyricsto "voiceMelody" \verseI
}
\new Lyrics = "lyricsII" {
\lyricsto "voiceMelody" \verseII
}
\new Lyrics = "lyricsIII" {
\lyricsto "voiceMelody" \verseIII
}
>>
}```

The result:

 (1)

One little problem here: I don’t like these diagrams. Fortunately, LilyPond has an option to include the (more pleasing) predefined fretboard diagrams. To do this, one has to add just one line to the script:

\include "predefined-guitar-fretboards.ly"

Now the output looks much nicer:

 (2)

The closer inspection shows that the last chord still does not look right. The reason is that the table of predefined diagrams does not include sixth chords. Therefore, one has to add this fret diagram manually to the script, e.g. like this:

```\storePredefinedDiagram \chordmode {e:6}
#guitar-tuning
#"o;2-2;2-3;1-1;2-4;o;"```

Here’s the final version:

 (3)