* Modes in the Key of C
** Processes
*** Generate Bass Line

#+name: generate_bassline(root="")
#+begin_src ruby :results silent

one_bar =
"#{root},,8 #{root}, #{root}, #{root}, " + 
"#{root},, #{root}, #{root}, #{root}16 #{root},16 | " 
 one_bar + one_bar + one_bar + one_bar

#+end_src

*** Generate Triad

#+name: generate_triad_sequence(root="c",chord="c")
#+begin_src ruby :results silent

@key_of_c = ["c","d","e","f","g","a","b"]
@key_of_g = ["g","a","b","c","d","e","fis"]

def triad(key, root, chord, duration)
  note = key.index(root) + (chord - 1)
  "< " +
  key.fetch(key.index(root)) + "' "+
  key.fetch(note % 7)       + "'" + # * (note       < 7 ? 1 : 2) + " " +
  key.fetch((note + 2) % 7) + "'" + # * ((note + 2) < 7 ? 1 : 2) + " " +
  key.fetch((note + 4) % 7) + "'" + # * ((note + 4) < 7 ? 1 : 2) + " " +
  ">" + " " + duration.to_s
end

 triad(@key_of_c, root, chord, 4) + 
 triad(@key_of_c, root, chord, 4) + 
 triad(@key_of_c, root, chord, 4) + 
 triad(@key_of_c, root, chord, 4) 

#+end_src

*** Generate arpeggio

#+name: generate_arpeggio(root="c",ascending="t")
#+begin_src ruby :results silent

@key_of_c = ["c","d","e","f","g","a","b"]
@key_of_g = ["g","a","b","c","d","e","fis"]

def note(key, root, value)
  value = key.index(root) + value
  note_value = key.fetch(value % 7)
  pitch = "'"
  pitch += "'" if value >= 7
  note_value + pitch + "16 "
end

def arpeggio(key, root, ascending)
  if ascending == 1
    x,y = 0, 1
  else 
    x,y  = 7, -1
  end
  notes = ""
  8.times do  
    notes += note(key, root, x)
    x = x + y
  end  
  notes + "r2 " 
end

arpeggio @key_of_c, root, ascending

#+end_src

** Version

#+begin_src lilypond 

\version "2.12.3"

#+end_src

** Notes
*** Arpeggios

#+begin_src lilypond 

Carp = {
  <<generate_arpeggio(root="c",ascending=1)>>
  <<generate_arpeggio(root="c",ascending=0)>>
}

Garp = {
  <<generate_arpeggio(root="g",ascending=1)>>
  <<generate_arpeggio(root="g",ascending=0)>>
}

Darp = {
  <<generate_arpeggio(root="d",ascending=1)>>
  <<generate_arpeggio(root="d",ascending=0)>>
}

Aarp = {
  <<generate_arpeggio(root="a",ascending=1)>>
  <<generate_arpeggio(root="a",ascending=0)>>
}

Earp = {
  <<generate_arpeggio(root="e",ascending=1)>>
  <<generate_arpeggio(root="e",ascending=0)>>
}

Barp = {
  <<generate_arpeggio(root="b",ascending=1)>>
  <<generate_arpeggio(root="b",ascending=0)>>
}

Farp = {
  <<generate_arpeggio(root="f",ascending=1)>>
  <<generate_arpeggio(root="f",ascending=0)>>
}

#+end_src

*** Triads

#+begin_src lilypond

Ctriads = {
  <<generate_triad_sequence(root="c",chord=4)>>
  <<generate_triad_sequence(root="c",chord=5)>>
}

Gtriads = {
  <<generate_triad_sequence(root="g",chord=4)>>
  <<generate_triad_sequence(root="g",chord=5)>>
}

Dtriads = {
  <<generate_triad_sequence(root="d",chord=4)>>
  <<generate_triad_sequence(root="d",chord=5)>>
}

Atriads = {
  <<generate_triad_sequence(root="a",chord=4)>>
  <<generate_triad_sequence(root="a",chord=5)>>
}

Etriads = {
  <<generate_triad_sequence(root="e",chord=4)>>
  <<generate_triad_sequence(root="e",chord=5)>>
}

Btriads = {
  <<generate_triad_sequence(root="b",chord=4)>>
  <<generate_triad_sequence(root="b",chord=5)>>
}

