\version "2.10.10"

\include "english.ly"
\include "../mrlUtil.ly"
\include "../mrlSwash.ly"

#(ly:set-option 'point-and-click #f)

headerMarkup = \markup { "Bull:" \italic "Preludium, Parthenia ix" } 

#(set-global-staff-size 20)

\paper {
  #(set-paper-size "letter")
  top-margin = .4\in
  bottom-margin = .4\in
  left-margin = .75\in
  line-width = 7\in
  between-system-padding = 0.05\in
  page-top-space = 0\in
  indent = 0\in
  %ragged-last-bottom = ##t
  
  myStaffSize = #20
  #(define fonts
    (make-pango-font-tree "Adobe Garamond Pro"
                          "Sans"
                          "Mono"
                           (/ myStaffSize 20)))
                           
                           
    oddHeaderMarkup = \markup
    \fill-line {
      ""
      \on-the-fly #not-first-page \headerMarkup
      \on-the-fly #print-page-number-check-first \fromproperty #'page:page-number-string
    }

    evenHeaderMarkup = \markup
    \fill-line {
      \on-the-fly #print-page-number-check-first \fromproperty #'page:page-number-string
      \on-the-fly #not-first-page \headerMarkup
      ""
    }
}

%smallCapsACode = #smallCapsACodeLL
%oldStyleZeroCode = #oldStyleZeroCodeLL

engravedLine = #(string-append "Engraved by LilyPond " (lilypond-version))

\header {
	composer = \markup { \realCaps "Dr John Bull" \oldStyleNum "(c. 1562–1628)" }
    %subtitle = ""
    %poet = ""
    opus = \markup { \italic "Parthenia" \oldStyleNum "(1612/13)," "ix" }
	title = \markup { \medium \italic \makeSwashMarkupG "Preludium" }
    tagline = ""
    copyright = \markup { \smaller \center-align {
        \oldStyleNum "Edition copyright © 2008 by Michael Lauer."
        "This edition may be freely distributed, duplicated, performed, or recorded." 
        \oldStyleNum \engravedLine } } 

}

