lilypond-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: share note stem between voice


From: Carl D. Sorensen
Subject: Re: share note stem between voice
Date: Wed, 5 Nov 2008 06:44:00 -0700

I have copied this to devel, as well as to user, because I'd like to get
permission to add the function setHeadColor to ly/music-function-init.ly.



On 11/3/08 5:51 AM, "Daniel Cheng" <address@hidden> wrote:
> It have some special feature, and I want to replicate it.
>
> See http://img235.imageshack.us/my.php?image=stemnr2.png
>
> Red note-heads are voice 1, Blue note-head are voice 2.
>
> As you can see:
>    - both voice share the same stem
>    - the stem directory always towards voice 2
>    - when two voice are on the same note, two stem are drew
>
> I know I can use the \stemUp / \stemDown to do this manually,
> but I want to know if there are any easier ways.

At first I thought it would be trivial to do this, but all of the
straightforward ways I tried didn't work the way I wanted them to.

\override applies to all noteheads in the current voice at the current
moment, so I couldn't get two different color notes on the same stem in the
same voice.

When I used two music expressions in parallel with << \\ >>, the stems on
the three eighth notes in the second measure were in opposite directions for
the two voices.

When I used two music expressions is parallel with << \\ >> and \oneVoice, I
got two sets of beams/flags.

The answer I finally arrived at is to put both sets of the notes in the same
voice so they share stems properly, but in separate music expressions so I
can change their colors using a music function.

So I finally wrote a music function to color all of the note heads in a
music expression using tweaks.  I think it gives just what you want.  Here's
an example:


%%%%% Beginning of LilyPond code

\version "2.11.64"

%  Here we define music functions to set the notehead color
%  for a note.

setHeadColor =
#(define-music-function (parser location note-head-color music)
                        (pair? ly:music?)
   (_i "Set all note-heads in @code{music} to @code{not-head-color}.")

   ; set-notehead-color is a helper function
   ; use let and lambda definition for set-notehead-color because
   ; define-music-function won't let user do define at top level
   ; and I have no variables I want to define in the let block

    (let ((set-notehead-color (lambda (head-color music)
       (if (music-is-of-type? music 'note-event)
           (begin
             (set! (ly:music-property music 'tweaks)
                   (acons 'color head-color
                          (ly:music-property music 'tweaks)))
             music)
           music))))

   ;main body of function
    (music-map (lambda (y) (set-notehead-color note-head-color y))
               music)))

% End of music function definition


voiceOneNotes = \relative c' {
  \time 3/4
  f4 e8 e4 g8 |
  d8 d d g4 a8 |
  c2 r4
}

voiceTwoNotes = \relative c' {
  \time 3/4
  d4 b8 b4 e8 |
  g8 g g d4 d8 |
  c'2 r4
}

\score {
  \new Voice {
  <<
    {
      \setHeadColor #blue \voiceOneNotes
    }
    {
      \setHeadColor #red \voiceTwoNotes
    }
  >>
  }
}

%%%%% End of LilyPond code

HTH,

Carl





reply via email to

[Prev in Thread] Current Thread [Next in Thread]