//
// Programmer:    Craig Stuart Sapp <craig@ccrma.stanford.edu>
// Creation Date: Mon Nov 20 12:22:41 PST 2000
// Last Modified: Mon Nov 20 12:22:44 PST 2000
// Filename:      ...sig/examples/all/noteset.cpp
// Web Address:   http://sig.sapp.org/examples/museinfo/humdrum/noteset.cpp
// Syntax:        C++; museinfo
//
// Description:   Collects all notes found in <tt>**kern</tt> spines in the
//                input Hudrum data into one spine formatted with given options.
//                Useful for converting Humdrum **kern data into another format.
//

#include "humdrum.h"

#include <string.h>

#define STYLE_BASE40 1
#define STYLE_MIDI   2
#define STYLE_ASCII  3


// function declarations
void   checkOptions(Options& opts, int argc, char* argv[]);
void   example(void);
void   printAnalysis(HumdrumFile& infile);
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          countQ     = 0;     // used with the -c option
int          pcQ        = 0;     // used with the -p option

int          nlflag     = 0;     // used for note list style.
int          styleType  = STYLE_ASCII ;

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


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

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

      printAnalysis(infile);
   }

   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|count=b",     "count notes instead of display the list");   
   opts.define("p|pitchclass=b","ignore octave registers on notes");   
   opts.define("s|sort=b",      "sort notes from lowest to highest");   
   opts.define("u|uniq=b",      "display only one instance of each pitch");   
   opts.define("f|fill=b",      "fill the values of null records first");   
   opts.define("m|midi=b",      "display results in MIDI note numbers");   
   opts.define("b|base40=b",    "display results in base-40 note number");   

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

   appendQ = opts.getBoolean("append");
   countQ = opts.getBoolean("count");

   if (opts.getBoolean("sort")) {
      nlflag |= NL_SORT;
   }
   if (opts.getBoolean("uniq")) {
      nlflag |= NL_UNIQ;
   }
   if (opts.getBoolean("fill")) {
      nlflag |= NL_FILL;
   }
   if (opts.getBoolean("pitchclass")) {
      pcQ = 1;
      nlflag |= NL_PC;
   } else {
      pcQ = 0;
   }

   if (opts.getBoolean("midi")) {
      nlflag |= NL_MIDI;
      styleType = STYLE_MIDI;
   } 
   if (opts.getBoolean("base40")) {
      nlflag |= NL_NOMIDI;
      styleType = STYLE_BASE40;
   }
   
}



//////////////////////////////
//
// example -- example usage of the quality program
//

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

//////////////////////////////
//
// printNoteArray --
//

void printNoteArray(Array& notes) {
   if (countQ) {
      cout << notes.getSize();
      return;
   }
   if (notes.getSize() == 0) {
      cout << "r";
      return;
   } 

   int i;

   if (styleType == STYLE_BASE40) {
      for (i=0; i<notes.getSize(); i++) {
         if (notes[i] == E_base40_rest) {
            cout << 'r';
         } else {
            cout << notes[i];
         }
         if (i < notes.getSize() - 1) {
            cout << ' ';
         }
      }
      return;
   } 

   if (styleType == STYLE_MIDI) {
      for (i=0; i<notes.getSize(); i++) {
         if (notes[i] < 0) {
            cout << 'r';
         } else {
            cout << notes[i];
         }
         if (i < notes.getSize() - 1) {
            cout << ' ';
         }
      }
      return;
   } 

   if (styleType == STYLE_ASCII) {
      char buffer[128] = {0};
      for (i=0; i<notes.getSize(); i++) {
         if (notes[i] < 0) {
            cout << 'r';
         } else {
            if (pcQ) {
               Convert::base40ToKern(buffer, notes[i] + 4 * 40);
            } else {
               Convert::base40ToKern(buffer, notes[i]);
            }
            cout << buffer;
         }
         if (i < notes.getSize() - 1) {
            cout << ' ';
         }
      }
      return;
   } 

   cout << "XXX";
}



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

void printAnalysis(HumdrumFile& infile) {
   int i;
   Array<int> notes;

   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";
            infile.getNoteList(notes, i, nlflag);
            printNoteArray(notes);
            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"
                    << "**notes" << "\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:
            infile.getNoteList(notes, i, nlflag);
            printNoteArray(notes);
            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 << "**notes" << "\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: aab48a47161d3a6aead660c622607b48 noteset.cpp [20050403]