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