// file: $isip/class/mmedia/AudioFile/adf_09.cc
// version: $Id: adf_09.cc 9280 2003-08-02 15:00:05Z parihar $
//

// isip include files
//
#include "AudioFile.h"
#include <VectorByte.h>
#include <VectorShort.h>
#include <VectorLong.h>

// method: writeAudioData
//
// arguments:
//  Vector<VectorFloat>& data: (input) the audio data to write
//  long ctag: (input) channel tag
//  
// return: the number of elements written
//
// this method writes the audio data to raw audio file
//
long AudioFile::writeAudioData(Vector<VectorFloat>& data_a,
			       long ctag_a) {
  
  // make sure the file is open
  //
  if (!isOpen()) {
    Error::handle(name(), L"writeAudioData", ERR_NOTOPN, __FILE__, __LINE__);
    return -1;
  }
  
  // write to raw file
  //
  if (file_format_d == RAW) {

    // call the raw method
    //
    return writeRawData(data_a, ctag_a);
  }

  // Sof file
  //
  else if (file_format_d == SOF) {

    // call the sof method
    //
    return writeSofData(data_a, ctag_a);
  }

  // sphere not yet supported
  //
  else if (file_format_d == SPHERE) {

    // call the sphere method
    //
    return writeSphereData(data_a, ctag_a);
  }
  
  // else format supported bu sgi's audiofile library
  //
  else {

    // call the AF method
    //
    return writeAFData(data_a, ctag_a);
  }

  // exit gracefully
  //
  return true;
}

// method: revertData
//
// arguments:
//  VectorLong& whole_data: (output) the Vector to hold all channel data
//  Vector<VectorFloat>& data: (input) the input multi-channel data
//  long ctag: (input) the tag of channels to be processed 
//
// return: a boolean value indicating status
//
// this method combines the data of multiple channels and put into a
// VectorLong
//
boolean AudioFile::revertData(VectorLong& whole_data_a,
			      Vector<VectorFloat>& data_a,
			      long ctag_a) const {
  
  // local variables
  //
  long num_channels = 0;
  float sample_value;
  
  // get the number of channels to write
  //
  if (ctag_a >= 0) {
    num_channels = 1;
  }
  else if (ctag_a == CHANNEL_TAG_ALL) {
    num_channels = num_channels_d;
  }
  else {
    num_channels = 1;
  }
  
  // compute the total number of samples
  //
  long num_samples = data_a(0).length();
  long num_write = num_samples * num_channels;

  // set the output data
  //
  whole_data_a.setLength(num_write);
  
  if (ctag_a >= 0) {
    
    for (long i = 0; i < num_samples; i++) {

      sample_value = (float)(data_a(ctag_a)(i)) * max_sample_val_d;
      whole_data_a(i) = (long)Integral::round(sample_value);
    }
  }

  else if (ctag_a == CHANNEL_TAG_ALL) {

    //    return Error::handle(name(), L"revertData", ERR, __FILE__, __LINE__);
    
    for (long i = 0; i < num_samples; i++) {
      
      // loop each channel sample
      //
      for (long ctag = 0; ctag < num_channels; ctag++) {

	sample_value = ((float)data_a(ctag)(i)) * max_sample_val_d;
	
	whole_data_a((long)(i*num_channels + ctag)) =
	  (long)Integral::round(sample_value);
	
      }
    }
  }
  
  // exit gracefully
  //
  return true;
}
