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
6 % This file is in the public domain.
7 %% Note: this file works from version 2.15.31
11 %% Translation of GIT committish: 6977ddc9a3b63ea810eaecb864269c7d847ccf98
14 Este ejemplo demuestra cómo se puede definir el grabador de ámbito en
15 el espacio del usuario, con un grabador de Scheme.
17 Esto es básicamente una reescritura en Scheme del código de
18 @file{lily/ambitus-engraver.cc}.
22 doctitlees = "Definir un grabador en Scheme: grabador de ámbito"
25 lsrtags = "contexts-and-engravers"
28 texidoc = "This example demonstrates how the ambitus engraver may be
29 defined on the user side, with a Scheme engraver.
31 This is basically a rewrite in Scheme of the code from
32 @file{lily/ambitus-engraver.cc}.
35 doctitle = "Defining an engraver in Scheme: ambitus engraver"
39 #(use-modules (oop goops))
44 %%% These are literal rewrites of some C++ methods used by the ambitus engraver.
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))
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)
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}
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))))))
75 %%% Ambitus data structure
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
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
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
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
103 (start-key-sig #:accessor ambitus-start-key-sig
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."
111 (ambitus-up-note ambitus)
112 (ambitus-down-note ambitus)))
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.
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.
124 #(define-class <ambitus-note> ()
125 (head #:accessor ambitus-note-head
127 (accidental #:accessor ambitus-note-accidental
129 (cause #:accessor ambitus-note-cause
131 (pitch #:accessor ambitus-note-pitch
135 %%% Ambitus engraving logics
137 %%% Rewrite of the code from @file{lily/ambitus-engraver.cc}.
139 #(define (make-ambitus translator)
140 "Build an ambitus object: initialize all the grobs and their relations.
142 The Ambitus grob contain all other grobs:
145 |- AmbitusNoteHead for upper note
146 |- AmbitusAccidental for upper note
147 |- AmbitusNoteHead for lower note
148 |- AmbitusAccidental for lower note
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
177 (set! (ambitus-note-head (ambitus-note ambitus direction))
179 (set! (ambitus-note-accidental (ambitus-note ambitus direction))
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))
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))
194 (set! (ambitus-start-c0 ambitus)
195 (ly:context-property (ly:translator-context translator)
198 (set! (ambitus-start-key-sig ambitus)
199 (ly:context-property (ly:translator-context translator)
202 #(define-method (update-ambitus-notes (ambitus <ambitus>) note-grob)
203 "Update the upper and lower ambitus pithes of @var{ambitus}, using
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)))
218 (ambitus-note-pitch (ambitus-note ambitus direction))))
220 (set! (ambitus-note-pitch (ambitus-note ambitus direction))
222 (set! (ambitus-note-cause (ambitus-note ambitus direction))
225 (list ly:pitch<? (lambda (p1 p2)
226 (ly:pitch<? p2 p1))))))))
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
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))
252 (ambitus-note-cause (ambitus-note ambitus direction)))
253 (set! (ly:grob-property (ambitus-note-head (ambitus-note ambitus direction))
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
269 (ambitus-note-accidental (ambitus-note ambitus direction)))
270 (set! (ly:grob-object (ambitus-note-head (ambitus-note ambitus direction))
274 ;; otherwise, the accidental shall be printed
275 (set! (ly:grob-property (ambitus-note-accidental
276 (ambitus-note ambitus direction))
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
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)
293 (ambitus-note-head (ambitus-note ambitus direction)))))
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
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))))
303 (ly:grob-suicide! ambitus-line))))
306 %%% Ambitus engraver definition
308 #(define ambitus-engraver
311 ;; when music is processed: make the ambitus object, if not already built
313 ((process-music translator)
315 (set! ambitus (make-ambitus translator))))
316 ;; set the ambitus clef and key signature state
317 ((stop-translation-timestep translator)
319 (initialize-ambitus-state ambitus translator)))
320 ;; when a note-head grob is built, update the ambitus notes
322 ((note-head-interface engraver grob source-engraver)
324 (update-ambitus-notes ambitus grob))))
325 ;; finally, typeset the ambitus according to its upper and lower notes
327 ((finalize translator)
329 (typeset-ambitus ambitus translator)))))))
337 \new Staff { c'4 des' e' fis' gis' }
338 \new Staff { \clef "bass" c4 des ~ des ees b, }
340 \layout { \context { \Staff \consists #ambitus-engraver } }