Hi Michael,

over time, I found that doing something like this in an engraver (as opposed to a music function) is actually much easier and conceptually clear, in spite of the seeming difficulty of the engraver syntax. The advantage of using an engraver being that you see the "actual" pitches and don't have to fight with problems of \relative, \transpose and so on.

\version "2.24.0"

pitch-replace-dictionary =
#(list
  (cons #{ c #} #{ cis #})
  (cons #{ d #} #{ des #})
  )

#(define (pitch-class= p q)
   (and
    (= (ly:pitch-notename p) (ly:pitch-notename q))
    (= (ly:pitch-alteration p) (ly:pitch-alteration q))))

Pitch_replace_engraver =
#(lambda (context)
   (make-engraver
    (listeners
     ((note-event engraver event)
      (let*
       ((pitch (ly:event-property event 'pitch))
        (rule (assoc pitch pitch-replace-dictionary pitch-class=)))

       (if rule
           (ly:event-set-property!
            event 'pitch
            (ly:make-pitch (ly:pitch-octave pitch)
                           (ly:pitch-notename (cdr rule))
                           (ly:pitch-alteration (cdr rule))))))))))

\layout {
  \context {
    \Score
    \consists #Pitch_replace_engraver
  }
}

\relative {
  c'4 d e c8 8
  \transpose f c \relative {
    f'4 g a
  }
}

This engraver can also be added to just a single score (\layout inside \score {}) or even a single Staff or Voice. At the moment, the replacement dictionary is global, but this could be changed if needed.

At the moment the mechanism I chose is too crude to do replacements that involve changing the pitch-octave (eg from c to b,). Do you need this?

Lukas

Am 31.08.23 um 12:53 schrieb Michael Winter via LilyPond user discussion:
I would like to do something (hopefully simple), which is basically a custom find and replace for a set of notes in an entire score.

For example {c cis d dis fih g aih} -> {c cis dih dis f gis a}

So basically on arbitrary list of pitches / scale to another.

Is this possible without writing a custom function. If not, any hints on how to tackle the problem would be much appreciated. Thanks in advance.

-Michael

Reply via email to