1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
|
#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();
}
}
|