Search code examples
schemeguilelilypondmusic-notation

Passing chords to and returning music with Lilypond music substitution functions


I am trying to write functions in Lilypond which take a chord (or list of pitches) as an argument and return music with said chord inserted into a rhythm. More specifically, I would like the function to be invoked in some way like this:

\chordFunction <c ef f af>
% or
\chordFunction #'(c ef f af)

and to return Lilypond code like so:

\tuplet 3/2 {<c ef f af>4 <c ef f af>8~} <c ef f af>2

pretty picture of desired output once engraved
I have tried

jazzsyncoA = 
#(define-music-function
  (parser location chord)
  (symbol-list-or-music?)
  #{
    \tuplet 3/2 {$<chord>4 $<chord>8~} $<chord>2
  #}
)

but that throws

error: GUILE signaled an error for the expression beginning here
    \tuplet 3/2 {$
                  <chord>4 $<chord>8~} $<chord>2

along with other errors when I attempt invocation. How should I write functions to accomplish this? Am I approaching the problem improperly?


I've looked in the Lilypond docs on predefined type predicates, but I can't recognize any of potential use.


Solution

  • I think you're more or less in the right direction. You could look at this LilyPond doc page to see how you might get started writing a pure Scheme function to solve this and other problems.

    As for your function, if you want to use your function like \chordFunction <c ef f af>, then you could ly:music? as the predicate:

    \version "2.18.0"
    \language "english"
    
    jazzsyncoA = #(define-music-function (parser location my-notes) (ly:music?)
        #{
            \tuplet 3/2 {  #my-notes q8~ } q2
        #}
    )
    
    \score {
        \new Staff {
            \clef "bass"
            \key c \minor
            \new Voice = "one" {
                \jazzsyncoA <c ef f af>4
            }
        }
    }
    
    

    This gives the the same result as what's in your picture. I tested this on version 2.19.82, but I'm guessing that it will work on 2.18.0 as well. Hope that it helps!