lilypond-devel
[Top][All Lists]
Advanced

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

Re: New feature: automatically invert chords or drop/rise chord notes (i


From: dak
Subject: Re: New feature: automatically invert chords or drop/rise chord notes (issue 365840043 by address@hidden)
Date: Mon, 28 Jan 2019 13:53:04 -0800

On 2019/01/28 20:02:53, Valentin Villenave wrote:

I can’t figure out how to make it work without resetting the
EventChord’s
elements list. How would you proceed?

Without any pointer to what you are having problems with, this is
essentially "do it yourself".  Sigh.  I don't even understand what those
interfaces are supposed to be good for so I at least changed the
internals to stop chaotically mixing directions and counts.  The
user-level commands remain as chaotic as I find them to be.

Try this patched-up variant of code posted in the mailing list.

%%%%%%%%%%%%%%%%%%%%%%%%%

\version "2.21.0"

#(define-public (move-chord-note n octs)
   (_i "Transpose a note (numbered as @var{n}) by @var{octs} octaves.
@var{n} is zero-based and can be negative to count from the end.")
   (lambda (music)
     (define (proper-pitch note)
       (let* ((oct (ly:music-property note 'octavation))
              (pitch (ly:music-property note 'pitch)))
         (if (number? oct)
             (ly:pitch-transpose pitch (ly:make-pitch oct 0))
             pitch)))
     (if (music-is-of-type? music 'event-chord)
         (let* ((elts (extract-typed-music music 'note-event))
                (l (length elts))
                ;; if direction is up, count from the bottom note upward,
                ;; if direction is down, count from the top note downward.
                (count-from (if (negative? n) (+ l n) n))
                ;; Notes may not have been entered from bottom to top;
                ;; extract the pitches and put them in order.
                (notes (sort-list elts
                                  (lambda (a b)
                                    (ly:pitch<? (proper-pitch a) (proper-pitch 
b))))))
           (if (< -1 count-from l)
               (let* ((note (list-ref elts count-from))
                      (oct (ly:music-property note 'octavation 0)))
                 (ly:music-transpose note (ly:make-pitch octs 0))
                 (set! (ly:music-property note 'octavation) (+ oct octs))))))
     music))

dropNote =
#(define-music-function (num music) (integer? ly:music?)
   (_i "Drop a note of any chords in @var{music}, in @var{num}
position from above.")
   (music-map (move-chord-note (- num) -1) music))

raiseNote =
#(define-music-function (parser location num music) (integer? ly:music?)
   (_i "Raise a note of any chords in @var{music}, in @var{num}
position from below.")
   (music-map (move-chord-note (1- num) 1) music))

invertChords =
#(define-music-function (num music) (integer? ly:music?)
   (_i "Invert any chords in @var{music} into their @var{num}-th
position.
(Chord inversions may be directed downwards using negative integers.)")
   (let loop ((num num) (music music))
     (cond ((zero? num) music)
       ((negative? num) (loop (1+ num) (dropNote 1 music)))
       (else (loop (1- num) (raiseNote 1 music))))))


ac =
\chordmode {
  \dropNote 2 c:maj
  \raiseNote 1 d:m7
  \invertChords 2 e:m7
}

<<
  \chords { \ac }
  {
    <>^\markup \sans "(should be: C△, Dm7 and Em7)"
    \ac  \bar "||"
  }


%%%%%%%%%%%%%%%%%


https://codereview.appspot.com/365840043/

reply via email to

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