lilypond-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Function for /score block


From: David Kastrup
Subject: Re: Function for /score block
Date: Thu, 25 Apr 2019 16:37:13 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux)

Aaron Hill <address@hidden> writes:

> I found using ly:parser-include-string was helpful for this purpose.

That's a sledgehammer.

> Consider the following:
>
> %%%%
> \version "2.19.82"
>
> Alice.Melody = \fixed f' { f4 g a bes c'1 }
> Alice.Lyrics = \lyricmode { do re mi fa sol }
>
> Bob.Melody = \fixed f' { c'4 bes d' g a1 }
> Bob.Lyrics = \lyricmode { sol fa la re mi }
>
> Global = {
>   \key f \major
>   s1*2 \bar "|."
> }
>
> makeItSo = #(define-music-function (label) (symbol?)
>   (define (lookup section) (ly:parser-include-string
>     (format #f "\\~a.~a" label section)))
>   #{ <<
>        \new Staff \with {
>          instrumentName = $(symbol->string label)
>        } <<
>          \Global
>          \new Voice = "melody" { $(lookup 'Melody) }
>        >>
>        \new Lyrics \lyricsto "melody" { $(lookup 'Lyrics) }
>      >> #} )
>
> \makeItSo #'Alice
> \makeItSo #'Bob
> %%%%
>
> This \makeItSo is akin to a macro, able to inject a dynamically
> constructed string into the parser as if it were typed by hand.

With the same interface, I'd use
%%%%
\version "2.19.82"

Alice.Melody = \fixed f' { f4 g a bes c'1 }
Alice.Lyrics = \lyricmode { do re mi fa sol }

Bob.Melody = \fixed f' { c'4 bes d' g a1 }
Bob.Lyrics = \lyricmode { sol fa la re mi }

Global = {
  \key f \major
  s1*2 \bar "|."
}

makeItSo =
#(define-music-function (label) (symbol?)
  (let ((name (symbol->string label))
        (value (ly:parser-lookup label)))
   #{ <<
       \new Staff \with {
         instrumentName = #name
       } <<
         \Global
         \new Voice = "melody" { $value . Melody }
       >>
       \new Lyrics \lyricsto "melody" { $value . Lyrics }
     >> #} ))

\makeItSo #'Alice
\makeItSo #'Bob
%%%%

But I don't like this kind of double-serve and it's not particularly
flexible, so I'd rather use

%%%%
\version "2.19.82"

Alice.Melody = \fixed f' { f4 g a bes c'1 }
Alice.Lyrics = \lyricmode { do re mi fa sol }

Bob.Melody = \fixed f' { c'4 bes d' g a1 }
Bob.Lyrics = \lyricmode { sol fa la re mi }

Global = {
  \key f \major
  s1*2 \bar "|."
}

makeItSo =
#(define-music-function (name value) (markup? list?)
   #{ <<
       \new Staff \with {
         instrumentName = #name
       } <<
         \Global
         \new Voice = "melody" { $value . Melody }
       >>
       \new Lyrics \lyricsto "melody" { $value . Lyrics }
     >> #} )

\makeItSo "Alice" \Alice
\makeItSo "Bob" \Bob
%%%%

Or you could also refer to this from a main variable by putting

Alice.name = "Alice"
Bob.name = "Bob"

and then fetching stuff from there.

-- 
David Kastrup



reply via email to

[Prev in Thread] Current Thread [Next in Thread]