Hello Paul, > I currently find writing a *SkipToBar* function very helpful for certain > things like tempi which only happen occasionally. > > I would like to have template so I could write a function which knows that > the time signature changes on a particular bar. I have tried writing scheme > to do it, but I’m afraid I have never been able to get to grips with it > properly.
There is a structural problem here: A music function does (usually) no know what time signature it is in. While it can try to take time change events into account that happen in the music it has no way of knowing what initial time it is in nor can it account for time signature being changed in different locations. So a better place for this would be the translation stage, since here we have contexts and an actual concept of measures. Now, it would in theory be possible to have a music with a special iterator that broadcasts events based on the current position. But see appended an approach using an engraver. This file registers a new context property called `position-events` that is an alist mapping bar numbers to alists of measure positions (as moment or as fractions) to lists of stream events (or alternatively just one stream event or even only mapping bar number to one or multiple events, which would be interpreted as bar position 0). So possible values for it are mappings bar-number -> barPosition -> (event1 event2 ...) bar-number -> barPosition -> event bar-number -> (event1 event2 ...) bar-number -> event Then we add an engraver to (efficiently) look up events for the current measure and position and broadcast them (on the way making sure that all positions specified actually schedule a timestep). This way we can then quite simply say: In measure X change tempo, in measure Y, one half note in add a dynamic marking, or something. Cheers, Tina
#(define (position-event-dispatcher context)
(let ((last-measure -inf.0)
(current-info #f) ; last seen position-events
(current-head #f)) ; sorted tail of position events
; Check if position-events info has changed and if so reset states
(define (check-info i)
(if (not (eq? i current-info))
(begin
(set! current-info i)
(if (list? current-info)
(set! current-head
(sort current-info
(lambda (x y) (< (car x) (car y)))))
(set! current-head #f)))))
; Get specification for measure m from sorted tail. Also sets current-head
; to the first note with measure >= m
(define (head-get-measure m)
(if (null? current-head)
#f
(cond ((> (caar current-head) m) #f)
((= (caar current-head) m) (cdar current-head))
(else
(set! current-head (cdr current-head))
(head-get-measure m)))))
(make-engraver
((start-translation-timestep engraver)
(let
((events (ly:context-property context 'position-events #f)))
; If position events have changes reset states
(check-info events)
(if events ; No need to do anything if no events are defined
(let*
((current-measure (ly:context-property context 'currentBarNumber))
; Naively we'd be doing (assoc-get current-measure events)
; But this would require iterating the list for each timestep
; Intead we assume measures are non-decreasing and start
; iteration at the point we last stopped, turning this from
; O(n_steps * n_eventmeasures) to O(n_steps + n_eventmeasures) and
; (assuming there must be at least as many timesteps as there are
; measures) thus O(n_steps).
(this-measure-events (head-get-measure current-measure)))
(if
this-measure-events
(begin
; Measure events should be an alist of events per measure position
; Alternatively enter a single event (this-measure-events is an event)
; or a list (elements in this-measure-events are not pairs, but events)
; In this case they are interpreted as measure position 0.
(if
(or (ly:stream-event? this-measure-events)
(any ly:stream-event? this-measure-events))
(set! this-measure-events (list (cons 0 this-measure-events))))
; When first reaching this target schedule all partial moments
; to make sure they are handled
(if (!= current-measure last-measure)
(let* ((all-moments-r (map car this-measure-events))
(all-moments
(map
(lambda (m) (if (ly:moment? m) m (ly:make-moment m)))
all-moments-r))
(all-moments (delete-duplicates! all-moments equal?))
(current-moment (ly:context-current-moment context))
(mpos (ly:context-property context 'measurePosition))
(basepos (ly:moment-sub current-moment mpos))
(all-moments
(filter (lambda (m) (ly:moment<? mpos m))
all-moments))
(all-moments-abs
(map (lambda (x)
(ly:moment-add basepos x))
all-moments)))
(for-each
(lambda (m)
(ly:context-schedule-moment context m))
all-moments-abs)))
(set! last-measure current-measure)
; Extract and broadcast events for the current moment
(let*
((pos (ly:context-property context 'measurePosition))
(this-pos-events (assoc-get pos this-measure-events '()))
(this-pos-num-events ; this handles moment specified as fraction
(assoc-get (ly:moment-main pos) this-measure-events '()))
(dispatcher (ly:context-event-source context)))
; If at a time only one event is needed don't require a list:
(if (ly:stream-event? this-pos-events)
(set! this-pos-events (list this-pos-events)))
(if (ly:stream-event? this-pos-num-events)
(set! this-pos-num-events (list this-pos-num-events)))
(for-each
(lambda (evt)
(ly:broadcast dispatcher evt))
(append this-pos-events this-pos-num-events))))))))))))
#(set-object-property! 'position-events
'translation-type?
list?)
\layout {
\context {
\Voice
\consists #position-event-dispatcher
}
}
{
\set position-events =
#`((4 . ,(ly:make-stream-event
(ly:make-event-class 'tempo-change-event)
`((metronome-count . 60) (tempo-unit . ,(ly:make-duration 2)) (text . ()))))
(6 .
((0
. ,(ly:make-stream-event
(ly:make-event-class 'tempo-change-event)
`((metronome-count . 100) (tempo-unit . ,(ly:make-duration 2)) (text . ()))))
(1/2
. ,(ly:make-stream-event
(ly:make-event-class 'absolute-dynamic-event)
`((text . "pp")))))
))
c'1 d' e' f' e' d'1
}
signature.asc
Description: This is a digitally signed message part.
