lilypond-user
[Top][All Lists]
Advanced

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

Re: Defining variables mid-stream in a music expression using tags.


From: Valentin Petzel
Subject: Re: Defining variables mid-stream in a music expression using tags.
Date: Thu, 18 May 2023 22:24:29 +0200

You should be aware that in pretty much any programming language there is a 
difference between

... some code ...

and

function: ... some code ...

The first will be evaulated intantly, while the latter is only evaluated when 
the function is called (but anytime the function is called!). So using 
functions allows you to control when things are evaluated. To be more specific: 
It allows us to evaluate the same thing in different manners.

Using tags will first evaluate an expression and then modify the resulting 
object as you like. But instead we can use a function to actually directly 
create the object you want. For example we can do a keepWithTag-like behaviour 
like this:

myMusic = {
  \tag #'a c'
  \tag #'b d'
  \tag #'c e'
}

\keepWithTag #'a \myMusic
\keepWithTag #'b \myMusic

myMusicII =
#(define-music-function (flags) (symbol-list-or-symbol?)
   (if (symbol? flags) (set! flags (list flags)))
   #{
     {
       $(if (member 'a flags) #{ c' #})
       $(if (member 'b flags) #{ d' #})
       $(if (member 'c flags) #{ e' #})
     }
   #})

\myMusicII #'a
\myMusicII #'b

Of course this is a rather stupid example, but you can do much more complex 
stuff this way than what tags allow you to do.

Cheers,
Valentin

Am Donnerstag, 18. Mai 2023, 17:46:18 CEST schrieb dfro:
> Valentin,
> 
> Thank you, for the  explanation! I see what you mean about controlling
> when evaluation happens by avoiding global variables that change during
> the flow of a music expression. I will avoid doing that.
> 
> I see now that it is better to create two or more static global
> variables and switch music/markup function behavior by calling different
> global variables as the input variable to a music or markup function in
> the music expression. I have been doing it that way.
> 
>   Could you please show me what you mean by the quote below from your
> response? That sounds interesting, but I am not getting it.
> 
> On 5/18/23 06:31, Valentin Petzel wrote:
> > More generally if you want have parts of your score behave in a
> > conditionally different manner it may also be an option to wrap your
> > whole music expression into a markup function which behaves differently
> > based on some parameter.
> The \keepWithTag #'... command outside the music expression when the
> staves are created, is the only way I know of switching behavior of
> markup functions between staves, while using the same music expression.
> 
> I would like to be able to declare a variable that has a scope at the
> staff level, and effects the markup/music functions within that staff
> and not other staves.
> 
> I hope that I am not belaboring the point. The method with the static
> global variables, tags, music function, and markup function works. I am
> just wondering if there is a simpler way.
> 
> 
> Peace,
> 
> David

Attachment: signature.asc
Description: This is a digitally signed message part.


reply via email to

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