lilypond-user
[Top][All Lists]
Advanced

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

Re: Automatically identify beats


From: Aaron Hill
Subject: Re: Automatically identify beats
Date: Sat, 06 Oct 2018 22:19:23 -0700
User-agent: Roundcube Webmail/1.3.6

On 2018-10-06 10:01 am, Daniel Sales wrote:
Hi, is it possible to identify the beats on each different timing
automatically, in order to set a different color for strong, medium
and weak beats ?

something like

\coloredBeats <color_for_strong_beat> <color_for_medium_beat>
<color_for_weak_beat> (black as default)
...

For example, the expected output for

\score {
  \new Staff  {
    \coloredBeats #red #green #black
    \time 2/4 a4 b |
    \time 3/4 a4 b c |
    \time 4/4 a4 b c d |
    \time 6/8 f8 g a b c d
  }
}

 would be equivalent to:

strongBeat = {
    \override NoteHead.color = #red
    \override Stem.color = #red
}
mediumBeat = {
    \override NoteHead.color = #green
    \override Stem.color = #green
}
weakBeat = {
    \override NoteHead.color = #black
    \override Stem.color = #black
}
\score {
  \new Staff  {
    \time 2/4 \strongBeat a4 \weakBeat b |
    \time 3/4 \strongBeat a4 \mediumBeat b \weakBeat c |
    \time 4/4 \strongBeat a4 \weakBeat b \mediumBeat c \weakBeat d |
    \time 6/8 \strongBeat f8 \mediumBeat g \weakBeat a \strongBeat b
\mediumBeat c \weakBeat d
  }
}

Hi Daniel,

The biggest hurdle as I see it is computationally determining what is meant by "strong", "medium", and "weak". Those labels might have an intuitive understanding by musicians, but for computers they require additional input.

For instance, 6/8 timing could be thought of as "1 2 3 / 4 5 6" or "1 2 / 3 4 / 5 6". Aside from other possible subdivisions, these two timing options each could have different degrees of emphasis applied (e.g. "S M W / S M W", "S W W / M W W", "S W / M W / M W" ...). As such, the time signature alone is not enough information for a computer to be able to color note heads correctly.

So, let me propose an alternative to your original interface that will hopefully be both useful and easier to implement/maintain:

%%%%
\version "2.19.82"

#(define (rational-color-pair? x)
  (and (pair? x) (rational? (car x)) (color? (cdr x))))
#(define (rational-color-pair-list? x)
  (and (list? x) (rational-color-pair? (car x))
    (or (null? (cdr x)) (rational-color-pair-list? (cdr x)))))
colorizeBeats = #(define-music-function (args)
  (rational-color-pair-list?)
  "Conditionally overrides the color of the note head based on
   its position within a measure."
  #{ \override NoteHead.color = #(lambda (grob)
       (let* ((moment (grob::rhythmic-location grob))
              (matches? (lambda (m r)
                (eqv? 0 (ly:moment-main-numerator
                  (ly:moment-mod m (ly:make-moment r))))))
              (color (assoc (cdr moment) args matches?)))
        (if color (cdr color)))) #} )

{
  \colorizeBeats #(list (cons 1/2 (x11-color 'red))
                        (cons 3/8 (x11-color 'orange)))
  \time 4/4 \repeat unfold 16 { b'8 }
  \colorizeBeats #(list (cons 3/8 (x11-color 'red))
                        (cons 3/16 (x11-color 'orange)))
  \time 6/8 \repeat unfold 2 { b'8 8 8 8. 8. }
  \colorizeBeats #(list (cons 1/4 (x11-color 'red))
                        (cons 1/12 (x11-color 'orange)))
  \time 5/4 \repeat unfold 2 { b'2 \tuplet 3/2 { 8 8 8 } 4 8 8 }
}
%%%%

This approach does not attempt to parse the time signature but rather requires the user to provide a list of time intervals and their desired color. Note that the first rule in the list that matches will be applied, so the ordering within the list is important.

In the example provided, the first section highlights in red every half note (or more precisely, every note that *aligns* to a half note) and in orange every dotted quarter. The second highlights dotted quarters and dotted eighths in a similar manner. The third example shows the effect working with tuplets by coloring so-called "twelfth" notes.

-- Aaron Hill

Attachment: beatcolor.cropped.png
Description: PNG image


reply via email to

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