I'm trying to get data from WAV file for FIR filter.That's what I have so far. Is this code reading WAV correctly? Also I need to store data into an array of double to be able to pass them to a FIR filter function later. I'm not sure how to do that.
EDIT: After reading some more information about WAV files, I now changed my code now. Does it look better now? How can I convert now an array of short of the data into an array of doubles?
struct wav_header_t
{
char chunkID[4]; //"RIFF"
unsigned long chunkSize;
char format[4]; //"WAVE"
char subchunk1ID[4]; //"fmt "
unsigned long subchunk1Size; //16
unsigned short audioFormat;
unsigned short numChannels;
unsigned long sampleRate;
unsigned long byteRate;
unsigned short blockAlign;
unsigned short bitsPerSample;
};
//Chunks
struct chunk_t
{
char ID[4]; //"data"
unsigned long size; //Chunk data bytes
};
void WAV::vRead()
{
//open file
cout << sName <<" will be open"<<endl;
FILE* handle;
fopen_s(&handle, sName, "rb");
if (handle == NULL)
{
cout << "file opened unsuccessfully" << endl;
return;
}
else
{
cout << sName << " is open" << endl;
}
//size of file.wav
fseek(handle, 0, SEEK_END);
long fileSize = ftell(handle);
//read wav-Headers
wav_header_t header;
fread(&header, sizeof(header), 1, handle);
//Reading file
chunk_t chunk;
//go to data chunk
while (true)
{
fread(&chunk, sizeof(chunk), 1, handle);
if (*(unsigned int*)&chunk.ID == 0x61746164)
break;
//skip chunk data bytes
fseek(handle, chunk.size, SEEK_CUR);
}
//Number of samples
int sample_size = header.bitsPerSample / 8;
int samples_count = chunk.size * 8 / header.bitsPerSample;
short int* value = new short int[samples_count];
memset(value, 0, sizeof(short int) * samples_count);
//Reading data
for (int i = 0; i < samples_count; i++)
{
fread(&value[i], sample_size, 1, handle);
}
fclose(handle);
return;
}