Discussion:
get current bar number
Kevin Barry
2018-03-23 00:55:54 UTC
Permalink
Hi All,

I would like to be able to get the current bar number context property so
that I can do something with it (like printing it in markup for example).

I have tried the following scheme function:
(ly:context-property 'Score 'currentBarNumber)
but it errors out, telling me that Score is not a context. Do I need to get
the context object somehow?

Any help appreciated.

Kevin
Jan-Peter Voigt
2018-03-23 06:21:05 UTC
Permalink
Hi Kevin,

yes, you need to get a context object. If you want to use it inside some
music expression \applyContext is the key:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
doSomething =
\applyContext #(lambda (context)
(display
(ly:context-property context 'currentBarNumber)
))

{ c''4 \doSomething d'' }
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

If you are going to write an engraver you can find some informations in
the archive.

HTH
Jan-Peter
Post by Kevin Barry
Hi All,
I would like to be able to get the current bar number context property
so that I can do something with it (like printing it in markup for example).
(ly:context-property 'Score 'currentBarNumber)
but it errors out, telling me that Score is not a context. Do I need to
get the context object somehow?
Any help appreciated.
Kevin
_______________________________________________
lilypond-user mailing list
https://lists.gnu.org/mailman/listinfo/lilypond-user
Wols Lists
2018-03-23 13:50:29 UTC
Permalink
Post by Jan-Peter Voigt
If you are going to write an engraver you can find some informations in
the archive.
Also look at the rehearsal mark engraver. One of the options is to use
the bar number as the rehearsal mark, so that might give you some ideas.

Cheers,
Wol
Kevin Barry
2018-03-24 23:04:53 UTC
Permalink
Hi Jan/Wols,

Thank you for responding. I have tinkered with \applyContext and
cannot get the property out of it to do anything useful with it. Since
it always returns music I can't feed it to a markup command or convert
it to markup or set it as the value for a property (using a callback
or something like that). I have tried using it to set! another
property and print that, but it doesn't work.

I tried looking at the code for the rehearsal mark engraver, but it's
in C++ and I don't understand it (and I suspect context objects are in
the #included contexts.hh). I also looked at the code for the
Measure_counter_engraver, which is in scheme, but it takes a context
object as its parameter, which doesn't seem to get me any closer.

I must be misunderstanding something. My best attempt is below. Any
further help would be appreciated.

#(define-markup-command (print-bar-number layout props) ()
(define barnum 0)
(let* ((setbarnum (lambda (context)
(set! barnum
(ly:context-property context 'currentBarNumber)))))
(make-apply-context setbarnum)
(interpret-markup layout props
(markup (number->string barnum)))))

\repeat unfold 3 { b1^\markup \print-bar-number }

Kevin
Thomas Morley
2018-03-25 18:46:36 UTC
Permalink
Post by Kevin Barry
Hi Jan/Wols,
Thank you for responding. I have tinkered with \applyContext and
cannot get the property out of it to do anything useful with it. Since
it always returns music I can't feed it to a markup command or convert
it to markup or set it as the value for a property (using a callback
or something like that). I have tried using it to set! another
property and print that, but it doesn't work.
I tried looking at the code for the rehearsal mark engraver, but it's
in C++ and I don't understand it (and I suspect context objects are in
the #included contexts.hh). I also looked at the code for the
Measure_counter_engraver, which is in scheme, but it takes a context
object as its parameter, which doesn't seem to get me any closer.
I must be misunderstanding something. My best attempt is below. Any
further help would be appreciated.
#(define-markup-command (print-bar-number layout props) ()
(define barnum 0)
(let* ((setbarnum (lambda (context)
(set! barnum
(ly:context-property context 'currentBarNumber)))))
(make-apply-context setbarnum)
(interpret-markup layout props
(markup (number->string barnum)))))
\repeat unfold 3 { b1^\markup \print-bar-number }
Kevin
How about:

foo =
\applyOutput Voice.TextScript
#(lambda (grob ctx c)
(ly:grob-set-property!
grob
'text
(format #f "\ncurrentBarNumber: ~a"
(ly:context-property ctx 'currentBarNumber))))

{
\textLengthOn
\repeat unfold 3 { \foo c1^"" }
}

HTH,
Harm
Kevin Barry
2018-03-29 17:05:57 UTC
Permalink
That worked perfectly!

Thanks to all for responding.
Post by Kevin Barry
Post by Kevin Barry
Hi Jan/Wols,
Thank you for responding. I have tinkered with \applyContext and
cannot get the property out of it to do anything useful with it. Since
it always returns music I can't feed it to a markup command or convert
it to markup or set it as the value for a property (using a callback
or something like that). I have tried using it to set! another
property and print that, but it doesn't work.
I tried looking at the code for the rehearsal mark engraver, but it's
in C++ and I don't understand it (and I suspect context objects are in
the #included contexts.hh). I also looked at the code for the
Measure_counter_engraver, which is in scheme, but it takes a context
object as its parameter, which doesn't seem to get me any closer.
I must be misunderstanding something. My best attempt is below. Any
further help would be appreciated.
#(define-markup-command (print-bar-number layout props) ()
(define barnum 0)
(let* ((setbarnum (lambda (context)
(set! barnum
(ly:context-property context
'currentBarNumber)))))
Post by Kevin Barry
(make-apply-context setbarnum)
(interpret-markup layout props
(markup (number->string barnum)))))
\repeat unfold 3 { b1^\markup \print-bar-number }
Kevin
foo =
\applyOutput Voice.TextScript
#(lambda (grob ctx c)
(ly:grob-set-property!
grob
'text
(format #f "\ncurrentBarNumber: ~a"
(ly:context-property ctx 'currentBarNumber))))
{
\textLengthOn
\repeat unfold 3 { \foo c1^"" }
}
HTH,
Harm
Loading...