I have a problem with validating wav file size in the wav header.
Let's suppose riff_size
should match with filelength
, but the value in not tally.
Is there a faster way to convert little endian to big endian in 16 bits, with not allow using other lib, except iostream, fstream and cstring?
#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();
}
}
here the source wav file https://drive.google.com/file/d/1ynmSQkG9vMlhUz8_3wCE35QSpq0ayw3h/view?usp=sharing
From this header format reference
This is the size of the rest of the chunk following this number. This is the size of the entire file in bytes minus 8 bytes for the two fields not included in this count: ChunkID and ChunkSize.
[Emphasis mine]
If we take the size of the riff_size
field, 88200
, and add 36
and 8
then we get the size 88244
which is the file size.