//
// Programmer:    Craig Stuart Sapp <craig@ccrma.stanford.edu>
// Creation Date: Wed Oct 16 22:51:28 PDT 2002
// Last Modified: Wed Oct 16 22:51:31 PDT 2002
// Filename:      ...sig/doc/examples/sig/sigfile/setsrate/setsrate.cpp
// Syntax:        C++; sig
// 
// Description: Set the sampling rate of a soundfile to the secified 
//	sampling rate value without changing the data.  Currently
//	only works with WAVE files.
//

#include "sigAudio.h"

#include <stdlib.h>

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

void checkOptions(Options& opts);
void example(void);
void usage(const char* command);


// command line intervace variables:
int srate = 44100;   // used with the -r option

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

int main(int argc, char* argv[]) {
   Options options(argc, argv);
   checkOptions(options);

   // first check if the file is probably a WAVE soundfile
   #ifndef OLDCPP
      #ifdef VISUAL
         fstream soundfile(options.getArg(1), ios::in | ios::out | ios::binary);
      #else
         fstream soundfile(options.getArg(1), ios::in | ios::out);
      #endif
   #else
      #ifdef VISUAL
         fstream soundfile(options.getArg(1), ios::in | ios::out | ios::nocreate | ios::binary);
      #else
         fstream soundfile(options.getArg(1), ios::in | ios::out | ios::nocreate);
      #endif
   #endif

   if (!soundfile.is_open()) {
      cout << "Error: cannot open soundfile: " << options.getArg(1) << endl;
      exit(1);
   }

   union { long i; unsigned char c[4]; } data;
   union { long i; unsigned char c[4]; } sdata;

   soundfile.read((char*)data.c, 4);
   if (data.c[0] != 'R' || data.c[1] != 'I' || data.c[2] != 'F'
         || data.c[3] != 'F') {
      cout << "The soundfile: " << options.getArg(1) << " is not a WAVE file" 
	   << endl;
      exit(1);
   }

   sdata.i = (long)srate;
   data.c[0] = sdata.c[3];
   data.c[1] = sdata.c[2];
   data.c[2] = sdata.c[1];
   data.c[3] = sdata.c[0];
   soundfile.seekp(24);
   soundfile.write((char*)sdata.c, 4);
   soundfile.close();

   return 0;
}


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


//////////////////////////////
//
// checkOptions -- handle command-line options.
//

void checkOptions(Options& opts) {
   opts.define("s|r|samplerate|rate|srate=i:44100");

   opts.define("author=b");
   opts.define("version=b");
   opts.define("example=b");
   opts.define("help=b");
   opts.process();

   if (opts.getBoolean("author")) {
      cout << "Written by Craig Stuart Sapp, "
           << "craig@ccrma.stanford.edu, Oct 2002" << endl;
      exit(0);
   }
   if (opts.getBoolean("version")) {
      cout << "compiled: " << __DATE__ << endl;
      cout << SIG_VERSION << endl;
      exit(0);
   }
   if (opts.getBoolean("help")) {
      usage(opts.getCommand());
      exit(0);
   }
   if (opts.getBoolean("example")) {
      example();
      exit(0);
   }

   // can only have one filename
   if (opts.getArgCount() == 0) {
      cout << "Error: need one soundfile name." << endl;
      usage(opts.getCommand());
      exit(1);
   } else if (opts.getArgCount() > 1) {
      cout << "Error: too many arguments.  Given " 
           << opts.getArgCount() << " but need only 1." << endl;
      usage(opts.getCommand());
      exit(1);
   }

   srate = opts.getInteger("samplerate");

}
   


//////////////////////////////
//
// example -- gives example calls to the osc program.
//

void example(void) {
   cout << 
   "# setsrate examples:                                                     \n"
   << endl;
}



//////////////////////////////
//
// usage -- how to run the osc program on the command line.
//

void usage(const char* command) {
   cout << 
   "                                                                         \n"
   "Creates a frequency and amplitude varying sinusoid.                      \n"
   "                                                                         \n"
   "Usage: " << command << " [-s srate] soundfile                            \n"
   "                                                                         \n"
   "Options:                                                                 \n"
   "   -s = sampling rate to set the soundfile to                            \n"
   "   --options = list of all options, aliases and default values.          \n"
   "                                                                         \n"
   "                                                                         \n"
   << endl;
}



// md5sum: 4ff1da78a2f59dd1380cbfcee64b9276 setsrate.cpp [20050403]