The attached file is good enough for me.

It includes (duration-equals?) which may be of general interest.

It also includes the \sw function, which, given a sequence, returns two
tagged sequences:

  \sw {
    \relative c' {
      a'4 a8 b a fs e d |
    }
  }

becomes

  {
     \tag #'layout {
       \relative c' {
         a'4 a8 b a fs e d |
       }
     }
     \tag #'midi {
       \relative c' {
         a'4
         \times 2/3 a4 \times 2/3 b8
         \times 2/3 a4 \times 2/3 fs8
         \times 2/3 e4 \times 2/3 d8 |
       }
     }
  }

It can then be used with

 \keepWithTag #'layout \theTune
 \keepWithTag #'midi \theTune

Caveats:

1) The swung result is suitable for MIDI output, but not for print
layout; as it was done by tweaking internals, it lacks the proper
triplet brackets around the pairs of notes.

2) This is strictly sufficient to my needs.  In particular, \sw assumes
that its content is a relative sequence, and will produce odd results
otherwise.  It does work with chords, as long as all the notes in the
chord are the same duration.  It does not handle un-paired eighth notes.
 It probably handles eighth rests, but I haven’t checked yet.

If anyone wants to make this a more general and better utility, please
feel free.

~Chris
-- 
Chris Maden, text nerd  <URL: http://crism.maden.org/ >
Does this mean we can have our civil liberties back now?
GnuPG Fingerprint: C6E4 E2A9 C9F8 71AC 9724 CAA3 19F8 6677 0077 C319
\version "2.12.3"

% Written by Chris Maden, <URL: http://crism.maden.org/ >.
% Released into the public domain.
% Disclaimer: good enough for me; may not be good enough for you.

% (duration-equals? dur1 dur2)
% Returns #t if all duration components are equal, #f otherwise.
#(define (duration-equals? dur1 dur2)
  (let ((factor1 (ly:duration-factor dur1))
        (factor2 (ly:duration-factor dur2)))
   (and
    (=
     (ly:duration-log dur1)
     (ly:duration-log dur2))
    (=
     (ly:duration-dot-count dur1)
     (ly:duration-dot-count dur2))
    (=
     (car factor1)
     (car factor2))
    (=
     (cdr factor1)
     (cdr factor2)))))

% (swing-eighth chord is-first)
% Changes the duration of all the notes in a chord.  If is-first is
% true, it alters the duration to be 2 0 2 3 (a quarter-note triplet),
% and if false, to 3 0 2 3 (an eighth-note triplet).  It does not
% actually check that the chord is composed entirely or even partly of
% eighth notes.
#(define (swing-eighth chord is-first)
  (let ((duration (if is-first
                   (ly:make-duration 2 0 2 3)
                   (ly:make-duration 3 0 2 3))))
   (make-music
    'EventChord
    'elements
    (map (lambda (note)
          (ly:music-set-property! note 'duration duration)
          note)
     (ly:music-property chord 'elements)))))

% (swing-eighths straight swung in-pair)
% Recurses through a set of straight notes, building up a swung
% series.  Uses in-pair as an internal semaphore.
% Does not work with unpaired eighth notes, nor does it look for
% chords with different duration notes.
#(define (swing-eighths straight swung in-pair)
  (if (null? straight)
   swung
   (let
    ((event (car straight)))
    (let
     ((is-eighth (and
                  (eq?
                   (ly:music-property
                    event
                    'name)
                   'EventChord)
                  (duration-equals?
                   (ly:music-property
                    (car (ly:music-property
                          event
                          'elements))
                    'duration)
                   (ly:make-duration 3 0 1 1)))))
     (swing-eighths
      (cdr
       straight)
      (append
       swung
       (if is-eighth
        (list (swing-eighth event (not in-pair)))
        (list event)))
      (and (not in-pair) is-eighth))))))

% \sw music
% Turns a series of notes into two tagged sequences (tagged 'layout
% and 'midi)
% The 'layout tagged sequence is unaltered.  The 'midi tagged sequence
% has pairs of eighth notes turned into swung triplets.
sw = #(define-music-function (parser location notes) (ly:music?)
       (let ((swing (make-music
                     'SequentialMusic
                     'elements
                     (list
                      (make-music
                       'RelativeOctaveMusic
                       'element
                       (make-music
                        'SequentialMusic
                        'elements
                        (swing-eighths
                         (ly:music-deep-copy
                          (ly:music-property
                           (ly:music-property
                            (car (ly:music-property
                                  notes
                                  'elements))
                            'element)
                           'elements))
                         '()
                         #f)))))))
        #{
          \tag #'layout { $notes }
          \tag #'midi { $swing }
        #}
      )
     )
_______________________________________________
lilypond-user mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/lilypond-user

Reply via email to