Difference between revisions of "LilyPond"

From Wiki
Jump to navigation Jump to search
(hints about newer LilyPond versions (2.7.26))
m (Text replacement - "</cmd>" to "}}")
(46 intermediate revisions by 10 users not shown)
Line 1: Line 1:
 
< [[Graphics]] | [[Using Graphics]] >
 
< [[Graphics]] | [[Using Graphics]] >
  
[http://www.lilypond.org LilyPond] is a great music engraver, and you can include LilyPond in ConTeXt source using [http://modules.contextgarden.net/t-lilypond the lilypond module.]
+
[http://lilypond.org LilyPond] is a great music engraver, and you can include LilyPond in ConTeXt source using [https://github.com/adityam/filter the filter module] with some setup. This is different from using the lilypond-book preprocessor with LaTeX. (For LaTeX there’s also a package that works similar to our ConTeXt setup here: [https://github.com/jperon/lyluatex lyluatex].)
  
== Setup ==
+
== Simple Filter Setup ==
 +
 
 +
This works with ConTeXt MkII and MkIV, but takes only the first page of multi-pages scores, and you must create the folder "lilytemp" manually:
  
* First you need a working [http://www.lilypond.org LilyPond] installation plus dependencies like GhostScript.
 
* Include the lilypond module
 
 
<texcode>
 
<texcode>
\usemodule[lilypond]
+
\def\readPDFfile#1{\externalfigure[#1]}
 +
 
 +
\usemodule[filter]
 +
\defineexternalfilter[lilypond]
 +
[continue=yes,
 +
readcommand=\readPDFfile,
 +
directory=lilytemp/, % directory for LilyPond's files
 +
output={\externalfilterbasefile.pdf},
 +
filtercommand={lilypond -dbackend=eps -ddelete-intermediate-files -o"lilytemp/\externalfilterbasefile" "\externalfilterinputfile"}]
 
</texcode>
 
</texcode>
* If you want, you can change the default settings using <cmd>setuplilypond</cmd>
 
* If you need lyrics with accented characters (e.g. umlauts), you '''must''' typeset in UTF-8 encoding (see [[Encodings_and_Regimes]]), because LilyPond doesn't understand anything else. And you need to use UTF-8 without BOM, because ConTeXt doesn't understand that.
 
  
== Snippets ==
+
== Multi Page Filter Setup ==
 +
 
 +
This uses Lua and therefore only works with ConTeXt MkIV. It includes all pages of multi-page scores.
 +
It doesn’t look into the complete (multi-page) PDF, but reads a "-system.count" auxiliary file written by LilyPond that contains the number of systems (pages) and includes the single-system PDFs.
  
 
<texcode>
 
<texcode>
There are some notes \lilypond{ \relative{bes a c b} } embedded in this line.
+
\def\LILYTEMP{lilytemp} % name of folder for LilyPond/buffer files
 +
 
 +
\def\ParseLilypondFile#1% #1 is the name of the output file
 +
  {\ctxlua{thirddata.parselilypondfile("#1")}}
 +
 
 +
\startluacode
 +
thirddata = thirddata or {}
 +
 +
-- create temp folder if missing
 +
if not lfs.isdir("\LILYTEMP") then
 +
  lfs.mkdir("\LILYTEMP")
 +
end
 +
 +
function thirddata.parselilypondfile(name)
 +
  -- include all systems (pages)
 +
  -- name is like \LILYTEMP/mainfile-temp-lilypond-21.pdf
 +
  logs.report("LILYPOND","name='" .. name .. "'")
 +
  local scname = string.gsub(name, '%.pdf$', '-systems.count')
 +
  local syco = tonumber(io.loaddata(scname)) or 0
 +
 
 +
  for nr = 1, syco do
 +
    logs.report("LILYPOND","including system no." .. nr)
 +
    context("\\externalfigure[" .. string.gsub(name, '%.pdf$', '-' .. nr) .. "]")
 +
  end
 +
end
 +
\stopluacode
 +
 
 +
\usemodule[filter]
 +
\defineexternalfilter[lilypond]
 +
[continue=yes,
 +
        cache=yes,
 +
readcommand=\ParseLilypondFile,
 +
directory=\LILYTEMP/,
 +
output={\externalfilterbasefile.pdf},
 +
filtercommand={lilypond -dbackend=eps -o"\LILYTEMP/\externalfilterbasefile" "\externalfilterinputfile"}]
 
</texcode>
 
</texcode>
  
== Sections ==
 
  
E.g. for a songbook you want to place big chunks of LilyPond output (i.e. note staffs) in your text. It behaves like any other graphics, especially similar to embedded [[Metapost|MetaPost]] code:
+
== LilyPond Settings ==
 +
 
 +
Collect your LilyPond settings in a .ly file, put it in your lilytemp directory and include it from within your lilypond block like this:
  
 
<texcode>
 
<texcode>
\section{A Tune}
+
\startlilypond
 +
\include "mysettings.ly"
 +
...
 +
\stoplilypond
 +
</texcode>
  
\startlilypond
+
You can avoid this \include line following [http://modules.contextgarden.net/dl/t-filter/doc/context/third/filter/filter.txt filter module documentation], section "Prepend and append text".
\relative {
+
In short: you can inline your complete LilyPond settings or at least the include line:
\repeat volta 2 {
+
 
\partial 4 e4 |
+
<texcode>
a2 c4 d |
+
\startbuffer[lilypond::settings]
e2 f4 e |
+
\include "mysettings.ly"
d2. c4 |
+
\stopbuffer
b4. c8 d4 e |
+
 
a,2 c4 d |
+
\defineexternalfilter[lilypond][
e2 f4 e |
+
...
g,2 a |
+
bufferbefore={lilypond::settings},
\partial 2. b2. |
+
...]
}
+
</texcode>
\repeat volta 2 {
+
 
\partial 4 r4 |
+
=== Sample Include File ===
f' g f d |
+
 
e f e c |
+
"mysettings.ly" could look like this:
a b c d |
+
 
e2. e4 |
+
<texcode>
f g f8 e d4 |
+
\version "2.18.2"
e f e c |
+
#(ly:set-option (quote no-point-and-click))
a c b8 a g4 |
+
#(set-global-staff-size 14)
}
+
 
\alternative {
+
% --- start of setup for single-line output files ---
{ \partial 2. g2. }
+
#(define default-toplevel-book-handler
{ \partial 2. a2. }
+
  print-book-with-defaults-as-systems )
 +
 
 +
#(define toplevel-book-handler
 +
  (lambda ( . rest)
 +
  (set! output-empty-score-list #f)
 +
  (apply print-book-with-defaults rest)))
 +
 
 +
#(define toplevel-music-handler
 +
  (lambda ( . rest)
 +
  (apply collect-music-for-book rest)))
 +
 
 +
#(define toplevel-score-handler
 +
  (lambda ( . rest)
 +
  (apply collect-scores-for-book rest)))
 +
 
 +
#(define toplevel-text-handler
 +
  (lambda ( . rest)
 +
  (apply collect-scores-for-book rest)))
 +
 
 +
#(set! output-empty-score-list #t)
 +
 
 +
% --- stop single-line setup ---
 +
 
 +
\paper {
 +
#(define dump-extents #t)
 +
indent = 0\mm
 +
ragged-bottom = ##t
 +
ragged-last-bottom = ##t
 +
print-page-number = ##f
 +
line-width = 120\mm
 +
oddFooterMarkup  = ##f
 +
oddHeaderMarkup  = ##f
 +
bookTitleMarkup  = ##f
 +
scoreTitleMarkup = ##f
 +
}
 +
 
 +
\layout {
 +
#(layout-set-staff-size 14) % beware, this resets fonts!
 +
% set fonts for rm / ss / tt
 +
#(define fonts (make-pango-font-tree "TeX Gyre Schola" "LMSans10" "LMTypewriter10 Regular" (/ 14 20)))
 +
\context { \Score
 +
\remove "Bar_number_engraver"
 +
\override PaperColumn #'keep-inside-line = ##t
 +
}
 +
        % some example settings
 +
\context { \Staff
 +
  \override TimeSignature #'style = #'numbered
 +
}
 +
\context { \ChordNames
 +
chordChanges = ##t
 +
majorSevenSymbol = \markup{ 7+ }
 
}
 
}
 
}
 
}
\stoplilypond
 
  
There's nothing to say about this tune yet; my friend heard it from Lúnasa.
 
 
</texcode>
 
