Haipins ending with text: scheme help

classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

Haipins ending with text: scheme help

Antonio Gervasoni
This post was updated on .
Hi everyone,

I'm working on a score where I need hairpins to end on a specific text. I do this because I don't really like the hairpins with a circle tip. I prefer to use "n." for niente and "pd." for perdendosi (the first one for strings, the second one for winds).

Now, on some occasions the hairpin must end with the text placed just before the bar line, like this

As I find the typical solution of creating a second voice with spacers rather clumsy, I managed to come up with a better one by tweaking the snippet for placing text under a hairpin. However, I'm know nothing of scheme so my solution is far from perfect. The code is here:

hairpinWithRightText =
#(define-music-function (parser location text) (markup?)
#{
  \once \override Voice.Hairpin.after-line-breaking =
    #(lambda (grob)
      (let* ((stencil (ly:hairpin::print grob))
             (new-stencil (ly:stencil-aligned-to
               (ly:stencil-combine-at-edge
                 (ly:stencil-aligned-to stencil X RIGHT)
                 Y CENTER
                 (ly:stencil-aligned-to (grob-interpret-markup grob text) X CENTER))
               X LEFT))
             (staff-space (ly:output-def-lookup (ly:grob-layout grob) 'staff-space))
             (staff-line-thickness
               (ly:output-def-lookup (ly:grob-layout grob) 'line-thickness))
             (grob-name (lambda (x) (assq-ref (ly:grob-property x 'meta) 'name)))
             (par-x (ly:grob-parent grob X))
             (dyn-text (eq? (grob-name par-x) 'DynamicText ))
             (dyn-text-stencil-x-length
               (if dyn-text
                 (interval-length
                   (ly:stencil-extent (ly:grob-property par-x 'stencil) X))
                 0))
             (x-shift
               (if dyn-text
                 (-
                   (+ staff-space dyn-text-stencil-x-length)
                   (* 0.5 staff-line-thickness)) 0)))

      (ly:grob-set-property! grob 'Y-offset 0)
      (ly:grob-set-property! grob 'stencil
         (ly:stencil-translate-axis
          new-stencil
          x-shift X))))
#})

hairpinN = {
        \override Hairpin #'bound-padding = #2.3
        \once \hairpinWithRightText \markup { \italic "      n." }
}

hairpinPD = {
        \override Hairpin #'bound-padding = #3.2
        \once \hairpinWithRightText \markup { \italic "        pd." }
}

If you check the original code for this snippet, you will see that I just changed the word "dir" for CENTER, on the 10th line of hairpinWithRightText. I have then defined the two hairpins and placed a lot of spaces before each text. Then, I have increased the padding of the hairpins so that they leave enough space for the text. The result is here

Not a bad result, but the "pd." is not properly aligned (verticaly). See how the "pd." is placed when attached to a rest here

You can see the vertical alignment is not the same. It's a tiny difference but I would like it to be perfect. Also, my solution is not really very elegant, is it? Can anyone who knows scheme find a better way to do this? Something simple, effective and accurate? I would appreciate very much any help on this.

Antonio
P.S.: this could also be used to end hairpins with normal dynamic text, thus avoiding the tiresome solution of writing a second voice with spacers.
Reply | Threaded
Open this post in threaded view
|

Re: Haipins ending with text: scheme help

Thomas Morley-2
2014-07-29 6:59 GMT+02:00 Antonio Gervasoni <[hidden email]>:
> Hi everyone,
>
> I'm working on a score where I need hairpins to end on a specific text. I do
> this because I don't really like the hairpins with a circle tip. I prefer to
> use "n." for niente and "pd." for perdendosi (the first one for strings, the
> second one for winds).
>
> Now, on some occasions the hairpin must end with the text placed just before
> the bar line, like this:

All links to images are broken in your _mail_
I can follow them only on nabble.

> <http://lilypond.1069038.n5.nabble.com/file/n165055/Screen_Shot_2014-07-28_at_11.png>
>
> As I find the typical solution of creating a second voice with spacers
> rather clumsy, I managed to come up with a better one by tweaking the
> snippet for placing text under a hairpin. However, I'm know nothing of
> scheme so my solution is far from perfect. The code is here:
[...]


How about the code below. Works with linebreaking hairpins as well.
Needs some real life testing though.

\version "2.18.0"

%% from output-lib.scm, extended to get the possibility to return other
%% hairpin-length than default
#(define ((elbowed-hairpin coords x-length-corr mirrored?) grob)
  "Create hairpin based on a list of @var{coords} in @code{(cons x y)}
form.  @code{x} is the portion of the width consumed for a given line
and @code{y} is the portion of the height.  For example,
@code{'((0.3 . 0.7) (0.8 . 0.9) (1.0 . 1.0))} means that at the point
where the hairpin has consumed 30% of its width, it must
be at 70% of its height.  Once it is to 80% width, it
must be at 90% height.  It finishes at
100% width and 100% height.  @var{mirrored?} indicates if the hairpin
is mirrored over the Y-axis or if just the upper part is drawn.
Returns a function that accepts a hairpin grob as an argument
and draws the stencil based on its coordinates.
The length of that hairpin may be adjusted with @var{x-length-corr}
@lilypond[verbatim,quote]
#(define simple-hairpin
  (elbowed-hairpin '((1.0 . 1.0)) #t))

\\relative c' {
  \\override Hairpin #'stencil = #simple-hairpin
  a\\p\\< a a a\\f
}
@end lilypond
"
  (define (pair-to-list pair)
    (list (car pair) (cdr pair)))
  (define (normalize-coords goods x y)
    (map
     (lambda (coord)
       (cons (* x (car coord)) (* y (cdr coord))))
     goods))
  (define (my-c-p-s points thick decresc?)
    (make-connected-path-stencil
     points
     thick
     (if decresc? -1.0 1.0)
     1.0
     #f
     #f))
  ;; outer let to trigger suicide
  (let ((sten (ly:hairpin::print grob)))
    (if (grob::is-live? grob)
        (let* ((decresc? (eq? (ly:grob-property grob 'grow-direction) LEFT))
               (thick (ly:grob-property grob 'thickness 0.1))
               (thick (* thick (layout-line-thickness grob)))
               (x-ext (ly:stencil-extent sten X))
               (xex (cons (car x-ext) (- (cdr x-ext) x-length-corr)))
               (lenx (interval-length xex))
               (yex (ly:stencil-extent sten Y))
               (leny (interval-length yex))
               (xtrans (+ (car xex) (if decresc? lenx 0)))
               (ytrans (car yex))
               (uplist (map pair-to-list
                            (normalize-coords coords lenx (/ leny 2))))
               (downlist (map pair-to-list
                              (normalize-coords coords lenx (/ leny -2)))))
          (ly:stencil-translate
           (ly:stencil-add
            (my-c-p-s uplist thick decresc?)
            (if mirrored? (my-c-p-s downlist thick decresc?) empty-stencil))
           (cons xtrans ytrans)))
        '())))

#(define (shortened-hairpin corr)
  (elbowed-hairpin '((0 . 0) (1.0 . 1.0)) corr #t))

#(define (hairpin-with-right-text text grob)
  "Rebuild a hairpin and add @var{text} to the right.
  The hairpin is shortened by the length of @var{text},
  @code{bound-padding} is taken into account"
      (let* ((text-stil (grob-interpret-markup grob text))
             (text-stil-x-extent (ly:stencil-extent text-stil X))
             (text-stil-length (interval-length text-stil-x-extent))
             (staff-space
               (ly:output-def-lookup (ly:grob-layout grob) 'staff-space))
             (bound-padding
               (ly:grob-property grob 'bound-padding staff-space))
             (x-corr (+ text-stil-length (/ bound-padding 2))))

      (ly:grob-set-property! grob 'stencil (shortened-hairpin x-corr))

        (let* ((stencil (ly:grob-property grob 'stencil))
               (stil-x-ext
                 (ordered-cons
                   (car (ly:stencil-extent stencil X))
                   (cdr (ly:stencil-extent stencil X))))
               (new-stencil
                 (ly:stencil-add
                   (ly:stencil-aligned-to stencil Y CENTER)
                   (ly:stencil-translate-axis
                     (ly:stencil-aligned-to text-stil Y CENTER)
                     (+ (cdr stil-x-ext) bound-padding)
                     X))))

        (ly:grob-set-property! grob 'stencil new-stencil))))

#(define (hairpin-with-right-text-callback txt)
"
 Returns a stencil build by @code{hairpin-with-right-text} for unbroken or the
 last of broken hairpins.
"
  (lambda (grob)
    (let* ((orig (ly:grob-original grob))
           (siblings (ly:spanner-broken-into orig)))
       (if (or (null? siblings)
               (equal? grob (car (last-pair siblings))))
           (hairpin-with-right-text txt grob)))))


hairpinWithRightText =
#(define-music-function (parser location text) (markup?)
"
 Returns a hairpin with text at the right.
"
#{
  \once \override Voice.Hairpin.after-line-breaking =
    #(hairpin-with-right-text-callback text)
#})


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% EXAMPLES
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

  \new Staff \relative c' {
      \hairpinWithRightText \markup \italic "pd."
      cis''2(\> c~ c1) d\!
  }

<<
  \new Staff \relative c' {
      \hairpinWithRightText \markup \italic "dal niente"
      cis''2(\pppp\> c~
      \break
      c2 b)\!
  }
  \new Staff \relative c' {
      \hairpinWithRightText \markup \italic "pd."
      cis''2(\pppp\> c~
      c2 b)\!
  }
>>

<<
  \new Staff \relative c' {
      \hairpinWithRightText \markup \italic "molto"
      f'2(\fff\< e~
      e) r\!
  }
  \new Staff \relative c' {
      \hairpinWithRightText \markup \dynamic "pp"
      f'2(\fff\> e~
      e) r\!
  }
>>

HTH,
  Harm

_______________________________________________
lilypond-user mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/lilypond-user
Reply | Threaded
Open this post in threaded view
|

Re: Haipins ending with text: scheme help

Antonio Gervasoni
Thank you so much, Thomas! I'll try it right away and will post the results.

Yes, I'm aware of the broken links. I was unable to upload the images on my first attempt (on Nabble) so I modified my post and included links to files in my Dopbox instead. I couldn't find a way to do the same on Gmane.

For anyone else that might follow this thread, the images are here, here and here.

Thanks again!

Antonio
Reply | Threaded
Open this post in threaded view
|

Re: Haipins ending with text: scheme help

Antonio Gervasoni
Thomas,

I tried your solution. It's definitely far better than mine. Works flawlessly with "pd." but not so well with "n.", which now experiences the same vertical displacement. Here is how it looks when the "n." is placed as a dynamic text attached to a rest, and here how it looks with the code you provided.

Of course, the difference is minimal,.... it's not so disturbing as the other one, so I suppose I can live with that. LOL

On the other hand, is there a way to increase a little bit the space before the bar line? I feel the text is too close to it.

Also, could you recommend a good source to learn Scheme?

Thank you very much for your help!

Sincerely,

Antonio
Reply | Threaded
Open this post in threaded view
|

Re: Haipins ending with text: scheme help

Antonio Gervasoni
This post was updated on .
Oops... I did it again! The order of the images is inverted... so sorry!

Antonio
Reply | Threaded
Open this post in threaded view
|

Re: Haipins ending with text: scheme help

Thomas Morley-2
In reply to this post by Antonio Gervasoni
2014-07-29 20:56 GMT+02:00 Antonio Gervasoni <[hidden email]>:

> Thomas,
>
> I tried your solution. It's definitely far better than mine. Works
> flawlessly with "pd." but not so well with "n.", which now experiences the
> same vertical displacement.  Here
> <https://dl.dropboxusercontent.com/u/4857747/d.png>   is how it looks when
> the "n." is placed as a dynamic text attached to a rest, and  here
> <https://dl.dropboxusercontent.com/u/4857747/e.png>   how it looks with the
> code you provided.
>
> Of course, the difference is minimal,.... it's not so disturbing as the
> other one, so I suppose I can live with that. LOL
>
> On the other hand, is there a way to increase a little bit the space before
> the bar line? I feel the text is too close to it.
>
> Also, could you recommend a good source to learn Scheme?
>
> Thank you very much for your help!
>
> Sincerely,
>
> Antonio

Hi Antonio,

could you provide tiny code-examples causing the difference?
No need to reinclude my scheme-coding. Just how you use it.

Cheers,
  Harm

_______________________________________________
lilypond-user mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/lilypond-user
Reply | Threaded
Open this post in threaded view
|

Re: Haipins ending with text: scheme help

Antonio Gervasoni
Hi Thomas,

Sure! Here it is:


%%% Definition of the two types of hairpin


hairpinN = \hairpinWithRightText \markup \italic "n."

hairpinPD = \hairpinWithRightText \markup \italic "pd."


%%% Example


{

\new Staff \relative c''' {

a1(\p\<

gis2.)\mf\> r4\n

a1(\p\<

dis2.)\mf\> r4\n

e2(\p\< dis4 e \break

f2 g

\hairpinN

gis1\mp\>)

R1\!

R1

}

}


Best,


Antonio



On Tue, Jul 29, 2014 at 2:28 PM, Thomas Morley-2 [via Lilypond] <[hidden email]> wrote:
2014-07-29 20:56 GMT+02:00 Antonio Gervasoni <[hidden email]>:

> Thomas,
>
> I tried your solution. It's definitely far better than mine. Works
> flawlessly with "pd." but not so well with "n.", which now experiences the
> same vertical displacement.  Here
> <https://dl.dropboxusercontent.com/u/4857747/d.png>   is how it looks when
> the "n." is placed as a dynamic text attached to a rest, and  here
> <https://dl.dropboxusercontent.com/u/4857747/e.png>   how it looks with the
> code you provided.

>
> Of course, the difference is minimal,.... it's not so disturbing as the
> other one, so I suppose I can live with that. LOL
>
> On the other hand, is there a way to increase a little bit the space before
> the bar line? I feel the text is too close to it.
>
> Also, could you recommend a good source to learn Scheme?
>
> Thank you very much for your help!
>
> Sincerely,
>
> Antonio
Hi Antonio,

could you provide tiny code-examples causing the difference?
No need to reinclude my scheme-coding. Just how you use it.

Cheers,
  Harm

_______________________________________________
lilypond-user mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/lilypond-user



If you reply to this email, your message will be added to the discussion below:
http://lilypond.1069038.n5.nabble.com/Haipins-ending-with-text-scheme-help-tp165055p165085.html
To unsubscribe from Haipins ending with text: scheme help, click here.
NAML

Reply | Threaded
Open this post in threaded view
|

Re: Haipins ending with text: scheme help

Thomas Morley-2
2014-07-29 21:44 GMT+02:00 Antonio Gervasoni <[hidden email]>:
> Hi Thomas,
>
> Sure! Here it is:
>

Hi Antonio,

your example is not very helpful, because it does not show "a dynamic
text attached to a rest".
And it does not compile, because of the unknown \n

>
> %%% Definition of the two types of hairpin
>
>
> hairpinN = \hairpinWithRightText \markup \italic "n."
>
> hairpinPD = \hairpinWithRightText \markup \italic "pd."
>
>
> %%% Example
>
>
> {
>
> \new Staff \relative c''' {
>
> a1(\p\<
>
  gis2.)\mf\> r4\n <------ ??

>
> a1(\p\<
>
> dis2.)\mf\> r4\n
>
> e2(\p\< dis4 e \break
>
> f2 g
>
> \hairpinN
>
> gis1\mp\>)
>
> R1\!
>
> R1
>
> }
>
> }
>
Anyway, I added some code, so that the added _text_ is always
vertically aligned to the _hairpin_ in the same manner (regarding its
baseline). At least for most european languages.

See attached.

HTH,
  Harm

_______________________________________________
lilypond-user mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/lilypond-user

hairpin-with-text-right.ly (7K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Haipins ending with text: scheme help

Antonio Gervasoni
> your example is not very helpful, because it does not show "a dynamic 
> text attached to a rest". 
> And it does not compile, because of the unknown \n 

Aw! I forgot that! It's on the file called by \include. Both dynamics are defined on this way:

n = #(make-dynamic-script #{ \markup \line { \normal-text \normalsize \italic "n." } #})

pd = #(make-dynamic-script #{ \markup \line { \normal-text \normalsize \italic "pd." } #})


After that, the \n will appear under a rest, on the second and fourth bars. I apologise for the mistake.


Thank you so much. I'll try it now.


Best,


Antonio



On Tue, Jul 29, 2014 at 3:58 PM, Thomas Morley-2 [via Lilypond] <[hidden email]> wrote:
2014-07-29 21:44 GMT+02:00 Antonio Gervasoni <[hidden email]>:
> Hi Thomas,
>
> Sure! Here it is:
>

Hi Antonio,

your example is not very helpful, because it does not show "a dynamic
text attached to a rest".
And it does not compile, because of the unknown \n

>
> %%% Definition of the two types of hairpin

>
>
> hairpinN = \hairpinWithRightText \markup \italic "n."
>
> hairpinPD = \hairpinWithRightText \markup \italic "pd."
>
>
> %%% Example
>
>
> {
>
> \new Staff \relative c''' {
>
> a1(\p\<
>
  gis2.)\mf\> r4\n <------ ??

>
> a1(\p\<
>
> dis2.)\mf\> r4\n
>
> e2(\p\< dis4 e \break
>
> f2 g
>
> \hairpinN
>
> gis1\mp\>)
>
> R1\!
>
> R1
>
> }
>
> }
>
Anyway, I added some code, so that the added _text_ is always
vertically aligned to the _hairpin_ in the same manner (regarding its
baseline). At least for most european languages.

See attached.

HTH,
  Harm

_______________________________________________
lilypond-user mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/lilypond-user



If you reply to this email, your message will be added to the discussion below:
http://lilypond.1069038.n5.nabble.com/Haipins-ending-with-text-scheme-help-tp165055p165091.html
To unsubscribe from Haipins ending with text: scheme help, click here.
NAML

Reply | Threaded
Open this post in threaded view
|

Re: Haipins ending with text: scheme help

Antonio Gervasoni
In reply to this post by Thomas Morley-2
Works perfectly! You are a genius!

Thanks again!

You should publish it on the snippet repository. It's far better than creating a second voice with spacers, as is suggested there (or somewhere else... I can't remember now)

Best regards,

Antonio


On Tue, Jul 29, 2014 at 5:25 PM, Antonio Gervasoni <[hidden email]> wrote:
> your example is not very helpful, because it does not show "a dynamic 
> text attached to a rest". 
> And it does not compile, because of the unknown \n 

Aw! I forgot that! It's on the file called by \include. Both dynamics are defined on this way:

n = #(make-dynamic-script #{ \markup \line { \normal-text \normalsize \italic "n." } #})

pd = #(make-dynamic-script #{ \markup \line { \normal-text \normalsize \italic "pd." } #})


After that, the \n will appear under a rest, on the second and fourth bars. I apologise for the mistake.


Thank you so much. I'll try it now.


Best,


Antonio



On Tue, Jul 29, 2014 at 3:58 PM, Thomas Morley-2 [via Lilypond] <[hidden email]> wrote:
2014-07-29 21:44 GMT+02:00 Antonio Gervasoni <[hidden email]>:
> Hi Thomas,
>
> Sure! Here it is:
>

Hi Antonio,

your example is not very helpful, because it does not show "a dynamic
text attached to a rest".
And it does not compile, because of the unknown \n

>
> %%% Definition of the two types of hairpin

>
>
> hairpinN = \hairpinWithRightText \markup \italic "n."
>
> hairpinPD = \hairpinWithRightText \markup \italic "pd."
>
>
> %%% Example
>
>
> {
>
> \new Staff \relative c''' {
>
> a1(\p\<
>
  gis2.)\mf\> r4\n <------ ??

>
> a1(\p\<
>
> dis2.)\mf\> r4\n
>
> e2(\p\< dis4 e \break
>
> f2 g
>
> \hairpinN
>
> gis1\mp\>)
>
> R1\!
>
> R1
>
> }
>
> }
>
Anyway, I added some code, so that the added _text_ is always
vertically aligned to the _hairpin_ in the same manner (regarding its
baseline). At least for most european languages.

See attached.

HTH,
  Harm

_______________________________________________
lilypond-user mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/lilypond-user



If you reply to this email, your message will be added to the discussion below:
http://lilypond.1069038.n5.nabble.com/Haipins-ending-with-text-scheme-help-tp165055p165091.html
To unsubscribe from Haipins ending with text: scheme help, click here.
NAML


Reply | Threaded
Open this post in threaded view
|

Re: Haipins ending with text: scheme help

Thomas Morley-2



2014-07-30 0:39 GMT+02:00 Antonio Gervasoni <[hidden email]>:
Works perfectly! You are a genius!

Thanks again!

You're welcome

You should publish it on the snippet repository.


I'm one of the LSR-editors and can promise a fast approval ;)

Cheers,
  Harm

_______________________________________________
lilypond-user mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/lilypond-user
Reply | Threaded
Open this post in threaded view
|

Re: Haipins ending with text: scheme help

Antonio Gervasoni
How about you put it in yourself?

I just did. :-)

I'm one of the LSR-editors and can promise a fast approval ;)

Wow! Excellent!

Best,

Antonio


On Tue, Jul 29, 2014 at 5:59 PM, Thomas Morley-2 [via Lilypond] <[hidden email]> wrote:



2014-07-30 0:39 GMT+02:00 Antonio Gervasoni <[hidden email]>:

Works perfectly! You are a genius!

Thanks again!

You're welcome

You should publish it on the snippet repository.

How about you put it in youself?
http://lsr.di.unimi.it/LSR/html/contributing.html

I'm one of the LSR-editors and can promise a fast approval ;)

Cheers,
  Harm

_______________________________________________
lilypond-user mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/lilypond-user



If you reply to this email, your message will be added to the discussion below:
http://lilypond.1069038.n5.nabble.com/Haipins-ending-with-text-scheme-help-tp165055p165099.html
To unsubscribe from Haipins ending with text: scheme help, click here.
NAML