Jump to content

Lowest and highest pitch as chord: Difference between revisions

From LilyPond wiki
Manuela (talk | contribs)
m Code replaced with Harm's version from the German Lilypond forum
Manuela (talk | contribs)
m Link to Forum added
 
Line 2: Line 2:
with duration <code>DURATION</code>, relying on the type M provides.
with duration <code>DURATION</code>, relying on the type M provides.
The optional <code>PROC</code>, supposed to be <code>{first}</code> or <code>{last}</code>, may be used to get the bottom or top note only.
The optional <code>PROC</code>, supposed to be <code>{first}</code> or <code>{last}</code>, may be used to get the bottom or top note only.
 
*[https://lilypondforum.de/index.php/topic,1601.0.html Ambitus feststellen?]
<lilypond version="2.26.0" >
<lilypond version="2.26.0" >
#(define (make-note-event pitch duration)
#(define (make-note-event pitch duration)
   "Takes PITCH and DURATION and returns a NoteEvent."
   "Takes PITCH and DURATION and returns a NoteEvent."

Latest revision as of 20:15, 4 May 2026

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 }