</texcode>
  
== Developer's Corner ==
+
Please look up the meaning of settings in [http://lilypond.org/doc/v2.18/Documentation/web/manuals LilyPond’s great documentation]!
 +
 
 +
If you’d like to exchange measures like text width between ConTeXt and LilyPond, you could write those into the LilyPond buffer.
 +
To give LilyPond measures to ConTeXt, you would need to write them into a temp file (using Scheme) and read that in again (useing Lua).
 +
For the time being this is left as an exercise to the user. ;)
 +
 
 +
Unfortunately you can’t define LilyPond’s text (lyrics) size with an absolute value, but only relative to staff size...
 +
 
 +
== Named Buffers ==
 +
 
 +
Normally, your LilyPond snippets just get a running number. If you re-order your scores, each one gets re-rendered.
 +
 
 +
You can avoid that if you name your LilyPond snippets: just add <tt>[name=myfunnyname]</tt> to {{cmd|startlilypond}}.
 +
 
 +
If you have just one LilyPond part per component (e.g. songs in a songbook), you could use <tt>[name=\currentcomponent]</tt>.
 +
 
 +
== Automatical width adaption ==
 +
 
 +
If you want to set your note line width automatically to your current text width, try this:
 +
 
 +
  -- this goes after "create temp folder"
 +
  io.savedata("\LILYTEMP/texsettings.ly", "\\paper { line-width = " ..
 +
string.gsub(number.todimen(tex.dimen.textwidth),"pt","\\pt") .. " } \n")
 +
 
 +
