Lowest and highest pitch as chord

Revision as of 07:58, 3 May 2026 by Manuela (talk | contribs)

This code returns the lowest and the highest pitch of a music expression as chord.

The duration of the chord can be chosen arbitrarily as input parameter.

You can easily change the chord to a fixed duration. You can even chose different durations for the pitches in the chord.

\version "2.26.0"


%% This snippets returns the lowest and the highest pitch
%% of a music expression als chord
%% the duration of the chord is passed as argument

myambitus =
#(define-music-function (mus dur)(ly:music? ly:duration?)
   (let*
    (
      (alle-pitches
       (let loop ((mus mus) (pitches '()))
         (let ((p  (ly:music-property mus 'pitch)))
           (if (ly:pitch? p)
               (cons p pitches)
               (let ((elt (ly:music-property mus 'element)))
                 (fold loop
                       (if (ly:music? elt)
                           (loop elt pitches)
                           pitches)
                       (ly:music-property mus 'elements)))))))
      (alle-sortiert (sort alle-pitches ly:pitch<?))
      (tief (car alle-sortiert))
      (hoch (car (reverse alle-sortiert)))
      )
    ;     (display tief )(display hoch)
    ;     (write-me "alle pitches----> " (list? alle-pitches))
    (make-music
     'EventChord 'elements
     (list
      (make-music
       'NoteEvent
       'duration
       dur
       'pitch
       tief)
      (make-music
       'NoteEvent
       'pitch
       hoch
       'duration
       dur)))
    ))

mymus = \relative { c' c f,, }
{ \clef bass \myambitus \mymus #(ly:make-duration 0) }
{ \clef bass \myambitus \transpose c e \mymus 2. }