//
// Programmer:    Craig Stuart Sapp <craig@ccrma.stanford.edu>
// Creation Date: Sat Nov  5 01:31:39 PDT 2011
// Last Modified: Sat Nov  5 01:31:41 PDT 2011
// Filename:      ...sig/examples/all/vrange.cpp
// Web Address:   http://sig.sapp.org/examples/museinfo/humdrum/vrange.cpp
// Syntax:        C++; museinfo
//
// Description:   Estimate the vocal fuction of parts.
// 

#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   fillSequence(Array<double>& list, int centerPosition);
void   processFile(HumdrumFile& infile);
void   printDiatonicName(int value);

// global variables
Options      options;            // database for command-line arguments
int          debugQ = 0;         // used with --debug 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> midibins;
   midibins.setSize(128);
   midibins.setAll(0);
   midibins.allowGrowth(0);

   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));
      }

      processFile(infile);
   }

   return 0;
}


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


//////////////////////////////
//
// processFile --
//

void processFile(HumdrumFile& infile) {
   Array<Array<int> > diatonic;
   int i, j, k;
   int track;
   int tcount;
   char buffer[1024] = {0};
   diatonic.setSize(infile.getMaxTracks() + 1);
   for (i=0; i<diatonic.getSize(); i++) {
      diatonic[i].setSize(7*8);
      diatonic[i].setAll(0);
   }

   int value;
   for (i=0; i<infile.getNumLines(); i++) {
      if (!infile[i].isData()) {
         continue;
      }
      for (j=0; j<infile[i].getFieldCount(); j++) {
         if (strcmp(infile[i][j], ".") == 0) {
            continue;
         }
         tcount = infile[i].getTokenCount(j);
         track = infile[i].getPrimaryTrack(j);
         for (k=0; k<tcount; k++) {
            infile[i].getToken(buffer, j, k);         
            if (strchr(buffer, 'r') != NULL) {
               continue;
            }
            if (strchr(buffer, ']') != NULL) {
               continue;
            }
            if (strchr(buffer, '_') != NULL) {
               continue;
            }
            value = Convert::kernToDiatonicPitch(buffer);
            if (value > 0) {
               diatonic[track][value]++;      
            }
         }
      }
   }

   if (debugQ) {
      int startindex = Convert::kernToDiatonicPitch("AAA");
      int endindex = Convert::kernToDiatonicPitch("ccc");
      for (j=startindex; j<endindex; j++) {
         printDiatonicName(j);
         cout << "\t";
         for (i=1; i<diatonic.getSize(); i++) {
            cout << diatonic[i][j];
            if (i < diatonic.getSize()-1) {
               cout << "\t";
            }
         }
         cout << "\n";
      }
   }

}



//////////////////////////////
//
// printDiatonicName --
//

void printDiatonicName(int value) {
   int octave = value / 7;
   int pc = value % 7;
   switch (pc) {
      case 0: cout << "C"; break;
      case 1: cout << "D"; break;
      case 2: cout << "E"; break;
      case 3: cout << "F"; break;
      case 4: cout << "G"; break;
      case 5: cout << "A"; break;
      case 6: cout << "B"; break;
   }
   cout << octave;
}



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

void checkOptions(Options& opts, int argc, char* argv[]) {

   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, Nov 2011" << endl;
      exit(0);
   } else if (opts.getBoolean("version")) {
      cout << argv[0] << ", version: 3 Nov 2011" << 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");

}


//////////////////////////////
//
// fillSequence --
//

void fillSequence(Array& list, int centerPosition) {
   list.setAll(0);
   int& cen = centerPosition;
   if (cen-6 >= 0) { list[cen-6] = 0.0917; }
   if (cen-5 >= 0) { list[cen-5] = 0.1352; }
   if (cen-4 >= 0) { list[cen-4] = 0.2585; }
   if (cen-3 >= 0) { list[cen-3] = 0.3317; }
   if (cen-2 >= 0) { list[cen-2] = 0.4017; }
   if (cen-1 >= 0) { list[cen-1] = 0.4545; }
   list[cen] = 0.5004;
   if (cen+1 < list.getSize()) { list[cen+1] = 0.4573; }
   if (cen+2 < list.getSize()) { list[cen+2] = 0.4002; }
   if (cen+3 < list.getSize()) { list[cen+3] = 0.3442; }
   if (cen+4 < list.getSize()) { list[cen+4] = 0.2304; }
   if (cen+5 < list.getSize()) { list[cen+5] = 0.1485; }
   if (cen+6 < list.getSize()) { list[cen+6] = 0.1058; }
   if (cen+7 < list.getSize()) { list[cen+7] = 0.0957; }
}



//////////////////////////////
//
// 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;
}




// md5sum: e591f5492fe210cef04d4fd0e1536ff7 prange.cpp [20101222]