Hi Lukas, hi Aaron,

This is looking marvellous! I’ve modified Lukas’ version a bit so that we can 
use tags to control if events are sent and if they are removed in their 
original location. Also if we define multiple (named) channels we can send 
events to a specific channel.

Cheers,
Valentin
%%%%
\version "2.22.0"

newChannel =
#(define-scheme-function (tag) (symbol?)
   (cons tag (ly:make-dispatcher)))

#(define (channel? chan)
   (and
    (pair? chan)
    (ly:dispatcher? (cdr chan))))
   

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

sendToChannel =
#(define-music-function (chan event-type) (channel? symbol?)
   (define (event-proc ev)
    (if (or (member 'send (ly:event-property ev 'tags)) (member (symbol-append 'send (car chan)) (ly:event-property ev 'tags)))
        (begin
         (let* ((evc (ly:event-deep-copy ev)))
           (if (ly:event-property evc 'sentandremoved #f)
               (ly:event-set-property! evc 'tweaks (delete1! (cons 'stencil #f) (ly:event-property evc 'tweaks))))
           (ly:broadcast (cdr chan) evc)
           (if (and (not (ly:event-property ev 'sentandremoved #f)) (not (member 'sendkeep (ly:event-property ev 'tags))))
               (begin
                 (ly:event-set-property! ev 'tweaks (cons (cons 'stencil #f) (ly:event-property ev 'tweaks)))
                 (ly:event-set-property! ev 'sentandremoved #t)))))))
   (define (context-proc ctxt)
    (ly:add-listener event-proc (ly:context-events-below ctxt) event-type))
   #{ \applyContext #context-proc #})

send = \tag #'send \etc
myChannel = \newChannel #'A
myChannelB = \newChannel #'B
myOtherChannel = \newChannel #'C
sendA = \tag #'sendA \etc
sendB = \tag #'sendB \etc
sendC = \tag #'sendC \etc
keep = \tag #'sendkeep \etc

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

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

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

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

Attachment: signature.asc
Description: This is a digitally signed message part.

Reply via email to