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 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 (layout-instruction-event . (apply-output-event))
27 (script-event . (articulation-event text-script-event))
28 (part-combine-event . (solo-one-event solo-two-event unisono-event))
29 (break-event . (line-break-event page-break-event page-turn-event))
30 (dynamic-event . (absolute-dynamic-event))
31 (span-event . (span-dynamic-event beam-event ligature-event
32 pedal-event phrasing-slur-event slur-event staff-span-event
33 text-span-event trill-span-event tremolo-span-event
35 (span-dynamic-event . (decrescendo-event crescendo-event))
36 (pedal-event . (sostenuto-event sustain-event una-corda-event))
37 (rhythmic-event . (lyric-event melodic-event multi-measure-rest-event
38 rest-event skip-event bass-figure-event))
39 (melodic-event . (cluster-note-event note-event))
41 (Announcement . (AnnounceNewContext))
44 ;; Maps event-class to a list of ancestors (inclusive)
45 (define ancestor-lookup (make-hash-table 11))
47 ;; Each class will be defined as
48 ;; (class parent grandparent .. )
49 ;; so that (eq? (cdr class) parent) holds.
54 (hashq-set! ancestor-lookup type
55 (cons type (hashq-ref ancestor-lookup (car rel) '()))))
59 ;; TODO: Allow entering more complex classes, by taking unions.
60 (define-public (ly:make-event-class leaf)
61 (hashq-ref ancestor-lookup leaf))
63 ;; does this exist in guile already?
64 (define (map-tree f t)
67 (map (lambda (x) (map-tree f x)) t))
69 (cons (map-tree f (car t)) (map-tree f (cdr t))))
72 ;; expand each non-leaf subtree to (root . children), recursively
73 (define (expand-event-tree root)
74 (let ((children (assq root event-classes)))
76 (cons root (map expand-event-tree (cdr children)))
79 ;; All leaf event classes that no translator listens to
80 ;; directly. Avoids printing a warning.
81 (define unlistened-music-event-classes
82 '(harmonic-event line-break-event page-break-event page-turn-event
83 solo-one-event solo-two-event skip-event unisono-event))
85 ;; produce neater representation of music event tree.
86 ;; TODO: switch to this representation for the event-classes list?
87 (define music-event-tree (expand-event-tree 'music-event))
89 (define (stringify el)
92 (symbol->string (first el))))
94 (sort (map (lambda (el)
96 (cons (car el) (sort-tree (cdr el)))
99 (lambda (a b) (string<? (stringify a) (stringify b))))
102 ;;(use-modules (ice-9 pretty-print))
103 ;;(pretty-print (cons (car music-event-tree) (sort-tree (cdr music-event-tree))))
105 ;; check that the music event tree corresponds well with the set of
106 ;; available translators; print warnings otherwise.
107 (map-tree (lambda (sym)
108 (if (and (symbol? sym)
109 (not (ly:is-listened-event-class sym))
110 (not (assq sym event-classes))
111 (not (memq sym unlistened-music-event-classes)))
112 (ly:programming-error (_ "event class ~A seems to be unused") sym)))
116 (if (not (pair? (ly:make-event-class sym)))
117 ;; should be programming-error
118 (ly:error (_ "translator listens to nonexisting event class ~A") sym)))
119 (ly:get-listened-event-classes))
121 (defmacro-public make-stream-event (expr)
122 (Stream_event::undump (primitive-eval (list 'quasiquote expr))))
124 (define* (simplify e)
126 ;; Special case for lists reduces stack consumption.
127 ((list? e) (map simplify e))
128 ((pair? e) (cons (simplify (car e))
130 ((ly:stream-event? e)
131 (list 'unquote `(make-stream-event ,(simplify (Stream_event::dump e)))))
133 (list 'unquote (music->make-music e)))
135 (list 'unquote `(ly:make-moment
136 ,(ly:moment-main-numerator e)
137 ,(ly:moment-main-denominator e)
138 . ,(if (eq? 0 (ly:moment-grace-numerator e))
140 (list (ly:moment-grace-numerator e)
141 (ly:moment-grace-denominator e))))))
143 (list 'unquote `(ly:make-duration
145 ,(ly:duration-dot-count e)
146 ,(car (ly:duration-factor e))
147 ,(cdr (ly:duration-factor e)))))
149 (list 'unquote `(ly:make-pitch
151 ,(ly:pitch-notename e)
152 ,(ly:pitch-alteration e))))
153 ((ly:input-location? e)
154 (list 'unquote '(ly:dummy-input-location)))
157 (define-public (ly:simplify-scheme e)
158 (list 'quasiquote (simplify e)))