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 . (annotate-output-event
18 arpeggio-event breathing-event extender-event span-event
19 rhythmic-event dynamic-event break-event percent-event
20 key-change-event string-number-event stroke-finger-event tie-event part-combine-event
21 beam-forbid-event script-event
22 tremolo-event bend-after-event fingering-event glissando-event
23 harmonic-event hyphen-event laissez-vibrer-event mark-event
24 multi-measure-text-event note-grouping-event
25 pes-or-flexa-event repeat-tie-event spacing-section-event
26 layout-instruction-event))
28 (layout-instruction-event . (apply-output-event ))
29 (script-event . (articulation-event text-script-event))
30 (part-combine-event . (solo-one-event solo-two-event unisono-event))
31 (break-event . (line-break-event page-break-event page-turn-event))
32 (dynamic-event . (absolute-dynamic-event))
33 (span-event . (span-dynamic-event beam-event ligature-event
34 pedal-event phrasing-slur-event slur-event staff-span-event
35 text-span-event trill-span-event tremolo-span-event
37 (span-dynamic-event . (decrescendo-event crescendo-event))
38 (pedal-event . (sostenuto-event sustain-event una-corda-event))
39 (rhythmic-event . (lyric-event melodic-event multi-measure-rest-event
40 rest-event skip-event bass-figure-event))
41 (melodic-event . (cluster-note-event note-event))
43 (Announcement . (AnnounceNewContext))
46 ;; Maps event-class to a list of ancestors (inclusive)
47 (define ancestor-lookup (make-hash-table 11))
49 ;; Each class will be defined as
50 ;; (class parent grandparent .. )
51 ;; so that (eq? (cdr class) parent) holds.
56 (hashq-set! ancestor-lookup type
57 (cons type (hashq-ref ancestor-lookup (car rel) '()))))
61 ;; TODO: Allow entering more complex classes, by taking unions.
62 (define-public (ly:make-event-class leaf)
63 (hashq-ref ancestor-lookup leaf))
65 (define-public (ly:in-event-class? ev cl)
66 "Does event @var{ev} belong to event class @var{cl}?"
67 (memq cl (ly:make-event-class (ly:event-property ev 'class))))
69 ;; does this exist in guile already?
70 (define (map-tree f t)
73 (map (lambda (x) (map-tree f x)) t))
75 (cons (map-tree f (car t)) (map-tree f (cdr t))))
78 ;; expand each non-leaf subtree to (root . children), recursively
79 (define (expand-event-tree root)
80 (let ((children (assq root event-classes)))
82 (cons root (map expand-event-tree (cdr children)))
85 ;; All leaf event classes that no translator listens to
86 ;; directly. Avoids printing a warning.
87 (define unlistened-music-event-classes
88 '(harmonic-event line-break-event page-break-event page-turn-event
89 solo-one-event solo-two-event skip-event unisono-event))
91 ;; produce neater representation of music event tree.
92 ;; TODO: switch to this representation for the event-classes list?
93 (define music-event-tree (expand-event-tree 'music-event))
95 (define (stringify el)
98 (symbol->string (first el))))
100 (sort (map (lambda (el)
102 (cons (car el) (sort-tree (cdr el)))
105 (lambda (a b) (string<? (stringify a) (stringify b))))
108 ;;(use-modules (ice-9 pretty-print))
109 ;;(pretty-print (cons (car music-event-tree) (sort-tree (cdr music-event-tree))))
111 ;; check that the music event tree corresponds well with the set of
112 ;; available translators; print warnings otherwise.
113 (map-tree (lambda (sym)
114 (if (and (symbol? sym)
115 (not (ly:is-listened-event-class sym))
116 (not (assq sym event-classes))
117 (not (memq sym unlistened-music-event-classes)))
118 (ly:programming-error (_ "event class ~A seems to be unused") sym)))
122 (if (not (pair? (ly:make-event-class sym)))
123 ;; should be programming-error
124 (ly:error (_ "translator listens to nonexisting event class ~A") sym)))
125 (ly:get-listened-event-classes))
127 (defmacro-public make-stream-event (expr)
128 (Stream_event::undump (primitive-eval (list 'quasiquote expr))))
130 (define* (simplify e)
132 ;; Special case for lists reduces stack consumption.
133 ((list? e) (map simplify e))
134 ((pair? e) (cons (simplify (car e))
136 ((ly:stream-event? e)
137 (list 'unquote (list 'make-stream-event (simplify (Stream_event::dump e)))))
139 (list 'unquote (music->make-music e)))
141 (list 'unquote `(ly:make-moment
142 ,(ly:moment-main-numerator e)
143 ,(ly:moment-main-denominator e)
144 . ,(if (eq? 0 (ly:moment-grace-numerator e))
146 (list (ly:moment-grace-numerator e)
147 (ly:moment-grace-denominator e))))))
149 (list 'unquote `(ly:make-duration
151 ,(ly:duration-dot-count e)
152 ,(car (ly:duration-factor e))
153 ,(cdr (ly:duration-factor e)))))
155 (list 'unquote `(ly:make-pitch
157 ,(ly:pitch-notename e)
158 ,(ly:pitch-alteration e))))
159 ((ly:input-location? e)
160 (list 'unquote '(ly:dummy-input-location)))
163 (define-public (ly:simplify-scheme e)
164 (list 'quasiquote (simplify e)))