(function () {
  "use strict";

  var _ = require("lodash"),
    chordFactory = require("./chords"),
    rhythmFactory = require("./rhythm"),
    melodyFactory = require("./melody"),
    abc = require("./abc");

  var SONG_STRUCTURE = "VVRVRVRR";
  /**

    @lyrics {Array[Array[[String]]} All the lyrics. lyrics[2][3] is the 3rd line of the 2nd verse (0-index natch)
    @inspiration {String} the one good line
    @voicePart {String} "B" for bass
  */
  var generateMusic = function (inputLyrics, inspiration, voicePart) {
    var key = "G";
    voicePart = "B"; // obviously it's a cheat that we secretly change this to bass

    var chords = [];
    var lyrics = [];
    var rhythm = [];
    var melody = [];

    var refrainChords = chordFactory.getChords();
    var refrainLyrics = inputLyrics[0];
    var lyricIndex = 1;
    var refrainRhythm = rhythmFactory.getRhythm(refrainLyrics);
    var refrainMelody = melodyFactory.getMelody(key, voicePart, refrainRhythm, refrainChords);
    var verseChords = chordFactory.getChords();

    _.each(SONG_STRUCTURE, function (stanzaType) {
      var verseLyrics,
        verseRhythm;

      if (stanzaType === 'R') {
        chords.push(refrainChords);
        lyrics.push(refrainLyrics);
        rhythm.push(refrainRhythm);
        melody.push(refrainMelody);
      } else if (stanzaType === 'V') {
        chords.push(verseChords);
        verseLyrics = inputLyrics[lyricIndex++];
        lyrics.push(verseLyrics);
        // TODO: really the rhythm and the melody should be pretty much the same
        // for all verses, with minor tweaks for varying syllable count
        verseRhythm = rhythmFactory.getRhythm(verseLyrics);
        rhythm.push(verseRhythm);
        melody.push(melodyFactory.getMelody(key, voicePart, verseRhythm, verseChords));

      } else {
          // one day a bridge?
      }

    });
    return abc.getAbc(_.flatten(lyrics), _.flatten(chords), _.flatten(melody, true), _.flatten(rhythm, true), inspiration);
  };
  exports.generateMusic = generateMusic;

}());

@@ -50,24 +50,60 @@
 (%22lodash%22),%0A
+    adaptor = require(%22./adaptor%22),%0A
     chordFac
@@ -126,24 +126,24 @@
 ./chords%22),%0A
-
     rhythmFa
@@ -743,16 +743,41 @@
  = %5B%5D;%0A%0A
+    var lyricIndex = 0;%0A%0A
     var 
@@ -858,20 +858,8 @@
 ics%5B
-0%5D;%0A    var 
 lyri
@@ -868,12 +868,11 @@
 ndex
- = 1
+++%5D
 ;%0A  
@@ -1024,24 +1024,25 @@
 ainChords);%0A
+%0A
     var vers
@@ -1076,16 +1076,68 @@
 hords();
+%0A    var firstVerseRhythm;%0A    var firstVerseMelody;
 %0A%0A    _.
@@ -1206,29 +1206,8 @@
 rics
-,%0A        verseRhythm
 ;%0A%0A 
@@ -1529,32 +1529,335 @@
 h(verseLyrics);%0A
+        if (!firstVerseRhythm) %7B%0A          firstVerseRhythm = rhythmFactory.getRhythm(verseLyrics);%0A          firstVerseMelody = melodyFactory.getMelody(key, voicePart, firstVerseRhythm, verseChords);%0A          rhythm.push(firstVerseRhythm);%0A          melody.push(firstVerseMelody);%0A%0A        %7D else %7B%0A  
         // TODO:
@@ -1854,14 +1854,8 @@
   //
- TODO:
  rea
@@ -1884,16 +1884,40 @@
  melody 
+of any subsequent verse 
 should b
@@ -1939,16 +1939,18 @@
 he same%0A
+  
         
@@ -2025,60 +2025,105 @@
     
-verseRhythm = rhythmFac
+  var adaptedVerse = adap
 tor
-y
 .get
-Rhythm(verseLyrics
+AdaptedVerse(verseLyrics, firstVerseRhythm, firstVerseMelody
 );%0A
+  
     
@@ -2138,22 +2138,30 @@
 hm.push(
-v
+adaptedV
 erse
-R
+.r
 hythm);%0A
@@ -2160,32 +2160,34 @@
 hythm);%0A        
+ 
+ 
 melody.push(melo
@@ -2186,75 +2186,39 @@
 ush(
-melodyFactory.getMelody(key, voicePart, verseRhythm, verseChords));
+adaptedVerse.melody);%0A        %7D
 %0A%0A  

use adaptor to make verses similar