5 lsrtags = "contexts-and-engravers"
8 texidoc = "This example demonstrates how the ambitus engraver may be
9 defined on the user side, with a Scheme engraver.
11 This is basically a rewrite in Scheme of the code from
12 @file{lily/ambitus-engraver.cc}.
15 doctitle = "Defining an engraver in Scheme: ambitus engraver"
18 #(use-modules (oop goops))
23 %%% These are literal rewrites of some C++ methods used by the ambitus engraver.
25 #(define (ly:separation-item::add-conditional-item grob grob-item)
26 "Add @var{grob-item} to the array of conditional elements of @var{grob}.
27 Rewrite of @code{Separation_item::add_conditional_item} from @file{lily/separation-item.cc}."
28 (ly:pointer-group-interface::add-grob grob 'conditional-elements grob-item))
30 #(define (ly:accidental-placement::accidental-pitch accidental-grob)
31 "Get the pitch from the grob cause of @var{accidental-grob}.
32 Rewrite of @code{accidental_pitch} from @file{lily/accidental-placement.cc}."
33 (ly:event-property (ly:grob-property (ly:grob-parent accidental-grob Y) 'cause)
36 #(define (ly:accidental-placement::add-accidental grob accidental-grob)
37 "Add @var{accidental-grob}, an @code{Accidental} grob, to the
38 list of the accidental grobs of @var{grob}, an @code{AccidentalPlacement}
40 Rewrite of @code{Accidental_placement::add_accidental} from @file{lily/accidental-placement.cc}."
41 (let ((pitch (ly:accidental-placement::accidental-pitch accidental-grob)))
42 (set! (ly:grob-parent accidental-grob X) grob)
43 (set! (ly:grob-property accidental-grob 'X-offset)
44 ly:grob::x-parent-positioning)
45 (let* ((accidentals (ly:grob-object grob 'accidental-grobs))
46 (handle (assq (ly:pitch-notename pitch) accidentals))
47 (entry (if handle (cdr handle) '())))
48 (set! (ly:grob-object grob 'accidental-grobs)
49 (assq-set! accidentals
50 (ly:pitch-notename pitch)
51 (cons accidental-grob entry))))))
54 %%% Ambitus data structure
57 %%% The <ambitus> class holds the various grobs that are created
58 %%% to print an ambitus:
59 %%% - ambitus-group: the grob that groups all the components of an ambitus
61 %%% - ambitus-line: the vertical line between the upper and lower ambitus
62 %%% notes (AmbitusLine grob);
63 %%% - ambitus-up-note and ambitus-down-note: the note head and accidental
64 %%% for the lower and upper note of the ambitus (see <ambitus-note> class
66 %%% The other slots define the key and clef context of the engraver:
67 %%% - start-c0: position of middle c at the beginning of the piece. It
68 %%% is used to place the ambitus notes according to their pitch;
69 %%% - start-key-sig: the key signature at the beginning of the piece. It
70 %%% is used to determine if accidentals shall be printed next to ambitus
73 #(define-class <ambitus> ()
74 (ambitus-group #:accessor ambitus-group)
75 (ambitus-line #:accessor ambitus-line)
76 (ambitus-up-note #:getter ambitus-up-note
77 #:init-form (make <ambitus-note>))
78 (ambitus-down-note #:getter ambitus-down-note
79 #:init-form (make <ambitus-note>))
80 (start-c0 #:accessor ambitus-start-c0
82 (start-key-sig #:accessor ambitus-start-key-sig
85 %%% Accessor for the lower and upper note data of an ambitus
86 #(define-method (ambitus-note (ambitus <ambitus>) direction)
87 "If @var{direction} is @code{UP}, then return the upper ambitus note
88 of @var{ambitus}, otherwise return the lower ambitus note."
90 (ambitus-up-note ambitus)
91 (ambitus-down-note ambitus)))
93 %%% The <ambitus-note> class holds the grobs that are specific to ambitus
94 %%% (lower and upper) notes:
95 %%% - head: an AmbitusNoteHead grob;
96 %%% - accidental: an AmbitusAccidental grob, to be possibly printed next
97 %%% to the ambitus note head.
99 %%% - pitch is the absolute pitch of the note
100 %%% - cause is the note event that causes this ambitus note, i.e. the lower
101 %%% or upper note of the considered music sequence.
103 #(define-class <ambitus-note> ()
104 (head #:accessor ambitus-note-head
106 (accidental #:accessor ambitus-note-accidental
108 (cause #:accessor ambitus-note-cause
110 (pitch #:accessor ambitus-note-pitch
114 %%% Ambitus engraving logics
116 %%% Rewrite of the code from @file{lily/ambitus-engraver.cc}.
118 #(define (make-ambitus translator)
119 "Build an ambitus object: initialize all the grobs and their relations.
121 The Ambitus grob contain all other grobs:
124 |- AmbitusNoteHead for upper note
125 |- AmbitusAccidental for upper note
126 |- AmbitusNoteHead for lower note
127 |- AmbitusAccidental for lower note
129 The parent of an accidental is the corresponding note head,
130 and the accidental is set as the 'accidental-grob of the note head
131 so that is printed by the function that prints notes."
132 ;; make the ambitus object
133 (let ((ambitus (make <ambitus>)))
134 ;; build the Ambitus grob, which will contain all other grobs
135 (set! (ambitus-group ambitus) (ly:engraver-make-grob translator 'Ambitus '()))
136 ;; build the AmbitusLine grob (line between lower and upper note)
137 (set! (ambitus-line ambitus) (ly:engraver-make-grob translator 'AmbitusLine '()))
138 ;; build the upper and lower AmbitusNoteHead and AmbitusAccidental
139 (for-each (lambda (direction)
140 (let ((head (ly:engraver-make-grob translator 'AmbitusNoteHead '()))
141 (accidental (ly:engraver-make-grob translator 'AmbitusAccidental '()))
142 (group (ambitus-group ambitus)))
143 ;; The parent of the AmbitusAccidental grob is the
144 ;; AmbitusNoteHead grob
145 (set! (ly:grob-parent accidental Y) head)
146 ;; The AmbitusAccidental grob is set as the accidental-grob
147 ;; object of the AmbitusNoteHead. This is later used by the
148 ;; function that prints notes.
149 (set! (ly:grob-object head 'accidental-grob) accidental)
150 ;; both the note head and the accidental grobs are added
151 ;; to the main ambitus grob.
152 (ly:axis-group-interface::add-element group head)
153 (ly:axis-group-interface::add-element group accidental)
154 ;; the note head and the accidental grobs are added to the
156 (set! (ambitus-note-head (ambitus-note ambitus direction))
158 (set! (ambitus-note-accidental (ambitus-note ambitus direction))
161 ;; The parent of the ambitus line is the lower ambitus note head
162 (set! (ly:grob-parent (ambitus-line ambitus) X)
163 (ambitus-note-head (ambitus-note ambitus DOWN)))
164 ;; the ambitus line is added to the ambitus main grob
165 (ly:axis-group-interface::add-element (ambitus-group ambitus) (ambitus-line ambitus))
168 #(define-method (initialize-ambitus-state (ambitus <ambitus>) translator)
169 "Initialize the state of @var{ambitus}, by getting the starting
170 position of middle C and key signature from @var{translator}'s context."
171 (if (not (ambitus-start-c0 ambitus))
173 (set! (ambitus-start-c0 ambitus)
174 (ly:context-property (ly:translator-context translator)
177 (set! (ambitus-start-key-sig ambitus)
178 (ly:context-property (ly:translator-context translator)
181 #(define-method (update-ambitus-notes (ambitus <ambitus>) note-grob)
182 "Update the upper and lower ambitus pithes of @var{ambitus}, using
184 ;; Get the event that caused the note-grob creation
185 ;; and check that it is a note-event.
186 (let ((note-event (ly:grob-property note-grob 'cause)))
187 (if (ly:in-event-class? note-event 'note-event)
188 ;; get the pitch from the note event
189 (let ((pitch (ly:event-property note-event 'pitch)))
190 ;; if this pitch is lower than the current ambitus lower
191 ;; note pitch (or it has not been initialized yet),
192 ;; then this pitch is the new ambitus lower pitch,
193 ;; and conversely for upper pitch.
194 (for-each (lambda (direction pitch-compare)
195 (if (or (not (ambitus-note-pitch (ambitus-note ambitus direction)))
197 (ambitus-note-pitch (ambitus-note ambitus direction))))
199 (set! (ambitus-note-pitch (ambitus-note ambitus direction))
201 (set! (ambitus-note-cause (ambitus-note ambitus direction))
204 (list ly:pitch<? (lambda (p1 p2)
205 (ly:pitch<? p2 p1))))))))
207 #(define-method (typeset-ambitus (ambitus <ambitus>) translator)
208 "Typeset the ambitus:
209 - place the lower and upper ambitus notes according to their pitch and
210 the position of the middle C;
211 - typeset or delete the note accidentals, according to the key signature.
212 An accidental, if it is to be printed, is added to an AccidentalPlacement
213 grob (a grob dedicated to the placement of accidentals near a chord);
214 - both note heads are added to the ambitus line grob, so that a line should
215 be printed between them."
216 ;; check if there are lower and upper pitches
217 (if (and (ambitus-note-pitch (ambitus-note ambitus UP))
218 (ambitus-note-pitch (ambitus-note ambitus DOWN)))
219 ;; make an AccidentalPlacement grob, for placement of note accidentals
220 (let ((accidental-placement (ly:engraver-make-grob
223 (ambitus-note-accidental (ambitus-note ambitus DOWN)))))
224 ;; For lower and upper ambitus notes:
225 (for-each (lambda (direction)
226 (let ((pitch (ambitus-note-pitch (ambitus-note ambitus direction))))
227 ;; set the cause and the staff position of the ambitus note
228 ;; according to the associated pitch
229 (set! (ly:grob-property (ambitus-note-head (ambitus-note ambitus direction))
231 (ambitus-note-cause (ambitus-note ambitus direction)))
232 (set! (ly:grob-property (ambitus-note-head (ambitus-note ambitus direction))
234 (+ (ambitus-start-c0 ambitus)
235 (ly:pitch-steps pitch)))
236 ;; determine if an accidental shall be printed for this note,
237 ;; according to the key signature
238 (let* ((handle (or (assoc (cons (ly:pitch-octave pitch)
239 (ly:pitch-notename pitch))
240 (ambitus-start-key-sig ambitus))
241 (assoc (ly:pitch-notename pitch)
242 (ambitus-start-key-sig ambitus))))
243 (sig-alter (if handle (cdr handle) 0)))
244 (cond ((= (ly:pitch-alteration pitch) sig-alter)
245 ;; the note alteration is in the key signature
246 ;; => it does not have to be printed
248 (ambitus-note-accidental (ambitus-note ambitus direction)))
249 (set! (ly:grob-object (ambitus-note-head (ambitus-note ambitus direction))
253 ;; otherwise, the accidental shall be printed
254 (set! (ly:grob-property (ambitus-note-accidental
255 (ambitus-note ambitus direction))
257 (ly:pitch-alteration pitch)))))
258 ;; add the AccidentalPlacement grob to the
259 ;; conditional items of the AmbitusNoteHead
260 (ly:separation-item::add-conditional-item
261 (ambitus-note-head (ambitus-note ambitus direction))
262 accidental-placement)
263 ;; add the AmbitusAccidental to the list of the
264 ;; AccidentalPlacement grob accidentals
265 (ly:accidental-placement::add-accidental
267 (ambitus-note-accidental (ambitus-note ambitus direction)))
268 ;; add the AmbitusNoteHead grob to the AmbitusLine grob
269 (ly:pointer-group-interface::add-grob
270 (ambitus-line ambitus)
272 (ambitus-note-head (ambitus-note ambitus direction)))))
274 ;; add the AccidentalPlacement grob to the main Ambitus grob
275 (ly:axis-group-interface::add-element (ambitus-group ambitus) accidental-placement))
276 ;; no notes ==> suicide the grobs
278 (for-each (lambda (direction)
279 (ly:grob-suicide! (ambitus-note-accidental (ambitus-note ambitus direction)))
280 (ly:grob-suicide! (ambitus-note-head (ambitus-note ambitus direction))))
282 (ly:grob-suicide! ambitus-line))))
285 %%% Ambitus engraver definition
287 #(define ambitus-engraver
290 ;; when music is processed: make the ambitus object, if not already built
292 ((process-music translator)
294 (set! ambitus (make-ambitus translator))))
295 ;; set the ambitus clef and key signature state
296 ((stop-translation-timestep translator)
298 (initialize-ambitus-state ambitus translator)))
299 ;; when a note-head grob is built, update the ambitus notes
301 ((note-head-interface engraver grob source-engraver)
303 (update-ambitus-notes ambitus grob))))
304 ;; finally, typeset the ambitus according to its upper and lower notes
306 ((finalize translator)
308 (typeset-ambitus ambitus translator)))))))
316 \new Staff { c'4 des' e' fis' gis' }
317 \new Staff { \clef "bass" c4 des ~ des ees b, }
319 \layout { \context { \Staff \consists #ambitus-engraver } }