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