Lowest and highest pitch as chord: Difference between revisions

Manuela (talk | contribs)
mNo edit summary
Manuela (talk | contribs)
m Code replaced with Harm's version from the German Lilypond forum
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
This code returns the lowest and the highest pitch of a music expression as chord.
Takes <code>MUSIC</code> and returns sequential music, simultaneous music or an event-chord
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 duration of the chord can be chosen arbitrarily as input parameter.
<lilypond version="2.26.0" >


You can easily change the chord to a fixed duration. You can even chose different durations for the pitches in the chord.
#(define (make-note-event pitch duration)
 
  "Takes PITCH and DURATION and returns a NoteEvent."
<lilypond version="2.26.0" >
  (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)))))


%% This snippets returns the lowest and the highest pitch
extrema =
%% of a music expression als chord
#(define-music-function (proc m music duration)
%% the duration of the chord is passed as argument
  ((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
%%%%%%%%%%%%


myambitus =
testMus = \relative { d' e fis g a b cis }
#(define-music-function (mus dur)(ly:music? ly:duration?)
 
  (let*
{ \extrema {} \testMus 4 }
    (
%% Below, preceed with \new Staff to avoid implicit creation of two staves
      (alle-pitches
{ \extrema <<>> \testMus 4 }
      (let loop ((mus mus) (pitches '()))
{ \extrema <> \testMus 4 }
        (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,, }
%% Get only bottom or top note
{ \clef bass \myambitus \mymus #(ly:make-duration 0) }
{ \extrema #first {} \testMus 4 }
{ \clef bass \myambitus \transpose c e \mymus 2. }
{ \extrema #last {} \testMus 4 }</lilypond>
</lilypond>
[[Category:Pitches]]
[[Category:Pitches]]
[[Category:Scheme]]
[[Category:Scheme]]