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

📄 adf_02.cc

📁 这是一个从音频信号里提取特征参量的程序
💻 CC
📖 第 1 页 / 共 2 页
字号:
// file: $isip/class/mmedia/AudioFile/adf_02.cc// version: $Id: adf_02.cc,v 1.15 2002/11/25 04:17:57 gao Exp $//// isip include files//#include <Console.h>#include "AudioFile.h"// method: diagnose//// arguments://  Integral::DEBUG level: (input) debug level for diagnostics//// return: a boolean value indicating status//boolean AudioFile::diagnose(Integral::DEBUG level_a) {  //---------------------------------------------------------------------------  //  // 0. preliminaries  //  //---------------------------------------------------------------------------  // output the class name  //  if (level_a > Integral::NONE) {    Filename output(L"diagnosing class ");    output.concat(CLASS_NAME);    output.concat(L": ");    Console::put(output);    Console::increaseIndention();  }    // --------------------------------------------------------------------  //  // 1. required public methods  //  // --------------------------------------------------------------------  if (level_a >= Integral::BRIEF) {    Console::put(L"testing constructors:\n");  }  AudioFile adf1(BINARY, RAW, 16000, 2, 1);  adf1.setCompType(LINEAR);  adf1.setAmplitudeRange(0.01);    if (adf1.getFileFormat() != RAW ) {    return Error::handle(name(), L"set/getFileType", Error::TEST,			 __FILE__, __LINE__);  }  if (adf1.getCompType() != LINEAR ) {    return Error::handle(name(), L"set/getCompType", Error::TEST,			 __FILE__, __LINE__);  }  if (adf1.getAmplitudeRange() != (double)0.01 ) {    return Error::handle(name(), L"set/getAmplitudeRange", Error::TEST,			 __FILE__, __LINE__);  }    if (adf1.getSampleFrequency() != 16000 ) {    return Error::handle(name(), L"set/getSampleFrequency", Error::TEST,			 __FILE__, __LINE__);  }    if (adf1.getSampleNumBytes() != 2 ) {    return Error::handle(name(), L"set/getSampleNumBytes", Error::TEST,			 __FILE__, __LINE__);  }    if (adf1.getSamplePrecision() != NONE ) {    return Error::handle(name(), L"set/getSamplePrecision", Error::TEST,			 __FILE__, __LINE__);  }    if (adf1.getNumChannels() != 1 ) {    return Error::handle(name(), L"set/getNumChannels", Error::TEST,			 __FILE__, __LINE__);  }    // test copy constructor and eq method  //  AudioFile adf2(adf1);  if (!adf2.eq(adf1)) {    adf2.debug(L"copy adf");    return Error::handle(name(), L"copy assign/ eq", Error::TEST,			 __FILE__, __LINE__);  }    // test new and delete  //  AudioFile* adf3 = new AudioFile(adf1);  delete adf3;    // --------------------------------------------------------------------  //  // 2. check Sof i/o methods  //  // --------------------------------------------------------------------  {    if (level_a >= Integral::BRIEF) {      Console::put(L"testing i/o methods:\n");    }        Filename file_text;    Filename file_bin;    Integral::makeTemp(file_text);    Integral::makeTemp(file_bin);        Sof sof0;    sof0.open(file_text, File::WRITE_ONLY, File::TEXT);    Sof sof1;    sof1.open(file_bin, File::WRITE_ONLY, File::BINARY);    // allocate an object and change some parameter to a non default    // value    //    AudioFile src;    src.setAmplitudeRange(0.01);    src.setSampleFrequency(16000);    src.sampled_data_d.setLength(1);    src.sampled_data_d(0).assign(L"1, 2, 3");;        src.write(sof0, 0);    src.write(sof1, 0);        sof0.close();    sof1.close();    sof0.open(file_text);    sof1.open(file_bin);    AudioFile dst_text;    dst_text.read(sof0, 0);    if (!dst_text.eq(src)) {      src.debug(L"before write (text)");      dst_text.debug(L"text read");      return Error::handle(name(), L"read", Error::TEST, __FILE__, __LINE__);    }        AudioFile dst_bin;    dst_bin.read(sof1, 0);        if (!dst_bin.eq(src)) {      dst_bin.debug(L"bin read");      return Error::handle(name(), L"read", Error::TEST, __FILE__, __LINE__);    }    sof0.close();    sof1.close();  }  // --------------------------------------------------------------------  //  // 3. check data i/o methods  //  // --------------------------------------------------------------------  if (level_a >= Integral::BRIEF) {    Console::put(L"testing data i/o methods (core functionality):\n");    Console::increaseIndention();  }    Filename file_text;  Filename file_bin;  Integral::makeTemp(file_text);  Integral::makeTemp(file_bin);  Filename output400;  Filename output8000;  Filename output8000a;  Filename output32000;  Filename output32000a;  Filename output200b;  Filename output_full;  Filename output_fulla;  Filename output_swap;  Filename output_swapa;  Filename output_sof;  Filename output_full_sof;  Filename output_text;    {    if (level_a >= Integral::BRIEF) {      Console::put(L"test 1: testing 400 bytes in 4 chunks:\n");    }    //  test 1: copy a 400 byte file in 4 chunks    //    Filename input(L"$ISIP_DEVEL/doc/examples/data/audio/input_400.raw");    Integral::makeTemp(output400);    File::registerTemp(output400);    AudioFile src;    src.setFileFormat(AudioFile::RAW);    src.setFileType(BINARY);    src.open(input);    AudioFile dst(BINARY, RAW);    dst.setID(src.getID());    dst.open(output400, File::WRITE_ONLY);    src.setNumChannels(1);    dst.setNumChannels(1);    // get the number of samples    //    if (src.getNumSamples() != 400) {      Console::put(L"make sure your input raw files contains 400 samples");      return Error::handle(name(), L"getNumSamples", Error::TEST,			   __FILE__, __LINE__);    }        // read the first 0.01 second samples into a vector    //    Vector<VectorFloat> data(1);    src.getData(data, 0.0, 0.01);             // 0.01 * 8000 = 80 samples        // write these 80 samples to the output    //    dst.writeAudioData(data, 0);        // read a window of samples into a vector    //    src.getWindow(data, 0.02, 0.02);    // write these 160 samples to the output    //    dst.writeAudioData(data, 0);    // read samples of a certain range into a vector    //    src.getRange(data, 0.03, 0.05);    // write these 160 samples to the output    //    dst.writeAudioData(data, 0);    if (src.getID().ne(L"input_400")) {      src.getID().debug(L"id_d");      return Error::handle(name(), L"getID", Error::TEST, __FILE__, __LINE__);    }    src.close();    dst.close();    // make sure the output file is equivalent    //    if (!File::compare(input, output400)) {      return Error::handle(name(), L"compare", Error::TEST,__FILE__,__LINE__);    }        // write this file configuration a temporary file    //    Sof sof0;    sof0.open(file_text, File::WRITE_ONLY, File::TEXT);    Sof sof1;    sof1.open(file_bin, File::WRITE_ONLY, File::BINARY);    src.write(sof0, 0);    src.write(sof1, 0);    sof0.close();    sof1.close();  }    {    if (level_a >= Integral::BRIEF) {      Console::put(L"test 2: testing 16000 bytes in 80 chunks:\n");    }    //  test 2: copy a 16000 byte file in 80 chunks. note that    // this will require more data to be buffered since the block_size    // is 4096 samples.    //    Filename input(L"$ISIP_DEVEL/doc/examples/data/audio/input_8000.raw");    Integral::makeTemp(output8000);    File::registerTemp(output8000);    AudioFile src;    src.setFileFormat(AudioFile::RAW);    src.setFileType(BINARY);    src.open(input);    AudioFile dst(BINARY, RAW);    src.setNumChannels(1);        // read the configuration from the Sof AudioFile    //    Sof sof0;    sof0.open(file_text);    dst.read(sof0, 0);    dst.setID(src.getID());    dst.open(output8000, File::WRITE_ONLY);    sof0.close();        // loop through 100 samples at a time    //    for (long i = 0; i < 80; i++) {          Vector<VectorFloat> data(1);      src.getData(data(0), 0, i * 100, 100l);      // write these 100 samples to the output      //      dst.writeAudioData(data, 0);    }    src.close();    dst.close();    // we can't compare the files since the output is an Sof file    //  }    {    if (level_a >= Integral::BRIEF) {      Console::put(L"test 2a: testing 16000 bytes in 1 chunk:\n");    }    // test 2a: copy a 16000 byte file in 1 chunk. note that    // this will require more data to be buffered since the block_size    // is 4096 samples.    //    Filename input(L"$ISIP_DEVEL/doc/examples/data/audio/input_8000.raw");    Integral::makeTemp(output8000a);    File::registerTemp(output8000a);    AudioFile src;    src.setFileFormat(AudioFile::RAW);    src.setFileType(BINARY);    src.open(input);    AudioFile dst(BINARY, RAW);    VectorFloat coeffs(L"1.0,0.0,0.0");    src.setMaCoeff(coeffs);    src.setNumChannels(1);        Sof sof0;    sof0.open(file_bin);    dst.readHeader(sof0, 0);    dst.setID(src.getID());    dst.open(output8000a, File::WRITE_ONLY);    sof0.close();    Vector<VectorFloat> data(1);    src.getData(data(0), 0, 0l, 8000);    dst.writeAudioData(data, 0);    src.close();    dst.close();    // make sure the output file is equivalent to that of the last step    //    if (!File::compare((unichar*)output8000, (unichar*)output8000a)) {      return Error::handle(name(), L"compare", Error::TEST,__FILE__,__LINE__);    }  }    {    if (level_a >= Integral::BRIEF) {      Console::put(L"test 3: testing 64000 bytes in 640 chunk:\n");    }    //  test 3: copy a 64000 sample file in 640 chunks. note    // that this will require more data to be buffered since the    // block_size is 4096 samples. this will also have to clear out    // the start of the circular buffer since.    //    Filename input(L"$ISIP_DEVEL/doc/examples/data/audio/input_32000.raw");    Integral::makeTemp(output32000);    File::registerTemp(output32000);    AudioFile src;    src.setFileFormat(AudioFile::RAW);    src.setFileType(BINARY);    src.open(input);    AudioFile dst(BINARY, RAW);    dst.setID(src.getID());    dst.open(output32000, File::WRITE_ONLY);    src.setNumChannels(1);    dst.setNumChannels(1);        // loop through 100 samples at a time    //    for (long i = 0; i < 320; i++) {          Vector<VectorFloat> data(1);      src.getData(data(0), 0, i * 100, 100l);      // write these 100 samples to the output      //      dst.writeAudioData(data, 0);    }    src.close();    dst.close();    // make sure the output file is equivalent    //    if (!File::compare(input, output32000)) {      return Error::handle(name(), L"compare", Error::TEST,__FILE__,__LINE__);    }  }  {    if (level_a >= Integral::BRIEF) {      Console::put(L"test 3a: testing 64000 bytes in 1 chunk:\n");    }    //  test 3a: copy a 16000 byte file in 1 chunk. note that    // this will require more data to be buffered since the block_size    // is 4096 samples.    //    Filename input(L"$ISIP_DEVEL/doc/examples/data/audio/input_32000.raw");    Integral::makeTemp(output32000a);        File::registerTemp(output32000a);    AudioFile src;    src.setFileFormat(AudioFile::RAW);    src.setFileType(BINARY);        src.open(input);    AudioFile dst(BINARY, RAW);    dst.setID(src.getID());    dst.open(output32000a, File::WRITE_ONLY);    if (src.file_format_d != RAW) {      return Error::handle(name(), L"type", Error::TEST, __FILE__, __LINE__);    }    if (dst.file_format_d != RAW) {      return Error::handle(name(), L"type", Error::TEST, __FILE__, __LINE__);    }        src.setNumChannels(1);    dst.setNumChannels(1);        Vector<VectorFloat> data(1);    src.getData(data(0), 0, 0l, 32000);    dst.writeAudioData(data, 0);    // now that we are at the end of the file, read the first 200    // bytes again.    //    src.getData(data(0), 0, 0l, 200);    Integral::makeTemp(output200b);    File::registerTemp(output200b);    AudioFile dstb;    dstb.setNumChannels(1);    dstb.open(output200b, File::WRITE_ONLY);    dstb.writeAudioData(data, 0);    dstb.close();        src.close();    dst.close();    // make sure the output file is equivalent    //    if (!File::compare(input, output32000a)) {      return Error::handle(name(), L"compare", Error::TEST,__FILE__,__LINE__);    }  }  {    if (level_a >= Integral::BRIEF) {      Console::put(L"test 4: testing entire file:\n");    }    //  test 4: copy the full file    //    Filename input(L"$ISIP_DEVEL/doc/examples/data/audio/little_endian.raw");    Integral::makeTemp(output_full);     File::registerTemp(output_full);    AudioFile src;    src.setFileFormat(AudioFile::RAW);    src.setFileType(BINARY);    src.open(input);    AudioFile dst(BINARY, RAW);    dst.setID(src.getID());    dst.open(output_full, File::WRITE_ONLY);    src.setNumChannels(1);    dst.setNumChannels(1);        // loop through 100 samples at a time    //    Vector<VectorFloat> data(1);    long i = 0;    while (src.getData(data(0), 0, i * 100l, 100) > 0) {            // write these 100 samples to the output      //      dst.writeAudioData(data, 0);      i++;    }    src.close();    dst.close();    // make sure the output file is equivalent    //    if (!File::compare(input, output_full)) {      return Error::handle(name(), L"compare", Error::TEST,__FILE__,__LINE__);    }  }  {    if (level_a >= Integral::BRIEF) {      Console::put(L"test 4a: testing entire file in 1 chunk:\n");    }    // test 4a: copy the full file in one chunk    //    Filename input(L"$ISIP_DEVEL/doc/examples/data/audio/little_endian.raw");

⌨️ 快捷键说明

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