/***************************************************************************
 *   Copyright (C) 2007 by tr   *
 *   oleg@pallant-mobile.ru   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program 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 General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/


#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <iostream>
#include <cstdlib>


#include "wav.h"
#include "parsertools.h"

using namespace std;



int main(int argc, char *argv[])
{
  if ( argc <= 2 ) { 
    cout << "usage: wavextractor <file.wav> <res.bin>" << endl;
    return 0;
  }
  
  BinReader src;
  cout << "file: " << argv[1] << endl;  
  if ( !src.Open(argv[1]) ) {
    cout << "file not found: " << argv[1] << endl;
    return -1;
  };
  
  cout << "size: " << src.size() << " bytes" << endl;
  
  while ( true ) {
    TWaveChunkHeader hdr;    
    TWaveFormatex fmt;
    
    src.Read(&hdr, sizeof(TWaveChunkHeader));
    string chunkName = string(hdr.iName);
    chunkName.resize(4);
    
//cout << chunkName << endl;
//cout << hdr.iSize << endl;

    
    
    if (chunkName == "RIFF") {
      char rt[5];
      rt[4] = 0;
      src.Read(rt, 4);      
      cout << "type: " << rt << endl;       
      if ( strcmp(rt, "WAVE") != 0 ) {
        cout << "unsupported type" << endl;
        return -1;
      }
    } else if (chunkName == "fmt ") {
      src.Read(&fmt, sizeof(TWaveFormatex));
      cout << "format: " << fmt.iFormatTag << endl;
      if ( fmt.iFormatTag != 1) {
        cout << "unsupported format" << endl;
        return -1;      
      }
      cout << "channels: " << fmt.iChannels << endl;
      cout << "sample rate: " << fmt.iSamplePerSec << " Hz" << endl;
      cout << "bytes per sec: " << fmt.iAvgBytesPerSec << endl;            
    } else if (chunkName == "data") {
      int sz = hdr.iSize - 8;
      cout << "total samples: " << sz/ fmt.iBlockAlign << endl;  
      cout << "durability: " << (double(sz)/fmt.iAvgBytesPerSec) << " sec" << endl;
      
      unsigned char *data = new unsigned char[sz];
      src.Read(data, sz);
      for (unsigned int i = 0; i < sz; i++ )
         if ( data[i] == 0 )
            data[i]++;
      BinWriter res;
      if ( !res.Open(argv[2]) ) {
        cout << "can't create out file: " << argv[2] << endl;
        return -1;
      }
      int delay = 16;
      res.Write(&delay, 1);
      res.Write(data, sz);
/*      
      delay = 0x00;
      res.Write(&delay, 1);
      delay = 0x80;
      for (unsigned int i = sz+2; i < 32*1024; i++)
         res.Write(&delay, 1);
*/      
      delete[] data;
      return 0;
    }
    

  }

  return EXIT_SUCCESS;
}
