Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lilypond macro that outputs a \score block

Tags:

lilypond

I have the following Lilypond source file consisting of a few blocks of the following form:

global = \relative { ... }
Soprano = \relative { ... }
Alto = \relative { ... } % ditto Tenor, Bass

\score { \new StaffGroup <<
  \new Staff << \clef "G" \global \Soprano >>
  \new Staff << \clef "G" \global \Alto >>
  \new Staff << \clef "G_8" \global \Tenor >>
  \new Staff << \clef "F" \global \Bass >>
>> \layout { } }

Obviously the global, Soprano, Alto, Tenor, Bass definitions change each time, but the \score block remains the same.

I want to factor that block in a Scheme macro. However, the simplest definition I tried,

#(define (Choral) (ly:make-score #{ \new StaffGroup <<
  \new Staff << \clef "G" \global \Soprano >>
  \new Staff << \clef "G" \global \Alto >>
  \new Staff << \clef "G_8" \global \Tenor >>
  \new Staff << \clef "F" \global \Bass >>
>> #} ))

has the following inconvenients: (1) it must be invoked by #(Choral) instead of the more natural \Choral, and worse, (2) it produces no output whatsoever. If I try to put a \layout { } block in the (Choral) definition lilypond produces the following error: error: syntax error, unexpected \layout.

Is there a simple way to write a macro that produces a \score block with attached \layout ?

like image 550
Circonflexe Avatar asked Nov 07 '25 08:11

Circonflexe


1 Answers

Why a Scheme macro? You can just use \include (it's like pasting the content of a file in the line you put it). So you might use the same score block file:

% myScoreBlock.ly file
\score {
  \new StaffGroup <<
    \new Staff << \clef "G" \global \Soprano >>
    \new Staff << \clef "G" \global \Alto >>
    \new Staff << \clef "G_8" \global \Tenor >>
    \new Staff << \clef "F" \global \Bass >>
  >> \layout { }
}

to be included in any other file which have the same variables:

% example of a piece
\version "2.19.54"

global = { \time 2/4 }
Soprano = \relative { d2 }
Alto = \relative { f2 }
Tenor = \relative { e2 }
Bass = \relative { g2 }

\include "myScoreBlock.ly"

Another option to reduce the size of the input is using built-in templates. There's also a built-in template for SATB scores.

like image 130
fedelibre Avatar answered Nov 11 '25 00:11

fedelibre



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!