1 ;;;; This file is part of LilyPond, the GNU music typesetter.
3 ;;;; Copyright (C) 2005--2014 Erik Sandberg <mandolaerik@gmail.com>
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.
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.
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/>.
19 (use-modules (srfi srfi-1))
21 ;; Event class hierarchy. Each line is on the form (Parent . (List of children))
23 '((() . (StreamEvent))
26 ChangeParent Override Revert UnsetProperty SetProperty
27 music-event OldMusicEvent CreateContext Prepare
28 OneTimeStep Finish GraceChange))
29 (music-event . (annotate-output-event
30 footnote-event arpeggio-event breathing-event
31 extender-event span-event rhythmic-event dynamic-event
32 break-event label-event percent-event key-change-event
33 string-number-event stroke-finger-event tie-event
34 part-combine-event part-combine-force-event
35 beam-forbid-event script-event tempo-change-event
36 tremolo-event bend-after-event fingering-event
37 glissando-event harmonic-event hyphen-event
38 laissez-vibrer-event mark-event multi-measure-text-event
39 note-grouping-event pes-or-flexa-event repeat-tie-event
40 spacing-section-event layout-instruction-event
42 completize-extender-event break-span-event alternative-event))
44 (layout-instruction-event . (apply-output-event))
45 (script-event . (articulation-event text-script-event))
46 (part-combine-event . (solo-one-event solo-two-event unisono-event))
47 (break-event . (line-break-event page-break-event page-turn-event))
48 (dynamic-event . (absolute-dynamic-event))
49 (span-event . (span-dynamic-event
50 beam-event episema-event ligature-event
51 measure-counter-event pedal-event
52 phrasing-slur-event slur-event
53 staff-span-event text-span-event
54 trill-span-event tremolo-span-event
56 (span-dynamic-event . (decrescendo-event crescendo-event))
57 (break-span-event . (break-dynamic-span-event))
58 (pedal-event . (sostenuto-event sustain-event una-corda-event))
59 (rhythmic-event . (lyric-event
60 melodic-event multi-measure-rest-event
61 double-percent-event percent-event
62 repeat-slash-event rest-event
63 skip-event bass-figure-event))
64 (melodic-event . (cluster-note-event note-event))
66 (Announcement . (AnnounceNewContext))
69 (define-public (event-class-cons class parent classlist)
70 (let ((lineage (assq parent classlist)))
73 (if (not (null? parent))
74 (ly:warning (_ "unknown parent class `~a'") parent))
77 (acons class lineage classlist)
78 (fold (lambda (elt alist)
79 (acons elt lineage alist))
82 (define all-event-classes
83 (fold (lambda (elt classlist)
84 (event-class-cons (cdr elt) (car elt) classlist))
87 ;; Maps event-class to a list of ancestors (inclusive)
88 (define ancestor-lookup (make-hash-table (length all-event-classes)))
90 (define (ancestor-lookup-initialize)
91 (hash-clear! ancestor-lookup)
92 (for-each (lambda (ent) (hashq-set! ancestor-lookup (car ent) ent))
95 (ancestor-lookup-initialize)
96 (call-after-session ancestor-lookup-initialize)
98 ;; Each class will be defined as
99 ;; (class parent grandparent .. )
100 ;; so that (eq? (cdr class) parent) holds.
102 (define-public (define-event-class class parent)
103 "Defines a new event @code{class} derived from @code{parent}, a
104 previously defined event class."
105 (let ((parentclass (ly:make-event-class parent)))
107 ((ly:make-event-class class)
108 (ly:error (_ "Cannot redefine event class `~S'") class))
110 (ly:error (_ "Undefined parent event class `~S'" parentclass)))
112 (hashq-set! ancestor-lookup
114 (cons class parentclass))))
117 ;; TODO: Allow entering more complex classes, by taking unions.
118 (define-public (ly:make-event-class leaf)
119 (hashq-ref ancestor-lookup leaf))
121 (define-public (ly:in-event-class? ev cl)
122 "Does event @var{ev} belong to event class @var{cl}?"
123 (memq cl (ly:event-property ev 'class)))
125 ;; does this exist in guile already?
126 (define (map-tree f t)
129 (map (lambda (x) (map-tree f x)) t))
131 (cons (map-tree f (car t)) (map-tree f (cdr t))))
134 ;; expand each non-leaf subtree to (root . children), recursively
135 (define (expand-event-tree root)
136 (let ((children (assq root event-classes)))
138 (cons root (map expand-event-tree (cdr children)))
141 ;; produce neater representation of music event tree.
142 ;; TODO: switch to this representation for the event-classes list?
143 (define music-event-tree (expand-event-tree 'music-event))
144 (define (sort-tree t)
145 (define (stringify el)
148 (symbol->string (first el))))
150 (sort (map (lambda (el)
152 (cons (car el) (sort-tree (cdr el)))
155 (lambda (a b) (string<? (stringify a) (stringify b))))
158 ;;(use-modules (ice-9 pretty-print))
159 ;;(pretty-print (cons (car music-event-tree) (sort-tree (cdr music-event-tree))))
161 (defmacro-public make-stream-event (expr)
162 (ly:stream-event::undump (primitive-eval (list 'quasiquote expr))))
164 (define* (simplify e)
166 ;; Special case for lists reduces stack consumption.
167 ((list? e) (map simplify e))
168 ((pair? e) (cons (simplify (car e))
170 ((ly:stream-event? e)
171 (list 'unquote (list 'make-stream-event (simplify (ly:stream-event::dump e)))))
173 (list 'unquote (music->make-music e)))
175 (list 'unquote `(ly:make-moment
176 ,(ly:moment-main-numerator e)
177 ,(ly:moment-main-denominator e)
178 . ,(if (zero? (ly:moment-grace-numerator e))
180 (list (ly:moment-grace-numerator e)
181 (ly:moment-grace-denominator e))))))
183 (list 'unquote `(ly:make-duration
185 ,(ly:duration-dot-count e)
186 ,(ly:duration-scale e))))
188 (list 'unquote `(ly:make-pitch
190 ,(ly:pitch-notename e)
191 ,(ly:pitch-alteration e))))
192 ((ly:input-location? e)
193 (list 'unquote '(ly:dummy-input-location)))
196 (define-public (ly:simplify-scheme e)
197 (list 'quasiquote (simplify e)))