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: dfro
Subject: Re: Defining variables mid-stream in a music expression using tags.
Date: Thu, 18 May 2023 21:15:08 -0400
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.10.0

On 5/18/23 16:24, Valentin Petzel wrote:
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


Valentin,

Thank you for the explanation. I see what you meant now.

My conundrum is that I want to call the same music expression (that is declared as a variable) from different staves, but also cause markup functions inside the music expression to behave differently from outside the music expression. Using tags is the only way I have found to do that. I am glad that I have a method that works. Thank you for clearly conveying to me that I should keep global variables static!

My noob thought is - why not allow custom variables to be declared when making a \new Staff in its \with statement? These variables would have the scope of working only on those calls to them that are from within that particular staff. Different staves could create the same variable with different values in their \with statement and thus change the behavior of different functions within the same called music expression. These variables would take precedence over global variables of the same name. Please, take or leave.


Peace,

David




reply via email to

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