Ftriads = {
  <<generate_triad_sequence(root="f",chord=4)>>
  <<generate_triad_sequence(root="f",chord=5)>>
}

#+end_src

** Drums (four bars)

#+begin_src lilypond
  
  DrumsFourBars = {
    \drummode {
      bd16 hh16 hh16 hh16 sn16 hh16 hh16 hh16 
      bd16 hh16 hh16 hh16 sn16 hh16 hh16 hh16 |
      bd16 hh16 hh16 hh16 sn16 hh16 hh16 hh16 
      bd16 hh16 hh16 hh16 sn16 hh16 hh16 bd16 |
      bd16 hh16 hh16 hh16 sn16 hh16 hh16 hh16 
      bd16 hh16 hh16 hh16 sn16 hh16 hh16 hh16 |
      bd16 hh16 hh16 hh16 sn16 hh16 hh16 hh16 
      bd16 hh16 hh16 hh16 sn16 hh16 sn16 bd16 |
    }
  }
  
#+end_src

** Number of bars to compile (showLastLength)

#+begin_src lilypond

%  showLastLength = R1*8
 
#+end_src

** Score
#+begin_src lilypond

  \score {

  <<

    \new Staff {
      \relative c' 
      \key c \major
      
       \set Staff.midiInstrument = #"acoustic grand"
        \Barp  \Barp
        \Earp  \Earp
        \Aarp  \Aarp
        \Darp  \Darp
        \Garp  \Garp
        \Carp  \Carp
        \Farp  \Farp

        \Carp  \Carp
        \Garp  \Garp
        \Darp  \Darp
        \Aarp  \Aarp
        \Earp  \Earp
        \Barp  \Barp
        \Farp  \Farp
        \Carp  \Carp
    }

    \new Staff {
      \relative c' 
      \key c \major
       \set Staff.midiInstrument = #"acoustic grand"
        \Btriads  \Btriads
        \Etriads  \Etriads
        \Atriads  \Atriads
        \Dtriads  \Dtriads
        \Gtriads  \Gtriads
        \Ctriads  \Ctriads
        \Ftriads  \Ftriads
        
        \Ctriads  \Ctriads
        \Gtriads  \Gtriads
        \Dtriads  \Dtriads
        \Atriads  \Atriads
        \Etriads  \Etriads
        \Btriads  \Btriads
        \Ftriads  \Ftriads
        \Ctriads  \Ctriads
 
    }

    \new Staff {
      \clef bass
      \relative c 
      \key c \major
       \set Staff.midiInstrument = #"slap bass 2"
      <<generate_bassline(root="b")>>
      <<generate_bassline(root="e")>>
      <<generate_bassline(root="a")>>
      <<generate_bassline(root="d")>>
      <<generate_bassline(root="g")>>
      <<generate_bassline(root="c")>>
      <<generate_bassline(root="f")>>
      
      <<generate_bassline(root="c")>>
      <<generate_bassline(root="g")>>
      <<generate_bassline(root="d")>>
      <<generate_bassline(root="a")>>
      <<generate_bassline(root="e")>>
      <<generate_bassline(root="b")>>
      <<generate_bassline(root="f")>>
      <<generate_bassline(root="c")>>

    }

    \new DrumStaff {
      \DrumsFourBars
      \DrumsFourBars
      \DrumsFourBars
      \DrumsFourBars
      \DrumsFourBars
      \DrumsFourBars
      \DrumsFourBars

      \DrumsFourBars
      \DrumsFourBars
      \DrumsFourBars
      \DrumsFourBars
      \DrumsFourBars
      \DrumsFourBars
      \DrumsFourBars
      \DrumsFourBars
    }
  
  >>
    
  \layout {
    }
    \midi {
      \context {
        \Score
        tempoWholesPerMinute = #(ly:make-moment 60 4)
      }
    }
  
  }
    
#+end_src

** Paper

#+begin_src lilypond 

\paper {
  #(define dump-extents #t) 
  
  indent = 0\mm
  line-width = 200\mm - 2.0 * 0.4\in
  ragged-right = #""
  force-assignment = #""
  line-width = #(- line-width (* mm  3.000000))
}

#+end_src

** Header

#+begin_src lilypond

\header {
  title = \markup \center-column {"Modes in the Key of C"} 
  composer =  \markup \center-column { "Music by" \small "Martyn Jago" }
  poet =  \markup \center-column { "ob-lilypond" \small "example 2" }
}

#+end_src