\version "2.18.2" % Counting notes %<<< #(begin ; manual counting (define music-note-count 0) (define czero (define-music-function (parser location) () (set! music-note-count 0) #{ #})) (define cset (define-music-function (parser location x) (number?) (set! music-note-count x) #{ #} )) (define (make-count-markup n) (markup #:fontsize -1 #:bold #:sans (number->string n))) (define counter-manual (define-music-function (parser location) () (set! music-note-count (+ 1 music-note-count)) (make-music 'TextScriptEvent 'direction 1 'text (make-count-markup music-note-count)) )) ; automatic counting (define counter-symbol 'Counter) (define counter-auto (make-music 'TextScriptEvent 'direction 1 'text counter-symbol)) (define acount counter-auto) (define count counter-manual) (define (substitute-count music n) (let* ( (name (ly:music-property music 'name)) (prop (ly:music-mutable-properties music)) (out (list name)) (add-out! (lambda (s v) (set! out (append! out (list s v))))) ) (map (lambda (x) (let* ( (s (car x)) (v (cdr x)) ) ; inner let* (if (eq? s 'element) (set! v (substitute-count v n))) (if (member s '(elements articulations)) (set! v (map (lambda (y) (substitute-count y n)) v))) (if (and (eq? name 'TextScriptEvent) (eq? s 'text) (eq? v counter-symbol)) (set! v (make-count-markup n))) (add-out! s v))) ; lambda prop) (apply make-music out) )) (define (repeat-with-count n music) (make-music 'SequentialMusic 'elements (map (lambda (i) (substitute-count music (+ 1 i))) (iota n)) ) ) (define repeatWithCount (define-music-function (parser location n music) (number? ly:music?) (make-music 'SequentialMusic 'elements (map (lambda (i) (substitute-count music (+ 1 i))) (iota n)) ) )) ) % begin %>>> foo = { a4^\count a^\count a^\count } qux = \repeatWithCount 5 { c'^\acount } \foo \qux