Jump to content

Splitting chords: Difference between revisions

From LilyPond wiki
Fsarud (talk | contribs)
Some scheme functions to split chords in two (monophonic) voices.
 
mNo edit summary
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
This snippet is a 2009 contribution by Gilles Thibault in [https://lists.gnu.org/archive/html/lilypond-user/2009-01/msg00685.html lilypond-user]. It works well in 2.24.4 and even in 2.25.35. I find it very useful.
This snippet is a 2009 contribution by Gilles Thibault in [https://lists.gnu.org/archive/html/lilypond-user/2009-01/msg00685.html lilypond-user]. It splits a chord sequence into two separated voices: the first one built on the first note (usually the lowest) of every chord and the other on the remaining notes. It works well in 2.24.4 and even in 2.25.35. I find it very useful. It also preserves articulations and slurs. The only drawback: it doesn't work if you use «q» to repeat last chord.  


<lilypond version="2.24">
<lilypond version="2.24">
#(define (has-duration? music)
(ly:duration? (ly:music-property music 'duration)))


#(define (not-has-duration? music)
#(define (has-duration? music)
(not (has-duration? music)))
(ly:duration? (ly:music-property music 'duration)))
#(define (not-has-duration? music)
(not (has-duration? music)))
keepsOnlyFirstNote = #(define-music-function (parser location music) (ly:music?)
(music-map
  (lambda (evt)
  (if (eq? 'EventChord (ly:music-property evt 'name))
      (let ((elts (ly:music-property evt 'elements)))
      (if (has-duration? (car elts))
            (ly:music-set-property! evt 'elements (cons
                (car elts)
                (filter not-has-duration? (cdr elts)))))))
  evt)
music))
deleteFirstNote = #(define-music-function (parser location music) (ly:music?)
(music-map
  (lambda (evt)
  (if (eq? 'EventChord (ly:music-property evt 'name))
      (let ((elts (ly:music-property evt 'elements)))
          (if (has-duration? (car elts))
                (ly:music-set-property! evt 'elements  (cdr elts)))))
  evt)
music))
music =\relative c' {
<c e>4-> <d f>( <b g'>) <c e>-. g2 c2
}
\markup { Music with chords }
\new Staff \music
\markup { Music splitted in 2 staffs }
<<
  \new Staff \deleteFirstNote \music
  \new Staff \keepsOnlyFirstNote \music
>>
</lilypond>


keepsOnlyFirstNote = #(define-music-function (parser location music) (ly:music?)
(music-map
(lambda (evt)
  (if (eq? 'EventChord (ly:music-property evt 'name))
    (let ((elts (ly:music-property evt 'elements)))
      (if (has-duration? (car elts))
          (ly:music-set-property! evt 'elements (cons
                (car elts)
                (filter not-has-duration? (cdr elts)))))))
evt)
music))


deleteFirstNote = #(define-music-function (parser location music) (ly:music?)
[[category:Chords]]
(music-map
[[category:Scheme]]
(lambda (evt)
[[category:Really cool]]
  (if (eq? 'EventChord (ly:music-property evt 'name))
    (let ((elts (ly:music-property evt 'elements)))
          (if (has-duration? (car elts))
              (ly:music-set-property! evt 'elements  (cdr elts)))))
evt)
music))
 
 
music =\relative c' {
<c e>4-> <d f>( <b g'>) <c e>-. g2 c2
}
 
\markup { Music with chords }
\new Staff \music
\markup { Music splitted in 2 staffs }
<<
  \new Staff \deleteFirstNote \music
  \new Staff \keepsOnlyFirstNote \music
>>
</lilypond>

Latest revision as of 14:38, 23 March 2026

This snippet is a 2009 contribution by Gilles Thibault in lilypond-user. It splits a chord sequence into two separated voices: the first one built on the first note (usually the lowest) of every chord and the other on the remaining notes. It works well in 2.24.4 and even in 2.25.35. I find it very useful. It also preserves articulations and slurs. The only drawback: it doesn't work if you use «q» to repeat last chord.

\version "2.24"


 #(define (has-duration? music)
 (ly:duration? (ly:music-property music 'duration)))
 
 #(define (not-has-duration? music)
 (not (has-duration? music)))
 
 keepsOnlyFirstNote = #(define-music-function (parser location music) (ly:music?)
 (music-map
  (lambda (evt)
   (if (eq? 'EventChord (ly:music-property evt 'name))
      (let ((elts (ly:music-property evt 'elements)))
       (if (has-duration? (car elts))
            (ly:music-set-property! evt 'elements (cons
                 (car elts)
                 (filter not-has-duration? (cdr elts)))))))
  evt)
 music))
 
 deleteFirstNote = #(define-music-function (parser location music) (ly:music?)
 (music-map
  (lambda (evt)
   (if (eq? 'EventChord (ly:music-property evt 'name))
      (let ((elts (ly:music-property evt 'elements)))
           (if (has-duration? (car elts))
                (ly:music-set-property! evt 'elements  (cdr elts)))))
  evt)
 music))
 
 
 music =\relative c' {
 <c e>4-> <d f>( <b g'>) <c e>-. g2 c2
 }
 
 \markup { Music with chords }
 \new Staff \music
 \markup { Music splitted in 2 staffs }
 <<
   \new Staff \deleteFirstNote \music
   \new Staff \keepsOnlyFirstNote \music
 >>