and then adapt your snippets or preamble buffer:
 +
<texcode>
 +
\include "../mysettings.ly"
 +
\include "texsettings.ly"
 +
</texcode>
 +
 
 +
You must comment/delete the "line-width" setting in mysettings.ly, otherwise it doesn’t work.
  
For information how the integration works, please check the module code and its pdf version (both available from [http://modules.contextgarden.net/t-lilypond]).  Unlike lilypond-book for LaTeX, the module does not use a precompiling step and thus can react to local width changes and the like, even if the lilypond code is stored in a buffer.
+
== Example ==
  
Things that have not been implemented yet include:
+
Here's an example of placing score snippets in the body of the text, with fonts in the score & body matching:
* set the text font (default should be ConTeXt's bodyfont instead of LilyPond's)
 
* make lilypond call back on ConTeXt for included TeX (cf. [http://lsr.dsi.unimi.it/LSR/Item?id=107])
 
* get information from lilypond about the baseline, for run-in music fragments
 
* tell lilypond how much space is left on the first page
 
* check and probably work on proper multi-page music
 
* LilyPond variables
 
  
LilyPond uses fontconfig for font search. At least on MacOS X it's very restricted what kind of fonts it can use - only single-style TrueType (including dfont). I didn't manage yet to use fonts from the TeX tree. (That doesn't seem to be fontconfig's fault - it writes appropriate font cache files everywhere.)
+
<texcode>
 +
\def\LILYTEMP{lilytemp} % name of folder for LilyPond/buffer files
  
To remember:
+
\def\ParseLilypondFile#1% #1 is the name of the output file
* see [http://lilypond.org/doc/v2.7/Documentation/out-www/ LilyPond Docs]!
+
  {\ctxlua{thirddata.parselilypondfile("#1")}}
* How do we handle version/syntax changes of LilyPond?
 
  
--[[User:ChristopherCreutzig|Christopher Creutzig]] after ideas by [[User:Hraban|Hraban]]
+
\startluacode
 +
thirddata = thirddata or {}
  
== Workarounds ==
+
-- create temp folder if missing
 +
if not lfs.isdir("\LILYTEMP") then
 +
  lfs.mkdir("\LILYTEMP")
 +
end
  
=== LilyPond is not found ===
+
function thirddata.parselilypondfile(name)
 +
  -- include all systems (pages)
 +
  -- name is like \LILYTEMP/mainfile-temp-lilypond-21.pdf
 +
  logs.report("LILYPOND","name='" .. name .. "'")
 +
  local scname = string.gsub(name, '%.pdf$', '-systems.count')
 +
  local syco = tonumber(io.loaddata(scname)) or 0
  
Calling lilypond from command line in Windows fails. (Seems like a bug in lilypond.)
+
  for nr = 1, syco do
 +
    logs.report("LILYPOND","including system no." .. nr)
 +
    context("\\setupfloats[location=right,frame=off]\\placefigure[none]{}{\\externalfigure[" .. string.gsub(name, '%.pdf$', '-' .. nr) .. "]}")
 +
  end
 +
end
 +
\stopluacode
  
You have two options:
+
\usemodule[filter]
* modify the module, so that lilypond is called with the whole path (<code>C:/prog/lilypond/usr/bin/lilypond</code> for example instead of <code>lilypond</code> alone).
+
\defineexternalfilter[lilypond]
* place a <code>lilypond.bat</code> somewhere in your search path, calling
+
  [continue=yes,
c:/prog/lilypond/usr/bin/lilypond %1 %2 %3 %4 %5 %6 %7 %8 %9
+
        cache=yes,
 +
  readcommand=\ParseLilypondFile,
 +
  directory=\LILYTEMP/,
 +
  output={\externalfilterbasefile.pdf},
 +
  filtercommand={lilypond -dbackend=eps -dinclude-eps-fonts -dno-gs-load-fonts -o"\LILYTEMP/\externalfilterbasefile" "\externalfilterinputfile"}]
  
Calling lilypond from ConTeXt via shell exit (write18) in MacOS X also fails, even if the shell's path is right. (Seems more like write18 wouldn't use the environment.) Modify the module (try <tt>which lilypond</tt> to get its path).
+
\setuplayout[textwidth=6in] % matches line-width below
 +
\definefontfeature[main][protrusion=quality, expansion=quality]
 +
\definefontfamily[mainface][rm][Adobe Jenson Pro][features=main]
 +
\setupbodyfont[mainface,13pt]
 +
\setupalign[hz,hanging]
  
LilyPond 2.7.26 comes without a command line "version", at least in the MacOS X edition, i.e. the <tt>lilypond.sh</tt> script is missing.
+
\starttext
Here's it's relevant content:
 
  
#!/bin/sh
+
\input zapf
## where was the app installed?
+
 
  INSTALLDIR=/Applications
+
\startlilypond
+
 
if [ "$1" == "--print-appdir" ]; then
+
\layout{
  echo "$INSTALLDIR/LilyPond.app/"
+
  indent=0\mm
  exit 0
+
  ragged-right = ##f
fi
+
}
+
\paper {
# where to put the output
+
myStaffSize = #20
export LILYPOND_DESTDIR=`pwd`
+
  #(define fonts
+
    (make-pango-font-tree "Adobe Jenson Pro"
# run the program
+
                          "Myriad Pro"
  python "$INSTALLDIR/LilyPond.app/Contents/Resources/lilycall.py" \
+
                          "Myriad Pro"
  "$INSTALLDIR/LilyPond.app/" $@
+
(/ myStaffSize 20)))
 +
line-width=6\in
 +
  oddFooterMarkup=##f
 +
  oddHeaderMarkup=##f
 +
  bookTitleMarkup = ##f
 +
  scoreTitleMarkup = ##f
 +
  }
 +
melody = \relative c'' {
 +
  \clef treble
 +
  \key c \major
 +
  \time 4/4
 +
 
 +
  a b c d
 +
}
 +
 
 +
text = \lyricmode {
 +
Aaa Bee Cee Dee
 +
}
 +
 
 +
upper = \relative c'' {
 +
  \clef treble
 +
  \key c \major
 +
  \time 2/4
 +
 
 +
  a4 b c d
 +
}
 +
 
 +
lower = \relative c {
 +
  \clef bass
 +
  \key c \major
 +
  \time 2/4
 +
 
 +
  a2 c
 +
}
 +
 
 +
\score {
 +
  <<
 +
    \new Voice = "mel" { \autoBeamOff \melody }
 +
    \new Lyrics \lyricsto mel \text
 +
    \new PianoStaff <<
 +
      \new Staff = "upper" \upper
 +
      \new Staff = "lower" \lower
 +
    >>
 +
  >>
 +
  \layout {
 +
    \context { \Staff \RemoveEmptyStaves }
 +
  }
 +
}
 +
 
 +
\stoplilypond
 +
\input tufte
 +
 
 +
\stoptext
 +
</texcode>
 +
 
 +
== Too many open files ==
 +
 
 +
With bigger projects and the above setup you might run into "too many open files" errors, at least in Linux, OSX and other Unix-like OSes.
 +
Ask your shell about the open files maximum and set it higher:
 +
 
 +
  $ ulimit -n
 +
  256
 +
  $ ulimit -S -n 2048
  
Save this as <tt>lilypond.sh</tt>, make it executable and symlink it into a directory on your path, e.g.
+
== Deprecation ==
chmod a+x lilypond.sh
 
sudo ln -s lilypond.sh /usr/local/bin/lilypond
 
  
=== LilyPond stops with an 'Unbound variable' error ===
+
The old LilyPond module (t-lilypond) doesn't work any more with recent versions of ConTeXt, therefore we removed its documentation here.
  
The [http://modules.contextgarden.net/t-lilypond LilyPond module] as of 2005-09-12 uses some commands in its header that work only with LilyPond 2.6 (at least no more with 2.7.25); replace "ly:parser-print-score" with "print-score-with-defaults" and "ly:music-scorify" with "scorify-music" for LilyPond 2.7 series.
 
  
 
[[Category:Modules]]
 
[[Category:Modules]]
[[Category:Music]]
 
[[Category:Requests]]
 

Revision as of 13:23, 9 August 2020

< Graphics | Using Graphics >

LilyPond is a great music engraver, and you can include LilyPond in ConTeXt source using the filter module with some setup. This is different from using the lilypond-book preprocessor with LaTeX. (For LaTeX there’s also a package that works similar to our ConTeXt setup here: lyluatex.)

Simple Filter Setup

This works with ConTeXt MkII and MkIV, but takes only the first page of multi-pages scores, and you must create the folder "lilytemp" manually:

\def\readPDFfile#1{\externalfigure[#1]}

\usemodule[filter]
\defineexternalfilter[lilypond]
	[continue=yes,
	readcommand=\readPDFfile,
	directory=lilytemp/, % directory for LilyPond's files
	output={\externalfilterbasefile.pdf},
	filtercommand={lilypond -dbackend=eps -ddelete-intermediate-files -o"lilytemp/\externalfilterbasefile" "\externalfilterinputfile"}]

Multi Page Filter Setup

This uses Lua and therefore only works with ConTeXt MkIV. It includes all pages of multi-page scores. It doesn’t look into the complete (multi-page) PDF, but reads a "-system.count" auxiliary file written by LilyPond that contains the number of systems (pages) and includes the single-system PDFs.

\def\LILYTEMP{lilytemp} % name of folder for LilyPond/buffer files

\def\ParseLilypondFile#1% #1 is the name of the output file
  {\ctxlua{thirddata.parselilypondfile("#1")}}

\startluacode
 thirddata = thirddata or {}
 
 -- create temp folder if missing
 if not lfs.isdir("\LILYTEMP") then
   lfs.mkdir("\LILYTEMP")
 end
 
 function thirddata.parselilypondfile(name)
   -- include all systems (pages)
   -- name is like \LILYTEMP/mainfile-temp-lilypond-21.pdf
   logs.report("LILYPOND","name='" .. name .. "'")
   local scname = string.gsub(name, '%.pdf$', '-systems.count')
   local syco = tonumber(io.loaddata(scname)) or 0
   
   for nr = 1, syco do
     logs.report("LILYPOND","including system no." .. nr)
     context("\\externalfigure[" .. string.gsub(name, '%.pdf$', '-' .. nr) .. "]")
   end
 end
\stopluacode

\usemodule[filter]
\defineexternalfilter[lilypond]
	[continue=yes,
        cache=yes,
	readcommand=\ParseLilypondFile,
	directory=\LILYTEMP/,
	output={\externalfilterbasefile.pdf},
	filtercommand={lilypond -dbackend=eps -o"\LILYTEMP/\externalfilterbasefile" "\externalfilterinputfile"}]


LilyPond Settings

Collect your LilyPond settings in a .ly file, put it in your lilytemp directory and include it from within your lilypond block like this:

\startlilypond
\include "mysettings.ly"
...
\stoplilypond

You can avoid this \include line following filter module documentation, section "Prepend and append text". In short: you can inline your complete LilyPond settings or at least the include line:

\startbuffer[lilypond::settings]
\include "mysettings.ly"
\stopbuffer

\defineexternalfilter[lilypond][
	...
	bufferbefore={lilypond::settings},
	...]

Sample Include File

"mysettings.ly" could look like this:

\version "2.18.2"
#(ly:set-option (quote no-point-and-click))
#(set-global-staff-size 14)

% --- start of setup for single-line output files ---
#(define default-toplevel-book-handler
  print-book-with-defaults-as-systems )

#(define toplevel-book-handler
  (lambda ( . rest)
  (set! output-empty-score-list #f)
  (apply print-book-with-defaults rest)))

#(define toplevel-music-handler
  (lambda ( . rest)
   (apply collect-music-for-book rest)))

#(define toplevel-score-handler
  (lambda ( . rest)
   (apply collect-scores-for-book rest)))

#(define toplevel-text-handler
  (lambda ( . rest)
   (apply collect-scores-for-book rest)))

