Hi,

On Wed, Oct 8, 2014 at 9:48 AM, David Bellows <davebell...@gmail.com> wrote:

> >Here's a preliminary experiment.  It will add an automatic \ottava 1
> before passages where notes have at least a specified number of ledger
> lines.
>
>
The attached function will work with more complex music expressions.

The only problem here is with the handling of chords.  Right now, the
function either ignores them or attempts to add the ottava within the
EventChord expression (which causes an error).  This shouldn't be too hard
to fix, though it will take some thought.

Hope this proves useful!

David
\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
}
_______________________________________________
lilypond-user mailing list
lilypond-user@gnu.org
https://lists.gnu.org/mailman/listinfo/lilypond-user

Reply via email to