Jump to content

Lowest and highest pitch as chord

From LilyPond wiki

Takes MUSIC and returns sequential music, simultaneous music or an event-chord with duration DURATION, relying on the type M provides. The optional PROC, supposed to be {first} or {last}, may be used to get the bottom or top note only.

\version "2.26.0"


#(define (make-note-event pitch duration)
  "Takes PITCH and DURATION and returns a NoteEvent."
  (make-music
   'NoteEvent
   'duration duration
   'pitch pitch))
 
#(define (bottom-top-notes music duration)
  "Takes MUSIC and returns lowest and highest note each with duration DURATION
as a plain list."
  (let* ((all-pitches (music-pitches music))
         (sorted-pitches (sort all-pitches ly:pitch<?)))
    (map
      (lambda (pitch) (make-note-event pitch duration))
      (list (car sorted-pitches) (last sorted-pitches)))))

extrema =
#(define-music-function (proc m music duration)
  ((procedure? identity) ly:music? ly:music? ly:duration?)
"Takes MUSIC and returns sequential music, simultaneous music or an event-chord
with duration DURATION, relying on the type M provides.
The optional PROC, supposed to be @code{first} or @code{last}, may be used to
get the bottom or top note only."
  (ly:music-set-property! m 'elements 
    (ensure-list (proc (bottom-top-notes music duration))))
  m)
    
%%%%%%%%%%%% 
%% EXAMPLES
%%%%%%%%%%%%

testMus = \relative { d' e fis g a b cis }
  
{ \extrema {} \testMus 4 }
%% Below, preceed with \new Staff to avoid implicit creation of two staves
{ \extrema <<>> \testMus 4 }
{ \extrema <> \testMus 4 }

%% Get only bottom or top note
{ \extrema #first {} \testMus 4 }
{ \extrema #last {} \testMus 4 }