#(set! output-empty-score-list #t)

% --- stop single-line setup ---

\paper {
	#(define dump-extents #t)
	indent = 0\mm
	ragged-bottom = ##t
	ragged-last-bottom = ##t
	print-page-number = ##f
	line-width = 120\mm
	oddFooterMarkup  = ##f
	oddHeaderMarkup  = ##f
	bookTitleMarkup  = ##f
	scoreTitleMarkup = ##f
}

\layout {
	#(layout-set-staff-size 14) % beware, this resets fonts!
	% set fonts for rm / ss / tt 
	#(define fonts (make-pango-font-tree "TeX Gyre Schola" "LMSans10" "LMTypewriter10 Regular" (/ 14 20)))
 	\context { \Score
		\remove "Bar_number_engraver"
		\override PaperColumn #'keep-inside-line = ##t
 	}
        % some example settings
	\context { \Staff
  		\override TimeSignature #'style = #'numbered
 	}
	\context { \ChordNames
		chordChanges = ##t
		majorSevenSymbol = \markup{ 7+ }
	}
}

Please look up the meaning of settings in LilyPond’s great documentation!

If you’d like to exchange measures like text width between ConTeXt and LilyPond, you could write those into the LilyPond buffer. To give LilyPond measures to ConTeXt, you would need to write them into a temp file (using Scheme) and read that in again (useing Lua). For the time being this is left as an exercise to the user. ;)

