Fragments del LilyPond: Contexts and engravers

# Contexts and engravers

Changing defaults

Contexts and engravers

## Afegir un baix xifrat a sobre o a sota de les notes

En escriure un baix xifrat, podem ubicar les xifres a sobre a o a sota de les notes del baix, mitjançant la definició de la propietat `BassFigureAlignmentPositioning #'direction` (exclusivament dins d’un context `Staff`). es pot escollir entre `#UP` (o `#1`, a sobre), `#CENTER` (o `#0`, centraat) i `#DOWN` (o `#-1`, a sota).

Aquesta propietat es pot canviar tantes vegades com volem. Utilitzeu `\once \override` si no voleu que la sobreescriptura s’apliqui a tota la partitura.

    bass = {
      \clef bass
      g4 b, c d
      e d8 c d2
    }

    continuo = \figuremode {
      <_>4 <6>4 <5/>4
      \override Staff.BassFigureAlignmentPositioning.direction = #UP
      %\bassFigureStaffAlignmentUp
      < _+ >4 <6>
      \set Staff.useBassFigureExtenders = ##t
      \override Staff.BassFigureAlignmentPositioning.direction = #DOWN
      %\bassFigureStaffAlignmentDown
      <4>4. <4>8 <_+>4
    }

    \score {
      <<
        \new Staff = bassStaff \bass
        \context Staff = bassStaff \continuo
      >>
    }

## Afegir un pentagrama nou

Es pot afegir (possiblement de forma temporal) un pentagrama nou un cop la peça ha començat.

    \score {
      <<
        \new Staff \relative c'' {
          c1 | c | c | c | c
        }
        \new StaffGroup \relative c'' {
          \new Staff {
            c1 | c
            <<
              {
                c1 | d
              }
              \new Staff {
                \once \omit Staff.TimeSignature
                c1 | b
              }
            >>
            c1
          }
        }
      >>
    }

## Afegir un pentagrama addicional a un salt de línia

En afegir un pentagrama nou a un salt de línia, per desgràcia s’afegeix un espai addicional al final de la línia abans del salt (reservat per fer lloc a un canvi d’armadura que de totes formes no s’imprimirà). La solució alternativa és afegir un ajustament per a `Staff.explicitKeySignatureVisibility` com es mostra a l’exemple.

    \score {
      \new StaffGroup \relative c'' {
        \new Staff
        \key f \major
        c1 c^"Unwanted extra space" \break
        << { c1 | c }
           \new Staff {
             \key f \major
             \once \omit Staff.TimeSignature
             c1 | c
           }
        >>
        c1 | c^"Fixed here" \break
        << { c1 | c }
           \new Staff {
             \once \set Staff.explicitKeySignatureVisibility = #end-of-line-invisible
             \key f \major
             \once \omit Staff.TimeSignature
             c1 | c
           }
        >>
      }
    }

## Canviar la direcció de la plica de les notes de tercera línia automàticament, basat en la melodia

El LilyPond pot alterar la direcció de la plica de les notes que van en la tercera línia d’un pentagrama de forma que segueixi la melodia, mitjançant l’addició del gravador `Melody_engraver` al context Voice i sobreescrivint el valor de `neutral-direction` per a l’objecte Stem (plica).

    \relative c'' {
      \time 3/4
      a8 b g f b g |
      \set suspendMelodyDecisions = ##t
      a  b g f b g |
      \unset suspendMelodyDecisions
      c  b d c b c |
    }

    \layout {
      \context {
        \Voice
        \consists "Melody_engraver"
        \autoBeamOff
      }
    }

## Nombres de compàs centrats

Sovint, les partitures d’obres per a conjunts grnas tenen els nombres de compàs a sota del sistems i centrats horitzontalment sobre l’amplada del compàs. Aquest fragment de codi mostra com pot usar-se el gravador `Measure_counter_engraver` per simular aquesta pràctica de notació. Aquí hem afegit el gravador a un context `Dynamics`.

    \layout {
      \context {
        \Dynamics
        \consists #Measure_counter_engraver
        \override MeasureCounter.direction = #DOWN
        \override MeasureCounter.font-encoding = #'latin1
        \override MeasureCounter.font-shape = #'italic
        % to control the distance of the Dynamics context from the staff:
        \override VerticalAxisGroup.nonstaff-relatedstaff-spacing.padding = #2
      }
      \context {
        \Score
        \remove "Bar_number_engraver"
      }
    }

    pattern = \repeat unfold 7 { c'4 d' e' f' }

    \new StaffGroup <<
      \new Staff {
        \pattern
      }
      \new Staff {
        \pattern
      }
      \new Dynamics {
        \startMeasureCount
        s1*7
        \stopMeasureCount
      }
    >>

## Modificar la sortida MIDI perquè tingui un canal per a cada veu

En produir una sortida MIDI, el comportament predeterminat és que cada pentagrama representa un canal MIDI, amb totes les veus d’aquest pentagrama barrejades. Això redueix al mínim el risc que s’esgoti el nombre de canals MIDI disponibles, atès que hi ha un màxim de 16 canals per cada port MIDI, i la majoria dels dispositius sols tenen un port.

