]> git.donarmstrong.com Git - lilypond.git/blob - scm/define-event-classes.scm
Merge branch 'master' into lilypond/translation
[lilypond.git] / scm / define-event-classes.scm
1 ;;;; This file is part of LilyPond, the GNU music typesetter.
2 ;;;;
3 ;;;; Copyright (C) 2005--2010 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
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 part-combine-event
32       beam-forbid-event script-event
33       tremolo-event bend-after-event fingering-event glissando-event
34       harmonic-event hyphen-event laissez-vibrer-event mark-event
35       multi-measure-text-event note-grouping-event 
36       pes-or-flexa-event repeat-tie-event spacing-section-event
37       layout-instruction-event completize-extender-event))
38     
39     (layout-instruction-event . (apply-output-event ))
40     (script-event . (articulation-event text-script-event))
41     (part-combine-event . (solo-one-event solo-two-event unisono-event))
42     (break-event . (line-break-event page-break-event page-turn-event))
43     (dynamic-event . (absolute-dynamic-event))
44     (span-event . (span-dynamic-event beam-event episema-event ligature-event
45                          pedal-event phrasing-slur-event slur-event staff-span-event
46                          text-span-event trill-span-event tremolo-span-event 
47                          tuplet-span-event))
48     (span-dynamic-event . (decrescendo-event crescendo-event))
49     (pedal-event . (sostenuto-event sustain-event una-corda-event))
50     (rhythmic-event . (lyric-event melodic-event multi-measure-rest-event
51                                    percent-event
52                                    rest-event skip-event bass-figure-event))
53     (melodic-event . (cluster-note-event note-event))
54     (() . (Announcement))
55     (Announcement . (AnnounceNewContext))
56     ))
57
58 ;; Maps event-class to a list of ancestors (inclusive)
59 (define ancestor-lookup (make-hash-table 11))
60
61 ;; Each class will be defined as
62 ;; (class parent grandparent .. )
63 ;; so that (eq? (cdr class) parent) holds.
64 (for-each
65  (lambda (rel)
66    (for-each
67     (lambda (type)
68       (hashq-set! ancestor-lookup type 
69                   (cons type (hashq-ref ancestor-lookup (car rel) '()))))
70     (cdr rel)))
71  event-classes)
72
73 ;; TODO: Allow entering more complex classes, by taking unions.
74 (define-public (ly:make-event-class leaf)
75  (hashq-ref ancestor-lookup leaf))
76
77 (define-public (ly:in-event-class? ev cl)
78   "Does event @var{ev} belong to event class @var{cl}?"
79   (memq cl (ly:make-event-class (ly:event-property ev 'class))))
80
81 ;; does this exist in guile already?
82 (define (map-tree f t)
83   (cond
84    ((list? t)
85     (map (lambda (x) (map-tree f x)) t))
86    ((pair? t)
87     (cons (map-tree f (car t)) (map-tree f (cdr t))))
88    (else (f t))))
89
90 ;; expand each non-leaf subtree to (root . children), recursively
91 (define (expand-event-tree root)
92   (let ((children (assq root event-classes)))
93     (if children
94         (cons root (map expand-event-tree (cdr children)))
95         root)))
96
97 ;; All leaf event classes that no translator listens to
98 ;; directly. Avoids printing a warning.
99 (define unlistened-music-event-classes
100   '(harmonic-event line-break-event page-break-event page-turn-event label-event
101                    solo-one-event solo-two-event skip-event unisono-event))
102
103 ;; produce neater representation of music event tree.
104 ;; TODO: switch to this representation for the event-classes list?
105 (define music-event-tree (expand-event-tree 'music-event))
106 (define (sort-tree t)
107   (define (stringify el)
108               (if (symbol? el)
109                   (symbol->string el)
110                   (symbol->string (first el))))
111   (if (list? t)
112       (sort (map (lambda (el)
113                    (if (list? el)
114                        (cons (car el) (sort-tree (cdr el)))
115                        el))
116                  t)
117             (lambda (a b) (string<? (stringify a) (stringify b))))
118       t))
119
120 ;;(use-modules (ice-9 pretty-print))
121 ;;(pretty-print (cons (car music-event-tree) (sort-tree (cdr music-event-tree))))
122
123 ;; check that the music event tree corresponds well with the set of
124 ;; available translators; print warnings otherwise.
125 (map-tree (lambda (sym) 
126             (if (and (symbol? sym)
127                      (not (ly:is-listened-event-class sym))
128                      (not (assq sym event-classes))
129                      (not (memq sym unlistened-music-event-classes)))
130                 (ly:programming-error (_ "event class ~A seems to be unused") sym)))      
131           music-event-tree)
132
133 (map (lambda (sym)
134        (if (not (pair? (ly:make-event-class sym)))
135            ;; should be programming-error
136            (ly:error (_ "translator listens to nonexisting event class ~A") sym)))
137      (ly:get-listened-event-classes))
138
139 (defmacro-public make-stream-event (expr)
140   (Stream_event::undump (primitive-eval (list 'quasiquote expr))))
141
142 (define* (simplify e)
143   (cond
144    ;; Special case for lists reduces stack consumption.
145    ((list? e) (map simplify e))
146    ((pair? e) (cons (simplify (car e))
147                     (simplify (cdr e))))
148    ((ly:stream-event? e)
149     (list 'unquote (list 'make-stream-event (simplify (Stream_event::dump e)))))
150    ((ly:music? e)
151     (list 'unquote (music->make-music e)))
152    ((ly:moment? e)
153     (list 'unquote `(ly:make-moment
154                      ,(ly:moment-main-numerator e)
155                      ,(ly:moment-main-denominator e)
156                      . ,(if (eq? 0 (ly:moment-grace-numerator e))
157                             '()
158                             (list (ly:moment-grace-numerator e)
159                                   (ly:moment-grace-denominator e))))))
160    ((ly:duration? e)
161     (list 'unquote `(ly:make-duration
162                      ,(ly:duration-log e)
163                      ,(ly:duration-dot-count e)
164                      ,(car (ly:duration-factor e))
165                      ,(cdr (ly:duration-factor e)))))
166    ((ly:pitch? e)
167     (list 'unquote `(ly:make-pitch
168                      ,(ly:pitch-octave e)
169                      ,(ly:pitch-notename e)
170                      ,(ly:pitch-alteration e))))
171    ((ly:input-location? e)
172     (list 'unquote '(ly:dummy-input-location)))
173    (#t e)))
174
175 (define-public (ly:simplify-scheme e)
176   (list 'quasiquote (simplify e)))
177