Unfortunately you can’t define LilyPond’s text (lyrics) size with an absolute value, but only relative to staff size...

Named Buffers

Normally, your LilyPond snippets just get a running number. If you re-order your scores, each one gets re-rendered.

You can avoid that if you name your LilyPond snippets: just add [name=myfunnyname] to \startlilypond.

If you have just one LilyPond part per component (e.g. songs in a songbook), you could use [name=\currentcomponent].

Automatical width adaption

If you want to set your note line width automatically to your current text width, try this:

 -- this goes after "create temp folder"
 io.savedata("\LILYTEMP/texsettings.ly", "\\paper { line-width = " .. 
	string.gsub(number.todimen(tex.dimen.textwidth),"pt","\\pt") .. " } \n")

and then adapt your snippets or preamble buffer:

\include "../mysettings.ly"
\include "texsettings.ly"

You must comment/delete the "line-width" setting in mysettings.ly, otherwise it doesn’t work.

Example

Here's an example of placing score snippets in the body of the text, with fonts in the score & body matching:

\def\LILYTEMP{lilytemp} % name of folder for LilyPond/buffer files

\def\ParseLilypondFile#1% #1 is the name of the output file
  {\ctxlua{thirddata.parselilypondfile("#1")}}

\startluacode
 thirddata = thirddata or {}

 -- create temp folder if missing
 if not lfs.isdir("\LILYTEMP") then
   lfs.mkdir("\LILYTEMP")
 end

 function thirddata.parselilypondfile(name)
   -- include all systems (pages)
   -- name is like \LILYTEMP/mainfile-temp-lilypond-21.pdf
   logs.report("LILYPOND","name='" .. name .. "'")
   local scname = string.gsub(name, '%.pdf$', '-systems.count')
   local syco = tonumber(io.loaddata(scname)) or 0

   for nr = 1, syco do
     logs.report("LILYPOND","including system no." .. nr)
     context("\\setupfloats[location=right,frame=off]\\placefigure[none]{}{\\externalfigure[" .. string.gsub(name, '%.pdf$', '-' .. nr) .. "]}")
   end
 end
