⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 afnd_06.cc

📁 这是一个从音频信号里提取特征参量的程序
💻 CC
📖 第 1 页 / 共 2 页
字号:
// file: $isip/class/sp/AudioFrontEnd/afnd_06.cc// version: $Id: afnd_06.cc,v 1.60 2002/08/27 20:13:09 zheng Exp $//// isip include files//#include "AudioFrontEnd.h"// method: processNextFrame//// arguments://// return: a boolean value indicating status//// this method processes the next frame of data and append to circular buffer//boolean AudioFrontEnd::processNextFrame() {  // check for end of data  //  if (!processFrame(buf_d.getLastIndex() + 1)) {        // finish out the buffers that can approximate boundaries    //    finishFrames();        return false;  }      if (buf_d.getBuffer()(0).getNumForward() > 0) {    buf_d.advanceCurr();  }  else if (buf_d.getBuffer()(0).getNumElements() == 1) {    buf_d.setFrameIndex(buf_d.getFrameIndex() + 1);  }  // advance the read pointer  //  buf_d.advanceRead();  // exit gracefully  //  return true;}// method: processFrame//// arguments://  long frame_index: (input) frame to process//  // return: a boolean value indicating status//// this method processes the given frame of data//boolean AudioFrontEnd::processFrame(long frame_index_a) {    if (coef_name_d.length() < 0) {    return Error::handle(name(), L"processFrame",			 ERR, __FILE__, __LINE__);  }  if (verbosity_d >= Integral::ALL) {    String output(L"processing frame ");    output.concat(frame_index_a);    output.concat(L":\n");    Console::put(output);    Console::increaseIndention();  }      // read input will pull the given frame out of the input and append  // it to the end of the circular buffer  //  long new_offset = -1;  long nchan = 1;  if (!input_flag_d) { // this is pseudo input for no input file    Vector<AlgorithmData> in_signal(nchan);    for (long c = 0; c < nchan; c++) {      in_signal(c).makeVectorFloat();          in_signal(c).getVectorFloat().setLength(5);      for (long i = 0; i < 5; i++) {	in_signal(c).getVectorFloat()(i) = 0;      }    }      buf_d.ensureChannels(SAMPLED_DATA_NAME, nchan);    for (long i = 0; i < nchan; i++) {      buf_d.getBuffer(SAMPLED_DATA_NAME)(i).append(in_signal(i));    }    new_offset = buf_d.getBuffer(SAMPLED_DATA_NAME)(0).getNumForward();  }  else if (!readInput(new_offset, frame_index_a)) {    return false;  }  for (long i = 0;       (i < coef_components_d.length()) && (i <= new_offset); i++) {    // process these components with this offset    //    if (!processComponents(-i, new_offset)) {      return false;    }  }  buf_d.setLastIndex(buf_d.getLastIndex() + 1);    if (verbosity_d >= Integral::ALL) {    Console::decreaseIndention();  }  // exit gracefully  //  return true;}// method: finishFrames//// arguments: none//  // return: a boolean value indicating status//// this method finishes the processing of the given frame of data//boolean AudioFrontEnd::finishFrames() {    // we now need to run the partial data through  //  long num_to_process = buf_d.getLastIndex() - buf_d.getFrameIndex();  for (long i = 0; i < num_to_process; i++) {    for (long j = i + 1; j < coef_components_d.length(); j++) {      if (!processComponents(-j, num_to_process + i + 1, true)) {	if (verbosity_d >= Integral::ALL) {	  String output(L"Out of data in frame ");	  output.concat(buf_d.getFrameIndex());	  Console::put(output);	}      }    }  }    // advance the pointers to the end  //  while (buf_d.getBuffer()(0).getNumForward() > 0) {    buf_d.advanceCurr();  }    // exit gracefully  //  return true;}// method: readInput//// arguments://  long& new_offset: (output) how far forward//  long frame_index: (input) frame to process//  // return: a boolean value indicating status//// this method reads the given frame of data//boolean AudioFrontEnd::readInput(long& new_offset_a, long frame_index_a) {  // local variables  //  boolean no_data = true;  long nchan = 1;  Vector<AlgorithmData> in_signal;    // initialize the output  //  new_offset_a = -1;  if (debug_level_d >= Integral::ALL) {    Long(frame_index_a).debug(L"frame_index_a");  }   // can we read audio data?  //  if (input_data_type_d == FrontEndBase::SAMPLED_DATA) {    nchan = audio_input_d.getNumChannels();    in_signal.setLength(nchan);    for (long c = 0; c < nchan; c++) {      in_signal(c).setCoefType(AlgorithmData::SIGNAL);    }        // we are adding data, set the flag    //    no_data = false;        // read data from the AudioFile    //    long frame_nsamp = (long)Integral::round((double)frame_duration_d				     * audio_input_d.getSampleFrequency());    // partial reading start point    //    long start_point = (long)Integral::round((double)start_time_d				     * audio_input_d.getSampleFrequency());    // partial reading end point    //    long end_point = (long)Integral::round((double)end_time_d				     * audio_input_d.getSampleFrequency());            long start_samp = (frame_index_a * frame_nsamp) + start_point;    long one_frame = frame_nsamp;    // partial reading last frame size    //    if ((start_samp + frame_nsamp > end_point) &&	(end_time_d != DEF_END_TIME) &&	(partial_process_d)) {      one_frame =  end_point - start_samp;     }        // check for end of data    //    long num_read = 0;    for (long c = 0; c < nchan; c++) {      if ((channel_index_d != DEF_CHANNEL_INDEX) &&	  (c != (long)channel_index_d)) {	if (debug_level_d >= Integral::ALL) {	  String output(L"this channel is skiped: ");	  output.concat(c);	  output.concat(L"\n");	  Console::put(output);	  continue;	}      }      num_read = audio_input_d.getData(in_signal(c).makeVectorFloat(), c,				       start_samp, one_frame);            buf_d.setLeftoverSamps(num_read);    }    if (num_read != frame_nsamp) {      if (debug_level_d >= Integral::DETAILED) {	String output(L"the file is out of data, last_samps = ");	output.concat(num_read);	output.concat(L", (");	output.concat((double)num_read / (double)frame_nsamp);	output.concat(L")\n");	Console::put(output);      }      if (num_read == 0) {	return false;      }      // pad the rest of the frame with zeros      //      for (long c = 0; c < nchan; c++) {	if ((channel_index_d != DEF_CHANNEL_INDEX) &&	    (c != (long)channel_index_d)) {	  if (debug_level_d >= Integral::ALL) {	    String output(L"this channel is skiped: ");	    output.concat(c);	    output.concat(L"\n");	    Console::put(output);	    continue;	  }	}	in_signal(c).getVectorFloat().setLength(frame_nsamp);	for (long i = num_read; i < frame_nsamp; i++) {	  in_signal(c).getVectorFloat()(i) = 0;	}      }    }    if (debug_level_d >= Integral::ALL) {      SAMPLED_DATA_NAME.debug(L"appending this coefficient");      //      in_signal.debug(L"in_signal");    }        if (channel_index_d == DEF_CHANNEL_INDEX) {      buf_d.ensureChannels(SAMPLED_DATA_NAME, nchan);    }        for (long i = 0; i < nchan; i++) {      if ((channel_index_d != DEF_CHANNEL_INDEX) &&	  (i != (long)channel_index_d)) {	if (debug_level_d >= Integral::ALL) {	  String output(L"this channel is skiped: ");	  output.concat(i);	  output.concat(L"\n");	  Console::put(output);	  continue;	}      }      else if ((channel_index_d != DEF_CHANNEL_INDEX) &&	       ( i == (long)channel_index_d)) {	buf_d.getBuffer(SAMPLED_DATA_NAME)(0).append(in_signal(i));      }      if (channel_index_d == DEF_CHANNEL_INDEX) {	buf_d.getBuffer(SAMPLED_DATA_NAME)(i).append(in_signal(i));      }    }        new_offset_a = buf_d.getBuffer(SAMPLED_DATA_NAME)(0).getNumForward();    double frame_percent = (double)num_read / (double)frame_nsamp;        if (data_mode_d == AlgorithmBase::SAMPLE_BASED &&	Integral::ceil(frame_percent) <= 0.0) {      return false;    }    else if (data_mode_d == AlgorithmBase::FRAME_BASED &&	     Integral::round(frame_percent) <= 0.0) {      return false;    }      }  // read from features file  //  else if (input_data_type_d == FrontEndBase::FEATURES) {        nchan = feature_input_d.getNumChannels();    in_signal.setLength(nchan);    long index = (frame_index_a + offset_frame_d) * nchan;    long num_read = 0;    for (long c = 0; c < nchan; c++) {      in_signal(c).makeVectorFloat();    }                num_read = feature_input_d.getBufferedData(in_signal, index, (long)nchan);    long num_frames = getNumFrames();    if (frame_index_a >= num_frames) {      if (debug_level_d >= Integral::DETAILED) {	String output(L"out of data from feature file, num_frames = ");	output.concat(num_frames);	Console::put(output);      }      return false;    }            if (debug_level_d >= Integral::DETAILED) {      Long((long)Integral::round(num_frames)).debug(L"****after getData****");    }      buf_d.setLeftoverSamps(num_read);    // we are adding data, set the flag    //    no_data = false;    String vname(Recipe::INPUT_PREFIX);    vname.concat(feature_input_d.getName());    if (channel_index_d == DEF_CHANNEL_INDEX) {      buf_d.ensureChannels(vname, nchan);    }    else {      buf_d.ensureChannels(vname, 1);    }        for (long c = 0; c < nchan; c++) {      in_signal(c).setCoefType(feature_input_d.getCoefType());      if ((channel_index_d != DEF_CHANNEL_INDEX) &&	  (c != (long)channel_index_d)) {	if (debug_level_d >= Integral::ALL) {	  String output(L"this channel is skiped: ");	  output.concat(c);	  output.concat(L"\n");	  Console::put(output);	  continue;	}      }      else if ((channel_index_d != DEF_CHANNEL_INDEX) &&	       ( c == (long)channel_index_d)) {	buf_d.getBuffer(vname)(0).append(in_signal(c));      }            if (channel_index_d == DEF_CHANNEL_INDEX) {		buf_d.getBuffer(vname)(c).append(in_signal(c));      }    }    if (debug_level_d >= Integral::ALL) {      Long(frame_index_a).debug(L"frame_index_a");      String output(L"appending coefficient <");      output.concat(vname);      output.concat(L", type = ");      output.concat(AlgorithmData::CTYPE_MAP((long)feature_input_d.getCoefType()));      output.concat(L">");      in_signal.debug(L"in_signal");      Console::put(output);    }    buf_d.setLeftoverSamps(in_signal(0).getVectorFloat().length());        long tmp = buf_d.getBuffer(vname)(0).getNumForward();    if ((new_offset_a >= 0) && (new_offset_a != tmp)) {      return Error::handle(name(), L"readInput", ERR, __FILE__, __LINE__);    }    new_offset_a = tmp;  }  else {    debug(L"FrontEnd");    return Error::handle(name(), L"readInput", Error::ARG,			 __FILE__, __LINE__, Error::WARNING);      }    // if there is no new data, we are processing from existing buffers  //  if (no_data) {    // the negative starting value for buf_d.frame_index_d() causes    // problems in the calculations, only subtract positive values to    // obtain the proper number    //    if (buf_d.getFrameIndex() < 0) {      new_offset_a = frame_index_a;    }    else {      new_offset_a = frame_index_a - buf_d.getFrameIndex();    }  }    // exit gracefully  //  return true;}// method: processComponents//// arguments://  long offset: (input) negative offset from current frame//  long pad_offset: (input) positive offset from the current frame//  boolean append: (input) should we append data to the end?//  // return: a boolean value indicating status//// this method processes the given frame of data//boolean AudioFrontEnd::processComponents(long offset_a,					 long pad_offset_a, boolean append_a) {  if ((offset_a > 0) || (-offset_a > coef_components_d.length())) {    return Error::handle(name(), L"processComponents",			 Error::ARG, __FILE__, __LINE__);  }  if (pad_offset_a < 0) {    return Error::handle(name(), L"processComponents", Error::ARG,			 __FILE__, __LINE__);  }  long num_recip = 0;  long num_skipped = 0;  boolean skip = false;  // loop over all components  //  Component* proc_ptr;  for (boolean more = coef_components_d(-offset_a).gotoFirst();       more;       more = coef_components_d(-offset_a).gotoNext()) {    num_recip++;    Vector<AlgorithmData> output;    proc_ptr = coef_components_d(-offset_a).getCurr();    if (proc_ptr == (Component*)NULL) {      return Error::handle(name(), L"processComponents",			   ERR, __FILE__, __LINE__);    }    long real_offset = offset_a + proc_ptr->getInputOffset(0) + pad_offset_a;        if (real_offset > pad_offset_a) {      return Error::handle(name(), L"processComponents",			   ERR, __FILE__, __LINE__);    }        if (real_offset < 0) {      return Error::handle(name(), L"processComponents",			   ERR, __FILE__, __LINE__);    }    // two cases: either there is only one input or there are multiple    // inputs. for a single input, we just use it directly. If there    // are multiple inputs, we need to make an AlgorithmData object in    // mode COMBINATION to hold a Vector of these inputs.    //    if (proc_ptr->getNumInputs() != 1) {      // make sure we only need a single frame per input      //      if ((proc_ptr->getLeadingPad() != 0) || 	  (proc_ptr->getTrailingPad() != 0)) {	return Error::handle(name(), L"processComponents", Error::NOT_IMPLEM,			     __FILE__, __LINE__);      }      // make sure the multiple inputs each have the same number of      // channels      //      long num_inputs = proc_ptr->getNumInputs();      long nchan = buf_d.getBuffer(proc_ptr->getInputName(0)).length();            for (long i = 1; i < num_inputs; i++) {	if (buf_d.getBuffer(proc_ptr->getInputName(i)).length() != nchan) {	  return Error::handle(name(), L"processComponents",			       ERR, __FILE__, __LINE__);	}      }      // we need to produce a Vector<CircularBuffer<Vector<AlgoData>>      //      Vector< CircularBuffer<AlgorithmData> > tmp_buf(nchan);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -