//
// Programmer:    Craig Stuart Sapp <craig@ccrma.stanford.edu>
// Creation Date: Wed Nov 22 18:42:05 PST 2000
// Last Modified: Wed Nov 29 14:29:53 PST 2000
// Last Modified: Wed Jan  1 22:55:05 PST 2003 (change Maxwell function style)
// Filename:      ...sig/examples/all/arto.cpp
// Web Address:   http://sig.sapp.org/examples/museinfo/humdrum/arto.cpp
// Syntax:        C++; museinfo
//
// Description:   reduces chordal textures to their unembellished forms.
//

#include "humdrum.h"
#include <ctype.h>

#ifndef OLDCPP
   #include <iostream>
   using namespace std;
#else
   #include <iostream.h>
#endif

// function declarations
int    displayChord(HumdrumFile& infile, Array<int>& chord, int line);
void   generateReduction(HumdrumFile& infile, Array<int>& chord);
double getChordDuration(HumdrumFile& infile, Array<int>& chord, int line);
void   printNewDuration(const char* kern, double duration);

void   checkOptions(Options& opts, int argc, char* argv[]);
void   example(void);
void   usage(const char* command);


// 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 main(int argc, char* argv[]) {
   HumdrumFile infile;
   Array<int>  sonrel;

   // 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::analyzeSonorityRelations(infile, sonrel);
      generateReduction(infile, sonrel);
   }

   return 0;
}



//////////////////////////////
//
// generateReduction -- remove chordal embellishments.
//

void generateReduction(HumdrumFile& infile, Array& chord) {
   int i;
   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:
      case E_humrec_data_comment:
      case E_humrec_data_interpretation:
      case E_humrec_data_measure:
         cout << infile[i].getLine() << "\n";
         break;
      case E_humrec_data:
         i = displayChord(infile, chord, i);
         break;
      }
   }
}



//////////////////////////////
// 
// displayChord -- display only the true chords
//

int displayChord(HumdrumFile& infile, Array& chord, int line) {
   double duration = 0.0;
   int i;
   if (chord[line] == 'c') {
      duration = getChordDuration(infile, chord, line);
      for (i=0; i<infile[line].getFieldCount(); i++) {
         if (strcmp(infile[line][i], ".") == 0) {
            printNewDuration(infile.getDotValue(line, i), duration);
         } else {
            printNewDuration(infile[line][i], duration);
         }
         if (i < infile[line].getFieldCount() - 1) {
            cout << "\t";
         }
      }
      cout << "\n";
   }
   return line;
}

//////////////////////////////
//
// printNewDuration -- 
//

void printNewDuration(const char* kern, double duration) {
   char buffer[128] = {0};
   Convert::durationToKernRhythm(buffer, duration);
   int length = strlen(kern);
   int notprinted = 1;

   for (int i=0; i<length; i++) {
      if (isdigit(kern[i]) && notprinted) {
         cout << buffer;
         notprinted = 0;
         continue;
      }

      if (isdigit(kern[i]) || kern[i] == '.' || kern[i] == '[' ||
          kern[i] == '_' || kern[i] == ']') {
         continue;
      }

      if (tolower(kern[i]) == 'a' || tolower(kern[i]) == 'b' ||
          tolower(kern[i]) == 'c' || tolower(kern[i]) == 'd' ||
          tolower(kern[i]) == 'e' || tolower(kern[i]) == 'f' ||
          tolower(kern[i]) == 'g' || kern[i] == '-' ||
          kern[i] == '#' || kern[i] == '-' || kern[i] == ';') {
         cout << kern[i];
      }

   }

}



//////////////////////////////
//
// getChordDuration -- 
//

double getChordDuration(HumdrumFile& infile, Array& chord, int line) {
   double output = 0;

   output += infile[line].getDuration();

   int i;
   for (i=line+1; i<chord.getSize(); i++) {
      if (chord[i] == 'c') {
         break;
      }
      if (chord[i] == 'p') {
         output += infile[i].getDuration();
      }
      if (chord[i] == 'g') {
         output += infile[i].getDuration();
      }
      if (chord[i] == 'n') {
         break;
      }
   }

   for (i=line-1; i>= 0; i--) {
      if (chord[i] == 'c') {
         break;
      }
      if (chord[i] == 'n') {
         output += infile[i].getDuration();
      }
      if (chord[i] == 'g') {
         break;
      }
      if (chord[i] == 'p') {
         break;
      }
   }

   return output;
}



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

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

}



//////////////////////////////
//
// 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: 82e4cb1ccf446d2eead5adbb2e75ad95 arto.cpp [20050403]