c ++ 17使用wav头验证wav文件大小

时间:2018-11-09 15:40:49

标签: c++ wav

我在验证wav标头中的wav文件大小时遇到​​问题。

enter image description here

让我们假设riff_size应该与filelength匹配,但是值不相符。

有没有一种更快的方法可以将16位的小字节序转换为大字节序,并且不允许使用除iostream,fstream和cstring之外的其他lib?

#include <iostream>
#include <fstream>
#include <cstring>

#define EXIT_SUCCESS 0
#define EXIT_FAILURE 1

struct WaveHeader {
 char riff_label[4]; // (offset 0) = {‘R’, ‘I’, ‘F’, ‘F’}
 unsigned riff_size; // (offset 4) = 36 + data_size
 char file_tag[4]; // (offset 8) = {‘W’, ‘A’, ‘V’, ‘E’}
 char fmt_label[4]; // (offset 12) = {‘f’, ‘m’,’t’, ’ ‘}
 unsigned fmt_size; // (offset 16) = 16
 unsigned short audio_format; // (offset 20) = 1
 unsigned short channel_count; // (offset 22) = 1 or 2
 unsigned sampling_rate; // (offset 24) = <anything>
 unsigned bytes_per_second; // (offset 28) = <ignore>
 unsigned short bytes_per_sample; // (offset 32) = <ignore>
 unsigned short bits_per_sample; // (offset 34) = 16
 char data_label[4]; // (offset 36) = {‘d’, ‘a’, ‘t’, ‘a’}
 unsigned data_size; // (offset 40) = <# of bytes of data>
};

std::ifstream::pos_type filesize(const char* filename)
{
    std::ifstream in(filename, std::ifstream::ate | std::ifstream::binary);
    return in.tellg(); 
}

void RunReserveProgram(const char * input , const char * output)
{
  FILE *wavFile=fopen(input, "rb");
  if (wavFile) 
  {
    WaveHeader wavHeader;
    int headerSize = sizeof(wavHeader), filelength = 0;;
    fread(&wavHeader,headerSize,1,wavFile);

    filelength = filesize(input);

    std::cout <<wavHeader.data_size <<" " << filelength << std::endl;


    if(!(wavHeader.file_tag[0] =='W' && wavHeader.file_tag[1] =='A' 
      && wavHeader.file_tag[2] =='V' && wavHeader.file_tag[3] =='E' 
      && wavHeader.riff_size == filelength ))
    {
       fclose(wavFile);
       std::cout << "bad wave file" << std::endl;
       exit(EXIT_FAILURE);
    }

    fclose(wavFile);
  }
  else
  {
    std::cout << "unable to open file \'" << input << "\'" << std::endl;
    exit(EXIT_FAILURE);
  }
}

void PrintUsage()
{
  std::cout << "Usage:" << std::endl;
  std::cout << "\treverse <wave in> <wave out> " << std::endl;
  std::cout << "where:" << std::endl;
  std::cout << "\t<wave in> -- input wave file " << std::endl;
  std::cout << "\t<wave out>-- output wave file" << std::endl;
  exit(EXIT_SUCCESS);
}

void ErrorMsg()
{
  std::cout << "incorrect number of arguments "<< std::endl;
  exit(EXIT_FAILURE);
}

int main(int argc, char** argv)
{
  if(argc == 4)
  {
    RunReserveProgram(argv[2],argv[3]);
  }
  else if(argc == 2)
  {
    PrintUsage();
  }
  else
  {
     ErrorMsg();
  }
}

此处是源wav文件 https://drive.google.com/file/d/1ynmSQkG9vMlhUz8_3wCE35QSpq0ayw3h/view?usp=sharing

1 个答案:

答案 0 :(得分:1)

来自this header format reference

  

这是该数字后面其余块的大小。 这是此计数中未包括的两个字段的整个文件的大小,以字节减去8个字节为单位:ChunkID和ChunkSize。

[重点突出]

如果我们将riff_size字段的大小88200加上36 8,那么我们得到的大小{{ 1}},即文件大小。