Here is another revision (hopefully the last?!).  The previous version would 
include the key name over a key cancellation[1], but it would often be an 
incorrect/unrelated key name (!).  

This version does not include the name of the key above a key-cancellation, 
unless the tonic note is the same in both keys, like a switch from G major to G 
minor.  (This is a glitch I haven't been able to work around.  It always gives 
the correct key name now at least.  I tried checking for natural signs as the 
type of accidental used in key cancellations, but this does not work.)

Cheers,
-Paul 

[1] http://www.lilypond.org/doc/v2.16/Documentation/internals/keycancellation



\version "2.16.0"

% tonic-semi: semitone of tonic note, 0-11 
% psn: vertical staff position of the last accidental sign in key signature
% accsign: the accidental sign type, 1/2=sharp, -1/2=flat
% Knowing these 3 things is enough to identify the key and whether it is 
major/minor

#(define Custom_key_sig_engraver
   (make-engraver
    (acknowledgers
     ((key-signature-interface engraver grob source-engraver)
      (let* (
                (context          (ly:translator-context engraver))
                (tonic-pitch  (ly:context-property context 'tonic))
                (tonic-semi  (modulo (ly:pitch-semitones tonic-pitch) 12)) 
                (acclist          (ly:grob-property grob 'alteration-alist))
                (accsign   (if (null? acclist) 0 (cdr (list-ref acclist 0)))) 
                (psn              (if (null? acclist) 0 (car (list-ref acclist 
0)))) 
                (key-name   #{ \markup {} #})
          )
          (cond 
          ((= accsign 0) (cond
                        ((= tonic-semi 0) (set! key-name #{ \markup \raise #3.4 
{C Major} #}))
                        ((= tonic-semi 9) (set! key-name #{ \markup \raise #3.4 
{A Minor} #}))
                        (else (set! key-name #{\markup{}#}))
                ))
          ((= accsign 1/2)
                (cond 
                ((= psn 3) (cond 
                        ((= tonic-semi 7) (set! key-name #{ \markup {G Major} 
#}))
                        ((= tonic-semi 4) (set! key-name #{ \markup {E Minor} 
#}))
                        (else (set! key-name #{\markup{}#}))
                ))
                ((= psn 0) (cond
                        ((= tonic-semi 2)   (set! key-name #{ \markup {D Major} 
#}))
                        ((= tonic-semi 11) (set! key-name #{ \markup {B Minor} 
#}))
                        (else (set! key-name #{\markup{}#}))
                ))
                ((= psn 4) (cond 
                        ((= tonic-semi 9) (set! key-name #{ \markup {A Major} 
#}))
                        ((= tonic-semi 6) (set! key-name #{ \markup {F\raise 
#.6 {\sharp} Minor} #}))
                        (else (set! key-name #{\markup{}#}))
                ))
                ((= psn 1) (cond 
                        ((= tonic-semi 4) (set! key-name #{ \markup {E Major} 
#}))
                        ((= tonic-semi 1) (set! key-name #{ \markup {C\raise 
#.6 {\sharp} Minor} #}))
                        (else (set! key-name #{\markup{}#}))
                ))
                ((= psn 5) (cond 
                        ((= tonic-semi 11) (set! key-name #{ \markup {B Major} 
#}))
                        ((= tonic-semi 8)   (set! key-name #{ \markup {G\raise 
#.6 {\sharp} Minor} #}))
                        (else (set! key-name #{\markup{}#}))
                ))
                ((= psn 2) (cond
                        ((= tonic-semi 6) (set! key-name #{ \markup {F\raise 
#.6 {\sharp} Major} #}))
                        ((= tonic-semi 3) (set! key-name #{ \markup {D\raise 
#.6 {\sharp} Minor} #}))
                        (else (set! key-name #{\markup{}#}))
                ))
                ((= psn 6) (cond
                        ((= tonic-semi 1)   (set! key-name #{ \markup {C\raise 
#.6 {\sharp} Major} #}))
                        ((= tonic-semi 10) (set! key-name #{ \markup {A\raise 
#.6 {\sharp} Minor} #}))
                        (else (set! key-name #{\markup{}#}))
                ))
                )
          )
          ((= accsign -1/2)
                (cond
                ((= psn 6) (cond
                        ((= tonic-semi 5) (set! key-name #{ \markup {F Major} 
#}))
                        ((= tonic-semi 2) (set! key-name #{ \markup {D Minor} 
#}))
                        (else (set! key-name #{\markup{}#}))
                ))
                ((= psn 2) (cond
                        ((= tonic-semi 10) (set! key-name #{ \markup {B\raise 
#.4 {\flat} Major} #}))
                        ((= tonic-semi 7)   (set! key-name #{ \markup {G Minor} 
#}))
                        (else (set! key-name #{\markup{}#}))
                ))
                ((= psn 5) (cond
                        ((= tonic-semi 3) (set! key-name #{ \markup {E\raise 
#.4 {\flat} Major} #}))
                        ((= tonic-semi 0) (set! key-name #{ \markup {C Minor} 
#}))
                        (else (set! key-name #{\markup{}#}))
                ))
                ((= psn 1) (cond
                        ((= tonic-semi 8) (set! key-name #{ \markup {A\raise 
#.4 {\flat} Major} #}))
                        ((= tonic-semi 5) (set! key-name #{ \markup {F Minor} 
#}))
                        (else (set! key-name #{\markup{}#}))
                ))
                ((= psn 4) (cond
                        ((= tonic-semi 1)   (set! key-name #{ \markup {D\raise 
#.4 {\flat} Major} #}))
                        ((= tonic-semi 10) (set! key-name #{ \markup {B\raise 
#.4 {\flat} Minor} #}))
                        (else (set! key-name #{\markup{}#}))
                ))
                ((= psn 0) (cond
                        ((= tonic-semi 6) (set! key-name #{ \markup {G\raise 
#.4 {\flat} Major} #}))
                        ((= tonic-semi 3) (set! key-name #{ \markup {E\raise 
#.4 {\flat} Minor} #}))
                        (else (set! key-name #{\markup{}#}))
                ))
                ((= psn 3) (cond
                        ((= tonic-semi 11) (set! key-name #{ \markup {C\raise 
#.4 {\flat} Major} #}))
                        ((= tonic-semi 8)   (set! key-name #{ \markup {A\raise 
#.4 {\flat} Minor} #}))
                        (else (set! key-name #{\markup{}#}))
                ))
                )       
          )
          )     
          (ly:grob-set-property! grob 'stencil 
                (ly:stencil-combine-at-edge (ly:key-signature-interface::print 
grob) 1 1 
                        (ly:stencil-scale (grob-interpret-markup grob key-name) 
.7 .7) 
                        .5 )) 
)))))

% Resize text by changing the .7 .7 above in "(grob-interpret-markup grob 
key-name) .7 .7)" 
% Adjust padding by changing the .5 on the following line


\layout {
  \context {
    \Staff
    \consists \Custom_key_sig_engraver
  }
}

\new Staff {
  \key c \major
  c'1
  \key a \minor
  a'1
  \key g \major 
  g'1 
  \key e \minor
  e'1
  \key d \major 
  d'1
  \key b \minor
  b'1 
  \key a \major 
  a'1 
  \key fis \minor
  fis'1
  \key e \major 
  e'1
  \key cis \minor
  cis'1
  \key b \major 
  b'1 
  \key gis \minor
  gis'1
  \key fis \major 
  fis'1 
  \key dis \minor
  dis'1
  \key cis \major 
  cis'1
  \key ais \minor
  ais'1
  \key f \major 
  f'1
  \key d \minor
  d'1
  \key bes \major 
  bes'1
  \key g \minor
  g'1
  \key ees \major 
  ees'1
  \key c \minor
  c'1
  \key aes \major 
  aes'1
  \key f \minor
  f'1
  \key des \major 
  des'1
  \key bes \minor
  bes'1
  \key ges \major 
  ges'1
  \key ees \minor
  ees'1
  \key ces \major 
  ces'1
  \key aes \minor
  aes'1
}
_______________________________________________
lilypond-user mailing list
lilypond-user@gnu.org
https://lists.gnu.org/mailman/listinfo/lilypond-user

Reply via email to