#(define-markup-command (fcol layout props markups) (markup-list?)
  "Print a column of fingering indications--use a reasonable baseline-skip."
  (interpret-markup layout props
    (markup
        (#:left-align
        (#:override '(baseline-skip . 1.5)
            (#:finger (make-center-align-markup markups) ))))))

                
orn = #(define-music-function (parser location m) (ly:music?)
    (define (addToChord x) 
        (if (eq? (ly:music-property x 'name) 'EventChord)
            (let* ((els (ly:music-property x 'elements))
                   (dur-log (ly:duration-log (ly:music-property (car els) 'duration)))
                   (tremLen (if (< dur-log 3) 16 (expt 2 (+ dur-log 2))))
                   (ev (make-music 'TremoloEvent 'tremolo-type tremLen))
                   (newEls (append els (list ev))))
                  (ly:music-set-property! x 'elements newEls)
                  x)
            x)
        )
    (let* ((added (music-map addToChord m))) 
#{
    \once \override Voice.StemTremolo #'beam-thickness = #.25
    $added
#})
)

fic = \once \set suggestAccidentals = ##f

global = {
  % main
  %\set Staff.forceClef = ##t
  %\set Staff.forceTime = ##f
  %\set Timing.defaultBarType = ""

  \set Staff.midiInstrument = "harpsichord"
  
  \override Staff.VerticalAxisGroup #'minimum-Y-extent = #'(-1 . 1)
  %#(override-auto-beam-setting '(end 1 16 * *) 1 4)
  #(override-auto-beam-setting '(end 1 16 * *) 2 4)
  %#(override-auto-beam-setting '(end 1 16 * *) 3 4)
  #(override-auto-beam-setting '(end 1 16 * *) 4 4)

  \set followVoice = ##t
  
  \key g \mixolydian
  
  \time 2/2
  %\set subdivideBeams = ##t
  %\set Score.beatLength = #(ly:make-moment 1 4)
  \skip 1*16
  \bar "|."

}

up = \tag #'scoreOnly { \change Staff = treble }
down = \tag #'scoreOnly { \change Staff = bass }

voiceI = 
    \relative g'' {
        \context Voice = "I"
        \voiceOne
        \set fingeringOrientations = #'(up)
        
        g2 g4 g |
        r16 g16 fs e d c b a g4 a |
        b8[ c] e[ d] c4 b |
        a16 b c a b c d e fs g e fs d e c d |
        b8[ a] c[ b] a8 g4 \orn fs8 |
        g16 d e f g a b c d e f g a[ g fs e] |
        d16 c b a g f e d e f d e cs d b cs |
        d a b c d e fs g a d, e fs g a b c |
        
        d4. c8 b8[ g] b[ a] |
        c8[ b] c[ a] b8[ a] b[ g] |
        a8[ b c a] c8[ b a g] |
        fs16 e fs g a b c d e d c b a g fs e |
        \repeat unfold 6 { g16[ fs] } g16 fs e fs |
        g2 g2 |
        g1 <g b,>1\fermata |
    }
    
voiceII =
    \relative b' {
        \context Voice = "II"
        \voiceTwo
        \set fingeringOrientations = #'(up)
        
        <b d>2 <b d>4 <b d> |
        s1*12 |
        b,4 e d c4~ |
        c4 c \orn b a |
        \orn e'4 d d2 |
    }
    
voiceIII =
    \relative g {
        \context Voice = "III"
        \voiceOne
        \set fingeringOrientations = #'(down)
        
        g8[ d' \orn e f] g16 g, f' e d c b a |
        <d g>2 r16 g, a b c d e fs |
        g16 d e fs g a b g a16 a, e' fs g g, d' e |
        fs4 g a8[ g] fs[ e] |
        d16 e f d e c d b c[ d e b c a] d8\noBeam |
        b8[ c] e8[ d c b] a[ c] |
        b8[ c d b] c8[ b a g] |
        fs8[ g b a] a[ g fs e] |
        
        d16 e f g a g a fs g a b c d e fs g |
        a16[ a, d e] a,[ e' c d] g,[ g' c, d] g,[ g' c, e] |
        a,16[ fs' d g] a,[ a' d, fs] a,[ a' e g] a,[ fs'] e8 |
        d4 c8[ b] a8[ e fs g] |
        <a d>1 |
        \times 4/3 { e32[ d e d c b] } c16 d e f 
        \times 4/3 { g32[ f g f e d] } \times 4/3 { f32 e f e d c } |
        e2 d4 c |
    }

voiceIV =
    \relative g {
        \context Voice = "IV"
        \voiceTwo
        \set fingeringOrientations = #'(down)
        s1 |
        g2 s2 |
        s1 |
        d'2 d |
        s1 |
        g,4 g2 fs4 |
        g2 e |
        d2 d |
        
        s1*3 |
        r8 d4.~ d2 |
        d1 |
        s2 b4 c |
        g1 |
        <g g'>1\fermata |
    }
    
    
theMusic =  
\context PianoStaff <<
	\context Staff = "treble" <<
	    \removeWithTag #'lower \global 
	    \clef treble
	    \voiceI
	    \voiceII 
	>>
	\context Staff = "bass" <<
	    \global
	    \clef bass
        \voiceIII
        \voiceIV
	>>
    >>
    
% a different staff for each voice
midiMusic =  
\context StaffGroup <<
	\context Staff = "treble" <<
	    \removeWithTag #'lower \global 
	    \clef treble
	    \voiceI
	>>
	\context Staff = "treble II" <<
	    \removeWithTag #'lower \global 
	    \clef treble
	    \voiceII 
	>>
	\context Staff = "bass" <<
	    \global
	    \clef bass
        \voiceIII
	>>
	\context Staff = "bass  II" <<
	    \global
	    \clef bass
        \voiceIV
	>>
    >>
    
\score {
  %\removeWithTag #'incipit \theMusic
  \theMusic
  \layout {
    \context {
      \Score
      \override MetronomeMark #'transparent = ##t
      \override BarNumber #'padding = #2
      %\override BarLine #'transparent = ##t
      %\remove "System_start_delimiter_engraver"
       beatLength = #(ly:make-moment 1 4)
  
    }
    \context {
      \Voice
      \override StemTremolo #'beam-thickness = #.3
      \override Accidental #'cautionary-style = #'smaller 
      \override AccidentalSuggestion #'cautionary-style = #'smaller 
      \override NoteHead #'style = #'baroque
      subdivideBeams = ##t
    }
    \context {
      \Staff
      %\remove "Time_signature_engraver"
      \consists "Custos_engraver"
      \override Custos #'style = #'mensural
      \override Custos #'stencil = #(lambda (grob)
          (interpret-markup
               (ly:grob-layout grob)
               '(())
               (markup #:musicglyph "custodes.mensural.u0" )))
      \override Custos #'X-offset = #-3.2
      \override Custos #'extra-offset = #'(1.2 . 0.0)
      \override Custos #'space-alist = #'((right-edge minimum-distance . 0 ))
    }
  }
}

\score {
  \unfoldRepeats \keepWithTag #'midiOnly \midiMusic
  
  \midi {
    \context {
      \Score
      tempoWholesPerMinute = #(ly:make-moment 40 2)
      }
    }



 }