//
// Programmer:    Craig Stuart Sapp <craig@ccrma.stanford.edu>
// Creation Date: Fri Dec 24 20:43:18 PST 2004
// Last Modified: Fri Dec 24 20:43:21 PST 2004
// Filename:      ...sig/examples/all/beat.cpp
// Web Address:   http://sig.sapp.org/examples/museinfo/humdrum/conductor.cpp
// Syntax:        C++; museinfo
//
// Description:   Generates conductor track for radio baton.
//

#include "humdrum.h"

#include <string.h>
#include <ctype.h>


// function declarations
void      checkOptions(Options& opts, int argc, char* argv[]);
void      example(void);
void      usage(const char* command);
void      printOutput(HumdrumFile& file);

// global variables
Options   options;            // database for command-line arguments
int       appendQ  = 0;       // used with -a option
int       prependQ = 1;       // used with -a option
int       absQ     = 0;
int       sumQ     = 0;
int       durQ     = 0;
int       beatQ    = 0;
int       zero     = 0;


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

int main(int argc, char* argv[]) {
   HumdrumFile infile, outfile;

   // process the command-line options
   checkOptions(options, argc, argv);

   // figure out the number of input files to process
   int numinputs = options.getArgCount();

   for (int i=0; i<numinputs || i==0; i++) {
      infile.clear();
      outfile.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));
      }
      // analyze the input file according to command-line options
      infile.analyzeRhythm();

      printOutput(infile);
   }

   return 0;
}


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

//////////////////////////////
//
// printOutput --
//

void printOutput(HumdrumFile& file) {

   for (int i=0; i<file.getNumLines(); i++) {
      switch (file[i].getType()) {
         case E_humrec_data_comment:
            if (appendQ) {
               cout << file[i] << "\t" << "!" << "\n";
            } 
            break;
         case E_humrec_data_kern_measure:
            if (appendQ) {
               cout << file[i] << "\t";
            } 
            cout << file[i][0] << "\n";
            break;
         case E_humrec_interpretation:
            if (appendQ) {
               cout << file[i] << "\t";
            }
            if (strncmp(file[i][0], "**", 2) == 0) {
               if (absQ) {
                  cout << "**absbeat" << "\n";
               } else if (durQ) {
                  cout << "**dur" << "\n";
               } else {
                  cout << "**beat" << "\n";
               }
            } else if (strcmp(file[i][0], "*-") == 0) {
               cout << "*-" << "\n";
            } else {
               if (appendQ) {
                  cout << "*\n";
               }
            }
            break;
         case E_humrec_data:
            if (appendQ) {
               cout << file[i] << "\t";
            } 
            if (durQ) {
               cout << file[i].getDuration() << "\n";
            } else if (absQ) {
               cout << file[i].getAbsBeat() << "\n";
            } else {
               cout << file[i].getBeat() << "\n";
            }
            break;
         case E_humrec_none:
         case E_humrec_empty:
         case E_humrec_global_comment:
         case E_humrec_bibliography:
         default:
            cout << file[i] << "\n";
            break;
      }
   }


}



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

void checkOptions(Options& opts, int argc, char* argv[]) {
   opts.define("a|assemble=b");           // assemble analysis with input
   opts.define("base|timebase=s:");       // rhythmic unit of one beat
   opts.define("b|beat=b");               // display beat position of note in measure
   opts.define("t|total-beat=b");         // absoute beat location
   opts.define("d|dur|duration=b");       // display rhymic duration of records
   opts.define("s|sum=b");                // sum the duration of each measure
   opts.define("z|zero|zero-offset=b");   // first beat is represented by a 0
   opts.define("n|nulls|keep-nulls=b");   // doesn't remove nulls with -s option
   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, Oct 2000" << endl;
      exit(0);
   } else if (opts.getBoolean("version")) {
      cout << argv[0] << ", version: 28 May 2002" << 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);
   }

   if (options.getBoolean("zero")) {
      zero = 1;
   }
   appendQ = opts.getBoolean("assemble");
   durQ    = opts.getBoolean("duration");
   absQ    = opts.getBoolean("total-beat");
   sumQ    = opts.getBoolean("sum");
   beatQ   = opts.getBoolean("beat");
  
   // display beat information if no other output option is given.
   if (!(absQ || durQ || sumQ)) {
      beatQ = 1;
   }
}
  


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

void example(void) {
   cout << endl;
}



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

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



// md5sum: e36031ca1c69e141187861cf7651d0f1 conductor.cpp [20090626]