]> git.donarmstrong.com Git - lilypond.git/blob - Documentation/tex/lilypond-overview.doc
4aea7128387689dab0eaf75e0024398dcee113be
[lilypond.git] / Documentation / tex / lilypond-overview.doc
1 %-*-LaTeX-*-
2
3 \documentclass{article}
4 \usepackage{a4}
5 \def\postMudelaExample{\setlength{\parindent}{1em}}
6 \title{LilyPond, a Music Typesetter}
7 \author{HWN}
8 \usepackage{musicnotes}
9 \usepackage{graphics}
10
11
12 \begin{document}
13 \maketitle
14
15 [THIS IS WORK IN PROGRESS.  THIS PAPER IS NOT FINISHED]
16
17 % -*-LaTeX-*-
18 \section{Introduction}
19
20 The Internet has become a popular medium for collaborative work on
21 information.  Its success is partly due to its use of simple, text-based
22 formats.  Examples of these formats are HTML and \LaTeX.  Anyone can
23 produce or modify such files using nothing but a text editor, they are
24 easily processed with run-of-the-mill text tools, and they can be
25 integrated into other text-based formats.
26
27 Software for processing this information and presenting these formats
28 in an elegant form is available freely (Netscape, \LaTeX, etc.).
29 Ubiquitousness of the software and simplicity of the formats have
30 revolutionised the way people publish text-based information
31 nowadays.
32
33 In the field of performed music, where the presentation takes the form
34 of sheet music, such a revolution has not started yet.  Let us review
35 some alternatives that have been available for transmitting sheet
36 music until now:
37 \begin{itemize}
38 \item MIDI\cite{midi}.  This format was designed for interchanging performances
39   of music; one should think of it as a glorified tape recorder
40   format.  It needs dedicated editors, since it is binary.  It does
41   not provide enough information for producing musical scores: some of
42   the abstract musical content of what is performed is thrown away.
43   
44 \item PostScript\cite{Postscript}. This format is a printer control
45   language.  Printed musical scores can be transmitted in PostScript,
46   but once a score is converted to PostScript, it is virtually
47   impossible to modify the score in a meaningful way.
48   
49 \item Formats for various notation programs.  Notation programs either
50   work with binary  formats (e.g., NIFF\cite{niff-web}), need specific
51   platforms   (e.g.,  Sibelius\cite{sibelius}),  are   proprietary  or
52   non-portable  tools  themselves  (idem), produce  inadequate  output
53   (e.g.,  MUP\cite{mup}),  are   based  on  graphical  content  (e.g.,
54   MusixTeX\cite{musixtex1}),  limit themselves to  specific subdomains
55   (e.g.,  ABC\cite{abc2mtex}),  or   require  considerable  skill  and
56   knowledge to use (e.g., SCORE\cite{score})
57   
58 \item SMDL\cite{smdl-web}.  This is a very rich ASCII format, that is
59   designed for storing many types of music.  Unfortunately, there is
60   no implementation of a program to print music from SMDL available.
61   Moreover, SMDL is so verbose, that it is not suitable for human
62   production.
63   
64 \item TAB\cite{tablature-web}.  Tab (short for tablature) is a popular
65   format, for interchanging music over e-mail, but it can only be used
66   for guitar music.
67 \end{itemize}
68
69 In summary, sheet music is not published and edited on a wide scale
70 across the internet  because no format for music
71 interchange exists that is:
72 \begin{itemize}
73 \item open, i.e., with publically available specifications.
74 \item based on ASCII, and therefore suitable for human consumption and
75   production.
76 \item rich enough for producing publication quality sheet music from
77   it.
78 \item based on musical content (unlike, for example, PostScript), and
79   therefore suitable for making modifications.
80 \item accompanied by tools for processing it that are freely available
81   across multiple platforms.
82 \end{itemize}
83
84
85 With the creation of LilyPond, we have tried to create both a
86 convenient format for storing sheet music, and a portable,
87 high-quality implementation of a compiler, that compiles the input
88 into a printable score.  You can find a small example of LilyPond
89 input along with output in Figure~\ref{fig:intro-fig}.
90 %
91 \begin{figure}[htbp]
92   \begin{center}
93 \begin{mudela}[verbatim]
94       \score {
95         \notes
96           \context GrandStaff <
97              \transpose c'' { c4 c4 g4 g4 a4 a4 g2 }
98              { \clef "bass"; c4 c'4
99                \context Staff <e'2 {\stemdown c'4 c'4}> f'4 c'4 e'4 c'4 }
100            >
101            \paper { 
102              linewidth = -1.0\cm ;
103            }
104         }      
105 \end{mudela}
106     \caption{A small example of LilyPond input}
107     \label{fig:intro-fig}
108   \end{center}
109 \end{figure}
110 %
111
112  
113 The input language encodes musical events (such as notes and rests) on
114 the basis of their time-ordering.  For example, the grammar includes
115 constructs that specify that notes start simultaneous and that notes
116 are to be played in sequence.  In this encoding some context that is
117 present in sheet music is lost.
118
119 The compiler reconstructs the notation from the encoded music.  Its
120 operation comprises four different steps (see
121 Figure~\ref{fig:intro-steps}).
122
123 \begin{description}
124 \item[Parsing] During parsing, the input is converted in a syntax tree.
125   
126 \item[Interpreting] In the \emph{interpreting} step, it is determined
127   which symbols have to be printed.  Objects that correspond to
128   notation (\emph{Graphical objects}) are created from the syntax tree
129   in this phase. Generally speaking, for every symbol printed there is
130   one graphical object.  These objects are incomplete: their position
131   and their final shape is unknown.
132   
133   The context that was lost by encoding the input in a language is
134   reconstructed during this conversion.
135 \item[Formatting] The next step is determing where symbols are to be
136   placed, this is called \emph{formatting}.
137 \item[Outputting]   
138   Finally, all Graphical objects are outputted as PostScript or \TeX\ code.
139 \end{description}
140
141 \def\staffsym{\vbox to 16pt{
142     \hbox{\vrule width 1cm depth .2pt height .2pt}\nointerlineskip
143     \vfil
144     \hbox{\vrule width 1cm depth .2pt height .2pt}\nointerlineskip
145     \vfil
146     \hbox{\vrule width 1cm depth .2pt height .2pt}\nointerlineskip
147     \vfil
148     \hbox{\vrule width 1cm depth .2pt height .2pt}\nointerlineskip
149     \vfil
150     \hbox{\vrule width 1cm depth .2pt height .2pt}\nointerlineskip
151 }}
152
153 \def\vspacer{\vbox to 20pt{\vss}}
154 \begin{figure}[h]
155 \def\spacedhbox#1{\hbox{\ #1\ }}
156 \begin{eqnarray*}
157   {\spacedhbox{Input}\atop \hbox{\texttt{\{c8 c8\}}}} {\spacedhbox{Parsing}\atop\longrightarrow}
158   {\spacedhbox{Syntax tree}\atop\spacedhbox{\textsf{Sequential(Note,Note)}}}
159   {\spacedhbox{Interpreting}\atop\longrightarrow}\\
160   \vspacer\\
161   {\spacedhbox{Graphic objects}\atop\spacedhbox{\texttrebleclef \textquarterhead\texteighthflag\textquarterhead\texteighthflag \staffsym }}
162   {\spacedhbox{Formatting}\atop\longrightarrow}
163   {\spacedhbox{Formatted objects}\atop\hbox{
164     \mudela{c''8 c''8}
165     }}\\
166 \vspacer\\
167   {\spacedhbox{Outputting}\atop\longrightarrow}
168   {\spacedhbox{PostScript code}\atop\hbox{\texttt{\%!PS-Adobe}\ldots}}
169 \end{eqnarray*}
170   \caption{Parsing, Interpreting, Formatting and Outputting}
171     \label{fig:intro-steps}
172 \end{figure}
173
174
175 The second step, the interpretation phase of the compiler, can be
176 manipulated as a separate entity: the interpretation process is
177 composed of many separate modules, and the behaviour of the modules is
178 parameterised.  By recombining these interpretation modules,
179 and changing parameter settings, the same piece of music can be
180 printed differently, as is shown in Figure~\ref{fig:intro-interpret}.
181
182 This makes it easy to extend the program. Moreover, this enables the
183 same music to be printed in different versions, e.g., in a conductors
184 score and in extracted parts.
185
186
187 \begin{figure}[h]
188   \begin{center}
189     \begin{mudela}
190       \score {
191         \notes
192           \context GrandStaff <
193              \transpose c'' { c4 c4 g4 g4 a4 a4 g2 }
194              { \clef "bass"; c4 c'4
195                \context Staff <e'2 {\stemdown c'4 c'4}> f'4 c'4 e'4 c'4 }
196            >
197            \paper { 
198              linewidth = -1.0\cm ;
199              \translator {  
200                 \VoiceContext
201                 \remove "Stem_engraver";
202              }
203            \translator {
204              \StaffContext
205                numberOfStaffLines = 3;
206            }
207           }
208         }
209     \end{mudela}
210     \caption{The interpretation phase can be manipulated: the same
211       music as in Figure~\ref{fig:intro-fig} is interpreted
212       differently: three staff lines and no stems.}
213     \label{fig:intro-interpret}
214   \end{center}
215 \end{figure}
216
217
218
219 \section{Preliminaries}
220
221 To understand the rest of the article, it is necessary to know
222 something about music notation and music typography.  Since both
223 communicate music, we will explain some characteristics of instruments
224 and western music that motivate some notational constructs.
225
226 \subsection{Music}
227
228 Music notation is meant to be read by human performers.  They sing or
229 play instruments that can produce sounds of different pitches.  These
230 sounds are called \emph{notes}. Additionally, the sounds can be
231 articulated in differents ways, e.g., staccato (short and separated)
232 or legato (fluently bound together).  The loudness of the notes can
233 also be varied.  Changes in loudness are called \emph{dynamics}.
234
235 Silence is also an element of music.  The musical terminology for
236 silence within music is \emph{rest}.
237
238 The basic unit of pitch is the \emph{octave}.  The octave corresponds
239 to a frequency ratio of 1:2. For example the pitch denoted by a'
240 (frequency: 440 hertz) is one octave lower than a'' (frequency: 880
241 hertz).  Various instruments have a limited \emph{pitch range}, for
242 example, a trumpet has a range of about 2.5 octaves.  Not all
243 instruments have ranges in the same register: a tuba also has a range
244 of 2.5 octaves, but the range of the tuba is much lower.
245
246 Musicology has a confusing mix of relative and absolute measures for
247 pitches: the term `octave' refers to both a difference between two
248 pitches (the frequency ratio of 1:2), and to a range of pitches.  For
249 example, the term `[eengestreept] octave' refers to the pitch range
250 between 261.6 Hz and 523.3 Hz.
251
252
253 The octave is divided into smaller pitch steps.  In modern western
254 music, every octave is divided into twelve approximately equidistant
255 pitch steps, and each step is called a \emph{semitone}.  Usually, the
256 pitches in a musical piece come from a smaller subset of these twelve
257 possible pitches.  This smaller subset along with the musical
258 functions fo the pitches is called the
259 \emph{tonality}\footnote{Tonality also refers to the relations between
260   and functions of certain pitches.  Since these do not have any
261   impact on notation, we ignore this} of the piece.
262
263
264 The standard tonality that forms the basis of music notation 
265 (the key of C major) is a set of seven pitches within every octave.
266 Each of these seven is denoted by a name. In English, these are names
267 are (in rising pitch) denoted by c, d, e, f, g, a and b.  Pitches that
268 are a semitone higher or lower than one of these seven can be
269 represented by suffixing the name with `sharp' or `flat'
270 respectively (this is called an \emph{chromatic alteration}).
271
272 A pitch therefore can be fully specified by a combination of the
273 octave number, the note name and a chromatic alteration.
274 Figure~\ref{fig:intro-pitches} shows the relation between names and
275 frequencies.
276
277
278
279
280 \begin{figure}[h]
281   \begin{center}
282     [te doen]
283   \end{center}
284   \caption{Pitches in western music.  The octave number is denoted
285     by a superscript.}
286   \label{fig:intro-pitches}
287 \end{figure}
288
289
290 Many instruments can produce more than one note at the same time, e.g.
291 pianos and guitars.  When more notes are played simultaneously, they
292 form a so-called \emph{chord}.
293
294 The unit of duration is the \emph{beat}. When playing, the tempo is
295 determined by setting the number of beats per minute.  In western
296 music, beats are often stressed in a regular pattern: for example
297 Waltzes have a stress pattern that is strong-weak-weak, i.e. every
298 note that starts on a `strong' beat is louder and has more pronounced
299 articulation.  This stress pattern is called \emph{meter}.
300
301 \subsection{Music notation}
302
303 Music notation is a system that tries to represent musical ideas
304 through printed symbols.  Music notation has no precise definition,
305 but most conventions have described in reference manuals on music
306 notation\cite{read-notation}.
307
308 In music notation, sounds and silences are represented by symbols that
309 are called note and rest respectively.\footnote{These names serve a
310   double purpose: the same terms are used to denote the musical
311   concepts.}  The shape of notes and rests indicates their duration
312 (See figure~\ref{noteshapes}) relative to the whole note.
313
314 \begin{figure}[h]
315   \begin{center}
316 \begin{mudela}
317   \score {
318     \notes \transpose c''{ c\longa*1/4 c\breve*1/2 c1 c2 c4 c8 c16 c32 c64 }
319     \paper {
320      \translator {
321        \StaffContext
322        \remove "Staff_symbol_engraver";
323         \remove "Time_signature_engraver";
324         \remove "Bar_engraver";
325         \remove "Clef_engraver";
326  }
327 linewidth = -1.;
328     }
329 }
330 \end{mudela}
331 \begin{mudela}
332   \score {
333     \notes \transpose c''\context Staff { r\longa*1/4 r\breve*1/2 r1 r2 r4 r8 r16 r32 r64 }
334     \paper {
335       \translator {
336         \StaffContext
337         \remove "Staff_symbol_engraver";
338         \remove "Time_signature_engraver";
339         \remove "Bar_engraver";
340         \remove "Clef_engraver";
341         }
342       linewidth = -1.;
343     }
344 }
345 \end{mudela}
346     \caption{Note and rest shapes encode the length.  At the top notes
347       are shown, at the bottom rests.  From left to right a quadruple
348       note (\emph{longa}), double (\emph{breve}), whole, half,
349       quarter, eigth, sixteenth, thirtysecond and sixtyfourth. Each
350       note has half of the duration of its predecessor.}
351     \label{fig:noteshapes}
352 \end{center}
353 \end{figure}
354
355 Notes are printed in a grid of horizontal lines called \emph{staff} to
356 denote their pitch: each line represents the pitch of from the
357 standard scale (c, d, e, f, g, a, b).  The reference point is the
358 \emph{clef}, eg., the treble clef marks the location of the $g^1$
359 pitch.  The notes are printed in their time order, from left to right.
360
361
362 \begin{figure}[h]
363   \begin{center}
364     \begin{mudela}
365       \score { \notes {
366       a4 b c d e f g a \clef bass;
367       a4 b c d e f g a \clef alto;
368       a4 b c d e f g a \clef treble;
369       }
370       \paper { linewidth = 15.\cm; }
371       }
372     \end{mudela}
373     \caption{Pitches ranging from $a, b, c',\ldots a'$, in different
374       clefs.  From left right the bass, alto and treble clef are
375       featured.}
376     \label{fig:pitches}
377   \end{center}
378 \end{figure}
379
380 The chromatic alterations are indicated by printing a flat sign or a
381 sharp sign in front of the note head.  If these chromatic alterations
382 occur systematically (if they are part of the tonality of the piece),
383 then this indicated with a \emph{key signature}.  This is a list of
384 sharp/flat signs which is printed next to the clef.
385
386 Articulation is notated by marking the note shapes wedges, hats and
387 dots all indicate specific articulations.  If the notes are to be
388 bound fluently (legato), the note shapes are encompassed by a smooth
389 curve called \emph{slur},
390
391 \begin{figure}[h]
392   \begin{center}
393     \begin{mudela}
394       c'4-> c'4-. g'4 ( b'4  ) g''4
395     \end{mudela}
396     \caption{Some articulations.  From left to right: extra stress
397       (\emph{marcato}), short (staccato), slurred notes (legato).}
398     \label{fig:articulation}
399   \end{center}
400 \end{figure}
401
402
403
404 Dynamics are notated in two ways: absolute dynamics are indicated by
405 letters: \textbf{f} (from Italian ``forte'') stands for loud,
406 \textbf{p} (from Italian ``piano'') means soft.  Gradual changes in
407 loudness are notated by (de)crescendos.  These are hairpin like shapes
408 below the staff.
409
410 \begin{figure}[h]
411   \begin{center}
412     \begin{mudela}
413       g'4\pp \<  g'4 \! g'4 \ff \> g'4 g' \! g'\ppp
414     \end{mudela}
415     \caption{Dynamics: start very soft (pp), grow to loud (ff) and
416       decrease to extremely soft (ppp)}
417     \label{fig:dynamics}
418   \end{center}
419 \end{figure}
420
421
422 The meter is indicated by barlines: every start of the stress pattern
423 is preceded by a vertical line, the \emph{bar line}.  The space
424 between two bar lines is called measure.  It is therefore the unit of
425 the rhythmic pattern.
426
427 The time signature also indicates what kind of rhythmic pattern is
428 desired.  The time signature takes the form of two numbers stacked
429 vertically. The top number is the number of beats in one measure, the
430 bottom number is the duration (relative to the whole note) of the note
431 that takes  one beat.  Example: 2/4  time signature means ``two beats
432 per measure, and a quarter note takes one beat''
433
434 Chords are written by attaching multiple note heads to one stem.  When
435 the composer wants to emphasize the horizontal relationships between
436 notes, the simultaneous notes can be written as voices (where every
437 note head has its own stem).  A small example is given in
438 Figure~\ref{fig:simultaneous}.
439
440 \begin{figure}[h]
441   \begin{center}
442     \begin{mudela}
443       \relative c'' {\time 2/4;  <c4 e> <d f>
444                 \context Staff < \context Voice = VA{
445                   \stemdown
446                   c4 d
447                   b16 b b b b b b b }
448                 \context Voice = VB {
449                   \stemup e4 f g8 g4 g8 } >
450           }
451     \end{mudela}
452     \caption{Notes sounding together.  Chord notation (left, before
453       the bar line) emphasizes vertical relations, voice notation
454       emphasizes horizontal relations. Separate voices needn't have
455       synchronous rhythms (third measure). 
456       }
457     \label{fig:simultaneous}
458   \end{center}
459 \end{figure}
460
461 Separate voices do not have to share one rhythmic pattern---this is
462 also demonstrated in Figure~\ref{fig:simultaneous}--- they are in a sense%vaag
463 independent.  A different way to express this in notation, is by
464 printing each voice on a different staff.  This is customary when
465 writing for piano (both left and right hand have a staff of their own)
466 and for ensemble (every instrument has a staff of its own).
467
468
469
470 \subsection{Music typography}
471
472 Music typography is the art of placing symbols in esthetically
473 pleasing configuration.  Little is explicitly known about music
474 typography.  There are only a few reference works
475 available\cite{ross,wanske}.  Most of the knowledge of this art has
476 been transmitted verbally, and was subsequently lost.
477
478 The motivation behind choices in typography is to represent the idea
479 as clearly as possible. Among others, this results in the following
480 guidelines:
481 \begin{itemize}
482 \item The printed score should use visual hints to accentuate the
483   musical content
484 \item The printed score should not contain distracting elements, such
485   as large empty regions or blotted regions.
486 \end{itemize}
487
488 An example of the first guideline in action is the horizontal spacing.
489 The amount of space following a note should reflect the duration of
490 that note: short notes get a small amount of space, long notes larger
491 amounts.  Such spacing constraints can be  subtle, for the
492 ``amount of space'' is only the impression that should be conveyed; there
493 has to be some correction for optical illusions.  See
494 Figure~\ref{fig:spacing}.
495
496 \begin{figure}[h]
497   \begin{center}
498     \begin{mudela}
499       \relative c'' { \time 3/4; c16 c c c c8 c8 | f4 f, f'  }
500     \end{mudela}
501     \caption{Spacing conveys information about duration. Sixteenth
502       notes at the left get less space than quarter notes in the
503       middle. Spacing is ``visual'', there should be more space
504       after  the first note of the last measure, and  less after second. }
505     \label{fig:spacing}
506   \end{center}
507 \end{figure}
508
509 Another example of music typography is clearly visible in collisions.
510 When chords or separate voices are printed, the notes that start at
511 the same time should be printed aligned (ie., with the same $x$
512 position).  If the pitches are close to each other, the note heads
513 would collide. To prevent this, some notes (or note heads) have to be
514 shifted horizontally.  An example of this is given in
515 Figure~\ref{fig:collision}.
516 \begin{figure}[h]
517   \begin{center}
518     [todo]
519     \caption{Collisions}
520     \label{fig:collision}
521   \end{center}
522 \end{figure}
523
524 \bibliographystyle{hw-plain}
525 \bibliography{engraving,boeken,colorado,computer-notation,other-packages}
526
527 \section{Requirements}
528
529
530 \section{Approach}
531
532 \subsection{Input}
533
534 The input format consists of combining a symbolic representation of
535 music with style sheet that describes how the symbolic presentation
536 can converted to notation.  The symbolic representation is based on a
537 context free language called \textsf{music}.  Music is a recursively
538 defined construction in the input language.  It can be constructed by
539 combining lists of \textsf{music} sequentially or parallel or from
540 terminals like notes or lyrics.
541
542 The grammar for \textsf{music} is listed below.  It has been edited to
543 leave out the syntactic and ergonomic details.
544
545 \begin{center}
546     \begin{tabular}{ll}
547 Music:  & SimpleMusic\\
548         & $|$ REPEATED int Music ALTERNATIVE MusicList\\
549         & $|$ SIMULTANEOUS MusicList\\
550         & $|$ SEQUENTIAL MusicList\\
551         & $|$ CONTEXT STRING '=' STRING Music\\
552         & $|$ TIMES int int Music     \\
553         & $|$ TRANSPOSE PITCH Music \\
554 SimpleMusic: & $|$ Note\\
555         & $|$ Lyric\\
556         & $|$ Rest\\
557         & $|$ Chord\\
558         & $|$ Command\\
559 Command: & METERCHANGE\\
560         & $|$ CLEFCHANGE\\
561         &$|$ PROPERTY STRING '=' STRING\\
562 Chord: &PitchList DURATION\\
563 Rest: &REST DURATION\\
564 Lyric: &STRING DURATION\\
565 Note: &PITCH DURATION\\
566 \end{tabular}
567 \end{center}
568   
569 The terminals are both purely musical concepts that have a duration,
570 and take a non-zero amount of musical time, like notes and lyrics, and
571 commands that behave as if they have no duration.\footnote{The
572   PROPERTY command is a generic mechanism for controlling the
573   interpretation, i.e. the musical style sheets. See [forward ref]}
574
575 The nonterminal productions can
576 \begin{itemize}
577 \item Some productions combine multiple elements: one can specify that
578   element are to be played in sequence, simultaneously or repetitively.
579 \item There are productions for transposing music, and for dilating
580   durations of music: the TIMES production can be used to encode a
581   triplet.\footnote{A triplet is a group of three notes marked by a
582     bracket, that are played 3/2 times faster.}
583 \item
584   There are productions that give directions to the interpretation
585   engine (the CONTEXT production)
586 \end{itemize}
587
588
589 \section{Context in notation} 
590
591 Music notation relies heavily on context.  Notational symbols do not
592 have meaning if they are not surrounded by other context elements.  In
593 this section we give some examples how the reader uses this context do
594 derive meaning of a piece of notation.   We will focus on the prime
595 example of context: the staff. 
596
597 A staff is the grid of five horizontal lines, but it contains more components :
598 \begin{itemize}
599 \item A staff can have a key signature (printed at the left)
600 \item A staff can have a time signature (printed at the left)
601 \item A staff has bar lines
602 \item A staff has a clef (printed at the left)
603 \end{itemize}
604
605 It is still possible to print notes without these components, but one
606 cannot determine the meaning of the notes.
607 \begin{mudela}
608 \score{
609 \notes \relative c' {  \time 2/4; g'4 c,4 a'4 f4 e c d2 }
610 \paper { 
611   linewidth = -1.;
612   \translator {
613   \StaffContext
614   \remove "Time_signature_engraver";
615   \remove "Bar_engraver";
616   \remove "Staff_symbol_engraver";
617   \remove "Clef_engraver";
618   \remove "Key_engraver";
619   }
620  }
621 }
622 \end{mudela}
623
624 As you can see, you can still make out the general form of the melody
625 and the rhythm that is to be played, but the notation is difficult to
626 read and the musical information is not complete.  The stress
627 pattern in the notes can't be deduced from this output.  For this, we
628 need a time signature.  Adding barlines helps with finding the strong
629 and weak beats.
630 \begin{mudela}
631 \score {
632   \notes \relative c' {  \time 2/4; g'4 c,4 a'4 f4 e c d2 }
633   \paper{
634   linewidth = -1.;
635 \translator{
636   \StaffContext
637   \remove "Staff_symbol_engraver";
638   \remove "Clef_engraver";
639   \remove "Key_engraver";}
640   }
641  }
642 \end{mudela}
643
644 It is impossible to deduce the exact pitch of the notes.  One needs a
645 clef to do so.  Staff lines help the eye in determining the vertical
646 position of a note wrt. to the clef.
647 \begin{mudela}
648 \score {
649   \notes \relative c' {\clef alto;  \time 2/4; g'4 c,4 a'4 f4 e c d2 }
650   \paper {
651     linewidth = -1.;
652   }
653 }  
654 \end{mudela}
655
656 Now you know the pitch of the notes: you look at the start of the line
657 and see a clef, and with this clef, you can determine the notated pitches.
658 You have found the em(context) in which the notation is to be
659 interpreted!
660
661
662 \section{Interpretation context}
663
664 Context (clef, time signature etc.) determines the relationship
665 between musical and its notation in notes.  Because LilyPond writes
666 notation, context works the other way around for LilyPond: with
667 context a piece of music can be converted to notation.
668
669 A reader remembers this context while reading the notation from left
670 to right.  By analogy, LilyPond constructs this context while
671 constructing notes from left to right.  This is what happens in the
672 ``Interpretation'' phase from~\ref{fig:intro-fig}.  In LilyPond, the
673 state of this context is a set of variables with their values; A staff
674 context contains variables like
675
676 \begin{itemize}
677 \item current clef
678 \item current time signature
679 \item current key
680 \end{itemize}
681
682 These variables determine when and how clefs, time signatures, bar
683 lines and accidentals are printed.
684
685
686 Staff is not the only form of context in notation.  In polyphonic
687 music, the stem direction shows which notes form a voice: all notes of
688 the same voice have stems pointing in the same direction.  The value
689 of this variable determines the appearance of the printed stems.
690
691 In LilyPond ``Notation context'' is abstracted to a data structure
692 that is used, constructed and modified during the interpretation
693 phase.  It contains context properties, and is responsible for
694 creating notational elements: the staff context creates symbols for
695 clefs, time signatures and key signatures.  The Voice context creates
696 stems, note heads.
697
698 For the fragment of polyphonic music below,
699 \begin{mudela}
700   \context Staff { c'4 < { \stemup c'4 } \context Voice = VB { \stemdown a4 } > }
701 \end{mudela}
702 A staff context is created.  Within this staff context (which printed
703 the clef), a Voice context is created, which prints the first note.
704 Then, a second Voice context is created, with stem direction set to
705 ``up'', and the direction for the other is set to down. Both Voice
706 contexts  are  still part of the same Staff context.
707
708 In the same way, multiple staff scores are created: within the score
709 context, multiple staff contexts are created.  Every staff context
710 creates the notation associated with a staff.  
711
712 \section{Discussion}
713
714
715
716 \end{document}
717
718 The complexity of  music notation was tackled by adopting a modular
719 design: both the formatting system (which encodes the esthetic rules of
720 notation), and the interpretation system (which encodes the semantic
721 rules) are highly modular.
722
723
724 The difficulty in creating a format for music notation is rooted in
725 the fact that music is multi dimensional: each sound has its own
726 duration, pitch, loudness and articulation. Additionally, multiple
727 sounds may be played simultaneously.  Because of this, there is no
728 obvious way to ``flatten'' music into a context-free language.
729
730 The difficulty in creating a printing engine is rooted in the fact
731 that music notation complicated: it is very large graphical
732 ``language'' with many arbitrary esthetic and semantic conventions.
733 Building a system that formats full fledged musical notation is a
734 challenge in itself, regardless of whether it is part of a compiler or
735 an editor.
736
737 The fact that music and its notation are of a different nature,
738 implies that the conversion between input notation is non-trivial.
739
740 In LilyPond we solved the above problem in the following way:
741