\version "2.7.40"
\include "english.ly"
\header {
  composer = "Adam Spiers"
  title = "The Most Tune"
}

intro = {
  \override Score.RehearsalMark
    #'break-align-symbols = #'(left-edge clef)
  \override Score.RehearsalMark
    #'self-alignment-X = #LEFT

    \mark \markup { \box Intro }
  cs4 fs8 b,~b4 a | 
  e'8 gs,~ gs2.~ | 
  gs1 \break
}

Asection = {
  \mark \markup { 
    \box A
    \hspace #3
    \musicglyph #"scripts.segno"
  }

  \repeat volta 2 {
    as8 cs fs^\markup {
      \italic "legato, slur freely"
    } cs b as gs fs | 
    b( fs as) gs~gs4 fs8( gs) |
    a? gs fs e d b e d | 
    cs4 fs8 b,~ b4 a8( b) | \break
    
    cs8 fs cs' fs, b as gs fs |
    b8 cs fs e~e4 cs8 a? | 
  }
  \alternative {
    { 
      cs8 fs fs, cs'~ cs4 fs,8 b~ | 
      b2 a8 b~b4 |
    }
    { 
      cs8 fs fs, cs'~ cs4 fs,8 b~ | 
      b2. gs8( as) | \break
    }
  }
}

Bsection = {
  \mark \markup { \box B }

  b4 fs'8( b,) b( as fs) as(~ |
  as8 b16 as gs2) gs8( as) |
  b4 a'?8( b,) b( as fs) cs'~ |
  cs2. cs8( d) | \break

  e4. e8 e c~c f~ | 
  f8 f8 bf2. |
  
  \once \override Score.RehearsalMark 
    #'self-alignment-X = #RIGHT
  \mark \markup { 
    "last time to" \hspace #1
    \musicglyph #"scripts.coda" 
  }

  cs,?4 fs?8 b,~b4 a4 |
  e'8 gs,8~gs2. 
  
%   \set Score.repeatCommands = 
%     #'((volta "2, 5") end-repeat)
  \bar "||" 

  \once \override Score.RehearsalMark
    #'self-alignment-X = #RIGHT
  \mark \markup {
    "D.S. al Coda"
    \hspace #1
    \musicglyph #"scripts.coda"
  }
  
  s1 \bar "" \break
}

outro = {
  \mark \markup { \musicglyph #"scripts.coda" }

  \time 15/8
  \relative c'' {
    cs?4 fs?8 b,4. 
    a4 e'8 gs,4.~gs8 a( b) |

    \time 12/8
    cs?4 fs?8 b,4. 
    a4 e'8 gs,4 cs8 |

    fs,4 b8 e,4 a8 d,?4 g8
    cs,4.\fermata |
    \bar "|."
  } 
}

tune = {
  \new Staff = "tune" \with { 
    \consists "Volta_engraver"
  } {
    \time 4/4 
    \key a \major

    \relative c'' {
      \intro
      \Asection
      \Bsection
      \outro
    }
  }
}

allchords = {
  \new ChordNames = "chords" {
    \chordmode { 
      \set minorChordModifier = \markup { "-" }
      %\set additionalPitchPrefix = ""

      % intro
      ds4.:m d4.:maj7.11+ b4:min7 | s8 cs:sus4 s2. | s1
      
      % A section
      fs1 | e:maj7.11+ | d:maj7.11+ | b4.:min d s4 |
      fs1/cs | d:maj7 | 
      % 1st time
      g:maj7.11+ | s |
      % 2nd time
      g2.:maj7.11+ s8 c:maj7 | s1
      
      % B section
      gs1:min7 | cs:7 | 
      g2:maj7.11+ gs4.:min7.5- b8:min7.5- | s1
      a2:maj7 f4.:maj7 bf8:maj7 | s4 ef2.:maj7
      ds4.:min7 d4.:maj7.11+ b4:min7 | s8 cs:sus4 s2. |

      s1
      
      % Coda
      ds4.:min7 d:maj7.11+ b:min7 cs:sus4 s |
      ds4.:min7 d:maj7.11+ b:min7 a:maj7.5+ |
      gs4.:min7 g:maj7.11+ e:min7 d:maj7.5+ |
    }
  }
}

showBarLines = {
  \override RhythmicStaff.BarLine
    #'transparent = ##f
}

hideBarLines = {
  \override RhythmicStaff.BarLine
    #'transparent = ##t
}

showOneBarLine = {
  \once \override RhythmicStaff.BarLine
    #'transparent = ##f
}
      
pushes = {
  \new RhythmicStaff \with {
    % This one causes a warning
    % \remove Staff_symbol_engraver
    \override StaffSymbol #'line-count = #0
    \remove Time_signature_engraver
    
    fontSize = #-3
    \override StaffSymbol 
      #'staff-space = #(magstep -6)

%     \override StaffSpacing 
    \override VerticalAxisGroup 
	#'default-staff-staff-spacing = 
	#'((basic-distance . 0)
	    (padding . -10))
    
    \override BarLine #'transparent = ##t

    alignAboveContext = 
      %#"tune"
      #"chords"
  }
  \relative c'
  \new Voice {
%     \improvisationOn \teeny
    % intro
    c4. c8~c4 c | s1 | s |
    
    % A section
    s1 | s | s | s4. c8~c4 s |
    s1 | s | 
    % 1st time
    s | s | 
    % 2nd time
    s2 s4. c8~ | 
    \showOneBarLine c2. s4 |

    % B section
    s1 | s | \showOneBarLine
    c2 c4. c8~ | \showOneBarLine c2. s4 |
    c2 c4. c8~ | \showBarLines c4 c2. | 
    c4. c8~c4 c | r8 c~ c2. | 
    \hideBarLines
    
    s1

    % Coda
    c4. c c c r \showBarLines | 
    c4. c c c   | 
    c4. c c c\fermata   | 
    \hideBarLines
  }
}

\score {
  <<
    { \pushes }

    %\transpose ds d
    \allchords

    %\transpose ds d
    \tune
  >>
}
  
%\include "letter.ly"
\paper {
  %  #(set-paper-size "a4")
  #(set-paper-size "letter")
  annotate-spacing = ##t
% top-markup-spacing #'basic-distance = #8
% markup-system-spacing #'basic-distance = #0
% system-system-spacing #'basic-distance = #8
}
#(ly:set-option 'debug-skylines)

\layout { 
  indent = #0
  \context {
    \Score
    \remove "Volta_engraver"
  }
}

\midi {
  \context {
    \Score 
    tempoWholesPerMinute = #(ly:make-moment 144 4)
  }
}