Lowest and highest pitch as chord: Difference between revisions
Appearance
m Manuela moved page User:Manuela/lowest and highest pitch as chord to Lowest and highest pitch as chord |
m Code replaced with Harm's version from the German Lilypond forum |
||
| Line 1: | Line 1: | ||
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. | |||
<lilypond 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 }</lilypond> | |||
</lilypond> | |||
[[Category:Pitches]] | [[Category:Pitches]] | ||
[[Category:Scheme]] | [[Category:Scheme]] | ||
Latest revision as of 11:07, 3 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 }