]> git.donarmstrong.com Git - lilypond.git/blob - scm/define-event-classes.scm
Change class field in stream events to contain the complete event type list
[lilypond.git] / scm / define-event-classes.scm
1 ;;;; This file is part of LilyPond, the GNU music typesetter.
2 ;;;;
3 ;;;; Copyright (C) 2005--2012 Erik Sandberg <mandolaerik@gmail.com>
4 ;;;;
5 ;;;; LilyPond is free software: you can redistribute it and/or modify
6 ;;;; it under the terms of the GNU General Public License as published by
7 ;;;; the Free Software Foundation, either version 3 of the License, or
8 ;;;; (at your option) any later version.
9 ;;;;
10 ;;;; LilyPond is distributed in the hope that it will be useful,
11 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 ;;;; GNU General Public License for more details.
14 ;;;;
15 ;;;; You should have received a copy of the GNU General Public License
16 ;;;; along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
17
18
19 (use-modules (srfi srfi-1))
20
21 ;; Event class hierarchy. Each line is on the form (Parent . (List of children))
22 (define event-classes
23   '((() . (StreamEvent))
24     (StreamEvent .
25                  (RemoveContext ChangeParent Override Revert UnsetProperty
26                                 SetProperty music-event OldMusicEvent CreateContext Prepare
27                                 OneTimeStep Finish))
28     (music-event . (annotate-output-event footnote-event
29                     arpeggio-event breathing-event extender-event span-event
30       rhythmic-event dynamic-event break-event label-event percent-event
31       key-change-event string-number-event stroke-finger-event tie-event
32       part-combine-event part-combine-force-event
33       beam-forbid-event script-event tempo-change-event
34       tremolo-event bend-after-event fingering-event glissando-event
35       harmonic-event hyphen-event laissez-vibrer-event mark-event
36       multi-measure-text-event note-grouping-event
37       pes-or-flexa-event repeat-tie-event spacing-section-event
38       layout-instruction-event completize-extender-event break-span-event
39       alternative-event))
40
41     (layout-instruction-event . (apply-output-event))
42     (script-event . (articulation-event text-script-event))
43     (part-combine-event . (solo-one-event solo-two-event unisono-event))
44     (break-event . (line-break-event page-break-event page-turn-event))
45     (dynamic-event . (absolute-dynamic-event))
46     (span-event . (span-dynamic-event beam-event episema-event ligature-event
47                          pedal-event phrasing-slur-event slur-event staff-span-event
48                          text-span-event trill-span-event tremolo-span-event
49                          tuplet-span-event))
50     (span-dynamic-event . (decrescendo-event crescendo-event))
51     (break-span-event . (break-dynamic-span-event))
52     (pedal-event . (sostenuto-event sustain-event una-corda-event))
53     (rhythmic-event . (lyric-event melodic-event multi-measure-rest-event
54                                    double-percent-event percent-event
55                                    repeat-slash-event rest-event
56                                    skip-event bass-figure-event))
57     (melodic-event . (cluster-note-event note-event))
58     (() . (Announcement))
59     (Announcement . (AnnounceNewContext))
60     ))
61
62 ;; Maps event-class to a list of ancestors (inclusive)
63 (define ancestor-lookup (make-hash-table 11))
64
65 ;; Each class will be defined as
66 ;; (class parent grandparent .. )
67 ;; so that (eq? (cdr class) parent) holds.
68 (for-each
69  (lambda (rel)
70    (for-each
71     (lambda (type)
72       (hashq-set! ancestor-lookup type
73                   (cons type (hashq-ref ancestor-lookup (car rel) '()))))
74     (cdr rel)))
75  event-classes)
76
77 (define-public (define-event-class leaf heritage)
78   (cond
79    ((not (eq? leaf (car heritage)))
80     (ly:warning (_ "All classes must be the last in their matrilineal line.")))
81    ((not (equal? (cdr heritage)
82                  (list-head (hashq-ref ancestor-lookup (cadr heritage) '())
83                             (length (cdr heritage)))))
84     (ly:warning (_ "All classes must have a well-defined pedigree in the existing class hierarchy.")))
85    (else (hashq-set! ancestor-lookup
86                      leaf
87                      (cons leaf
88                            (hashq-ref ancestor-lookup
89                                       (cadr heritage)
90                                       '())))))
91   *unspecified*)
92
93 ;; TODO: Allow entering more complex classes, by taking unions.
94 (define-public (ly:make-event-class leaf)
95  (hashq-ref ancestor-lookup leaf))
96
97 (define-public (ly:in-event-class? ev cl)
98   "Does event @var{ev} belong to event class @var{cl}?"
99   (memq cl (ly:event-property ev 'class)))
100
101 ;; does this exist in guile already?
102 (define (map-tree f t)
103   (cond
104    ((list? t)
105     (map (lambda (x) (map-tree f x)) t))
106    ((pair? t)
107     (cons (map-tree f (car t)) (map-tree f (cdr t))))
108    (else (f t))))
109
110 ;; expand each non-leaf subtree to (root . children), recursively
111 (define (expand-event-tree root)
112   (let ((children (assq root event-classes)))
113     (if children
114         (cons root (map expand-event-tree (cdr children)))
115         root)))
116
117 ;; All leaf event classes that no translator listens to
118 ;; directly.  Avoids printing a warning.
119 (define unlistened-music-event-classes
120   '(harmonic-event line-break-event page-break-event page-turn-event label-event
121                    solo-one-event solo-two-event skip-event unisono-event
122                    part-combine-force-event break-dynamic-span-event
123                    stroke-finger-event))
124
125 ;; produce neater representation of music event tree.
126 ;; TODO: switch to this representation for the event-classes list?
127 (define music-event-tree (expand-event-tree 'music-event))
128 (define (sort-tree t)
129   (define (stringify el)
130               (if (symbol? el)
131                   (symbol->string el)
132                   (symbol->string (first el))))
133   (if (list? t)
134       (sort (map (lambda (el)
135                    (if (list? el)
136                        (cons (car el) (sort-tree (cdr el)))
137                        el))
138                  t)
139             (lambda (a b) (string<? (stringify a) (stringify b))))
140       t))
141
142 ;;(use-modules (ice-9 pretty-print))
143 ;;(pretty-print (cons (car music-event-tree) (sort-tree (cdr music-event-tree))))
144
145 ;; check that the music event tree corresponds well with the set of
146 ;; available translators; print warnings otherwise.
147 (map-tree (lambda (sym)
148             (if (and (symbol? sym)
149                      (not (ly:is-listened-event-class sym))
150                      (not (assq sym event-classes))
151                      (not (memq sym unlistened-music-event-classes)))
152                 (ly:programming-error (_ "event class ~A seems to be unused") sym)))
153           music-event-tree)
154
155 (map (lambda (sym)
156        (if (not (pair? (ly:make-event-class sym)))
157            ;; should be programming-error
158            (ly:error (_ "translator listens to nonexisting event class ~A") sym)))
159      (ly:get-listened-event-classes))
160
161 (defmacro-public make-stream-event (expr)
162   (Stream_event::undump (primitive-eval (list 'quasiquote expr))))
163
164 (define* (simplify e)
165   (cond
166    ;; Special case for lists reduces stack consumption.
167    ((list? e) (map simplify e))
168    ((pair? e) (cons (simplify (car e))
169                     (simplify (cdr e))))
170    ((ly:stream-event? e)
171     (list 'unquote (list 'make-stream-event (simplify (Stream_event::dump e)))))
172    ((ly:music? e)
173     (list 'unquote (music->make-music e)))
174    ((ly:moment? e)
175     (list 'unquote `(ly:make-moment
176                      ,(ly:moment-main-numerator e)
177                      ,(ly:moment-main-denominator e)
178                      . ,(if (eq? 0 (ly:moment-grace-numerator e))
179                             '()
180                             (list (ly:moment-grace-numerator e)
181                                   (ly:moment-grace-denominator e))))))
182    ((ly:duration? e)
183     (list 'unquote `(ly:make-duration
184                      ,(ly:duration-log e)
185                      ,(ly:duration-dot-count e)
186                      ,(car (ly:duration-factor e))
187                      ,(cdr (ly:duration-factor e)))))
188    ((ly:pitch? e)
189     (list 'unquote `(ly:make-pitch
190                      ,(ly:pitch-octave e)
191                      ,(ly:pitch-notename e)
192                      ,(ly:pitch-alteration e))))
193    ((ly:input-location? e)
194     (list 'unquote '(ly:dummy-input-location)))
195    (#t e)))
196
197 (define-public (ly:simplify-scheme e)
198   (list 'quasiquote (simplify e)))