// file: $isip/class/mmedia/AudioFile/adf_05.cc
// version: $Id: adf_05.cc 8173 2002-06-07 14:32:52Z gao $
//

// isip include files
//
#include <Console.h>
#include "AudioFile.h"
 
// method: clear
//
// arguments:
//  Integral::CMODE cmode: (input) clear mode
//
// return: a boolean value indicating status
//
// clears the contents of AudioFile and close the file if necessary
//
boolean AudioFile::clear(Integral::CMODE cmode_a) {

  // release resources
  //
  // we allow stdout and stderr to be deleted without closing. this is
  // necessary for the static File object's used in Console.
  //
  if ((fp_d == (FILE*)stdout) || (fp_d == (FILE*)stderr)) {
    close();
  }
  
  // close the file if it is still open
  //
  if (fp_d != (FILE*)NULL) {

    // warn the user
    //
    return Error::handle(name(), L"clear", ERR_NOTCLS,
			 __FILE__, __LINE__, Error::WARNING);

    // close the file
    //
    close();
  }
  
  resetBuffer();
  resetFilter();

  if (cmode_a >= Integral::RELEASE) {

    // delete internal memory
    //
    if (io_buf_d != (byte*)NULL) {
      scratch_mgr_d.releaseBlock(io_buf_d);
      io_buf_d = (byte*)NULL;
    }
  }

  // reset the data members
  //
  file_type_d = DEF_FILE_TYPE;
  file_format_d = DEF_FILE_FORMAT;
  compression_type_d = DEF_COMP_TYPE;
  amplitude_range_d = DEF_AMPLITUDE_RANGE;
  no_data_d = true;
  sample_freq_d = DEF_SAMPLE_FREQ;
  sample_num_bytes_d = DEF_SAMPLE_NUM_BYTES;
  num_channels_d = DEF_NUM_CHANNELS;
  sample_precision_d = DEF_SAMPLE_PRECISION;

  setMaCoeff(DEF_MA_COEFF);
  setArCoeff(DEF_AR_COEFF);
      
  // exit gracefully
  //
  return true;
}

// method: resetBuffer
//
// arguments:
//  long ctag: (input) channel to clear
//
// return: a boolean value indicating status
//
// clear out CircularBuffer, we have no good data
//
boolean AudioFile::resetBuffer(long ctag_a) {

  // process all channels
  //
  if (ctag_a < 0) {

    // loop over all channels
    //
    for (long i = 0; i < num_channels_d; i++) {
      resetBuffer(i);
    }

    // exit gracefully
    //
    return true;
  }

  // clear the buffer
  //
  buffers_d(ctag_a).clear(Integral::RESET);

  // reset time index
  //
  buf_end_samp_d(ctag_a) = -1;

  end_of_file_d = false;
  
  // exit gracefully
  //
  return true;
}

// method: resetFilter
//
// arguments:
//  long ctag: (input) channel to clear
//
// return: a boolean value indicating status
//
// clear out CircularBuffer, we have no good data
//
boolean AudioFile::resetFilter(long ctag_a) {

  // process all channels
  //
  if (ctag_a < 0) {

    // loop over all channels
    //
    for (long i = 0; i < num_channels_d; i++) {
      resetFilter(i);
    }

    // exit gracefully
    //
    return true;
  }

  // clear the filter memory
  //
  Float f0 = 0.0;
  ma_mem_d(ctag_a).clear(Integral::RESET);
  for (long i = ma_coeff_d.length() - 1; i >= 0; i--) {
    ma_mem_d(ctag_a).append(f0);
  }
  ma_mem_d(ctag_a).seekCurr(ma_mem_d(ctag_a).getNumForward());

  ar_mem_d(ctag_a).clear(Integral::RESET);
  for (long i = ar_coeff_d.length() - 1; i >= 0; i--) {
    ar_mem_d(ctag_a).append(f0);
  }
  ar_mem_d(ctag_a).seekCurr(ar_mem_d(ctag_a).getNumForward());

  // exit gracefully
  //
  return true;
}
