\include "deutsch.ly" 

\version "2.16.1"

%#(ly:set-option 'point-and-click #f)


%--------------------
% Maintainer: Sven Kaiser / e-mail: noten(at)oemsel.de
% es_war_ein_koenig_in_thule_sa-tb.ly
% 2013-05-26

\header
  {
    title = "Der König in Thule"    
    poet =  \markup \tiny {"Text: 1774 Johann Wolfgang von Goethe (1749 – 1832)"}
    composer = \markup \tiny {"Musik: 1812 Carl Friedrich Zelter (1758 – 1832)"}
    arranger =\markup \tiny {"Bearbeitung: Georg Schumann (1866 – 1952)"}
    tagline = \markup \tiny \italic \center-column { {
               {"© 2009 by Georg Schumann Gesellschaft, Berlin – www.georgschumanngesellschaft.de"} \line
               {"Nach der Erstausgabe (Leipzig, 1915) herausgegeben von Klaus Rettinghaus. Die Ausgabe ist unter der Verpflichtung  "} \line
               {"der Nennung des Copyright bzw. der Urheberschaft und des Herausgebers frei verfüg- und kopierbar."} \line 
               {"Redaktionelle Bearbeitung 2013 Rev. 1 von S. Kaiser mit http://lilypond.org   -   Notendownload: www.oemsel.de/noten"} } }
  }

#(set-global-staff-size 19)

\paper
  {
    paper-width = 21.0\cm
    paper-height = 29.7\cm
    top-margin = 0.8\cm
    bottom-margin = 1.3\cm
    left-margin = 2\cm
    right-margin = 2\cm
    print-page-number = ##f
  }

global = { \key a \minor \time 6/4 \tieDashed } 

SopNote = \relative c'
  {
    \overrideProperty #"Score.NonMusicalPaperColumn"
      #'line-break-system-details #'((Y-offset . 14) )
    \repeat volta 1 
      {
        \partial 4 
        \once \override TextScript #'X-offset = #-7.0
        \once \override TextScript #'Y-offset = #5.8
        e4
          ^\markup \column {{ \bold {"Sanft und frei"}\line 
                            \tiny \italic {"Melodie im Bass hervorheben, die Oberstimmen durchaus leise"}}}
          -\tweak #'X-offset #-3.8
          -\tweak #'Y-offset #1.8 ^\p | %1
        c'2 c4 h4.~ h8 h4 | %2
        a2. g2 g4 | %3
        \break
        \overrideProperty #"Score.NonMusicalPaperColumn"
          #'line-break-system-details #'((Y-offset . 60) )
        f2 e8~ e8 \noBeam d2 f4 | %4
        e1 r4 h'4 | %5
        c2 c4 d4.~ d8 \noBeam d4 | %6
        \break
        \overrideProperty #"Score.NonMusicalPaperColumn"
          #'line-break-system-details #'((Y-offset . 104) )
        e2. d2 d8~ d8 \noBeam | %7
        c2 a8~ a8 \noBeam h2 gis4 | %8
        \once \override Tie
          #'control-points = #'((1.5 . 0.6) (2 . 3.2) (24 . 3.2) (26.2 . 0.6))
        \tieSolid a1. ~  | %9
        a1 r4 %10
        \bar "|." 
      }
    }

AltNote = \relative c' 
  {
    \repeat volta 1 
      {
        \partial 4
        e4
          -\tweak #'X-offset #-4.0
          -\tweak #'Y-offset #-2.2 _\p %1
        a2 a4 g4.~ g8 g4 | %2
        f2. e2 e4 | %3
        d2 c8_~ c8 \noBeam h2 h4 | %4
        h1 r4 e4 | %5
        e2 e4 f4._~ f8 \noBeam f4 | %6
        e2. f2 a8~ a8 | %7
        a2 f8~ f8 f2 e4 | %8
        \once \override Slur
          #'control-points = #'((1.5 . -2.8) (2 . -4) (20 . -8) (26.2 . -3.8))
        e2.( f2 d4 | %9
        cis1) \slurDown  r4  %10
        \bar "|." 
    } 
  }

TenNote = \relative c' 
  {
    \repeat volta 1
      {
        \partial 4
        e4
          -\tweak #'X-offset #-3.8
          -\tweak #'Y-offset #1.8 ^\p | %1
        e2 e4 e4.~ e8 e4 | %2
        c2. c2 c4 | %3
        a2 a8~ a8 \noBeam a2 a4 | %4
        gis1 r4 gis4 | %5
        a2 a4 h4.~ h8 \noBeam h4 | %6
        c2. a2 f'8~ f8 | %7
        e2 c8~ c8 d2 h4 | %8
        c2( cis4 d2 f,4 | %9
        e1) r4 %10
        \bar "|." 
      } 
  }
   
