1 ;;;; stream-event-classes.scm -- define the tree of stream-event classes.
3 ;;;; source file of the GNU LilyPond music typesetter
5 ;;;; (c) 2005-2006 Erik Sandberg <mandolaerik@gmail.com>
8 (use-modules (srfi srfi-1))
10 ;; Event class hierarchy. Each line is on the form (Parent . (List of children))
12 '((() . (StreamEvent))
14 (RemoveContext ChangeParent Override Revert UnsetProperty
15 SetProperty music-event OldMusicEvent CreateContext Prepare
17 (music-event . (arpeggio-event breathing-event extender-event span-event
18 rhythmic-event dynamic-event break-event percent-event
19 key-change-event string-number-event tie-event part-combine-event
20 metronome-change-event beam-forbid-event script-event
21 tremolo-event bend-after-event fingering-event glissando-event
22 harmonic-event hyphen-event laissez-vibrer-event mark-event
23 multi-measure-text-event note-grouping-event
24 pes-or-flexa-event repeat-tie-event spacing-section-event
25 layout-instruction-event))
26 (script-event . (articulation-event text-script-event))
27 (part-combine-event . (solo1-event solo2-event unisono-event))
28 (break-event . (line-break-event page-break-event page-turn-event))
29 (dynamic-event . (absolute-dynamic-event))
30 (span-event . (span-dynamic-event beam-event ligature-event
31 pedal-event phrasing-slur-event slur-event staff-span-event
32 text-span-event trill-span-event tremolo-span-event
34 (span-dynamic-event . (decrescendo-event crescendo-event))
35 (pedal-event . (sostenuto-event sustain-event una-corda-event))
36 (rhythmic-event . (lyric-event melodic-event multi-measure-rest-event
37 rest-event skip-event bass-figure-event))
38 (melodic-event . (cluster-note-event note-event))
40 (Announcement . (AnnounceNewContext))
43 ;; Maps event-class to a list of ancestors (inclusive)
44 (define ancestor-lookup (make-hash-table))
46 ;; Each class will be defined as
47 ;; (class parent grandparent .. )
48 ;; so that (eq? (cdr class) parent) holds.
53 (hashq-set! ancestor-lookup type
54 (cons type (hashq-ref ancestor-lookup (car rel) '()))))
58 ;; TODO: Allow entering more complex classes, by taking unions.
59 (define-public (ly:make-event-class leaf)
60 (hashq-ref ancestor-lookup leaf))
62 ;; does this exist in guile already?
63 (define (map-tree f t)
66 (map (lambda (x) (map-tree f x)) t))
68 (cons (map-tree f (car t)) (map-tree f (cdr t))))
71 ;; expand each non-leaf subtree to (root . children), recursively
72 (define (expand-event-tree root)
73 (let ((children (assq root event-classes)))
75 (cons root (map expand-event-tree (cdr children)))
78 ;; All leaf event classes that no translator listens to
79 ;; directly. Avoids printing a warning.
80 (define unlistened-music-event-classes
81 '(harmonic-event line-break-event page-break-event page-turn-event
82 solo1-event solo2-event skip-event unisono-event))
84 ;; produce neater representation of music event tree.
85 ;; TODO: switch to this representation for the event-classes list?
86 (define music-event-tree (expand-event-tree 'music-event))
88 (define (stringify el)
91 (symbol->string (first el))))
93 (sort (map (lambda (el)
95 (cons (car el) (sort-tree (cdr el)))
98 (lambda (a b) (string<? (stringify a) (stringify b))))
101 ;;(use-modules (ice-9 pretty-print))
102 ;;(pretty-print (cons (car music-event-tree) (sort-tree (cdr music-event-tree))))
104 ;; check that the music event tree corresponds well with the set of
105 ;; available translators; print warnings otherwise.
106 (map-tree (lambda (sym)
107 (if (and (symbol? sym)
108 (not (memq sym (ly:get-listened-event-classes)))
109 (not (assq sym event-classes))
110 (not (memq sym unlistened-music-event-classes)))
111 (ly:programming-error (_ "event class ~A seems to be unused") sym)))
114 (if (not (pair? (ly:make-event-class sym)))
115 ;; should be programming-error
116 (ly:error (_ "translator listens to nonexisting event class ~A") sym)))
117 (ly:get-listened-event-classes))
119 (defmacro-public make-stream-event (expr)
120 (Stream_event::undump (primitive-eval (list 'quasiquote expr))))
122 (define* (simplify e)
124 ;; Special case for lists reduces stack consumption.
125 ((list? e) (map simplify e))
126 ((pair? e) (cons (simplify (car e))
128 ((ly:stream-event? e)
129 (list 'unquote `(make-stream-event ,(simplify (Stream_event::dump e)))))
131 (list 'unquote (music->make-music e)))
133 (list 'unquote `(ly:make-moment
134 ,(ly:moment-main-numerator e)
135 ,(ly:moment-main-denominator e)
136 . ,(if (eq? 0 (ly:moment-grace-numerator e))
138 (list (ly:moment-grace-numerator e)
139 (ly:moment-grace-denominator e))))))
141 (list 'unquote `(ly:make-duration
143 ,(ly:duration-dot-count e)
144 ,(car (ly:duration-factor e))
145 ,(cdr (ly:duration-factor e)))))
147 (list 'unquote `(ly:make-pitch
149 ,(ly:pitch-notename e)
150 ,(ly:pitch-alteration e))))
151 ((ly:input-location? e)
152 (list 'unquote '(ly:dummy-input-location)))
155 (define-public (ly:simplify-scheme e)
156 (list 'quasiquote (simplify e))
159 ; used by lily/dispatcher.cc
160 (define-public (car< a b) (< (car a) (car b)))