//
// Programmer:    Craig Stuart Sapp <craig@ccrma.stanford.edu>
// Creation Date: Mon Jun  7 17:19:40 PDT 2010
// Last Modified: Wed Jun  9 16:29:44 PDT 2010 (added -s option)
// Last Modified: Wed Jun 16 14:42:14 PDT 2010 (added tied durations)
// Filename:      ...sig/examples/all/musebeat.cpp
// Web Address:   http://sig.sapp.org/examples/museinfo/humdrum/musebeat.cpp
// Syntax:        C++; museinfo
//
// Description:   Print the record type for each line in a MuseData file.
//                Basic debugging program for parsing MuseData files.
//

#include "MuseData.h"
#include "Options.h"
#include "museinfo.h"

#ifndef OLDCPP
   using namespace std;
#endif
   
// function declarations
void      checkOptions(Options& opts, int argc, char* argv[]);
void      example(void);
void      usage(const char* command);
void      printData(MuseData& infile);
void      printDataSorted(MuseData& infile);

// global variables
Options   options;             // database for command-line arguments
int       linenumberQ = 0;     // used with -l option
int       ticklineQ   = 0;     // used with -t option
int       tiedurQ     = 0;     // used with -T option
int       rationalQ   = 0;     // used with -r option
int       sortQ       = 0;     // used with -s option
int       repeatQ     = 0;     // used with -R option

///////////////////////////////////////////////////////////////////////////

int main(int argc, char* argv[]) {
   checkOptions(options, argc, argv);
   int numinputs = options.getArgCount();
   MuseData infile;

   for (int i=0; i<numinputs || i==0; i++) {
      infile.clear();

      // if no command-line arguments read data file from standard input
      if (numinputs < 1) {
         infile.read(cin);
      } else {
         infile.read(options.getArg(i+1));
      }
      if (sortQ) {
         printDataSorted(infile);
      } else {
         printData(infile);
      }
	   
   }

   return 0;
}


///////////////////////////////////////////////////////////////////////////


//////////////////////////////
//
// printDataSorted --
//

void printDataSorted(MuseData& infile) {
   int i;
   int j;
   for (i=0; i<infile.getEventCount(); i++) {
      if (tiedurQ) {
         if (rationalQ) {
            infile.getEvent(i).getTime().printTwoPart(cout);
         } else {
            cout << infile.getEvent(i).getTime().getFloat();
         }
      } else {
         if (rationalQ) {
            infile.getEvent(i).getTime().printTwoPart(cout);
         } else {
            cout << infile.getEvent(i).getTime().getFloat();
         }
      }

      j = 0;
      cout << '\t';
      cout << "L:"; cout << infile.getLineIndex(i, j);    cout << '\t';
      cout << "l:"; cout << infile.getLineDuration(i, j); cout << '\t';
      cout << "D:"; cout << infile.getNoteDuration(i, j); cout << '\t';
      cout << "d:"; cout << infile.getTiedDuration(i, j); cout << '\t';
      cout << "t:"; cout << infile.getLastTiedNoteLineIndex(i, j);
      cout << ","; cout << infile.getNextTiedNoteLineIndex(i, j); cout << '\t';
      cout << "T:"; cout << (char)infile.getType(i, j);   cout << '\t';
      cout << infile.getEvent(i)[j].getLine();
      cout << endl;

      for (j=1; j<infile.getEvent(i).getEventCount(); j++) {
         if (repeatQ) {
            if (rationalQ) {
               infile.getEvent(i).getTime().printTwoPart(cout);
            } else {
               cout << infile.getEvent(i).getTime().getFloat();
            }
         }

         cout << '\t';
         cout << "L:"; cout << infile.getLineIndex(i, j);    cout << '\t';
         cout << "l:"; cout << infile.getLineDuration(i, j); cout << '\t';
         cout << "D:"; cout << infile.getNoteDuration(i, j); cout << '\t';
         cout << "d:"; cout << infile.getTiedDuration(i, j); cout << '\t';
         cout << "t:"; cout << infile.getLastTiedNoteLineIndex(i, j);
         cout << ","; cout << infile.getNextTiedNoteLineIndex(i, j); 
	 cout << '\t';
         cout << "T:"; cout << (char)infile.getType(i, j);   cout << '\t';
         cout << infile.getEvent(i)[j].getLine();
         cout << endl;
      }
   }
}



//////////////////////////////
//
// printData --
//

void printData(MuseData& infile) {
   int i;
   for (i=0; i<infile.getNumLines(); i++) {
      if (linenumberQ) {
         cout << i+1 << ':';
      }
      if (ticklineQ) {
         cout << infile.getLineTickDuration(i);
      } else if (rationalQ) {
         infile.getAbsBeatR(i).printTwoPart(cout);
      } else {
         cout << infile.getAbsBeat(i);
      }

      cout << '\t';
      cout << infile[i] << endl;
   }

}



//////////////////////////////
//
// checkOptions -- validate and process command-line options.
//

void checkOptions(Options& opts, int argc, char* argv[]) {
   opts.define("l|linenumber=b", "print the line number of each record");
   opts.define("t|tickline=b",   "print duration of each line in ticks");
   opts.define("T|tie-duration=b", "print tied duration note (first only)");
   opts.define("r|rational=b",   "print cumulative duration of each line");
   opts.define("R|repeat=b",     "print absolute time for a line when sort");
   opts.define("s|sort=b",       "print int time-sorted order");

   opts.define("debug=b");              // determine bad input line num
   opts.define("author=b");             // author of program
   opts.define("version=b");            // compilation info
   opts.define("example=b");            // example usages
   opts.define("h|help=b");             // short description
   opts.process(argc, argv);
   
   // handle basic options:
   if (opts.getBoolean("author")) {
      cout << "Written by Craig Stuart Sapp, "
           << "craig@ccrma.stanford.edu, June 2010" << endl;
      exit(0);
   } else if (opts.getBoolean("version")) {
      cout << argv[0] << ", version: 7 June 2010" << endl;
      cout << "compiled: " << __DATE__ << endl;
      cout << MUSEINFO_VERSION << endl;
      exit(0);
   } else if (opts.getBoolean("help")) {
      usage(opts.getCommand());
      exit(0);
   } else if (opts.getBoolean("example")) {
      example();
      exit(0);
   }

   linenumberQ = opts.getBoolean("linenumber");
   ticklineQ   = opts.getBoolean("tickline");
   tiedurQ     = opts.getBoolean("tie-duration");
   rationalQ   = opts.getBoolean("rational");
   repeatQ     = opts.getBoolean("repeat");
   sortQ       = opts.getBoolean("sort");
}



//////////////////////////////
//
// example -- example usage of the program
//

void example(void) {
   cout <<
   "                                                                         \n"
   << endl;
}



//////////////////////////////
//
// usage -- gives the usage statement for the program
//

void usage(const char* command) {
   cout <<
   "                                                                         \n"
   << endl;
}


// md5sum: 035b33f13bd242a243aefccd7c1e74c8 musebeat.cpp [20100618]