lilypond-user
[Top][All Lists]
Advanced

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

Re: Will my scheme function work have unintended consequences? Adding st


From: David Kastrup
Subject: Re: Will my scheme function work have unintended consequences? Adding staccatos to notes.
Date: Fri, 14 Sep 2018 23:18:11 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux)

Peter Engelbert <address@hidden> writes:

> Hello all,
>
> I've written the following functions to add staccatos to an arbitrary number 
> of notes:
>
> ===addStaccatos.ly=
> \version "2.19.82"
>
> #(define (add-staccato note-event)
>   (set! (ly:music-property note-event 'articulations)
>    (cons (make-music 'ArticulationEvent 'articulation-type "staccato")
>     (ly:music-property note-event 'articulations))))
>
> addStaccatos =
> #(define-music-function (music) (ly:music?)
>    (map add-staccato (ly:music-property music 'elements)) music)
> ===================
>
> Here is the MWE I've used to test it:
>
> =====test.ly=======
> \version "2.19.82"
> \include "addStaccatos.ly"
>
> { c4 \addStaccatos { c4 c4 c4 | c4^> } }
> ===================
>
> I am just checking, with more experienced eyes, whether or not this
> function will work in all cases.

No.

> It seems to work in my minimal example, but I really don't know very
> much about how scheme represents music, so I'm concerned that there
> might be some cases where this will not work.

Music can be structured in a large number of ways.

> Also, I tried a few earlier attempts at this before getting it to
> work, one of them used the MUSIC-MAP function.  What does MUSIC-MAP
> do,

Apply to all contained music.  Not what you want here.

> and how does it differ from the standard MAP?

It descends into music rather than a list.  And at all levels.

> And most importantly, what is its proper syntax? I looked it up in the
> .scm file where it's defined, but it's a bit beyond me at the moment.

I think this is rather a case for map-some-music where you can stop the
recursion at appropriate levels.

\version "2.19.82"

#(define (add-staccato event prop)
   (set! (ly:music-property event prop)
         (append
          (ly:music-property event prop)
          (list
           (make-music 'ArticulationEvent 'articulation-type "staccato"))))
   event)

addStaccatos =
#(define-music-function (music) (ly:music?)
   (define note? (music-type-predicate 'note-event))
   (define chord? (music-type-predicate 'event-chord))
   (define rhythmic-event? (music-type-predicate 'rhythmic-event))
   (map-some-music
    (lambda (m)
      (cond ((note? m) (add-staccato m 'articulations))
            ((chord? m)
             (if (or (any note? (ly:music-property m 'elements))
                     (ly:duration? (ly:music-property m 'duration)))
                 (add-staccato m 'elements)
                 m))
            ((rhythmic-event? m) m)
            (else #f)))
    music))

%=====test.ly=======
%\version "2.19.82"
%\include "addStaccatos.ly"

{ c4 \addStaccatos { c4 c4 <c e>4 | c4^> } }
%===================
Note that recursion is stopped at rhythmic events, single note events
get staccato added (at their end) in 'articulations, chords containing
note events (or being a repeat chord, notable by having a duration) also
get a staccato.  There are a number of code remnants using event-chord
as a useless wrapper for non-chords, so I don't just embellish chords
unconditionally here.  Most of the time, you should be fine just
checking for event-chord.  But you asked about a general robust
solution, so...

-- 
David Kastrup

reply via email to

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