4 lsrtags = "contexts-and-engravers"
7 This example demonstrates how the ambitus engraver may be defined on
8 the user side, with a Scheme engraver.
10 This is basically a rewrite in Scheme of the code from
11 @code{lily/ambitus-engraver.cc}.
14 doctitle = "Defining an engraver in Scheme: ambitus engraver"
16 #(use-modules (oop goops))
21 %%% These are literal rewrites of some C++ methods used by the ambitus engraver.
23 #(define (ly:separation-item::add-conditional-item grob grob-item)
24 "Add @var{grob-item} to the array of conditional elements of @var{grob}.
25 Rewrite of @code{Separation_item::add_conditional_item} from @file{lily/separation-item.cc}."
26 (ly:pointer-group-interface::add-grob grob 'conditional-elements grob-item))
28 #(define (ly:accidental-placement::accidental-pitch accidental-grob)
29 "Get the pitch from the grob cause of @var{accidental-grob}.
30 Rewrite of @code{accidental_pitch} from @file{lily/accidental-placement.cc}."
31 (ly:event-property (ly:grob-property (ly:grob-parent accidental-grob Y) 'cause)
34 #(define (ly:accidental-placement::add-accidental grob accidental-grob)
35 "Add @var{accidental-grob}, an @code{Accidental} grob, to the
36 list of the accidental grobs of @var{grob}, an @code{AccidentalPlacement}
38 Rewrite of @code{Accidental_placement::add_accidental} from @file{lily/accidental-placement.cc}."
39 (let ((pitch (ly:accidental-placement::accidental-pitch accidental-grob)))
40 (set! (ly:grob-parent accidental-grob X) grob)
41 (let* ((accidentals (ly:grob-object grob 'accidental-grobs))
42 (handle (assq (ly:pitch-notename pitch) accidentals))
43 (entry (if handle (cdr handle) '())))
44 (set! (ly:grob-object grob 'accidental-grobs)
45 (assq-set! accidentals
46 (ly:pitch-notename pitch)
47 (cons accidental-grob entry))))))
50 %%% Ambitus data structure
53 %%% The <ambitus> class holds the various grobs that are created
54 %%% to print an ambitus:
55 %%% - ambitus-group: the grob that groups all the components of an ambitus
57 %%% - ambitus-line: the vertical line between the upper and lower ambitus
58 %%% notes (AmbitusLine grob);
59 %%% - ambitus-up-note and ambitus-down-note: the note head and accidental
60 %%% for the lower and upper note of the ambitus (see <ambitus-note> class
62 %%% The other slots define the key and clef context of the engraver:
63 %%% - start-c0: position of middle c at the beginning of the piece. It
64 %%% is used to place the ambitus notes according to their pitch;
65 %%% - start-key-sig: the key signature at the beginning of the piece. It
66 %%% is used to determine if accidentals shall be printed next to ambitus
69 #(define-class <ambitus> ()
70 (ambitus-group #:accessor ambitus-group)
71 (ambitus-line #:accessor ambitus-line)
72 (ambitus-up-note #:getter ambitus-up-note
73 #:init-form (make <ambitus-note>))
74 (ambitus-down-note #:getter ambitus-down-note
75 #:init-form (make <ambitus-note>))
76 (start-c0 #:accessor ambitus-start-c0
78 (start-key-sig #:accessor ambitus-start-key-sig
81 %%% Accessor for the lower and upper note data of an ambitus
82 #(define-method (ambitus-note (ambitus <ambitus>) direction)
83 "If @var{direction} is @code{UP}, then return the upper ambitus note
84 of @var{ambitus}, otherwise return the lower ambitus note."
86 (ambitus-up-note ambitus)
87 (ambitus-down-note ambitus)))
89 %%% The <ambitus-note> class holds the grobs that are specific to ambitus
90 %%% (lower and upper) notes:
91 %%% - head: an AmbitusNoteHead grob;
92 %%% - accidental: an AmbitusAccidental grob, to be possibly printed next
93 %%% to the ambitus note head.
95 %%% - pitch is the absolute pitch of the note
96 %%% - cause is the note event that causes this ambitus note, i.e. the lower
97 %%% or upper note of the considered music sequence.
99 #(define-class <ambitus-note> ()
100 (head #:accessor ambitus-note-head
102 (accidental #:accessor ambitus-note-accidental
104 (cause #:accessor ambitus-note-cause
106 (pitch #:accessor ambitus-note-pitch
110 %%% Ambitus engraving logics
112 %%% Rewrite of the code from @file{lily/ambitus-engraver.cc}.
114 #(define (make-ambitus translator)
115 "Build an ambitus object: initialize all the grobs and their relations.
117 The Ambitus grob contain all other grobs:
120 |- AmbitusNoteHead for upper note
121 |- AmbitusAccidental for upper note
122 |- AmbitusNoteHead for lower note
123 |- AmbitusAccidental for lower note
125 The parent of an accidental is the corresponding note head,
126 and the accidental is set as the 'accidental-grob of the note head
127 so that is printed by the function that prints notes."
128 ;; make the ambitus object
129 (let ((ambitus (make <ambitus>)))
130 ;; build the Ambitus grob, which will contain all other grobs
131 (set! (ambitus-group ambitus) (ly:engraver-make-grob translator 'Ambitus '()))
132 ;; build the AmbitusLine grob (line between lower and upper note)
133 (set! (ambitus-line ambitus) (ly:engraver-make-grob translator 'AmbitusLine '()))
134 ;; build the upper and lower AmbitusNoteHead and AmbitusAccidental
135 (for-each (lambda (direction)
136 (let ((head (ly:engraver-make-grob translator 'AmbitusNoteHead '()))
137 (accidental (ly:engraver-make-grob translator 'AmbitusAccidental '()))
138 (group (ambitus-group ambitus)))
139 ;; The parent of the AmbitusAccidental grob is the
140 ;; AmbitusNoteHead grob
141 (set! (ly:grob-parent accidental Y) head)
142 ;; The AmbitusAccidental grob is set as the accidental-grob
143 ;; object of the AmbitusNoteHead. This is later used by the
144 ;; function that prints notes.
145 (set! (ly:grob-object head 'accidental-grob) accidental)
146 ;; both the note head and the accidental grobs are added
147 ;; to the main ambitus grob.
148 (ly:axis-group-interface::add-element group head)
149 (ly:axis-group-interface::add-element group accidental)
150 ;; the note head and the accidental grobs are added to the
152 (set! (ambitus-note-head (ambitus-note ambitus direction))
154 (set! (ambitus-note-accidental (ambitus-note ambitus direction))
157 ;; The parent of the ambitus line is the lower ambitus note head
158 (set! (ly:grob-parent (ambitus-line ambitus) X)
159 (ambitus-note-head (ambitus-note ambitus DOWN)))
160 ;; the ambitus line is added to the ambitus main grob
161 (ly:axis-group-interface::add-element (ambitus-group ambitus) (ambitus-line ambitus))
164 #(define-method (initialize-ambitus-state (ambitus <ambitus>) translator)
165 "Initialize the state of @var{ambitus}, by getting the starting
166 position of middle C and key signature from @var{translator}'s context."
167 (if (not (ambitus-start-c0 ambitus))
169 (set! (ambitus-start-c0 ambitus)
170 (ly:context-property (ly:translator-context translator)
173 (set! (ambitus-start-key-sig ambitus)
174 (ly:context-property (ly:translator-context translator)
177 #(define-method (update-ambitus-notes (ambitus <ambitus>) note-grob)
178 "Update the upper and lower ambitus pithes of @var{ambitus}, using
180 ;; Get the event that caused the note-grob creation
181 ;; and check that it is a note-event.
182 (let ((note-event (ly:grob-property note-grob 'cause)))
183 (if (ly:in-event-class? note-event 'note-event)
184 ;; get the pitch from the note event
185 (let ((pitch (ly:event-property note-event 'pitch)))
186 ;; if this pitch is lower than the current ambitus lower
187 ;; note pitch (or it has not been initialized yet),
188 ;; then this pitch is the new ambitus lower pitch,
189 ;; and conversely for upper pitch.
190 (for-each (lambda (direction pitch-compare)
191 (if (or (not (ambitus-note-pitch (ambitus-note ambitus direction)))
193 (ambitus-note-pitch (ambitus-note ambitus direction))))
195 (set! (ambitus-note-pitch (ambitus-note ambitus direction))
197 (set! (ambitus-note-cause (ambitus-note ambitus direction))
200 (list ly:pitch<? (lambda (p1 p2)
201 (ly:pitch<? p2 p1))))))))
203 #(define-method (typeset-ambitus (ambitus <ambitus>) translator)
204 "Typeset the ambitus:
205 - place the lower and upper ambitus notes according to their pitch and
206 the position of the middle C;
207 - typeset or delete the note accidentals, according to the key signature.
208 An accidental, if it is to be printed, is added to an AccidentalPlacement
209 grob (a grob dedicated to the placement of accidentals near a chord);
210 - both note heads are added to the ambitus line grob, so that a line should
211 be printed between them."
212 ;; check if there are lower and upper pitches
213 (if (and (ambitus-note-pitch (ambitus-note ambitus UP))
214 (ambitus-note-pitch (ambitus-note ambitus DOWN)))
215 ;; make an AccidentalPlacement grob, for placement of note accidentals
216 (let ((accidental-placement (ly:engraver-make-grob
219 (ambitus-note-accidental (ambitus-note ambitus DOWN)))))
220 ;; For lower and upper ambitus notes:
221 (for-each (lambda (direction)
222 (let ((pitch (ambitus-note-pitch (ambitus-note ambitus direction))))
223 ;; set the cause and the staff position of the ambitus note
224 ;; according to the associated pitch
225 (set! (ly:grob-property (ambitus-note-head (ambitus-note ambitus direction))
227 (ambitus-note-cause (ambitus-note ambitus direction)))
228 (set! (ly:grob-property (ambitus-note-head (ambitus-note ambitus direction))
230 (+ (ambitus-start-c0 ambitus)
231 (ly:pitch-steps pitch)))
232 ;; determine if an accidental shall be printed for this note,
233 ;; according to the key signature
234 (let* ((handle (or (assoc (cons (ly:pitch-octave pitch)
235 (ly:pitch-notename pitch))
236 (ambitus-start-key-sig ambitus))
237 (assoc (ly:pitch-notename pitch)
238 (ambitus-start-key-sig ambitus))))
239 (sig-alter (if handle (cdr handle) 0)))
240 (cond ((= (ly:pitch-alteration pitch) sig-alter)
241 ;; the note alteration is in the key signature
242 ;; => it does not have to be printed
244 (ambitus-note-accidental (ambitus-note ambitus direction)))
245 (set! (ly:grob-object (ambitus-note-head (ambitus-note ambitus direction))
249 ;; otherwise, the accidental shall be printed
250 (set! (ly:grob-property (ambitus-note-accidental
251 (ambitus-note ambitus direction))
253 (ly:pitch-alteration pitch)))))
254 ;; add the AccidentalPlacement grob to the
255 ;; conditional items of the AmbitusNoteHead
256 (ly:separation-item::add-conditional-item
257 (ambitus-note-head (ambitus-note ambitus direction))
258 accidental-placement)
259 ;; add the AmbitusAccidental to the list of the
260 ;; AccidentalPlacement grob accidentals
261 (ly:accidental-placement::add-accidental
263 (ambitus-note-accidental (ambitus-note ambitus direction)))
264 ;; add the AmbitusNoteHead grob to the AmbitusLine grob
265 (ly:pointer-group-interface::add-grob
266 (ambitus-line ambitus)
268 (ambitus-note-head (ambitus-note ambitus direction)))))
270 ;; add the AccidentalPlacement grob to the main Ambitus grob
271 (ly:axis-group-interface::add-element (ambitus-group ambitus) accidental-placement))
272 ;; no notes ==> suicide the grobs
274 (for-each (lambda (direction)
275 (ly:grob-suicide! (ambitus-note-accidental (ambitus-note ambitus direction)))
276 (ly:grob-suicide! (ambitus-note-head (ambitus-note ambitus direction))))
278 (ly:grob-suicide! ambitus-line))))
281 %%% Ambitus engraver definition
283 #(define ambitus-engraver
286 ;; when music is processed: make the ambitus object, if not already built
288 ((process-music translator)
290 (set! ambitus (make-ambitus translator))))
291 ;; set the ambitus clef and key signature state
292 ((stop-translation-timestep translator)
294 (initialize-ambitus-state ambitus translator)))
295 ;; when a note-head grob is built, update the ambitus notes
297 ((note-head-interface engraver grob source-engraver)
299 (update-ambitus-notes ambitus grob))))
300 ;; finally, typeset the ambitus according to its upper and lower notes
302 ((finalize translator)
304 (typeset-ambitus ambitus translator)))))))
312 \new Staff { c'4 des' e' fis' gis' }
313 \new Staff { \clef "bass" c4 des ~ des ees b, }
315 \layout { \context { \Staff \consists #ambitus-engraver } }