lilypond-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Sending around contexts


From: Lukas-Fabian Moser
Subject: Re: Sending around contexts
Date: Thu, 28 Oct 2021 22:00:30 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.13.0

Hi Aaron,

%%%%
\version "2.22.0"

#(define event-transfer-sink (ly:make-dispatcher))
eventTransferSink = \applyContext
#(lambda (ctxt)
 (ly:connect-dispatchers
  (ly:context-event-source ctxt)
  event-transfer-sink))

eventTransferSource =
#(define-music-function (event-type) (symbol?)
  (define (event-proc ev)
   (ly:broadcast event-transfer-sink (ly:event-deep-copy ev)))
  (define (context-proc ctxt)
   (ly:add-listener event-proc (ly:context-events-below ctxt) event-type))
  #{ \applyContext #context-proc #})

Wow.

That's what you might call elegant (and I have to admit that this is the first time I hear about dispatchers).

But as I imagine Kieren is going to want to use this a LOT :-), it might be reasonable to use named transfer channels:

%%%%
\version "2.22.0"

newChannel =
#(define-scheme-function () () (ly:make-dispatcher))

receiveFromChannel =
#(define-music-function (chan) (ly:dispatcher?)
   #{
     \applyContext
     #(lambda (ctxt)
        (ly:connect-dispatchers
         (ly:context-event-source ctxt)
         chan))
   #})

sendToChannel =
#(define-music-function (chan event-type) (ly:dispatcher? symbol?)
  (define (event-proc ev)
   (ly:broadcast chan (ly:event-deep-copy ev)))
  (define (context-proc ctxt)
   (ly:add-listener event-proc (ly:context-events-below ctxt) event-type))
  #{ \applyContext #context-proc #})

piano_upper = {
  c'4\p d' e' f'
  g'1\mp
  g'4\f f' e' d'
  c'1
}

piano_upper_II = {
  c'4\ff\< d' e' f'
  g'1\ffff
  g'4 f' e' d'
  c'1
}

piano_lower = {
  \clef bass
  c1
  g,1
  g,1
  c1\ff
}

myChannel = \newChannel
myOtherChannel = \newChannel

\score {
  <<
    \new PianoStaff <<
      \new Staff \with {
        \sendToChannel \myChannel dynamic-event
        \omit DynamicText
      } \piano_upper
      \new Dynamics \with { \receiveFromChannel \myChannel } { #(skip-of-length piano_upper) }
      \new Staff \with {
        \sendToChannel \myChannel dynamic-event
        \omit DynamicText
      } \piano_lower
    >>
    \new PianoStaff <<
      \new Staff \with {
        \sendToChannel \myOtherChannel dynamic-event
        \sendToChannel \myOtherChannel span-dynamic-event
        \omit DynamicText
        \omit Hairpin
      } \piano_upper_II
      \new Dynamics \with { \receiveFromChannel \myOtherChannel } { #(skip-of-length piano_upper) }
      \new Staff \with {
        \receiveFromChannel \myOtherChannel
      } \piano_lower
    >>
  >>
}
%%%%

Lukas



reply via email to

[Prev in Thread] Current Thread [Next in Thread]