lilypond-user
[Top][All Lists]
Advanced

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

Re: Variable assignment in music functions


From: David Kastrup
Subject: Re: Variable assignment in music functions
Date: Thu, 03 Nov 2011 17:04:41 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.0.90 (gnu/linux)

Michael Ellis <address@hidden> writes:

> What's the right way to define a function that assigns string values
> to LilyPond variables? I'm trying to create a function in an include
> file that allows me to assign different values to variables used as
> midi instrument specifiers.
> %--------------------------------------------------------
> setMainCueClapInstruments=
> #(define-music-function (p l main cue clap) (string? string? string?)
>    #{
>        mainInstrument = #$main
>        cueInstrument  = #$cue
>        clapInstrument = #$clap
>    #}
>    (make-music 'SequentialMusic 'void #t))
> \setMainCueClapInstruments #"cello" #"acoustic grand" #"woodblock"
> %--------------------------------------------------------
> But the parser throws errors, starting with the following ...
> Parsing...
> <string>:2:7: error: syntax error, unexpected STRING
>
>        mainInstrument = #lilyvartmpbg

A music function can only do things you could also do inside of music.
Assignments are _not_ permitted in music.  Music is something you can
put into music variables and shuffle around.  Assignments are acted on
immediately.

What you _can_ put into music are property overrides and sets: those
happen at the time they are replayed, and are wrapped into music events.

You can, of course, just use ly:parser-define! inside of your function
to manipulate variables.  But they will get changed at the _location_
you call the music function, not at the _time_ the music expression is
executed.  If you put setMainCueClapInstruments into a music variable
then, the effect will occur at the time you define the music variable,
not at the time you use it.

If you want the latter, you need to go through properties.  Or even
\ApplyToContext, but that's really obscure.

-- 
David Kastrup




reply via email to

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