//
// Programmer:    Craig Stuart Sapp <craig@ccrma.stanford.edu>
// Creation Date: Mon Nov 20 17:32:36 PST 2000
// Last Modified: Tue Nov 28 16:53:34 PST 2000
// Last Modified: Wed Jan  1 22:40:22 PST 2003 added Maxwell class access
// Last Modified: Fri Mar 11 09:10:03 PST 2011 added mark system.
// Filename:      ...sig/examples/all/dissic.cpp
// Web Address:   http://sig.sapp.org/examples/museinfo/humdrum/dissic.cpp
// Syntax:        C++; museinfo
//
// Description:   determination of dissonance in context using rules given
//                by John Maxwell in:
//
// Reference:     "An Expert System for Harmonizing Analysis of Tonal
//                Music." pp 335-353 in: "Understanding Music with AI: 
//                Perspectives on Music Cognition." Ed. by Mira Balaban, 
//                Kemal Ebcioglu, and Otto Laske.  MIT Press; 1992.
//                [ISBN 0-262-52170-9]
//
// 

#include "humdrum.h"
#include "PerlRegularExpression.h"

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

// function declarations
void   checkOptions(Options& opts, int argc, char* argv[]);
void   example(void);
void   printAnalysis(HumdrumFile& infile, Array<int>& dissic);
void   usage(const char* command);
void   printTokenWithMarks(const char* token, const char* mark);
void   printAnalysisMark(HumdrumFile& infile, Array<int>& dissic);

// global variables
Options      options;            // database for command-line arguments
int          debugQ     = 0;     // used with --debug option
int          appendQ    = 0;     // used with -a option
int          compoundQ  = 1;     // used with -c option
int          markQ      = 0;     // used with -m option

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

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

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

      Maxwell::analyzeDissonantInContext(infile, dissic);
      if (markQ) {
         printAnalysisMark(infile, dissic);
      } else {
         printAnalysis(infile, dissic);
      }
   }

   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("m|mark=b",      "mark notes, not adding extra spine");   

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

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

}



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

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



//////////////////////////////
//
// printAnalysisMark -- mark dissonant sonorities
//

void printAnalysisMark(HumdrumFile& infile, Array& dissic) {
   int i, j;
   const char* markchar = "@";

   for (i=0; i<infile.getNumLines(); i++) {
      if (!infile[i].isData()) {
         cout << infile[i] << "\n";
         continue;
      }
      for (j=0; j<infile[i].getFieldCount(); j++) {
         if (!infile[i].isExInterp(j, "**kern")) {
            cout << infile[i][j];
            if (j < infile[i].getFieldCount()-1) {
               cout << "\t";
            }
            continue;
         }
         if (strcmp(infile[i][j], ".") == 0) {
            cout << infile[i][j];
         } else if (strchr(infile[i][j], 'r') != NULL) {
            cout << infile[i][j];
         } else {
            if (dissic[i] == DISSIC_YES) {
               printTokenWithMarks(infile[i][j], markchar);
            } else {
               cout << infile[i][j];
            }
         }
         if (j < infile[i].getFieldCount()-1) {
            cout << "\t";
         }
      }
      cout << "\n";
   }
   cout << "!!!RDF**kern: " << markchar << " = " << "marked note color=\"#008000\" (dissonant in context)" << endl;
}



//////////////////////////////
//
// printTokenWithMarks --
//

void printTokenWithMarks(const char* token, const char* mark) {
   PerlRegularExpression pre;
   Array<Array<char> > tokens;
   pre.getTokens(tokens, "\\s+", token);
   int i;
   for (i=0; i<tokens.getSize(); i++) {
      cout << tokens[i] << mark;
      if (i < tokens.getSize() - 1) {
         cout << " ";
      }
   }
}



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

void printAnalysis(HumdrumFile& infile, Array& dissic) {
   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";
            if (dissic[i] == DISSIC_YES) {
               cout << "y";
            } else {
               cout << "n";
            }
            cout << "\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 << "**dissic" << "\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:
            if (dissic[i] == DISSIC_YES) {
               cout << "y";
            } else {
               cout << "n";
            }
            cout << "\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 << "**dissic" << "\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: 268476118ad83fe8e9fb5036fc638785 dissic.cpp [20110317]