//
// Programmer:    Craig Stuart Sapp <craig@ccrma.stanford.edu>
// Creation Date: Mon Mar 21 20:59:18 PST 2005
// Last Modified: Mon Mar 21 20:59:21 PST 2005
// Filename:      ...sig/examples/all/irange.cpp
// Web Address:   http://sig.sapp.org/examples/museinfo/humdrum/irange.cpp
// Syntax:        C++; museinfo
//
// Description:   Identify the range of notes in a score.
// 

#include "humdrum.h"

#include <string.h>
#include <math.h>

// function declarations
void   checkOptions(Options& opts, int argc, char* argv[]);
void   example(void);
void   usage(const char* command);
void   generateAnalysis(HumdrumFile& infile, 
                                 Array< Array<char> >& rdata);
void   printAnalysis(HumdrumFile& infile, 
                                 Array< Array<char> >& rdata);
char   getRange(int keynum, Array<int>& rangeinfo);

// global variables
Options      options;            // database for command-line arguments
int          debugQ     = 0;     // used with the --debug option
int          appendQ    = 0;     // used with the -a option

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

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

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

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

   Array<double> values;
   values.setSize(0);

   Array<Array<char> > rdata;
   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));
      }

      generateAnalysis(infile, rdata);
      printAnalysis(infile, rdata);

   }

   return 0;
}


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

//////////////////////////////
//
// printAnalysis --
//

void printAnalysis(HumdrumFile& infile, Array >& rdata) {
   int i;
   int j;
   cout << "**irange" << "\n";
   for (i=0; i<infile.getNumLines(); i++) {
      if (infile[i].getType() != E_humrec_data) {
         continue;
      }
      if (rdata[i].getSize() == 0) {
         continue;
      }
      for (j=0; j<rdata[i].getSize(); j++) {
         cout << rdata[i][j];
         if (j < rdata[i].getSize() - 1) {
            cout << ' ';
         }
      }
      cout << "\n";
   }
   cout << "*-" << "\n";

}



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

void checkOptions(Options& opts, int argc, char* argv[]) {
   opts.define("a|append=b",    "append analysis to data in output");   

   opts.define("debug=b",       "trace input parsing");   
   opts.define("author=b",      "author of the program");   
   opts.define("version=b",     "compilation information"); 
   opts.define("example=b",     "example usage"); 
   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, Mar 2005" << endl;
      exit(0);
   } else if (opts.getBoolean("version")) {
      cout << argv[0] << ", version: 22 Mar 2005" << 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);
   }

   debugQ = opts.getBoolean("debug");
   appendQ = opts.getBoolean("append");


}



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

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




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

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



//////////////////////////////
//
// printAnalysis --
//

void generateAnalysis(HumdrumFile& infile, Array< Array >& rdata) {
   int i;
   int j;
   int k;

   // fixed range information for now (set to piano range)
   Array<int> rangeinfo;
   rangeinfo.setSize(2);
   rangeinfo.allowGrowth(0);
   rangeinfo[0] = 60;   // boundary between low and mid range
   rangeinfo[1] = 72;   // boundary between mid and hi range
   
   rdata.setSize(infile.getNumLines());
   rdata.allowGrowth(0);


   for (i=0; i<rdata.getSize(); i++) {
      rdata[i].setSize(32);
      rdata[i].setSize(0);
      rdata[i].setGrowth(32);
   }

   char buffer[1024] = {0};
   char rangedatum;
   int tokencount;
   int keynum;
   for (i=0; i<infile.getNumLines(); i++) {
      if (infile[i].getType() != E_humrec_data) {
         continue;
      } 
      for (j=0; j<infile[i].getFieldCount(); j++) {
         if (strcmp(infile[i].getExInterp(j), "**kern") != 0) {
            continue;
         }
         if (strcmp(infile[i][j], ".") == 0) {
            continue;
         }
         tokencount = infile[i].getTokenCount(j);
         for (k=0; k<tokencount; k++) {
            infile[i].getToken(buffer, j, k);         
            if (strcmp(buffer, ".") == 0) {
               continue;
            }
            if (strchr(buffer, 'r') != NULL) {
               // ignore rests
               continue;
            }
            keynum = Convert::kernToMidiNoteNumber(buffer);
            rangedatum = getRange(keynum, rangeinfo);
            rdata[i].append(rangedatum);
         }

      }

   }

}



//////////////////////////////
//
// getRange --
//

char getRange(int keynum, Array& rangeinfo) {
   char output = 'x';

   if (keynum < rangeinfo[0]) { output = 'L'; }
   else if (keynum < rangeinfo[1]) { output = 'M'; }
   else { output = 'H'; }

   return output;
}


// md5sum: 228236952e2a4cf8c13817d0c6acc070 irange.cpp [20050403]