]> git.donarmstrong.com Git - lilypond.git/blob - Documentation/snippets/generating-whole-scores-also-book-parts-in-scheme-without-using-the-parser.ly
cd6139fcfa8a0cc8cc009514a25a355b1128c1c9
[lilypond.git] / Documentation / snippets / generating-whole-scores-also-book-parts-in-scheme-without-using-the-parser.ly
1 %% DO NOT EDIT this file manually; it is automatically
2 %% generated from LSR http://lsr.dsi.unimi.it
3 %% Make any changes in LSR itself, or in Documentation/snippets/new/ ,
4 %% and then run scripts/auxiliar/makelsr.py
5 %%
6 %% This file is in the public domain.
7 \version "2.14.2"
8
9 \header {
10   lsrtags = "automatic-notation, really-cool, scheme-language"
11
12   texidoc = "
13 A lilypond score internally is just a Scheme expression, generated by
14 the lilypond parser. Using scheme, one can also automatically generate
15 a score without an input file. If you have the music expression in
16 scheme, a score can be generated by simply calling (scorify-music music
17 parser) on your music. This will generate a score object, for which you
18 can then set a custom layout block with (let* ((layout
19 (ly:output-def-clone $defaultlayout)))
20    ; modify the layout here, then assign it:
21    (ly:score-add-output-def! score layout)
22   )
23
24
25 Finally, all you have to do it to pass this score to lilypond for
26 typesetting. This snippet defines functions @code{(add-score parser
27 score)}, @code{(add-text parser text)} and @code{(add-music parser
28 music)} to pass a complete score, some markup or some music to lilypond
29 for typesetting.
30
31 This snippet also works for typesetting scores inside a @code{\\book
32 @{...@}} block, as well as top-level scores. To achieve this, each
33 score schedulled for typesetting is appended to the list of toplevel
34 scores and the toplevel-book-handler (which is a scheme function called
35 to process a book once a @code{\\book@{..@}} block is closed) is
36 modified to inser all collected scores so far to the book.
37
38 "
39   doctitle = "Generating whole scores (also book parts) in scheme without using the parser"
40 } % begin verbatim
41
42 #(define-public (add-score parser score)
43    (ly:parser-define! parser 'toplevel-scores
44                       (cons score (ly:parser-lookup parser 'toplevel-scores))))
45
46 #(define-public (add-text parser text)
47   (add-score parser (list text)))
48
49 #(define-public (add-music parser music)
50   (collect-music-aux (lambda (score)
51                        (add-score parser score))
52                      parser
53                      music))
54
55 #(define-public (toplevel-book-handler parser book)
56    (map (lambda (score)
57           (ly:book-add-score! book score))
58         (reverse! (ly:parser-lookup parser 'toplevel-scores)))
59    (ly:parser-define! parser 'toplevel-scores (list))
60    (print-book-with-defaults parser book))
61
62 #(define-public (book-score-handler book score)
63    (add-score parser score))
64
65 #(define-public (book-text-handler book text)
66    (add-text parser text))
67
68 #(define-public (book-music-handler parser book music)
69    (add-music parser music))
70
71 %%%
72
73
74 %% Just some example score to show how to use these functions:
75 #(define add-one-note-score #f)
76 #(let ((pitch 0))
77   (set! add-one-note-score
78         (lambda (parser)
79           (let* ((music (make-music 'EventChord
80                           'elements (list (make-music 'NoteEvent
81                                             'duration (ly:make-duration 2 0 1 1)
82                                             'pitch (ly:make-pitch 0 pitch 0)))))
83                  (score (scorify-music music parser))
84                  (layout (ly:output-def-clone $defaultlayout))
85                  (note-name (case pitch
86                               ((0) "do")
87                               ((1) "rĂ©")
88                               ((2) "mi")
89                               ((3) "fa")
90                               ((4) "sol")
91                               ((5) "la")
92                               ((6) "si")
93                               (else "huh")))
94                  (title (markup #:large #:line ("Score with a" note-name))))
95             (ly:score-add-output-def! score layout)
96             (add-text parser title)
97             (add-score parser score))
98             (set! pitch (modulo (1+ pitch) 7)))))
99
100 oneNoteScore =
101 #(define-music-function (parser location) ()
102    (add-one-note-score parser)
103    (make-music 'Music 'void #t))
104
105 %%%
106
107 \book {
108   \oneNoteScore
109 }
110
111
112 \book {
113   \oneNoteScore
114   \oneNoteScore
115 }
116
117 % Top-level scores are also handled correctly
118 \oneNoteScore
119 \oneNoteScore