BasNote = \relative a
  {
    \repeat volta 1
      {
        \partial 4
        e4	
          -\tweak #'X-offset #-4.8
          -\tweak #'Y-offset #-2.5 _\mf %1
        a2 a4 e4.~ e8 e4 | %2
        f2. c2 c4 | %3
        d2 e8~ e8 \noBeam f2 d4 | %4
        e1 r4 e4 | %5
        a2 a4 g4._~ g8 \noBeam g4 | %6
        c,2. d2 d8~ d8 \noBeam | %7
        e2 f8~ f8 \noBeam d2 e4 | %8
        \tieSolid a,1.-\tweak #'Y-offset #-0.5 ~ | %9
        a1 r4 %10
        \bar "|." 
      } 
  }
   
TxtLnA = \lyricmode {
\set stanza = "1. " 
Es
war ein \set ignoreMelismata = ##t Kö -- nig \unset ignoreMelismata in |
Thu -- le gar |
treu bis __ an das |
Grab, dem |
ster -- bend sei -- ne |
Buh -- le \set ignoreMelismata = ##t ei -- nen \unset ignoreMelismata |
gold -- nen __ Be -- cher |
gab. __ |
} 

TxtLnB = \lyricmode {
\set stanza = "2. " 
Es
ging ihm nichts __ dar -- |
ü -- ber, er |
leert ihn __ je -- den |
Schmaus; die |
Au -- gen \set ignoreMelismata = ##t gin -- gen \unset ignoreMelismata ihm |
ü -- ber, so __ |
oft er __ trank dar -- |
aus. __ |
} 

TxtLnC = \lyricmode {
\set stanza = "3. " 
Und 
als er kam __ zu |
ster -- ben, zählt |
er \set ignoreMelismata = ##t sei -- ne \unset ignoreMelismata Städt im |
Reich, gönnt |
al -- les sei -- nem | 
Er -- ben, den __ |
Be -- cher __ nicht zu -- |
gleich. __ |
} 

TxtLnD = \lyricmode {
\set stanza = "4. " 
Er |
saß beim Kö -- nigs -- |
mah -- le, die |
Rit -- ter __ um ihn |
her, auf |
ho -- hem Vä -- ter -- |
saa -- le, dort __ |
auf dem __ Schloß am |
Meer. __ |
} 

TxtLnE = \lyricmode {
\set stanza = "5. " 
Dort |
stand der al -- te |
Ze -- cher, trank |
letz -- te __ Le -- bens -- |
glut, und |
warf den heil -- gen |
Be -- cher hin -- 
un -- ter __ in die |
Flut. __ |
}

TxtLnF = \lyricmode {
\set stanza = "6. " 
Er |
sah ihn stür -- zen, |
trin -- ken und |
sin -- ken __ tief ins |
Meer. Die |
Au -- gen \set ignoreMelismata = ##t tä -- ten \unset ignoreMelismata ihm |
sin -- ken, trank |
nie \set ignoreMelismata = ##t ei -- nen \unset ignoreMelismata Trop -- fen  |
mehr. __ |
}

%% Override RestCollision
%% Usage:
%%   \new Staff \with {
%%     \override RestCollision #'positioning-done = #merge-rests-on-positioning
%%   } << \somevoice \\ \othervoice >>
%% or (globally):
%%   \layout {
%%     \context {
%%       \Staff
%%       \override RestCollision #'positioning-done = #merge-rests-on-positioning
%%     }
%%   } 
%%
%% Limitations:
%% - only handles two voices
%% - does not handle multi-measure/whole-measure rests

