Jump to content

Splitting chords: Difference between revisions

From LilyPond wiki
m Node -> Note
m Not sure how it could happen that changing a single letter in the docstring completely ruined the rendering...
Line 3: Line 3:
Note that these function don't support <samp>q</samp> to repeat the last chord.
Note that these function don't support <samp>q</samp> to repeat the last chord.


<span></span>
<lilypond version="2.24">
\version "2.24"
#(define (has-duration? music)
  (ly:duration? (ly:music-property music 'duration)))
#(define (has-duration? music)
 
    (ly:duration? (ly:music-property music 'duration)))
#(define (not-has-duration? music)
  (not (has-duration? music)))
#(define (not-has-duration? music)
 
    (not (has-duration? music)))
keepOnlyFirstNote =
#(define-music-function (parser location music) (ly:music?)
keepOnlyFirstNote =
  (music-map
#(define-music-function (parser location music) (ly:music?)
    (lambda (evt)
    (music-map
      (when (eq? 'EventChord (ly:music-property evt 'name))
    (lambda (evt)
      (when (eq? 'EventChord (ly:music-property evt 'name))
  (let ((elts (ly:music-property evt 'elements)))
  (let ((elts (ly:music-property evt 'elements)))
    (when (has-duration? (car elts))
    (when (has-duration? (car elts))
Line 23: Line 21:
      (cons (car elts)
      (cons (car elts)
    (filter not-has-duration? (cdr elts)))))))
    (filter not-has-duration? (cdr elts)))))))
      evt)
      evt)
    music))
    music))
 
deleteFirstNote =
deleteFirstNote =
#(define-music-function (parser location music) (ly:music?)
#(define-music-function (parser location music) (ly:music?)
    (music-map
  (music-map
    (lambda (evt)
    (lambda (evt)
      (when (eq? 'EventChord (ly:music-property evt 'name))
      (when (eq? 'EventChord (ly:music-property evt 'name))
  (let ((elts (ly:music-property evt 'elements)))
  (let ((elts (ly:music-property evt 'elements)))
          (when (has-duration? (car elts))
          (when (has-duration? (car elts))
            (ly:music-set-property! evt 'elements  (cdr elts)))))
            (ly:music-set-property! evt 'elements  (cdr elts)))))
      evt)
      evt)
    music))
    music))
 
 
music = \relative c' {
music = \relative c' {
  <c e>4-> <d f>( <nowiki><b g'>) <c e>-. g2 c2</nowiki>
  <c e>4-> <d f>( <b g'>) <c e>-. g2 c2
}
}
 
\markup { Music with chords }
\markup { Music with chords }
\new Staff \music
\new Staff \music
 
\markup { Music split into two staves }
\markup { Music split into two staffs }
<<
<<
  \new Staff \deleteFirstNote \music
  \new Staff \deleteFirstNote \music
  \new Staff \keepOnlyFirstNote \music
  \new Staff \keepOnlyFirstNote \music
>>
>>
</lilypond>
<span></span>


[[category:Chords]]
[[category:Chords]]
[[category:Scheme]]
[[category:Scheme]]
[[category:Really cool]]
[[category:Really cool]]

Revision as of 03: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 function 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 (parser location 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 (parser location 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
>>