Hi Viktor,
Please forgive my ignorance, I have two follow-up questions:
1. How do I change the pitch of the 'root' note? I tried playing with
the three numbers from "(+ tonic-position 3) 7) 3)))". But I can't
understand their logic.
I'm not quite sure what you mean: The pitch of the 'root' note is taken
directly from the \key command.
For example, the following example generates all reasonable roots for
key signatures with two sharps:
{
\key d \major
a'1
\key e \dorian
a'1
\key fis \phrygian
a'1
\key g \lydian
a'1
\key a \mixolydian
a'1
\key b \aeolian
a'1
\key cis \locrian
a'1
}
Does this already solve your question?
But I can also explain the logic behind the three numbers 3 7 3 in the
expression you quoted:
* "tonic" is the tonic as a LilyPond pitch.
* "tonic-position" is defined as (+ (ly:pitch-steps tonic)
(ly:grob-property grob 'c0-position), i.e.
... the vertical position of "middle c" for the current clef
(measured in half-staff distances, i.e. note steps, from the
centermost staff line)
plus
... the number of steps our tonic lies above middle c.
Now we have the problem that this gives us the position of _some_
tonic note, but we do not know yet in which octave it will lie; so
it may very well be sitting far above or far below the staff. Therefore:
* "adjusted-tonic-position" is defined as (- (modulo (+ tonic-position
3) 7) 3))), i.e. ((tonic-position + 3) mod 7) - 3. The idea is: Take
the tonic-position modulo 7 since pitch names repeat after 7 steps.
If we would simply take "tonic-position mod 7", this would amount to
guaranteeing the tonic-position to lie between 0 and 6: But this
means our note will lie on or above the centermost staff line (the
lower staff lines will not be used).
What we want instead is a "shifted" modulo operation that returns
values not between 0 and 6 but between -3 and 3; in other words:
Values of "tonic-position" between -3 and 3 should not get changed.
So we first add 3 (yielding a value between 0 and 6), then doing the
modulo and finally transforming back by subtracting 3 again.
To sum up: The 7 should not be changed (that's the amount of steps in
our note name system), and the two 3's should be equal (and it's no
coincidence that 3 = (7-1)/2).
2. How can I remove the parenthesis? I tried but continuously broke
the code:-)
The notehead stencil is in the "notehead" variable, and the
paranthesized stencil is stored in "notehead-parens". The simplest way
to get rid of the parentheses would be to replace "notehead-parens" in
the final construction of the stencil by "notehead". But then we can
also get rid of "notehead-parens" completely, hence we get:
notehead_key_signature = #
(lambda (grob)
(let*
((key-sig (ly:key-signature-interface::print grob))
(notehead
(grob-interpret-markup grob
(markup #:tiny #:musicglyph "noteheads.s2")))
(tonic (assq-ref (ly:grob-property grob 'details) 'tonic))
(tonic-position (+ (ly:pitch-steps tonic)
(ly:grob-property grob 'c0-position)))
(adjusted-tonic-position
(- (modulo (+ tonic-position 3) 7) 3)))
(ly:stencil-combine-at-edge
key-sig X RIGHT
(ly:stencil-translate-axis notehead
(/ adjusted-tonic-position 2) Y)
0.5)))
HTH
Lukas