\version "2.19.10" #(define (ledger-line-no middle-C-pos p) "Returns the number of ledger-lines a pitch @var{p} will have with middle C position @var{middle-C-pos} expressed as staff-steps from the middle staff line." (let* ((ps (ly:pitch-steps p)) (mid-staff-steps (- middle-C-pos)) (top-line (+ mid-staff-steps 4)) (bottom-line (- mid-staff-steps 4)) (above? (> ps top-line)) (below? (< ps bottom-line)) (steps-outside-staff (cond (below? (- ps bottom-line)) (above? (- ps top-line)) (else 0)))) (truncate (/ steps-outside-staff 2)))) octavate = #(define-music-function (parser location threshold mus) (integer? ly:music?) "Create an ottava for notes which have at least @var{threshold} ledger lines" (let ((e (ly:music-property mus 'element)) (elts (ly:music-property mus 'elements))) (define (build-new-elts mus-expr new-expr start-loco? start-ottava?) (if (null? mus-expr) ;; ensure that ottava does not extend past a localized ;; use of \octavate (append new-expr (list (make-music 'OttavaMusic 'ottava-number 0))) (let ((p (ly:music-property (car mus-expr) 'pitch))) (cond ((not (ly:pitch? p)) (build-new-elts (cdr mus-expr) (append new-expr (list (car mus-expr))) #t #t)) ((and (ly:pitch? p) start-ottava? (>= (ledger-line-no -6 p) threshold)) (build-new-elts (cdr mus-expr) (append new-expr (list (make-music 'OttavaMusic 'ottava-number 1)) (list (car mus-expr))) #t #f)) ((and (ly:pitch? p) start-loco? (< (ledger-line-no -6 p) threshold)) (build-new-elts (cdr mus-expr) (append new-expr (list (make-music 'OttavaMusic 'ottava-number 0)) (list (car mus-expr))) #f #t)) (else (build-new-elts (cdr mus-expr) (append new-expr (list (car mus-expr))) #t #t)))))) (define (recurse music) (let ((elts (ly:music-property music 'elements)) (e (ly:music-property music 'element))) (if (ly:music? e) (recurse e)) (if (pair? elts) (if (any (lambda (elt) (music-is-of-type? elt 'note-event)) elts) (set! (ly:music-property music 'elements) (build-new-elts elts '() #t #t)) (map recurse elts))))) (recurse mus) mus)) %%%%%%%%%%% EXAMPLE %%%%%%%%%%%% music = \relative c''' { \repeat volta 2 { a8 b c d e f g a } } musictwo = \new PianoStaff << \new Staff { \music R1 } \new Staff { \unfoldRepeats \transpose c e { \music } } >> { \octavate #5 \music \octavate #4 \music \octavate #3 \music \octavate #2 \music } { \octavate #3 \musictwo }