Hi again,

Thinking about this some more, I realized that there is a bit of redundant
calculation going on: the pitches to be colored are extracted each time the
function \colorPitches is called. While the wasted time probably wouldn't
be noticeable with the two or three notes a handbell player would have in
circulation, it bugs me.  It might be noticeable if you were to do
something like color the range held in common between two instrumental
parts--don't know why you'd want to, but there it is.

The attached uses a new function \extractPitches which returns a list of
the pitches used in a music expression.  This might be useful for other
purposes.

[BTW, there's a correction in extract-pitches.]

HTH,
David


On Sun, May 25, 2014 at 10:47 AM, David Nalesnik
<[email protected]>wrote:

>
> On Sun, May 25, 2014 at 10:06 AM, David Nalesnik <[email protected]
> > wrote:
>
>
>>  (Something could be worked out for enharmonics, I suppose.)
>>
>
> OK, this handles enharmonics:
>
>
\version "2.18.2"

#(define (get-pitch elt)
   (ly:music-property elt 'pitch))

#(define (extract-pitches lst result) 
   (cond
    ((null? lst) result)
    ((ly:pitch? (get-pitch (car lst)))
     (set! result (append result (list (get-pitch (car lst)))))
     (extract-pitches (cdr lst) result))
    ((ly:music-property (car lst) 'elements)
     (append
      (extract-pitches (ly:music-property (car lst) 'elements) result)
      (extract-pitches (cdr lst) '())))
    (else (extract-pitches (cdr lst) result))))

extractPitches =
#(define-scheme-function
  (parser location lst)
  (ly:music?)
  (extract-pitches
   (extract-named-music lst '(EventChord NoteEvent))
   '()))

colorPitches =
#(define-music-function (parser location pitch-list col)
   (list? color?)
   #{
     \override NoteHead.color =
     #(lambda (grob)
        (let ((pitch (ly:event-property (ly:grob-property grob 'cause) 'pitch)))
          (if (any
               (lambda (x) (= (ly:pitch-semitones pitch) (ly:pitch-semitones x)))
               pitch-list)
              col)))
   #})



%%%%%%%%%%%%%%%%%%%%%%%% EXAMPLE %%%%%%%%%%%%%%%%%%%%%%%%%%


upper = \relative c' {
  c cis d dis es f fis fisis g aeses gis a ais bes b c
}

% \colorPitches takes a list of pitches.  These may be specified using the Scheme
% function ly:make-pitch or they can be extracted from a music expression, using
% the function \extractPitches

playerI = #(list (ly:make-pitch 0 1 0) (ly:make-pitch 0 4 1/2))

playerII = \extractPitches { \relative c' { cis es g } }


\score {
  \new Staff \upper
  \layout { }
}

\score {
  \new Staff {
    \colorPitches \playerI #blue
    \upper
  }
  \layout { }
}

\score {
  \new Staff {
    \colorPitches \playerII #green
    \upper
  }
  \layout { }
}

\score {
  \new Staff {
    \colorPitches \extractPitches { \relative c' { <fis ais> } } #yellow
    \upper
  }
  \layout { }
}

% demonstrates \extractPitches
%#(display #{ \extractPitches \upper #})
_______________________________________________
lilypond-user mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/lilypond-user

Reply via email to