// file: $isip_ifc/class/sp/AudioFrontEnd/afnd_07.cc
// version: $Id: afnd_07.cc 10598 2006-08-08 21:45:21Z ss754 $
//

// isip include files
//
#include "AudioFrontEnd.h"
#include <Sdb.h>

// method: getVector
//
// arguments:
//  Vector<VectorFloat>& coeffs: (output) coefficients
//  long frame_index: (input) frame index
//  
// return: a boolean value indicating status
//
// this method retrieves data from the internal buffers.
//
boolean AudioFrontEnd::getVector(Vector<VectorFloat>& coeffs_a,
				 long frame_index_a) {
  coeffs_a.setLength(1);
  boolean status = getVector(coeffs_a(0), 0, frame_index_a);
  long nchan = buf_d.getBuffer().length();
  if (nchan > 1) {
    coeffs_a.setLength(nchan);
    for (long i = 1; i < nchan; i++) {
      status &= getVector(coeffs_a(i), i, frame_index_a);
    }
  }
  return status;
}

//MY
// method: getVector
//
// arguments:
//  Vector<AlgorithmData>& coeffs: (output) coefficients
//  long frame_index: (input) frame index
//  
// return: a boolean value indicating status
//
// this method retrieves data from the internal buffers.
//
boolean AudioFrontEnd::getVector(Vector<AlgorithmData>& coeffs_a,
				 long frame_index_a) {
  coeffs_a.setLength(1);
  boolean status = getVector(coeffs_a(0), 0, frame_index_a);
  long nchan = buf_d.getBuffer().length();
  if (nchan > 1) {
    coeffs_a.setLength(nchan);
    for (long i = 1; i < nchan; i++) {
      status &= getVector(coeffs_a(i), i, frame_index_a);
    }
  }
  return status;
}

// method: isNameInput
//
// arguments:
//  const String& name: (input) needed coefficient name
//  
// return: a boolean value indicating status
//
// this method checks whether the specified coefficient name is
// available among the inputs.
//
boolean AudioFrontEnd::isNameInput(const String& name_a) const {


  // if input file do not exist, directly return true
  //
  if (!input_flag_d && name_a.eq(SAMPLED_DATA_NAME)) {
    return true;
  }
  
  // if audio data is available allow for that name
  //
  if (audio_input_d.isOpen() && name_a.eq(SAMPLED_DATA_NAME)) {
    return true;
  }

  // check the input coefficient hash
  //
  if (feature_input_d.isOpen()) {
    String str(Recipe::INPUT_PREFIX);
    str.concat(feature_input_d.getName());
    if (str.eq(name_a)) {
      // name found
      //
      return true;
    }
  }

  // name not found
  //
  return false;
}

// method: getInputNames
//
// arguments:
//  Vector<String>& name: (output) list of inputs
//  
// return: a boolean value indicating status
//
// this method returns a list of all inputs
//
boolean AudioFrontEnd::getInputNames(Vector<String>& name_a) {

  // clear the output
  //
  name_a.clear();

  // if input file do not exist, directly copy a dummy starting point
  //
  if (!input_flag_d) {
    name_a.concat(SAMPLED_DATA_NAME);
  }

  // if audio data is available allow for that name
  //
  if (audio_input_d.isOpen()) {
    name_a.concat(SAMPLED_DATA_NAME);
  }

  // if feature data is available allow for that name
  //
  else if (feature_input_d.isOpen()) {
    name_a.concat(feature_input_d.getName());
  }

  // exit gracefully
  //
  return true;
}

