// voctest.cc
/*
    Vocoder example for the Sound Processing Kit
    Copyright (C) 2002 Jarno Wuolijoki and Pilvari Pirtola

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public
    License along with this library; if not, write to the Free
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/


#include <spkit/spkit.h>
#include <spkit/apwriter.h>
#include <string.h>
#include "vocoder.h"
#include <math.h>



int main(int argc, char **argv) {
  SPKitAudioPort *ap=0;
  SPKitProcessor *mod=0;
  SPKitProcessor *car=0;
  char *ofname=0;
  int filterCount=10;
  SPKitFloat minFreq=150, maxFreq=6000;


  /*
   *   First parse the command line
   */

  if(argc==1) {
     printf("Usage: voctest -c carrierinputfile.au  
     -m modulatorinputfile.au -o outputfile.au -fc number of filters -fmin minimun 
frequency (default 150) -max maximun frequency (default 6000)");
     exit(0);
    } 


  for (int i=1; i<argc; i++) 
{
    
    if (i<argc-1 && (strcmp(argv[i], "-i")==0 || strcmp(argv[i], "-c")==0)) { 
      
      // -c / -i carrier input

      i++;
      SPKitReader *tmp=new SPKitReader();
      tmp->setInput(argv[i]);
      car=tmp;

    } 
      else if (i<argc-1 && strcmp(argv[i], "-m")==0) { 

      // -m modulator input

      i++;
      SPKitReader *tmp=new SPKitReader();
      tmp->setInput(argv[i]);
      mod=tmp;

    } else if (i<argc-1 && strcmp(argv[i], "-o")==0) { 

      // -o output

      i++;
      ofname=argv[i];

    } else if (i<argc-1 && strcmp(argv[i], "-fc")==0) { 

      // -fc filter count

      i++;
      filterCount=atoi(argv[i]);

    } else if (i<argc-1 && strcmp(argv[i], "-fmin")==0) { 

      // -fmin minimum frequency

      i++;
      minFreq=atof(argv[i]);

    } else if (i<argc-1 && strcmp(argv[i], "-fmax")==0) { 

      // -fmax maximum frequency

      i++;
      maxFreq=atof(argv[i]);

    } else {

      // all invalid options are considered as help requests

      printf("Usage: %s [-c carrier] [-m modulator] [-o output] [-fc filtercount] [-fmin frequency] [-fmax frequency]\n", argv[0]);
      printf("If no carrier/modulator/output files are given, realtime audio in/out will be used.\n\n");
      return 1;
    }
  }


  /*
   *   Setup default inputs if none given
   */

  if (!car || !mod || !ofname) {
    ap=new SPKitAudioPort();
    ap->setChannelCount(1); 
    ap->init();
  }
  if (!car) {
    car=ap;
  }
  if (!mod) {
    mod=ap;
  }



  /*
   *   The actual vocoder object used is right here:
   *   
   *   create - set filters - set inputs
   */

  SPKitVocoder *vox=new SPKitVocoder();
  vox->setDefaultFilters(minFreq, maxFreq, filterCount);
  vox->setModulatorInput(mod);
  vox->setInput(car);


  /*
   *   Create a writer to a file or audioport and loop it thru the data.
   */

  if (ofname) {
    SPKitWriter *output=new SPKitWriter(); 
    output->setInputOutput(vox, ofname);
    output->run();
  } else {
    SPKitAudioPortWriter *output=new SPKitAudioPortWriter();
    output->setInputOutput(vox, ap);
    output->run();
  }
}
