Jump to content

Splitting chords: Difference between revisions

From LilyPond wiki
Fsarud (talk | contribs)
Add that it doesn't work with q for repeating last chord
No edit summary
 
(8 intermediate revisions by 4 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. (Beware: it doesn't work if you use «q» to repeat last chord).  
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 provides two functions to split a chord sequence into two separate voices, also preserving articulations and slurs: <code>\keepOnlyFirstNote</code> extracts the first note (as given in the input; this is usually the lowest one) of every chord, and <code>\deleteFirstNote</code> collects the remaining notes.


<span></span>
Note that these functions don't support <code>q</code> to repeat the last chord.
\version "2.24"
 
<lilypond version="2.24">
#(define (has-duration? music)
#(define (has-duration? music)
(ly:duration? (ly:music-property music 'duration)))
  (ly:duration? (ly:music-property music 'duration)))
 
#(define (not-has-duration? music)
#(define (not-has-duration? music)
(not (has-duration? music)))
  (not (has-duration? music)))
 
keepsOnlyFirstNote = #(define-music-function (parser location music) (ly:music?)
keepOnlyFirstNote =
(music-map
#(define-music-function (music) (ly:music?)
  (lambda (evt)
  (music-map
  (if (eq? 'EventChord (ly:music-property evt 'name))
    (lambda (evt)
      (let ((elts (ly:music-property evt 'elements)))
      (when (eq? 'EventChord (ly:music-property evt 'name))
      (if (has-duration? (car elts))
(let ((elts (ly:music-property evt 'elements)))
            (ly:music-set-property! evt 'elements (cons
  (when (has-duration? (car elts))
                (car elts)
    (ly:music-set-property!
                (filter not-has-duration? (cdr elts)))))))
    evt 'elements
  evt)
    (cons (car elts)
music))
  (filter not-has-duration? (cdr elts)))))))
      evt)
deleteFirstNote = #(define-music-function (parser location music) (ly:music?)
    music))
(music-map
 
  (lambda (evt)
deleteFirstNote =
  (if (eq? 'EventChord (ly:music-property evt 'name))
#(define-music-function (music) (ly:music?)
      (let ((elts (ly:music-property evt 'elements)))
  (music-map
          (if (has-duration? (car elts))
    (lambda (evt)
                (ly:music-set-property! evt 'elements  (cdr elts)))))
      (when (eq? 'EventChord (ly:music-property evt 'name))
  evt)
(let ((elts (ly:music-property evt 'elements)))
music))
          (when (has-duration? (car elts))
            (ly:music-set-property! evt 'elements  (cdr elts)))))
      evt)
music =\relative c' {
    music))
<c e>4-> <d f>( <nowiki><b g'>) <c e>-. g2 c2</nowiki>
 
}
 
music = \relative c' {
\markup { Music with chords }
  <c e>4-> <d f>( <b g'>) <c e>-. g2 c2
\new Staff \music
}
\markup { Music splitted in 2 staffs }
 
<<
\markup { Music with chords }
  \new Staff \deleteFirstNote \music
\new Staff \music
  \new Staff \keepsOnlyFirstNote \music
 
>>
\markup { Music split into two staves }
<span></span>
<<
  \new Staff \deleteFirstNote \music
  \new Staff \keepOnlyFirstNote \music
>>
</lilypond>
 
[[category:Chords]]
[[category:Scheme]]
[[category:Really cool]]

Latest revision as of 09:51, 7 April 2026

This snippet is a 2009 contribution by Gilles Thibault in lilypond-user. It provides two functions to split a chord sequence into two separate voices, also preserving articulations and slurs: \keepOnlyFirstNote extracts the first note (as given in the input; this is usually the lowest one) of every chord, and \deleteFirstNote collects the remaining notes.

Note that these functions don't support q to repeat the 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)))

keepOnlyFirstNote =
#(define-music-function (music) (ly:music?)
   (music-map
    (lambda (evt)
      (when (eq? 'EventChord (ly:music-property evt 'name))
 	(let ((elts (ly:music-property evt 'elements)))
 	  (when (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 (music) (ly:music?)
   (music-map
    (lambda (evt)
      (when (eq? 'EventChord (ly:music-property evt 'name))
 	(let ((elts (ly:music-property evt 'elements)))
          (when (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 split into two staves }
<<
  \new Staff \deleteFirstNote \music
  \new Staff \keepOnlyFirstNote \music
>>