// method: configureComponents
//
// arguments: none
//  
// return: a boolean value indicating status
//
// this method passes information on this data into each process
//
boolean AudioFrontEnd::configureComponents() {

  // get the signal length between users specify and real file
  // signal
  //
  long num_frames = getNumFrames();
  
  signal_duration_d = frame_duration_d * (float)num_frames;

  // if the input file exist, using input file's sample frequency
  // else get from Generator class
  //
  if (!input_flag_d) {
    // loop over all channels
    //
    for (long ctag = 0; ctag < num_channels_d; ctag++) {
      
      // loop over all lags
      //
      for (long i = 0; i < coef_components_d.length(); i++) {
	
      // loop over all processes in this list
	//
	for (boolean more = coef_components_d(i).gotoFirst();
	     more; more = coef_components_d(i).gotoNext()) {
	  
	  Component* algo = coef_components_d(i).getCurr();

	  // get the sample frequency from Generator class if it is
	  // exist
	  //
	algo->getGenSampleFrequency(sample_frequency_d);
	}
      }
    }
  }

  // loop over all channels
  //
  for (long ctag = 0; ctag < num_channels_d; ctag++) {
      
    // loop over all lags
    //
    for (long i = 0; i < coef_components_d.length(); i++) {

      // loop over all processes in this list
      //
      for (boolean more = coef_components_d(i).gotoFirst();
	   more; more = coef_components_d(i).gotoNext()) {
      
	Component* algo = coef_components_d(i).getCurr();
	
	// set the sample frequency, freq, etc
	//
	algo->setFrameDuration(frame_duration_d);
	algo->setSampleFrequency(sample_frequency_d);
	algo->setSignalDuration(signal_duration_d);
	algo->setInputFilename(input_filename_d(0));
	
	if (output_basename_d.length() > 0)
	  algo->setOutputBasename(output_basename_d);
	if (output_extension_d.length() > 0)
	  algo->setOutputExtension(output_extension_d);	
	if (output_suffix_d.length() > 0)
	  algo->setOutputSuffix(output_suffix_d);
	if (output_directory_d.length() > 0)
	  algo->setOutputDirectory(output_directory_d);	
	if ((long)output_preserve_d > 0)
	  algo->setOutputPreserve(output_preserve_d);
	
	if (output_type_flag_d)
	  algo->setOutputType(output_type_d);
	String temp;
	algo->getOutputFilename(temp);
	if (temp.length() > 0) {
	  output_filename_d.concat(temp);
	}

	temp.assign(String::NULL_STRING);
	algo->getInputFilename(temp);
	if (temp.length() > 0) {
	  input_filename_d.concat(temp);
	}	
      }
    }
  }

  // exit gracefully
  //
  return true;
}

// method: preConfigureComponents
//
// arguments: 
//  SingleLinkedList<Component>& comps: (output) components to be preconfigured
//
// return: a boolean value indicating status
//
// this method sets component information needed to calculate delays
// between components
//
boolean AudioFrontEnd::preconfigureComponents(SingleLinkedList<Component>& comps_a) {

  // get the signal length between users specify and real file
  // signal
  //
  long num_frames = getNumFrames();
  
  signal_duration_d = frame_duration_d * (float)num_frames;

  // if the input file exist, using input file's sample frequency
  // else get from Generator class
  //
  if (!input_flag_d) {
    // loop over all channels
    //
    for (long ctag = 0; ctag < num_channels_d; ctag++) {
      
      // loop over all processes in this list
      //
      for (boolean more = comps_a.gotoFirst();
	   more; more = comps_a.gotoNext()) {
	
	Component* algo = comps_a.getCurr();
	
	// get the sample frequency from Generator class if it is
	// exist
	//
	algo->getGenSampleFrequency(sample_frequency_d);
      }
    }
  }
  
  // loop over all channels
  //
  for (long ctag = 0; ctag < num_channels_d; ctag++) {
      
    // loop over all processes in this list
    //
    for (boolean more = comps_a.gotoFirst();
	 more; more = comps_a.gotoNext()) {
      
      Component* algo = comps_a.getCurr();
      
      // set the sample frequency, freq, etc
      //
      algo->setFrameDuration(frame_duration_d);
      algo->setSampleFrequency(sample_frequency_d);
      algo->setSignalDuration(signal_duration_d);
      algo->setInputFilename(input_filename_d(0));
      
      if (output_basename_d.length() > 0)
	algo->setOutputBasename(output_basename_d);
      if (output_extension_d.length() > 0)
	algo->setOutputExtension(output_extension_d);	
      if (output_suffix_d.length() > 0)
	algo->setOutputSuffix(output_suffix_d);
      if (output_directory_d.length() > 0)
	algo->setOutputDirectory(output_directory_d);	
      if ((long)output_preserve_d > 0)
	algo->setOutputPreserve(output_preserve_d);
      
      if (output_type_flag_d)
	algo->setOutputType(output_type_d);
      String temp;
      algo->getOutputFilename(temp);
      if (temp.length() > 0) {
	output_filename_d.concat(temp);
      }
      
      temp.assign(String::NULL_STRING);
      algo->getInputFilename(temp);
      if (temp.length() > 0) {
	input_filename_d.concat(temp);
      }	
    }
  }
  
  // exit gracefully
  //
  return true;
}



