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 label-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
41 rest-event skip-event bass-figure-event))
42 (melodic-event . (cluster-note-event note-event))
44 (Announcement . (AnnounceNewContext))
47 ;; Maps event-class to a list of ancestors (inclusive)
48 (define ancestor-lookup (make-hash-table 11))
50 ;; Each class will be defined as
51 ;; (class parent grandparent .. )
52 ;; so that (eq? (cdr class) parent) holds.
57 (hashq-set! ancestor-lookup type
58 (cons type (hashq-ref ancestor-lookup (car rel) '()))))
62 ;; TODO: Allow entering more complex classes, by taking unions.
63 (define-public (ly:make-event-class leaf)
64 (hashq-ref ancestor-lookup leaf))
66 (define-public (ly:in-event-class? ev cl)
67 "Does event @var{ev} belong to event class @var{cl}?"
68 (memq cl (ly:make-event-class (ly:event-property ev 'class))))
70 ;; does this exist in guile already?
71 (define (map-tree f t)
74 (map (lambda (x) (map-tree f x)) t))
76 (cons (map-tree f (car t)) (map-tree f (cdr t))))
79 ;; expand each non-leaf subtree to (root . children), recursively
80 (define (expand-event-tree root)
81 (let ((children (assq root event-classes)))
83 (cons root (map expand-event-tree (cdr children)))
86 ;; All leaf event classes that no translator listens to
87 ;; directly. Avoids printing a warning.
88 (define unlistened-music-event-classes
89 '(harmonic-event line-break-event page-break-event page-turn-event label-event
90 solo-one-event solo-two-event skip-event unisono-event))
92 ;; produce neater representation of music event tree.
93 ;; TODO: switch to this representation for the event-classes list?
94 (define music-event-tree (expand-event-tree 'music-event))
96 (define (stringify el)
99 (symbol->string (first el))))
101 (sort (map (lambda (el)
103 (cons (car el) (sort-tree (cdr el)))
106 (lambda (a b) (string<? (stringify a) (stringify b))))
109 ;;(use-modules (ice-9 pretty-print))
110 ;;(pretty-print (cons (car music-event-tree) (sort-tree (cdr music-event-tree))))
112 ;; check that the music event tree corresponds well with the set of
113 ;; available translators; print warnings otherwise.
114 (map-tree (lambda (sym)
115 (if (and (symbol? sym)
116 (not (ly:is-listened-event-class sym))
117 (not (assq sym event-classes))
118 (not (memq sym unlistened-music-event-classes)))
119 (ly:programming-error (_ "event class ~A seems to be unused") sym)))
123 (if (not (pair? (ly:make-event-class sym)))
124 ;; should be programming-error
125 (ly:error (_ "translator listens to nonexisting event class ~A") sym)))
126 (ly:get-listened-event-classes))
128 (defmacro-public make-stream-event (expr)
129 (Stream_event::undump (primitive-eval (list 'quasiquote expr))))
131 (define* (simplify e)
133 ;; Special case for lists reduces stack consumption.
134 ((list? e) (map simplify e))
135 ((pair? e) (cons (simplify (car e))
137 ((ly:stream-event? e)
138 (list 'unquote (list 'make-stream-event (simplify (Stream_event::dump e)))))
140 (list 'unquote (music->make-music e)))
142 (list 'unquote `(ly:make-moment
143 ,(ly:moment-main-numerator e)
144 ,(ly:moment-main-denominator e)
145 . ,(if (eq? 0 (ly:moment-grace-numerator e))
147 (list (ly:moment-grace-numerator e)
148 (ly:moment-grace-denominator e))))))
150 (list 'unquote `(ly:make-duration
152 ,(ly:duration-dot-count e)
153 ,(car (ly:duration-factor e))
154 ,(cdr (ly:duration-factor e)))))
156 (list 'unquote `(ly:make-pitch
158 ,(ly:pitch-notename e)
159 ,(ly:pitch-alteration e))))
160 ((ly:input-location? e)
161 (list 'unquote '(ly:dummy-input-location)))
164 (define-public (ly:simplify-scheme e)
165 (list 'quasiquote (simplify e)))