1 ;;; display-lily.scm -- Display music expressions using LilyPond notation
5 ;;; (c) 2005--2006 Nicolas Sceaux <nicolas.sceaux@free.fr>
8 ;;; - This file defines the procedures used to define display methods for each
9 ;;; music type: define-display-method and define-extra-display-method.
10 ;;; See scm/define-music-display-methods.scm
11 ;;; Display methods are stored in the `display-methods' property of each music
14 ;;; - `display-lily-music' can be called to display a music expression using
15 ;;; LilyPond notation. `music->lily-string' return a string describing a music
16 ;;; expression using LilyPond notation.
18 ;;; - `with-music-match' can be used to destructure a music expression, extracting
19 ;;; some interesting music properties.
22 (define-module (scm display-lily)
23 #:use-module (ice-9 optargs)
24 #:use-module (ice-9 format)
25 #:use-module (ice-9 regex)
26 #:use-module (ice-9 pretty-print)
27 #:use-module (srfi srfi-1)
28 #:use-module (srfi srfi-13)
29 #:use-module (srfi srfi-39)
31 #:use-syntax (srfi srfi-39)
32 #:use-syntax (ice-9 optargs))
34 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
36 ;;; Display method definition and call
40 (define-macro (define-display-method music-type vars . body)
41 "Define a display method for a music type and store it in the
42 `display-methods' property of the music type entry found in the
43 `music-name-to-property-table' hash table. Print methods previously
44 defined for that music type are lost.
45 Syntax: (define-display-method MusicType (expression)
47 `(let ((type-props (hashq-ref music-name-to-property-table
52 (assoc-set! type-props 'display-methods (list method)))
53 (hashq-set! music-name-to-property-table
58 (define-macro (define-extra-display-method music-type vars . body)
59 "Add a display method for a music type. A primary display method
60 is supposed to have been previously defined with `define-display-method'.
61 This new method should return a string or #f. If #f is returned, the next
62 display method will be called."
63 `(let* ((type-props (hashq-ref music-name-to-property-table
65 (methods (assoc-ref type-props 'display-methods))
66 (new-method (lambda ,vars
69 (assoc-set! type-props
71 (cons new-method methods)))
72 (hashq-set! music-name-to-property-table
77 (define* (tag->lily-string expr #:optional (post-event? #f))
80 (format #f "~a\\tag #'~a" (if post-event? "-" "") tag))
81 (ly:music-property expr 'tags))))
83 (define-public (music->lily-string expr)
84 "Print expr, a music expression, in LilyPond syntax"
86 (let* ((music-type (ly:music-property expr 'name))
87 (procs (assoc-ref (hashq-ref music-name-to-property-table
90 (result-string (and procs (any (lambda (proc)
95 (tag->lily-string expr (post-event? expr))
97 (format #f "%{ Print method not implemented for music type ~a %}"
99 (format #f "%{ expecting a music expression: ~a %}" expr)))
101 (define*-public (display-lily-music expr #:key force-duration)
102 (parameterize ((*indent* 0)
103 (*previous-duration* (ly:make-duration 2))
104 (*force-duration* force-duration))
105 (display (music->lily-string expr))
108 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
110 ;;; Music pattern matching
114 (and (symbol? x) (char=? #\? (string-ref (symbol->string x) 0))))
117 (and (pair? x) (eqv? (car x) 'music)))
119 (define (music-list? x)
123 (define (music-or-var-list? x)
126 (or (music? e) (var? e)))
129 (define (key-val-list->alist lst)
130 (define (key-val-list->alist-aux lst prev-result)
133 (key-val-list->alist-aux (cddr lst)
134 (cons (cons (first lst) (second lst))
136 (reverse! (key-val-list->alist-aux lst (list))))
138 (define (gen-condition expr pattern)
139 "Helper function for `with-music-match'.
140 Generate an form that checks if the properties of `expr'
141 match thoses desscribed in `pattern'."
142 (let* (;; all (property . value) found at the first depth in pattern,
143 ;; including a (name . <Musictype>) pair.
144 (pat-all-props (cons (cons 'name (second pattern))
145 (key-val-list->alist (cddr pattern))))
146 ;; all (property . value) pairs found in pattern, where value is not
147 ;; a ?var, a music expression or a music list.
148 (prop-vals (remove (lambda (kons)
149 (or (var? (cdr kons))
151 (music-or-var-list? (cdr kons))))
153 ;; list of (property . element) pairs, where element is a music expression
154 (element-list (filter (lambda (kons) (music? (cdr kons)))
156 ;; list of (property . (e1 e2 ..)) pairs, where (e1 e2 ...) is a
157 ;; list a music expressions
158 (elements-list (filter (lambda (kons) (music-or-var-list? (cdr kons)))
161 ;; a form that checks that `expr' is a music expression
162 ;; before actually accessing its properties...
164 ;; a form that checks that `expr' properties have the same
165 ;; values as those given in `pattern'
166 ,@(map (lambda (prop-val)
167 (let ((prop (car prop-val))
168 (val (cdr prop-val)))
169 `(and (not (null? (ly:music-property ,expr ',prop)))
170 (equal? (ly:music-property ,expr ',prop) ,val))))
172 ;; build the test condition for each element found in a (property . element) pair.
173 ;; (typically, property will be 'element)
174 ,@(map (lambda (prop-element)
175 (gen-condition `(ly:music-property ,expr ',(car prop-element)) (cdr prop-element)))
177 ;; build the test conditions for each element found in a (property . (e1 e2 ...)) pair.
178 ;; this requires accessing to an element of a list, hence the index.
179 ;; (typically, property will be 'elements)
180 ,@(map (lambda (prop-elements)
183 `(and ,@(map (lambda (e)
184 (set! index (1+ index))
186 (gen-condition `(and (> (length (ly:music-property ,expr ',(car prop-elements)))
188 (list-ref (ly:music-property ,expr ',(car prop-elements))
192 (cdr prop-elements)))))
195 (define (gen-bindings expr pattern)
196 "Helper function for `with-music-match'.
197 Generate binding forms by looking for ?var symbol in pattern."
198 (let* (;; all (property . value) found at the first depth of pattern,
199 ;; including a (name . <Musictype>) pair.
200 (pat-all-props (cons (cons 'name (second pattern))
201 (key-val-list->alist (cddr pattern))))
202 ;; all (property . ?var) pairs
203 (prop-vars (filter (lambda (kons) (var? (cdr kons)))
205 ;; list of (property . element) pairs, where element is a music expression
206 (element-list (filter (lambda (kons) (music? (cdr kons)))
208 ;; list of (property . (e1 e2 ..)) pairs, where (e1 e2 ...) is a
209 ;; list a music expressions
210 (elements-list (filter (lambda (kons) (music-or-var-list? (cdr kons)))
213 ;; the binding form for the ?var variable found in pattern (first depth).
214 ;; ?var is bound to the value of `expr' property
215 (map (lambda (prop-var)
216 `(,(cdr prop-var) (ly:music-property ,expr ',(car prop-var))))
218 ;; generate bindings for each element found in a (property . element) pair.
219 ;; (typically, property will be 'element)
220 (append-map (lambda (prop-element)
221 (gen-bindings `(ly:music-property ,expr ',(car prop-element))
224 ;; generate bindings for each element found in a (property . (e1 e2 ...)) pair
225 ;; (typically, property will be 'elements)
226 (append-map (lambda (prop-elements)
228 (append-map (lambda (e)
229 (set! index (1+ index))
231 `((,e (list-ref (ly:music-property ,expr ',(car prop-elements)) ,index)))
232 (gen-bindings `(list-ref (ly:music-property ,expr ',(car prop-elements))
235 (cdr prop-elements))))
238 (define-macro (with-music-match music-expr+pattern . body)
239 "If `music-expr' matches `pattern', call `body'. `pattern' should look like:
243 element (music <MusicType> ...)
244 elements ((music <MusicType> ...)
246 (music <MusicType> ...)))
247 The properties of `music-expr' are checked against the values given in the
248 pattern (the name property being the <MusicType> symbol after the `music'
249 keyword), then all music expression found in its properties (such as 'element
251 When ?var is found instead of a property value, ?var is bound that property value,
252 as read inside `music-expr'. ?var may also be used to refere to a whole music
253 expression inside an elements list for instance. These bindings are accessible
255 (let ((music-expr (first music-expr+pattern))
256 (pattern (second music-expr+pattern))
258 `(let ((,expr-sym ,music-expr))
259 (if ,(gen-condition expr-sym pattern)
260 (let ,(gen-bindings expr-sym pattern)
264 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
266 ;;; Special parameters
270 (define *indent* (make-parameter 0))
272 ;;; set to #t to force duration printing
273 (define *force-duration* (make-parameter #f))
275 ;;; last duration found
276 (define *previous-duration* (make-parameter (ly:make-duration 2)))
278 ;;; Set to #t to force a line break with some kinds of expressions (eg sequential music)
279 (define *force-line-break* (make-parameter #t))
280 (define *max-element-number-before-break* (make-parameter 6))
282 ;; \times factor (used in durations)
283 (define *time-factor-denominator* (make-parameter #f))
284 (define *time-factor-numerator* (make-parameter #f))
286 (define *parser* (make-parameter #f))
288 (define *current-context* (make-parameter 'Bottom))
290 (define *explicit-mode* (make-parameter #t))
292 (define (new-line->lily-string)
293 (format #f "~%~v_" (max 0 (1- (*indent*)))))
296 ;;; music type predicate maker
299 (define (make-music-type-predicate . music-types)
300 (define ((make-music-type-predicate-aux mtypes) expr)
303 (or (eqv? (car mtypes) (ly:music-property expr 'name))
304 ((make-music-type-predicate-aux (cdr mtypes)) expr))))
305 (make-music-type-predicate-aux music-types))
307 (load "define-music-display-methods.scm")