#(define (rest-score r)
  (let ((score 0)
	(yoff (ly:grob-property-data r 'Y-offset))
	(sp (ly:grob-property-data r 'staff-position)))
    (if (number? yoff)
	(set! score (+ score 2))
	(if (eq? yoff 'calculation-in-progress)
	    (set! score (- score 3))))
    (and (number? sp)
	 (<= 0 2 sp)
	 (set! score (+ score 2))
	 (set! score (- score (abs (- 1 sp)))))
    score))

#(define (merge-rests-on-positioning grob)
  (let* ((can-merge #f)
	 (elts (ly:grob-object grob 'elements))
	 (num-elts (and (ly:grob-array? elts)
			(ly:grob-array-length elts)))
	 (two-voice? (= num-elts 2)))
    (if two-voice?
	(let* ((v1-grob (ly:grob-array-ref elts 0))
	       (v2-grob (ly:grob-array-ref elts 1))
	       (v1-rest (ly:grob-object v1-grob 'rest))
	       (v2-rest (ly:grob-object v2-grob 'rest)))
	  (and
	   (ly:grob? v1-rest)
	   (ly:grob? v2-rest)	     	   
	   (let* ((v1-duration-log (ly:grob-property v1-rest 'duration-log))
		  (v2-duration-log (ly:grob-property v2-rest 'duration-log))
		  (v1-dot (ly:grob-object v1-rest 'dot))
		  (v2-dot (ly:grob-object v2-rest 'dot))
		  (v1-dot-count (and (ly:grob? v1-dot)
				     (ly:grob-property v1-dot 'dot-count -1)))
		  (v2-dot-count (and (ly:grob? v2-dot)
				     (ly:grob-property v2-dot 'dot-count -1))))
	     (set! can-merge
		   (and 
		    (number? v1-duration-log)
		    (number? v2-duration-log)
		    (= v1-duration-log v2-duration-log)
		    (eq? v1-dot-count v2-dot-count)))
	     (if can-merge
		 ;; keep the rest that looks best:
		 (let* ((keep-v1? (>= (rest-score v1-rest)
				      (rest-score v2-rest)))
			(rest-to-keep (if keep-v1? v1-rest v2-rest))
			(dot-to-kill (if keep-v1? v2-dot v1-dot)))
		   ;; uncomment if you're curious of which rest was chosen:
		   ;;(ly:grob-set-property! v1-rest 'color green)
		   ;;(ly:grob-set-property! v2-rest 'color blue)
		   (ly:grob-suicide! (if keep-v1? v2-rest v1-rest))
		   (if (ly:grob? dot-to-kill)
		       (ly:grob-suicide! dot-to-kill))
		   (ly:grob-set-property! rest-to-keep 'direction 0)
		   (ly:rest::y-offset-callback rest-to-keep)))))))
    (if can-merge
	#t
	(ly:rest-collision::calc-positioning-done grob))))


% Aufbau SATB mit SA- und TB-Notenzeilen und (mehrstrophigen) Text zwischen den Notenzeilen
\score {
	\new ChoirStaff << 

		\new Staff = "SopAltStf" << 
			\clef "G" 
			\set Staff.instrumentName = \markup {\center-column {"Sopran " \line {"Alt"}}}
			\new Voice = "SopVce" { \voiceOne << 
				\autoBeamOn 
				{ \global \SopNote } 
				>>
			}
			\new Voice = "AltVce" { \voiceTwo << 
				\autoBeamOff 
				{ \global \AltNote } 
				>>
			} 
		>> 

		\new Lyrics = CenTxtLnA 
		\new Lyrics = CenTxtLnB 
		\new Lyrics = CenTxtLnC 
		\new Lyrics = CenTxtLnD 
		\new Lyrics = CenTxtLnE 
		\new Lyrics = CenTxtLnF 

		\new Staff = TenBasStf << 
			\clef "F" 
			\set Staff.instrumentName = \markup {\center-column {"Tenor " \line {"Bass"}}}
			\new Voice = "TenVce" { \voiceOne << 
				\autoBeamOff 
				{ \global \TenNote } 
				>>
			} 
			\new Voice = "BasVce" { \voiceTwo << 
				\autoBeamOn 
				{ \global \BasNote } 
				>>
			} 
		>> 
	
	\context Lyrics = CenTxtLnA \lyricsto SopVce \TxtLnA 
	\context Lyrics = CenTxtLnB \lyricsto SopVce \TxtLnB 
	\context Lyrics = CenTxtLnC \lyricsto SopVce \TxtLnC 
	\context Lyrics = CenTxtLnD \lyricsto SopVce \TxtLnD 
	\context Lyrics = CenTxtLnE \lyricsto SopVce \TxtLnE 
	\context Lyrics = CenTxtLnF \lyricsto SopVce \TxtLnF 
	
	>> 

	\layout { 
	% Steuert die Höhe der Taktnummerierung über dem System
          \context { \Score \override BarNumber #'padding = #2.0 } 
        % Pausenzeichen aus zwei Stimmen zusammenfassen
          \context { \Staff \override RestCollision #'positioning-done = #merge-rests-on-positioning }
        % Mindestabstand Textbindestriche
          \context { \Lyrics  \override LyricHyphen #'minimum-distance = #1.2 }
	}
}

\score {
  \unfoldRepeats 
    {
      <<
        \new Voice = "Sopran" { \autoBeamOff \SopNote }
        \new Voice = "Alt" { \autoBeamOff \AltNote }
        \new Voice = "Tenor" { \autoBeamOff \TenNote }
        \new Voice = "Bass" { \autoBeamOff \BasNote }
      >>
    }	
  \midi { }
}
