//
// Programmer:    Craig Stuart Sapp <craig@ccrma.stanford.edu>
// Creation Date: Sat Dec 16 13:17:40 PST 2000
// Last Modified: Sat Dec 16 13:17:43 PST 2000
// Filename:      ...sig/examples/all/cliche.cpp
// Web Address:   http://sig.sapp.org/examples/museinfo/humdrum/cliche.cpp
// Syntax:        C++; museinfo
//
// Description:   search for identical textures in a musical score.
//
// Note: The program is not yet finished.
//
// -u       = match only using non-repeated notes
// -p       = match only using pitch classes
// -d       = duration to match
// -i       = display cliches in integer form (default it alphabetic)
// -b       = match by exact beat locations
// -n       = minimum occurances of the cliche in order to mark it
// -m       = match by exact meter position
// -t       = match by transposition
// etc.
//
//
//

#include "humdrum.h"

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

// function declarations
void   checkOptions(Options& opts, int argc, char* argv[]);
void   example(void);
void   printAnalysis(HumdrumFile& infile, Array<int>& cliche, 
                                      int max);
void   usage(const char* command);
char*  getClicheSymbol(char* buffer, int cliche, int maxcount,  
                                   int style);

// 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          compoundQ  = 1;     // used with the -c option
int          pcQ        = 0;     // used with the -p option
double       duration   = 1.0;   // used with the -d option
int          minimum    = 1;     // used with the -n option
int          integerQ   = 0;     // used with the -i option

char         buffer[1000] = {0};

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

int main(int argc, char* argv[]) {
   HumdrumFile infile;
   Array<int> cliche;

   // 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();

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

      infile.analyzeRhythm();
      int max = infile.analyzeCliche(cliche, duration, minimum);
      printAnalysis(infile, cliche, max);
   }

   return 0;
}


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


//////////////////////////////
//
// 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("C|compound=b",  "don't try to use compound meters");   
   opts.define("p|pitch-class=b", "match by pitch class (ignore octaves");
   opts.define("d|duration=d:1.0", "match music with this length");
   opts.define("m|minimum=i:1", "minimum number of cliches for a match");
   opts.define("i|integer=b", "display cliches in integer format");
 

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

   if (opts.getBoolean("compound")) {
      compoundQ = 0;
   } else {
      compoundQ = 0;
   }

   duration = opts.getDouble("duration");
   if (duration <= 0) {
      duration = 1;
   }

   minimum =opts.getInteger("minimum");
   if (minimum < 1) {
      minimum = 1;
   }

   integerQ = opts.getBoolean("integer");
}



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

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


//////////////////////////////
//
// getClicheSymbol --
//

char* getClicheSymbol(char* buffer, int cliche, int maxcount, int style) {
   if (style == 1) {
      if (cliche > 0) {
         sprintf(buffer, "%d", cliche);
      } else {
         buffer[0] = '.';
         buffer[1] = '\0';
      }
      return buffer;
   }

   if (cliche == 0) {
      buffer[0] = '.';
      buffer[1] = '\0';
      return buffer;
   }

   int copy = abs(cliche);
   int digits = (int)(log10((double)copy)/log10(26.0) + 1);

   buffer[0] = '\0';
   if (cliche < 0) {
      buffer[0] = '@';
      buffer[1] = '\0';
   }
   char letter[2];
   letter[1] = '\0';

   int i;
   int current = copy-1;
   int digit;
   for (i=digits; i>0; i--) {
      digit = current % (int)pow(26.0, i);
      letter[0] = 'a' + digit;
      current -= (int)(digit * pow(26.0, i));
      strcat(buffer, letter);
   }

   return buffer;
}



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

void printAnalysis(HumdrumFile& infile, Array& cliche, int max) {
   int i;
   if (appendQ) {
      for (i=0; i<infile.getNumLines(); i++) {
         switch (infile[i].getType()) {
         case E_humrec_global_comment:
         case E_humrec_bibliography:
         case E_humrec_none:
         case E_humrec_empty:
            cout << infile[i].getLine() << "\n";
            break;
         case E_humrec_data:
            cout << infile[i].getLine() << "\t";
            cout << getClicheSymbol(buffer, cliche[i], max, integerQ) << "\n";
            break;
         case E_humrec_data_comment:
            if (infile[i].equalFieldsQ("**kern")) {
               cout << infile[i].getLine() << "\t"
                    << infile[i][0] << "\n";
            } else {
               cout << infile[i].getLine() << "\t!\n";
            }
            break;
         case E_humrec_data_measure:
            if (infile[i].equalFieldsQ("**kern")) {
               cout << infile[i].getLine() << "\t"
                    << infile[i][0] << "\n";
            } else {
               cout << infile[i].getLine() << "\t=\n";
            }
            break;
         case E_humrec_data_interpretation:
            if (strncmp(infile[i][0], "**", 2) == 0) {
               cout << infile[i].getLine() << "\t";
               cout << "**cliche" << "\n";
            } else if (infile[i].equalFieldsQ("**kern")) {
               cout << infile[i].getLine() << "\t"
                    << infile[i][0] << "\n";
            } else {
               cout << infile[i].getLine() << "\t*\n";
            }
            break;
         }
      }

   } else {

      for (i=0; i<infile.getNumLines(); i++) {
         switch (infile[i].getType()) {
         case E_humrec_global_comment:
         case E_humrec_bibliography:
         case E_humrec_none:
         case E_humrec_empty:
            cout << infile[i].getLine() << "\n";
            break;
         case E_humrec_data:
            cout << getClicheSymbol(buffer, cliche[i], max, integerQ) << "\n";
            break;
         case E_humrec_data_comment:
            if (infile[i].equalFieldsQ("**kern")) {
               cout << infile[i][0] << "\n";
            } else {
               // do nothing
            }
            break;
         case E_humrec_data_measure:
            if (infile[i].equalFieldsQ("**kern")) {
               cout << infile[i][0] << "\n";
            } else {
               cout << "\t=\n";
            }
            break;
         case E_humrec_data_interpretation:
            if (strncmp(infile[i][0], "**", 2) == 0) {
               cout << "**cliche" << "\n";
            } else if (infile[i].equalFieldsQ("**kern")) {
               cout << infile[i][0] << "\n";
            } else {
               // do nothing
            }
            break;
         }
      }
   }
}



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

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


// md5sum: 1ffbe64df35a7bd7fc74ccee3537192a cliche.cpp [20050403]