No obstant, quan es trasllada l’interpretador `Staff_performer` al context `Voice`, cada veu d’un pentagrama pot tenir el seu propi canal MIDI, com es mostra a l’exemple següent: malgrat d’estar sobre el mateix pentagrama, es creen dos canals MIDI, cadascú amb un `midiInstrument` diferent.

    \score {
      \new Staff <<
        \new Voice \relative c''' {
          \set midiInstrument = #"flute"
          \voiceOne
          \key g \major
          \time 2/2
          r2 g-"Flute" ~
          g fis ~
          fis4 g8 fis e2 ~
          e4 d8 cis d2
        }
        \new Voice \relative c'' {
          \set midiInstrument = #"clarinet"
          \voiceTwo
          b1-"Clarinet"
          a2. b8 a
          g2. fis8 e
          fis2 r
        }
      >>
      \layout { }
      \midi {
        \context {
          \Staff
          \remove "Staff_performer"
        }
        \context {
          \Voice
          \consists "Staff_performer"
        }
        \tempo 2 = 72
      }
    }

## Modificar el compàs d’una secció polimètrica utilitzant `\scaleDurations`

La propietat `measureLength`, junt amb `measurePosition`, determina quan cal dibuixar una línia divisòria. No obstant, en utilitzar `\scaleDurations`, l’escalat proporcional de les duracions fa difícil introduir canvis de compàs. En aquest cas s’ha d’establir manualment el valor de `measureLength` utilitzant la funció `ly:make-moment`. El segon argument ha de ser el mateix que el segon artgument de `\scaleDurations`.

    \layout {
      \context {
        \Score
        \remove "Timing_translator"
      }
      \context {
        \Staff
        \consists "Timing_translator"
      }
    }

    <<
      \new Staff {
        \scaleDurations 8/5 {
          \time 6/8
          \set Timing.measureLength = #(ly:make-moment 6/5)
          b8 b b b b b
          \time 2/4
          \set Timing.measureLength = #(ly:make-moment 4/5)
          b4 b
        }
      }
      \new Staff {
        \clef bass
        \time 2/4
        c2 d e f
      }
    >>

## Notació de responsos o salms

Aquest tipus de notació s’utilitza per als cants salmòdics, en les quals les estrofes no sempre tenen la mateixa longitud.

    stemOff = \hide Staff.Stem
    stemOn  = \undo \stemOff

    \score {
      \new Staff \with { \remove "Time_signature_engraver" }
      {
        \key g \minor
        \cadenzaOn
        \stemOff a'\breve bes'4 g'4
        \stemOn a'2 \section
        \stemOff a'\breve g'4 a'4
        \stemOn f'2 \section
        \stemOff a'\breve^\markup { \italic flexe }
        \stemOn g'2 \fine
      }
    }

## Crear pentagrames en blanc

Per crear pentagrames en blanc, genereu compassos buits i després elimineu el gravador de números de compàs `Bar_number_engraver` del context `Score`, i els gravadors de la indicacó de compàs `Time_signature_engraver`, de la clau `Clef_engraver` i dels compassos `Bar_engraver` del context de `Staff`.

    #(set-global-staff-size 20)

    \score {
      {
        \repeat unfold 12 { s1 \break }
      }
      \layout {
        indent = 0\in
        \context {
          \Staff
          \remove "Time_signature_engraver"
          \remove "Clef_engraver"
          \remove "Bar_engraver"
        }
        \context {
          \Score
          \remove "Bar_number_engraver"
        }
      }
    }

    % uncomment these lines for "letter" size
    %{
    \paper {
      #(set-paper-size "letter")
      ragged-last-bottom = ##f
      line-width = 7.5\in
      left-margin = 0.5\in
      bottom-margin = 0.25\in
      top-margin = 0.25\in
    }
    %}

    % uncomment these lines for "A4" size
    %{
    \paper {
      #(set-paper-size "a4")
      ragged-last-bottom = ##f
      line-width = 180
      left-margin = 15
      bottom-margin = 10
      top-margin = 10
    }
    %}

## Creació d’armadures personalitzades

El LilyPond accepta armadures personalitzades. En aquest exemple, es mostra l’armadura de re menor amb una rang estès de bemolls.

    \new Staff \with {
      \override StaffSymbol.line-count = #8
      \override KeySignature.flat-positions = #'((-7 . 6))
      \override KeyCancellation.flat-positions = #'((-7 . 6))
      % presumably sharps are also printed in both octaves
      \override KeySignature.sharp-positions = #'((-6 . 7))
      \override KeyCancellation.sharp-positions = #'((-6 . 7))

      \override Clef.stencil = #
      (lambda (grob)(grob-interpret-markup grob
      #{ \markup\combine
        \musicglyph "clefs.C"
        \translate #'(-3 . -2)
        \musicglyph "clefs.F"
       #}))
        clefPosition = #3
        middleCPosition = #3
        middleCClefPosition = #3
    }

    {
      \key d\minor
      f bes, f bes,
    }

## Pliques de pentagrama creuat

Aquest fragment de codi mostra l’ús del gravador `Span_stem_engraver` i de `\crossStaff` per connectar automàticament pliques d’un pentagrama a un altre. No cal especificar la longitud de la plica perquè la distància variable entre els caps de les notes i els pentagrames es calcula automàticament.

    \layout {
      \context {
        \PianoStaff
        \consists "Span_stem_engraver"
      }
    }

    {
      \new PianoStaff <<
        \new Staff {
          <b d'>4 r d'16\> e'8. g8 r\!
          e'8 f' g'4 e'2
        }
        \new Staff {
          \clef bass
          \voiceOne
          \autoBeamOff
          \crossStaff { <e g>4 e, g16 a8. c8} d
          \autoBeamOn
          g8 f g4 c2
        }
      >>
    }

## Definir un gravador en l’Scheme: gravador d’àmbit o tessitura

Aquest exemple mostra com pot definir-se el gravador d’àmbit o tessitura a l’espai de l’usuari, com un gravador de l’Scheme.

Això és bàsicament una reescriptura en l’Scheme del codi de ‘`lily/ambitus-engraver.cc`’.

    #(use-modules (oop goops))

    %%%
    %%% Grob utilities
    %%%
    %%% These are literal rewrites of some C++ methods used by the ambitus engraver.

    #(define (ly:separation-item::add-conditional-item grob grob-item)
       "Add @var{grob-item} to the array of conditional elements of @var{grob}.
    Rewrite of @code{Separation_item::add_conditional_item} from @file{lily/separation-item.cc}."
       (ly:pointer-group-interface::add-grob grob 'conditional-elements grob-item))

    #(define (ly:accidental-placement::accidental-pitch accidental-grob)
       "Get the pitch from the grob cause of @var{accidental-grob}.
    Rewrite of @code{accidental_pitch} from @file{lily/accidental-placement.cc}."
       (ly:event-property (ly:grob-property (ly:grob-parent accidental-grob Y) 'cause)
                          'pitch))

    #(define (ly:accidental-placement::add-accidental grob accidental-grob)
       "Add @var{accidental-grob}, an @code{Accidental} grob, to the
    list of the accidental grobs of @var{grob}, an @code{AccidentalPlacement}
    grob.
    Rewrite of @code{Accidental_placement::add_accidental} from @file{lily/accidental-placement.cc}."
       (let ((pitch (ly:accidental-placement::accidental-pitch accidental-grob)))
         (set! (ly:grob-parent accidental-grob X) grob)
         (let* ((accidentals (ly:grob-object grob 'accidental-grobs))
                (handle (assq (ly:pitch-notename pitch) accidentals))
                (entry (if handle (cdr handle) '())))
           (set! (ly:grob-object grob 'accidental-grobs)
                 (assq-set! accidentals
                            (ly:pitch-notename pitch)
                            (cons accidental-grob entry))))))

    %%%
    %%% Ambitus data structure
    %%%

    %%% The <ambitus> class holds the various grobs that are created
    %%% to print an ambitus:
    %%% - ambitus-group: the grob that groups all the components of an ambitus
    %%% (Ambitus grob);
    %%% - ambitus-line: the vertical line between the upper and lower ambitus
    %%% notes (AmbitusLine grob);
    %%% - ambitus-up-note and ambitus-down-note: the note head and accidental
    %%% for the lower and upper note of the ambitus (see <ambitus-note> class
    %%% below).
    %%% The other slots define the key and clef context of the engraver:
    %%% - start-c0: position of middle c at the beginning of the piece.  It
    %%% is used to place the ambitus notes according to their pitch;
    %%% - start-key-sig: the key signature at the beginning of the piece.  It
    %%% is used to determine if accidentals shall be printed next to ambitus
    %%% notes.

    #(define-class <ambitus> ()
       (ambitus-group #:accessor ambitus-group)
       (ambitus-line #:accessor ambitus-line)
       (ambitus-up-note #:getter ambitus-up-note
                        #:init-form (make <ambitus-note>))
       (ambitus-down-note #:getter ambitus-down-note
                          #:init-form (make <ambitus-note>))
       (start-c0 #:accessor ambitus-start-c0
                 #:init-value #f)
       (start-key-sig #:accessor ambitus-start-key-sig
                      #:init-value '()))

    %%% Accessor for the lower and upper note data of an ambitus
    #(define-method (ambitus-note (ambitus <ambitus>) direction)
       "If @var{direction} is @code{UP}, then return the upper ambitus note
    of @var{ambitus}, otherwise return the lower ambitus note."
       (if (= direction UP)
           (ambitus-up-note ambitus)
           (ambitus-down-note ambitus)))

    %%% The <ambitus-note> class holds the grobs that are specific to ambitus
    %%% (lower and upper) notes:
    %%% - head: an AmbitusNoteHead grob;
    %%% - accidental: an AmbitusAccidental grob, to be possibly printed next
    %%% to the ambitus note head.
    %%% Moreover:
    %%% - pitch is the absolute pitch of the note
    %%% - cause is the note event that causes this ambitus note, i.e. the lower
    %%% or upper note of the considered music sequence.

    #(define-class <ambitus-note> ()
       (head #:accessor ambitus-note-head
             #:init-value #f)
       (accidental #:accessor ambitus-note-accidental
                   #:init-value #f)
       (cause #:accessor ambitus-note-cause
              #:init-value #f)
       (pitch #:accessor ambitus-note-pitch
              #:init-value #f))

    %%%
    %%% Ambitus engraving logics
    %%%
    %%% Rewrite of the code from @file{lily/ambitus-engraver.cc}.

    #(define (make-ambitus translator)
       "Build an ambitus object: initialize all the grobs and their relations.

    The Ambitus grob contain all other grobs:
     Ambitus
      |- AmbitusLine
      |- AmbitusNoteHead   for upper note
      |- AmbitusAccidental for upper note
      |- AmbitusNoteHead   for lower note
      |- AmbitusAccidental for lower note

    The parent of an accidental is the corresponding note head,
    and the accidental is set as the 'accidental-grob of the note head
    so that is printed by the function that prints notes."
       ;; make the ambitus object
       (let ((ambitus (make <ambitus>)))
         ;; build the Ambitus grob, which will contain all other grobs
         (set! (ambitus-group ambitus) (ly:engraver-make-grob translator 'Ambitus '()))
         ;; build the AmbitusLine grob (line between lower and upper note)
         (set! (ambitus-line ambitus) (ly:engraver-make-grob translator 'AmbitusLine '()))
         ;; build the upper and lower AmbitusNoteHead and AmbitusAccidental
         (for-each (lambda (direction)
                     (let ((head (ly:engraver-make-grob translator 'AmbitusNoteHead '()))
                           (accidental (ly:engraver-make-grob translator 'AmbitusAccidental '()))
                           (group (ambitus-group ambitus)))
                       ;; The parent of the AmbitusAccidental grob is the
                       ;; AmbitusNoteHead grob
                       (set! (ly:grob-parent accidental Y) head)
                       ;; The AmbitusAccidental grob is set as the accidental-grob
                       ;; object of the AmbitusNoteHead.  This is later used by the
                       ;; function that prints notes.
                       (set! (ly:grob-object head 'accidental-grob) accidental)
                       ;; both the note head and the accidental grobs are added
                       ;; to the main ambitus grob.
                       (ly:axis-group-interface::add-element group head)
                       (ly:axis-group-interface::add-element group accidental)
                       ;; the note head and the accidental grobs are added to the
                       ;; ambitus object
                       (set! (ambitus-note-head (ambitus-note ambitus direction))
                             head)
                       (set! (ambitus-note-accidental (ambitus-note ambitus direction))
                             accidental)))
                   (list DOWN UP))
         ;; The parent of the ambitus line is the lower ambitus note head
         (set! (ly:grob-parent (ambitus-line ambitus) X)
               (ambitus-note-head (ambitus-note ambitus DOWN)))
         ;; the ambitus line is added to the ambitus main grob
         (ly:axis-group-interface::add-element (ambitus-group ambitus) (ambitus-line ambitus))
         ambitus))

    #(define-method (initialize-ambitus-state (ambitus <ambitus>) translator)
       "Initialize the state of @var{ambitus}, by getting the starting
    position of middle C and key signature from @var{translator}'s context."
       (if (not (ambitus-start-c0 ambitus))
           (begin
             (set! (ambitus-start-c0 ambitus)
                   (ly:context-property (ly:translator-context translator)
                                        'middleCPosition
                                        0))
             (set! (ambitus-start-key-sig ambitus)
                   (ly:context-property (ly:translator-context translator)
                                        'keyAlterations)))))

    #(define-method (update-ambitus-notes (ambitus <ambitus>) note-grob)
       "Update the upper and lower ambitus pithes of @var{ambitus}, using
    @var{note-grob}."
       ;; Get the event that caused the note-grob creation
       ;; and check that it is a note-event.
       (let ((note-event (ly:grob-property note-grob 'cause)))
         (if (ly:in-event-class? note-event 'note-event)
             ;; get the pitch from the note event
             (let ((pitch (ly:event-property note-event 'pitch)))
               ;; if this pitch is lower than the current ambitus lower
               ;; note pitch (or it has not been initialized yet),
               ;; then this pitch is the new ambitus lower pitch,
               ;; and conversely for upper pitch.
               (for-each (lambda (direction pitch-compare)
                           (if (or (not (ambitus-note-pitch (ambitus-note ambitus direction)))
                                   (pitch-compare pitch
                                                  (ambitus-note-pitch (ambitus-note ambitus direction))))
                               (begin
                                 (set! (ambitus-note-pitch (ambitus-note ambitus direction))
                                       pitch)
                                 (set! (ambitus-note-cause (ambitus-note ambitus direction))
                                       note-event))))
                         (list DOWN UP)
                         (list ly:pitch<? (lambda (p1 p2)
                                            (ly:pitch<? p2 p1))))))))

    #(define-method (typeset-ambitus (ambitus <ambitus>) translator)
       "Typeset the ambitus:
    - place the lower and upper ambitus notes according to their pitch and
      the position of the middle C;
    - typeset or delete the note accidentals, according to the key signature.
      An accidental, if it is to be printed, is added to an AccidentalPlacement
      grob (a grob dedicated to the placement of accidentals near a chord);
    - both note heads are added to the ambitus line grob, so that a line should
      be printed between them."
       ;; check if there are lower and upper pitches
       (if (and (ambitus-note-pitch (ambitus-note ambitus UP))
                (ambitus-note-pitch (ambitus-note ambitus DOWN)))
           ;; make an AccidentalPlacement grob, for placement of note accidentals
           (let ((accidental-placement (ly:engraver-make-grob
                                        translator
                                        'AccidentalPlacement
                                        (ambitus-note-accidental (ambitus-note ambitus DOWN)))))
             ;; For lower and upper ambitus notes:
             (for-each (lambda (direction)
                         (let ((pitch (ambitus-note-pitch (ambitus-note ambitus direction))))
                           ;; set the cause and the staff position of the ambitus note
                           ;; according to the associated pitch
                           (set! (ly:grob-property (ambitus-note-head (ambitus-note ambitus direction))
                                                   'cause)
                                 (ambitus-note-cause (ambitus-note ambitus direction)))
                           (set! (ly:grob-property (ambitus-note-head (ambitus-note ambitus direction))
                                                   'staff-position)
                                 (+ (ambitus-start-c0 ambitus)
                                    (ly:pitch-steps pitch)))
                           ;; determine if an accidental shall be printed for this note,
                           ;; according to the key signature
                           (let* ((handle (or (assoc (cons (ly:pitch-octave pitch)
                                                           (ly:pitch-notename pitch))
                                                     (ambitus-start-key-sig ambitus))
                                              (assoc (ly:pitch-notename pitch)
                                                     (ambitus-start-key-sig ambitus))))
                                  (sig-alter (if handle (cdr handle) 0)))
                             (cond ((= (ly:pitch-alteration pitch) sig-alter)
                                    ;; the note alteration is in the key signature
                                    ;; => it does not have to be printed
                                    (ly:grob-suicide!
                                     (ambitus-note-accidental (ambitus-note ambitus direction)))
                                    (set! (ly:grob-object (ambitus-note-head (ambitus-note ambitus direction))
                                                          'accidental-grob)
                                          '()))
                                   (else
                                    ;; otherwise, the accidental shall be printed
                                    (set! (ly:grob-property (ambitus-note-accidental
                                                             (ambitus-note ambitus direction))
                                                            'alteration)
                                          (ly:pitch-alteration pitch)))))
                           ;; add the AccidentalPlacement grob to the
                           ;; conditional items of the AmbitusNoteHead
                           (ly:separation-item::add-conditional-item
                            (ambitus-note-head (ambitus-note ambitus direction))
                            accidental-placement)
                           ;; add the AmbitusAccidental to the list of the
                           ;; AccidentalPlacement grob accidentals
                           (ly:accidental-placement::add-accidental
                            accidental-placement
                            (ambitus-note-accidental (ambitus-note ambitus direction)))
                           ;; add the AmbitusNoteHead grob to the AmbitusLine grob
                           (ly:pointer-group-interface::add-grob
                            (ambitus-line ambitus)
                            'note-heads
                            (ambitus-note-head (ambitus-note ambitus direction)))))
                       (list DOWN UP))
             ;; add the AccidentalPlacement grob to the main Ambitus grob
             (ly:axis-group-interface::add-element (ambitus-group ambitus) accidental-placement))
           ;; no notes ==> suicide the grobs
           (begin
             (for-each (lambda (direction)
                         (ly:grob-suicide! (ambitus-note-accidental (ambitus-note ambitus direction)))
                         (ly:grob-suicide! (ambitus-note-head (ambitus-note ambitus direction))))
                       (list DOWN UP))
             (ly:grob-suicide! ambitus-line))))

    %%%
    %%% Ambitus engraver definition
    %%%
    #(define ambitus-engraver
       (lambda (context)
         (let ((ambitus #f))
           ;; when music is processed: make the ambitus object, if not already built
           (make-engraver
        ((process-music translator)
         (if (not ambitus)
             (set! ambitus (make-ambitus translator))))
        ;; set the ambitus clef and key signature state
        ((stop-translation-timestep translator)
         (if ambitus
             (initialize-ambitus-state ambitus translator)))
        ;; when a note-head grob is built, update the ambitus notes
        (acknowledgers
              ((note-head-interface engraver grob source-engraver)
           (if ambitus
               (update-ambitus-notes ambitus grob))))
        ;; finally, typeset the ambitus according to its upper and lower notes
        ;; (if any).
        ((finalize translator)
         (if ambitus
             (typeset-ambitus ambitus translator)))))))

    %%%
    %%% Example
    %%%

    \score {
      \new StaffGroup <<
        \new Staff { c'4 des' e' fis' gis' }
        \new Staff { \clef "bass" c4 des ~ des ees b, }
      >>
      \layout { \context { \Staff \consists #ambitus-engraver } }
    }

## Mostrar un sistema GrandStaff complet si segueix amb vida un sol dels seus pentagrames

A vegades, a les partitures orquestrals es deixen en silenci instruments individuals o grups d’ells durant un període de temps, i els seus pentagrames corresponents es poden suprimir durant aquest temps (amb `\removeEmptyStaves`).

Quan tornen a sonar, sovint es prefereix mostrar tots els instruments del grup. Això es pot fer afegint el gravador `Keep_alive_together_engraver` en el context agrupador (per exemple: un GrandStaff o un StaffGroup)

En aquest exemple, els violins estan en silenci durant els sistemes segon i tercer. Sols el violí primer sona a l’últim compàs, però es mostra també el pentagrama del violí segon.

    \score {
      <<
        \new StaffGroup = "StaffGroup_woodwinds"
        <<
          \new Staff = "Staff_flute" \with {
            instrumentName = "Flute"
            shortInstrumentName = "Fl"
          }
          \relative c' {
            \repeat unfold 3 { c'4 c c c | c c c c | c c c c | \break }
          }
        >>
        \new StaffGroup = "StaffGroup_Strings"
        <<
          \new GrandStaff = "GrandStaff_violins"
          <<
            \new Staff = "StaffViolinI" \with {
              instrumentName = "Violin I"
              shortInstrumentName = "Vi I"
            }
            \relative c'' {
              a1 \repeat unfold 7 { s1 } \repeat unfold 12 a16  a4
            }
            \new Staff = "StaffViolinII" \with {
              instrumentName = "Violin II"
              shortInstrumentName = "Vi II"
            }
            \relative c' { e1 \repeat unfold 8 { s1 } }
          >>
          \new Staff = "Staff_cello" \with {
            instrumentName = "Cello"
            shortInstrumentName = "Ce"
          }
          \relative c { \clef bass \repeat unfold 9 { c1 }}
        >>
      >>
    }
    \layout {
      indent = 3.0\cm
      short-indent = 1.5\cm
      \context {
        \GrandStaff
        \consists Keep_alive_together_engraver
      }
      \context {
        \Staff
        \RemoveEmptyStaves
      }
    }

## Els gravadors, un per un

Del problema central de la notació, és a dir, crear un determinat símbol, s’encarreguen els «plugins» o complements afegits. Cada un dels complements es coneix com un gravador. A aquest exemple els gravadors es van activant un per un, a l’ordre següent:

- cap de les notes,

- el símbol del pentagrama,

- clau,

- pliques,

- barres, lligadures d’expressió, accents,

- alteracions, línies divisòries, indicació del compàs, i armadura.

Els gravadors es troben agrupats. Per exemple, els caps de nota, les lligadures d’expressió, les barres de corxera, etc., formen un context de veu `Voice`. Els gravadors de l’armadura, alteracions línies de compàs, etc., formen un context de pentagrama `Staff`.

    %% sample music
    topVoice = \relative c' {
      \key d \major
      es8([ g] a[ fis])
      b4
      b16[-. b-. b-. cis-.]
      d4->
    }

    botVoice = \relative c' {
      \key d \major
      c8[( f] b[ a)]
      es4
      es16[-. es-. es-. fis-.]
      b4->
    }

    hoom = \relative c {
      \key d \major
      \clef bass
      g8-. r
      r4
      fis8-.
      r8
      r4
      b'4->
    }

    pah = \relative c' {
      r8 b-.
      r4
      r8 g8-.
      r16 g-. r8
      \clef treble
      fis'4->
    }

    %
    % setup for Request->Element conversion. Guru-only
    %

    MyStaff = \context {
      \type "Engraver_group"
      \name Staff

      \description "Handles clefs, bar lines, keys, accidentals.  It can contain
    @code{Voice} contexts."

      \consists "Output_property_engraver"

      \consists "Font_size_engraver"

      \consists "Volta_engraver"
      \consists "Separating_line_group_engraver"
      \consists "Dot_column_engraver"

      \consists "Ottava_spanner_engraver"
      \consists "Rest_collision_engraver"
      \consists "Piano_pedal_engraver"
      \consists "Piano_pedal_align_engraver"
      \consists "Instrument_name_engraver"
      \consists "Grob_pq_engraver"
      \consists "Forbid_line_break_engraver"
      \consists "Axis_group_engraver"

      \consists "Pitch_squash_engraver"

      localAlterations = #'()

      % explicitly set instrumentName, so we don't get
      % weird effects when doing instrument names for
      % piano staves

      instrumentName = #'()
      shortInstrumentName = #'()

      \accepts "Voice"
      \defaultchild "Voice"
    }

    MyVoice = \context {
      \type "Engraver_group"
      \name Voice

      \description "
        Corresponds to a voice on a staff.  This context handles the
        conversion of dynamic signs, stems, beams, super- and subscripts,
        slurs, ties, and rests.

        You have to instantiate this explicitly if you want to have
        multiple voices on the same staff."

      localAlterations = #'()
      \consists "Font_size_engraver"

      % must come before all
      \consists "Output_property_engraver"
      \consists "Arpeggio_engraver"
      \consists "Multi_measure_rest_engraver"
      \consists "Text_spanner_engraver"
      \consists "Grob_pq_engraver"
      \consists "Note_head_line_engraver"
      \consists "Glissando_engraver"
      \consists "Ligature_bracket_engraver"
      \consists "Breathing_sign_engraver"
      % \consists "Rest_engraver"
      \consists "Grace_beam_engraver"
      \consists "New_fingering_engraver"
      \consists "Chord_tremolo_engraver"
      \consists "Percent_repeat_engraver"
      \consists "Slash_repeat_engraver"

      %{
        Must come before text_engraver, but after note_column engraver.
      %}
      \consists "Text_engraver"
      \consists "Dynamic_engraver"
      \consists "Dynamic_align_engraver"
      \consists "Fingering_engraver"

      \consists "Script_column_engraver"
      \consists "Rhythmic_column_engraver"
      \consists "Cluster_spanner_engraver"
      \consists "Tie_engraver"
      \consists "Tie_engraver"
      \consists "Tuplet_engraver"
      \consists "Note_heads_engraver"
      \consists "Rest_engraver"
    }

    \score {
      \topVoice
      \layout {
        \context { \MyStaff }
        \context { \MyVoice }
      }
    }

    MyStaff = \context {
      \MyStaff
      \consists "Staff_symbol_engraver"
    }

    \score {
      \topVoice
      \layout {
        \context { \MyStaff }
        \context { \MyVoice }
      }
    }

    MyStaff = \context {
      \MyStaff
      \consists "Clef_engraver"
      \remove "Pitch_squash_engraver"
    }

    \score {
      \topVoice
      \layout {
        \context { \MyStaff }
        \context { \MyVoice }
      }
    }

    MyVoice = \context {
      \MyVoice
      \consists "Stem_engraver"
    }

    \score {
      \topVoice
      \layout {
        \context { \MyStaff }
        \context { \MyVoice }
      }
    }

    MyVoice = \context {
      \MyVoice
      \consists "Beam_engraver"
    }

    \score {
      \topVoice
      \layout {
        \context { \MyStaff }
        \context { \MyVoice }
      }
    }

    MyVoice = \context {
      \MyVoice
      \consists "Phrasing_slur_engraver"
      \consists "Slur_engraver"
      \consists "Script_engraver"
    }

    \score {
      \topVoice
      \layout {
        \context { \MyStaff }
        \context { \MyVoice }
      }
    }

    MyStaff = \context {
      \MyStaff
      \consists "Bar_engraver"
      \consists "Time_signature_engraver"
    }

    \score {
      \topVoice
      \layout {
        \context { \MyStaff }
        \context { \MyVoice }
      }
    }

    MyStaff = \context {
      \MyStaff
      \consists "Accidental_engraver"
      \consists "Key_engraver"
    }
    \score {
      \topVoice
      \layout {
        \context { \MyStaff }
        \context { \MyVoice }
      }
    }

## Disposició Mensurstriche (línies divisòries entre pentagrames)

La disposició «mensurstriche» en els quals les línies divisòries no estan dibuixades sobre els pentagrames, sinó entre ells, es pot aconseguir amb un `StaffGroup` en comptes d’un `ChoirStaff`. La línia divisòria sobre els pentagrames s’esborra usant `\hide`.

    \layout {
      \context {
        \Staff
        measureBarType = "-span|"
      }
    }

    music = \fixed c'' {
      c1
      d2 \section e2
      f1 \fine
    }

    \new StaffGroup <<
      \new Staff \music
      \new Staff \music
    >>

## Niuat de grups de pentagrames

Es pot utilitzar la propietat `systemStartDelimiterHierarchy` per crear grups de pentagrames niuats de forma més complexa. L’ordre `\set StaffGroup.systemStartDelimiterHierarchy` pren una llista alfabètic del nombre de pentagrames produïts. Es pot proporcionar abans de cada pentagrama un delimitador de començament del sistema. Es pot envoltar entre corxets i admetre tants pentagrames com envoltin els corxets. Es poden ometre els elements de la llista, però el primer corxet sempre abasta tots els pentagrames. Les possibilitat són `SystemStartBar`, `SystemStartBracket`, `SystemStartBrace` `SystemStartSquare`.

    \new StaffGroup
    \relative c'' <<
      \override StaffGroup.SystemStartSquare.collapse-height = #4
      \set StaffGroup.systemStartDelimiterHierarchy
        = #'(SystemStartSquare (SystemStartBrace (SystemStartBracket a
                                 (SystemStartSquare b)  ) c ) d)
      \new Staff { c1 }
      \new Staff { c1 }
      \new Staff { c1 }
      \new Staff { c1 }
      \new Staff { c1 }
    >>

## Numerar grups de compassos

Aquest fragment de codi mostra l’ús del gravador `Measure_counter_engraver` per numerar grups de compassos successius. Es pot numerar qualsevol període de compassos tant si té repeticions com si no.

S’ha d’afegir el gravador al context adequat. Aquí s’usa un context `Staff`; una altra possibilitat seria un context `Dynamics`.

El comptador s’inicia amb `\startMeasureCount` i finalitza amb `\stopMeasureCount`. La numeració comença amb `1`, de forma predeterminada, però es pot modificar aquest comportament sobreescrivint la propietat `count-from`.

Quan un compàs s’estén més enllà d’un salt de línia, el número apareix dues vegades, la segona vegada entre parèntesi.

    \layout {
      \context {
        \Staff
        \consists #Measure_counter_engraver
      }
    }

    \new Staff {
      \startMeasureCount
      \repeat unfold 7 {
        c'4 d' e' f'
      }
      \stopMeasureCount
      \bar "||"
      g'4 f' e' d'
      \override Staff.MeasureCounter.count-from = #2
      \startMeasureCount
      \repeat unfold 5 {
        g'4 f' e' d'
      }
      g'4 f'
      \bar ""
      \break
      e'4 d'
      \repeat unfold 7 {
        g'4 f' e' d'
      }
      \stopMeasureCount
    }

## Print ChordNames with same root and different bass as slash and bass-note

To print subsequent `ChordNames` only differing in its bass note as slash and bass note use the here defined engraver. The behaviour may be controlled in detail by the `chordChanges` context property.

    #(define Bass_changes_equal_root_engraver
      (lambda (ctx)
      "For sequential @code{ChordNames} with same root, but different bass, the root
    markup is dropped: D D/C D/B  -> D /C /B
    The behaviour may be controlled by setting the @code{chordChanges}
    context-property."
        (let ((chord-pitches '())
              (last-chord-pitches '())
              (bass-pitch #f))
          (make-engraver
            ((initialize this-engraver)
              (let ((chord-note-namer (ly:context-property ctx 'chordNoteNamer)))
                ;; Set 'chordNoteNamer, respect user setting if already done
                (ly:context-set-property! ctx 'chordNoteNamer
                  (if (procedure? chord-note-namer)
                      chord-note-namer
                      note-name->markup))))
            (listeners
              ((note-event this-engraver event)
                (let* ((pitch (ly:event-property event 'pitch))
                       (pitch-name (ly:pitch-notename pitch))
                       (pitch-alt (ly:pitch-alteration pitch))
                       (bass (ly:event-property event 'bass #f))
                       (inversion (ly:event-property event 'inversion #f)))
                ;; Collect notes of the chord
                ;;  - to compare inversed chords we need to collect the bass note
                ;;    as usual member of the chord, whereas an added bass must be
                ;;    treated separate from the usual chord-notes
                ;;  - notes are stored as pairs containing their
                ;;    pitch-name (an integer), i.e. disregarding their octave and
                ;;    their alteration
                (cond (bass (set! bass-pitch pitch))
                      (inversion
                        (set! bass-pitch pitch)
                        (set! chord-pitches
                              (cons (cons pitch-name pitch-alt) chord-pitches)))
                      (else
                        (set! chord-pitches
                              (cons (cons pitch-name pitch-alt) chord-pitches)))))))
            (acknowledgers
              ((chord-name-interface this-engraver grob source-engraver)
                (let ((chord-changes (ly:context-property ctx 'chordChanges #f)))
                  ;; If subsequent chords are equal apart from their bass,
                  ;; reset the 'text-property.
                  ;; Equality is done by comparing the sorted lists of this chord's
                  ;; elements and the previous chord. Sorting is needed because
                  ;; inverted chords may have a different order of pitches.
                  ;; `chord-changes' needs to be true
                  (if (and bass-pitch
                           chord-changes
                           (equal?
                             (sort chord-pitches car<)
                             (sort last-chord-pitches car<)))
                      (ly:grob-set-property! grob 'text
                        (make-line-markup
                          (list
                            (ly:context-property ctx 'slashChordSeparator)
                            ((ly:context-property ctx 'chordNoteNamer)
                             bass-pitch
                             (ly:context-property ctx 'chordNameLowercaseMinor))))))
                  (set! last-chord-pitches chord-pitches)
                  (set! chord-pitches '())
                  (set! bass-pitch #f))))
            ((finalize this-engraver)
              (set! last-chord-pitches '()))))))

    myChords = \chordmode {
      %\germanChords

      \set chordChanges = ##t
      d2:m d:m/cis

      d:m/c
      \set chordChanges = ##f
      d:m/b

      e1:7
      \set chordChanges = ##t
      e
      \break
      \once \set chordChanges = ##f
      e1/f
      e2/gis e/+gis e e:m/f d:m d:m/cis d:m/c
      \set chordChanges = ##f
      d:m/b
    }

    <<
      \new ChordNames
        \with { \consists #Bass_changes_equal_root_engraver }
        \myChords
      \new Staff \myChords
    >>

## Supressió dels nombres de compàs de tota la partitura

Es poden eliminar completament els nombres de compàs traient el gravador `Bar_number_engraver` del context de `Score`.

    \layout {
      \context {
        \Score
        \omit BarNumber
        % or:
        %\remove "Bar_number_engraver"
      }
    }

    \relative c'' {
      c4 c c c \break
      c4 c c c
    }

## Us del corxet recte al començament d’un grup de pentagrames

Es pot usar el delimitador de començament d’un sistema `SystemStartSquare` establint-lo explícitament dins d’un context `StaffGroup` o `ChoirStaffGroup`.

    \score {
      \new StaffGroup { <<
      \set StaffGroup.systemStartDelimiter = #'SystemStartSquare
        \new Staff { c'4 d' e' f' }
        \new Staff { c'4 d' e' f' }
      >> }
    }

## Using marklines in a Frenched score

Using `MarkLine` contexts (such as in LSR1010) in a Frenched score can be problematic if all the staves between two `MarkLine`s are removed in one system. The `Keep_alive_together_engraver` can be used within each `StaffGroup` to keep the `MarkLine` alive only as long as the other staves in the group stay alive.

    bars = {
      \tempo "Allegro" 4=120
      s1*2
      \repeat unfold 5 { \mark \default s1*2 }
      \bar "||"
      \tempo "Adagio" 4=40
      s1*2
      \repeat unfold 8 { \mark \default s1*2 }
      \bar "|."
    }
    winds = \repeat unfold 120 { c''4 }
    trumpet = { \repeat unfold 8 g'2 R1*16 \repeat unfold 4 g'2 R1*8 }
    trombone = { \repeat unfold 4 c'1 R1*8 d'1 R1*17 }
    strings = \repeat unfold 240 { c''8 }

    #(set-global-staff-size 16)
    \paper {
      systems-per-page = 5
      ragged-last-bottom = ##f
    }

    \layout {
      indent = 15\mm
      short-indent = 5\mm
      \context {
        \name MarkLine
        \type Engraver_group
        \consists Output_property_engraver
        \consists Axis_group_engraver
        \consists Mark_engraver
        \consists Metronome_mark_engraver
        \consists Staff_collecting_engraver
        \override VerticalAxisGroup.remove-empty = ##t
        \override VerticalAxisGroup.remove-layer = #'any
        \override VerticalAxisGroup.staff-affinity = #DOWN
        \override VerticalAxisGroup.nonstaff-relatedstaff-spacing.padding = 1
        keepAliveInterfaces = #'()
      }
      \context {
        \Staff
        \override VerticalAxisGroup.remove-empty = ##t
        \override VerticalAxisGroup.remove-layer = ##f
      }
      \context {
        \StaffGroup
        \accepts MarkLine
        \consists Keep_alive_together_engraver
      }
      \context {
        \Score
        \remove Mark_engraver
        \remove Metronome_mark_engraver
        \remove Staff_collecting_engraver
      }
    }

    \score {
      <<
        \new StaffGroup = "winds" \with {
          instrumentName = "Winds"
          shortInstrumentName = "Winds"
        } <<
          \new MarkLine \bars
          \new Staff \winds
        >>
        \new StaffGroup = "brass" <<
          \new MarkLine \bars
          \new Staff = "trumpet" \with {
            instrumentName = "Trumpet"
            shortInstrumentName = "Tpt"
          } \trumpet
          \new Staff = "trombone" \with {
            instrumentName = "Trombone"
            shortInstrumentName = "Tbn"
          } \trombone
        >>
        \new StaffGroup = "strings" \with {
          instrumentName = "Strings"
          shortInstrumentName = "Strings"
        } <<
          \new MarkLine \bars
          \new Staff = "strings" { \strings }
        >>
      >>
    }

## Plantilla per a conjunt amb lletres alineades a sobre i a sota dels pentagrames

Aquesta plantilla és, bàsicament, la mateixa que la senzilla plantilla “Conjunt vocal”, excepte que aquí totes les línies de lletra es col·loquen utilitzant `alignAboveContext` i `alignBelowContext`.

    global = {
      \key c \major
      \time 4/4
    }

    sopMusic = \relative c'' {
      c4 c c8[( b)] c4
    }
    sopWords = \lyricmode {
      hi hi hi hi
    }

    altoMusic = \relative c' {
      e4 f d e
    }
    altoWords = \lyricmode {
      ha ha ha ha
    }

    tenorMusic = \relative c' {
      g4 a f g
    }
    tenorWords = \lyricmode {
      hu hu hu hu
    }

    bassMusic = \relative c {
      c4 c g c
    }
    bassWords = \lyricmode {
      ho ho ho ho
    }

    \score {
      \new ChoirStaff <<
        \new Staff = "women" <<
          \new Voice = "sopranos" { \voiceOne << \global \sopMusic >> }
          \new Voice = "altos" { \voiceTwo << \global \altoMusic >> }
        >>
        \new Lyrics \with { alignAboveContext = #"women" }
          \lyricsto "sopranos" \sopWords
        \new Lyrics \with { alignBelowContext = #"women" }
          \lyricsto "altos" \altoWords
        % we could remove the line about this with the line below, since
        % we want the alto lyrics to be below the alto Voice anyway.
        % \new Lyrics \lyricsto "altos" \altoWords

        \new Staff = "men" <<
          \clef bass
          \new Voice = "tenors" { \voiceOne << \global \tenorMusic >> }
          \new Voice = "basses" { \voiceTwo << \global \bassMusic >> }
        >>
        \new Lyrics \with { alignAboveContext = #"men" }
          \lyricsto "tenors" \tenorWords
        \new Lyrics \with { alignBelowContext = #"men" }
          \lyricsto "basses" \bassWords
        % again, we could replace the line above this with the line below.
        % \new Lyrics \lyricsto "basses" \bassWords
      >>
    }

## Estrofa per a solista i tornada a dues veus

Aquesta plantilla crea una partitura que comença amb una estrofa per a solista i continua amb una tornada a dues veus. També mostra l’ús de silencis de separació dins de la variable `\global` per definit canvis de compàs (i altres exemple que són comuns a totes les parts) al llarg de tota la partitura.

    global = {
      \key g \major

      % verse
      \time 3/4
      s2.*2
      \break

      % refrain
      \time 2/4
      s2*2
      \bar "|."
    }

    SoloNotes = \relative g' {
      \clef "treble"

      % verse
      g4 g g |
      b4 b b |

      % refrain
      R2*2 |
    }

    SoloLyrics = \lyricmode {
      One two three |
      four five six |
    }

    SopranoNotes = \relative c'' {
      \clef "treble"

      % verse
      R2.*2 |

      % refrain
      c4 c |
      g4 g |
    }

    SopranoLyrics = \lyricmode {
      la la |
      la la |
    }

    BassNotes = \relative c {
      \clef "bass"

      % verse
      R2.*2 |

      % refrain
      c4 e |
      d4 d |
    }

    BassLyrics = \lyricmode {
      dum dum |
      dum dum |
    }

    \score {
      <<
        \new Voice = "SoloVoice" << \global \SoloNotes >>
        \new Lyrics \lyricsto "SoloVoice" \SoloLyrics

        \new ChoirStaff <<
          \new Voice = "SopranoVoice" << \global \SopranoNotes >>
          \new Lyrics \lyricsto "SopranoVoice" \SopranoLyrics

          \new Voice = "BassVoice" << \global \BassNotes >>
          \new Lyrics \lyricsto "BassVoice" \BassLyrics
        >>
      >>
      \layout {
        ragged-right = ##t
        \context { \Staff
          % these lines prevent empty staves from being printed
          \RemoveEmptyStaves
          \override VerticalAxisGroup.remove-first = ##t
        }
      }
    }

#### LilyPond — Fragments v2.24.4 (branca estable).