//
// Programmer:    Craig Stuart Sapp <craig@ccrma.stanford.edu>
// Creation Date: Wed Apr  6 15:19:29 PDT 2011
// Last Modified: Wed Apr  6 15:19:32 PDT 2011
// Filename:      ...sig/examples/all/timebase.cpp
// Web Address:   http://sig.sapp.org/examples/museinfo/humdrum/timebase.cpp
// Syntax:        C++; museinfo
//
// Description:   Like Humdrum Toolkit timebase program, but can understand
//                fractional rhythms.
//

#include "humdrum.h"

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

#ifndef OLDCPP
   #include <iostream>
   #include <fstream>
#else
   #include <iostream.h>
   #include <fstream.h>
#endif


// function declarations:
void      checkOptions(Options& opts, int argc, char** argv);
void      example(void);
void      usage(const char* command);
void      printOutput(HumdrumFile& infile, RationalNumber tb);

// User interface variables:
Options   options;
RationalNumber tb;

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

int main(int argc, char** argv) {
   tb = 1;
   // process the command-line options
   checkOptions(options, argc, argv);
   HumdrumFile infile;

   // 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));
      }
      // analyze the input file according to command-line options
      infile.analyzeRhythm("4");
    
      printOutput(infile, tb);
   }

   return 0;
}

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


//////////////////////////////
//
// printOutput -- 
//

void printOutput(HumdrumFile& infile, RationalNumber tb) {
   int i, ii, j;
   int count;
   RationalNumber linedur;
   RationalNumber rcount;

   for (i=0; i<infile.getNumLines(); i++) {
      if (!infile[i].isData()) {
         cout << infile[i] << "\n";
         continue;
      }
      linedur = infile[i].getDurationR();
      if (linedur == 0) {
         // line containing a grace note
         cout << infile[i] << "\n";
         continue;
      }
      rcount = linedur / tb;
      if (rcount.getDenominator() != 1) {
         cerr << "Error on line " << i+1 << ": the duration of the line "
              << "is not an integer multiple of the timebase" << endl;
         exit(1);
      }
      cout << infile[i] << '\n';
      count = rcount.getNumerator() - 1;
      for (ii=0; ii<count; ii++) {
         for (j=0; j<infile[i].getFieldCount(); j++) {
            cout << '.';
            if (j<infile[i].getFieldCount()-1) {
               cout << '\t';
            }
         }
         cout << '\n';
      }
   }
}


//////////////////////////////
//
// checkOptions -- 
//

void checkOptions(Options& opts, int argc, char* argv[]) {
   opts.define("t|timebase=s:1", "duration value for each line");

   opts.define("author=b",  "author of program"); 
   opts.define("version=b", "compilation info");
   opts.define("example=b", "example usages");   
   opts.define("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, Oct 2002" << endl;
      exit(0);
   } else if (opts.getBoolean("version")) {
      cout << argv[0] << ", version: 14 Oct 2002" << 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);
   }
   
   tb = Convert::kernToDurationR(opts.getString("timebase"));

}



//////////////////////////////
//
// example --
//

void example(void) {


}



//////////////////////////////
//
// usage --
//

void usage(const char* command) {

}



// md5sum: c8f34a7f6c04aed3445355b1f4deef3a timebase.cpp [20080518]