I would like to open chosen WAV file, read it's header and then put audio samples from this into arrays. I have already managed to read header and I think it looks quite fine but there is one question. After header in WAV file are placed samples and I would like to know which of parameter of header specify how many of samples there are because I don't quite understand these parameters yet. Also I would like to know if it is good when SubChunk2Size is equal to 3452816845.
EDIT// As I already know that received value of SubChunk2Size is wrong, I want to know why is that and how can I correct this.
Here is page I took under consideration: http://soundfile.sapp.org/doc/WaveFormat/
Here is my code:
#include "pch.h"
#include <iostream>
#include <time.h>
#include <cstdlib>
#include <stdio.h>
#include <cmath>
#include <iostream>
#include <Windows.h>
using namespace std;
struct WAVfile {
FILE *fp;
//The "RIFF" chunk descriptor
char ChunkID[4];
DWORD ChunkSize;
char Format[4];
//The "fmt" sub-chunk
char Subchunk1ID[4];
DWORD Subchunk1Size;
short AudioFormat;
short NumChannels;
DWORD SampleRate;
DWORD ByteRate;
short BlockAlign;
short BitsPerSample;
//The "data" sub-chunk
char Subchunk2ID[4];
DWORD Subchunk2Size;
WAVfile(const char* title) {
this->fp = NULL;
fp = fopen(title, "r");
if (!fp) {
getError("Error: Failed to open file");
}
fread(ChunkID, sizeof(char), 4, fp);
if (!strcmp(ChunkID, "RIFF")) {
getError("Error: Not RIFF format");
}
fread(&ChunkSize, sizeof(DWORD), 1, fp);
fread(Format, sizeof(char), 4, fp);
if (!strcmp(Format, "WAVE")) {
getError("Error: Not WAVE format");
}
fread(Subchunk1ID, sizeof(char), 4, fp);
if (!strcmp(Subchunk1ID, "fmt ")) {
getError("Error: Not fmt");
}
fread(&Subchunk1Size, sizeof(DWORD), 1, fp);
fread(&AudioFormat, sizeof(short), 1, fp);
fread(&NumChannels, sizeof(short), 1, fp);
fread(&SampleRate, sizeof(DWORD), 1, fp);
fread(&ByteRate, sizeof(DWORD), 1, fp);
fread(&BlockAlign, sizeof(short), 1, fp);
fread(&BitsPerSample, sizeof(short), 1, fp);
fread(Subchunk2ID, sizeof(char), 4, fp);
if (!strcmp(Subchunk2ID, "data")) {
getError("Error: Missing Data");
}
fread(&Subchunk2Size, sizeof(DWORD), 1, fp);
cout << "The \"RIFF\" chunk descriptor" << endl;
cout << "ChunkID:\t\t" << ChunkID << endl;
cout << "ChunkSize:\t\t" << ChunkSize << endl;
cout << "Format:\t\t\t" << Format << endl;
cout << endl;
cout << "The \"fmt\" sub-chunk" << endl;
cout << "Subchunk1ID:\t\t" << Subchunk1ID << endl;
cout << "Subchunk1Size:\t\t" << Subchunk1Size << endl;
cout << "AudioFormat:\t\t" << AudioFormat << endl;
cout << "NumChannels:\t\t" << NumChannels << endl;
cout << "SampleRate:\t\t" << SampleRate << endl;
cout << "ByteRate:\t\t" << ByteRate << endl;
cout << "BlockAlign:\t\t" << BlockAlign << endl;
cout << "BitsPerSample:\t\t" << BitsPerSample << endl;
cout << endl;
cout << "The \"data\" sub-chunk" << endl;
cout << "Subchunk2ID:\t\t" << Subchunk2ID << endl;
cout << "Subchunk2Size:\t\t" << Subchunk2Size << endl;
char n;
cin >> n;
}
void getError(const char* message) {
cout << message << endl;
char n;
cin >> n;
}
};
int main()
{
//MOJE
//const int N = 8;// 2 * 16; // 65536 sampli na sekundę
//complex *x, *Xfft;
//complex *d_x, *d_Xfft;
//int size = N * sizeof(complex);
WAVfile *wavfile = new WAVfile("Test2.wav");
return 0;
}