// file: $isip/class/mmedia/AudioFile/adf_04.cc
// version: $Id: adf_04.cc 9266 2003-07-15 19:33:59Z parihar $
//

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

// method: write
//
// arguments:
//  Sof& sof: (input) sof file object
//  long tag: (input) sof object instance tag
//  const String& name: (input) sof object instance name
//
// return: a boolean value indicating status
//
// this method has the object read itself from an Sof file
//
boolean AudioFile::write(Sof& sof_a, long tag_a, const String& name_a) const {

  long obj_size;

  if (sof_a.isText()) {
    obj_size = Sof::ANY_SIZE;
  }
  else {
    // AudioFile object have VectorFloat type data field
    // but the File format is VectorLong, VectorShort, VectorByte
    //
    long len = sampled_data_d.length();

    Vector<VectorByte> temp1(len);
    Vector<VectorShort> temp2(len);
    Vector<VectorLong> temp3(len);
    
    long size = 0;

    if (len > 0) {
      if (sample_num_bytes_d == (Long)sizeof(byte8)) {
	size = temp1.sofSize();
      }
      else if (sample_num_bytes_d == (Long)sizeof(int16)) {
	size = temp2.sofSize();
      }
      else if (sample_num_bytes_d == (Long)sizeof(int32)) {
	size = temp3.sofSize();
      }
    }   
    obj_size = sofSize() + size;
  }
  
  // put the object into the sof file's index
  //
  if (!sof_a.put(name_a, tag_a, obj_size)) {
    return false;
  }

  // exit gracefully
  //
  return writeData(sof_a);
}

// method: writeData
//
// arguments:
//  Sof& sof: (input) sof file object
//  const String& pname: (input) parameter name
//
// return: a boolean value indicating status
//
// this method writes the object to the Sof file. it assumes that the
// Sof file is already positioned correctly.
//
boolean AudioFile::writeData(Sof& sof_a, const String& pname_a) const {
  
  // write a start string if necessary
  //
  sof_a.writeLabelPrefix(pname_a);
    
  // write the configuration to the audiofile (sof format)
  //
  if (!writeSofConfig(sof_a)) {
    return Error::handle(name(), L"writeData", Error::IO,
			 __FILE__, __LINE__, Error::WARNING);
  }

  if (sampled_data_d.length() > 0) {

    // write the data
    //
    writeSofData(sof_a);
  }
  
  // put an end string if necessary
  //
  sof_a.writeLabelSuffix(pname_a);
  
  // exit gracefully
  //
  return true;
}

// method: writeSofStart
//
// arguments:
//  Sof& sof: (input) sof file object
//  long tag: (input) sof object instance tag
//  const String& name: (input) sof object instance name
//
// return: a boolean value indicating status
//
// this method has the object read itself from an Sof file
//
boolean AudioFile::writeSofStart(Sof& sof_a, long tag_a,
				 const String& name_a) const {

  long obj_size;
  
  if (sof_a.isText()) {
    obj_size = Sof::ANY_SIZE;
  }
  else {
    obj_size = sofSize();
  }
  
  // put the object into the sof file's index
  //
  if (!sof_a.put(name_a, tag_a, obj_size)) {
    return false;
  }

  // exit gracefully
  //
  return writeSofStartData(sof_a);
}

// method: writeSofStartData
//
// arguments:
//  Sof& sof: (input) sof file object
//  const String& pname: (input) textual parameter name
//
// return: a boolean value indicating status
//
// this method is required for the partial write
// 
boolean AudioFile::writeSofStartData(Sof& sof_a, const String& pname_a) const {
  
  // write a start string if necessary
  //
  sof_a.writeLabelPrefix(pname_a);

  // write the configuration to the audiofile (sof format)
  //
  if (!writeSofConfig(sof_a)) {
    return Error::handle(name(), L"writeSofStartData", Error::IO,
			 __FILE__, __LINE__, Error::WARNING);
  }

  // exit gracefully
  //
  return true;
}

// method: writeSofConfig
//
// arguments:
//  Sof& sof: (input) sof file object
//
// return: a boolean value indicating status
//
// this method has the object write its configuration to the Sof
// file. it assumes that the Sof file is already positioned correctly.
//
boolean AudioFile::writeSofConfig(Sof& sof_a) const {
  
  FILE_TYPE_MAP.writeElementData(sof_a,
				 PARAM_FILE_TYPE,
				 (ushort)file_type_d);
  FILE_FORMAT_MAP.writeElementData(sof_a,
				   PARAM_FILE_FORMAT,
				   (ushort)file_format_d);

  COMP_TYPE_MAP.writeElementData(sof_a,
				 PARAM_COMP_TYPE,
				 (ushort)compression_type_d);
  
  amplitude_range_d.writeData(sof_a, PARAM_RANGE);
  sample_freq_d.writeData(sof_a, PARAM_SAMPLE_FREQUENCY);
  sample_num_bytes_d.writeData(sof_a, PARAM_SAMPLE_NUM_BYTES);
  SAMPLE_PRECISION_MAP.writeElementData(sof_a, PARAM_SAMPLE_PRECISION,
                                        (ushort)sample_precision_d);

  BMODE_MAP.writeElementData(sof_a, PARAM_BYTE_ORDER, (ushort)byte_mode_d);
 
  num_channels_d.writeData(sof_a, PARAM_NUM_CHANNELS);

  tag_d.writeData(sof_a, PARAM_TAG);
  
  block_size_d.writeData(sof_a, PARAM_BLOCK_SIZE);
  buf_size_d.writeData(sof_a, PARAM_BUF_SIZE);

  id_d.writeData(sof_a, PARAM_ID);
  
  ma_coeff_d.writeData(sof_a, PARAM_MA_COEFF);
  ar_coeff_d.writeData(sof_a, PARAM_AR_COEFF);
  
  // exit gracefully
  //
  return true;
}

// method: writeSofData
//
// arguments:
//  Sof& sof: (input) sof file object
//  
// return: a boolean value indicating status
//
// this method writes the audio data to raw audio file
//
long AudioFile::writeSofData(Sof& sof_a) const {

  // combine the multi-channel data to a single Vector
  //
  VectorLong whole_data;

  Vector< VectorFloat> temp;
  temp.assign(sampled_data_d);
  
  revertData(whole_data, temp, CHANNEL_TAG_ALL);

  long num_write = whole_data.length();

  if (sample_num_bytes_d == (Long)sizeof(byte8)) {
    
    VectorByte buffer(num_write);

    for (long i = 0; i < num_write; i++) {
      buffer(i).assign(whole_data(i));
    }
      
    // call the partial write method
    //
    num_write = buffer.writeData(sof_a, PARAM_DATA);
  }
  else if (sample_num_bytes_d == (Long)sizeof(int16)) {
      
    VectorShort buffer(num_write);

    for (long i = 0; i < num_write; i++) {
      buffer(i).assign(whole_data(i));
    }
      
    // call the partial write method
    //
    num_write = buffer.writeData(sof_a, PARAM_DATA);
  }
  else if (sample_num_bytes_d == (Long)sizeof(int32)) {
      
    VectorLong buffer(num_write);

    for (long i = 0; i < num_write; i++) {
      buffer(i).assign(whole_data(i));
    }
      
    // call the partial write method
    //
    num_write = buffer.writeData(sof_a, PARAM_DATA);
  }
  else {
    return Error::handle(name(), L"writeSofData", ERR, __FILE__,
			 __LINE__);
  }
  
  // exit gracefully
  //
  return num_write;
}
