]> git.donarmstrong.com Git - lilypond.git/blob - Documentation/snippets/defining-an-engraver-in-scheme-ambitus-engraver.ly
Merge branch 'lilypond/translation' into staging
[lilypond.git] / Documentation / snippets / defining-an-engraver-in-scheme-ambitus-engraver.ly
1 % DO NOT EDIT this file manually; it is automatically
2 % generated from Documentation/snippets/new
3 % Make any changes in Documentation/snippets/new/
4 % and then run scripts/auxiliar/makelsr.py
5 %
6 % This file is in the public domain.
7 %% Note: this file works from version 2.15.31
8 \version "2.15.31"
9
10 \header {
11 %% Translation of GIT committish: 6977ddc9a3b63ea810eaecb864269c7d847ccf98
12
13   texidoces = "
14 Este ejemplo demuestra cómo se puede definir el grabador de ámbito en
15 el espacio del usuario, con un grabador de Scheme.
16
17 Esto es básicamente una reescritura en Scheme del código de
18 @file{lily/ambitus-engraver.cc}.
19
20 "
21
22   doctitlees = "Definir un grabador en Scheme: grabador de ámbito"
23
24
25   lsrtags = "contexts-and-engravers"
26
27
28   texidoc = "This example demonstrates how the ambitus engraver may be
29   defined on the user side, with a Scheme engraver.
30
31   This is basically a rewrite in Scheme of the code from
32   @file{lily/ambitus-engraver.cc}.
33 "
34
35   doctitle = "Defining an engraver in Scheme: ambitus engraver"
36 } % begin verbatim
37
38
39 #(use-modules (oop goops))
40
41 %%%
42 %%% Grob utilities
43 %%%
44 %%% These are literal rewrites of some C++ methods used by the ambitus engraver.
45
46 #(define (ly:separation-item::add-conditional-item grob grob-item)
47    "Add @var{grob-item} to the array of conditional elements of @var{grob}.
48 Rewrite of @code{Separation_item::add_conditional_item} from @file{lily/separation-item.cc}."
49    (ly:pointer-group-interface::add-grob grob 'conditional-elements grob-item))
50
51 #(define (ly:accidental-placement::accidental-pitch accidental-grob)
52    "Get the pitch from the grob cause of @var{accidental-grob}.
53 Rewrite of @code{accidental_pitch} from @file{lily/accidental-placement.cc}."
54    (ly:event-property (ly:grob-property (ly:grob-parent accidental-grob Y) 'cause)
55                       'pitch))
56
57 #(define (ly:accidental-placement::add-accidental grob accidental-grob)
58    "Add @var{accidental-grob}, an @code{Accidental} grob, to the
59 list of the accidental grobs of @var{grob}, an @code{AccidentalPlacement}
60 grob.
61 Rewrite of @code{Accidental_placement::add_accidental} from @file{lily/accidental-placement.cc}."
62    (let ((pitch (ly:accidental-placement::accidental-pitch accidental-grob)))
63      (set! (ly:grob-parent accidental-grob X) grob)
64      (set! (ly:grob-property accidental-grob 'X-offset)
65            ly:grob::x-parent-positioning)
66      (let* ((accidentals (ly:grob-object grob 'accidental-grobs))
67             (handle (assq (ly:pitch-notename pitch) accidentals))
68             (entry (if handle (cdr handle) '())))
69        (set! (ly:grob-object grob 'accidental-grobs)
70              (assq-set! accidentals
71                         (ly:pitch-notename pitch)
72                         (cons accidental-grob entry))))))
73
74 %%%
75 %%% Ambitus data structure
76 %%%
77
78 %%% The <ambitus> class holds the various grobs that are created
79 %%% to print an ambitus:
80 %%% - ambitus-group: the grob that groups all the components of an ambitus
81 %%% (Ambitus grob);
82 %%% - ambitus-line: the vertical line between the upper and lower ambitus
83 %%% notes (AmbitusLine grob);
84 %%% - ambitus-up-note and ambitus-down-note: the note head and accidental
85 %%% for the lower and upper note of the ambitus (see <ambitus-note> class
86 %%% below).
87 %%% The other slots define the key and clef context of the engraver:
88 %%% - start-c0: position of middle c at the beginning of the piece.  It
89 %%% is used to place the ambitus notes according to their pitch;
90 %%% - start-key-sig: the key signature at the beginning of the piece.  It
91 %%% is used to determine if accidentals shall be printed next to ambitus
92 %%% notes.
93
94 #(define-class <ambitus> ()
95    (ambitus-group #:accessor ambitus-group)
96    (ambitus-line #:accessor ambitus-line)
97    (ambitus-up-note #:getter ambitus-up-note
98                     #:init-form (make <ambitus-note>))
99    (ambitus-down-note #:getter ambitus-down-note
100                       #:init-form (make <ambitus-note>))
101    (start-c0 #:accessor ambitus-start-c0
102              #:init-value #f)
103    (start-key-sig #:accessor ambitus-start-key-sig
104                   #:init-value '()))
105
106 %%% Accessor for the lower and upper note data of an ambitus
107 #(define-method (ambitus-note (ambitus <ambitus>) direction)
108    "If @var{direction} is @code{UP}, then return the upper ambitus note
109 of @var{ambitus}, otherwise return the lower ambitus note."
110    (if (= direction UP)
111        (ambitus-up-note ambitus)
112        (ambitus-down-note ambitus)))
113
114 %%% The <ambitus-note> class holds the grobs that are specific to ambitus
115 %%% (lower and upper) notes:
116 %%% - head: an AmbitusNoteHead grob;
117 %%% - accidental: an AmbitusAccidental grob, to be possibly printed next
118 %%% to the ambitus note head.
119 %%% Moreover:
120 %%% - pitch is the absolute pitch of the note
121 %%% - cause is the note event that causes this ambitus note, i.e. the lower
122 %%% or upper note of the considered music sequence.
123
124 #(define-class <ambitus-note> ()
125    (head #:accessor ambitus-note-head
126          #:init-value #f)
127    (accidental #:accessor ambitus-note-accidental
128                #:init-value #f)
129    (cause #:accessor ambitus-note-cause
130           #:init-value #f)
131    (pitch #:accessor ambitus-note-pitch
132           #:init-value #f))
133
134 %%%
135 %%% Ambitus engraving logics
136 %%%
137 %%% Rewrite of the code from @file{lily/ambitus-engraver.cc}.
138
139 #(define (make-ambitus translator)
140    "Build an ambitus object: initialize all the grobs and their relations.
141
142 The Ambitus grob contain all other grobs:
143  Ambitus
144   |- AmbitusLine
145   |- AmbitusNoteHead   for upper note
146   |- AmbitusAccidental for upper note
147   |- AmbitusNoteHead   for lower note
148   |- AmbitusAccidental for lower note
149
150 The parent of an accidental is the corresponding note head,
151 and the accidental is set as the 'accidental-grob of the note head
152 so that is printed by the function that prints notes."
153    ;; make the ambitus object
154    (let ((ambitus (make <ambitus>)))
155      ;; build the Ambitus grob, which will contain all other grobs
156      (set! (ambitus-group ambitus) (ly:engraver-make-grob translator 'Ambitus '()))
157      ;; build the AmbitusLine grob (line between lower and upper note)
158      (set! (ambitus-line ambitus) (ly:engraver-make-grob translator 'AmbitusLine '()))
159      ;; build the upper and lower AmbitusNoteHead and AmbitusAccidental
160      (for-each (lambda (direction)
161                  (let ((head (ly:engraver-make-grob translator 'AmbitusNoteHead '()))
162                        (accidental (ly:engraver-make-grob translator 'AmbitusAccidental '()))
163                        (group (ambitus-group ambitus)))
164                    ;; The parent of the AmbitusAccidental grob is the
165                    ;; AmbitusNoteHead grob
166                    (set! (ly:grob-parent accidental Y) head)
167                    ;; The AmbitusAccidental grob is set as the accidental-grob
168                    ;; object of the AmbitusNoteHead.  This is later used by the
169                    ;; function that prints notes.
170                    (set! (ly:grob-object head 'accidental-grob) accidental)
171                    ;; both the note head and the accidental grobs are added
172                    ;; to the main ambitus grob.
173                    (ly:axis-group-interface::add-element group head)
174                    (ly:axis-group-interface::add-element group accidental)
175                    ;; the note head and the accidental grobs are added to the
176                    ;; ambitus object
177                    (set! (ambitus-note-head (ambitus-note ambitus direction))
178                          head)
179                    (set! (ambitus-note-accidental (ambitus-note ambitus direction))
180                          accidental)))
181                (list DOWN UP))
182      ;; The parent of the ambitus line is the lower ambitus note head
183      (set! (ly:grob-parent (ambitus-line ambitus) X)
184            (ambitus-note-head (ambitus-note ambitus DOWN)))
185      ;; the ambitus line is added to the ambitus main grob
186      (ly:axis-group-interface::add-element (ambitus-group ambitus) (ambitus-line ambitus))
187      ambitus))
188
189 #(define-method (initialize-ambitus-state (ambitus <ambitus>) translator)
190    "Initialize the state of @var{ambitus}, by getting the starting
191 position of middle C and key signature from @var{translator}'s context."
192    (if (not (ambitus-start-c0 ambitus))
193        (begin
194          (set! (ambitus-start-c0 ambitus)
195                (ly:context-property (ly:translator-context translator)
196                                     'middleCPosition
197                                     0))
198          (set! (ambitus-start-key-sig ambitus)
199                (ly:context-property (ly:translator-context translator)
200                                     'keySignature)))))
201
202 #(define-method (update-ambitus-notes (ambitus <ambitus>) note-grob)
203    "Update the upper and lower ambitus pithes of @var{ambitus}, using
204 @var{note-grob}."
205    ;; Get the event that caused the note-grob creation
206    ;; and check that it is a note-event.
207    (let ((note-event (ly:grob-property note-grob 'cause)))
208      (if (ly:in-event-class? note-event 'note-event)
209          ;; get the pitch from the note event
210          (let ((pitch (ly:event-property note-event 'pitch)))
211            ;; if this pitch is lower than the current ambitus lower
212            ;; note pitch (or it has not been initialized yet),
213            ;; then this pitch is the new ambitus lower pitch,
214            ;; and conversely for upper pitch.
215            (for-each (lambda (direction pitch-compare)
216                        (if (or (not (ambitus-note-pitch (ambitus-note ambitus direction)))
217                                (pitch-compare pitch
218                                               (ambitus-note-pitch (ambitus-note ambitus direction))))
219                            (begin
220                              (set! (ambitus-note-pitch (ambitus-note ambitus direction))
221                                    pitch)
222                              (set! (ambitus-note-cause (ambitus-note ambitus direction))
223                                    note-event))))
224                      (list DOWN UP)
225                      (list ly:pitch<? (lambda (p1 p2)
226                                         (ly:pitch<? p2 p1))))))))
227
228 #(define-method (typeset-ambitus (ambitus <ambitus>) translator)
229    "Typeset the ambitus:
230 - place the lower and upper ambitus notes according to their pitch and
231   the position of the middle C;
232 - typeset or delete the note accidentals, according to the key signature.
233   An accidental, if it is to be printed, is added to an AccidentalPlacement
234   grob (a grob dedicated to the placement of accidentals near a chord);
235 - both note heads are added to the ambitus line grob, so that a line should
236   be printed between them."
237    ;; check if there are lower and upper pitches
238    (if (and (ambitus-note-pitch (ambitus-note ambitus UP))
239             (ambitus-note-pitch (ambitus-note ambitus DOWN)))
240        ;; make an AccidentalPlacement grob, for placement of note accidentals
241        (let ((accidental-placement (ly:engraver-make-grob
242                                     translator
243                                     'AccidentalPlacement
244                                     (ambitus-note-accidental (ambitus-note ambitus DOWN)))))
245          ;; For lower and upper ambitus notes:
246          (for-each (lambda (direction)
247                      (let ((pitch (ambitus-note-pitch (ambitus-note ambitus direction))))
248                        ;; set the cause and the staff position of the ambitus note
249                        ;; according to the associated pitch
250                        (set! (ly:grob-property (ambitus-note-head (ambitus-note ambitus direction))
251                                                'cause)
252                              (ambitus-note-cause (ambitus-note ambitus direction)))
253                        (set! (ly:grob-property (ambitus-note-head (ambitus-note ambitus direction))
254                                                'staff-position)
255                              (+ (ambitus-start-c0 ambitus)
256                                 (ly:pitch-steps pitch)))
257                        ;; determine if an accidental shall be printed for this note,
258                        ;; according to the key signature
259                        (let* ((handle (or (assoc (cons (ly:pitch-octave pitch)
260                                                        (ly:pitch-notename pitch))
261                                                  (ambitus-start-key-sig ambitus))
262                                           (assoc (ly:pitch-notename pitch)
263                                                  (ambitus-start-key-sig ambitus))))
264                               (sig-alter (if handle (cdr handle) 0)))
265                          (cond ((= (ly:pitch-alteration pitch) sig-alter)
266                                 ;; the note alteration is in the key signature
267                                 ;; => it does not have to be printed
268                                 (ly:grob-suicide!
269                                  (ambitus-note-accidental (ambitus-note ambitus direction)))
270                                 (set! (ly:grob-object (ambitus-note-head (ambitus-note ambitus direction))
271                                                       'accidental-grob)
272                                       '()))
273                                (else
274                                 ;; otherwise, the accidental shall be printed
275                                 (set! (ly:grob-property (ambitus-note-accidental
276                                                          (ambitus-note ambitus direction))
277                                                         'alteration)
278                                       (ly:pitch-alteration pitch)))))
279                        ;; add the AccidentalPlacement grob to the
280                        ;; conditional items of the AmbitusNoteHead
281                        (ly:separation-item::add-conditional-item
282                         (ambitus-note-head (ambitus-note ambitus direction))
283                         accidental-placement)
284                        ;; add the AmbitusAccidental to the list of the
285                        ;; AccidentalPlacement grob accidentals
286                        (ly:accidental-placement::add-accidental
287                         accidental-placement
288                         (ambitus-note-accidental (ambitus-note ambitus direction)))
289                        ;; add the AmbitusNoteHead grob to the AmbitusLine grob
290                        (ly:pointer-group-interface::add-grob
291                         (ambitus-line ambitus)
292                         'note-heads
293                         (ambitus-note-head (ambitus-note ambitus direction)))))
294                    (list DOWN UP))
295          ;; add the AccidentalPlacement grob to the main Ambitus grob
296          (ly:axis-group-interface::add-element (ambitus-group ambitus) accidental-placement))
297        ;; no notes ==> suicide the grobs
298        (begin
299          (for-each (lambda (direction)
300                      (ly:grob-suicide! (ambitus-note-accidental (ambitus-note ambitus direction)))
301                      (ly:grob-suicide! (ambitus-note-head (ambitus-note ambitus direction))))
302                    (list DOWN UP))
303          (ly:grob-suicide! ambitus-line))))
304
305 %%%
306 %%% Ambitus engraver definition
307 %%%
308 #(define ambitus-engraver
309    (lambda (context)
310      (let ((ambitus #f))
311        ;; when music is processed: make the ambitus object, if not already built
312        (make-engraver
313         ((process-music translator)
314          (if (not ambitus)
315              (set! ambitus (make-ambitus translator))))
316         ;; set the ambitus clef and key signature state
317         ((stop-translation-timestep translator)
318          (if ambitus
319              (initialize-ambitus-state ambitus translator)))
320         ;; when a note-head grob is built, update the ambitus notes
321         (acknowledgers
322           ((note-head-interface engraver grob source-engraver)
323            (if ambitus
324                (update-ambitus-notes ambitus grob))))
325         ;; finally, typeset the ambitus according to its upper and lower notes
326         ;; (if any).
327         ((finalize translator)
328          (if ambitus
329              (typeset-ambitus ambitus translator)))))))
330
331 %%%
332 %%% Example
333 %%%
334
335 \score {
336   \new StaffGroup <<
337     \new Staff { c'4 des' e' fis' gis' }
338     \new Staff { \clef "bass" c4 des ~ des ees b, }
339   >>
340   \layout { \context { \Staff \consists #ambitus-engraver } }
341 }