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