\stopluacode

\usemodule[filter]
\defineexternalfilter[lilypond]
  [continue=yes,
        cache=yes,
  readcommand=\ParseLilypondFile,
  directory=\LILYTEMP/,
  output={\externalfilterbasefile.pdf},
  filtercommand={lilypond -dbackend=eps -dinclude-eps-fonts -dno-gs-load-fonts -o"\LILYTEMP/\externalfilterbasefile" "\externalfilterinputfile"}]

 \setuplayout[textwidth=6in] % matches line-width below
\definefontfeature[main][protrusion=quality, expansion=quality]
\definefontfamily[mainface][rm][Adobe Jenson Pro][features=main]
\setupbodyfont[mainface,13pt]
\setupalign[hz,hanging]

\starttext

\input zapf

\startlilypond

\layout{
  indent=0\mm
  ragged-right = ##f
}
\paper  {
myStaffSize = #20
  #(define fonts
    (make-pango-font-tree "Adobe Jenson Pro"
                          "Myriad Pro"
                          "Myriad Pro"
(/ myStaffSize 20)))
line-width=6\in
  oddFooterMarkup=##f
  oddHeaderMarkup=##f
  bookTitleMarkup = ##f
  scoreTitleMarkup = ##f
 }
melody = \relative c'' {
  \clef treble
  \key c \major
  \time 4/4

  a b c d
}

text = \lyricmode {
Aaa Bee Cee Dee
}

upper = \relative c'' {
  \clef treble
  \key c \major
  \time 2/4

  a4 b c d
}

lower = \relative c {
  \clef bass
  \key c \major
  \time 2/4

  a2 c
}

\score {
  <<
    \new Voice = "mel" { \autoBeamOff \melody }
    \new Lyrics \lyricsto mel \text
    \new PianoStaff <<
      \new Staff = "upper" \upper
      \new Staff = "lower" \lower
    >>
  >>
  \layout {
    \context { \Staff \RemoveEmptyStaves }
  }
}

\stoplilypond
\input tufte

\stoptext

Too many open files

With bigger projects and the above setup you might run into "too many open files" errors, at least in Linux, OSX and other Unix-like OSes. Ask your shell about the open files maximum and set it higher:

 $ ulimit -n
 256
 $ ulimit -S -n 2048

Deprecation

The old LilyPond module (t-lilypond) doesn't work any more with recent versions of ConTeXt, therefore we removed its documentation here.