[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: change clef inside a grob-callback?
From: |
Mark Polesky |
Subject: |
Re: change clef inside a grob-callback? |
Date: |
Fri, 17 Jul 2009 11:58:00 -0700 (PDT) |
Okay, I feel like I could be a lot closer. I rewrote the function using
\applyOutput and a music-function.
Everything is fine except:
1) using \ottava confuses the function, and
2) the clef changes one chord too late.
At the moment, the \ottava issue is not a problem (I'll deal with that
later), but obviously the delayed clef change *is* a problem. In the
example below, the 3rd chord should be in the bass clef already because
the average staff-position is below middle C. I'm hoping I can use the
parser argument to get around it, but I don't know how. If there's some
way I could get \applyOutput to see the previous chord...
Can someone help?
Thanks.
- Mark
_________________
\version "2.13.2"
#(define (first-note? note-grob)
(let* ((note-column (ly:grob-parent note-grob X))
(note-grob-array (ly:grob-object note-column 'note-heads))
(first-note (ly:grob-array-ref note-grob-array 0)))
(eq? note-grob first-note)))
#(define (get-avg-staff-pos note-grob)
(let* ((note-column (ly:grob-parent note-grob X))
(note-grob-array (ly:grob-object note-column 'note-heads))
(note-count (ly:grob-array-length note-grob-array)))
;; maybe there should be a ly:grob-array-map procedure...
(let loop ((staff-pos-sum 0) (i 0))
(if (>= i note-count)
(/ staff-pos-sum note-count)
(loop (+ staff-pos-sum
(ly:grob-staff-position
(ly:grob-array-ref note-grob-array i)))
(1+ i))))))
#(define (ly:context-set-properties! context alist)
(for-each
(lambda (prop)
(ly:context-set-property! context (car prop) (cdr prop)))
alist))
#(define (auto-clef grob grob-origin context)
(if (and (grob::has-interface grob 'note-head-interface)
(first-note? grob))
(if (< (get-avg-staff-pos grob)
(ly:context-property context 'middleCPosition))
(ly:context-set-properties! context
'((clefGlyph . "clefs.F")
(clefPosition . 2)
(middleCPosition . 6)))
(ly:context-set-properties! context
'((clefGlyph . "clefs.G")
(clefPosition . -2)
(middleCPosition . -6))))))
#(define (EventChord? music)
(eq? (ly:music-property music 'name) 'EventChord))
autoClefs =
#(define-music-function (parser location music) (ly:music?)
(music-map
(lambda (x)
(if (EventChord? x)
#{ \applyOutput #'Staff #auto-clef $x #}
x))
music))
\relative {
\autoClefs { <c e> <b d> <a c> <g b> }
}