Changes

Jump to navigation Jump to search
2,359 bytes added ,  18:21, 25 January 2021
adapt to current working setup
< [[Graphics]] | [[Using Graphics]] >
[http://www.lilypond.org LilyPond] is a great music engraver, and you can include LilyPond in ConTeXt source using [httphttps://modulesgithub.contextgarden.netcom/adityam/t-lilypond filter the lilypond 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: [[Imagehttps:T-lilypond//github.tex]com/jperon/lyluatex 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:
* First you need a working [http://www.lilypond.org LilyPond] installation plus dependencies like GhostScript.
* Include the lilypond module
<texcode>
\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>
* 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 (byte order marker), because ConTeXt doesn't understand that.
==Multi Page Filter Setup = options ==={| style="border: solid 1px black; padding: 0.5em; vertical-align: top; width: 50%;" |- style="border: solid 1px black; padding: 0.5em; vertical-align: top; background: #eeeeee;"! option !! values !! default !! meaning|-| staffsize || number (pt) || 20 || height of the staff|-| time || yes/no || yes || show time signature?|-| align || yes/no || depends on fragment || ragged-right = (not align)|-| fragment || yes/no || no || typeset only a snippet (instead of a whole line)?|-| barnumbers || yes/no || no || show bar numbers?|}
== Snippets ==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>
There are some notes \lilyponddef\LILYTEMP{lilytemp} % name of folder for LilyPond/buffer files \def\ParseLilypondFile#1% #1 is the name of the output file { \relativectxlua{bes a c bthirddata.parselilypondfile("#1")}}  \startluacode thirddata = thirddata or {} embedded in this line -- 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 -ddelete-intermediate-files -o"\LILYTEMP/\externalfilterbasefile/" "\externalfilterinputfile"},]
</texcode>
== Sections ==
E.g. for == LilyPond Settings == Collect your LilyPond settings in a songbook you want to place big chunks of LilyPond output (i.e. note staffs) ly file, put it in your text. It behaves lilytemp directory and include it from within your lilypond block like any other graphics, especially similar to embedded [[MetaPost]] codethis:
<texcode>
\section{A Tune}
 
\startlilypond
\relative { \repeat volta 2 { \partial 4 e4 | a2 c4 d | e2 f4 e | d2. c4 | b4. c8 d4 e | a,2 c4 d | e2 f4 e | g,2 a | \partial 2. b2. | } \repeat volta 2 { \partial 4 r4 | f' g f d | e f e c | a b c d | e2include "mysettings. e4 | f g f8 e d4 |ly" e f e c | a c b8 a g4 | } \alternative { { \partial 2. g2. } { \partial 2. a2. } }}
\stoplilypond
</texcode>
 
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".
In short: you can inline your complete LilyPond settings or at least the include line:
 
<texcode>
\startbuffer[lilypond::settings]
\include "mysettings.ly"
\stopbuffer
There's nothing to say about this tune yet; my friend heard it from Lúnasa\defineexternalfilter[lilypond][ ... bufferbefore={lilypond::settings}, ...]
</texcode>
 
=== Sample Include File ===
 
"mysettings.ly" could look like this:
<texcode>
\sectionversion "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 {Starlight #(rounddefine 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}
\startlilypond<<\context Staff = onlyone <<layout { \cleg treble#(layout-set-staff-size 14) % beware, this resets fonts! \key a \major% set fonts for rm / ss / tt \time 6#(define fonts (make-pango-font-tree "TeX Gyre Schola" "LMSans10" "LMTypewriter10 Regular" (/814 20))) \context Voice = one {\Score \relative cremove "Bar_number_engraver" \override PaperColumn #'' {keep-inside-line = ##t a4.^\markup{1. } e' | e8( d cis) b4. | % some example settings e4.^ \markupcontext {2.} d4 d8 |\Staff cis( b) a b4 e,8 | a4 a8 gis( a) b | \override TimeSignature #'style = #'numbered cis4 cis8 b( cis d) | cis( d) e e4 e,8 | fis4 fis8 gis4. } }>>\lyricsto one \new Lyrics context { \lyricmode {ChordNames Star -- light, star -- bright,chordChanges = ##t first star I see to -- night; I wish I may, I wish I might have the wish I wish to -- night.majorSevenSymbol = \markup{ 7+ }
}
}
>>
\stoplilypond
</texcode>
== Developer's Corner ==Please look up the meaning of settings in [http://lilypond.org/doc/v2.22/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 (see below).
For information how the integration worksTo give LilyPond measures to ConTeXt, please check the module code you would need to write them into a temp file (using Scheme) and its pdf version read that in again (both available from [http://modules.contextgarden.net/t-lilypond]using Lua). Unlike lilypond-book for LaTeX, For the module does not use a precompiling step and thus can react time being this is left as an exercise to local width changes and the like, even if the lilypond code is stored in a bufferuser.;)
Things that have not been implemented yet include:* set the Unfortunately you can’t define LilyPond’s text font (default should be ConTeXt's bodyfont instead of LilyPond'slyrics)* make lilypond call back on ConTeXt for included TeX (cfsize with an absolute value, but only relative to staff size. [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.)== Named Buffers ==
To remember:* see [http://lilypondNormally, your LilyPond snippets just get a running number.org/doc/v2If you re-order your scores, each one gets re-rendered.9/Documentation/ LilyPond Docs]!* How do we handle version/syntax changes of LilyPond?
--You can avoid that if you name your LilyPond snippets: just add <tt>[[User:ChristopherCreutzig|Christopher Creutzig]name=myfunnyname] after ideas by [[User:Hraban</tt> to {{cmd|Hraban]]startlilypond}}.
If you have just one LilyPond part per component (e.g. songs in a songbook), you could use <tt>[name== Tweaks ==\currentcomponent]</tt>.
At the moment it's not possible == Automatical width adaption == If you want to set your note line width automatically to change LilyPond's fontsyour 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. The text font is Century Schoolbookly"\include "texsettings. ly"</texcode> You can use must comment/delete the "line-width" setting in mysettings.ly, otherwise it as ConTeXtdoesn’t work. == Example == Here's default font like thisan example of placing score snippets in the body of the text, with fonts in the score & body matching:
<texcode>
\starttypescript[wiki][songbook]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("\usetypescript [serif] [schoolbook] [LILYTEMP") end  function thirddata.parselilypondfile(name) -- include all systems (pages) -- name is like \defaultencoding]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("\definetypeface \setupfloats[songbooklocation=right,frame=off] \\placefigure[rmnone] {}{\\externalfigure[serif" .. string.gsub(name, '%.pdf$', '-' .. nr) .. "] }") end end\stopluacode \usemodule[schoolbookfilter] \defineexternalfilter[defaultlilypond] [encodingcontinue=yes, cache=yes, readcommand=\defaultencoding]ParseLilypondFile, directory=\LILYTEMP/, output={\externalfilterbasefile.pdf}, filtercommand={lilypond -dbackend=eps -dinclude-eps-fonts -dno-gs-load-fonts -o"\definetypeface [songbook] [ss] [sansLILYTEMP/\externalfilterbasefile" "\externalfilterinputfile"}]   \setuplayout[defaulttextwidth=6in]% matches line-width below\definefontfeature[defaultmain] [encodingprotrusion=ecquality, expansion=quality]\definetypeface definefontfamily[songbookmainface] [ttrm] [monoAdobe Jenson Pro] [defaultfeatures=main]\setupbodyfont[defaultmainface,13pt] \setupalign[encodinghz,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 =ec]##f }melody = \relative c'' { \clef treble \key c \major \stoptypescripttime 4/4  a b c d}
text = \usetypescript [wiki] [songbook]lyricmode {\setupbodyfont[songbook, rm, 8.5pt]Aaa Bee Cee Dee</texcode>}
upper == Older Workarounds ==\relative c'' { \clef treble \key c \major \time 2/4
'''Beware: These relate to the old version of the module and older versions of LilyPond! Nowadays, LilyPond and epstopdf get called via texmfstart, that works without patching!''' a4 b c d}
lower === LilyPond is not found ===\relative c { \clef bass \key c \major \time 2/4
Calling lilypond from command line in Windows fails. (Seems like a bug in lilypond.) a2 c}
You have two options:\score {* modify the module, so that lilypond is called with the whole path ( <code>C:/prog/lilypond/usr/bin/lilypond</code> for example instead of \new Voice = "mel" { \autoBeamOff \melody } \new Lyrics \lyricsto mel \text \new PianoStaff <<code \new Staff = "upper" \upper \new Staff = "lower" \lower >lilypond</code> alone).* place a <code >lilypond.bat</code> somewhere in your search path, calling c:/prog/lilypond/usr/bin/lilypond %1 %2 %3 %4 %5 %6 %7 %8 %9 \layout { \context { \Staff \RemoveEmptyStaves } }}
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).\stoplilypond\input tufte
LilyPond 2.7.26 comes without a command line "version", at least in the MacOS X edition, i.e. the <tt>lilypond.sh\stoptext</tttexcode> script is missing.Here's it's relevant content:
#!/bin/sh ## where was the app installed? INSTALLDIR=/Applications if [ "$1" =Too many open files = "--print-appdir" ]; then echo "$INSTALLDIR/LilyPond.app/" exit 0 fi # where to put the output export LILYPOND_DESTDIR=`pwd` # run the program python "$INSTALLDIR/LilyPond.app/Contents/Resources/lilycall.py" \ "$INSTALLDIR/LilyPond.app/" $@
Save this as <tt>lilypond.sh</tt>, make it executable With bigger projects and symlink it the above setup you might run into a directory on your path"too many open files" errors, at least in older Linux, e.gOSX and other Unix-like OSes. chmod a+x lilypond.sh sudo ln -s lilypond.sh /usr/local/bin/lilypondAsk your shell about the open files maximum and set it higher:
=== LilyPond stops with an 'Unbound variable' error === $ ulimit -n 256 $ ulimit -S -n 2048
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:Music]]
[[Category:Requests]]

Navigation menu