1

How to define a function that displays a chord both as music and as markup?

I'd like to get something like <c e g>1^\markup "c e g" without doubling the chord (c e g). E.g. with a function like \myFunction <c e g>

myFunction = #(define-music-function (myChord)
                (ly:music?)
                #{
                  $myChord^\markup {$myChord} % <- $myChord should be "expanded" as string in the markup
                #})

\relative c' {
  <c e g>1^\markup "c e g"
  \myFunction <c e g>
}

1 Answer 1

1

You need to extract the pitches and format them like this

myFunction =
#(define-music-function (myChord) (ly:music?)
   (let* ((elts (ly:music-property myChord 'elements))
          (pitches (map (lambda (x) (ly:music-property x 'pitch)) elts))
          (pitches (filter (lambda (x) (not (null? x))) pitches))
          (pitch-strings (map (lambda (x) (note-name->markup x #t)) pitches)))
     #{
       $myChord ^\markup $pitch-strings
     #}))

or maybe like this

#(define (format-pc pitch)
   (define (impl pitches)
     (if (null? pitches)
         (ly:error "Pitch name does not exist!")
         (let ((p (cdar pitches)))
           (if (and (= (ly:pitch-notename pitch) (ly:pitch-notename p))
                    (= (ly:pitch-alteration pitch) (ly:pitch-alteration p)))
               (caar pitches)
               (impl (cdr pitches))))))
   (symbol->string (impl pitchnames)))

myFunctionII =
#(define-music-function (myChord) (ly:music?)
   (let* ((elts (ly:music-property myChord 'elements))
          (pitches (map (lambda (x) (ly:music-property x 'pitch)) elts))
          (pitches (filter (lambda (x) (not (null? x))) pitches))
          (pitch-strings (map format-pc pitches)))
     #{
       $myChord ^\markup $pitch-strings
     #}))

Or maybe something like this if you want Lilypond code output

myFunctionIII =
#(define-music-function (myChord) (ly:music?)
   #{
     $myChord ^\markup #((@ (lily display-lily) music->lily-string) myChord)
   #})
2
  • Thank you very much!
    – wolfrevo
    Oct 4 at 10:41
  • BTW: what does pitchnames mean? I've seen that this is a parser variable. But where is it defined?
    – wolfrevo
    Oct 4 at 11:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.